Ejemplo n.º 1
0
        private static KeyValuePair <Delegate, IntPtr> GetReader(ReaderMethodBuilderContext context, Type type)
        {
            var method = new DynamicMethod("Read_" + type.Name + "_AndCastToObject_" + Guid.NewGuid(), typeof(void),
                                           new[]
            {
                typeof(IntPtr), typeof(int).MakeByRefType(), typeof(object).MakeByRefType(), typeof(ReaderContext)
            }, context.Context.Module, true);

            using (var il = new GroboIL(method))
            {
                il.Ldarg(2);      // stack: [ref result]
                il.Ldarg(0);      // stack: [ref result, data]
                il.Ldarg(1);      // stack: [ref result, data, ref index]
                var value = il.DeclareLocal(type);
                il.Ldloca(value); // stack: [ref result, data, ref index, ref value]
                il.Ldarg(3);      // stack: [ref result, data, ref index, ref value, context]

                ReaderMethodBuilderContext.CallReader(il, type, context.Context);

                il.Ldloc(value); // stack: [ref result, value]
                if (type.IsValueType)
                {
                    il.Box(type); // stack: [ref result, (object)value]
                }
                else
                {
                    il.Castclass(type);
                }
                il.Stind(typeof(object)); // result = (object)value
                il.Ret();
            }
            var @delegate = method.CreateDelegate(typeof(ReaderDelegate));

            return(new KeyValuePair <Delegate, IntPtr>(@delegate, GroBufHelpers.ExtractDynamicMethodPointer(method)));
        }
Ejemplo n.º 2
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var il = context.Il;

            var readers      = GetReaders(context);
            var readersField = context.Context.InitConstField(Type, 0, readers.Select(pair => pair.Value).ToArray());

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

            context.LoadData();              // stack: [data]
            context.LoadIndexByRef();        // stack: [data, ref index]
            context.LoadResultByRef();       // stack: [data, ref index, ref result]
            context.LoadContext();           // stack: [data, ref index, ref result, context]
            context.LoadField(readersField); // stack: [data, ref index, ref result, context, readers]
            il.Ldloc(context.TypeCode);      // stack: [data, ref index, ref result, context, readers, typeCode]
            il.Ldelem(typeof(IntPtr));       // stack: [data, ref index, ref result, context, readers[typeCode]]
            il.Dup();                        // stack: [data, ref index, ref result, context, readers[typeCode], readers[typeCode]]
            var skipValueLabel = il.DefineLabel("skipValue");

            il.Brfalse(skipValueLabel); // if(readers[typeCode] == 0) goto skipValue;
            var parameterTypes = new[] { typeof(byte *), typeof(int).MakeByRefType(), typeof(object).MakeByRefType(), typeof(ReaderContext) };

            il.Calli(CallingConventions.Standard, typeof(void), parameterTypes); // readers[typeCode](data, ref index, ref result, context); stack: []
            il.Ret();
            il.MarkLabel(skipValueLabel);
            il.Pop();
            il.Pop();
            il.Pop();
            il.Pop();
            il.Pop();
            context.IncreaseIndexBy1();
            context.SkipValue();
        }
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            context.IncreaseIndexBy1();
            context.AssertTypeCode(GroBufTypeCode.DateTimeOffset); // Assert typeCode == TypeCode.DateTimeOffset
            var il = context.Il;

            context.LoadResultByRef(); // stack: [ref result]
            il.Dup();                  // stack: [ref result, ref result]
            il.Initobj(Type);          // result = default(DateTimeOffset); stack: [ref result]

            context.LoadData();        // stack: [ref result, data]
            context.LoadIndexByRef();  // stack: [ref result, data, ref index]
            var dateTime = il.DeclareLocal(typeof(DateTime));

            il.Ldloca(dateTime);                                                                   // stack: [ref result, data, ref index, ref dateTime]
            context.LoadContext();                                                                 // stack: [ref result, data, ref index, ref dateTime, context]
            context.CallReader(typeof(DateTime));                                                  // reader(pinnedData, ref index, ref dateTime, context); stack: [ref result]
            il.Dup();                                                                              // stack: [ref result, ref result]
            il.Ldloc(dateTime);                                                                    // stack: [ref result, ref result, dateTime]
            il.Stfld(Type.GetField("m_dateTime", BindingFlags.Instance | BindingFlags.NonPublic)); // result.m_dateTime = dateTime; stack: [ref result]

            context.LoadData();                                                                    // stack: [ref result, data]
            context.LoadIndexByRef();                                                              // stack: [ref result, data, ref index]
            var offset = il.DeclareLocal(typeof(short));

            il.Ldloca(offset);                                                                          // stack: [ref result, data, ref index, ref offset]
            context.LoadContext();                                                                      // stack: [ref result, data, ref index, ref offset, context]
            context.CallReader(typeof(short));                                                          // reader(pinnedData, ref index, ref offset, context); stack: [ref result]
            il.Ldloc(offset);                                                                           // stack: [ref result, ref result, offset]
            il.Stfld(Type.GetField("m_offsetMinutes", BindingFlags.Instance | BindingFlags.NonPublic)); // result.m_offsetMinutes = offset; stack: [ref result]
        }
