Beispiel #1
0
        internal void GenCall(CodeGenContext context)
        {
            int result = context.CreateLocal("result", PrimitiveType.Object);

            PERWAPI.CILLabel endLabel = context.NewLabel();
            PERWAPI.CILLabel retryLabel = context.NewLabel();

            context.CodeLabel(retryLabel);

            context.StartBlock(Clause.Try);
            {
                // object result = Call(...)
                GenCall0(context);
                context.stloc(result);

                context.Goto(endLabel);
            }
            PERWAPI.TryBlock tryBlock = context.EndTryBlock();
            context.StartBlock(Clause.Catch);
            {
                CatchBreakException(context, result, endLabel);
            }
            context.EndCatchBlock(Runtime.BreakExceptionRef, tryBlock);
            context.StartBlock(Clause.Catch);
            {
                CatchRetryException(context, retryLabel);
            }
            context.EndCatchBlock(Runtime.RetryExceptionRef, tryBlock);

            context.CodeLabel(endLabel);
            context.ldloc(result);

            context.ReleaseLocal(result, true);
        }
        internal override void GenCode0(CodeGenContext context)
        {
            PERWAPI.CILLabel finalLabel = context.NewLabel();

            int RescueTemp = context.CreateLocal("rescueTemp", PERWAPI.PrimitiveType.Object);
            context.ldnull();
            context.stloc(RescueTemp);

            if (ensure != null)
            {
                context.StartBlock(Clause.Try); // outer try block with finally

                context.StartBlock(Clause.Try); // inner try block with catch
            }

            GenInnerBlock(context, RescueTemp);

            if (ensure != null)
            {
                context.Goto(finalLabel);

                PERWAPI.TryBlock innerTry = context.EndTryBlock();

                context.StartBlock(Clause.Catch);
                GenRescue(context, null, 0, null);
                context.EndCatchBlock(Runtime.SystemExceptionRef, innerTry);

                PERWAPI.TryBlock outerTry = context.EndTryBlock();
                
                // Fixme: reset labels to prevent branches out of finally block.    
                context.StartBlock(Clause.Finally);
                ensure.GenCode(context);
                if (context.Reachable())
                    context.pop();
                context.endfinally();
                context.EndFinallyBlock(outerTry);

                context.CodeLabel(finalLabel);
                context.newEndPoint(location);
            }

            context.ldloc(RescueTemp);

            context.ReleaseLocal(RescueTemp, true);
        }
Beispiel #3
0
        internal void CatchReturnException(CodeGenContext context, PERWAPI.TryBlock tryBlock)
        {
            // catch (Ruby.ReturnException exception) { ... }
            context.StartBlock(Clause.Catch);
            {
                PERWAPI.CILLabel falseLabel = context.NewLabel();

                int exception = context.StoreInTemp("exception", Runtime.ReturnExceptionRef, location);

                // if (exception.scope == thisframe)
                context.ldloc(exception);
                context.ldfld(Runtime.ReturnException.scope);
                context.ldloc(0);
                context.bne(falseLabel);

                //     returnTemp = exception.return_value;
                context.ldloc(exception);
                context.ldfld(Runtime.ReturnException.return_value);
                context.stloc(returnTemp);
                context.Goto(context.labels.Return);

                // falseLabel:
                context.CodeLabel(falseLabel);
                // throw exception
                context.rethrow();

                context.ReleaseLocal(exception, true);
            }
            context.EndCatchBlock(Runtime.ReturnExceptionRef, tryBlock);
        }
