/// <summary> /// Emit a native method /// <summary> public void emitNative() { // emit an empty method this.code = null; // emit code which calls the peer PERWAPI.CILInstructions code = doEmit(); if (!emitter.stub) { if (isStatic) { string[] parTypes = new string[paramLen]; for (int i=0; i<paramLen; i++) parTypes[i] = emit.nname(method.m_vars[i].type); PERWAPI.Method peerMeth = emitter.findMethod(selfName + "Peer", name, parTypes, ret.nname()); pushArgs(code, false, paramLen); code.MethInst(PERWAPI.MethodOp.call, peerMeth); } else { string[] parTypes = new string[paramLen+1]; parTypes[0] = selfName; for (int i=0; i<paramLen; i++) parTypes[i+1] = emit.nname(method.m_vars[i].type); PERWAPI.Method peerMeth = emitter.findMethod(selfName + "Peer", name, parTypes, ret.nname()); peerMeth.AddCallConv(PERWAPI.CallConv.Instance); code.Inst(PERWAPI.Op.ldarg_0); code.FieldInst(PERWAPI.FieldOp.ldfld, emit.peerField); pushArgs(code, true, paramLen); code.MethInst(PERWAPI.MethodOp.call, peerMeth); } } code.Inst(PERWAPI.Op.ret); // emit default parameter wrappers emitWrappers(); }
/// <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); }
/// <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(); }