Ejemplo n.º 4
0
        private static KeyValuePair <Delegate, IntPtr>[] GetReaders(ReaderMethodBuilderContext context)
        {
            var result = new KeyValuePair <Delegate, IntPtr> [256];

            foreach (var type in GroBufHelpers.LeafTypes.Where(type => type != null))
            {
                result[(int)GroBufTypeCodeMap.GetTypeCode(type)] = GetReader(context, type);
            }
            result[(int)GroBufTypeCode.DateTimeOld] = result[(int)GroBufTypeCode.DateTimeNew];
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Reads TypeCode at <c>data</c>[<c>index</c>] and checks it
        ///     <para></para>
        ///     Returns default(<typeparamref name="T" />) if TypeCode = Empty
        /// </summary>
        /// <param name="context">Current context</param>
        private static void ReadTypeCodeAndCheck(ReaderMethodBuilderContext context)
        {
            var il            = context.Il;
            var notEmptyLabel = il.DefineLabel("notEmpty");

            il.Ldc_I4(1);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(byte));        // stack: [data[index]]
            il.Dup();                      // stack: [data[index], data[index]]
            il.Stloc(context.TypeCode);    // typeCode = data[index]; stack: [typeCode]

            il.Brtrue(notEmptyLabel);      // if(typeCode != 0) goto notNull;

            context.IncreaseIndexBy1();    // index = index + 1
            il.Ret();

            il.MarkLabel(notEmptyLabel);

            context.CheckTypeCode();
        }
Ejemplo n.º 6
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var il = context.Il;

            context.LoadResultByRef(); // stack: [ref result]

            context.LoadData();        // stack: [ref result, data]
            context.LoadIndexByRef();  // stack: [ref result, data, ref index]
            var argumentType = Type.GetGenericArguments()[0];
            var value        = il.DeclareLocal(argumentType);

            il.Ldloca(value);                 // stack: [ref result, data, ref index, ref value]
            context.LoadContext();            // stack: [ref result, data, ref index, ref value, context]
            context.CallReader(argumentType); // reader(pinnedData, ref index, ref value, context); stack: [ref result]
            il.Ldloc(value);                  // stack: [ref result, value]
            var constructor = Type.GetConstructor(new[] { argumentType });

            if (constructor == null)
            {
                throw new MissingConstructorException(Type, argumentType);
            }
            il.Newobj(constructor); // stack: [ref result, new elementType?(value)]
            il.Stobj(Type);         // result = new elementType?(value)
        }
Ejemplo n.º 7
0
 protected abstract void ReadNotEmpty(ReaderMethodBuilderContext context);
Ejemplo n.º 8
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            context.IncreaseIndexBy1();
            context.AssertTypeCode(GroBufTypeCode.Array);

            var il     = context.Il;
            var length = context.Length;

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]

            context.AssertLength();
            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation();                   // stack: [&data[index]]
            il.Ldind(typeof(uint));                          // stack: [array length]
            context.IncreaseIndexBy4();                      // index = index + 4; stack: [array length]
            il.Stloc(length);                                // length = array length; stack: []

            context.LoadResultByRef();                       // stack: [ref result]
            il.Newobj(Type.GetConstructor(Type.EmptyTypes)); // stack: [ref result, new HashSet() = hashSet]
            il.Dup();                                        // stack: [ref result, hashSet, hashSet]
            il.Ldloc(length);                                // stack: [ref result, hashSet, hashSet, length]
            var initializeMethod = Type.GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic);

            il.Call(initializeMethod); // hashSet.Initialize(length); stack: [ref result, hashSet] or [ref result, hashSet, actualHashSetSize]
            if (initializeMethod.ReturnType != typeof(void))
            {
                il.Pop();   // stack: [ref result, hashSet]
            }
            il.Stind(Type); // result = hashSet; stack: []

            context.StoreObject(Type);

            il.Ldloc(length); // stack: [length]
            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel); // if(length == 0) goto allDone; stack: []
            var i = il.DeclareLocal(typeof(uint));

            il.Ldc_I4(0); // stack: [0]
            il.Stloc(i);  // i = 0; stack: []
            var cycleStartLabel = il.DefineLabel("cycleStart");

            il.MarkLabel(cycleStartLabel);

            context.LoadData();       // stack: [pinnedData]
            context.LoadIndexByRef(); // stack: [pinnedData, ref index]
            var value = il.DeclareLocal(elementType);

            il.Ldloca(value);                // stack: [pinnedData, ref index, ref value]
            context.LoadContext();           // stack: [pinnedData, ref index, ref value, context]
            context.CallReader(elementType); // reader(pinnedData, ref index, ref value, context); stack: []

            context.LoadResult(Type);        // stack: [result]
            il.Ldloc(value);                 // stack: [result, value]
            il.Call(Type.GetMethod("Add"));  // stack: [result.Add(value)]
            il.Pop();                        // stack: []

            if (!elementType.IsValueType)
            {
                il.Ldnull();
                il.Stloc(value);
            }

            il.Ldloc(i);                   // stack: [i]
            il.Ldc_I4(1);                  // stack: [i, 1]
            il.Add();                      // stack: [i + 1]
            il.Dup();                      // stack: [i + 1, i + 1]
            il.Stloc(i);                   // i = i + 1; stack: [i]
            il.Ldloc(length);              // stack: [i, length]
            il.Blt(cycleStartLabel, true); // if(i < length) goto cycleStart
            il.MarkLabel(doneLabel);       // stack: []
        }
