protected override void ReadNotEmpty(ReaderMethodBuilderContext context) { context.IncreaseIndexBy1(); context.AssertTypeCode(GroBufTypeCode.Guid); // Assert typeCode == TypeCode.Guid var il = context.Il; var pinnedResult = il.DeclareLocal(Type.MakeByRefType(), true); il.Ldc_I4(16); context.AssertLength(); context.LoadResultByRef(); // stack: [ref result] il.Stloc(pinnedResult); // pinnedResult = ref result il.Ldloc(pinnedResult); // stack: [&result] il.Dup(); // stack: [&result, &result] context.GoToCurrentLocation(); // stack: [&result, &result, &data[index]] il.Ldind(typeof(long)); // stack: [&result, &result, (int64)data[index]] il.Stind(typeof(long)); // *result = (int64)data[index]; stack: [&result] context.IncreaseIndexBy8(); // index = index + 8 il.Ldc_I4(8); // stack: [&result, 8] il.Add(); // stack: [&result + 8] context.GoToCurrentLocation(); // stack: [&result + 8, &data[index]] il.Ldind(typeof(long)); // stack: [&result + 8, (int64)data[index]] il.Stind(typeof(long)); // *(&result + 8) = (int64)data[index]; stack: [] context.IncreaseIndexBy8(); // index = index + 8 il.FreePinnedLocal(pinnedResult); // pinnedResult = null; stack: [] }
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 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); }
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) }