private void debugPrintLine(MethodVisitor mw, IntegralType addType) { if (isDebug) { switch (addType) { case com.juliar.nodes.IntegralType.jdouble: mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(D)V", false); break; case com.juliar.nodes.IntegralType.jfloat: mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(F)V", false); break; case com.juliar.nodes.IntegralType.jinteger: mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V", false); break; case com.juliar.nodes.IntegralType.jlong: mw.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(L)V", false); break; default: break; } } }
public virtual void compile(CompilerContext context, MethodVisitor mv) { startCompile(context, mv); if (Branching) { compileBranch(context, mv); } else if (insn == Instructions.JR) { compileJr(context, mv); } else if (insn == Instructions.JALR) { compileJalr(context, mv); } else if (insn == Instructions.ERET) { context.compileEret(); } else if (interpretAllVfpuInstructions && insn.category().StartsWith("VFPU", StringComparison.Ordinal)) { context.visitIntepreterCall(opcode, insn); } else { insn.compile(context, Opcode); } context.endInstruction(); }
private int getBranchingOpcodeBV(CompilerContext context, MethodVisitor mv, int branchingOpcode, int notBranchingOpcode) { context.loadVcrCc(); CodeInstruction delaySlotCodeInstruction = getDelaySlotCodeInstruction(context); if (delaySlotCodeInstruction != null && delaySlotCodeInstruction.insn == insn) { // We are compiling a sequence where the delay instruction is again a BV // instruction: // bvt 0, label // bvt 1, label // bvt 2, label // bvt 3, label // nop // Handle the sequence by inserting nop's between the BV instructions: // bvt 0, label // nop // bvt 1, label // nop // bvt 2, label // nop // bvt 3, label // nop } else { compileDelaySlot(context, mv, delaySlotCodeInstruction); } return(branchingOpcode); }
private int getBranchingOpcodeBC1(CompilerContext context, MethodVisitor mv, int branchingOpcode, int notBranchingOpcode) { context.loadFcr31c(); compileDelaySlot(context, mv); return(branchingOpcode); }
public override MethodVisitor VisitMethod(AccessFlags access, string name, string descriptor , string signature, string[] exceptions) { MethodVisitor methodVisitor; if ("<clinit>".Equals(name)) { var newAccess = AccessFlags.Private + (int)AccessFlags.Static; var newName = renamedClinitMethodPrefix + numClinitMethods++; methodVisitor = base.VisitMethod(newAccess, newName, descriptor, signature, exceptions ); if (mergedClinitVisitor == null) { mergedClinitVisitor = base.VisitMethod(newAccess, name, descriptor, null, null); } mergedClinitVisitor.VisitMethodInsn(OpcodesConstants.Invokestatic, owner, newName , descriptor, false); } else { methodVisitor = base.VisitMethod(access, name, descriptor, signature, exceptions); } return(methodVisitor); }
public MethodResult ExtractPrototypeAndMethod(string code) { Guard.AgainstNullArgument("code", code); var @class = "class A { " + code + " } "; var visitor = new MethodVisitor(); var parser = new CSharpParser(); var syntaxTree = parser.Parse(@class); syntaxTree.AcceptVisitor(visitor); syntaxTree.Freeze(); var result = visitor.GetMethodDeclarations().FirstOrDefault(); // find newlines in method signature to maintain linenumbers var newLines = code.Substring(0, code.IndexOf("{", StringComparison.Ordinal) - 1) .Where(x => x.Equals('\n')) .Aggregate(string.Empty, (a, c) => a + c); // use code methodblock to maintain linenumbers var codeBlock = code.Substring(code.IndexOf("{", StringComparison.Ordinal), code.LastIndexOf("}", StringComparison.Ordinal) - code.IndexOf("{", StringComparison.Ordinal) + 1); var method = result.MethodExpression.ToString(); var blockStart = method.IndexOf("{", StringComparison.Ordinal); var blockEnd = method.LastIndexOf("}", StringComparison.Ordinal); method = method.Remove(blockStart, blockEnd - blockStart + 1); method = method.Insert(blockStart, codeBlock); return(new MethodResult { ProtoType = result.MethodPrototype.ToString().Trim() + newLines, MethodExpression = newLines + method.Trim() }); }
private void compile(CompilerContext context, MethodVisitor mv, IList <CodeInstruction> codeInstructions) { context.optimizeSequence(codeInstructions); int numberInstructionsToBeSkipped = 0; foreach (CodeInstruction codeInstruction in codeInstructions) { if (numberInstructionsToBeSkipped > 0) { if (!context.SkipDelaySlot && codeInstruction.BranchTarget) { context.compileDelaySlotAsBranchTarget(codeInstruction); } numberInstructionsToBeSkipped--; if (numberInstructionsToBeSkipped <= 0) { context.skipInstructions(0, false); } } else { codeInstruction.compile(context, mv); numberInstructionsToBeSkipped = context.NumberInstructionsToBeSkipped; } } }
public static int Calculate(CSharpSyntaxNode node) { MethodVisitor mv = new MethodVisitor(node, true); mv.Visit(node); return(mv.result); }
/// <summary> /// Constructs a new /// <see cref="MethodRemapper" /> /// . /// </summary> /// <param name="api"> /// the ASM API version supported by this remapper. Must be one of /// <see cref="Org.Objectweb.Asm.Opcodes.Asm4" /> /// , /// <see cref="Org.Objectweb.Asm.Opcodes.Asm5" /> /// or /// <see cref="Org.Objectweb.Asm.Opcodes.Asm6" /> /// . /// </param> /// <param name="methodVisitor">the method visitor this remapper must deleted to.</param> /// <param name="remapper"> /// the remapper to use to remap the types in the visited method. /// </param> protected internal MethodRemapper(VisitorAsmApiVersion api, MethodVisitor methodVisitor, Remapper remapper) : base(api, methodVisitor) { /* latest api = */ this.remapper = remapper; }
/// <summary> /// Constructs a new /// <see cref="TraceMethodVisitor" /> /// . /// </summary> /// <param name="methodVisitor"> /// the method visitor to which to delegate calls. May be /// <literal>null</literal> /// . /// </param> /// <param name="printer">the printer to convert the visited method into text.</param> public TraceMethodVisitor(MethodVisitor methodVisitor, Printer printer) : base(VisitorAsmApiVersion.Asm7, methodVisitor) { // DontCheck(MemberName): can't be renamed (for backward binary compatibility). /* latest api = */ p = printer; }
/// <summary>Makes the given visitor visit this try catch block.</summary> /// <param name="methodVisitor">a method visitor.</param> public virtual void Accept(MethodVisitor methodVisitor) { methodVisitor.VisitTryCatchBlock(start.GetLabel(), end.GetLabel(), handler == null ? null : handler.GetLabel(), type); if (visibleTypeAnnotations != null) { for (int i = 0, n = visibleTypeAnnotations.Count; i < n; ++i) { var typeAnnotation = visibleTypeAnnotations[i]; typeAnnotation.Accept(methodVisitor.VisitTryCatchAnnotation(typeAnnotation.typeRef , typeAnnotation.typePath, typeAnnotation.desc, true)); } } if (invisibleTypeAnnotations != null) { for (int i = 0, n = invisibleTypeAnnotations.Count; i < n; ++i) { var typeAnnotation = invisibleTypeAnnotations[i]; typeAnnotation.Accept(methodVisitor.VisitTryCatchAnnotation(typeAnnotation.typeRef , typeAnnotation.typePath, typeAnnotation.desc, false)); } } }
public Expression CompileMethod(MethodNode parseTree, SmalltalkClass cls, Expression self, Expression executionContext, Expression[] arguments) { if (parseTree == null) { throw new ArgumentNullException("parseTree"); } if (cls == null) { throw new ArgumentNullException("cls"); } if (self == null) { throw new ArgumentNullException("self"); } if (arguments == null) { throw new ArgumentNullException("arguments"); } RootCompilationContext context = this.GetCompilationContext(parseTree, cls, self, executionContext, arguments); MethodVisitor visitor = new MethodVisitor(context); Expression code = parseTree.Accept(visitor); return(code); }
public static int Calculate(CSharpSyntaxNode node) { entryPoint = node; MethodVisitor mv = new MethodVisitor(); mv.Visit(node); return(mv.nobcounter); }
private int getBranchingOpcodeCall0(CompilerContext context, MethodVisitor mv) { context.prepareCall(BranchingTo, Address + 8, _ra); compileDelaySlot(context, mv); context.visitCall(BranchingTo, Address + 8, _ra, isDelaySlotWritingRegister(context, _ra), false); return(Opcodes.NOP); }
/// <summary> /// Constructs a new /// <see cref="AdviceAdapter" /> /// . /// </summary> /// <param name="api"> /// the ASM API version implemented by this visitor. Must be one of /// <see cref="Opcodes.Asm4" /> /// , /// <see cref="Opcodes.Asm5" /> /// , /// <see cref="Opcodes.Asm6" /> /// or /// <see cref="Opcodes.Asm7" /> /// . /// </param> /// <param name="methodVisitor"> /// the method visitor to which this adapter delegates calls. /// </param> /// <param name="access"> /// the method's access flags (see /// <see cref="Opcodes" /> /// ). /// </param> /// <param name="name">the method's name.</param> /// <param name="descriptor"> /// the method's descriptor (see /// <see cref="Org.Objectweb.Asm.Type">Type</see> /// ). /// </param> protected internal AdviceAdapter(VisitorAsmApiVersion api, MethodVisitor methodVisitor, AccessFlags access , string name, string descriptor) : base(api, methodVisitor, access, name, descriptor) { methodAccess = access; methodDesc = descriptor; isConstructor = "<init>".Equals(name); }
private void compileJr(CompilerContext context, MethodVisitor mv) { // Retrieve the call address from the Rs register before executing // the delay slot instruction, as it might theoretically modify the // content of the Rs register. context.loadRs(); compileDelaySlot(context, mv); context.visitJump(); }
/// <summary>Makes the given visitor visit all the instructions in this list.</summary> /// <param name="methodVisitor">the method visitor that must visit the instructions.</param> public virtual void Accept(MethodVisitor methodVisitor) { var currentInsn = firstInsn; while (currentInsn != null) { currentInsn.Accept(methodVisitor); currentInsn = currentInsn.nextInsn; } }
private int getBranchingOpcodeBVL(CompilerContext context, MethodVisitor mv, int branchingOpcode, int notBranchingOpcode) { context.loadVcrCc(); CodeInstruction afterDelaySlotCodeInstruction = getAfterDelaySlotCodeInstruction(context); context.visitJump(notBranchingOpcode, afterDelaySlotCodeInstruction); compileDelaySlot(context, mv); return(Opcodes.GOTO); }
/// <summary> /// Constructs a new /// <see cref="AnalyzerAdapter" /> /// . <i>Subclasses must not use this constructor</i>. /// Instead, they must use the /// <see cref="AnalyzerAdapter(int, string, int, string, string, MethodVisitor) /// " /> /// version. /// </summary> /// <param name="owner">the owner's class name.</param> /// <param name="access"> /// the method's access flags (see /// <see cref="Opcodes" /> /// ). /// </param> /// <param name="name">the method's name.</param> /// <param name="descriptor"> /// the method's descriptor (see /// <see cref="Org.Objectweb.Asm.Type" /> /// ). /// </param> /// <param name="methodVisitor"> /// the method visitor to which this adapter delegates calls. May be /// <literal>null</literal> /// . /// </param> /// <exception cref="InvalidOperationException"> /// If a subclass calls this constructor. /// </exception> public AnalyzerAdapter(string owner, AccessFlags access, string name, string descriptor, MethodVisitor methodVisitor) : this(VisitorAsmApiVersion.Asm7, owner, access, name, descriptor, methodVisitor) { /* latest api = */ if (GetType() != typeof(AnalyzerAdapter)) { throw new InvalidOperationException(); } }
/// <summary> /// Constructs a new /// <see cref="LocalVariablesSorter" /> /// . <i>Subclasses must not use this constructor</i>. /// Instead, they must use the /// <see cref="LocalVariablesSorter(int, int, string, Org.Objectweb.Asm.MethodVisitor) /// " /> /// version. /// </summary> /// <param name="access">access flags of the adapted method.</param> /// <param name="descriptor"> /// the method's descriptor (see /// <see cref="Org.Objectweb.Asm.Type" /> /// ). /// </param> /// <param name="methodVisitor"> /// the method visitor to which this adapter delegates calls. /// </param> /// <exception cref="System.InvalidOperationException"> /// if a subclass calls this constructor. /// </exception> public LocalVariablesSorter(AccessFlags access, string descriptor, MethodVisitor methodVisitor ) : this(VisitorAsmApiVersion.Asm7, access, descriptor, methodVisitor) { /* latest api = */ if (GetType() != typeof(LocalVariablesSorter)) { throw new InvalidOperationException(); } }
private int getBranchingOpcodeBranch1(CompilerContext context, MethodVisitor mv, int branchingOpcode, int notBranchingOpcode) { // Retrieve the call address from the Rs register before executing // the delay slot instruction, as it might theoretically modify the // content of the Rs register. context.loadRs(); compileDelaySlot(context, mv); return(branchingOpcode); }
private static IList <MethodVisitorResult> ExtractMethodDeclaration(string code) { var visitor = new MethodVisitor(); var parser = new CSharpParser(); var syntaxTree = parser.Parse(code); syntaxTree.AcceptVisitor(visitor); syntaxTree.Freeze(); return(visitor.GetMethodDeclarations()); }
public override void Accept(MethodVisitor methodVisitor) { var labelsArray = new Label[labels.Count]; for (int i = 0, n = labelsArray.Length; i < n; ++i) { labelsArray[i] = labels[i].GetLabel(); } methodVisitor.VisitTableSwitchInsn(min, max, dflt.GetLabel(), labelsArray); AcceptAnnotations(methodVisitor); }
private void addConstructor(ClassVisitor cv) { MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, objectInternalName, "<init>", "()V"); mv.visitInsn(Opcodes.RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); }
/// <summary> /// Constructs a new /// <see cref="TryCatchBlockSorter" /> /// . /// </summary> /// <param name="methodVisitor"> /// the method visitor to which this visitor must delegate method calls. May /// be /// <literal>null</literal> /// . /// </param> /// <param name="access"> /// the method's access flags (see /// <see cref="Opcodes" /> /// ). This parameter also indicates if /// the method is synthetic and/or deprecated. /// </param> /// <param name="name">the method's name.</param> /// <param name="descriptor"> /// the method's descriptor (see /// <see cref="Type" /> /// ). /// </param> /// <param name="signature"> /// the method's signature. May be /// <literal>null</literal> /// if the method parameters, /// return type and exceptions do not use generic types. /// </param> /// <param name="exceptions"> /// the internal names of the method's exception classes (see /// <see cref="Type.GetInternalName()" /> /// ). May be /// <literal>null</literal> /// . /// </param> public TryCatchBlockSorter(MethodVisitor methodVisitor, AccessFlags access, string name, string descriptor, string signature, string[] exceptions) : this(VisitorAsmApiVersion.Asm7, methodVisitor, access, name, descriptor, signature, exceptions) { /* latest api = */ if (GetType() != typeof(TryCatchBlockSorter)) { throw new InvalidOperationException(); } }
private int getBranchingOpcodeBranch0(CompilerContext context, MethodVisitor mv) { compileDelaySlot(context, mv); if (BranchingTo == Address) { context.visitLogInfo(mv, string.Format("Pausing emulator - jump to self (death loop) at 0x{0:X8}", Address)); context.visitPauseEmuWithStatus(mv, Emulator.EMU_STATUS_JUMPSELF); } return(Opcodes.GOTO); }
/// <summary> /// Constructs a new /// <see cref="JSRInlinerAdapter" /> /// . <i>Subclasses must not use this constructor</i>. /// Instead, they must use the /// <see cref="JSRInlinerAdapter(int, MethodVisitor, int, string, string, string, string[]) /// " /> /// version. /// </summary> /// <param name="methodVisitor"> /// the method visitor to send the resulting inlined method code to, or /// <code> /// null</code> /// . /// </param> /// <param name="access">the method's access flags.</param> /// <param name="name">the method's name.</param> /// <param name="descriptor">the method's descriptor.</param> /// <param name="signature"> /// the method's signature. May be /// <literal>null</literal> /// . /// </param> /// <param name="exceptions"> /// the internal names of the method's exception classes. May be /// <literal>null</literal> /// . /// </param> /// <exception cref="InvalidOperationException"> /// if a subclass calls this constructor. /// </exception> public JSRInlinerAdapter(MethodVisitor methodVisitor, AccessFlags access, string name, string descriptor, string signature, string[] exceptions) : this(VisitorAsmApiVersion.Asm7, methodVisitor, access, name, descriptor, signature, exceptions) { // DontCheck(AbbreviationAsWordInName): can't be renamed (for backward binary compatibility). /* latest api = */ if (GetType() != typeof(JSRInlinerAdapter)) { throw new InvalidOperationException(); } }
public bool IsMethod(string code) { var @class = "class A { " + code + " } "; var visitor = new MethodVisitor(); var parser = new CSharpParser(); var syntaxTree = parser.Parse(@class); syntaxTree.AcceptVisitor(visitor); syntaxTree.Freeze(); return(visitor.GetMethodDeclarations().Any() && code.TrimEnd().EndsWith("}")); }
private int getBranchingOpcodeCall1L(CompilerContext context, MethodVisitor mv, int branchingOpcode, int notBranchingOpcode) { context.prepareCall(BranchingTo, Address + 8, _ra); context.loadRs(); CodeInstruction afterDelaySlotCodeInstruction = getAfterDelaySlotCodeInstruction(context); context.visitJump(notBranchingOpcode, afterDelaySlotCodeInstruction); compileDelaySlot(context, mv); context.visitCall(BranchingTo, Address + 8, _ra, isDelaySlotWritingRegister(context, _ra), true); return(Opcodes.NOP); }
/// <summary> /// Makes the given visitor visit this label and its source line numbers, if applicable. /// </summary> /// <param name="methodVisitor">a method visitor.</param> /// <param name="visitLineNumbers"> /// whether to visit of the label's source line numbers, if any. /// </param> internal void Accept(MethodVisitor methodVisitor, bool visitLineNumbers) { methodVisitor.VisitLabel(this); if (visitLineNumbers && lineNumber != 0) { methodVisitor.VisitLineNumber(lineNumber & 0xFFFF, this); if (otherLineNumbers != null) { for (var i = 1; i <= otherLineNumbers[0]; ++i) { methodVisitor.VisitLineNumber(otherLineNumbers[i], this); } } } }
override void accept(MethodVisitor visitor) { visitor.visitVarInsn(this.Opcode.Value, localVariable); }
override void accept(MethodVisitor visitor) { visitor.visitMultiANewArrayInsn(type.FullName, dimensions); }
void accept(MethodVisitor visitor) { if (defaultValue != null) { var v = visitor.visitAnnotationDefault(); defaultValue.accept(v); v.visitEnd(); } foreach (var p in parameters) { foreach (var a in p.Annotations) { a.accept(visitor.visitParameterAnnotation(p.Position, a.Type.Descriptor, a.IsRuntimeVisible)); } } foreach (var a in annotations) { a.accept(visitor.visitAnnotation(a.Type.Descriptor, a.IsRuntimeVisible)); } codeGenerator.accept(visitor); visitor.visitEnd(); codeGenerator = null; }
override void accept(MethodVisitor visitor) { visitor.visitLdcInsn(constantValue); }
override void accept(MethodVisitor visitor) { visitor.visitLabel(label); }
override void accept(MethodVisitor visitor) { visitor.visitJumpInsn(this.Opcode.Value, labelMarker.Label); }
override void accept(MethodVisitor visitor) { if (type.IsGenericParameter) { var desc = type.Descriptor; visitor.visitTypeInsn(this.Opcode.Value, desc.substring(1, desc.length() - 1)); } else { visitor.visitTypeInsn(this.Opcode.Value, type.FullName); } }
override void accept(MethodVisitor visitor) { visitor.visitIincInsn(localVariable, increment); }
override void accept(MethodVisitor visitor) { visitor.visitIntInsn(this.Opcode.Value, intOperand); }
override void accept(MethodVisitor visitor) { var t = new Label[sizeof(labels)]; for (int i = 0; i < sizeof(t); i++) { t[i] = labels[i].Label; } visitor.visitTableSwitchInsn(minimumKey, maximumKey, defaultLabel.Label, t); }
override void accept(MethodVisitor visitor) { visitor.visitInsn(this.Opcode.Value); }
override void accept(MethodVisitor visitor) { visitor.visitMethodInsn(this.Opcode.Value, method.DeclaringType.FullName, method.Name, method.Descriptor); }
void accept(MethodVisitor visitor) { if (!scopes.isEmpty()) { throw new IllegalStateException("Scope stack not empty"); } visitor.visitCode(); foreach (var e in exceptionTable) { visitor.visitTryCatchBlock(e.from, e.to, e.target, (e.type == null) ? null : e.type.FullName); } foreach (var i in instructions) { i.accept(visitor); } foreach (var l in locals) { visitor.visitLocalVariable(l.Name, l.Type.Descriptor, l.Type.Signature, l.beginLabel, l.endLabel, l.index); } foreach (var l in lineNumbers) { visitor.visitLineNumber(l.line, l.label); } visitor.visitMaxs(0, 0); }
override void accept(MethodVisitor visitor) { var t = new Label[sizeof(labels)]; for (int i = 0; i < sizeof(t); i++) { t[i] = labels[i].Label; } visitor.visitLookupSwitchInsn(defaultLabel.Label, keys, t); }