Beispiel #1
0
        internal void CopySimple(CodeGenContext context, Scope scope)
        {
            if (block != null)
            {
                // locals.block = block;
                string bname = ID.ToDotNetName(block.vid);
                context.ldloc(0);
                LoadBlock(context);
                context.stfld(scope.GetFrameField(bname));
            }

            for (Node f = normal; f != null; f = f.nd_next)
            {
                string fname = ID.ToDotNetName(((VAR)f).vid);

                // local.f = f;
                context.ldloc(0);
                context.ldarg(fname);
                context.stfld(scope.GetFrameField(fname));
            }
        }
Beispiel #2
0
        internal void AddScopeLocals(CodeGenContext context)
        {
            // ------------------ Start new Context ----------------------------

            // [InteropMethod("MyClass")]
            // private class ActivationFrame: Ruby.Frame { ... }
            frame_def = context.CreateGlobalClass("_Internal", "Frame" + (N++), Runtime.FrameRef);

            Scope parentClass;
            for (parentClass = this; parentClass != null && !(parentClass is CLASS_OR_MODULE); parentClass = parentClass.parent_scope) ;

            string className = "";
            if (parentClass != null)
                className = ((CLASS_OR_MODULE)parentClass).internal_name;

            ClassDef fileClass = FileClass();
            string src = "";
            if (fileClass != null && fileClass.Name().StartsWith("SourceFile_"))
            {
                src = fileClass.Name().Substring(11);
                frame_def.AddCustomAttribute(Runtime.FrameAttribute.ctor, new Constant[] { new StringConst(src), new StringConst(className) });
            }

            foreach (string local in locals_list)
                CodeGenContext.AddField(frame_def, PERWAPI.FieldAttr.Public, ID.ToDotNetName(local), PrimitiveType.Object);

            // internal ActivationFrame(Frame caller): base(caller) { }
            CodeGenContext frame_ctor = context.CreateConstructor(frame_def, new Param(ParamAttr.Default, "caller", Runtime.FrameRef));
            frame_ctor.ldarg(0);
            frame_ctor.ldarg("caller");
            frame_ctor.call(Runtime.Frame.ctor);
            frame_ctor.ret();
            frame_ctor.Close();

            // internal string file() {
            CodeGenContext file = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "file", PrimitiveType.String);

            file.startMethod(this.location);
            //    return "thisfile.rb"
            file.ldstr(this.location.file);
            file.ret();
            file.Close();

            // internal override string methodName() {
            CodeGenContext methodName = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "methodName", PrimitiveType.String);
            methodName.startMethod(this.location);
            //    return "CurrentMethodName"
            methodName.ldstr(CurrentMethodName());
            methodName.ret();
            methodName.Close();

            CreateNestingMethod(frame_def, context);
            CreateLastClassMethod(frame_def, context);

            // ------------------ Return to Old Context ----------------------

            // ActivationFrame frame = new ActivationFrame(caller);
            context.ldarg("caller");
            context.newobj(frame_ctor.Method);
            int frame = context.StoreInTemp("frame", FrameClass, location);
            Debug.Assert(frame == 0);

            // frame.block_arg = block;
            context.ldloc(frame);
            LoadBlock0(context);
            context.stfld(Runtime.Frame.block_arg);

            if (this is BLOCK)
            {
                // frame.current_block = this;
                context.ldloc(frame);
                context.ldarg(0);
                context.stfld(Runtime.Frame.current_block);
            }
        }
        internal override void Assign(CodeGenContext context, Node rhs)
        {
            // object value = rhs;
            bool created;
            ISimple value = context.PreCompute(rhs, "rhs", out created);

            // thisblock.localsN.vid = value;
            context.ldarg(0);
            context.ldfld(block.frameFields[depth - 1]);
            value.GenSimple(context);
            context.stfld(field);

            value.GenSimple(context);

            context.ReleaseLocal(value, created);
        }
        internal override void Assign(CodeGenContext context, Node rhs)
        {
            // object value = rhs;
            bool created;
            ISimple value = context.PreCompute(rhs, "rhs", out created);

            // locals.field = value
            context.ldloc(0);
            value.GenSimple(context);
            context.stfld(field);

            GenCode0(context);

            context.ReleaseLocal(value, created);
        }
