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 #2
0
        internal override void GenCode0(CodeGenContext context)
        {
            PERWAPI.CILLabel elseLabel = context.NewLabel();
            PERWAPI.CILLabel endLabel = context.NewLabel();

            // if (Eval.Test(cond))
            context.newLine(cond.location);
            cond.GenCode(context);
            context.call(Runtime.Eval.Test);
            context.brfalse(elseLabel);

            if (body != null)
            {
                context.newStartPoint(body.location);
                body.GenCode(context);
            }
            else
                context.ldnull();

            if (context.Reachable())
                context.br(endLabel);

            context.CodeLabel(elseLabel);


            if (_else != null)
            {
                context.newStartPoint(_else.location);
                _else.GenCode(context);
            }
            else
                context.ldnull();

            context.CodeLabel(endLabel);
            context.newEndPoint(location);
        }
        internal override void GenCode0(CodeGenContext context)
        {
            context.ldloc(0);
            context.call(Runtime.Frame.get_Tilde);
            PERWAPI.CILLabel label1 = context.NewLabel();
            context.brfalse(label1);
            context.ldloc(0);
            context.call(Runtime.Frame.get_Tilde);
            context.ldloc(0);

            switch (ch)
            {
                case '&':
                    context.callvirt(Runtime.Match.last_match);
                    break;
                case '`':
                    context.callvirt(Runtime.Match.match_pre);
                    break;               
                case '\'':
                    context.callvirt(Runtime.Match.match_post);
                    break;                
                case '+':
                    context.callvirt(Runtime.Match.match_last);
                    break;
                default:
                    throw new NotImplementedException("BACK_REF $" + ch);
            }
            PERWAPI.CILLabel label2 = context.NewLabel();
            context.br(label2);
            context.CodeLabel(label1);
            context.ldnull();
            context.CodeLabel(label2);
        }
         internal override void Defined(CodeGenContext context)
        {
            if (qualified)
                if (scope != null)
                {
                    // object result;
                    int result = context.CreateLocal("result", PrimitiveType.Object);
                    PERWAPI.CILLabel endLabel = context.NewLabel();
                    // try {
                    context.StartBlock(Clause.Try);
                    {
                        // result = Eval.const_defined(scope, vid, caller);
                        scope.GenCode(context);
                        context.ldstr(vid.ToString());
                        context.ldloc(0);
                        context.call(Runtime.Eval.const_defined);
                        context.stloc(result);
                        context.leave(endLabel);
                    }
                    TryBlock block = context.EndTryBlock();
                    // catch (System.Exception) {
                    context.StartBlock(Clause.Catch);
                    {
                        // result = null;
                        context.ldnull();
                        context.stloc(result);
                        context.leave(endLabel);
                    }
                    context.EndCatchBlock(Runtime.SystemExceptionRef, block);

                    context.CodeLabel(endLabel);
                    context.ldloc(result);
                    context.ReleaseLocal(result, true);
                }
                else
                {
                    context.ldsfld(Ruby.Compiler.Runtime.Init.rb_cObject);
                    context.ldstr(vid.ToString());
                    context.ldloc(0);
                    context.call(Runtime.Eval.const_defined);
                }
            else
            {
                context.ruby_cbase(parent_scope);
                context.ldstr(vid.ToString());
                context.ldloc(0);
                context.call(Runtime.Eval.const_defined);
            }
        }
 internal override void GenCode0(CodeGenContext context)
 {
     // ruby_class.undef_method(mid);
     context.newLine(location);
     context.ruby_class(parent_scope);
     context.ldstr(mid.ToString());
     context.callvirt(Runtime.Class.undef_method);
     context.ldnull();
 }
Beispiel #6
0
 internal void LoadBlock0(CodeGenContext context)
 {
     if (context.HasArg(Runtime.ArgListRef))
     {
         context.ldarg("args"); // args.block
         context.ldfld(Runtime.ArgList.block);
     }
     else if (context.HasArg(Runtime.ProcRef))
     {
         context.ldarg("block");
     }
     else
         context.ldnull();
 }
        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);
        }