Ejemplo n.º 9
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var constructor = Type.GetConstructor(new[] { typeof(long), typeof(DateTimeKind) });

            if (constructor == null)
            {
                throw new MissingConstructorException(Type, typeof(long), typeof(DateTimeKind));
            }
            var il = context.Il;

            il.Ldloc(context.TypeCode);                 // stack: [typeCode]
            il.Ldc_I4((int)GroBufTypeCode.DateTimeNew); // stack: [typeCode, GroBufTypeCode.DateTimeNew]
            var processOldFormatLabel = il.DefineLabel("processOldFormat");

            il.Bne_Un(processOldFormatLabel);  // if(typeCode != GroBufTypeCode.DateTimeNew) goto processOldFormat; stack: []
            context.IncreaseIndexBy1();
            il.Ldc_I4(8);                      // stack: [8]
            context.AssertLength();
            context.LoadResultByRef();         // stack: [ref result]
            context.GoToCurrentLocation();     // stack: [ref result, &data[index]]
            il.Ldind(typeof(long));            // stack: [ref result, (long)data[index]]
            il.Call(dateTimeFromBinaryMethod); // stack: [ref result, DateTime.FromBinary((long)data[index])]
            il.Stobj(Type);                    // result = DateTime.FromBinary((long)data[index])
            context.IncreaseIndexBy8();        // index = index + 8
            il.Ret();

            il.MarkLabel(processOldFormatLabel);
            var okLabel = il.DefineLabel("ok");

            il.Ldloc(context.TypeCode);                 // stack: [typeCode]
            il.Ldc_I4((int)GroBufTypeCode.DateTimeOld); // stack: [typeCode, GroBufTypeCode.DateTimeOld]
            il.Beq(okLabel);                            // if(typeCode == GroBufTypeCode.DateTimeOld) goto label

            il.Ldloc(context.TypeCode);                 // stack: [typeCode]
            il.Ldc_I4((int)GroBufTypeCode.Int64);       // stack: [typeCode, GroBufTypeCode.Int64]
            il.Beq(okLabel);                            // if(typeCode == GroBufTypeCode.Int64) goto label

            context.SkipValue();
            il.Ret();

            il.MarkLabel(okLabel);

            context.IncreaseIndexBy1();

            il.Ldc_I4(8); // stack: [8]
            context.AssertLength();

            context.LoadResultByRef();     // stack: [ref result]

            context.GoToCurrentLocation(); // stack: [ref result, &data[index]]
            il.Ldind(typeof(long));        // stack: [ref result, (long)&data[index] = ticks]
            il.Dup();                      // stack: [ref result, ticks, ticks]
            il.Ldc_I8(long.MinValue);      // stack: [ref result, ticks, ticks, 0x8000000000000000]
            il.And();                      // stack: [ref result, ticks, ticks & 0x8000000000000000]
            var notUtcLabel = il.DefineLabel("notUtc");

            il.Brtrue(notUtcLabel);           // if(ticks & 0x8000000000000000 != 0) goto notUtc; stack: [ref result, ticks]
            il.Ldc_I4((int)DateTimeKind.Utc); // stack: [ref result, ticks, DateTimeKind.Utc]
            il.Newobj(constructor);           // stack: [ref result, new DateTimeOld(ticks, DateTimeKind.Utc)]
            il.Stobj(Type);
            context.IncreaseIndexBy8();
            il.Ret();

            il.MarkLabel(notUtcLabel);
            context.IncreaseIndexBy8();
            il.Ldc_I4(1);
            context.AssertLength();
            il.Ldc_I8(long.MaxValue);      // stack: [ref result, ticks, 0x7FFFFFFFFFFFFFFF]
            il.And();                      // stack: [ref result, ticks & 0x7FFFFFFFFFFFFFFF]
            context.GoToCurrentLocation(); // stack: [ref result, ticks & 0x7FFFFFFFFFFFFFFF, &data[index]]
            il.Ldind(typeof(byte));        // stack: [ref result, ticks & 0x7FFFFFFFFFFFFFFF, (DateTimeKind)data[index] = kind]
            il.Newobj(constructor);        // stack: [ref result, new DateTimeOld(ticks, kind)]
            il.Stobj(Type);
            context.IncreaseIndexBy1();
        }
        protected override unsafe void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var il = context.Il;

            il.Ldloc(context.TypeCode);                          // stack: [type code]
            il.Ldc_I4((int)GroBufTypeCodeMap.GetTypeCode(Type)); // stack: [type code, GroBufTypeCode(Type)]
            var tryReadArrayElementLabel = il.DefineLabel("tryReadArrayElement");

            il.Bne_Un(tryReadArrayElementLabel); // if(type code != GroBufTypeCode(Type)) goto tryReadArrayElement; stack: []

            context.IncreaseIndexBy1();

            var size = il.DeclareLocal(typeof(int));

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [data length]
            il.Dup();                      // stack: [data length, data length]
            il.Stloc(size);                // size = data length; stack: [data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]
            context.AssertLength();

            var length = context.Length;

            il.Ldloc(size);                    // stack: [size]
            CountArrayLength(elementType, il); // stack: [array length]
            il.Stloc(length);                  // length = array length

            var array = il.DeclareLocal(elementType.MakeArrayType());

            context.LoadResultByRef(); // stack: [ref result]
            il.Dup();                  // stack: [ref result, ref result]
            il.Ldc_I4(0);              // stack: [ref result, ref result, 0]
            il.Stfld(offsetField);     // result._offset = 0; stack: [ref result]
            il.Dup();                  // stack: [ref result, ref result]
            il.Ldloc(length);          // stack: [ref result, ref result, length]
            il.Stfld(countField);      // result._count = length; stack: [ref result]
            il.Ldloc(length);          // stack: [ref result, length]
            il.Newarr(elementType);    // stack: [ref result, new type[length]]
            il.Dup();
            il.Stloc(array);           // array = new type[length]; stack: [ref result]
            il.Stfld(arrayField);      // result._array = array; stack: []

            il.Ldloc(length);
            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel); // if(length == 0) goto allDone; stack: []

            var arr = il.DeclareLocal(elementType.MakeByRefType(), true);

            il.Ldloc(array);                 // stack: [array]
            il.Ldc_I4(0);                    // stack: [array, 0]
            il.Ldelema(elementType);         // stack: [&array[0]]
            il.Stloc(arr);                   // arr = &array[0]; stack: []
            il.Ldloc(arr);                   // stack: [arr]
            context.GoToCurrentLocation();   // stack: [arr, &data[index]]
            il.Ldloc(length);                // stack: [arr, &data[index], length]
            CountArraySize(elementType, il); // stack: [arr, &data[index], size]
            il.Cpblk();                      // arr = &data[index]
            il.FreePinnedLocal(arr);         // arr = null; stack: []
            context.LoadIndexByRef();        // stack: [ref index]
            context.LoadIndex();             // stack: [ref index, index]
            il.Ldloc(size);                  // stack: [ref index, index, size]
            il.Add();                        // stack: [ref index, index + size]
            il.Stind(typeof(int));           // index = index + size
            il.Br(doneLabel);

            il.MarkLabel(tryReadArrayElementLabel);
            context.LoadResultByRef();       // stack: [ref result]
            il.Dup();                        // stack: [ref result, ref result]
            il.Ldc_I4(0);                    // stack: [ref result, ref result, 0]
            il.Stfld(offsetField);           // result._offset = 0; stack: [ref result]
            il.Dup();                        // stack: [ref result, ref result]
            il.Ldc_I4(1);                    // stack: [ref result, ref result, 1]
            il.Stfld(countField);            // result._count = 1; stack: [ref result]
            il.Ldc_I4(1);                    // stack: [ref result, 1]
            il.Newarr(elementType);          // stack: [ref result, new type[1]]
            il.Dup();
            il.Stloc(array);                 // array = new type[1]; stack: [ref result]
            il.Stfld(arrayField);            // result._array = array; stack: []

            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            il.Ldloc(array);                 // stack: [pinnedData, ref index, array]
            il.Ldc_I4(0);                    // stack: [pinnedData, ref index, array, 0]

            il.Ldelema(elementType);         // stack: [pinnedData, ref index, ref array[0]]
            context.LoadContext();           // stack: [pinnedData, ref index, ref array[0], context]

            context.CallReader(elementType); // reader(pinnedData, ref index, ref array[0], context); stack: []

            il.MarkLabel(doneLabel);         // stack: []
        }