Beispiel #4
0
        internal void AddScopeBody(CodeGenContext context)
        {
            returnTemp = context.CreateLocal("returnTemp", PrimitiveType.Object);

            context.labels = new Labels();
            context.labels.Redo = context.NewLabel();
            context.labels.Return = context.NewLabel();

            // try { ... }
            context.StartBlock(Clause.Try);
            {
                if (BEGIN != null)
                    BEGIN.GenCode(context);

                context.CodeLabel(context.labels.Redo);

                if (body != null)
                {
                    body.GenCode(context);

                    if (context.Reachable())
                        context.stloc(returnTemp);
                }

                context.Goto(context.labels.Return);
            }
            PERWAPI.TryBlock tryBlock = context.EndTryBlock();        

            CatchReturnException(context, tryBlock);

            // ReturnLabel:
            //    return returnTemp;
            context.CodeLabel(context.labels.Return);
            context.newEndPoint(location);
            if (context.Method.GetRetType() != PERWAPI.PrimitiveType.Void)
                context.ldloc(returnTemp);
            context.ret();

            context.ReleaseLocal(returnTemp, true);
        }
        private void GenInnerBlock(CodeGenContext context, int RescueTemp)
        {
            PERWAPI.CILLabel elseLabel = context.NewLabel();

            Labels catchLabels = new Labels();
            catchLabels.Break = context.labels.Break;
            catchLabels.Next = context.labels.Next;
            catchLabels.Redo = context.labels.Redo;
            catchLabels.Return = context.labels.Return;
            catchLabels.Retry = context.NewLabel();

            context.CodeLabel(catchLabels.Retry);

            context.StartBlock(Clause.Try);
            {
                if (body != null)
                    body.GenCode(context);
                else
                    context.ldnull();

                if (context.Reachable())
                {
                    context.stloc(RescueTemp);
                    context.Goto(elseLabel);
                }
            }
            PERWAPI.TryBlock innerTry = context.EndTryBlock();

            PERWAPI.CILLabel endLabel = context.NewLabel();

            if (rescue != null)
            {
                context.StartBlock(Clause.Catch);
                {
                    Labels original = context.labels;
                    context.labels = catchLabels;

                    GenRescue(context, endLabel, RescueTemp, rescue);
                    
                    context.labels = original;
                }
                context.EndCatchBlock(Runtime.SystemExceptionRef, innerTry);
            }

            context.CodeLabel(elseLabel);
            {
                if (_else != null)
                {
                    _else.GenCode(context);
                    if (context.Reachable())
                        context.stloc(RescueTemp);
                }
            }
            context.CodeLabel(endLabel);
        }
        internal override void GenCode0(CodeGenContext context)
        {
            int RescueTemp = context.CreateLocal("rescueTemp", PERWAPI.PrimitiveType.Object);
            int ExceptionTemp = context.CreateLocal("exceptionTemp", PERWAPI.PrimitiveType.Object);

            PERWAPI.CILLabel endLabel = context.NewLabel();

            context.StartBlock(Clause.Try);
            {
                expr.GenCode(context);
                if (context.Reachable())
                {
                    context.stloc(RescueTemp);
                    context.Goto(endLabel);
                }
            }
            PERWAPI.TryBlock tryBlock = context.EndTryBlock();

            context.StartBlock(Clause.Catch);
            {
                PERWAPI.CILLabel stdErrLabel = context.NewLabel();
                context.stloc(ExceptionTemp);
                context.ldloc(ExceptionTemp);
                context.ldfld(Runtime.RubyException.parent);
                context.isinst(Runtime.StandardErrorRef);
                context.brtrue(stdErrLabel);
                context.ldloc(ExceptionTemp);
                context.throwOp();
                context.CodeLabel(stdErrLabel);
                rescue.GenCode(context);
                if (context.Reachable())
                {
                    context.stloc(RescueTemp);
                    context.Goto(endLabel);
                }
            }

            context.EndCatchBlock(Runtime.RubyExceptionRef, tryBlock);

            context.CodeLabel(endLabel);

            context.ldloc(RescueTemp);

            context.ReleaseLocal(RescueTemp, true);
            context.ReleaseLocal(ExceptionTemp, true);
        }
        internal void GenCode(CodeGenContext context, PERWAPI.CILLabel endLabel, int RescueTemp, int exception)
        {
            for (RESCUE_CLAUSE clause = this; clause != null; clause = clause.next)
            {
                PERWAPI.CILLabel nextClause = context.NewLabel();
                PERWAPI.CILLabel thisClause = context.NewLabel();

                context.ldc_i4(0);
                LOCAL exceptionCaught = context.StoreInLocal("caught", PERWAPI.PrimitiveType.Boolean, this.location);

                for (Node type = clause.types; type != null; type = type.nd_next)
                {
                    PERWAPI.CILLabel label1 = context.NewLabel();

                    // Precompute each separately to avoid computing a list of types
                    type.GenCode0(context);
                    LOCAL tt = context.StoreInLocal("type", PERWAPI.PrimitiveType.Object, type.location);

                    new METHOD_CALL(tt, ID.intern(Tokens.tEQQ), new AST.LOCAL(exception, type.location), type.location).GenCode(context);

                    context.ReleaseLocal(tt.local, true);

                    context.call(Runtime.Eval.Test);
                    context.brfalse(label1);
                    context.PushTrue();
                    context.stloc(exceptionCaught.local);
                    context.CodeLabel(label1);                  
                }

                context.ldloc(exceptionCaught.local);
                context.brtrue(thisClause);
                context.ReleaseLocal(exceptionCaught.local, true);

                context.br(nextClause);

                context.CodeLabel(thisClause);

                if (clause.var != null)
                {
                    clause.var.Assign(context, new AST.LOCAL(exception, clause.var.location));
                    context.pop();
                }

                if (clause.body != null)
                    clause.body.GenCode(context);
                else
                    context.ldnull();

                if (context.Reachable())
                    context.stloc(RescueTemp);

                // reset $!
                //Eval.ruby_errinfo.value = null;
                context.ldsfld(Runtime.Eval.ruby_errinfo);
                context.ldnull();
                context.stfld(Runtime.global_variable.value);

                context.Goto(endLabel);

                context.CodeLabel(nextClause);
            }
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);

            if (return_val != null)
                return_val.GenCode(context);
            else
                context.ldnull();

            if (this.parent_scope is BLOCK)
            {
                //throw new Ruby.ReturnException(return_value, this.defining_scope);
                context.ldarg(0);  // current Ruby.MethodBody
                context.ldfld(Runtime.Block.defining_scope);
                context.newobj(Runtime.ReturnException.ctor);
                context.throwOp();
            }
            else if (this.parent_scope is BEGIN)
            {
                //throw new Ruby.ReturnException(return_value, caller);
                context.ldarg("caller");
                context.newobj(Runtime.ReturnException.ctor);
                context.throwOp();
            }
            else
            {
                // return
                context.stloc(parent_scope.returnTemp);
                context.Goto(context.labels.Return);
            }
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);

            if (context.labels != null && context.labels.Retry != null)
                context.Goto(context.labels.Retry);                
            else
            {
                System.Diagnostics.Debug.Assert(this.parent_scope is BLOCK);
                // throw new Ruby.RetryException(block.defining_scope);
                context.ldarg(0);  // current Ruby.MethodBody
                context.ldfld(Runtime.Block.defining_scope);
                context.newobj(Runtime.RetryException.ctor);
                context.throwOp();
            }
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);

            if (context.labels != null && context.labels.Redo != null)
                context.Goto(context.labels.Redo);
            else
                throw new System.Exception("unexpected REDO not in loop or block");
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);

            if (return_val != null)
                return_val.GenCode(context);
            else
                context.ldnull();

            context.stloc(parent_scope.returnTemp);

            if (context.labels != null && context.labels.Next != null)
                context.Goto(context.labels.Next);
            else
                context.Goto(context.labels.Return);
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);

            if (return_val != null)
                return_val.GenCode(context);
            else
                context.ldnull();

            if (context.labels != null && context.labels.Break != null)
            {
                context.stloc(parent_scope.returnTemp);
                context.Goto(context.labels.Break);
            }
            else
            {
                System.Diagnostics.Debug.Assert(this.parent_scope is BLOCK);
                //throw new Ruby.BreakException(return_value, this.defining_scope);
                context.ldarg(0);  // current Ruby.MethodBody
                context.ldfld(Runtime.Block.defining_scope);
                context.newobj(Runtime.BreakException.ctor);
                context.throwOp();
            }
        }