Beispiel #5
0
 internal void SetLine(CodeGenContext context)
 {
     if (location != null)
     {
         // frame.line = thisline
         context.ldloc(0);
         context.ldc_i4(this.location.first_line);
         context.stfld(Runtime.Frame.line);
     }
 }
        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 void GenRescue(CodeGenContext context, PERWAPI.CILLabel endLabel, int RescueTemp, RESCUE_CLAUSE clauses)
        {
            // catch (System.Exception e) {

            int e = context.StoreInTemp("e", Runtime.SystemExceptionRef, location);

            //if (e is Ruby.ControlException)
            PERWAPI.CILLabel else1 = context.NewLabel();
            context.ldloc(e);
            context.isinst(Runtime.ControlExceptionRef);
            context.brfalse(else1);
            //    throw e;
            context.rethrow();
            context.CodeLabel(else1);

            // Ruby.Exception exception;
            int exception = context.CreateLocal("exception", Runtime.ExceptionRef);

            //if (!(e is Ruby.RubyException))
            PERWAPI.CILLabel else2 = context.NewLabel();
            PERWAPI.CILLabel end = context.NewLabel();
            context.ldloc(e);
            context.isinst(Runtime.RubyExceptionRef);
            context.brtrue(else2);
            //    exception = new Ruby.CLRException(frame, e);
            context.ldloc(0);
            context.ldloc(e);
            context.newobj(Runtime.CLRException.ctor);
            context.stloc(exception);
            context.br(end);

            //else
            context.CodeLabel(else2);
            //     exception = (Ruby.RubyException)e.parent;
            context.ldloc(e);
            context.cast(Runtime.RubyExceptionRef);
            context.ldfld(Runtime.RubyException.parent);
            context.stloc(exception);

            context.CodeLabel(end);

            //Eval.ruby_errinfo.value = exception;
            context.ldsfld(Runtime.Eval.ruby_errinfo);
            context.ldloc(exception);
            context.stfld(Runtime.global_variable.value);

            if (clauses != null)
                clauses.GenCode(context, endLabel, RescueTemp, exception);

            context.rethrow();

            context.ReleaseLocal(e, true);
            context.ReleaseLocal(exception, true);
        }
Beispiel #8
0
        internal override ISimple GenArgList(CodeGenContext context, out bool created)
        {
            bool single = true;

            // ArgList arglist = new ArgList();
            context.newobj(Runtime.ArgList.ctor);
            int arglist = context.StoreInTemp("arglist", Runtime.ArgListRef, location);

            int added = 0;
            for (Node arg = parameters; arg != null; arg = arg.nd_next)
            {
                //object argument = arg;
                bool argument_created;
                ISimple argument = context.PreCompute0(arg, "arg", out argument_created);

                // arglist.Add(argument);
                context.ldloc(arglist);
                argument.GenSimple(context);
                context.callvirt(Runtime.ArgList.Add);
                added++;

                context.ReleaseLocal(argument, argument_created);
            }

            if (added != 1)
                single = false;

            if (hashlist != null)
            {
                // object hash = hashlist;
                bool hash_created;
                ISimple hash = context.PreCompute(new HASH(hashlist, hashlist.location), "hashlist", out hash_created);

                // arglist.Add(hash);
                context.ldloc(arglist);
                hash.GenSimple(context);
                context.callvirt(Runtime.ArgList.Add);
                single = false;

                context.ReleaseLocal(hash, hash_created);
            }

            if (array != null)
            {
                // object list = array;
                bool list_created;
                ISimple list = context.PreCompute(array, "array", out list_created);

                // arglist.AddArray(list, caller);
                context.ldloc(arglist);
                list.GenSimple(context);
                context.ldloc(0);
                context.callvirt(Runtime.ArgList.AddArray);
                single = false;

                context.ReleaseLocal(list, list_created);
            }

            if (block != null)
            {
                // object b = block;
                bool b_created;
                ISimple b = context.PreCompute(block, "block", Runtime.ProcRef, out b_created);

                // arglist.block = b;
                context.ldloc(arglist);
                b.GenSimple(context);
                context.stfld(Runtime.ArgList.block);

                context.ReleaseLocal(b, b_created);
            }

            if (single)
            {
                context.ldloc(arglist);
                context.PushTrue();
                context.stfld(Runtime.ArgList.single_arg);
            }

            created = true;
            return new LOCAL(arglist, location);
        }