Ejemplo n.º 11
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            int[]   values;
            ulong[] hashCodes;
            EnumHelpers.BuildValuesTable(Type, out values, out hashCodes);
            var valuesField    = context.Context.InitConstField(Type, 0, values);
            var hashCodesField = context.Context.InitConstField(Type, 1, hashCodes);
            var il             = context.Il;

            il.Ldloc(context.TypeCode); // stack: [typeCode]
            il.Ldc_I4((int)GroBufTypeCode.Enum);
            var tryParseLabel = il.DefineLabel("tryParse");

            il.Bne_Un(tryParseLabel);      // if(typeCode != GroBufTypeCode.Enum) goto tryParse;
            context.IncreaseIndexBy1();
            il.Ldc_I4(8);                  // stack: [8]
            context.AssertLength();
            context.LoadResultByRef();     // stack: [ref result]
            context.GoToCurrentLocation(); // stack: [ref result, &result[index]]
            il.Ldind(typeof(long));        // stack: [ref result, *(int64*)result[index] = hashCode]
            context.IncreaseIndexBy8();    // index = index + 8; stack: [ref result, hashCode]

            var parseByHashCodeLabel = il.DefineLabel("parseByHashCode");

            il.MarkLabel(parseByHashCodeLabel);

            il.Dup();                    // stack: [ref result, hashCode, hashCode]
            il.Ldc_I8(hashCodes.Length); // stack: [ref result, hashCode, hashCode, (int64)hashCodes.Length]
            il.Rem(true);                // stack: [ref result, hashCode, hashCode % hashCodes.Length = idx]
            il.Conv <int>();             // stack: [ref result, hashCode, (int)(hashCode % hashCodes.Length)]
            var idx = context.Length;

            il.Stloc(idx);                     // idx = (int)(hashCode % hashCodes.Length); stack: [ref result, hashCode]

            context.LoadField(hashCodesField); // stack: [ref result, hashCode, hashCodes]
            il.Ldloc(idx);                     // stack: [ref result, hashCode, hashCodes, idx]
            il.Ldelem(typeof(long));           // stack: [ref result, hashCode, hashCodes[idx]]
            var returnDefaultLabel = il.DefineLabel("returnDefault");

            il.Bne_Un(returnDefaultLabel);  // if(hashCode != hashCodes[idx]) goto returnDefault; stack: [ref result]
            context.LoadField(valuesField); // stack: [ref result, values]
            il.Ldloc(idx);                  // stack: [ref result, values, idx]
            il.Ldelem(typeof(int));         // stack: [ref result, values[idx]]
            il.Stind(typeof(int));          // result = values[idx]; stack: []
            il.Ret();
            il.MarkLabel(returnDefaultLabel);
            il.Ldc_I4(0);          // stack: [0]
            il.Stind(typeof(int)); // result = 0
            il.Ret();

            il.MarkLabel(tryParseLabel);
            il.Ldloc(context.TypeCode);            // stack: [typeCode]
            il.Ldc_I4((int)GroBufTypeCode.String); // stack: [typeCode, GroBufTypeCode.String]
            var readAsIntLabel = il.DefineLabel("readAsInt");

            il.Bne_Un(readAsIntLabel); // if(typeCode != GroBufTypeCode.String) goto readAsInt;
            var str = il.DeclareLocal(typeof(string));

            context.LoadData();                                                                              // stack: [pinnedData]
            context.LoadIndexByRef();                                                                        // stack: [pinnedData, ref index]
            il.Ldloca(str);                                                                                  // stack: [pinnedData, ref index, ref str]
            context.LoadContext();                                                                           // stack: [pinnedData, ref index, ref str, context]
            context.CallReader(typeof(string));                                                              // reader<string>(pinnedData, ref index, ref str, context); stack: []
            context.LoadResultByRef();                                                                       // stack: [ref result]
            il.Ldloc(str);                                                                                   // stack: [ref result, str]
            il.Call(typeof(GroBufHelpers).GetMethod("CalcHash", BindingFlags.Public | BindingFlags.Static)); // stack: [ref result, GroBufHelpers.CalcHash(str)]
            il.Br(parseByHashCodeLabel);

            il.MarkLabel(readAsIntLabel);

            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            context.LoadResultByRef();       // stack: [pinnedData, ref index, ref result]
            context.LoadContext();           // stack: [pinnedData, ref index, ref result, context]
            context.CallReader(typeof(int)); // reader<int>(pinnedData, ref index, ref result, context)
        }
