Exemplo n.º 1
0
        /// <summary>
        /// Emit a constructor - constructors get created as a static
        /// factory methods, so that that CallNew can just push args
        /// and invoke them
        ///
        ///   fan:
        ///     class Foo { new make(Long a) { ... } }
        ///   .net:
        ///     static Foo make(Long a) { return make_(new Foo(), a) }
        ///     static Foo make_(Foo self, Long a) { ... return self }
        ///
        /// We call the first method "make" the "factory" and the
        /// second method "make_" the "body".  CallNew opcodes are
        /// routed to the ctor factory, and CallCtor opcodes are routed
        /// to the ctor body.
        /// </summary>
        public void emitCtor()
        {
            string ctorName = this.name;

            // both factory and body are static from CLR's perspective
            this.isStatic = true;
            this.isHide   = true;

            // first emit the body with implicit self
            this.name = ctorName + "_";
            this.self = true;
            doEmit();
            PERWAPI.MethodDef make = emitter.methodDef;

            // emit body default parameter wrappers
            emitWrappers();

            // then emit the factory
            this.name = ctorName;
            this.self = false;
            this.ret  = emit.pod.typeRef(emit.type.m_self);
            this.code = null;

            PERWAPI.CILInstructions code = doEmit();
            PERWAPI.Method          ctor = emitter.findMethod(selfName, ".ctor", new string[0], "System.Void");
            code.MethInst(PERWAPI.MethodOp.newobj, ctor);
            code.Inst(PERWAPI.Op.dup);
            pushArgs(code, false, method.m_paramCount);
            code.MethInst(PERWAPI.MethodOp.call, make);
            code.Inst(PERWAPI.Op.ret);

            // emit factory default parameter wrappers
            emitWrappers();
        }
Exemplo n.º 2
0
        protected override void emitType()
        {
            PERWAPI.TypeAttr classAttr = PERWAPI.TypeAttr.Public;
            if (isAbstract)
            {
                classAttr |= PERWAPI.TypeAttr.Abstract;
            }

            emitter.emitClass(baseClassName, className, interfaces, classAttr);

            // generate private static Type $Type; set in clinit
            typeField = emitter.classDef.AddField(
                PERWAPI.FieldAttr.Public | PERWAPI.FieldAttr.Static,
                "$type", emitter.findType("Fan.Sys.Type"));

            // generate type() instance method
            PERWAPI.MethodDef m = emitter.classDef.AddMethod(
                PERWAPI.MethAttr.Public | PERWAPI.MethAttr.Virtual, PERWAPI.ImplAttr.IL,
                "typeof", emitter.findType("Fan.Sys.Type"), new PERWAPI.Param[0]);
            m.AddCallConv(PERWAPI.CallConv.Instance);
            emitter.addToMethodMap(className, "typeof", new string[0], m);

            PERWAPI.CILInstructions code = m.CreateCodeBuffer();
            code.FieldInst(PERWAPI.FieldOp.ldsfld, typeField);
            code.Inst(PERWAPI.Op.ret);

            // generate peer field if native
            if (isNative)
            {
                peerField = emitter.classDef.AddField(PERWAPI.FieldAttr.Public,
                                                      "m_peer", emitter.findType(className + "Peer"));
            }

            // Create ctor emit objects first so we can reference them

            // .ctor
            ctor = emitter.findMethod(selfName, ".ctor", new string[0], "System.Void") as PERWAPI.MethodDef;
            ctor.SetMethAttributes(
                PERWAPI.MethAttr.Public |
                PERWAPI.MethAttr.HideBySig |
                PERWAPI.MethAttr.SpecialRTSpecialName);
            ctor.AddCallConv(PERWAPI.CallConv.Instance);

            // .cctor
            cctor = emitter.findMethod(selfName, ".cctor", new string[0], "System.Void") as PERWAPI.MethodDef;
            cctor.SetMethAttributes(
                PERWAPI.MethAttr.Private |
                PERWAPI.MethAttr.Static |
                PERWAPI.MethAttr.HideBySig |
                PERWAPI.MethAttr.SpecialRTSpecialName);
        }
