Beispiel #1
0
        private MethodDef GenConstructor(ClassDef Class, CodeGenContext context)
        {
            // internal Block_N(outer locals ...): base(defining_scope) { ... }
            List<Param> parameters = new List<Param>();

            foreach (FieldDef field in frameFields)
                parameters.Add(new Param(ParamAttr.Default, field.Name(), field.GetFieldType()));

            CodeGenContext constructor = context.CreateConstructor(Class, parameters.ToArray());

            constructor.ldarg(0); // this
            constructor.ldarg(1); // defining_scope
            constructor.call(Runtime.Block.ctor);

            int N = 0;
            foreach (FieldDef field in frameFields)
            {
                // locals.field = localsN;
                constructor.ldarg(0);
                constructor.ldarg("locals" + (N++));
                constructor.stfld(field);
            }

            constructor.ret();

            constructor.Close();

            return constructor.Method;
        }
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);
            }
        }
Beispiel #3
0
        private FieldDef GenerateClassForMethod(CodeGenContext context)
        {
            ClassRef baseClass;
            if (formals.ShortAndSimple())
                baseClass = Runtime.MethodBodyNRef(formals.min_args);
            else
                baseClass = Runtime.MethodBodyRef;

            // private class MyMethod: MethodBody? {
            ClassDef methodClass = context.CreateGlobalClass("_Internal", "Method_" + ID.ToDotNetName(method_id), baseClass);
            methodClass.AddCustomAttribute(Runtime.InteropMethodAttribute.ctor, System.Text.UnicodeEncoding.UTF8.GetBytes(method_id));

            //     internal static Calln(Class last_class, object recv, ...) { body }
            GenCallMethod(methodClass, context);

            //     internal MyMethod() {}
            CodeGenContext constructor = context.CreateConstructor(methodClass);
            constructor.ldarg(0);
            constructor.call(Runtime.MethodBodyCtor(baseClass));
            constructor.ret();
            constructor.Close();

            //     internal static MyMethod singleton;
            FieldDef singleton = CodeGenContext.AddField(methodClass, FieldAttr.PublicStatic, "myRubyMethod", methodClass);

            //     static MyMethod() {
            //         singleton = new MyMethod();
            //     }
            CodeGenContext staticConstructor = context.CreateStaticConstructor(methodClass);
            staticConstructor.newobj(constructor.Method);
            staticConstructor.stsfld(singleton);
            staticConstructor.ret();
            staticConstructor.Close();
            
            // }

            // ------------------- Return to original context -----------------------------

            return singleton;
        }
