当前位置: 首页>>代码示例>>C#>>正文


C# ILGenerator.BeginScope方法代码示例

本文整理汇总了C#中System.Reflection.Emit.ILGenerator.BeginScope方法的典型用法代码示例。如果您正苦于以下问题:C# ILGenerator.BeginScope方法的具体用法?C# ILGenerator.BeginScope怎么用?C# ILGenerator.BeginScope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.ILGenerator的用法示例。


在下文中一共展示了ILGenerator.BeginScope方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GenerateCode

        public override void GenerateCode(ILGenerator codeGenerator, TypeBuilder typeBuilder, ModuleBuilder moduleBuilder)
        {
            Type elementType = Environment.GetCLRType(((ArrayType)ReturnType).ElementsType);

            codeGenerator.BeginScope();
            Label forStart = codeGenerator.DefineLabel();
            Label forEnd = codeGenerator.DefineLabel();
            LocalBuilder lengthBuilder = codeGenerator.DeclareLocal(typeof(int));
            LocalBuilder i = codeGenerator.DeclareLocal(typeof(int));
            codeGenerator.Emit(OpCodes.Ldc_I4_M1);
            codeGenerator.Emit(OpCodes.Stloc, i);

            SizeExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
            codeGenerator.Emit(OpCodes.Stloc, lengthBuilder);
            codeGenerator.Emit(OpCodes.Ldloc, lengthBuilder);
            codeGenerator.Emit(OpCodes.Newarr, elementType);

            codeGenerator.MarkLabel(forStart);

            // i++
            codeGenerator.Emit(OpCodes.Dup);
            codeGenerator.Emit(OpCodes.Ldloc, i);
            codeGenerator.Emit(OpCodes.Ldc_I4_1);
            codeGenerator.Emit(OpCodes.Add);
            codeGenerator.Emit(OpCodes.Stloc, i);
            codeGenerator.Emit(OpCodes.Ldloc, i);

            // if i >= lengthBuilder goto END
            codeGenerator.Emit(OpCodes.Ldloc, lengthBuilder);
            codeGenerator.Emit(OpCodes.Bge, forEnd);

            // Stack:   arrayRef / i / obj / Stelem
            codeGenerator.Emit(OpCodes.Ldloc, i);
            InitialValueExp.GenerateCode(codeGenerator, typeBuilder, moduleBuilder);
            codeGenerator.Emit(OpCodes.Stelem, elementType);

            codeGenerator.Emit(OpCodes.Br, forStart);

            codeGenerator.MarkLabel(forEnd);

            codeGenerator.Emit(OpCodes.Pop);
            codeGenerator.EndScope();
        }
开发者ID:sandygk,项目名称:Tiger-Compiler,代码行数:43,代码来源:ArrayCreationNode.cs

