xronos /Microsoft.Scripting/Generation/GlobalStaticFieldRewriter.cs

Language C# Lines 315
MD5 Hash f0b26f48a0130af080d7612daa633712 Estimated Cost $5,038 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections.Generic;
using System.Diagnostics;
#if CODEPLEX_40
using System.Linq.Expressions;
#else
using Microsoft.Linq.Expressions;
#endif
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.Scripting.Ast;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Generation {

    /// <summary>
    /// Rewrites globals to static fields on a type
    /// Also rewrites constants to static fields
    /// </summary>
    internal sealed class GlobalStaticFieldRewriter : GlobalOptimizedRewriter {
        private readonly Dictionary<GlobalVariableExpression, FieldBuilder> _fields = new Dictionary<GlobalVariableExpression, FieldBuilder>();
        private FieldBuilder _contextField;

        // TODO: remove this static data, and switch to instance data.
        // It's only static currently because NewTypeMaker has a dependency on
        // OptimizedScriptCode.InitializeFields
        private readonly Dictionary<object, int> _constantCache = new Dictionary<object, int>(ReferenceEqualityComparer<object>.Instance);
        private List<FieldBuilder> _staticFields = new List<FieldBuilder>();
        private static readonly List<object> _staticData = new List<object>();
        private static readonly object _nullVal = new object();
        [MultiRuntimeAware]
        private static int _lastCheck, _empties;

        internal GlobalStaticFieldRewriter(TypeGen typeGen) {
            TypeGen = typeGen;
        }

        protected override Expression VisitLambda<T>(Expression<T> node) {
            // only run this for top lambda
            if (_contextField == null) {
                // Optimization: use the static field codecontext rather than
                // the argument. It's faster for the nested functions that
                // would otherwise need to close over the context argument.
                _contextField = TypeGen.TypeBuilder.DefineField(
                    CodeContext.ContextFieldName,
                    typeof(CodeContext),
                    FieldAttributes.Public | FieldAttributes.Static
                );
                Context = Expression.Field(null, _contextField);
            }
            return base.VisitLambda(node);
        }

        protected override Expression MakeWrapper(GlobalVariableExpression variable) {
            Debug.Assert(!_fields.ContainsKey(variable));

            FieldBuilder field = TypeGen.TypeBuilder.DefineField(
                variable.Name,
                typeof(ModuleGlobalWrapper),
                FieldAttributes.Assembly | FieldAttributes.Static
            );

            _fields.Add(variable, field);

            return Expression.Field(null, field);
        }

        #region runtime constant support

        protected override Expression VisitConstant(ConstantExpression node) {
            object data = node.Value;
            Type type = node.Type;

            // if the constant can be emitted into IL, nothing to do
            if (CompilerHelpers.CanEmitConstant(data, type)) {
                return node;
            }

            type = TypeUtils.GetConstantType(type);

            int index;
            if (!_constantCache.TryGetValue(data, out index)) {
                int number = AddStaticData(data);
                FieldBuilder field = TypeGen.AddStaticField(type, "#Constant" + number);
                index = _staticFields.Count;
                _staticFields.Add(field);
                _constantCache.Add(data, index);
            }

            return Expression.Field(null, _staticFields[index]);
        }

        private static int AddStaticData(object data) {
            lock (_staticData) {
                if (_empties != 0) {
                    while (_lastCheck < _staticData.Count) {
                        if (_staticData[_lastCheck] == null) {
                            _staticData[_lastCheck] = data == null ? _nullVal : data;
                            _empties--;
                            return _lastCheck;
                        }
                        _lastCheck++;
                    }
                }

                _lastCheck = 0;
                _staticData.Add(data == null ? _nullVal : data);
                return _staticData.Count - 1;
            }
        }

        internal static object GetConstantData(int index) {
            lock (_staticData) {
                object res = _staticData[index];
                _staticData[index] = null;
                _empties++;
                Debug.Assert(res != null);
                return res == _nullVal ? null : res;
            }
        }

        internal static object GetConstantDataReusable(int index) {
            lock (_staticData) {
                object res = _staticData[index];
                Debug.Assert(res != null);
                return res == _nullVal ? null : res;
            }
        }

        #endregion

        internal void EmitDictionary() {
            MakeGetMethod();
            MakeSetMethod();
            MakeRawKeysMethod();
            MakeInitialization();
        }

        #region EmitDictionary implementation

        private void MakeInitialization() {
            TypeGen.TypeBuilder.AddInterfaceImplementation(typeof(IModuleDictionaryInitialization));
            MethodInfo baseMethod = typeof(IModuleDictionaryInitialization).GetMethod("InitializeModuleDictionary");
            ILGen cg = TypeGen.DefineExplicitInterfaceImplementation(baseMethod);

            Label ok = cg.DefineLabel();
            cg.EmitFieldGet(_contextField);
            cg.Emit(OpCodes.Ldnull);
            cg.Emit(OpCodes.Ceq);
            cg.Emit(OpCodes.Brtrue_S, ok);
            cg.EmitNew(typeof(InvalidOperationException), Type.EmptyTypes);
            cg.Emit(OpCodes.Throw);
            cg.MarkLabel(ok);

            // arg0 -> this
            // arg1 -> MyModuleDictType.ContextSlot
            cg.EmitLoadArg(1);
            cg.EmitFieldSet(_contextField);

            ConstructorInfo wrapperCtor = typeof(ModuleGlobalWrapper).GetConstructor(new Type[] { typeof(CodeContext), typeof(SymbolId) });
            foreach (KeyValuePair<GlobalVariableExpression, FieldBuilder> kv in _fields) {
                // wrapper = new ModuleGlobalWrapper(context, name);
                cg.EmitLoadArg(1);
                EmitSymbolId(cg, SymbolTable.StringToId(kv.Key.Name));
                cg.Emit(OpCodes.Newobj, wrapperCtor);
                cg.Emit(OpCodes.Stsfld, kv.Value);
            }

            cg.Emit(OpCodes.Ret);
        }

        //
        // This generates a method like the following:
        //
        //  TryGetExtraValue(int name, object out value) {
        //      if (name1 == name) {
        //          value = type.name1Slot.RawValue;
        //          return value != Uninitialized.Instance;
        //      }
        //      if (name2 == name) {
        //          value = type.name2Slot.RawValue;
        //          return value != Uninitialized.Instance;
        //      }
        //      ...
        //      return false
        //  }

        private void MakeGetMethod() {
            MethodInfo baseMethod = typeof(CustomSymbolDictionary).GetMethod("TryGetExtraValue", BindingFlags.NonPublic | BindingFlags.Instance);
            ILGen cg = TypeGen.DefineMethodOverride(baseMethod);

            foreach (KeyValuePair<GlobalVariableExpression, FieldBuilder> kv in _fields) {
                SymbolId name = SymbolTable.StringToId(kv.Key.Name);

                EmitSymbolId(cg, name);
                // arg0 -> this
                cg.EmitLoadArg(1);
                cg.EmitCall(typeof(SymbolId), "op_Equality");

                Label next = cg.DefineLabel();
                cg.Emit(OpCodes.Brfalse_S, next);

                cg.EmitLoadArg(2);

                // Expects to push as an object.
                EmitGetRawFromObject(cg, kv.Value);
                cg.Emit(OpCodes.Stind_Ref);

                EmitGetRawFromObject(cg, kv.Value);
                cg.EmitFieldGet(typeof(Uninitialized), "Instance");
                cg.Emit(OpCodes.Ceq);
                cg.Emit(OpCodes.Not);
                cg.Emit(OpCodes.Ret);
                cg.MarkLabel(next);
            }
            cg.EmitInt(0);
            cg.Emit(OpCodes.Ret);
        }

        private static void EmitGetRawFromObject(ILGen cg, FieldBuilder wrapper) {
            cg.EmitFieldGet(wrapper);
            cg.EmitPropertyGet(typeof(ModuleGlobalWrapper), "RawValue");
        }

        // This generates a method like the following:
        //
        //  TrySetExtraValue(object name, object value) {
        //      if (name1 == name) {
        //          type.name1Slot = value;
        //          return 1;
        //      }
        //      if (name2 == name) {
        //          type.name2Slot = value;
        //          return 1;
        //      }
        //      ...
        //      return 0
        //  }

        private void MakeSetMethod() {
            MethodInfo baseMethod = typeof(CustomSymbolDictionary).GetMethod("TrySetExtraValue", BindingFlags.NonPublic | BindingFlags.Instance);
            ILGen cg = TypeGen.DefineMethodOverride(baseMethod);

            foreach (KeyValuePair<GlobalVariableExpression, FieldBuilder> kv in _fields) {
                SymbolId name = SymbolTable.StringToId(kv.Key.Name);

                EmitSymbolId(cg, name);
                // arg0 -> this
                cg.EmitLoadArg(1);
                cg.EmitCall(typeof(SymbolId), "op_Equality");

                Label next = cg.DefineLabel();
                cg.Emit(OpCodes.Brfalse_S, next);

                cg.EmitFieldGet(kv.Value);
                cg.EmitLoadArg(2);
                cg.EmitPropertySet(typeof(ModuleGlobalWrapper), "CurrentValue");

                cg.EmitInt(1);
                cg.Emit(OpCodes.Ret);
                cg.MarkLabel(next);
            }
            cg.EmitInt(0);
            cg.Emit(OpCodes.Ret);
        }

        private ILGen MakeRawKeysMethod() {
            FieldBuilder rawKeysCache = TypeGen.AddStaticField(typeof(SymbolId[]), "ExtraKeysCache");
            ILGen init = TypeGen.TypeInitializer;

            init.EmitInt(_fields.Count);
            init.Emit(OpCodes.Newarr, typeof(SymbolId));

            int current = 0;
            foreach (GlobalVariableExpression variable in _fields.Keys) {
                init.Emit(OpCodes.Dup);
                init.EmitInt(current++);
                EmitSymbolId(init, SymbolTable.StringToId(variable.Name));
                init.EmitStoreElement(typeof(SymbolId));
            }

            init.Emit(OpCodes.Stsfld, rawKeysCache);

            MethodInfo baseMethod = typeof(CustomSymbolDictionary).GetMethod("GetExtraKeys", BindingFlags.Public | BindingFlags.Instance);
            ILGen cg = TypeGen.DefineExplicitInterfaceImplementation(baseMethod);

            cg.Emit(OpCodes.Ldsfld, rawKeysCache);
            cg.Emit(OpCodes.Ret);
            return cg;
        }

        #endregion
    }
}
Back to Top