Beispiel #9
0
        private void CopyBlockFormal(CodeGenContext context, Scope scope)
        {
            if (block != null)
            {
                string name = ID.ToDotNetName(block.vid);

                // locals.name = args.block;
                context.ldloc(0);
                LoadBlock(context);
                context.stfld(scope.GetFrameField(name));
            }
        }
Beispiel #10
0
        private void CopyRestFormals(CodeGenContext context, Scope scope)
        {
            if (rest != null)
            {
                string name = ID.ToDotNetName(rest.vid);

                // locals.name = args.GetRest();
                context.ldloc(0);
                context.ldarg("args");
                context.callvirt(Runtime.ArgList.GetRest);
                context.stfld(scope.GetFrameField(name));
            }
        }
Beispiel #11
0
        private void CopyOptionalFormals(CodeGenContext context, Scope scope)
        {
            for (ASSIGNMENT opt = (ASSIGNMENT)optional; opt != null; opt = (ASSIGNMENT)opt.nd_next)
            {
                PERWAPI.CILLabel runout_label = context.NewLabel();
                PERWAPI.CILLabel end_label = context.NewLabel();

                string name = ID.ToDotNetName(((VAR)(opt.lhs)).vid);
                Node defaultValue = opt.rhs;

  

                // if (args.RunOut()) goto RunOut
                context.ldarg("args");
                context.callvirt(Runtime.ArgList.RunOut);
                context.brtrue(runout_label);

                // locals.name = args.GetNext();
                context.ldloc(0);
                context.ldarg("args");
                context.callvirt(Runtime.ArgList.GetNext);
                context.br(end_label);

                // RunOut:
                context.CodeLabel(runout_label);

                // object def = defaultValue;
                bool created;
                ISimple def = context.PreCompute(defaultValue, "default", out created);

                // locals.name = defaultValue
                context.ldloc(0);
                def.GenSimple(context);

                context.ReleaseLocal(def, created);

                context.CodeLabel(end_label);

                // locals.name = ...
                context.stfld(scope.GetFrameField(name));
            }
        }
Beispiel #12
0
        private void CopyNormalFormals(CodeGenContext context, Scope scope)
        {
            PERWAPI.CILLabel OKLabel = context.NewLabel();

            if (min_args > 0)
            {
                // if (args.Length < min_args)
                context.ldarg("args");
                context.callvirt(Runtime.ArgList.get_Length);
                int length = context.StoreInTemp("length", PrimitiveType.Int32, location);
                context.ldloc(length);
                context.ldc_i4(min_args);
                context.bge(OKLabel);

                //context.Inst(Op.clt);
                //context.brfalse(OKLabel);

                // context.Branch(BranchOp.bge, OKLabel);

                // throw new ArgumentError(string.Format("wrong number of arguments ({0} for {1})", args.Length, arity).raise(caller);
                // FIXME: next line needs a String
                context.ldstr("wrong number of arguments ({0} for {1})");
                context.ldloc(length);
                context.box(PrimitiveType.Int32);
                context.ldc_i4(min_args);
                context.box(PrimitiveType.Int32);
                context.call(Runtime.SystemString.Format);
                context.newobj(Runtime.ArgumentError.ctor);
                context.ldloc(0);
                context.callvirt(Runtime.Exception.raise);
                context.throwOp();

                context.ReleaseLocal(length, true);

                // OKLabel:
                context.CodeLabel(OKLabel);
            }

            // Copy parameters to locals
            for (Node f = normal; f != null; f = f.nd_next)
            {
                string name = ID.ToDotNetName(((VAR)f).vid);

                // local.f = args.GetNext();
                context.ldloc(0);
                context.ldarg("args");
                context.callvirt(Runtime.ArgList.GetNext);
                context.stfld(scope.GetFrameField(name));
            }
        }
Beispiel #13
0
        internal override void GenCode0(CodeGenContext context)
        {
            context.newLine(location);
            SetLine(context);

            if (args == null)
                args = ParentArgs(context);

            // object arguments = args;
            bool created;
            arguments = args.GenArgList(context, out created);

            if (parent_scope is BLOCK)
            {
                arguments.GenSimple(context);
                LoadBlock(context);
                context.stfld(Runtime.ArgList.block);
            }

            GenCall(context);

            context.ReleaseLocal(arguments, created);
        }