protected override void ReadNotEmpty(ReaderMethodBuilderContext context) { context.IncreaseIndexBy1(); context.AssertTypeCode(GroBufTypeCodeMap.GetTypeCode(elementType.MakeArrayType())); var il = context.Il; 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 count = context.Length; il.Ldloc(size); // stack: [size] CountArrayLength(elementType, il); // stack: [array length] il.Stloc(count); // count = array length 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(count); // stack: [ref result, hashSet, hashSet, count] var initializeMethod = Type.GetMethod("Initialize", BindingFlags.Instance | BindingFlags.NonPublic); il.Call(initializeMethod); // hashSet.Initialize(count); 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: [] il.Ldloc(count); var doneLabel = il.DefineLabel("done"); il.Brfalse(doneLabel); // if(count == 0) goto allDone; stack: [] var i = il.DeclareLocal(typeof(int)); il.Ldc_I4(0); // stack: [0] il.Stloc(i); // i = 0; stack: [] var cycleStartLabel = il.DefineLabel("cycleStart"); il.MarkLabel(cycleStartLabel); context.LoadResult(Type); // stack: [result] context.GoToCurrentLocation(); // stack: [result, &data[index]] il.Ldloc(i); // stack: [result, &data[index], i] MakeShift(elementType, il); // stack: [result, &data[index], i << x] il.Add(); // stack: [result, current] il.Ldind(elementType); // stack: [result, *current] il.Call(Type.GetMethod("Add")); // stack: [result.Add(*current)] il.Pop(); // stack: [] il.Ldloc(count); // stack: [count] il.Ldloc(i); // stack: [current, count, i] il.Ldc_I4(1); // stack: [current, count, i, 1] il.Add(); // stack: [current, count, i + 1] il.Dup(); // stack: [current, count, i + 1, i + 1] il.Stloc(i); // i = i + 1; stack: [current, count, i] il.Bgt(cycleStartLabel, false); // if(count > i) goto cycleStart; stack: [current] 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.MarkLabel(doneLabel); // stack: [] }
protected override unsafe void ReadNotEmpty(ReaderMethodBuilderContext context) { context.IncreaseIndexBy1(); context.AssertTypeCode(GroBufTypeCodeMap.GetTypeCode(elementType.MakeArrayType())); var il = context.Il; 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; stack: [] context.LoadResult(Type); // stack: [result] il.Ldfld(Type.GetField("_items", BindingFlags.Instance | BindingFlags.NonPublic)); // stack: [result._items] il.Ldlen(); // stack: [result._items.Length] il.Ldloc(length); // stack: [result.Length, length] var arrayCreatedLabel = il.DefineLabel("arrayCreated"); il.Bge(arrayCreatedLabel, false); // if(result.Length >= length) goto arrayCreated; context.LoadResult(Type); // stack: [result] il.Ldflda(Type.GetField("_items", BindingFlags.Instance | BindingFlags.NonPublic)); // stack: [ref result._items] 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.Newobj(Type.GetConstructor(new[] { typeof(int) })); // stack: [ref result, new List(length)] il.Stind(Type); // result = new List(length); stack: [] il.MarkLabel(arrayCreatedLabel); } else { context.LoadResultByRef(); // stack: [ref result] il.Ldloc(length); // stack: [ref result, length] il.Newobj(Type.GetConstructor(new[] { typeof(int) })); // stack: [ref result, new List(length)] il.Stind(Type); // result = new List(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.Ldfld(Type.GetField("_items", BindingFlags.Instance | BindingFlags.NonPublic)); // stack: [result._items] il.Ldc_I4(0); // stack: [result._items, 0] il.Ldelema(elementType); // stack: [&result._items[0]] il.Stloc(arr); // arr = &result._items[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 if (context.Context.GroBufReader.Options.HasFlag(GroBufOptions.MergeOnRead)) { context.LoadResult(Type); // stack: [result] il.Ldfld(Type.GetField("_size", BindingFlags.Instance | BindingFlags.NonPublic)); // stack: [result.Count] il.Ldloc(length); // stack: [result.Count, length] il.Bge(doneLabel, false); // if(result.Count >= length) goto done; stack: [] } context.LoadResult(Type); // stack: [result] il.Ldloc(length); // stack: [result.Count, length] il.Stfld(Type.GetField("_size", BindingFlags.Instance | BindingFlags.NonPublic)); // result._size = length; stack: [] il.MarkLabel(doneLabel); // stack: [] }
protected override void ReadNotEmpty(ReaderMethodBuilderContext context) { MemberInfo[] dataMembers; ulong[] hashCodes; BuildMembersTable(context.Context, out hashCodes, out dataMembers); var il = context.Il; var end = context.Length; var typeCode = context.TypeCode; var setters = dataMembers.Select(member => member == null ? default(KeyValuePair <Delegate, IntPtr>) : GetMemberSetter(context.Context, member)).ToArray(); var settersField = context.Context.InitConstField(Type, 0, setters.Select(pair => pair.Value).ToArray()); context.Context.InitConstField(Type, 1, setters.Select(pair => pair.Key).ToArray()); var hashCodesField = context.Context.InitConstField(Type, 2, hashCodes); context.IncreaseIndexBy1(); // index = index + 1 context.AssertTypeCode(GroBufTypeCode.Object); il.Ldc_I4(4); context.AssertLength(); context.GoToCurrentLocation(); // stack: [&data[index]] il.Ldind(typeof(uint)); // stack: [(uint)data[index] = data length] context.IncreaseIndexBy4(); // index = index + 4; stack: [data length] il.Dup(); // stack: [data length, data length] il.Stloc(end); // end = data length; stack: [data length] if (!Type.IsValueType) { context.LoadResultByRef(); // stack: [data length, ref result] il.Ldind(Type); // stack: [data length, result] var notNullLabel = il.DefineLabel("notNull"); il.Brtrue(notNullLabel); // if(result != null) goto notNull; stack: [data length] context.LoadResultByRef(); // stack: [data length, ref result] ObjectConstructionHelper.EmitConstructionOfType(Type, il); il.Stind(Type); // result = new type(); stack: [data length] il.MarkLabel(notNullLabel); } context.StoreObject(Type); var doneLabel = il.DefineLabel("done"); il.Brfalse(doneLabel); // if(data length == 0) goto done; stack: [] il.Ldloc(end); // stack: [data length] context.AssertLength(); // stack: [] il.Ldloc(end); // stack: [data length] context.LoadIndex(); // stack: [data length, index] il.Add(); // stack: [data length + index] il.Stloc(end); // end = data length + index var cycleStartLabel = il.DefineLabel("cycleStart"); il.MarkLabel(cycleStartLabel); il.Ldc_I4(9); context.AssertLength(); context.GoToCurrentLocation(); // stack: [&data[index]] il.Ldind(typeof(long)); // stack: [*(int64*)&data[index] = hashCode] context.IncreaseIndexBy8(); // index = index + 8; stack: [*(int64*)&data[index] = hashCode] il.Dup(); // stack: [hashCode, hashCode] il.Ldc_I8(dataMembers.Length); // stack: [hashCode, hashCode, (int64)hashCodes.Length] il.Rem(true); // stack: [hashCode, hashCode % hashCodes.Length] il.Conv <int>(); // stack: [hashCode, (int)(hashCode % hashCodes.Length)] var idx = il.DeclareLocal(typeof(int)); il.Stloc(idx); // idx = (int)(hashCode % hashCodes.Length); stack: [hashCode] context.LoadField(hashCodesField); // stack: [hashCode, hashCodes] il.Ldloc(idx); // stack: [hashCode, hashCodes, idx] il.Ldelem(typeof(long)); // stack: [hashCode, hashCodes[idx]] var skipDataLabel = il.DefineLabel("skipData"); il.Bne_Un(skipDataLabel); // if(hashCode != hashCodes[idx]) goto skipData; stack: [] // Read data 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.LoadField(settersField); // stack: [pinnedData, ref index, ref result, context, setters] il.Ldloc(idx); // stack: [pinnedData, ref index, ref result, context, setters, idx] il.Ldelem(typeof(IntPtr)); // stack: [pinnedData, ref index, ref result, context, setters[idx]] var parameterTypes = new[] { typeof(byte *), typeof(int).MakeByRefType(), Type.MakeByRefType(), typeof(ReaderContext) }; il.Calli(CallingConventions.Standard, typeof(void), parameterTypes); // setters[idx](pinnedData, ref index, ref result, context); stack: [] var checkIndexLabel = il.DefineLabel("checkIndex"); il.Br(checkIndexLabel); // goto checkIndex il.MarkLabel(skipDataLabel); // Skip data context.GoToCurrentLocation(); // stack: [&data[index]] il.Ldind(typeof(byte)); // stack: [data[index]] il.Stloc(typeCode); // typeCode = data[index]; stack: [] context.IncreaseIndexBy1(); // index = index + 1 context.CheckTypeCode(); context.SkipValue(); il.MarkLabel(checkIndexLabel); context.LoadIndex(); // stack: [index] il.Ldloc(end); // stack: [index, end] il.Blt(cycleStartLabel, true); // if(index < end) goto cycleStart; stack: [] var onDeserializedMethod = Type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .SingleOrDefault(method => method.GetCustomAttribute <OnDeserializedAttribute>() != null); if (onDeserializedMethod != null) { var parameters = onDeserializedMethod.GetParameters(); if (parameters.Length != 1 || parameters[0].ParameterType != typeof(StreamingContext)) { throw new InvalidOperationException(string.Format("The method '{0}' marked with 'OnDeserialized' attribute must accept exactly one parameter of type '{1}'", onDeserializedMethod, typeof(StreamingContext).FullName)); } context.LoadResult(Type); il.Ldc_I4((int)StreamingContextStates.Other); il.Newobj(typeof(StreamingContext).GetConstructor(new[] { typeof(StreamingContextStates) })); il.Call(onDeserializedMethod); } il.MarkLabel(doneLabel); }
public void BuildReader(ReaderTypeBuilderContext readerTypeBuilderContext) { var method = new DynamicMethod("Read_" + Type.Name + "_" + Guid.NewGuid(), typeof(void), new[] { typeof(IntPtr), typeof(int).MakeByRefType(), Type.MakeByRefType(), typeof(ReaderContext) }, readerTypeBuilderContext.Module, true); readerTypeBuilderContext.SetReaderMethod(Type, method); using (var il = new GroboIL(method)) { var context = new ReaderMethodBuilderContext(readerTypeBuilderContext, il, !Type.IsValueType && IsReference); ReadTypeCodeAndCheck(context); // Read TypeCode and check if (!Type.IsValueType && IsReference) { // Read reference context.LoadContext(); // stack: [context] il.Ldfld(ReaderContext.ObjectsField); // stack: [context.objects] var notReadLabel = il.DefineLabel("notRead"); il.Brfalse(notReadLabel); context.LoadIndex(); // stack: [external index] context.LoadContext(); // stack: [external index, context] il.Ldfld(ReaderContext.StartField); // stack: [external index, context.start] il.Sub(); // stack: [external index - context.start] il.Stloc(context.Index); // index = external index - context.start; stack: [] context.LoadContext(); // stack: [context] il.Ldfld(ReaderContext.ObjectsField); // stack: [context.objects] il.Ldloc(context.Index); // stack: [context.objects, index] var obj = il.DeclareLocal(typeof(object)); il.Ldloca(obj); object dummy; il.Call(HackHelpers.GetMethodDefinition <Dictionary <int, object> >(dict => dict.TryGetValue(0, out dummy))); // stack: [context.objects.TryGetValue(index, out obj)] il.Brfalse(notReadLabel); // if(!context.objects.TryGetValue(index, out obj)) goto notRead; context.LoadResultByRef(); // stack: [ref result] il.Ldloc(obj); // stack: [ref result, obj] il.Castclass(Type); // stack: [ref result, (Type)obj] il.Stind(Type); // result = (Type)obj; stack: [] context.IncreaseIndexBy1(); // Skip type code context.SkipValue(); // Skip value - it has already been read il.Ret(); il.MarkLabel(notReadLabel); il.Ldloc(context.TypeCode); // stack: [typeCode] il.Ldc_I4((int)GroBufTypeCode.Reference); // stack: [typeCode, GroBufTypeCode.Reference] var readUsualLabel = il.DefineLabel("readUsual"); il.Bne_Un(readUsualLabel); // if(typeCode != GroBufTypeCode.Reference) goto readUsual; stack: [] context.LoadContext(); // stack: [context] il.Ldfld(ReaderContext.ObjectsField); // stack: [context.objects] var objectsIsNotNullLabel = il.DefineLabel("objectsIsNotNull"); il.Brtrue(objectsIsNotNullLabel); // if(context.objects != null) goto objectsIsNotNull; stack: [context.objects] il.Ldstr("Reference is not valid at this point"); il.Newobj(typeof(DataCorruptedException).GetConstructor(new[] { typeof(string) })); il.Throw(); il.MarkLabel(objectsIsNotNullLabel); context.IncreaseIndexBy1(); // index = index + 1; stack: [] il.Ldc_I4(4); context.AssertLength(); context.GoToCurrentLocation(); var reference = il.DeclareLocal(typeof(int)); il.Ldind(typeof(int)); // stack: [*(int*)data[index]] il.Stloc(reference); // reference = *(int*)data[index]; stack: [] context.IncreaseIndexBy4(); // index = index + 4; stack: [] il.Ldloc(context.Index); // stack: [index] il.Ldloc(reference); // stack: [index, reference] var goodReferenceLabel = il.DefineLabel("goodReference"); il.Bgt(goodReferenceLabel, false); // if(index > reference) goto goodReference; stack: [] il.Ldstr("Bad reference"); il.Newobj(typeof(DataCorruptedException).GetConstructor(new[] { typeof(string) })); il.Throw(); il.MarkLabel(goodReferenceLabel); context.LoadContext(); // stack: [context] il.Ldfld(ReaderContext.ObjectsField); // stack: [context.objects] il.Ldloc(reference); // stack: [context.objects, reference] il.Ldloca(obj); // stack: [context.objects, reference, ref obj] il.Call(HackHelpers.GetMethodDefinition <Dictionary <int, object> >(dict => dict.TryGetValue(0, out dummy))); // stack: [context.objects.TryGetValue(reference, out obj)] var readObjectLabel = il.DefineLabel("readObject"); il.Brfalse(readObjectLabel); // if(!context.objects.TryGetValue(reference, out obj)) goto readObjects; stack: [] context.LoadResultByRef(); // stack: [ref result] il.Ldloc(obj); // stack: [ref result, obj] il.Castclass(Type); // stack: [ref result, (Type)obj] il.Stind(Type); // result = (Type)obj; stack: [] il.Ret(); il.MarkLabel(readObjectLabel); // Referenced object has not been read - this means that the object reference belongs to is a property that had been deleted context.LoadData(); // stack: [data] il.Ldloc(reference); // stack: [data, reference] context.LoadContext(); // stack: [data, reference, context] il.Ldfld(ReaderContext.StartField); // stack: [data, reference, context.start] il.Add(); // stack: [data, reference + context.start] il.Stloc(reference); // reference += context.start; stack: [data] il.Ldloca(reference); // stack: [data, ref reference] context.LoadResultByRef(); // stack: [data, ref reference, ref result] context.LoadContext(); // stack: [data, ref reference, ref result, context] context.CallReader(Type); il.Ret(); il.MarkLabel(readUsualLabel); } ReadNotEmpty(context); // Read obj il.Ret(); } var @delegate = method.CreateDelegate(typeof(ReaderDelegate <>).MakeGenericType(Type)); var pointer = GroBufHelpers.ExtractDynamicMethodPointer(method); readerTypeBuilderContext.SetReaderPointer(Type, pointer, @delegate); }
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: [] }
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: [] }