Ejemplo n.º 1
0
        protected override void WriteNotEmpty(WriterMethodBuilderContext context)
        {
            int[]   values;
            ulong[] hashCodes;
            EnumHelpers.BuildHashCodesTable(Type, out values, out hashCodes);
            var hashCodesField = context.Context.InitConstField(Type, 0, hashCodes);
            var valuesField    = context.Context.InitConstField(Type, 1, values);

            var il = context.Il;

            context.LoadField(valuesField); // stack: [values]
            context.LoadObj();              // stack: [values, obj]
            il.Ldc_I4(values.Length);       // stack: [values, obj, values.Length]
            il.Rem(true);                   // stack: [values, obj % values.Length]
            il.Ldelem(typeof(int));         // stack: [values[obj % values.Length]]
            context.LoadObj();              // stack: [values[obj % values.Length], obj]
            il.Ceq();                       // stack: [values[obj % values.Length] == obj]
            var writeIntLabel = il.DefineLabel("writeInt");

            il.Brfalse(writeIntLabel);         // if(values[obj % values.Length] != obj) goto writeInt

            context.LoadField(hashCodesField); // stack: [hashCodes]
            context.LoadObj();                 // stack: [hashCodes, obj]
            il.Ldc_I4(hashCodes.Length);       // stack: [hashCodes, obj, hashCodes.Length]
            il.Rem(true);                      // stack: [hashCodes, obj % hashCodes.Length]
            il.Ldelem(typeof(long));           // stack: [hashCodes[obj % hashCodes.Length] = hashCode]
            var hashCode = il.DeclareLocal(typeof(ulong));

            il.Dup();           // stack: [hashCode, hashCode]
            il.Stloc(hashCode); // hashCode = hashCodes[obj % hashCodes.Length]; stack: [hashCode]
            il.Brfalse(writeIntLabel);
            context.WriteTypeCode(GroBufTypeCode.Enum);
            il.Ldc_I4(8);
            context.AssertLength();
            context.GoToCurrentLocation(); // stack: [&result[index]]
            il.Ldloc(hashCode);            // stack: [&result[index], hashCode]
            il.Stind(typeof(long));        // *(int64*)&result[index] = hashCode
            context.IncreaseIndexBy8();    // index = index + 8;
            il.Ret();
            il.MarkLabel(writeIntLabel);
            context.WriteTypeCode(GroBufTypeCode.Int32);
            il.Ldc_I4(4);
            context.AssertLength();
            context.GoToCurrentLocation(); // stack: [&result[index]]
            context.LoadObj();             // stack: [&result[index], obj]
            il.Stind(typeof(int));         // result[index] = obj
            context.IncreaseIndexBy4();    // index = index + 4
        }
