public BinaryFunc(FuncName name, OpCode opcode, Type returnType) { Name = name; _case = 0; _opcode = opcode; _returnType = returnType; }
public void EmitIf(OpCode comparasionOpcode, bool not = false) { _generator .Emit(comparasionOpcode) .EmitIf(!not, a => a.Brfalse(_ifFalse)) .EmitIf(not, a => a.Brtrue(_ifFalse)); }
static DynamicMethodDefinition() { foreach (FieldInfo field in typeof(System.Reflection.Emit.OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)) { System.Reflection.Emit.OpCode reflOpCode = (System.Reflection.Emit.OpCode)field.GetValue(null); _ReflOpCodes[reflOpCode.Value] = reflOpCode; } foreach (FieldInfo field in typeof(Mono.Cecil.Cil.OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)) { Mono.Cecil.Cil.OpCode cecilOpCode = (Mono.Cecil.Cil.OpCode)field.GetValue(null); _CecilOpCodes[cecilOpCode.Value] = cecilOpCode; } foreach (MethodInfo method in typeof(ILGenerator).GetMethods()) { if (method.Name != "Emit") { continue; } ParameterInfo[] args = method.GetParameters(); if (args.Length != 2) { continue; } if (args[0].ParameterType != typeof(System.Reflection.Emit.OpCode)) { continue; } _Emitters[args[1].ParameterType] = method; } }
public void Emit(OpCode code, ConstructorInfo constructor) { if (code == OpCodes.Newobj) { instructions.Add((args) => EmitInternal(code, constructor)); } }
internal OperatorToken(Operator op) { switch (op) { case Operator.Add: _opCode = OpCodes.Add; break; case Operator.Subtract: _opCode = OpCodes.Sub; break; case Operator.Multiply: _opCode = OpCodes.Mul; break; case Operator.Divide: _opCode = OpCodes.Div; break; case Operator.Power: _opCode = OpCodes.Call; break; case Operator.Modulus: _opCode = OpCodes.Rem; break; default: throw new ArgumentException("Unknown operator."); } Operator = op; }
private static void EmitIndexedInstruction(this ILGenerator il, OpCode[] index, int argument) { if (argument < index.Length - 1) { il.Emit(index[argument]); return; } il.Emit(index[index.Length - 1], argument); }
int GetOperandSize(OpCode o, int pos) { switch (o.OperandType) { case OperandType.ShortInlineBrTarget: return 1; case OperandType.ShortInlineI: return 1; case OperandType.ShortInlineR: return 4; case OperandType.ShortInlineVar: return 1; case OperandType.InlineVar: return 2; case OperandType.InlineType: return 4; case OperandType.InlineTok: return 4; case OperandType.InlineString: return 4; case OperandType.InlineSig: return 4; case OperandType.InlineR: return 8; case OperandType.InlineNone: return 0; case OperandType.InlineMethod: return 4; case OperandType.InlineI8: return 8; case OperandType.InlineI: return 4; case OperandType.InlineField: return 4; case OperandType.InlineBrTarget: return 4; case OperandType.InlineSwitch: return 4 + 4 * BitConverter.ToInt32(code, pos); } throw new NotImplementedException(); }
private void Emit(OpCode opCode, [NotNull] MethodReference methodRefernce) { var type = GetRuntimeType(methodRefernce.DeclaringType); var parameterTypes = methodRefernce.Parameters.Select(param => GetRuntimeType(param.ParameterType)).ToArray(); if (methodRefernce.Name == Ctor) { var constructor = type.GetConstructor(parameterTypes); if (constructor == null) { throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Constructor {0} not found", methodRefernce.ToString())); } _ilGenerator.Emit(opCode, constructor); return; } var method = type.GetMethod(methodRefernce.Name, parameterTypes); if (method == null) { throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Method: {0} not found", methodRefernce.ToString())); } _ilGenerator.Emit(opCode, method); }
public static bool IsTailableCall(OpCode op) { return op == OpCodes.Call || //op == OpCodes.Calli || op == OpCodes.Callvirt; }
private void Emit(OpCode opCode, [NotNull] FieldReference fieldReference) { var type = GetRuntimeType(fieldReference.DeclaringType); var field = type.GetField(fieldReference.Name); _ilGenerator.Emit(opCode, field); }
public static int Get(OpCode op, Sigil.Label[] labels = null) { var baseSize = op.Size; int operandSize; switch (op.OperandType) { case OperandType.InlineBrTarget: operandSize = 4; break; case OperandType.InlineField: operandSize = 4; break; case OperandType.InlineI: operandSize = 4; break; case OperandType.InlineI8: operandSize = 8; break; case OperandType.InlineMethod: operandSize = 4; break; case OperandType.InlineNone: operandSize = 0; break; case OperandType.InlineR: operandSize = 8; break; case OperandType.InlineSig: operandSize = 4; break; case OperandType.InlineString: operandSize = 4; break; case OperandType.InlineSwitch: operandSize = 4 + labels.Length * 4; break; case OperandType.InlineTok: operandSize = 4; break; case OperandType.InlineType: operandSize = 4; break; case OperandType.InlineVar: operandSize = 2; break; case OperandType.ShortInlineBrTarget: operandSize = 1; break; case OperandType.ShortInlineI: operandSize = 1; break; case OperandType.ShortInlineR: operandSize = 4; break; case OperandType.ShortInlineVar: operandSize = 1; break; default: throw new Exception("Unexpected operand type [" + op.OperandType + "]"); } return baseSize + operandSize; }
static OpCodeTable() { short maxOpCodeValue = 0; foreach (FieldInfo field in typeof(OpCodes).GetFields()) { NetOpCode netOpCode = (NetOpCode)field.GetValue(null); OpCode opCode = new OpCode(netOpCode.Value, netOpCode.Name, netOpCode.FlowControl, netOpCode.OperandType); _table.Add(opCode.Value, opCode); maxOpCodeValue = Math.Max(opCode.Value, maxOpCodeValue); } _preIncrementOpCode = new OpCode(++maxOpCodeValue, "preInc", FlowControl.Next, OperandType.InlineNone); _table.Add(_preIncrementOpCode.Value, _preIncrementOpCode); _preDecrementOpCode = new OpCode(++maxOpCodeValue, "preDec", FlowControl.Next, OperandType.InlineNone); _table.Add(_preDecrementOpCode.Value, _preDecrementOpCode); _postIncrementOpCode = new OpCode(++maxOpCodeValue, "postInc", FlowControl.Next, OperandType.InlineNone); _table.Add(_postIncrementOpCode.Value, _postIncrementOpCode); _postDecrementOpCode = new OpCode(++maxOpCodeValue, "postDec", FlowControl.Next, OperandType.InlineNone); _table.Add(_postDecrementOpCode.Value, _postDecrementOpCode); _postIncrementStatementOpCode = new OpCode(++maxOpCodeValue, "postIncStmt", FlowControl.Next, OperandType.InlineNone); _table.Add(_postIncrementStatementOpCode.Value, _postIncrementStatementOpCode); _postDecrementStatementOpCode = new OpCode(++maxOpCodeValue, "postDecStmt", FlowControl.Next, OperandType.InlineNone); _table.Add(_postDecrementStatementOpCode.Value, _postDecrementStatementOpCode); }
internal MethodInstruction(int offset, OpCode opCode, int token, ModuleScopeTokenResolver resolver) { this.offset = offset; this.opCode = opCode; this.resolver = resolver; this.token = token; }
public virtual void Emit(OpCode opcode, MethodInfo method) { _gen.Emit(opcode, method); var methodParameters = method.GetParameters(); var parameters = string.Join(", ", methodParameters.Select(p => p.ParameterType.Name)); _writer.WriteLine(string.Concat(opcode, ", ", method.Name, "(", parameters, ")")); }
public static bool IsPrefix(OpCode op) { return op == OpCodes.Tailcall || op == OpCodes.Readonly || op == OpCodes.Volatile || op == OpCodes.Unaligned; }
internal ConversionOpcode(Emit.OpCode opcode) : base(opcode) { var match = nameRegex.Match(opcode.Name); targetDataType = DataTypeEnum.TryParseNameInOpcode(match.Groups[2].Value).Value; isSourceUnsigned = match.Groups[3].Success; isOverflowChecked = match.Groups[1].Success; }
public virtual void Emit(OpCode opcode, ConstructorInfo ctor) { _gen.Emit(opcode, ctor); var ctorParameters = ctor.GetParameters(); var parameters = string.Join(", ", ctorParameters.Select(p => p.ParameterType.Name)); _writer.WriteLine(string.Concat(opcode, ", ", ctor.Name, "(", parameters, ")")); }
public static OpCode ConvertOpCode(ROpCode ropcode) { if (reflectionToDnlib.TryGetValue(ropcode, out Opcode)) { return(Opcode); } return(OpCodes.Nop); }
private void AdvanceOffsetWithLabel(OpCode opcode) { AdvanceOffset(opcode); if (OpCodes.TakesSingleByteArgument(opcode)) { _offset++; } else { _offset += 4; } }
private void Emit(OpCode opCode, [NotNull] Instruction[] instructionRefs) { var labels = instructionRefs .Select(instructionRef => _labels[instructionRef.Offset]) .ToArray(); _ilGenerator.Emit(opCode, labels); }
public ConditionExpression( OpCode operation, Expression left, Expression right ) { _trueStmts = new ArrayList(); _falseStmts = new ArrayList(); _operation = operation; _left = left; _right = right; }
private BranchSet(OpCode brTrue, OpCode brFalse, OpCode brEq, OpCode brNe, OpCode brLt, OpCode brLtUn, OpCode brGt, OpCode brGtUn, OpCode brLe, OpCode brLeUn, OpCode brGe, OpCode brGeUn) { this.brTrue = brTrue; this.brFalse = brFalse; this.brEq = brEq; this.brNe = brNe; this.brLt = brLt; this.brLtUn = brLtUn; this.brGt = brGt; this.brGtUn = brGtUn; this.brLe = brLe; this.brLeUn = brLeUn; this.brGe = brGe; this.brGeUn = brGeUn; }
private void EmitIlShift(int amount, System.Reflection.Emit.OpCode ilOp) { if (amount > 0) { EmitLdc_I4(amount); Emit(ilOp); } }
protected void CheckAndEmit(OpCode? argument) { if(!argument.HasValue) { throw new InvalidOperationException(); } generator.Emit(argument.Value); }
public DynamicMethodBody Emit(OpCode opcode) { ExecutePreEmitActions(); #if DEBUG Console.WriteLine(string.Format("\t{0}", opcode)); #endif Emitter.Emit(opcode); return this; }
public void EmitCall(OpCode opCode, MethodInfo mi) { ProcessCommand( opCode, (mi.GetParameters().Length + 1) * -1 + (mi.ReturnType == typeof(void) ? 0 : 1), mi.ToString() ); ilGenerator.EmitCall(opCode, mi, null); }
internal ElementReferenceOpcode(Emit.OpCode opcode) : base(opcode) { string name = opcode.Name; int dotIndex = name.LastIndexOf('.'); if (dotIndex >= 0) { dataType = DataTypeEnum.TryParseNameInOpcode(name.Substring(dotIndex + 1)); } }
private OpCodeKb(OpCodeFamilyKb familyKb, OpCode? opcode) { FamilyKb = familyKb; _opCode = opcode; Tags = new HashSet<String>(); Meta = new Dictionary<String, String>(); Fields = new Dictionary<String, FieldSpec>(); Props = new Dictionary<String, PropertySpec>(); Prefixes = new Dictionary<String, PrefixSpec>(); }
internal void EmitInternal(OpCode code, ConstructorInfo constructor) { if (code == OpCodes.Newobj) { var parameterCount = constructor.GetParameters().Length; object[] arguments = Pop(parameterCount); Type type = constructor.DeclaringType; object instance = Activator.CreateInstance(type, arguments); stack.Push(instance); } }
public DynamicMethodBody Emit(OpCode opcode, Label arg) { ExecutePreEmitActions(); #if DEBUG Console.WriteLine("\t{0} IL_{1}", opcode, arg.GetHashCode()); #endif Emitter.Emit(opcode, arg); return this; }
public DynamicMethodBody Emit(OpCode opcode, string arg) { ExecutePreEmitActions(); #if DEBUG Console.WriteLine("\t{0} \"{1}\"", opcode, arg); #endif Emitter.Emit(opcode, arg); return this; }
public DynamicMethodBody Emit(OpCode opcode, TypeReference arg) { ExecutePreEmitActions(); #if DEBUG Console.WriteLine("\t{0} {1}", opcode, arg); #endif Emitter.Emit(opcode, arg); return this; }
public static Parser<IEnumerable<Instruction>, Instruction> OpCode(OpCode opcode) { return input => { var l = input.ToList(); if (l.Count >= 1 && l[0].OpCode == opcode) { return Parsers.Result(l[0], l.Skip(1)); } return null; }; }
public DynamicMethodBody Emit(OpCode opcode, double arg) { ExecutePreEmitActions(); #if DEBUG Console.WriteLine("\t{0} {1}", opcode, arg); #endif methodInfoField.GetILEmitter() .Emit(opcode, arg); return this; }
private static void EmitSpecific(short val, IReadOnlyDictionary<short, OpCode> map, OpCode generalCode, ILGenerator body) { OpCode code; if (map.TryGetValue(val, out code)) { body.Emit(code); } else { body.Emit(generalCode, val); } }
internal LabelInfo(ILGen il, LabelTarget node, bool canReturn) { _ilg = il; Node = node; Label = il.DefineLabel(); _canReturn = canReturn; if (node != null && node.Type != typeof(void)) { Value = il.DeclareLocal(node.Type); } // Until we have more information, default to a leave instruction, which always works _opCode = OpCodes.Leave; }
private static System.Reflection.Emit.OpCode SwitchShortOpCodes(System.Reflection.Emit.OpCode code) { if (code == OpCodes.Bge_Un_S) { code = OpCodes.Bge_Un; } if (code == OpCodes.Bne_Un_S) { code = OpCodes.Bne_Un; } if (code == OpCodes.Ble_Un_S) { code = OpCodes.Ble_Un; } if (code == OpCodes.Ble_S) { code = OpCodes.Ble; } if (code == OpCodes.Blt_S) { code = OpCodes.Blt; } if (code == OpCodes.Blt_Un_S) { code = OpCodes.Blt_Un; } if (code == OpCodes.Beq_S) { code = OpCodes.Beq; } if (code == OpCodes.Br_S) { code = OpCodes.Br; } if (code == OpCodes.Brtrue_S) { code = OpCodes.Brtrue; } if (code == OpCodes.Brfalse_S) { code = OpCodes.Brfalse; } if (code == OpCodes.Leave_S) { code = OpCodes.Leave; } if (code == OpCodes.Bge_S) { code = OpCodes.Bge; } return(code); }
private void Emit(OpCode opCode, [NotNull] MethodReference methodReference) { if (methodReference.Name == Ctor) { var constructorInfo = _methodMapper.GetConstructor(methodReference); _ilGenerator.Emit(opCode, constructorInfo); return; } var method = _methodMapper.GetMethod(methodReference); _ilGenerator.Emit(opCode, method); }
/// <summary> /// Generates opcodes to call a method (that takes a StreamingContext parameter and has no return value) that has a given attribute type /// </summary> /// <remarks> /// Used to generate method calls for OnSerializingAttribute, OnSerializedAttribute, etc. /// </remarks> public static void CallSerializationEventMethod( ILGenerator generator, OpCode pushObjectOpCode, OpCode pushStreamingContextOpCode, Type objType, Type attributeType ) { foreach ( MethodInfo method in objType.GetMethods( ) ) { if ( method.GetCustomAttributes( attributeType, false ).Length > 0 ) { generator.Emit( pushObjectOpCode ); generator.Emit( pushStreamingContextOpCode ); generator.Emit( OpCodes.Call, method ); return; } } }
private void EmitWithMethodOperand(System.Reflection.Emit.OpCode opcode, MethodBase methodBase) { MethodInfo methodInfo = methodBase as MethodInfo; if (methodInfo != null) { generator.Emit(opcode, methodInfo); } else { generator.Emit(opcode, (ConstructorInfo)methodBase); } }
protected override void OnEmit(OpCode opcode, Label arg) { var value = (int)typeof(Label) .GetFields(BindingFlags.Instance | BindingFlags.NonPublic) .First() .GetValue(arg); ProcessInstruction( _ilProcessorField.Create( opcode.ToCecil(), _labelsField[value].LabeledInstruction )); }
static _DMDEmit() { foreach (FieldInfo field in typeof(System.Reflection.Emit.OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)) { System.Reflection.Emit.OpCode reflOpCode = (System.Reflection.Emit.OpCode)field.GetValue(null); _ReflOpCodes[reflOpCode.Value] = reflOpCode; } foreach (FieldInfo field in typeof(Mono.Cecil.Cil.OpCodes).GetFields(BindingFlags.Public | BindingFlags.Static)) { Mono.Cecil.Cil.OpCode cecilOpCode = (Mono.Cecil.Cil.OpCode)field.GetValue(null); _CecilOpCodes[cecilOpCode.Value] = cecilOpCode; } }
internal static void _EmitCallSite(DynamicMethod dm, ILGenerator il, System.Reflection.Emit.OpCode opcode, CallSite csite) { /* The mess in this method is heavily based off of the code available at the following links: * https://github.com/Microsoft/referencesource/blob/3b1eaf5203992df69de44c783a3eda37d3d4cd10/mscorlib/system/reflection/emit/dynamicmethod.cs#L791 * https://github.com/Microsoft/referencesource/blob/3b1eaf5203992df69de44c783a3eda37d3d4cd10/mscorlib/system/reflection/emit/dynamicilgenerator.cs#L353 * https://github.com/mono/mono/blob/82e573122a55482bf6592f36f819597238628385/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs#L411 * https://github.com/mono/mono/blob/82e573122a55482bf6592f36f819597238628385/mcs/class/corlib/System.Reflection.Emit/ILGenerator.cs#L800 * https://github.com/dotnet/coreclr/blob/0fbd855e38bc3ec269479b5f6bf561dcfd67cbb6/src/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs#L57 */ List<object> _tokens = null; int _GetTokenForType(Type v) { _tokens.Add(v.TypeHandle); return _tokens.Count - 1 | 0x02000000 /* (int) MetadataTokenType.TypeDef */; } int _GetTokenForSig(byte[] v) { _tokens.Add(v); return _tokens.Count - 1 | 0x11000000 /* (int) MetadataTokenType.Signature */; } #if !NETSTANDARD DynamicILInfo _info = null; if (DynamicMethodDefinition._IsMono) { // GetDynamicILInfo throws "invalid signature" in .NET - let's hope for the best for mono... _info = dm.GetDynamicILInfo(); } else { #endif // For .NET, we need to access DynamicScope m_scope and its List<object> m_tokens _tokens = f_DynScope_m_tokens.GetValue(f_DynILGen_m_scope.GetValue(il)) as List<object>; #if !NETSTANDARD } int GetTokenForType(Type v) => _info != null ? _info.GetTokenFor(v.TypeHandle) : _GetTokenForType(v); int GetTokenForSig(byte[] v) => _info != null ? _info.GetTokenFor(v) : _GetTokenForSig(v); #else int GetTokenForType(Type v) => _GetTokenForType(v); int GetTokenForSig(byte[] v) => _GetTokenForSig(v); #endif byte[] signature = new byte[32]; int currSig = 0; int sizeLoc = -1; // This expects a MdSigCallingConvention AddData((byte) csite.CallingConvention); sizeLoc = currSig++; List<Type> modReq = new List<Type>(); List<Type> modOpt = new List<Type>(); ResolveWithModifiers(csite.ReturnType, out Type returnType, out Type[] returnTypeModReq, out Type[] returnTypeModOpt, modReq, modOpt);
private void EmitZnCheck(System.Reflection.Emit.OpCode ilCmpOp, int flag) { Emit(OpCodes.Dup); Emit(OpCodes.Ldc_I4_0); if (CurrOp.RegisterSize != RegisterSize.Int32) { Emit(OpCodes.Conv_I8); } Emit(ilCmpOp); EmitStflg(flag); }
/// <summary> /// 将指定的方法或构造函数的指定指令和元数据标记放到 Microsoft 中间语言 (MSIL) 指令流上。 /// </summary> /// <param name="il">IL 指令生成器。</param> /// <param name="opCode">要发到流中的 MSIL 指令。</param> /// <param name="method">方法或构造函数。</param> /// <exception cref="ArgumentNullException"><paramref name="il"/> 为 <c>null</c>。</exception> /// <exception cref="ArgumentNullException"><paramref name="method"/> 为 <c>null</c>。</exception> public static void Emit(this ILGenerator il, OpCode opCode, MethodBase method) { CommonExceptions.CheckArgumentNull(il, "il"); CommonExceptions.CheckArgumentNull(method, "method"); Contract.EndContractBlock(); if (method.MemberType == MemberTypes.Constructor) { il.Emit(opCode, (ConstructorInfo)method); } else { il.Emit(opCode, (MethodInfo)method); } }
protected override void OnEmit(OpCode opcode, Type arg) { var ts = _assemblyDefinitionField.MainModule.TypeSystem; TypeReference reference; if (arg == typeof(int)) { reference = ts.Int32; } else { throw new NotSupportedException(); } ProcessInstruction(_ilProcessorField.Create(opcode.ToCecil(), reference)); }
internal LoadConstantOpcode(Emit.OpCode opcode) : base(opcode) { char lastChar = opcode.Name[opcode.Name.Length - 1]; if (lastChar >= '0' && lastChar <= '9') { constantValue = (sbyte)(lastChar - '0'); if (opcode.Name[opcode.Name.Length - 2] == 'M') { constantValue *= -1; } } else { constantValue = sbyte.MinValue; } }
public void Emit(System.Reflection.Emit.OpCode opcode, MethodInfo meth) => generator.Emit(opcode, meth);
public void Emit(System.Reflection.Emit.OpCode opcode, double arg) => generator.Emit(opcode, arg);
public void Emit(System.Reflection.Emit.OpCode opcode, Label[] labels) => generator.Emit(opcode, labels);
public void Emit(System.Reflection.Emit.OpCode opcode, FieldInfo field) => generator.Emit(opcode, field);
public void Emit(System.Reflection.Emit.OpCode ilOp, ILLabel label) { _ilBlock.Add(new ILOpCodeBranch(ilOp, label)); }
public void Emit(System.Reflection.Emit.OpCode ilOp) { _ilBlock.Add(new ILOpCode(ilOp)); }
public static void Emit(this CecilILGenerator il, OpCode opcode, object operand) { emitCodeDelegate(il, opcode, operand); }
public void Emit(System.Reflection.Emit.OpCode opcode, ConstructorInfo con) => generator.Emit(opcode, con);
internal FieldReferenceOpcode(Emit.OpCode opcode) : base(opcode) { }
internal BranchOpcode(Emit.OpCode opcode) : base(opcode) { }
internal BranchOpcode(Emit.OpCode opcode, Comparison comparison) : base(opcode) { this.comparison = comparison; }
private static void ResolveMethod(ILGenerator generator, Dictionary <MethodBuilder, MethodInfo> methods, Dictionary <ConstructorBuilder, ConstructorInfo> constructors, VRage.Compiler.IlReader.IlInstruction instruction, System.Reflection.Emit.OpCode code) { bool found = false; var method = instruction.Operand as MethodBase; if (instruction.Operand is MethodInfo) { var methodInfo = instruction.Operand as MethodInfo; foreach (var met in methods) { if (met.Value == methodInfo) { generator.Emit(code, met.Key); found = true; break; } } } if (instruction.Operand is ConstructorInfo) { var methodInfo = instruction.Operand as ConstructorInfo; foreach (var met in constructors) { if (met.Value == methodInfo) { generator.Emit(code, met.Key); found = true; break; } } } if (false == found) { if (method is MethodInfo) { generator.Emit(code, method as MethodInfo); } else if (method is ConstructorInfo) { generator.Emit(code, method as ConstructorInfo); } } }
Instruction(ILReader reader, OpCode op, Operand arg, ushort label) { this.reader = reader; this.OpCode = op; this.Operand = arg; this.label = (ushort)label; }