Ejemplo n.º 12
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var il = context.Il;

            il.Ldloc(context.TypeCode);           // stack: [type code]
            il.Ldc_I4((int)GroBufTypeCode.Array); // stack: [type code, GroBufTypeCode.Array]
            var tryReadArrayElementLabel = il.DefineLabel("tryReadArrayElement");

            il.Bne_Un(tryReadArrayElementLabel); // if(type code != GroBufTypeCode.Array) goto tryReadArrayElement; stack: []

            context.IncreaseIndexBy1();
            var length = context.Length;

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]

            context.AssertLength();
            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [array length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [array length]
            il.Stloc(length);              // length = array length; stack: []

            var array = il.DeclareLocal(elementType.MakeArrayType());

            context.LoadResultByRef(); // stack: [ref result]
            il.Dup();                  // stack: [ref result, ref result]
            il.Ldc_I4(0);              // stack: [ref result, ref result, 0]
            il.Stfld(offsetField);     // result._offset = 0; stack: [ref result]
            il.Dup();                  // stack: [ref result, ref result]
            il.Ldloc(length);          // stack: [ref result, ref result, length]
            il.Stfld(countField);      // result._count = length; stack: [ref result]
            il.Ldloc(length);          // stack: [ref result, length]
            il.Newarr(elementType);    // stack: [ref result, new type[length]]
            il.Dup();
            il.Stloc(array);           // array = new type[length]; stack: [ref result]
            il.Stfld(arrayField);      // result._array = array; stack: []

            il.Ldloc(length);          // stack: [length]
            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel); // if(length == 0) goto allDone; stack: []
            var i = il.DeclareLocal(typeof(uint));

            il.Ldc_I4(0); // stack: [0]
            il.Stloc(i);  // i = 0; stack: []
            var cycleStartLabel = il.DefineLabel("cycleStart");

            il.MarkLabel(cycleStartLabel);

            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            il.Ldloc(array);                 // stack: [pinnedData, ref index, array]
            il.Ldloc(i);                     // stack: [pinnedData, ref index, array, i]

            il.Ldelema(elementType);         // stack: [pinnedData, ref index, ref array[i]]
            context.LoadContext();           // stack: [pinnedData, ref index, ref array[i], context]

            context.CallReader(elementType); // reader(pinnedData, ref index, ref array[i], context); stack: []
            il.Ldloc(i);                     // stack: [i]
            il.Ldc_I4(1);                    // stack: [i, 1]
            il.Add();                        // stack: [i + 1]
            il.Dup();                        // stack: [i + 1, i + 1]
            il.Stloc(i);                     // i = i + 1; stack: [i]
            il.Ldloc(length);                // stack: [i, length]
            il.Blt(cycleStartLabel, true);   // if(i < length) goto cycleStart
            il.Br(doneLabel);

            il.MarkLabel(tryReadArrayElementLabel);
            context.LoadResultByRef();       // stack: [ref result]
            il.Dup();                        // stack: [ref result, ref result]
            il.Ldc_I4(0);                    // stack: [ref result, ref result, 0]
            il.Stfld(offsetField);           // result._offset = 0; stack: [ref result]
            il.Dup();                        // stack: [ref result, ref result]
            il.Ldc_I4(1);                    // stack: [ref result, ref result, 1]
            il.Stfld(countField);            // result._count = 1; stack: [ref result]
            il.Ldc_I4(1);                    // stack: [ref result, 1]
            il.Newarr(elementType);          // stack: [ref result, new type[1]]
            il.Dup();
            il.Stloc(array);                 // array = new type[1]; stack: [ref result]
            il.Stfld(arrayField);            // result._array = array; stack: []

            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            il.Ldloc(array);                 // stack: [pinnedData, ref index, array]
            il.Ldc_I4(0);                    // stack: [pinnedData, ref index, array, 0]

            il.Ldelema(elementType);         // stack: [pinnedData, ref index, ref array[0]]
            context.LoadContext();           // stack: [pinnedData, ref index, ref array[0], context]

            context.CallReader(elementType); // reader(pinnedData, ref index, ref array[0], context); stack: []

            il.MarkLabel(doneLabel);         // stack: []
        }