Beispiel #13
0
        private void CatchBreakException(CodeGenContext context, int result, PERWAPI.CILLabel endLabel)
        {
            // catch (Exception exception)
            int exception = context.StoreInTemp("exception", Runtime.BreakExceptionRef, location);
            PERWAPI.CILLabel reThrowLabel = context.NewLabel();

            // if (exception.scope != current_frame) goto reThrowLabel; 
            context.ldloc(0);
            context.ldloc(exception);
            context.ldfld(Runtime.BreakException.scope);
            context.bne( reThrowLabel);

            // result = exception.return_value;
            context.ldloc(exception);
            context.ldfld(Runtime.BreakException.return_value);
            context.stloc(result);

            // goto endLabel;
            context.Goto(endLabel);

            // reThrowLabel:
            context.CodeLabel(reThrowLabel);

            // throw exception;
            context.ldloc(exception);
            context.throwOp();

            context.ReleaseLocal(exception, true);
        }
Beispiel #14
0
        private void CatchRetryException(CodeGenContext context, PERWAPI.CILLabel retryLabel)
        {
            // catch (Exception exception)
            int exception = context.StoreInTemp("exception", Runtime.RetryExceptionRef, location);
            PERWAPI.CILLabel reThrowLabel = context.NewLabel();

            // if (exception.scope != current_frame) goto reThrowLabel; 
            context.ldloc(0);
            context.ldloc(exception);
            context.ldfld(Runtime.RetryException.scope);
            context.bne( reThrowLabel);

            // goto retryLabel
            context.Goto(retryLabel);

            // reThrowLabel:
            context.CodeLabel(reThrowLabel);

            // throw exception;
            context.ldloc(exception);
            context.throwOp();

            context.ReleaseLocal(exception, true);
        }