Beispiel #4
0
        private void AddInteropConstructor(CodeGenContext context)
        {
            List<Param> args = new List<Param>();
            if (formals.arity < 0)
            {
                Param param = new Param(ParamAttr.Default, "args", new PERWAPI.ZeroBasedArray(PERWAPI.PrimitiveType.Object));
                param.AddCustomAttribute(Runtime.ParamArrayAttribute.ctor, new byte[0]);
                args.Add(param);
            }
            else
                for (int i = 1; i <= this.formals.arity; i++)
                    args.Add(new Param(ParamAttr.Default, "p" + i, PrimitiveType.Object));

            // public .ctor(...) {

            try
            {
                // remove existing zero-arg constructor if it exists
                if (formals.arity == 0 && CLASS_OR_MODULE.CurrentInteropClass().GetMethodDesc(".ctor", new Type[0]) != null)
                    CLASS_OR_MODULE.CurrentInteropClass().RemoveMethod(".ctor", new Type[0]);

                CodeGenContext method = context.CreateConstructor(CLASS_OR_MODULE.CurrentInteropClass(), args.ToArray());

                method.startMethod(this.location);

                // call base class constructor

                PERWAPI.Method superClassConstructor0 = null;
                PERWAPI.Method superClassConstructor1 = null;
                PERWAPI.Class superClass = null;

                ClassSkeleton superClassSkeleton = context.currentSkeleton.FindClass(CLASS_OR_MODULE.CurrentInteropClass().SuperType.Name());
                if (superClassSkeleton != null)
                {
                    superClass = superClassSkeleton.perwapiClass;
                    superClassConstructor0 = superClass.GetMethodDesc(".ctor", new Type[0]);

                    if (superClassConstructor0 is MethodDef)
                        superClassConstructor0 = ((MethodDef)superClassConstructor0).MakeRefOf();

                    superClassConstructor1 = superClass.GetMethodDesc(".ctor", new Type[] { Runtime.ClassRef });

                    if (superClassConstructor1 is MethodDef)
                        superClassConstructor1 = ((MethodDef)superClassConstructor1).MakeRefOf();
                }

                if (superClassConstructor1 != null)
                {
                    method.ldarg(0);
                    method.ldsfld(((CLASS_OR_MODULE)this.parent_scope).singletonField);
                    method.call(superClassConstructor1);
                }
                else if (superClassConstructor0 != null)
                {
                    method.ldarg(0);
                    method.call(superClassConstructor0);
                }

                //    return Eval.CallPrivateN(recv, null, "methodId", null, ...);
                method.ldarg(0);
                method.ldnull();
                method.ldstr(method_id);
                method.ldnull();

                if (formals.arity < 0)
                {
                    method.ldarg(1);
                    method.call(Runtime.Eval.Call("Private"));
                }
                else
                {
                    for (int i = 1; i <= args.Count; i++)
                        method.ldarg(i);
                    method.call(Runtime.Eval.Call("Private", args.Count));
                }

                method.pop();

                method.ret();

                method.Close();
            }
            catch (PERWAPI.DescriptorException)
            {
            }
        }