Ejemplo n.º 2
0
        protected override void WriteNotEmpty(WriterMethodBuilderContext context)
        {
            var customSerializerField = context.Context.InitConstField(Type, 0, customSerializer);
            var il = context.Il;

            var length = context.LocalInt;
            var start  = il.DeclareLocal(typeof(int));

            context.LoadIndexByRef();                 // stack: [ref index]
            context.LoadIndex();                      // stack: [ref index, index]
            il.Dup();                                 // stack: [ref index, index, index]
            il.Stloc(start);                          // start = index; stack: [ref index, index]
            il.Ldc_I4(5);                             // stack: [ref index, index, 5]
            il.Add();                                 // stack: [ref index, index + 5]
            il.Stind(typeof(int));                    // index = index + 5; stack: []

            context.LoadField(customSerializerField); // stack: [customSerializer]
            context.LoadObj();                        // stack: [customSerializer, obj]
            if (Type.IsValueType)
            {
                il.Box(Type);         // stack: [customSerializer, (object)obj]
            }
            context.LoadWriteEmpty(); // stack: [customSerializer, (object)obj, writeEmpty]
            context.LoadResult();     // stack: [customSerializer, (object)obj, writeEmpty, result]
            context.LoadIndexByRef(); // stack: [customSerializer, (object)obj, writeEmpty, result, ref index]
            context.LoadContext();    // stack: [customSerializer, (object)obj, writeEmpty, result, ref index, context]
            int dummy       = 0;
            var writeMethod = HackHelpers.GetMethodDefinition <IGroBufCustomSerializer>(serializer => serializer.Write(null, false, IntPtr.Zero, ref dummy, null));

            il.Call(writeMethod); // customSerializer.Write((object)obj, writeEmpty, result, ref index, context); stack: []

            context.LoadIndex();  // stack: [index]
            il.Ldloc(start);      // stack: [index, start]
            il.Sub();             // stack: [index - start]
            il.Ldc_I4(5);         // stack: [index - start, 5]
            il.Sub();             // stack: [index - start - 5]

            var writeLengthLabel = il.DefineLabel("writeLength");
            var doneLabel        = il.DefineLabel("done");

            il.Dup();                    // stack: [index - start - 5, index - start - 5]
            il.Stloc(length);            // length = index - start - 5; stack: [length]
            il.Brtrue(writeLengthLabel); // if(length != 0) goto writeLength;

            context.LoadIndexByRef();    // stack: [ref index]
            il.Ldloc(start);             // stack: [ref index, start]
            il.Stind(typeof(int));       // index = start
            context.WriteNull();

            il.MarkLabel(writeLengthLabel);
            context.LoadResult();                      // stack: [result]
            il.Ldloc(start);                           // stack: [result, start]
            il.Add();                                  // stack: [result + start]
            il.Dup();                                  // stack: [result + start, result + start]
            il.Ldc_I4((int)GroBufTypeCode.CustomData); // stack: [result + start, result + start, TypeCode.Object]
            il.Stind(typeof(byte));                    // *(result + start) = TypeCode.Object; stack: [result + start]
            il.Ldc_I4(1);                              // stack: [result + start, 1]
            il.Add();                                  // stack: [result + start + 1]
            il.Ldloc(length);                          // stack: [result + start + 1, length]
            il.Stind(typeof(int));                     // *(int*)(result + start + 1) = length
            il.MarkLabel(doneLabel);
        }
