private void Evaluate(TracingILGenerator il, bool shouldBeAddress) { if (!this._isBound) { this._binding.Evaluate(il); this._isBound = true; } this._expression.LoadValue(il, shouldBeAddress); }
public override void Evaluate(TracingILGenerator il) { il.TraceWriteLine("// Eval->: {0}", this); this.DoConditionalInstruction( il, () => this._thenExpression.Evaluate(il), () => this._elseExpression.Evaluate(il) ); il.TraceWriteLine("// ->Eval: {0}", this); }
public override void StoreValue(TracingILGenerator il) { il.TraceWriteLine("// Stor->: {0}", this); if (!this._isLocal) { throw new InvalidOperationException("Cannot overwrite argument."); } il.EmitAnyStloc(this._index); il.TraceWriteLine("// ->Stor: {0}", this); }
private static void EmitLoadTarget(Type targetType, TracingILGenerator il, LocalBuilder local) { if (targetType.IsValueType) { il.EmitAnyLdloca(local); } else { il.EmitAnyLdloc(local); } }
private static void EmitLoadTarget(Type targetType, TracingILGenerator il, int parameterIndex) { if (targetType.IsValueType) { il.EmitAnyLdarga(parameterIndex); } else { il.EmitAnyLdarg(parameterIndex); } }
public static void EmitGeneralRead(TracingILGenerator il, int unpackerArgumentIndex) { il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitAnyCall(_Unpacker.Read); Label target = il.DefineLabel("END_IF"); il.EmitBrtrue_S(target); il.EmitAnyCall(SerializationExceptions.NewUnexpectedEndOfStreamMethod); il.EmitThrow(); il.MarkLabel(target); }
public override void Evaluate(TracingILGenerator il) { il.TraceWriteLine("// Eval->: {0}", this); foreach (var statement in this._statements) { statement.Evaluate(il); } il.TraceWriteLine("// ->Eval: {0}", this); }
public override void Branch(TracingILGenerator il, Label @else) { il.TraceWriteLine("// Brnc->: {0}", this); foreach (var expression in this._expressions) { expression.LoadValue(il, false); il.EmitBrfalse(@else); } il.TraceWriteLine("// ->Brnc: {0}", this); }
public override void Evaluate(TracingILGenerator il) { if (this._isLocal && this._index < 0) { il.TraceWriteLine("// Eval->: {0}", this); this._index = il.DeclareLocal(this.ContextType.ResolveRuntimeType(), this._name).LocalIndex; il.TraceWriteLine("// ->Eval: {0}", this); } }
public static void EmitConstruction(TracingILGenerator il, LocalBuilder target, Action <TracingILGenerator> initialCountLoadingEmitter) { Contract.Requires(il != null); Contract.Requires(target != null); // TODO: For collection, supports .ctor(IEnumerable<> other) if (target.LocalType.IsArray) { Contract.Assert(initialCountLoadingEmitter != null); initialCountLoadingEmitter(il); il.EmitNewarr(target.LocalType.GetElementType()); il.EmitAnyStloc(target); return; } ConstructorInfo ctor = target.LocalType.GetConstructor(_ctor_Int32_ParameterTypes); if (ctor != null && initialCountLoadingEmitter != null && typeof(IEnumerable).IsAssignableFrom(target.LocalType)) { if (target.LocalType.IsValueType) { // Same as general method call var capacity = il.DeclareLocal(typeof(int), "capacity"); initialCountLoadingEmitter(il); il.EmitAnyStloc(capacity); il.EmitAnyLdloca(target); il.EmitAnyLdloc(capacity); il.EmitCallConstructor(ctor); } else { initialCountLoadingEmitter(il); il.EmitNewobj(ctor); il.EmitAnyStloc(target); } return; } if (target.LocalType.IsValueType) { // ValueType instance has been initialized by the runtime. return; } ctor = target.LocalType.GetConstructor(Type.EmptyTypes); if (ctor == null) { throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(target.LocalType); } il.EmitNewobj(ctor); il.EmitAnyStloc(target); }
private void EvaluateCore(TracingILGenerator il) { for (int i = 0; i < this._expressions.Count; i++) { this._expressions[i].LoadValue(il, false); if (i > 0) { il.EmitAnd(); } } }
private void Invoke(TracingILGenerator il) { ConstructorInfo asConsctructor; if ((asConsctructor = this._method as ConstructorInfo) != null) { if (asConsctructor.DeclaringType.GetIsValueType()) { this._target.LoadValue(il, true); foreach (var argument in this._arguments) { argument.LoadValue(il, false); } il.EmitCallConstructor(asConsctructor); // For compatibility to ref type. this._target.LoadValue(il, false); } else { foreach (var argument in this._arguments) { argument.LoadValue(il, false); } il.EmitNewobj(asConsctructor); } } else { // method if (!this._method.IsStatic) { this._target.LoadValue(il, this._target.ContextType.GetIsValueType()); } foreach (var argument in this._arguments) { argument.LoadValue(il, false); } if (this._method.IsStatic || this._target.ContextType.GetIsValueType()) { il.EmitCall(this._method as MethodInfo); } else { il.EmitCallvirt(this._method as MethodInfo); } } }
public override void StoreValue(TracingILGenerator il) { il.TraceWriteLine("// Stor->: {0}", this); if (this._instance != null) { this._instance.LoadValue(il, this._instance.ContextType.ResolveRuntimeType().GetIsValueType()); } this._value.LoadValue(il, false); il.EmitStfld(this._field); il.TraceWriteLine("// ->Stor: {0}", this); }
public sealed override void Branch(TracingILGenerator il, Label @else) { il.TraceWriteLine("// Brnc->: {0}", this); if (this.ContextType.ResolveRuntimeType() != typeof(bool)) { throw new InvalidOperationException( String.Format(CultureInfo.CurrentCulture, "Cannot branch with non boolean type '{0}'.", this.ContextType) ); } this.BranchCore(il, @else); il.TraceWriteLine("// ->Brnc: {0}", this); }
public static void EmitDeserializeValue(SerializerEmitter emitter, TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder result, SerializingMember member, LocalVariableHolder localHolder) { LocalBuilder deserializedValue; Contract.Requires(emitter != null); Contract.Requires(il != null); Contract.Requires(unpackerArgumentIndex >= 0); Contract.Requires(result != null); Label endOfDeserialization = il.DefineLabel("END_OF_DESERIALIZATION"); bool flag = false; if (member.Member.GetMemberValueType().GetIsValueType() && (Nullable.GetUnderlyingType(member.Member.GetMemberValueType()) == null)) { deserializedValue = localHolder.GetDeserializedValue(typeof(Nullable <>).MakeGenericType(new Type[] { member.Member.GetMemberValueType() })); flag = true; } else { deserializedValue = localHolder.GetDeserializedValue(member.Member.GetMemberValueType()); } if (deserializedValue.LocalType.GetIsValueType()) { il.EmitAnyLdloca(deserializedValue); il.EmitInitobj(deserializedValue.LocalType); } else { il.EmitLdnull(); il.EmitAnyStloc(deserializedValue); } EmitDeserializeValueCore(emitter, il, unpackerArgumentIndex, deserializedValue, result.LocalType, new SerializingMember?(member), member.Contract.Name, endOfDeserialization, localHolder); if (result.LocalType.IsValueType) { il.EmitAnyLdloca(result); } else { il.EmitAnyLdloc(result); } if (flag) { il.EmitAnyLdloca(deserializedValue); il.EmitGetProperty(typeof(Nullable <>).MakeGenericType(new Type[] { member.Member.GetMemberValueType() }).GetProperty("Value")); } else { il.EmitAnyLdloc(deserializedValue); } EmitStoreValue(il, member.Member); il.MarkLabel(endOfDeserialization); }
private static void CreateArrayUnpackTo(Type targetType, SerializerEmitter emitter, CollectionTraits traits) { TracingILGenerator unpackToMethodILGenerator = emitter.GetUnpackToMethodILGenerator(); try { EmitInvokeArrayUnpackToHelper(targetType, emitter, traits, unpackToMethodILGenerator, 1, il0 => il0.EmitAnyLdarg(2)); unpackToMethodILGenerator.EmitRet(); } finally { unpackToMethodILGenerator.FlushTrace(); } }
public static void EmitUnpackerEndReadSubtree(TracingILGenerator il, LocalBuilder subtreeUnpacker) { Contract.Requires(il != null); Contract.Requires(subtreeUnpacker != null); il.BeginFinallyBlock(); il.EmitAnyLdloc(subtreeUnpacker); Label target = il.DefineLabel("END_IF"); il.EmitBrfalse_S(target); il.EmitAnyLdloc(subtreeUnpacker); il.EmitAnyCall(_IDisposable.Dispose); il.MarkLabel(target); il.EndExceptionBlock(); }
private static void EmitExceptionHandling( TracingILGenerator il, Type returnType, Label endOfMethod, Action <TracingILGenerator, LocalBuilder> exceptionHandlerInvocation ) { il.BeginCatchBlock(typeof(Exception)); var exception = il.DeclareLocal(typeof(Exception), "exception"); il.EmitAnyStloc(exception); exceptionHandlerInvocation(il, exception); il.EmitLeave(endOfMethod); il.EndExceptionBlock(); }
public static void EmitUnpackerBeginReadSubtree(TracingILGenerator il, int unpackerArgumentIndex, LocalBuilder subtreeUnpacker) { Contract.Requires(il != null); Contract.Requires(unpackerArgumentIndex >= 0); Contract.Requires(subtreeUnpacker != null); /* * subtreeUnpacker = unpacker.ReadSubtree() */ il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitAnyCall(Metadata._Unpacker.ReadSubtree); il.EmitAnyStloc(subtreeUnpacker); il.BeginExceptionBlock(); }
public static void EmitLoadValue(TracingILGenerator il, MemberInfo member) { Contract.Requires(il != null); Contract.Requires(member != null); PropertyInfo property = member as PropertyInfo; if (property != null) { il.EmitGetProperty(property); } else { Contract.Assert(member is FieldInfo, member.ToString() + ":" + member.MemberType); il.EmitLdfld(member as FieldInfo); } }
protected sealed override void EmitPackMembers(SerializerEmitter emitter, TracingILGenerator packerIL, SerializingMember[] entries) { var localHolder = new LocalVariableHolder(packerIL); packerIL.EmitAnyLdarg(1); packerIL.EmitAnyLdc_I4(entries.Length); packerIL.EmitAnyCall(Metadata._Packer.PackMapHeader); packerIL.EmitPop(); foreach (var entry in entries) { if (entry.Member == null) { // skip undefined member. continue; } packerIL.EmitAnyLdarg(1); packerIL.EmitLdstr(entry.Contract.Name); packerIL.EmitAnyCall(Metadata._Packer.PackString); packerIL.EmitPop(); Emittion.EmitSerializeValue( emitter, packerIL, 1, entry.Member.GetMemberValueType(), entry.Contract.Name, entry.Contract.NilImplication, il0 => { if (typeof(TObject).IsValueType) { il0.EmitAnyLdarga(2); } else { il0.EmitAnyLdarg(2); } Emittion.EmitLoadValue(il0, entry.Member); }, localHolder ); } packerIL.EmitRet(); }
protected override void EmitPackMembers(SerializerEmitter emitter, TracingILGenerator packerIL, SerializingMember[] entries) { var localHolder = new LocalVariableHolder(packerIL); packerIL.EmitAnyLdarg(1); packerIL.EmitAnyLdc_I4(entries.Length); packerIL.EmitAnyCall(Metadata._Packer.PackArrayHeader); packerIL.EmitPop(); foreach (var member in entries) { if (member.Member == null) { // missing member, always nil packerIL.EmitAnyLdarg(1); packerIL.EmitAnyCall(Metadata._Packer.PackNull); packerIL.EmitPop(); } else { Emittion.EmitSerializeValue( emitter, packerIL, 1, member.Member.GetMemberValueType(), member.Member.Name, member.Contract.NilImplication, il => { if (typeof(TObject).IsValueType) { il.EmitAnyLdarga(2); } else { il.EmitAnyLdarg(2); } Emittion.EmitLoadValue(il, member.Member); }, localHolder ); } } packerIL.EmitRet(); }
private static void EmitUnpackMembersFromMap(SerializerEmitter emitter, TracingILGenerator unpackerIL, SerializingMember[] entries, LocalBuilder result, LocalVariableHolder localHolder) { Label label = unpackerIL.DefineLabel("BEGIN_LOOP"); Label target = unpackerIL.DefineLabel("END_LOOP"); unpackerIL.MarkLabel(label); LocalBuilder memberName = localHolder.MemberName; unpackerIL.EmitAnyLdarg(1); unpackerIL.EmitAnyLdloca(memberName); unpackerIL.EmitAnyCall(_Unpacker.ReadString); unpackerIL.EmitBrfalse(target); LocalBuilder unpackedData = localHolder.UnpackedData; LocalBuilder unpackedDataValue = localHolder.UnpackedDataValue; for (int i = 0; i < entries.Length; i++) { if (entries[i].Contract.Name != null) { unpackerIL.EmitAnyLdloc(memberName); unpackerIL.EmitLdstr(entries[i].Contract.Name); unpackerIL.EmitAnyCall(_String.op_Equality); Label label3 = unpackerIL.DefineLabel("END_IF_MEMBER_" + i); unpackerIL.EmitBrfalse(label3); if (entries[i].Member == null) { Emittion.EmitGeneralRead(unpackerIL, 1); } else if (UnpackHelpers.IsReadOnlyAppendableCollectionMember(entries[i].Member)) { Emittion.EmitDeserializeCollectionValue(emitter, unpackerIL, 1, result, entries[i].Member, entries[i].Member.GetMemberValueType(), entries[i].Contract.NilImplication, localHolder); } else { Emittion.EmitDeserializeValue(emitter, unpackerIL, 1, result, entries[i], localHolder); } unpackerIL.EmitBr(label); unpackerIL.MarkLabel(label3); } } unpackerIL.EmitAnyLdarg(1); unpackerIL.EmitCallvirt(_Unpacker.Read); unpackerIL.EmitPop(); unpackerIL.EmitBr(label); unpackerIL.MarkLabel(target); }
public override Func <SerializationContext, EnumSerializationMethod, MessagePackSerializer <T> > CreateConstructor <T>() { if (!this._typeBuilder.IsCreated()) { /* * .ctor( PackerCompatibilityOptions c, EnumSerializerMethod method ) * : base( c, method ) * { * } */ var il = new TracingILGenerator(this._contextConstructorBuilder, TextWriter.Null, this._isDebuggable); // : base( c, method ) il.EmitLdarg_0(); il.EmitLdarg_1(); il.EmitLdarg_2(); Contract.Assert(this._typeBuilder.BaseType != null); il.EmitCallConstructor( this._typeBuilder.BaseType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, this._constructorParameterTypes, null ) ); il.EmitRet(); } var ctor = this._typeBuilder.CreateType().GetConstructor(this._constructorParameterTypes); var contextParameter = Expression.Parameter(typeof(SerializationContext), "context"); var methodParameter = Expression.Parameter(typeof(EnumSerializationMethod), "method"); #if DEBUG Contract.Assert(ctor != null, "ctor != null"); #endif return (Expression.Lambda <Func <SerializationContext, EnumSerializationMethod, MessagePackSerializer <T> > >( Expression.New( ctor, contextParameter, methodParameter ), contextParameter, methodParameter ).Compile()); }
/// <summary> /// Emits gets <see cref="Unpacker.ItemsCount"/> with exception handling. /// Note that final state is the value is pushed top of the evaluation stack. /// </summary> /// <param name="il">IL generator.</param> /// <param name="unpackerArgumentIndex">Argument index of the unpacker.</param> /// <param name="localHolder">The <see cref="LocalVariableHolder"/> which holds shared local variable information.</param> public static void EmitGetUnpackerItemsCountAsInt32(TracingILGenerator il, int unpackerArgumentIndex, LocalVariableHolder localHolder) { Contract.Requires(il != null); Contract.Requires(unpackerArgumentIndex >= 0); /* * long rawItemsCount; * try * { * rawItemsCount = unpacker.ItemsCount; * } * catch ( InvalidOperationException ex ) * { * throw SerializationExceptions.NewIsIncorrectStream( ex ); * } * * if( rawItemsCount > Int32.MaxValue ) * { * throw SerializationException.NewIsTooLargeCollection(); * } * * ... unchecked( ( int )rawItemsCount ); */ il.EmitAnyLdloca(localHolder.RawItemsCount); il.EmitInitobj(typeof(long)); il.BeginExceptionBlock(); il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(Metadata._Unpacker.ItemsCount); il.EmitAnyStloc(localHolder.RawItemsCount); il.BeginCatchBlock(typeof(InvalidOperationException)); il.EmitAnyCall(SerializationExceptions.NewIsIncorrectStreamMethod); il.EmitThrow(); il.EndExceptionBlock(); il.EmitAnyLdloc(localHolder.RawItemsCount); il.EmitLdc_I8(Int32.MaxValue); var endIf = il.DefineLabel(); il.EmitBle_S(endIf); il.EmitAnyCall(SerializationExceptions.NewIsTooLargeCollectionMethod); il.EmitThrow(); il.MarkLabel(endIf); il.EmitAnyLdloc(localHolder.RawItemsCount); il.EmitConv_I4(); }
public override void LoadValue(TracingILGenerator il, bool shouldBeAddress) { il.TraceWriteLine("// Load->: {0}", this); if (this._instance != null) { this._instance.LoadValue(il, this._instance.ContextType.GetIsValueType()); } if (shouldBeAddress) { il.EmitLdflda(this._field); } else { il.EmitLdfld(this._field); } il.TraceWriteLine("// ->Load: {0}", this); }
private static void EmitUnpackMembers(SerializerEmitter emitter, TracingILGenerator unpackerIL, SerializingMember[] entries, LocalBuilder result) { LocalVariableHolder localHolder = new LocalVariableHolder(unpackerIL); unpackerIL.EmitAnyLdarg(1); unpackerIL.EmitGetProperty(_Unpacker.IsArrayHeader); Label target = unpackerIL.DefineLabel("ELSE"); Label label2 = unpackerIL.DefineLabel("END_IF"); unpackerIL.EmitBrfalse(target); EmittingSerializerBuilder <TObject> .EmitUnpackMembersFromArray(emitter, unpackerIL, entries, result, localHolder); unpackerIL.EmitBr(label2); unpackerIL.MarkLabel(target); EmittingSerializerBuilder <TObject> .EmitUnpackMembersFromMap(emitter, unpackerIL, entries, result, localHolder); unpackerIL.MarkLabel(label2); }
public static void EmitConstruction(TracingILGenerator il, LocalBuilder target, Action <TracingILGenerator> initialCountLoadingEmitter) { Contract.Requires(il != null); Contract.Requires(target != null); if (target.LocalType.IsArray) { Contract.Assert(initialCountLoadingEmitter != null); initialCountLoadingEmitter(il); il.EmitNewarr(target.LocalType.GetElementType()); il.EmitAnyStloc(target); } else { ConstructorInfo constructor = target.LocalType.GetConstructor(_ctor_Int32_ParameterTypes); if (((constructor != null) && (initialCountLoadingEmitter != null)) && typeof(IEnumerable).IsAssignableFrom(target.LocalType)) { if (target.LocalType.IsValueType) { LocalBuilder local = il.DeclareLocal(typeof(int), "capacity"); initialCountLoadingEmitter(il); il.EmitAnyStloc(local); il.EmitAnyLdloca(target); il.EmitAnyLdloc(local); il.EmitCallConstructor(constructor); } else { initialCountLoadingEmitter(il); il.EmitNewobj(constructor); il.EmitAnyStloc(target); } } else if (!target.LocalType.IsValueType) { constructor = target.LocalType.GetConstructor(Type.EmptyTypes); if (constructor == null) { throw SerializationExceptions.NewTargetDoesNotHavePublicDefaultConstructorNorInitialCapacity(target.LocalType); } il.EmitNewobj(constructor); il.EmitAnyStloc(target); } } }
public override void LoadValue(TracingILGenerator il, bool shouldBeAddress) { if (this._statements.Length == 0) { base.LoadValue(il, shouldBeAddress); return; } il.TraceWriteLine("// Eval(Load)->: {0}", this); for (var i = 0; i < this._statements.Length - 1; i++) { this._statements[i].Evaluate(il); } this._statements.Last().LoadValue(il, shouldBeAddress); il.TraceWriteLine("// ->Eval(Load): {0}", this); }
public static void EmitNilImplication(TracingILGenerator il, int unpackerArgumentIndex, string memberName, NilImplication nilImplication, Label endOfDeserialization, LocalVariableHolder localHolder) { LocalBuilder unpackedData; LocalBuilder unpackedDataValue; switch (nilImplication) { case NilImplication.MemberDefault: il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); il.EmitBrtrue(endOfDeserialization); break; case NilImplication.Prohibit: { il.EmitAnyLdarg(unpackerArgumentIndex); il.EmitGetProperty(_Unpacker.Data); unpackedData = localHolder.UnpackedData; il.EmitAnyStloc(unpackedData); il.EmitAnyLdloca(unpackedData); il.EmitGetProperty(_Nullable <MessagePackObject> .Value); unpackedDataValue = localHolder.UnpackedDataValue; il.EmitAnyStloc(unpackedDataValue); il.EmitAnyLdloca(unpackedDataValue); il.EmitGetProperty(_MessagePackObject.IsNil); Label target = il.DefineLabel("END_IF0"); il.EmitBrfalse_S(target); il.EmitLdstr(memberName); il.EmitAnyCall(SerializationExceptions.NewNullIsProhibitedMethod); il.EmitThrow(); il.MarkLabel(target); break; } } }