private static void loadVar(CILInstructions code, int stackType, int index, int paramCount) { if (index < paramCount) { switch (index) { case 0: code.Inst(Op.ldarg_0); break; case 1: code.Inst(Op.ldarg_1); break; case 2: code.Inst(Op.ldarg_2); break; case 3: code.Inst(Op.ldarg_3); break; default: code.IntInst(IntOp.ldarg, index); break; } } else { index -= paramCount; switch (index) { case 0: code.Inst(Op.ldloc_0); break; case 1: code.Inst(Op.ldloc_1); break; case 2: code.Inst(Op.ldloc_2); break; case 3: code.Inst(Op.ldloc_3); break; default: code.IntInst(IntOp.ldloc, index); break; } } }
internal TryBlock MakeTryBlock(List <CILLabel> labels) { Contract.Requires(labels != null); Contract.Ensures(Contract.Result <TryBlock>() != null); TryBlock tBlock = new TryBlock(CILInstructions.GetLabel(labels, tryOffset), CILInstructions.GetLabel(labels, tryOffset + tryLength)); CILLabel hStart = CILInstructions.GetLabel(labels, handlerOffset); CILLabel hEnd = CILInstructions.GetLabel(labels, handlerOffset + handlerLength); HandlerBlock handler = null; switch (clauseType) { case (EHClauseType.Exception): handler = new Catch((Class)classToken, hStart, hEnd); break; case (EHClauseType.Filter): handler = new Filter(CILInstructions.GetLabel(labels, filterOffset), hStart, hEnd); break; case (EHClauseType.Finally): handler = new Finally(hStart, hEnd); break; case (EHClauseType.Fault): handler = new Fault(hStart, hEnd); break; } tBlock.AddHandler(handler); return(tBlock); }
internal uint AddCode(CILInstructions byteCode) { byteCodes.Add(byteCode); uint offset = codeSize; codeSize += byteCode.GetCodeSize(); return(offset); }
public FCodeEmit(FTypeEmit parent, FBuf fcode, CILInstructions code, Reg[] regs, FTypeRef ret) { this.pod = parent.pod; this.emitter = parent.emitter; this.parent = parent; this.buf = fcode.m_buf; this.len = fcode.m_len; this.code = code; this.podClass = FanUtil.toDotnetTypeName(pod.m_podName, "$Pod", false); this.jumps = new Jumps(code); this.regs = regs; this.ret = ret; }
////////////////////////////////////////////////////////////////////////// // Constructor ////////////////////////////////////////////////////////////////////////// public FCodeEmit(FTypeEmit parent, FMethod fmethod, CILInstructions code) : this(parent, fmethod.m_code, code, initRegs(parent.pod, fmethod.isStatic(), fmethod.m_vars), parent.pod.typeRef(fmethod.m_ret)) { this.fmethod = fmethod; this.vars = fmethod.m_vars; this.isStatic = (fmethod.m_flags & FConst.Static) != 0; this.paramCount = fmethod.m_paramCount; if (!isStatic) { paramCount++; } }
public void ReadPDB() { PDBReader reader = new PDBReader(this.fileName); foreach (ClassDef cDef in GetClasses()) { foreach (MethodDef mDef in cDef.GetMethods()) { CILInstructions buffer = mDef.GetCodeBuffer(); PDBMethod meth = reader.GetMethod((int)mDef.Token()); if (meth == null) { continue; // no symbols for this method } PDBSequencePoint[] spList = meth.SequencePoints; MergeBuffer mergeBuffer = new MergeBuffer(buffer.GetInstructions()); PDBScope outer = meth.Scope; Scope root = ReadPDBScope(outer, mergeBuffer, null, mDef); buffer.currentScope = root; bool hasRootScope = mergeBuffer.hasRootScope(); if (!hasRootScope) { mergeBuffer.Add(new OpenScope(root), (uint)0); } foreach (PDBSequencePoint sp in spList) { PDBDocument doc = sp.Document; mergeBuffer.Add( new Line((uint)sp.Line, (uint)sp.Column, (uint)sp.EndLine, (uint)sp.EndColumn, SourceFile.GetSourceFile(doc.URL, doc.Language, doc.LanguageVendor, doc.DocumentType)), (uint)sp.Offset); } if (!hasRootScope) { mergeBuffer.Add(new CloseScope(root), (uint)outer.EndOffset); } buffer.SetInstructions(mergeBuffer.Instructions); } } }
internal void TraverseCode(MetaDataOut md) { try { if (code != null) { if (code.IsEmpty()) { code = null; } else { code.BuildTables(md); } } } catch (InstructionException ex) { throw new Exception(ex.AddMethodName(name)); } }
/// <summary> /// Load variable onto stack using Java type and java index (which might /// not map to Fantom index. Return next available java index /// </summary> internal static void loadVar(CILInstructions code, int stackType, int index) { loadVar(code, stackType, index, Int32.MaxValue); }
public Jumps(CILInstructions code) { this.code = code; }
internal static void ILSpyMtNoRt(ClassDef classDef, MethodDef methodDef, Method startLogMethod, Method endLogMethod) { string classNameString = MethodLoggerUtil.GetQualifiedClassName(classDef); string methodNameString = methodDef.Name(); string paramsString = MethodLoggerUtil.GetParamsAsString(methodDef.GetParams()); List <Local> CLRLocals = new List <Local>(); CLRLocals.Clear(); Param[] parms = methodDef.GetParams(); if (methodDef.GetMaxStack() < 3) { methodDef.SetMaxStack(3); } string strGuid = Guid.NewGuid().ToString(); CILInstructions instructions = methodDef.GetCodeBuffer(); if (instructions == null) { return; } instructions.StartInsert(); instructions.Inst(Op.nop); instructions.StartBlock(); // Try #1 instructions.StartBlock(); // Try #2 instructions.Inst(Op.nop); instructions.ldstr(strGuid); instructions.ldstr(classNameString); instructions.ldstr(methodNameString); instructions.ldstr(paramsString); instructions.MethInst(MethodOp.call, startLogMethod); instructions.EndInsert(); while (instructions.GetNextInstruction().GetPos() < instructions.NumInstructions() - 2) { ; } instructions.StartInsert(); instructions.Inst(Op.nop); CILLabel cel0 = instructions.NewLabel(); CILLabel cel9 = instructions.NewLabel(); instructions.Branch(BranchOp.leave_s, cel9); TryBlock tBlk2 = instructions.EndTryBlock(); // #2 instructions.StartBlock(); int istloc = 0; if (methodDef.GetLocals() != null) { istloc = methodDef.GetLocals().Length; } instructions.IntInst(IntOp.stloc_s, istloc); instructions.Inst(Op.nop); //instructions.Inst(Op.rethrow); CILLabel cel = instructions.NewLabel(); instructions.CodeLabel(cel0); instructions.OpenScope(); //start---add exceptiong to stocks variables Local loc = new Local("SpyMtNoRt", Runtime.SystemExceptionRef); if (methodDef.GetLocals() != null) { foreach (Local lab in methodDef.GetLocals()) { CLRLocals.Add(lab); } } CLRLocals.Add(loc); methodDef.AddLocals(CLRLocals.ToArray(), false); foreach (Local la in methodDef.GetLocals()) { instructions.BindLocal(la); } //start---add exceptiong to stocks variables instructions.CloseScope(); instructions.IntInst(IntOp.ldloc_s, istloc); Method LogException = null; MethodLoggerUtil.GetMethodsFromClass("LogException", out LogException); instructions.MethInst(MethodOp.call, LogException); instructions.Inst(Op.nop); instructions.Inst(Op.nop); instructions.Branch(BranchOp.leave_s, cel9); instructions.EndCatchBlock(Runtime.SystemExceptionRef, tBlk2); instructions.CodeLabel(cel9); instructions.Branch(BranchOp.leave_s, cel); TryBlock tBlk1 = instructions.EndTryBlock(); // #1 instructions.StartBlock(); // Finally instructions.Inst(Op.nop); instructions.ldstr(strGuid); instructions.ldstr(classNameString); instructions.ldstr(methodNameString); instructions.ldstr(paramsString); instructions.MethInst(MethodOp.call, endLogMethod); instructions.Inst(Op.nop); instructions.Inst(Op.nop); instructions.Inst(Op.endfinally); instructions.EndFinallyBlock(tBlk1); instructions.CodeLabel(cel); instructions.EndInsert(); }
/// <summary> /// Create a code buffer for this method to add the IL instructions to /// </summary> /// <returns>a buffer for this method's IL instructions</returns> public CILInstructions CreateCodeBuffer() { code = new CILInstructions(this); return(code); }