示例2: TranslateToIL

 internal override void TranslateToIL(ILGenerator il, Type rtype){
   //This assumes that rtype == Void.class.
   bool savedInsideProtectedRegion = compilerGlobals.InsideProtectedRegion;
   compilerGlobals.InsideProtectedRegion = true;
   compilerGlobals.BreakLabelStack.Push(compilerGlobals.BreakLabelStack.Peek(0));
   compilerGlobals.ContinueLabelStack.Push(compilerGlobals.ContinueLabelStack.Peek(0));
   il.BeginExceptionBlock(); 
   if (this.finally_block != null){
     if (this.finallyHasControlFlowOutOfIt)
       il.BeginExceptionBlock();
     if (this.handler != null) 
       il.BeginExceptionBlock();
   }
   this.body.TranslateToIL(il, Typeob.Void);
   if (this.tryEndContext != null)
     this.tryEndContext.EmitLineInfo(il);
   if (this.handler != null){
     if (this.type == null){
       il.BeginCatchBlock(typeof(Exception));
       this.handler.context.EmitLineInfo(il);
       this.EmitILToLoadEngine(il);
       il.Emit(OpCodes.Call, CompilerGlobals.jScriptExceptionValueMethod);
     }else{
       Type filterType = this.type.ToType();
       if (typeof(Exception).IsAssignableFrom(filterType)){
         il.BeginCatchBlock(filterType);
         this.handler.context.EmitLineInfo(il);
       }
       else{
         il.BeginExceptFilterBlock();
         this.handler.context.EmitLineInfo(il);
         this.EmitILToLoadEngine(il);
         il.Emit(OpCodes.Call, CompilerGlobals.jScriptExceptionValueMethod);
         il.Emit(OpCodes.Isinst, filterType);
         il.Emit(OpCodes.Ldnull);
         il.Emit(OpCodes.Cgt_Un);
         il.BeginCatchBlock(null);
         this.EmitILToLoadEngine(il);
         il.Emit(OpCodes.Call, CompilerGlobals.jScriptExceptionValueMethod);
         Convert.Emit(this, il, Typeob.Object, filterType);
       }
     }
     Object tok = this.field is JSVariableField ? ((JSVariableField)this.field).GetMetaData() : this.field;
     if (tok is LocalBuilder)
       il.Emit(OpCodes.Stloc, (LocalBuilder)tok);
     else if (tok is FieldInfo)
       il.Emit(OpCodes.Stsfld, (FieldInfo)tok);
     else
       Convert.EmitLdarg(il, (short)tok);
     if (this.handler_scope != null){
       if (!this.handler_scope.isKnownAtCompileTime){ //I.e. eval or nested func
         this.EmitILToLoadEngine(il);
         il.Emit(OpCodes.Ldstr, this.fieldName);
         ConstantWrapper.TranslateToILInt(il, this.handler_scope.scopeId);
         il.Emit(OpCodes.Call, typeof(Try).GetMethod("PushHandlerScope"));
         Globals.ScopeStack.Push(this.handler_scope);
       }
       il.BeginScope(); // so that we can emit local scoped information for the handler variable
       if (this.context.document.debugOn)
         this.handler_scope.EmitLocalInfoForFields(il);
     }
     this.handler.TranslateToIL(il, Typeob.Void);
     if (this.handler_scope != null){
       il.EndScope(); 
       if (!this.handler_scope.isKnownAtCompileTime){ //I.e. eval or nested func
         this.EmitILToLoadEngine(il);
         il.Emit(OpCodes.Call, CompilerGlobals.popScriptObjectMethod);
         il.Emit(OpCodes.Pop);
         Globals.ScopeStack.Pop();
       }
     }
     il.EndExceptionBlock();
   }
   if (this.finally_block != null){
     bool savedInsideFinally = compilerGlobals.InsideFinally;
     int savedFinallyStackTop = compilerGlobals.FinallyStackTop;
     compilerGlobals.InsideFinally = true;
     compilerGlobals.FinallyStackTop = compilerGlobals.BreakLabelStack.Size();
     il.BeginFinallyBlock();
     this.finally_block.TranslateToIL(il, Typeob.Void);
     il.EndExceptionBlock();
     compilerGlobals.InsideFinally = savedInsideFinally;
     compilerGlobals.FinallyStackTop = savedFinallyStackTop;
     if (this.finallyHasControlFlowOutOfIt){
       il.BeginCatchBlock(typeof(BreakOutOfFinally));
       il.Emit(OpCodes.Ldfld, typeof(BreakOutOfFinally).GetField("target"));
       // don't need to go to 0 in the loop because 0 is the outmost block (i.e. function body)
       // and that would generate a JIT assert because the jump is sometimes outside the function
       for (int i = compilerGlobals.BreakLabelStack.Size()-1, n = i; i > 0; i--){
         il.Emit(OpCodes.Dup);
         ConstantWrapper.TranslateToILInt(il, i);
         Label lab = il.DefineLabel();
         il.Emit(OpCodes.Blt_S, lab);
         il.Emit(OpCodes.Pop);
         if (savedInsideFinally && i < savedFinallyStackTop)
           il.Emit(OpCodes.Rethrow);
         else
           il.Emit(OpCodes.Leave, (Label)compilerGlobals.BreakLabelStack.Peek(n-i));
         il.MarkLabel(lab);
       }
//.........这里部分代码省略.........
开发者ID:ArildF,项目名称:masters,代码行数:101,代码来源:try.cs

示例3: TranslateToIL

      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();
      }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:44,代码来源:with.cs

示例4: Compile

 public override void Compile(ILGenerator il)
 {
     SymbolTable.PushScope();
     il.BeginScope();
     EmitDebugInfo(il, 0, true);
     if (Options.Debug) {
         il.Emit(OpCodes.Nop); //To step correctly
     }
     if (Variables != null) {
         Variables.Compile(il);
     }
     Statements.Compile(il);
     il.EndScope();
     SymbolTable.PopScope();
     EmitDebugInfo(il, 1, true);
 }
开发者ID:einaregilsson,项目名称:While-Language,代码行数:16,代码来源:Statements.cs

示例5: TranslateToIL

 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();
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:47,代码来源:With.cs

示例6: HandleStatement


//.........这里部分代码省略.........
                                            ILgen.Emit(OpCodes.Stloc, (LocalBuilder)local.Base); //Assigns the number to the variable.

                                            locals.Add(local); //Remembers the variable.
                                            i += 1;
                                        }
                                        else if (next.Token.Type == TokenType.COMMA)
                                        {
                                            //An array.

                                            dynamic info = GetLineAndCol(code, stmt.Token.StartPos);

                                            Console.Error.WriteLine("Error: Static arrays are not supported at this time. Line: {0}, Col: {1}", info.Line, info.Col);
                                            err = true;
                                            return;
                                        }
                                        else
                                        {
                                            #region Iterating Variable
                                            //Its a range
                                            var lowNum = stmt.Nodes[i + 2]; //Name is mis-informing. This is really the first number.
                                            var highNum = stmt.Nodes[i + 5]; //Same ^^, this is the second number.

                                            LocalBuilderEx local = new LocalBuilderEx();

                                            local.LoopHigh = int.Parse(highNum.Token.Text);
                                            local.LoopLow = int.Parse(lowNum.Token.Text);

                                            local.Name = token.Token.Text;
                                            local.Type = LocalType.LoopVar;
                                            local.VariableType = typeof(int);

                                            var looplab = ILgen.DefineLabel();

                                            ILgen.BeginScope();
                                            local.Base = ILgen.DeclareLocal(local.VariableType);

                                            local.LoopLabel = looplab;

                                            if (IsDebug) local.Base.SetLocalSymInfo(token.Token.Text); //Set variable name for debug info.

                                            ILgen.Emit(OpCodes.Ldc_I4, local.LoopLow); //Sets the number
                                            ILgen.Emit(OpCodes.Stloc, (LocalBuilder)local.Base); //Assigns the number to the variable.

                                            ILgen.MarkLabel(looplab);
                                            //this is where the IL will execute.

                                            local.LoopAction = () =>
                                            {

                                                //Updates the iterator by 1
                                                ILgen.Emit(OpCodes.Ldloc, local.Base);
                                                ILgen.Emit(OpCodes.Ldc_I4_1);

                                                if (local.LoopLow < local.LoopHigh)
                                                {
                                                    ILgen.Emit(OpCodes.Add); //Loop up
                                                    local.LoopDirection = LoopDirectionEnum.Up;
                                                }
                                                else if (local.LoopLow > local.LoopHigh)
                                                {
                                                    ILgen.Emit(OpCodes.Sub); //Loop down.
                                                    local.LoopDirection = LoopDirectionEnum.Down;
                                                }
                                                else
                                                {
                                                    dynamic info = GetLineAndCol(code, stmt.Token.StartPos);
开发者ID:GunioRobot,项目名称:Project-Mizu,代码行数:67,代码来源:Program.cs

示例7: ProduceInitCode

        /// <summary>
        /// Produces BetterFuck initialization code to specified generator.
        /// </summary>
        private static void ProduceInitCode(ILGenerator ilGenerator)
        {
            ilGenerator.BeginScope();

            ilGenerator.DeclareLocal(typeof(Engine));
            ilGenerator.Emit(OpCodes.Ldc_I4, DefaultMemorySize);

            var constructorInfo = typeof(Engine).GetConstructor(new[] { typeof(int) });
            ilGenerator.Emit(OpCodes.Newobj, constructorInfo);
            ilGenerator.Emit(OpCodes.Stloc_0);
        }
开发者ID:ForNeVeR,项目名称:BetterFuck,代码行数:14,代码来源:Emitter.cs

示例8: GenCode

        public override void GenCode(TypeBuilder tb, MethodBuilder mb, ILGenerator cg)
        {
            Type elementType;
            if (Value.ReturnType.Name == "nil")
                elementType = Compiler.SearchType(Name);
            else
                elementType = Compiler.SearchType(Value.ReturnType.Name);

            Type tipoArray = elementType.MakeArrayType();//int[]

            LocalBuilder array = cg.DeclareLocal(tipoArray);//int[]array
            Length.GenCode(tb, mb, cg);

            cg.Emit(OpCodes.Dup);
            cg.Emit(OpCodes.Newarr, elementType);//int[5]
            cg.Emit(OpCodes.Stloc, array);//array=int[5]

            LocalBuilder extremoIntervalo = cg.DeclareLocal(typeof(int));
            cg.Emit(OpCodes.Stloc, extremoIntervalo);//guarda el tamnno del array

            cg.BeginScope();//crea un nuevo scope que al final se cierrra
            LocalBuilder value = cg.DeclareLocal(elementType);

            //comienzo a llenar el array..es un for cuando lo desemsamblo
            System.Reflection.Emit.Label star = cg.DefineLabel();
            System.Reflection.Emit.Label end = cg.DefineLabel();

            LocalBuilder i = cg.DeclareLocal(typeof(int));
            cg.Emit(OpCodes.Ldc_I4_0);
            cg.Emit(OpCodes.Stloc, i);

            cg.MarkLabel(star);
            cg.Emit(OpCodes.Ldloc, i);
            cg.Emit(OpCodes.Ldloc, extremoIntervalo);
            cg.Emit(OpCodes.Bge, end);

            Value.GenCode(tb, mb, cg);
            cg.Emit(OpCodes.Stloc, value);

            cg.Emit(OpCodes.Ldloc, array);
            cg.Emit(OpCodes.Ldloc, i);
            cg.Emit(OpCodes.Ldloc, value);

            cg.Emit(OpCodes.Stelem, value.LocalType);

            cg.Emit(OpCodes.Ldc_I4_1);
            cg.Emit(OpCodes.Ldloc, i);
            cg.Emit(OpCodes.Add);
            cg.Emit(OpCodes.Stloc, i);
            cg.Emit(OpCodes.Br, star);

            cg.MarkLabel(end);

            cg.EndScope();
            cg.Emit(OpCodes.Ldloc, array);
        }
开发者ID:oisbel,项目名称:TigerCompiler,代码行数:56,代码来源:ArrayNode.cs

示例9: GenStmt


//.........这里部分代码省略.........

                var ilMeth = symbolTable.functionTable[functionName].GetILGenerator();
                GenStmt(stmt.ChildNodes[1], ilMeth, localSymbols);
                ilMeth.Emit(OpCodes.Ret);
            }
            else if (stmt.Term.Name == "result")
            {
                GenExpr(stmt.ChildNodes[1], TypeOfExpr(stmt.ChildNodes[1], symbolTable), il, symbolTable);
                var result = il.DeclareLocal(TypeOfExpr(stmt.ChildNodes[1], symbolTable));
                il.Emit(OpCodes.Stloc, result);
                il.Emit(OpCodes.Ldloc, result);
                il.Emit(OpCodes.Ret, result);
            }
            else if (stmt.Term.Name == "functionCall" | stmt.Term.Name == "memberCall")
            {
                GenExpr(stmt, null, il, symbolTable);
            }
            else if (stmt.Term.Name == "ifBlock")
            {
                Label ifTrue = il.DefineLabel();
                Label ifFalse = il.DefineLabel();
                Label endLabel = il.DefineLabel();

                GenExpr(stmt.ChildNodes[0], typeof(bool), il, symbolTable);//expression to check if true
                il.Emit(OpCodes.Brtrue, ifTrue);//if true then jump to true block
                il.Emit(OpCodes.Br, ifFalse);//otherwise jump to false block

                il.MarkLabel(ifTrue);//true block
                GenStmt(stmt.ChildNodes[1], il, symbolTable);
                il.Emit(OpCodes.Br, endLabel);//jump to after false block

                il.MarkLabel(ifFalse);//false block
                if (stmt.ChildNodes[2].ChildNodes.Count > 0)//then there's an else-if, this takes place in the else section
                {
                    ParseTreeNode elseBlockStmt = stmt.ChildNodes[2];//Turn the elsif to an inner if statement
                    elseBlockStmt.ChildNodes.Add(stmt.ChildNodes[3]);//Move the optional else statement to the inner if statement
                    elseBlockStmt.Term.Name = "ifBlock";

                    GenStmt(elseBlockStmt, il, symbolTable);
                }
                else if (stmt.ChildNodes[3].ChildNodes.Count > 0)
                    GenStmt(stmt.ChildNodes[3].ChildNodes[0], il, symbolTable);//generate expresson for false section, otherwise the label will be at the same spot as the end

                il.MarkLabel(endLabel);//the end of the if statement
            }
            else if (stmt.Term.Name == "loop")
            {
                Label beginLoop = il.DefineLabel();
                Label endLoop = il.DefineLabel();
                il.MarkLabel(beginLoop);
                GenStmt(stmt.ChildNodes[0], il, symbolTable, endLoop);
                il.Emit(OpCodes.Br, beginLoop);
                il.MarkLabel(endLoop);
            }
            else if (stmt.Term.Name == "forLoop")
            {
                il.BeginScope();

                Label beginLoop = il.DefineLabel();
                Label endLoop = il.DefineLabel();
                LocalBuilder i = il.DeclareLocal(typeof(int));
                string identName = stmt.ChildNodes[1].Token.ValueString;
                symbolTable.AddLocal(identName, i);
                symbolTable.AddLocal("___endLoop", il.DeclareLocal(typeof(int)));
                if (stmt.ChildNodes[2].ChildNodes.Count == 1)//then an identifier is used as a range, or char. We just fail for now
                    throw new NotImplementedException();
                else
                {
                    GenExpr(stmt.ChildNodes[2].ChildNodes[0], typeof(int), il, symbolTable);
                    symbolTable.Store(identName, typeof(int), il);
                    GenExpr(stmt.ChildNodes[2].ChildNodes[1], typeof(int), il, symbolTable);
                    symbolTable.Store("___endLoop", typeof(int), il);
                }
                il.MarkLabel(beginLoop);
                GenStmt(stmt.ChildNodes[4], il, symbolTable, endLoop);
                symbolTable.PushVar(identName, il);
                il.Emit(OpCodes.Ldc_I4_1);
                if (stmt.ChildNodes[3].ChildNodes.Count > 0)//then there is a decreasing statement, so do decreasing
                    il.Emit(OpCodes.Sub);
                else
                    il.Emit(OpCodes.Add);
                il.Emit(OpCodes.Dup);
                symbolTable.Store(identName, typeof(int), il);
                symbolTable.PushVar("___endLoop", il);

                if (stmt.ChildNodes[3].ChildNodes.Count > 0)//then there is a decreasing statement, so do decreasing
                    il.Emit(OpCodes.Bge, beginLoop);
                else
                    il.Emit(OpCodes.Ble, beginLoop);
                il.MarkLabel(endLoop);
                symbolTable.RemoveLocal(identName);
                symbolTable.RemoveLocal("___endLoop");

                il.EndScope();
            }
            else
            {
                throw new System.Exception("don't know how to gen a " + stmt.Term.Name);
            }
        }
开发者ID:mirhagk,项目名称:IronTuring,代码行数:101,代码来源:CodeGen.cs


注:本文中的System.Reflection.Emit.ILGenerator.BeginScope方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。