public void MaybSetLocalName(LocalBuilder lb, string name) { if (_isDebuggable) { lb.SetLocalSymInfo(name); } }
private static void EmitGetDefault(ILGenerator il, Type type) { if (type.IsArray) { // Create a 0-length array of type il.Emit(OpCodes.Ldc_I4_0); il.Emit(OpCodes.Newarr, type.GetElementType()); return; } if (type.IsValueType) { // Create default of type LocalBuilder defaultVal = il.DeclareLocal(type); #if DEBUG // Debug helpers defaultVal.SetLocalSymInfo(nameof(defaultVal)); #endif il.Emit(OpCodes.Ldloca_S, defaultVal); il.Emit(OpCodes.Initobj, type); il.Emit(OpCodes.Ldloc_S, defaultVal); return; } // Create null object il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Castclass, type); }
/// <summary> /// Creates local variable declarations. /// </summary> /// <param name="il">The IL generator to use.</param> /// <param name="method">The method to proxy.</param> protected virtual void DeclareLocals(ILGenerator il, MethodInfo method) { interceptors = il.DeclareLocal(typeof(IList)); targetType = il.DeclareLocal(typeof(Type)); arguments = il.DeclareLocal(typeof(Object[])); #if NET_2_0 if (method.IsGenericMethodDefinition) { genericTargetMethod = il.DeclareLocal(typeof(MethodInfo)); genericOnProxyTargetMethod = il.DeclareLocal(typeof(MethodInfo)); } #endif if (methodReturnsValue) { returnValue = il.DeclareLocal(method.ReturnType); } #if DEBUG interceptors.SetLocalSymInfo("interceptors"); targetType.SetLocalSymInfo("targetType"); arguments.SetLocalSymInfo("arguments"); #if NET_2_0 if (method.IsGenericMethodDefinition) { genericTargetMethod.SetLocalSymInfo("genericTargetMethod"); genericOnProxyTargetMethod.SetLocalSymInfo("genericOnProxyTargetMethod"); } #endif if (methodReturnsValue) { returnValue.SetLocalSymInfo("returnValue"); } #endif }
public static void EmitTypedArgs(ObjExpr objx, CljILGen ilg, ParameterInfo[] parms, List <HostArg> args) { for (int i = 0; i < parms.Length; i++) { HostArg ha = args[i]; ParameterInfo pi = parms[i]; bool argIsByRef = ha.ParamType == HostArg.ParameterType.ByRef; bool paramIsByRef = pi.ParameterType.IsByRef; if (!paramIsByRef) { EmitTypedArg(objx, ilg, pi.ParameterType, ha.ArgExpr); } else // paramIsByRef { if (argIsByRef) { EmitByRefArg(ha, objx, ilg); } else { EmitTypedArg(objx, ilg, parms[i].ParameterType, args[i].ArgExpr); LocalBuilder loc = ilg.DeclareLocal(pi.ParameterType); loc.SetLocalSymInfo("_byRef_temp" + i); ilg.Emit(OpCodes.Stloc, loc); ilg.Emit(OpCodes.Ldloca, loc); } } } }
private static IAssessmentAopAdviceProvider CreateInstance(string instanceName, string methodName, string returnValue) { TypeBuilder typeBuilder = moduleBuilder.DefineType("MvcAdviceProvider.MvcAdviceProviderType", TypeAttributes.Public, typeof(object), new Type[] { typeof(IAssessmentAopAdviceProvider) }); // typeBuilder.AddInterfaceImplementation(typeof(IAssessmentAopAdviceProvider)); MethodBuilder beforeMethodBuilder = typeBuilder.DefineMethod(methodName, MethodAttributes.Public | MethodAttributes.Virtual, typeof(string), new Type[] { typeof(int) }); beforeMethodBuilder.DefineParameter(1, ParameterAttributes.None, "value"); ILGenerator generator1 = beforeMethodBuilder.GetILGenerator(); LocalBuilder local1 = generator1.DeclareLocal(typeof(string)); local1.SetLocalSymInfo("param1"); generator1.Emit(OpCodes.Nop); generator1.Emit(OpCodes.Ldstr, returnValue); generator1.Emit(OpCodes.Stloc_0); generator1.Emit(OpCodes.Ldloc_0); generator1.Emit(OpCodes.Ret); Type providerType = typeBuilder.CreateType(); assemblyBuilder.Save("test.dll"); IAssessmentAopAdviceProvider provider = Activator.CreateInstance(providerType) as IAssessmentAopAdviceProvider; return(provider); }
public override void Compile(ILGenerator il) { EmitDebugInfo(il, 0, false); //Declare at first use if (Options.BookVersion && !SymbolTable.IsInScope(Variable.Name)) { SymbolTable.DefineVariable(Variable.Name); LocalBuilder lb = il.DeclareLocal(typeof(int)); if (Options.Debug) { lb.SetLocalSymInfo(Variable.Name); } } if (SymbolTable.IsResultArgument(Variable.Name)) { il.Emit(OpCodes.Ldarg, SymbolTable.GetValue(Variable.Name)); Expression.Compile(il); il.Emit(OpCodes.Stind_I4); } else if (SymbolTable.IsArgument(Variable.Name)) { Expression.Compile(il); il.Emit(OpCodes.Starg, SymbolTable.GetValue(Variable.Name)); } else { Expression.Compile(il); il.Emit(OpCodes.Stloc, SymbolTable.GetValue(Variable.Name)); } }
private static MethodInfo BuildMethodProxy(MethodInfo target) { TypeBuilder type = module.DefineType(Guid.NewGuid().ToString(), TypeAttributes.Public | TypeAttributes.Class); ParameterInfo [] parameters = target.GetParameters(); Type [] parameter_types = new Type [parameters.Length + 2]; parameter_types [0] = parameter_types [1] = typeof(IntPtr); for (int i = 0; i < parameters.Length; i++) { Type parameter_type = parameters [i].ParameterType; if (parameter_type.IsSubclassOf(typeof(Object)) || parameter_type == typeof(Object)) { parameter_types [2 + i] = typeof(IntPtr); } else if (parameter_type == typeof(System.Object)) { parameter_types [2 + i] = typeof(IntPtr); } else { parameter_types [2 + i] = parameter_type; } } MethodBuilder method = type.DefineMethod("Dispatch", MethodAttributes.Public | MethodAttributes.Static, typeof(IntPtr), parameter_types); ILGenerator ilg = method.GetILGenerator(); LocalBuilder args = ilg.DeclareLocal(typeof(object [])); args.SetLocalSymInfo("args"); ilg.Emit(OpCodes.Ldc_I4, parameters.Length); ilg.Emit(OpCodes.Newarr, typeof(object)); ilg.Emit(OpCodes.Stloc_0); for (int i = 2; i < parameter_types.Length; i++) { ilg.Emit(OpCodes.Ldloc_0); ilg.Emit(OpCodes.Ldc_I4, i - 2); ilg.Emit(OpCodes.Ldarg, i); if (parameter_types [i].IsValueType) { ilg.Emit(OpCodes.Box, parameter_types [i]); } ilg.Emit(OpCodes.Stelem_I4); } ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Ldarg_1); ilg.Emit(OpCodes.Ldloc_0); ilg.Emit(OpCodes.Call, typeof(ObjCInterop).GetMethod("Dispatch", BindingFlags.Public | BindingFlags.Static, null, new Type [] { typeof(IntPtr), typeof(IntPtr), typeof(object []) }, null)); ilg.Emit(OpCodes.Ret); return(type.CreateType().GetMethod("Dispatch", BindingFlags.Public | BindingFlags.Static)); }
public virtual void Declare(ILGenerator il) { if (_local != null) { throw new InvalidOperationException("Variable " + Name + " is already declared!"); } _local = il.DeclareLocal(this.Type); _local.SetLocalSymInfo(Name); }
/// <summary> /// Creates local variable declarations. /// </summary> /// <param name="il">The IL generator to use.</param> /// <param name="method">The method to proxy.</param> protected override void DeclareLocals(ILGenerator il, MethodInfo method) { base.DeclareLocals(il, method); target = il.DeclareLocal(typeof(object)); #if DEBUG target.SetLocalSymInfo("target"); #endif }
/// <summary> /// Creates local variable declarations. /// </summary> /// <param name="il">The IL generator to use.</param> /// <param name="method">The method to proxy.</param> protected override void DeclareLocals(ILGenerator il, MethodInfo method) { base.DeclareLocals(il, method); targetSource = il.DeclareLocal(typeof(ITargetSourceWrapper)); #if DEBUG targetSource.SetLocalSymInfo("targetSource"); #endif }
public void MaybSetLocalName(LocalBuilder lb, string name) { #if NET461 if (_isDebuggable) { lb.SetLocalSymInfo(name); } #endif }
protected override Slot CreateSlot(SymbolId name, Type type) { LocalBuilder b = _codeGen.DeclareLocal(type); if (_codeGen.EmitDebugInfo) { b.SetLocalSymInfo(SymbolTable.IdToString(Ast.Variable.UnGenSym(name))); } return(new LocalSlot(b, _codeGen)); }
public override Slot MakeSlot(Name name, Type type) { LocalBuilder b = codeGen.DeclareLocal(type); if (codeGen.EmitDebugInfo) { b.SetLocalSymInfo(name.GetString()); } return(new LocalSlot(b, codeGen)); }
private static void CreateNoOpMethod(MethodInfo method, ILGenerator generator) { if (method.ReturnType != typeof(void)) { LocalBuilder local = generator.DeclareLocal(method.ReturnType); local.SetLocalSymInfo("FakeReturn"); generator.Emit(OpCodes.Ldloc, local); } generator.Emit(OpCodes.Ret); }
public override void GenerateMSIL(ILGenerator ilGenerator, TypeBuilder typeBuilder) { LocalBuilder newVar = ilGenerator.DeclareLocal(typeof(int)); newVar.SetLocalSymInfo(Variable); RightHandExpression.GenerateMSIL(ilGenerator, typeBuilder); ilGenerator.Emit(OpCodes.Stloc, newVar); }
public static LocalBuilder CreateArray <T>(this ILGenerator ilg, int size, string name = "") { LocalBuilder array = ilg.DeclareLocal(typeof(T[])); array.SetLocalSymInfo(name); Load32BitIntegerConstant(ilg, size); ilg.Emit(OpCodes.Newarr, typeof(T)); ilg.Emit(OpCodes.Stloc, array); return(array); }
internal override void TranslateToIL(ILGenerator il, Type rtype) { //This assumes that rtype == Void.class this.context.EmitLineInfo(il); Globals.ScopeStack.Push(new WithObject(Globals.ScopeStack.Peek(), new JSObject(null, false))); bool savedInsideProtectedRegion = compilerGlobals.InsideProtectedRegion; compilerGlobals.InsideProtectedRegion = true; Label lab = il.DefineLabel(); compilerGlobals.BreakLabelStack.Push(lab); compilerGlobals.ContinueLabelStack.Push(lab); this.obj.TranslateToIL(il, Typeob.Object); this.EmitILToLoadEngine(il); il.Emit(OpCodes.Call, CompilerGlobals.jScriptWithMethod); // JScriptWith returns the with object as an 'Object' (used by the debugger EE) // define a local named 'with()' that the debugger EE will use to bind to the with object LocalBuilder withObj = null; if (context.document.debugOn) { il.BeginScope(); // used by the debugger to mark a with block withObj = il.DeclareLocal(Typeob.Object); withObj.SetLocalSymInfo("with()"); il.Emit(OpCodes.Stloc, withObj); } else { il.Emit(OpCodes.Pop); } il.BeginExceptionBlock(); this.block.TranslateToILInitializer(il); this.block.TranslateToIL(il, Typeob.Void); il.BeginFinallyBlock(); if (context.document.debugOn) { // null the local used by the debugger EE il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Stloc, withObj); } this.EmitILToLoadEngine(il); il.Emit(OpCodes.Call, CompilerGlobals.popScriptObjectMethod); il.Emit(OpCodes.Pop); il.EndExceptionBlock(); if (context.document.debugOn) { il.EndScope(); // used by the debugger to mark a with block } il.MarkLabel(lab); compilerGlobals.BreakLabelStack.Pop(); compilerGlobals.ContinueLabelStack.Pop(); compilerGlobals.InsideProtectedRegion = savedInsideProtectedRegion; Globals.ScopeStack.Pop(); }
public override void Compile(ILGenerator il) { EmitDebugInfo(il, 0, true); SymbolTable.DefineVariable(Variable.Name); LocalBuilder lb = il.DeclareLocal(typeof(int)); if (Options.Debug) { lb.SetLocalSymInfo(Variable.Name); } }
public ILocalVariable EmitLocalVarDeclaration(string localVarName, Type localVarType) { LocalBuilder localBuilder = ilGenerator.DeclareLocal(localVarType); // if (CanInitializeLocation(localVarType)) { // // Store the already prepared initializer // ilGenerator.Emit(OpCodes.Stloc, localBuilder); // } localBuilder.SetLocalSymInfo(localVarName); return(new CilLocalVariable(ilGenerator, localBuilder)); }
public static IEnumerable <CodeInstruction> Transpiler(IEnumerable <CodeInstruction> instructions, ILGenerator generator) { if (!Configuration.Current.Pickable.IsEnabled) { return(instructions); } List <CodeInstruction> il = instructions.ToList(); LocalBuilder newYieldLocal = null; for (int i = 0; i < il.Count; i++) { if (newYieldLocal == null && i > 0 && il[i].opcode == OpCodes.Stloc_1 && il[i - 1].opcode == OpCodes.Ldc_I4_0) { // Step 1: Call calculateYield() and store the result as a new local variable. // // Calling calculateYield takes several instructions: // LdArg.0 (load the "this" pointer), LdFld (load m_itemPrefab from this), LdArg.0 (this), LdFld (load m_amount from this), Call. // We then store the return value as our new local, used in the second patch below. newYieldLocal = generator.DeclareLocal(typeof(int)); newYieldLocal.SetLocalSymInfo("newYieldLocal"); il.Insert(++i, new CodeInstruction(OpCodes.Ldarg_0)); il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_ItemPrefab)); il.Insert(++i, new CodeInstruction(OpCodes.Ldarg_0)); il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_amount)); il.Insert(++i, new CodeInstruction(new CodeInstruction(OpCodes.Call, method_calculateYield))); il.Insert(++i, new CodeInstruction(OpCodes.Stloc_S, newYieldLocal.LocalIndex)); } if (newYieldLocal != null && il[i].opcode == OpCodes.Ldfld && il[i].operand.ToString().Contains("m_amount")) { // Step 2: Patch the for loop to loop calculateYield() iterations instead of m_amount iterations, // by replacing m_amount with our new local variable, created above. // // Get rid of LdArg.1 (i-1) and LdFld (i) and replace them with loading our previously calculated yield. il[i] = new CodeInstruction(OpCodes.Ldloc_S, newYieldLocal.LocalIndex); il.RemoveRange(i - 1, 1); ZLog.Log("Successfully transpiled Pickable.RPC_Pick to patch item yields"); // NOTE: This transpiler may be called multiple times, e.g. when starting the game, when connecting to a server and when disconnecting. // We need to re-do the initial setup every time, since the modifier values may have changed (as the server config will be used instead of the client config). initialSetup(); return(il.AsEnumerable()); } } ZLog.Log("Unable to transpile Pickable.RPC_Pick to patch item yields"); return(instructions); }
internal LocalBuilder GetNamedLocal(Type type, string name) { Assert.NotNull(type); if (_emitDebugSymbols && name != null) { LocalBuilder lb = _ilg.DeclareLocal(type); // TODO: we need to set the lexical scope properly, so it can // be freed and reused! lb.SetLocalSymInfo(name); return(lb); } return(_ilg.GetLocal(type)); }
internal void EmitLocalInfoForFields(ILGenerator il) { foreach (JSLocalField field in this.localFieldsForDebugInfo) { ((LocalBuilder)field.metaData).SetLocalSymInfo(field.debuggerName); } if (this.parent is GlobalScope) { LocalBuilder loc = il.DeclareLocal(Typeob.Int32); loc.SetLocalSymInfo("scopeId for catch block"); ConstantWrapper.TranslateToILInt(il, this.scopeId); il.Emit(OpCodes.Stloc, loc); } }
internal override void TranslateToIL(ILGenerator il, Type rtype) { base.context.EmitLineInfo(il); base.Globals.ScopeStack.Push(new WithObject(base.Globals.ScopeStack.Peek(), new JSObject(null, false))); bool insideProtectedRegion = base.compilerGlobals.InsideProtectedRegion; base.compilerGlobals.InsideProtectedRegion = true; Label item = il.DefineLabel(); base.compilerGlobals.BreakLabelStack.Push(item); base.compilerGlobals.ContinueLabelStack.Push(item); this.obj.TranslateToIL(il, Typeob.Object); base.EmitILToLoadEngine(il); il.Emit(OpCodes.Call, CompilerGlobals.jScriptWithMethod); LocalBuilder local = null; if (base.context.document.debugOn) { il.BeginScope(); local = il.DeclareLocal(Typeob.Object); local.SetLocalSymInfo("with()"); il.Emit(OpCodes.Stloc, local); } else { il.Emit(OpCodes.Pop); } il.BeginExceptionBlock(); this.block.TranslateToILInitializer(il); this.block.TranslateToIL(il, Typeob.Void); il.BeginFinallyBlock(); if (base.context.document.debugOn) { il.Emit(OpCodes.Ldnull); il.Emit(OpCodes.Stloc, local); } base.EmitILToLoadEngine(il); il.Emit(OpCodes.Call, CompilerGlobals.popScriptObjectMethod); il.Emit(OpCodes.Pop); il.EndExceptionBlock(); if (base.context.document.debugOn) { il.EndScope(); } il.MarkLabel(item); base.compilerGlobals.BreakLabelStack.Pop(); base.compilerGlobals.ContinueLabelStack.Pop(); base.compilerGlobals.InsideProtectedRegion = insideProtectedRegion; base.Globals.ScopeStack.Pop(); }
public void GenerateDeclaration(VariableDeclaration declNode, ILGenerator ilGenerator) { LocalBuilder localBuilder = ilGenerator.DeclareLocal(declNode.ReturnType.ConvertToType()); localBuilder.SetLocalSymInfo(declNode.Name); //add our local to dictionary varsLocalBuilders.Add(declNode.Name, localBuilder); if (declNode.Value != null) { GenerateExpression(declNode.Value, ilGenerator); //pop var from the stack and store in a value ilGenerator.Emit(OpCodes.Stloc, localBuilder); } }
/// <summary> /// 取值。 /// </summary> /// <param name="ilg">指令。</param> public override void Load(ILGenerator ilg) { if (local is null) { local = ilg.DeclareLocal(RuntimeType); #if NET40_OR_GREATER if (name?.Length > 0) { local.SetLocalSymInfo(name); } #endif } ilg.Emit(OpCodes.Ldloc, local); }
public override void Compile(CompileContext context) { Type procType = typeof(ProcessBase); ILGenerator il = context.ILGenerator; EmitDebug("Preparing to sync now...", context); LocalBuilder syncObject = il.DeclareLocal(typeof(ChannelSyncAction)); il.Emit(OpCodes.Ldarg_0); //this il.Emit(OpCodes.Ldstr, Name); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldc_I4, _children.Count); il.Emit(OpCodes.Ldc_I4_1); il.Emit(OpCodes.Newobj, typeof(ChannelSyncAction).GetConstructor(new Type[] { typeof(string), typeof(ProcessBase), typeof(int), typeof(bool) })); il.Emit(OpCodes.Stloc, syncObject); il.Emit(OpCodes.Ldloc, syncObject); il.Emit(OpCodes.Call, SyncMethod); context.MarkSequencePoint(this.LexicalInfo); //Save values to variables for (int i = 0; i < _children.Count; i++) { Variable var = (Variable)_children[i]; if (context.Options.Optimize && !var.IsUsed) { continue; } //Get the value to assign to it... il.Emit(OpCodes.Ldloc, syncObject); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Call, typeof(ChannelSyncAction).GetMethod("GetValue")); //...and assign it LocalBuilder local = context.Type.GetLocal(var.Name); if (local == null) //Inactions can be defining occurrences, so just create it... { local = context.ILGenerator.DeclareLocal(typeof(object)); if (context.Options.Debug) { local.SetLocalSymInfo(var.Name); } context.Type.Locals.Add(var.Name, local); } il.Emit(OpCodes.Stloc, local); } }
public void LocalVars(VarList v) { int max = v.Length(); for (int i = 0; i < max; i++) { Var e = v.FindByIndex(i); Type et = genDataTypeSig(e); LocalBuilder t = il.DeclareLocal(et); if (io.getGenDebug()) { t.SetLocalSymInfo(e.getName()); } e.setLocalToken(t); } localvars = v; }
public override void Compile(ILGenerator il) { EmitDebugInfo(il, 0, false); SanityCheck(); if (this.ChildNodes.Count > 0) { for (int i = 0; i < ChildNodes.Count - 1; i++) { this[i].Compile(il); } Procedure proc = WhileProgram.Instance.Procedures.GetByName(ProcedureName); if (proc.HasResultArgument) { Variable v = (Variable)this[ChildNodes.Count - 1]; //Create at first use if (Options.BookVersion && !SymbolTable.IsInScope(v.Name)) { SymbolTable.DefineVariable(v.Name); LocalBuilder lb = il.DeclareLocal(typeof(int)); if (Options.Debug) { lb.SetLocalSymInfo(v.Name); } } if (SymbolTable.IsResultArgument(v.Name)) { il.Emit(OpCodes.Ldarg, SymbolTable.GetValue(v.Name)); } else if (SymbolTable.IsArgument(v.Name)) { il.Emit(OpCodes.Ldarga, SymbolTable.GetValue(v.Name)); } else { il.Emit(OpCodes.Ldloca, SymbolTable.GetValue(v.Name)); } } else { this[ChildNodes.Count - 1].Compile(il); } } il.Emit(OpCodes.Call, WhileProgram.Instance.Procedures.Compiled[ProcedureName]); }
public void LocalVars(VarList v) { int max = v.Length(); for (int i = 0; i < max; i++) // loop thru the local params { Var e = v.FindByIndex(i); // indexed by number Type et = genDataTypeSig(e); // LocalToken t = emethod.DeclareLocal(et); LocalBuilder t = il.DeclareLocal(et); if (Io.gendebug) { t.SetLocalSymInfo(e.getName()); } e.setLocalToken(t); } localsdone = true; }
/// <summary> /// 赋值。 /// </summary> /// <param name="ilg">指令。</param> /// <param name="value">值。</param> protected override void Assign(ILGenerator ilg, AstExpression value) { if (local is null) { local = ilg.DeclareLocal(RuntimeType); #if NET40_OR_GREATER if (name?.Length > 0) { local.SetLocalSymInfo(name); } #endif } value.Load(ilg); ilg.Emit(OpCodes.Stloc, local); }