protected override void CountSizeNotEmpty(SizeCounterMethodBuilderContext 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 countAsIntLabel = il.DefineLabel("countAsInt"); il.Brfalse(countAsIntLabel); // if(values[obj % values.Length] != obj) goto countAsInt 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] il.Brfalse(countAsIntLabel); // if(hashCode == 0) goto countAsInt; il.Ldc_I4(9); // stack: [9] il.Ret(); // return 9; il.MarkLabel(countAsIntLabel); il.Ldc_I4(5); // stack: [5] }
protected override void CountSizeNotEmpty(SizeCounterMethodBuilderContext context) { var customSerializerField = context.Context.InitConstField(Type, 0, customSerializer); var il = context.Il; 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.LoadContext(); // stack: [customSerializer, (object)obj, writeEmpty, context] il.Call(HackHelpers.GetMethodDefinition <IGroBufCustomSerializer>(serializer => serializer.CountSize(null, false, null))); // stack: [customSerializer.CountSize((object)obj, writeEmpty, context)] var countLengthLabel = il.DefineLabel("countLength"); il.Dup(); // stack: [size, size] il.Brtrue(countLengthLabel); // if(size != 0) goto countLength; stack: [size] il.Pop(); // stack: [] context.ReturnForNull(); il.Ret(); il.MarkLabel(countLengthLabel); il.Ldc_I4(5); // stack: [size, 5] il.Add(); // stack: [size + 5] }
protected override void CountSizeNotEmpty(SizeCounterMethodBuilderContext context) { var il = context.Il; var counters = GroBufHelpers.LeafTypes.Select(type1 => type1 == null ? new KeyValuePair <Delegate, IntPtr>(null, IntPtr.Zero) : GetCounter(context, type1)).ToArray(); var countersField = context.Context.InitConstField(Type, 0, counters.Select(pair => pair.Value).ToArray()); context.Context.InitConstField(Type, 1, counters.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(counters.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.LoadContext(); // stack: [obj, writeEmpty, context] context.LoadField(countersField); // stack: [obj, writeEmpty, context, counters] il.Ldloc(index); // stack: [obj, writeEmpty, context, counters, index] il.Ldelem(typeof(IntPtr)); // stack: [obj, writeEmpty, context, counters[index]] var parameterTypes = new[] { typeof(object), typeof(bool), typeof(WriterContext) }; il.Calli(CallingConventions.Standard, typeof(int), parameterTypes); // stack: [counters[index](obj, writeEmpty)] il.Ret(); il.MarkLabel(tryAsArrayLabel); il.Ldloc(type); // stack: [obj.GetType()] il.Call(typeIsArrayProperty.GetGetMethod()); // stack: [obj.GetType().IsArray] var returnForNullLabel = il.DefineLabel("returnForNull"); il.Brfalse(returnForNullLabel); context.LoadObj(); // stack: [obj] context.LoadWriteEmpty(); // stack: [obj, writeEmpty] context.LoadContext(); // stack: [obj, writeEmpty, context] context.LoadField(countersField); // stack: [obj, writeEmpty, context, counters] il.Ldc_I4(Array.IndexOf(GroBufHelpers.LeafTypes, typeof(object[]))); // stack: [obj, writeEmpty, context, counters, index of typeof(object[])] il.Ldelem(typeof(IntPtr)); // stack: [obj, writeEmpty, context, counters[index of typeof(object[])]] parameterTypes = new[] { typeof(object), typeof(bool), typeof(WriterContext) }; il.Calli(CallingConventions.Standard, typeof(int), parameterTypes); // stack: [counters[index of typeof(object[])](obj, writeEmpty)] il.Ret(); il.MarkLabel(returnForNullLabel); context.ReturnForNull(); }