Ejemplo n.º 13
0
        protected override void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            context.IncreaseIndexBy1();
            context.AssertTypeCode(GroBufTypeCode.Dictionary);

            var il     = context.Il;
            var length = context.Length;

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]

            context.AssertLength();
            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation();                         // stack: [&data[index]]
            il.Ldind(typeof(uint));                                // stack: [array length]
            context.IncreaseIndexBy4();                            // index = index + 4; stack: [array length]
            il.Stloc(length);                                      // length = array length; stack: []

            context.LoadResultByRef();                             // stack: [ref result]
            il.Ldloc(length);                                      // stack: [ref result, length]
            il.Newobj(Type.GetConstructor(new[] { typeof(int) })); // stack: [ref result, new Dictionary(length)]
            il.Stind(Type);                                        // result = new Dictionary(length); stack: []

            context.StoreObject(Type);

            il.Ldloc(length); // stack: [length]
            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel); // if(length == 0) goto allDone; stack: []
            var i = il.DeclareLocal(typeof(uint));

            il.Ldc_I4(0); // stack: [0]
            il.Stloc(i);  // i = 0; stack: []
            var cycleStartLabel = il.DefineLabel("cycleStart");

            il.MarkLabel(cycleStartLabel);

            context.LoadData();       // stack: [pinnedData]
            context.LoadIndexByRef(); // stack: [pinnedData, ref index]
            var key   = il.DeclareLocal(Type.GetGenericArguments()[0]);
            var value = il.DeclareLocal(Type.GetGenericArguments()[1]);

            il.Ldloca(key);                // stack: [pinnedData, ref index, ref key]
            context.LoadContext();         // stack: [pinnedData, ref index, ref key, context]
            context.CallReader(keyType);   // reader(pinnedData, ref index, ref key, context); stack: []

            context.LoadData();            // stack: [pinnedData]
            context.LoadIndexByRef();      // stack: [pinnedData, ref index]
            il.Ldloca(value);              // stack: [pinnedData, ref index, ref value]
            context.LoadContext();         // stack: [pinnedData, ref index, ref value, context]
            context.CallReader(valueType); // reader(pinnedData, ref index, ref value, context); stack: []

            context.LoadResult(Type);
            il.Ldloc(key);
            il.Ldloc(value);
            il.Call(Type.GetMethod("Add"));

            if (!keyType.IsValueType)
            {
                il.Ldnull();
                il.Stloc(key);
            }
            if (!valueType.IsValueType)
            {
                il.Ldnull();
                il.Stloc(value);
            }

            il.Ldloc(i);                   // stack: [i]
            il.Ldc_I4(1);                  // stack: [i, 1]
            il.Add();                      // stack: [i + 1]
            il.Dup();                      // stack: [i + 1, i + 1]
            il.Stloc(i);                   // i = i + 1; stack: [i]
            il.Ldloc(length);              // stack: [i, length]
            il.Blt(cycleStartLabel, true); // if(i < length) goto cycleStart
            il.MarkLabel(doneLabel);       // stack: []
        }
        protected override unsafe void ReadNotEmpty(ReaderMethodBuilderContext context)
        {
            var il = context.Il;

            il.Ldloc(context.TypeCode);                          // stack: [type code]
            il.Ldc_I4((int)GroBufTypeCodeMap.GetTypeCode(Type)); // stack: [type code, GroBufTypeCode(Type)]
            var tryReadArrayElementLabel = il.DefineLabel("tryReadArrayElement");

            il.Bne_Un(tryReadArrayElementLabel); // if(type code != GroBufTypeCode(Type)) goto tryReadArrayElement; stack: []

            context.IncreaseIndexBy1();

            var size = il.DeclareLocal(typeof(int));

            il.Ldc_I4(4);
            context.AssertLength();

            context.GoToCurrentLocation(); // stack: [&data[index]]
            il.Ldind(typeof(uint));        // stack: [data length]
            il.Dup();                      // stack: [data length, data length]
            il.Stloc(size);                // size = data length; stack: [data length]
            context.IncreaseIndexBy4();    // index = index + 4; stack: [data length]
            context.AssertLength();

            var length = context.Length;

            il.Ldloc(size);                    // stack: [size]
            CountArrayLength(elementType, il); // stack: [array length]
            il.Stloc(length);                  // length = array length

            if (context.Context.GroBufReader.Options.HasFlag(GroBufOptions.MergeOnRead))
            {
                var createArrayLabel = il.DefineLabel("createArray");
                context.LoadResult(Type);     // stack: [result]
                il.Brfalse(createArrayLabel); // if(result == null) goto createArray;
                context.LoadResult(Type);     // stack: [result]
                il.Ldlen();                   // stack: [result.Length]
                il.Ldloc(length);             // stack: [result.Length, length]

                var arrayCreatedLabel = il.DefineLabel("arrayCreated");
                il.Bge(arrayCreatedLabel, false);                     // if(result.Length >= length) goto arrayCreated;

                context.LoadResultByRef();                            // stack: [ref result]
                il.Ldloc(length);                                     // stack: [ref result, length]
                il.Call(resizeMethod.MakeGenericMethod(elementType)); // Array.Resize(ref result, length)
                il.Br(arrayCreatedLabel);                             // goto arrayCreated

                il.MarkLabel(createArrayLabel);
                context.LoadResultByRef(); // stack: [ref result]
                il.Ldloc(length);          // stack: [ref result, length]
                il.Newarr(elementType);    // stack: [ref result, new type[length]]
                il.Stind(Type);            // result = new type[length]; stack: []

                il.MarkLabel(arrayCreatedLabel);
            }
            else
            {
                context.LoadResultByRef(); // stack: [ref result]
                il.Ldloc(length);          // stack: [ref result, length]
                il.Newarr(elementType);    // stack: [ref result, new type[length]]
                il.Stind(Type);            // result = new type[length]; stack: []
            }

            il.Ldloc(length);
            var doneLabel = il.DefineLabel("done");

            il.Brfalse(doneLabel); // if(length == 0) goto allDone; stack: []

            var arr = il.DeclareLocal(elementType.MakeByRefType(), true);

            context.LoadResult(Type);        // stack: [result]
            il.Ldc_I4(0);                    // stack: [result, 0]
            il.Ldelema(elementType);         // stack: [&result[0]]
            il.Stloc(arr);                   // arr = &result[0]; stack: []
            il.Ldloc(arr);                   // stack: [arr]
            context.GoToCurrentLocation();   // stack: [arr, &data[index]]
            il.Ldloc(length);                // stack: [arr, &data[index], length]
            CountArraySize(elementType, il); // stack: [arr, &data[index], size]
            il.Cpblk();                      // arr = &data[index]
            il.FreePinnedLocal(arr);         // arr = null; stack: []
            context.LoadIndexByRef();        // stack: [ref index]
            context.LoadIndex();             // stack: [ref index, index]
            il.Ldloc(size);                  // stack: [ref index, index, size]
            il.Add();                        // stack: [ref index, index + size]
            il.Stind(typeof(int));           // index = index + size
            il.Br(doneLabel);

            il.MarkLabel(tryReadArrayElementLabel);
            if (context.Context.GroBufReader.Options.HasFlag(GroBufOptions.MergeOnRead))
            {
                var createArrayLabel = il.DefineLabel("createArray");
                context.LoadResult(Type);     // stack: [result]
                il.Brfalse(createArrayLabel); // if(result == null) goto createArray;
                context.LoadResult(Type);     // stack: [result]
                il.Ldlen();                   // stack: [result.Length]
                il.Ldc_I4(1);                 // stack: [result.Length, 1]

                var arrayCreatedLabel = il.DefineLabel("arrayCreated");
                il.Bge(arrayCreatedLabel, false);                     // if(result.Length >= 1) goto arrayCreated;

                context.LoadResultByRef();                            // stack: [ref result]
                il.Ldc_I4(1);                                         // stack: [ref result, 1]
                il.Call(resizeMethod.MakeGenericMethod(elementType)); // Array.Resize(ref result, 1)
                il.Br(arrayCreatedLabel);                             // goto arrayCreated

                il.MarkLabel(createArrayLabel);
                context.LoadResultByRef(); // stack: [ref result]
                il.Ldc_I4(1);              // stack: [ref result, 1]
                il.Newarr(elementType);    // stack: [ref result, new type[1]]
                il.Stind(Type);            // result = new type[1]; stack: []

                il.MarkLabel(arrayCreatedLabel);
            }
            else
            {
                context.LoadResultByRef(); // stack: [ref result]
                il.Ldc_I4(1);              // stack: [ref result, 1]
                il.Newarr(elementType);    // stack: [ref result, new type[1]]
                il.Stind(Type);            // result = new type[1]; stack: []
            }

            context.LoadData();              // stack: [pinnedData]
            context.LoadIndexByRef();        // stack: [pinnedData, ref index]
            context.LoadResult(Type);        // stack: [pinnedData, ref index, result]
            il.Ldc_I4(0);                    // stack: [pinnedData, ref index, result, 0]

            il.Ldelema(elementType);         // stack: [pinnedData, ref index, ref result[0]]
            context.LoadContext();           // stack: [pinnedData, ref index, ref result[0], context]

            context.CallReader(elementType); // reader(pinnedData, ref index, ref result[0], context); stack: []

            il.MarkLabel(doneLabel);         // stack: []
        }