Ejemplo n.º 3
0
        protected override void WriteNotEmpty(WriterMethodBuilderContext context)
        {
            var il = context.Il;

            var writers      = GroBufHelpers.LeafTypes.Select(type1 => type1 == null ? new KeyValuePair <Delegate, IntPtr>(null, IntPtr.Zero) : GetWriter(context, type1)).ToArray();
            var writersField = context.Context.InitConstField(Type, 0, writers.Select(pair => pair.Value).ToArray());

            context.Context.InitConstField(Type, 1, writers.Select(pair => pair.Key).ToArray());

            il.Ldfld(typeof(GroBufHelpers).GetField("LeafTypeHandles", BindingFlags.Public | BindingFlags.Static)); // stack: [LeafTypeHandles]
            context.LoadObj();                                                                                      // stack: [LeafTypeHandles, obj]
            il.Call(getTypeMethod);                                                                                 // stack: [LeafTypeHandles, obj.GetType()]
            var type = il.DeclareLocal(typeof(Type));

            il.Dup();                                       // stack: [LeafTypeHandles, obj.GetType(), obj.GetType()]
            il.Stloc(type);                                 // type = obj.GetType(); stack: [LeafTypeHandles, obj.GetType()]
            il.Call(typeTypeHandleProperty.GetGetMethod()); // stack: [LeafTypeHandles, obj.GetType().TypeHandle]
            var typeHandle = il.DeclareLocal(typeof(RuntimeTypeHandle));

            il.Stloc(typeHandle);                                                              // typeHandle = obj.GetType().TypeHandle; stack: [LeafTypeHandles]
            il.Ldloca(typeHandle);                                                             // stack: [LeafTypeHandles, ref typeHandle]
            il.Call(runtimeTypeHandleValueProperty.GetGetMethod(), typeof(RuntimeTypeHandle)); // stack: [LeafTypeHandles, obj.GetType().TypeHandle.Value]
            var handle = il.DeclareLocal(typeof(IntPtr));

            il.Dup();                  // stack: [LeafTypeHandles, obj.GetType().TypeHandle.Value, obj.GetType().TypeHandle.Value]
            il.Stloc(handle);          // handle = obj.GetType().TypeHandle.Value; stack: [LeafTypeHandles, handle]
            il.Ldc_I4(writers.Length); // stack: [LeafTypeHandles, handle, LeafTypeHandles.Length]
            il.Rem(true);              // stack: [LeafTypeHandles, handle % LeafTypeHandles.Length]
            var index = il.DeclareLocal(typeof(int));

            il.Conv <int>();           // stack: [LeafTypeHandles, (int)(handle % LeafTypeHandles.Length)]
            il.Dup();                  // stack: [LeafTypeHandles, (int)(handle % LeafTypeHandles.Length), (int)(handle % LeafTypeHandles.Length)]
            il.Stloc(index);           // index = (int)(handle % LeafTypeHandles.Length); stack: [LeafTypeHandles, index]
            il.Ldelem(typeof(IntPtr)); // stack: [LeafTypeHandles[index]]
            il.Ldloc(handle);          // stack: [LeafTypeHandles[index], handle]
            var tryAsArrayLabel = il.DefineLabel("tryAsArray");

            il.Bne_Un(tryAsArrayLabel);      // if(LeafTypeHandles[index] != handle) goto tryAsArray; stack: []
            context.LoadObj();               // stack: [obj]
            context.LoadWriteEmpty();        // stack: [obj, writeEmpty]
            context.LoadResult();            // stack: [obj, writeEmpty, result]
            context.LoadIndexByRef();        // stack: [obj, writeEmpty, result, ref index]
            context.LoadContext();           // stack: [obj, writeEmpty, result, ref index, context]
            context.LoadField(writersField); // stack: [obj, writeEmpty, result, ref index, context, writers]
            il.Ldloc(index);                 // stack: [obj, writeEmpty, result, ref index, context, writers, index]
            il.Ldelem(typeof(IntPtr));       // stack: [obj, writeEmpty, result, ref index, context, writers[index]]
            var parameterTypes = new[] { typeof(object), typeof(bool), typeof(byte *), typeof(int).MakeByRefType(), typeof(WriterContext) };

            il.Calli(CallingConventions.Standard, typeof(void), parameterTypes); // stack: [writers[index](obj, writeEmpty, result, ref index, context)]
            il.Ret();

            il.MarkLabel(tryAsArrayLabel);
            il.Ldloc(type);                              // stack: [obj.GetType()]
            il.Call(typeIsArrayProperty.GetGetMethod()); // stack: [obj.GetType().IsArray]
            var writeNullLabel = il.DefineLabel("writeNull");

            il.Brfalse(writeNullLabel);
            context.LoadObj();                                                   // stack: [obj]
            context.LoadWriteEmpty();                                            // stack: [obj, writeEmpty]
            context.LoadResult();                                                // stack: [obj, writeEmpty, result]
            context.LoadIndexByRef();                                            // stack: [obj, writeEmpty, result, ref index]
            context.LoadContext();                                               // stack: [obj, writeEmpty, result, ref index, context]
            context.LoadField(writersField);                                     // stack: [obj, writeEmpty, result, ref index, context, writers]
            il.Ldc_I4(Array.IndexOf(GroBufHelpers.LeafTypes, typeof(object[]))); // stack: [obj, writeEmpty, result, ref index, context, writers, index of typeof(object[])]
            il.Ldelem(typeof(IntPtr));                                           // stack: [obj, writeEmpty, result, ref index, context, writers[index of typeof(object[])]]
            parameterTypes = new[] { typeof(object), typeof(bool), typeof(byte *), typeof(int).MakeByRefType(), typeof(WriterContext) };
            il.Calli(CallingConventions.Standard, typeof(void), parameterTypes); // stack: [writers[index of typeof(object[])](obj, writeEmpty, result, ref index, context)]
            il.Ret();

            il.MarkLabel(writeNullLabel);
            context.WriteNull();
        }