Beispiel #8
0
        internal override void GenCode0(CodeGenContext context)
        {
            PERWAPI.CILLabel endLabel = context.NewLabel();
            Node clause;

            if (target != null)
            {
                context.newLine(target.location);
                target.GenCode(context);
                LOCAL t = context.StoreInLocal("target", PrimitiveType.Object, location);
                
                for (clause = body; clause != null && clause is WHEN; clause = clause.nd_next)
                    ((WHEN)clause).GenCode(context, t, endLabel);

                context.ReleaseLocal(t.local, true);
            }
            else
            {
                for (clause = body; clause != null && clause is WHEN; clause = clause.nd_next)
                {
                    context.newLine(clause.location);
                    ((WHEN)clause).GenCode(context, endLabel);
                }
            }

            if (clause != null) /* assume else clause */
                clause.GenCode(context);
            else
                context.ldnull();

            context.CodeLabel(endLabel);
            context.newEndPoint(location);
        }
        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 (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 #12
0
        internal override void GenCode0(CodeGenContext context)
        {
            // String.ObjectToString(code, caller);

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

            context.ldloc(0);

            context.call(Runtime.String.ObjectAsString);
        }
 public void GenSimple(CodeGenContext context)
 {
     context.ldnull();
 }
Beispiel #14
0
        internal void GenCode(CodeGenContext context, PERWAPI.CILLabel endLabel)
        {
            PERWAPI.CILLabel elseLabel = context.NewLabel();

            context.newLine(comparison.location);
            // if (comparision.ToRubyArray().includes(true, caller))
            bool created;
            ISimple list = comparison.GenArgList(context, out created);
            list.GenSimple(context);
            context.ReleaseLocal(list, created);
            context.callvirt(Runtime.ArgList.ToRubyArray);
            new TRUE(comparison.location).GenCode(context);
            context.ldloc(0);
            context.callvirt(Runtime.Array.includes);
            context.brfalse(elseLabel);

            if (body != null)
            {
                context.newStartPoint(body.location);
                body.GenCode(context);
            }
            else
                context.ldnull();

            context.br(endLabel);
            context.CodeLabel(elseLabel);
        }
Beispiel #15
0
 internal void DefineMethod(CodeGenContext context)
 {
     // ... .define_method("MyMethod", MyMethod.singleton, arity, caller);
     context.ldstr(method_id.ToString());
     context.ldsfld(GenerateClassForMethod(context));
     context.ldc_i4(formals.arity);
     context.ldloc(0);
     context.callvirt(Runtime.Class.define_method);
     context.ldnull();
 }
        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);
            }
        }
Beispiel #17
0
 internal override void GenCode0(CodeGenContext context)
 {
     if (body != null)
         body.GenCode(context);
     else
         context.ldnull();
 }
Beispiel #18
0
        internal override void GenCode0(CodeGenContext context)
        {
            Labels original = context.labels;

            // ---------------- Create new label context for loop ----------------------
            context.labels = new Labels();
            context.labels.Next = context.NewLabel();
            context.labels.Break = context.NewLabel();
            context.labels.Redo = context.NewLabel();
            context.labels.Retry = context.NewLabel();
            context.labels.Return = original.Return;

            context.CodeLabel(context.labels.Retry);

            context.newStartPoint(location);

            context.ldnull();
            context.stloc(parent_scope.returnTemp);

            context.CodeLabel(context.labels.Redo);

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

            context.CodeLabel(context.labels.Next);

            // if (Eval.Test(cond))
            context.newLine(cond.location);
            cond.GenCode(context);
            context.call(Runtime.Eval.Test);
            if (whileTrue)
                context.brtrue(context.labels.Retry);
            else
                context.brfalse(context.labels.Retry);

            context.CodeLabel(context.labels.Break);

            context.newEndPoint(location);

            context.ldloc(parent_scope.returnTemp);

            // --------------------- Restore Label context -------------------------
            context.labels = original;
        }