Exemplo n.º 3
0
        //////////////////////////////////////////////////////////////////////////
        // Overrides
        //////////////////////////////////////////////////////////////////////////

        protected override void emitInstanceInit(FMethod m)
        {
            hasInstanceInit = true;

            // make peer
            if (isNative)
            {
                throw new System.Exception("No native support for Err subclasses");
            }

            // stub ctor2
            PERWAPI.MethodDef ctor2 = emitter.findMethod(selfName, ".ctor",
                                                         new string[] { "Fan.Sys.Err/Val" }, "System.Void") as PERWAPI.MethodDef;
            ctor2.SetMethAttributes(
                PERWAPI.MethAttr.Public |
                PERWAPI.MethAttr.HideBySig |
                PERWAPI.MethAttr.SpecialRTSpecialName);
            ctor2.AddCallConv(PERWAPI.CallConv.Instance);

            // no arg constructor -> calls this(Err/Val)
            PERWAPI.CILInstructions code = ctor.CreateCodeBuffer();
            code.Inst(PERWAPI.Op.ldarg_0);
            PERWAPI.Method valctor = emitter.findMethod(className + "/Val", ".ctor", new string[0], "System.Void");
            code.MethInst(PERWAPI.MethodOp.newobj, valctor);
            code.MethInst(PERWAPI.MethodOp.call, ctor2);
            code.Inst(PERWAPI.Op.ret);

            // arg constructor with Err$Val (and init implementation)
            code = ctor2.CreateCodeBuffer();
            code.Inst(PERWAPI.Op.ldarg_0);
            code.Inst(PERWAPI.Op.ldarg_1);
            PERWAPI.Method baseCtor = emitter.findMethod(baseClassName, ".ctor",
                                                         new string[] { "Fan.Sys.Err/Val" }, "System.Void");
            baseCtor.AddCallConv(PERWAPI.CallConv.Instance); // if stub, make sure instance callconv
            code.MethInst(PERWAPI.MethodOp.call, baseCtor);
            if (m == null)
            {
                //code.maxLocals = 2;
                //code.maxStack  = 2;
                code.Inst(PERWAPI.Op.ret);
            }
            else
            {
                // e.code.maxLocals++;  // alloc room for Val extra argument
                new FCodeEmit(this, m, code).emit();
            }
        }
Exemplo n.º 4
0
        //////////////////////////////////////////////////////////////////////////
        // Param Default Wrappers
        //////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Emit wrappers.
        /// </summary>
        private void emitWrappers()
        {
            // change flags so that defaults aren't abstract
            bool oldAbstract = this.isAbstract;

            this.isAbstract = false;

            PERWAPI.MethodDef main = emitter.methodDef;

            // handle generating default param wrappers
            for (int i = 0; i < method.m_paramCount; i++)
            {
                if (method.m_vars[i].def != null)
                {
                    emitWrapper(main, i);
                }
            }
            this.paramLen = method.m_paramCount;

            this.isAbstract = oldAbstract;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Emit wrapper.
        /// </summary>
        private void emitWrapper(PERWAPI.MethodDef main, int paramLen)
        {
            // use explicit param count, and clear code
            this.paramLen = paramLen;
            this.code     = null;
            int numArgs = isStatic && !self ? paramLen : paramLen + 1;

            // TODO - this code probably isn't quite right, since it looks
            // like we generate local variables even when they might not be
            // used.  Doesn't hurt anything, but is probably more efficient
            // if we could determine that from the fcode.

            // define our locals
            int numLocals = method.m_paramCount - paramLen;

            string[] localNames = new string[numLocals];
            string[] localTypes = new string[numLocals];
            for (int i = paramLen; i < method.m_paramCount; i++)
            {
                localNames[i - paramLen] = method.m_vars[i].name;
                localTypes[i - paramLen] = emit.nname(method.m_vars[i].type);
            }

            // emit code
            PERWAPI.CILInstructions code = doEmit(localNames, localTypes);

            // push arguments passed thru
            pushArgs(code, !(isStatic && !self), paramLen);

            // emit default arguments
            FCodeEmit.Reg[] regs      = FCodeEmit.initRegs(emit.pod, isStatic, method.m_vars);
            int             maxLocals = method.maxLocals();
            int             maxStack  = 16; // TODO - add additional default expr stack height

            for (int i = paramLen; i < method.m_paramCount; i++)
            {
                FCodeEmit ce = new FCodeEmit(emit, method.m_vars[i].def, code, regs, emit.pod.typeRef(method.m_ret));
                ce.paramCount = numArgs;
                ce.vars       = method.m_vars;
                ce.isStatic   = isStatic;
// TODO - is this correct?
                ce.emit(false); // don't emit debug s cope for wrappers
                maxStack = System.Math.Max(maxStack, 2 + i + 8);
            }
            // TODO
            //code.maxLocals = maxLocals;
            //code.maxStack  = maxStack;

            // call master implementation
            if (isStatic)
            {
                code.MethInst(PERWAPI.MethodOp.call, main);
            }
            else
            {
                code.MethInst(PERWAPI.MethodOp.callvirt, main);
            }

            // return
            code.Inst(PERWAPI.Op.ret);
        }