Beispiel #5
0
        private MethodDef GenConstructor(ClassDef Class, CodeGenContext context)
        {
            // internal Eval(MyBlock block) { ... }
            CodeGenContext constructor;
            if (block_type != null)
                constructor = context.CreateConstructor(Class, new Param[] { new Param(ParamAttr.Default, "frame", block_type) });
            else
                constructor = context.CreateConstructor(Class);

            constructor.ldarg(0); // this
            constructor.call(Runtime.SystemObject.ctor);

            if (block_type != null)
            {
                for (int depth = 0; true; depth++)
                {
                    FieldInfo field = frame.current_block.GetType().GetField("locals" + depth);

                    if (field == null)
                        break;

                    Field oldField = CodeGenContext.FindField(field);

                    FieldDef newField = CodeGenContext.AddField(Class, FieldAttr.Public, field.Name, CodeGenContext.FindClass(field.FieldType));

                    frameFields.Add(newField);

                    // this.myField = frame.myField
                    constructor.ldarg(0);
                    constructor.ldarg("frame");
                    constructor.ldfld(oldField);
                    constructor.stfld(newField);
                }
            }

            constructor.ret();

            constructor.Close();

            return constructor.Method;
        }
        internal override void GenCode0(CodeGenContext context)
        {
            bool scope_created, super_created;

            CodeGenContext newContext = new CodeGenContext(context);

            // scope and super must be evaluated in surrounding scope and passed to Init method
            ISimple scope = PushScope(context, out scope_created);
            ISimple super = PushSuper(context, out super_created);

            string basename = ID.ToDotNetName(name.vid);

            // BBTAG: try and get the superclass
            
            PERWAPI.Class superClass = Runtime.ObjectRef;
            bool superClassFound = false;
            PERWAPI.Method superClassConstructor0 = null;
            PERWAPI.Method superClassConstructor1 = Runtime.Object.ctor;

            if (this is CLASS)
            {
                superClass = ((CLASS)this).GetSuperClassRef(newContext);
                if (superClass != null)
                {
                    superClassFound = true;
                    superClassConstructor0 = superClass.GetMethodDesc(".ctor", new Type[0]);

                    if (superClassConstructor0 is MethodDef)
                        superClassConstructor0 = ((MethodDef)superClassConstructor0).MakeRefOf();

                    superClassConstructor1 = superClass.GetMethodDesc(".ctor", new Type[] { Runtime.ClassRef });

                    if (superClassConstructor1 is MethodDef)
                        superClassConstructor1 = ((MethodDef)superClassConstructor1).MakeRefOf();
                }
                else
                    superClass = Runtime.ObjectRef;
            }

            //public class MyClass: Object {

            // check if this class has already been created in a referenced DLL
            // FIXME: this won't work for nested classes
            PERWAPI.Class classRef = ClassSkeleton.FindPERWAPIClass(null, name, context.peFiles);
            bool defineInteropClass = (classRef == null);
            // if it seems like an extension of a ruby builtin class, don't generate an interop class
            // FIXME: this won't work for nested classes
            if (Ruby.Runtime.BuiltinClasses.IsBuiltinClass(basename))
            {
                Compiler.InteropWarning(basename + " is a Ruby built-in class, interop class not generated");
                defineInteropClass = false;
            }

            //ClassDef interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, Runtime.ObjectRef); // BBTAG
            ClassDef interopClass = null;
            if (defineInteropClass)
            {
                if (superClass is PERWAPI.ClassDef)
                    interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, ((PERWAPI.ClassDef)superClass).MakeRefOf()); // BBTAG
                else
                    interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, superClass); // BBTAG
                interopClasses.Push(interopClass);
                classRef = interopClass;
            }
            //context.classes[classname] = interopClass;

            // BBTAG: create skeleton for this class
            
            ClassSkeleton skeleton;

            if (context.currentSkeleton.nestedClasses.ContainsKey(basename))
                skeleton = context.currentSkeleton.nestedClasses[basename];
            else
                skeleton = new ClassSkeleton(basename, classRef);
            skeleton.lexicalParent = context.currentSkeleton;
            newContext.currentSkeleton = skeleton;
            skeleton.lexicalParent.nestedClasses[basename] = skeleton;

            if (!superClassFound && this is AST.CLASS && ((AST.CLASS)this).super_class != null && defineInteropClass)
            {
                // do not add to post pass list if supertype is a built-in Ruby class
                //List<string> qualifiedSuperClass = ClassSkeleton.ConstNodeToClassList(((AST.CLASS)this).super_class);
                //if (!(qualifiedSuperClass.Count == 1 && Ruby.Runtime.BuiltinClasses.IsBuiltinClass(qualifiedSuperClass[0])))
                newContext.postPassList.Add(new ClassSkeletonPostPass(skeleton, interopClass, ((AST.CLASS)this).super_class));
            }
          
            //    public static Class classname;
            int seqNo = 0;
            internal_name = basename;
            while (FileClass().GetField(internal_name) != null)
                internal_name = basename + (seqNo++);

            // Define singleton in file class so that it gets initialized by .cctor
            singletonField = FileClass().AddField(PERWAPI.FieldAttr.PublicStatic, internal_name, Runtime.ClassRef);
            newContext.CurrentRubyClass = singletonField;

            //    public MyClass() : base(singleton) { };
            CodeGenContext class_constructor = null; 
            if (defineInteropClass)
            {
                if (interopClass.GetMethod(".ctor", new Type[0]) == null)
                {
                    CodeGenContext class_constructor0 = newContext.CreateConstructor(interopClass);
                    class_constructor0.ldarg(0);
                    if (superClassConstructor0 != null)
                        class_constructor0.call(superClassConstructor0);
                    else
                    {
                        class_constructor0.ldsfld(singletonField);
                        class_constructor0.call(superClassConstructor1);
                    }
                    class_constructor0.ret();
                    class_constructor0.Close();
                }


                //    public MyClass(Class klass) : base(klass) { };
                MethodDef ctor = interopClass.GetMethod(".ctor", new Type[] { Runtime.ClassRef });
                //CodeGenContext class_constructor;
                if (ctor == null)
                {
                    class_constructor = newContext.CreateConstructor(interopClass, new Param(ParamAttr.Default, "klass", Runtime.ClassRef));
                    class_constructor.ldarg(0);
                    class_constructor.ldarg("klass");
                    class_constructor.call(superClassConstructor1);
                    class_constructor.ret();
                    class_constructor.Close();
                }
                else
                {
                    class_constructor = new CodeGenContext();
                    class_constructor.Method = ctor;
                }
            }

            //    internal static void Init_fullname(object scope, object super, object recv, Frame caller) {
            CodeGenContext Init = newContext.CreateMethod(FileClass(), MethAttr.PublicStatic, "Init_" + internal_name, PrimitiveType.Object,
                    new Param(ParamAttr.Default, "scope", PrimitiveType.Object),
                    new Param(ParamAttr.Default, "super", PrimitiveType.Object),
                    new Param(ParamAttr.Default, "recv", PrimitiveType.Object),
                    new Param(ParamAttr.Default, "caller", Runtime.FrameRef));

            skeleton.initMethod = Init.Method;

            Init.startMethod(this.location);

            AddScopeLocals(Init);

            // singleton = scope.define_???(...)
            DefineClass(Init, singletonField);

            // recv = singleton;
            Init.ldsfld(singletonField);
            Init.starg("recv");

            // Fixme: should be conditional
            // singleton.define_allocator(allocator);
            if (defineInteropClass)
            {
                FieldDef allocator = DefineAllocator(newContext, class_constructor.Method);
                if (allocator != null)
                {
                    Init.ldsfld(singletonField);
                    Init.ldsfld(allocator);
                    Init.call(Runtime.Class.define_alloc_func);
                }
            }

            AddScopeBody(Init);

            Init.ReleaseLocal(0, true);

            Init.Close();

            if (defineInteropClass)
                interopClasses.Pop();

            // --------------------- Return to old Context ----------------------------


            context.newLine(location);
            // Init(scope, super, recv, caller);
            scope.GenSimple(context);
            super.GenSimple(context);
            context.ldarg("recv");
            context.ldloc(0);
            context.call(Init.Method);

            context.ReleaseLocal(super, super_created);
            context.ReleaseLocal(scope, scope_created);
        }
        internal override FieldDef DefineAllocator(CodeGenContext context, MethodDef ctor)
        {
            // Conservative - don't create allocator if we're not sure what the base class is
            // BBTAG: should be able to create this in all cases now, since we do a test in 
            // define_alloc_func
            //if (super_class != null)
            //    return null;

            ClassRef baseClass = Runtime.MethodBodyNRef(0);
            ClassDef allocator = context.CreateGlobalClass("_Internal", "Method_" + internal_name + "_Allocator", baseClass);
            context.currentSkeleton.allocator = allocator;

            //     internal static Call0(Class last_class, object recv, ...) { body }
            CodeGenContext Call = context.CreateMethod(allocator, MethAttr.PublicVirtual, "Call0", PrimitiveType.Object, new Param[] { 
                    new Param(ParamAttr.Default, "last_class", Runtime.ClassRef),
                    new Param(ParamAttr.Default, "recv", PrimitiveType.Object),
                    new Param(ParamAttr.Default, "caller", Runtime.FrameRef),
                    new Param(ParamAttr.Default, "block", Runtime.ProcRef)});

           
            // return new MyClass((Class)recv));
            Call.ldarg("recv");
            Call.cast(Runtime.ClassRef);
            Call.newobj(ctor);
            Call.ret();
            Call.Close();

            //     internal MyAllocator() {}
            CodeGenContext constructor = context.CreateConstructor(allocator);
            constructor.ldarg(0);
            constructor.call(Runtime.MethodBodyCtor(baseClass));
            constructor.ret();
            constructor.Close();

            //     internal static MyAllocator singleton;
            FieldDef singleton = CodeGenContext.AddField(allocator, FieldAttr.PublicStatic, "myRubyMethod", allocator);

            //     static MyAllocator() {
            //         singleton = new MyAllocator();
            //     }
            CodeGenContext staticConstructor = context.CreateStaticConstructor(allocator);
            staticConstructor.newobj(constructor.Method);
            staticConstructor.stsfld(singleton);
            staticConstructor.ret();
            staticConstructor.Close();

            return singleton;
        }