List of usage examples for org.objectweb.asm Opcodes IF_ICMPGT
int IF_ICMPGT
To view the source code for org.objectweb.asm Opcodes IF_ICMPGT.
Click Source Link
From source file:bytecode.InstructionExporter.java
License:Apache License
/** * Output conditional branch instructions, choosing between integer and * reference comparisons, and also abbreviating comparisons with 0 or * <code>null</code>./*from w ww . j a v a2s. com*/ * * @param instruction Comparison instruction. * @return . <code>null</code> */ @Override public Void visit(Condition instruction) { Label label = BlockExporter.getLabel(instruction.getDestination()); // Produce integer instructions if (instruction.getOperandA().getType().isIntBased()) { // Comparisons with zero if (instruction.getOperandB().equals(new Constant(new Integer(0)))) { switch (instruction.getOperator()) { case EQ: mv.visitJumpInsn(Opcodes.IFEQ, label); break; case NE: mv.visitJumpInsn(Opcodes.IFNE, label); break; case LT: mv.visitJumpInsn(Opcodes.IFLT, label); break; case GE: mv.visitJumpInsn(Opcodes.IFGE, label); break; case GT: mv.visitJumpInsn(Opcodes.IFGT, label); break; case LE: mv.visitJumpInsn(Opcodes.IFLE, label); break; } // Direct comparisons } else { switch (instruction.getOperator()) { case EQ: mv.visitJumpInsn(Opcodes.IF_ICMPEQ, label); break; case NE: mv.visitJumpInsn(Opcodes.IF_ICMPNE, label); break; case LT: mv.visitJumpInsn(Opcodes.IF_ICMPLT, label); break; case GE: mv.visitJumpInsn(Opcodes.IF_ICMPGE, label); break; case GT: mv.visitJumpInsn(Opcodes.IF_ICMPGT, label); break; case LE: mv.visitJumpInsn(Opcodes.IF_ICMPLE, label); break; } } // Produce reference instructions } else if (instruction.getOperandA().getType().getSort() == Type.Sort.REF) { // Comparisons with null if (instruction.getOperandB().equals(new Constant(null))) { switch (instruction.getOperator()) { case EQ: mv.visitJumpInsn(Opcodes.IFNULL, label); break; case NE: mv.visitJumpInsn(Opcodes.IFNONNULL, label); break; } // Direct comparisons } else { switch (instruction.getOperator()) { case EQ: mv.visitJumpInsn(Opcodes.IF_ACMPEQ, label); break; case NE: mv.visitJumpInsn(Opcodes.IF_ACMPNE, label); break; } } // Flag up others. } else { throw new RuntimeException("Attempt to export condition of unknown type."); } return null; }
From source file:bytecode.MethodImporter.java
License:Apache License
/** * Imports branch instructions, both conditional and unconditional. * * @param opcode Opcode./*from ww w . j a v a 2 s. c o m*/ * @param label Destination of branch. */ @Override public void visitJumpInsn(final int opcode, final Label label) { BasicBlock section = getBasicBlock(label); switch (opcode) { // Unconditional Branch case Opcodes.GOTO: finishBlock(section); setCurrent(null); break; // Zero Test case Opcodes.IFEQ: createZeroCondition(Condition.Operator.EQ, section); break; case Opcodes.IFNE: createZeroCondition(Condition.Operator.NE, section); break; case Opcodes.IFLT: createZeroCondition(Condition.Operator.LT, section); break; case Opcodes.IFGE: createZeroCondition(Condition.Operator.GE, section); break; case Opcodes.IFGT: createZeroCondition(Condition.Operator.GT, section); break; case Opcodes.IFLE: createZeroCondition(Condition.Operator.LE, section); break; // Integer Comparison case Opcodes.IF_ICMPEQ: createCondition(Condition.Operator.EQ, section); break; case Opcodes.IF_ICMPNE: createCondition(Condition.Operator.NE, section); break; case Opcodes.IF_ICMPLT: createCondition(Condition.Operator.LT, section); break; case Opcodes.IF_ICMPGE: createCondition(Condition.Operator.GE, section); break; case Opcodes.IF_ICMPGT: createCondition(Condition.Operator.GT, section); break; case Opcodes.IF_ICMPLE: createCondition(Condition.Operator.LE, section); break; // Reference Comparisons case Opcodes.IF_ACMPEQ: createCondition(Condition.Operator.EQ, section); break; case Opcodes.IF_ACMPNE: createCondition(Condition.Operator.NE, section); break; case Opcodes.IFNULL: createNullCondition(Condition.Operator.EQ, section); break; case Opcodes.IFNONNULL: createNullCondition(Condition.Operator.NE, section); break; // TODO: JSR (paired with RET) case Opcodes.JSR: System.err.println("visitJumpInsn: JSR"); } }
From source file:com.codename1.tools.translator.BytecodeMethod.java
License:Open Source License
boolean optimize() { int instructionCount = instructions.size(); // optimize away a method that only contains the void return instruction e.g. blank constructors etc. if (instructionCount < 6) { int realCount = instructionCount; Instruction actual = null;/*from www. j a v a2 s.c o m*/ for (int iter = 0; iter < instructionCount; iter++) { Instruction current = instructions.get(iter); if (current instanceof LabelInstruction) { realCount--; continue; } if (current instanceof LineNumber) { realCount--; continue; } actual = current; } if (realCount == 1 && actual != null && actual.getOpcode() == Opcodes.RETURN) { return false; } } boolean astoreCalls = false; boolean hasInstructions = false; boolean hasTryCatch = false; for (int iter = 0; iter < instructionCount - 1; iter++) { Instruction current = instructions.get(iter); if (current instanceof TryCatch) { hasTryCatch = true; } current.setMethod(this); if (current.isOptimized()) { continue; } int currentOpcode = current.getOpcode(); switch (currentOpcode) { case Opcodes.CHECKCAST: { // Remove the check cast for now as it gets in the way of other optimizations instructions.remove(iter); iter--; instructionCount--; break; } } } for (int iter = 0; iter < instructionCount - 1; iter++) { Instruction current = instructions.get(iter); if (current.isOptimized()) { // This instruction has already been optimized // we should skip it and proceed to the next one continue; } Instruction next = instructions.get(iter + 1); int currentOpcode = current.getOpcode(); int nextOpcode = next.getOpcode(); if (ArithmeticExpression.isArithmeticOp(current)) { int addedIndex = ArithmeticExpression.tryReduce(instructions, iter); if (addedIndex >= 0) { iter = addedIndex; instructionCount = instructions.size(); continue; } } if (current instanceof Field) { int newIter = Field.tryReduce(instructions, iter); if (newIter >= 0) { iter = newIter; instructionCount = instructions.size(); continue; } } switch (currentOpcode) { case Opcodes.ARRAYLENGTH: { if (!dependentClasses.contains("java_lang_NullPointerException")) { dependentClasses.add("java_lang_NullPointerException"); } int newIter = ArrayLengthExpression.tryReduce(instructions, iter); if (newIter >= 0) { instructionCount = instructions.size(); iter = newIter; continue; } break; } case Opcodes.DUP: { int newIter = DupExpression.tryReduce(instructions, iter); if (newIter >= 0) { iter = newIter; instructionCount = instructions.size(); continue; } break; } case Opcodes.POP: { if (iter > 0) { Instruction prev = instructions.get(iter - 1); if (prev instanceof CustomInvoke) { CustomInvoke inv = (CustomInvoke) prev; if (inv.methodHasReturnValue()) { inv.setNoReturn(true); instructions.remove(iter); iter--; instructionCount--; continue; } } } break; } case Opcodes.ASTORE: case Opcodes.ISTORE: case Opcodes.DSTORE: case Opcodes.LSTORE: case Opcodes.FSTORE: { if (iter > 0 && current instanceof VarOp) { VarOp currentVarOp = (VarOp) current; Instruction prev = instructions.get(iter - 1); if (prev instanceof AssignableExpression) { AssignableExpression expr = (AssignableExpression) prev; StringBuilder sb = new StringBuilder(); if (currentVarOp.assignFrom(expr, sb)) { instructions.remove(iter - 1); instructions.remove(iter - 1); instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses)); iter = iter - 1; instructionCount = instructions.size(); continue; } } else if (prev instanceof CustomInvoke) { CustomInvoke inv = (CustomInvoke) prev; StringBuilder sb = new StringBuilder(); if (currentVarOp.assignFrom(inv, sb)) { instructions.remove(iter - 1); instructions.remove(iter - 1); instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses)); iter = iter - 1; instructionCount = instructions.size(); continue; } } } break; } case Opcodes.IRETURN: case Opcodes.FRETURN: case Opcodes.ARETURN: case Opcodes.LRETURN: case Opcodes.DRETURN: { if (iter > 0 && current instanceof BasicInstruction) { Instruction prev = instructions.get(iter - 1); if (prev instanceof AssignableExpression) { AssignableExpression expr = (AssignableExpression) prev; StringBuilder sb = new StringBuilder(); if (expr.assignTo(null, sb)) { instructions.remove(iter - 1); instructions.remove(iter - 1); String exprString = sb.toString().trim(); String retVal = exprString; sb.setLength(0); if (!prev.isConstant()) { sb.append("\n{\n "); switch (currentOpcode) { case Opcodes.IRETURN: sb.append("JAVA_INT"); break; case Opcodes.FRETURN: sb.append("JAVA_FLOAT"); break; case Opcodes.ARETURN: sb.append("JAVA_OBJECT"); break; case Opcodes.LRETURN: sb.append("JAVA_LONG"); break; case Opcodes.DRETURN: sb.append("JAVA_DOUBLE"); break; } sb.append(" ___returnValue=").append(exprString).append(";\n"); retVal = "___returnValue"; } if (synchronizedMethod) { if (staticMethod) { sb.append(" monitorExit(threadStateData, (JAVA_OBJECT)&class__"); sb.append(getClsName()); sb.append(");\n"); } else { sb.append(" monitorExit(threadStateData, __cn1ThisObject);\n"); } } if (hasTryCatch) { sb.append( " releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ") .append(retVal).append(";\n"); } else { sb.append(" releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ") .append(retVal).append(";\n"); } if (!prev.isConstant()) { sb.append("}\n"); } instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses)); iter--; instructionCount = instructions.size(); continue; } } else if (prev instanceof CustomInvoke) { CustomInvoke expr = (CustomInvoke) prev; String returnType = expr.getReturnValue(); if (returnType != null && !"JAVA_OBJECT".equals(returnType)) { // We can't safely return a JAVA_OBJECT directly because it needs to be added // to the stack for the GC StringBuilder sb = new StringBuilder(); if (expr.appendExpression(sb)) { instructions.remove(iter - 1); instructions.remove(iter - 1); String exprString = sb.toString().trim(); String retVal = exprString; sb.setLength(0); if (!expr.isConstant()) { sb.append("\n{\n "); switch (currentOpcode) { case Opcodes.IRETURN: sb.append("JAVA_INT"); break; case Opcodes.FRETURN: sb.append("JAVA_FLOAT"); break; case Opcodes.ARETURN: sb.append("JAVA_OBJECT"); break; case Opcodes.LRETURN: sb.append("JAVA_LONG"); break; case Opcodes.DRETURN: sb.append("JAVA_DOUBLE"); break; } sb.append(" ___returnValue=").append(exprString).append(";\n"); retVal = "___returnValue"; } if (synchronizedMethod) { if (staticMethod) { sb.append(" monitorExit(threadStateData, (JAVA_OBJECT)&class__"); sb.append(getClsName()); sb.append(");\n"); } else { sb.append(" monitorExit(threadStateData, __cn1ThisObject);\n"); } } if (hasTryCatch) { sb.append( " releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ") .append(retVal).append(";\n"); } else { sb.append( " releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ") .append(retVal).append(";\n"); } if (!expr.isConstant()) { sb.append("}\n"); } instructions.add(iter - 1, new CustomIntruction(sb.toString(), sb.toString(), dependentClasses)); iter--; instructionCount = instructions.size(); continue; } } } } break; } case Opcodes.BASTORE: case Opcodes.SASTORE: case Opcodes.CASTORE: case Opcodes.AASTORE: case Opcodes.IASTORE: case Opcodes.DASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: { if (iter > 2 && current instanceof BasicInstruction) { StringBuilder devNull = new StringBuilder(); String arrayLiteral = null; String indexLiteral = null; String valueLiteral = null; Instruction prev3 = instructions.get(iter - 3); if (prev3 instanceof AssignableExpression) { if (((AssignableExpression) prev3).assignTo(null, devNull)) { arrayLiteral = devNull.toString().trim(); } } devNull.setLength(0); Instruction prev2 = instructions.get(iter - 2); if (prev2 instanceof AssignableExpression) { if (((AssignableExpression) prev2).assignTo(null, devNull)) { indexLiteral = devNull.toString().trim(); } } devNull.setLength(0); Instruction prev1 = instructions.get(iter - 1); if (prev1 instanceof AssignableExpression) { if (((AssignableExpression) prev1).assignTo(null, devNull)) { valueLiteral = devNull.toString().trim(); } } else if (prev1 instanceof CustomInvoke) { devNull.setLength(0); if (((CustomInvoke) prev1).appendExpression(devNull)) { valueLiteral = devNull.toString().trim(); } } if (arrayLiteral != null && indexLiteral != null && valueLiteral != null) { String elementType = null; switch (current.getOpcode()) { case Opcodes.AASTORE: elementType = "OBJECT"; break; case Opcodes.IASTORE: elementType = "INT"; break; case Opcodes.DASTORE: elementType = "DOUBLE"; break; case Opcodes.LASTORE: elementType = "LONG"; break; case Opcodes.FASTORE: elementType = "FLOAT"; break; case Opcodes.CASTORE: elementType = "CHAR"; break; case Opcodes.BASTORE: elementType = "BYTE"; break; case Opcodes.SASTORE: elementType = "SHORT"; break; } if (elementType == null) { break; } instructions.remove(iter - 3); instructions.remove(iter - 3); instructions.remove(iter - 3); instructions.remove(iter - 3); String code = " CN1_SET_ARRAY_ELEMENT_" + elementType + "(" + arrayLiteral + ", " + indexLiteral + ", " + valueLiteral + ");\n"; instructions.add(iter - 3, new CustomIntruction(code, code, dependentClasses)); iter = iter - 3; instructionCount = instructions.size(); continue; } } break; } case Opcodes.FALOAD: case Opcodes.BALOAD: case Opcodes.IALOAD: case Opcodes.LALOAD: case Opcodes.DALOAD: case Opcodes.AALOAD: case Opcodes.SALOAD: case Opcodes.CALOAD: { int newIter = ArrayLoadExpression.tryReduce(instructions, iter); if (newIter >= 0) { iter = newIter; instructionCount = instructions.size(); continue; } break; } /* Try to optimize if statements that just use constants and local variables so that they don't need the intermediate push and pop from the stack. */ case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: case Opcodes.IF_ICMPLE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPGE: { if (iter > 1) { Instruction leftArg = instructions.get(iter - 2); Instruction rightArg = instructions.get(iter - 1); String leftLiteral = null; String rightLiteral = null; if (leftArg instanceof AssignableExpression) { StringBuilder sb = new StringBuilder(); if (((AssignableExpression) leftArg).assignTo(null, sb)) { leftLiteral = sb.toString().trim(); } } else if (leftArg instanceof CustomInvoke) { CustomInvoke inv = (CustomInvoke) leftArg; StringBuilder sb = new StringBuilder(); if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) { leftLiteral = sb.toString().trim(); } } if (rightArg instanceof AssignableExpression) { StringBuilder sb = new StringBuilder(); if (((AssignableExpression) rightArg).assignTo(null, sb)) { rightLiteral = sb.toString().trim(); } } else if (rightArg instanceof CustomInvoke) { CustomInvoke inv = (CustomInvoke) rightArg; StringBuilder sb = new StringBuilder(); if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) { rightLiteral = sb.toString().trim(); } } if (rightLiteral != null && leftLiteral != null) { Jump jmp = (Jump) current; instructions.remove(iter - 2); instructions.remove(iter - 2); instructions.remove(iter - 2); //instructions.remove(iter-2); iter -= 2; //instructionCount -= 2; StringBuilder sb = new StringBuilder(); String operator = null; String opName = null; switch (currentOpcode) { case Opcodes.IF_ICMPLE: operator = "<="; opName = "IF_ICMPLE"; break; case Opcodes.IF_ICMPLT: operator = "<"; opName = "IF_IMPLT"; break; case Opcodes.IF_ICMPNE: operator = "!="; opName = "IF_ICMPNE"; break; case Opcodes.IF_ICMPGT: operator = ">"; opName = "IF_ICMPGT"; break; case Opcodes.IF_ICMPGE: operator = ">="; opName = "IF_ICMPGE"; break; case Opcodes.IF_ICMPEQ: operator = "=="; opName = "IF_ICMPEQ"; break; case Opcodes.IF_ACMPEQ: operator = "=="; opName = "IF_ACMPEQ"; break; case Opcodes.IF_ACMPNE: operator = "!="; opName = "IF_ACMPNE"; break; default: throw new RuntimeException( "Invalid operator during optimization of integer comparison"); } sb.append("if (").append(leftLiteral).append(operator).append(rightLiteral).append(") /* ") .append(opName).append(" CustomJump */ "); CustomJump newJump = CustomJump.create(jmp, sb.toString()); //jmp.setCustomCompareCode(sb.toString()); newJump.setOptimized(true); instructions.add(iter, newJump); instructionCount = instructions.size(); } } break; } case Opcodes.IFNONNULL: case Opcodes.IFNULL: case Opcodes.IFLE: case Opcodes.IFLT: case Opcodes.IFNE: case Opcodes.IFGT: case Opcodes.IFEQ: case Opcodes.IFGE: { String rightArg = "0"; if (currentOpcode == Opcodes.IFNONNULL || currentOpcode == Opcodes.IFNULL) { rightArg = "JAVA_NULL"; } if (iter > 0) { Instruction leftArg = instructions.get(iter - 1); String leftLiteral = null; if (leftArg instanceof AssignableExpression) { StringBuilder sb = new StringBuilder(); if (((AssignableExpression) leftArg).assignTo(null, sb)) { leftLiteral = sb.toString().trim(); } } else if (leftArg instanceof CustomInvoke) { CustomInvoke inv = (CustomInvoke) leftArg; StringBuilder sb = new StringBuilder(); if (inv.appendExpression(sb)) { leftLiteral = sb.toString().trim(); } } if (leftLiteral != null) { Jump jmp = (Jump) current; instructions.remove(iter - 1); instructions.remove(iter - 1); //instructions.remove(iter-2); iter -= 1; //instructionCount -= 2; StringBuilder sb = new StringBuilder(); String operator = null; String opName = null; switch (currentOpcode) { case Opcodes.IFLE: operator = "<="; opName = "IFLE"; break; case Opcodes.IFLT: operator = "<"; opName = "IFLT"; break; case Opcodes.IFNE: operator = "!="; opName = "IFNE"; break; case Opcodes.IFGT: operator = ">"; opName = "IFGT"; break; case Opcodes.IFGE: operator = ">="; opName = "IFGE"; break; case Opcodes.IFEQ: operator = "=="; opName = "IFEQ"; break; case Opcodes.IFNULL: operator = "=="; opName = "IFNULL"; break; case Opcodes.IFNONNULL: operator = "!="; opName = "IFNONNULL"; break; default: throw new RuntimeException( "Invalid operator during optimization of integer comparison"); } sb.append("if (").append(leftLiteral).append(operator).append(rightArg).append(") /* ") .append(opName).append(" CustomJump */ "); CustomJump newJump = CustomJump.create(jmp, sb.toString()); //jmp.setCustomCompareCode(sb.toString()); newJump.setOptimized(true); instructions.add(iter, newJump); instructionCount = instructions.size(); } } break; } case Opcodes.INVOKEVIRTUAL: case Opcodes.INVOKESTATIC: case Opcodes.INVOKESPECIAL: case Opcodes.INVOKEINTERFACE: { if (current instanceof Invoke) { Invoke inv = (Invoke) current; List<ByteCodeMethodArg> invocationArgs = inv.getArgs(); int numArgs = invocationArgs.size(); //if (current.getOpcode() != Opcodes.INVOKESTATIC) { // numArgs++; //} if (iter >= numArgs) { String[] argLiterals = new String[numArgs]; StringBuilder devNull = new StringBuilder(); for (int i = 0; i < numArgs; i++) { devNull.setLength(0); Instruction instr = instructions.get(iter - numArgs + i); if (instr instanceof AssignableExpression && ((AssignableExpression) instr).assignTo(null, devNull)) { argLiterals[i] = devNull.toString().trim(); } else if (instr instanceof CustomInvoke) { CustomInvoke cinv = (CustomInvoke) instr; devNull.setLength(0); if (!"JAVA_OBJECT".equals(cinv.getReturnValue()) && cinv.appendExpression(devNull)) { // We can't add invocations that return objects directly // because they need to be added to the stack for GC argLiterals[i] = devNull.toString().trim(); } } else if (instr instanceof ArithmeticExpression) { argLiterals[i] = ((ArithmeticExpression) instr).getExpressionAsString().trim(); } else if (instr instanceof VarOp) { VarOp var = (VarOp) instr; switch (instr.getOpcode()) { case Opcodes.ALOAD: { if (!isStatic() && var.getIndex() == 0) { argLiterals[i] = "__cn1ThisObject"; } else { argLiterals[i] = "locals[" + var.getIndex() + "].data.o"; } break; } case Opcodes.ILOAD: { argLiterals[i] = "ilocals_" + var.getIndex() + "_"; break; } case Opcodes.ACONST_NULL: { argLiterals[i] = "JAVA_NULL"; break; } case Opcodes.DLOAD: { argLiterals[i] = "dlocals_" + var.getIndex() + "_"; break; } case Opcodes.FLOAD: { argLiterals[i] = "flocals_" + var.getIndex() + "_"; break; } case Opcodes.LLOAD: { argLiterals[i] = "llocals_" + var.getIndex() + "_"; break; } case Opcodes.ICONST_0: { argLiterals[i] = "0"; break; } case Opcodes.ICONST_1: { argLiterals[i] = "1"; break; } case Opcodes.ICONST_2: { argLiterals[i] = "2"; break; } case Opcodes.ICONST_3: { argLiterals[i] = "3"; break; } case Opcodes.ICONST_4: { argLiterals[i] = "4"; break; } case Opcodes.ICONST_5: { argLiterals[i] = "5"; break; } case Opcodes.ICONST_M1: { argLiterals[i] = "-1"; break; } case Opcodes.LCONST_0: { argLiterals[i] = "(JAVA_LONG)0"; break; } case Opcodes.LCONST_1: { argLiterals[i] = "(JAVA_LONG)1"; break; } case Opcodes.BIPUSH: case Opcodes.SIPUSH: { argLiterals[i] = String.valueOf(var.getIndex()); break; } } } else { switch (instr.getOpcode()) { case Opcodes.ACONST_NULL: { argLiterals[i] = "JAVA_NULL"; break; } case Opcodes.ICONST_0: { argLiterals[i] = "0"; break; } case Opcodes.ICONST_1: { argLiterals[i] = "1"; break; } case Opcodes.ICONST_2: { argLiterals[i] = "2"; break; } case Opcodes.ICONST_3: { argLiterals[i] = "3"; break; } case Opcodes.ICONST_4: { argLiterals[i] = "4"; break; } case Opcodes.ICONST_5: { argLiterals[i] = "5"; break; } case Opcodes.ICONST_M1: { argLiterals[i] = "-1"; break; } case Opcodes.LCONST_0: { argLiterals[i] = "(JAVA_LONG)0"; break; } case Opcodes.LCONST_1: { argLiterals[i] = "(JAVA_LONG)1"; break; } case Opcodes.BIPUSH: { if (instr instanceof BasicInstruction) { argLiterals[i] = String.valueOf(((BasicInstruction) instr).getValue()); } break; } case Opcodes.LDC: { if (instr instanceof Ldc) { Ldc ldc = (Ldc) instr; argLiterals[i] = ldc.getValueAsString(); } break; } } } } // Check to make sure that we have all the args as literals. boolean missingLiteral = false; for (String lit : argLiterals) { if (lit == null) { missingLiteral = true; break; } } // We have all of the arguments as literals. Let's // add them to our invoke instruction. if (!missingLiteral) { CustomInvoke newInvoke = CustomInvoke.create(inv); instructions.remove(iter); instructions.add(iter, newInvoke); int newIter = iter; for (int i = 0; i < numArgs; i++) { instructions.remove(iter - numArgs); newIter--; newInvoke.setLiteralArg(i, argLiterals[i]); } if (inv.getOpcode() != Opcodes.INVOKESTATIC) { Instruction ldTarget = instructions.get(iter - numArgs - 1); if (ldTarget instanceof AssignableExpression) { StringBuilder targetExprStr = new StringBuilder(); if (((AssignableExpression) ldTarget).assignTo(null, targetExprStr)) { newInvoke.setTargetObjectLiteral(targetExprStr.toString().trim()); instructions.remove(iter - numArgs - 1); newIter--; } } else if (ldTarget instanceof CustomInvoke) { // WE Can't pass a custom invoke as the target directly // because it the return value needs to be added to the // stack for the GC } else { switch (ldTarget.getOpcode()) { case Opcodes.ALOAD: { VarOp v = (VarOp) ldTarget; if (isStatic() && v.getIndex() == 0) { newInvoke.setTargetObjectLiteral("__cn1ThisObject"); } else { newInvoke.setTargetObjectLiteral("locals[" + v.getIndex() + "].data.o"); } instructions.remove(iter - numArgs - 1); newIter--; break; } } } } newInvoke.setOptimized(true); //iter = 0; instructionCount = instructions.size(); iter = newIter; } } } break; } } astoreCalls = astoreCalls || currentOpcode == Opcodes.ASTORE || currentOpcode == Opcodes.ISTORE || currentOpcode == Opcodes.LSTORE || currentOpcode == Opcodes.DSTORE || currentOpcode == Opcodes.FSTORE; hasInstructions = hasInstructions | current.getOpcode() != -1; } return hasInstructions; }
From source file:com.codename1.tools.translator.bytecodes.Jump.java
License:Open Source License
@Override public void appendInstruction(StringBuilder b, List<Instruction> instructions) { b.append(" "); switch (opcode) { case Opcodes.IFEQ: b.append("if(POP_INT() == 0) /* IFEQ */ "); break;// w ww.ja va2 s. c o m case Opcodes.IFNE: b.append("if(POP_INT() != 0) /* IFNE */ "); break; case Opcodes.IFLT: b.append("if(POP_INT() < 0) /* IFLT */ "); break; case Opcodes.IFGE: b.append("if(POP_INT() >= 0) /* IFGE */ "); break; case Opcodes.IFGT: b.append("if(POP_INT() > 0) /* IFGT */ "); break; case Opcodes.IFLE: b.append("if(POP_INT() <= 0) /* IFLE */ "); break; case Opcodes.IF_ICMPEQ: b.append("SP-=2; if((*SP).data.i == SP[1].data.i) /* IF_ICMPEQ */ "); break; case Opcodes.IF_ICMPNE: b.append("SP-=2; if((*SP).data.i != SP[1].data.i) /* IF_ICMPNE */ "); break; case Opcodes.IF_ICMPLT: b.append("SP-=2; if((*SP).data.i < SP[1].data.i) /* IF_ICMPLT */ "); break; case Opcodes.IF_ICMPGE: b.append("SP-=2; if((*SP).data.i >= SP[1].data.i) /* IF_ICMPGE */ "); break; case Opcodes.IF_ICMPGT: b.append("SP-=2; if((*SP).data.i > SP[1].data.i) /* IF_ICMPGT */ "); break; case Opcodes.IF_ICMPLE: b.append("SP-=2; if((*SP).data.i <= SP[1].data.i) /* IF_ICMPLE */ "); break; case Opcodes.IF_ACMPEQ: b.append("SP-=2; if((*SP).data.o == SP[1].data.o) /* IF_ACMPEQ */ "); break; case Opcodes.IF_ACMPNE: b.append("SP-=2; if((*SP).data.o != SP[1].data.o) /* IF_ACMPNE */ "); break; case Opcodes.GOTO: // this space intentionally left blank break; case Opcodes.JSR: b.append("/* JSR TODO */"); /*b.append("PUSH_") b.append("goto label_"); b.append(label.toString()); b.append(";\n"); b.append("JSR_RETURN_LABEL_"); b.append(jsrCounter); b.append(":"); jsrCounter++;*/ return; case Opcodes.IFNULL: b.append("if(POP_OBJ() == JAVA_NULL) /* IFNULL */ "); break; case Opcodes.IFNONNULL: b.append("if(POP_OBJ() != JAVA_NULL) /* IFNONNULL */ "); break; } if (TryCatch.isTryCatchInMethod()) { b.append("JUMP_TO(label_"); b.append(label.toString()); b.append(", "); b.append(LabelInstruction.getLabelCatchDepth(label, instructions)); b.append(");\n"); } else { b.append("goto label_"); b.append(label.toString()); b.append(";\n"); } }
From source file:com.github.anba.es6draft.compiler.assembler.InstructionAssembler.java
License:Open Source License
public void ificmpgt(Jump jump) { methodVisitor.visitJumpInsn(Opcodes.IF_ICMPGT, jump.target()); stack.ificmpgt(jump); }
From source file:com.google.devtools.build.android.desugar.BytecodeTypeInference.java
License:Open Source License
@Override public void visitJumpInsn(int opcode, Label label) { switch (opcode) { case Opcodes.IFEQ: case Opcodes.IFNE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: pop();/*from w w w .ja v a 2 s .co m*/ break; case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: pop(2); break; case Opcodes.GOTO: break; case Opcodes.JSR: throw new RuntimeException("The JSR instruction is not supported."); case Opcodes.IFNULL: case Opcodes.IFNONNULL: pop(1); break; default: throw new RuntimeException("Unhandled opcode " + opcode); } super.visitJumpInsn(opcode, label); }
From source file:com.google.test.metric.asm.MethodVisitorBuilder.java
License:Apache License
public void visitJumpInsn(final int opcode, final Label label) { if (opcode == Opcodes.GOTO) { recorder.add(new Runnable() { public void run() { block.addOp(new Transform(lineNumber, "GOTO", null, null, null)); block.unconditionalGoto(label); }//w ww . ja v a2 s . co m }); } else if (opcode == Opcodes.JSR) { recorder.add(new Runnable() { public void run() { block.jumpSubroutine(label, lineNumber); } }); } else { recorder.add(new Runnable() { public void run() { cyclomaticComplexity.add(lineNumber); switch (opcode) { case Opcodes.IFEQ: if1("IFEQ"); break; case Opcodes.IFNE: if1("IFNE"); break; case Opcodes.IFLT: if1("IFLT"); break; case Opcodes.IFGE: if1("IFGE"); break; case Opcodes.IFGT: if1("IFGT"); break; case Opcodes.IFLE: if1("IFLE"); break; case Opcodes.IFNONNULL: if1("IFNONNULL"); break; case Opcodes.IFNULL: if1("IFNULL"); break; case Opcodes.IF_ACMPEQ: if2("IF_ACMPEQ"); break; case Opcodes.IF_ACMPNE: if2("IF_ACMPNE"); break; case Opcodes.IF_ICMPEQ: if2("IF_ICMPEQ"); break; case Opcodes.IF_ICMPGE: if2("IF_ICMPGE"); break; case Opcodes.IF_ICMPGT: if2("IF_ICMPGT"); break; case Opcodes.IF_ICMPLE: if2("IF_ICMPLE"); break; case Opcodes.IF_ICMPLT: if2("IF_ICMPLT"); break; case Opcodes.IF_ICMPNE: if2("IF_ICMPNE"); break; default: throw new UnsupportedOperationException("" + opcode); } block.conditionalGoto(label); } private void if1(String name) { block.addOp(new Transform(lineNumber, name, JavaType.INT, null, null)); } private void if2(String name) { block.addOp(new Transform(lineNumber, name, JavaType.INT, JavaType.INT, null)); } }); } }
From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java
License:Apache License
public void performJumpInsn(int opcode, Label label) { switch (opcode) { case Opcodes.IFEQ: case Opcodes.IFNE: case Opcodes.IFLT: case Opcodes.IFGE: case Opcodes.IFGT: case Opcodes.IFLE: case Opcodes.IFNULL: case Opcodes.IFNONNULL: pop();//ww w. ja va 2s .co m break; case Opcodes.IF_ICMPEQ: case Opcodes.IF_ICMPNE: case Opcodes.IF_ICMPLT: case Opcodes.IF_ICMPGE: case Opcodes.IF_ICMPGT: case Opcodes.IF_ICMPLE: case Opcodes.IF_ACMPEQ: case Opcodes.IF_ACMPNE: pop2(); break; case Opcodes.GOTO: //nop break; case Opcodes.JSR: //nop -- a fudge break; } }
From source file:com.nginious.http.xsp.expr.SubstrFunction.java
License:Apache License
/** * Creates bytecode for evaluating this substring function. The specified method visitor * and type are used for generating bytecode. * /* w w w .j a v a 2s . co m*/ * @param visitor the method visitor * @param type the type */ void compile(MethodVisitor visitor, Type type) { value.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ASTORE, 1); value2.compile(visitor, Type.INT); visitor.visitVarInsn(Opcodes.ISTORE, 2); value3.compile(visitor, Type.INT); visitor.visitVarInsn(Opcodes.ISTORE, 3); Label nullLabel = new Label(); Label notNullLabel = new Label(); // check for null string visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitJumpInsn(Opcodes.IFNULL, nullLabel); // start < 0 Label label1 = new Label(); visitor.visitLdcInsn(0); visitor.visitVarInsn(Opcodes.ILOAD, 2); visitor.visitJumpInsn(Opcodes.IF_ICMPLT, label1); visitor.visitLdcInsn(0); visitor.visitVarInsn(Opcodes.ISTORE, 2); // start > value.length Label label2 = new Label(); visitor.visitLabel(label1); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"); visitor.visitVarInsn(Opcodes.ILOAD, 2); visitor.visitJumpInsn(Opcodes.IF_ICMPGT, label2); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"); visitor.visitVarInsn(Opcodes.ISTORE, 2); // end < start Label label3 = new Label(); visitor.visitLabel(label2); visitor.visitVarInsn(Opcodes.ILOAD, 2); visitor.visitVarInsn(Opcodes.ILOAD, 3); visitor.visitJumpInsn(Opcodes.IF_ICMPLT, label3); visitor.visitVarInsn(Opcodes.ILOAD, 2); visitor.visitVarInsn(Opcodes.ISTORE, 3); // end > value1.length Label label4 = new Label(); visitor.visitLabel(label3); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"); visitor.visitVarInsn(Opcodes.ILOAD, 3); visitor.visitJumpInsn(Opcodes.IF_ICMPGT, label4); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"); visitor.visitVarInsn(Opcodes.ISTORE, 3); // substr visitor.visitLabel(label4); visitor.visitVarInsn(Opcodes.ALOAD, 1); visitor.visitVarInsn(Opcodes.ILOAD, 2); visitor.visitVarInsn(Opcodes.ILOAD, 3); visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "substring", "(II)Ljava/lang/String;"); visitor.visitJumpInsn(Opcodes.GOTO, notNullLabel); visitor.visitLabel(nullLabel); visitor.visitInsn(Opcodes.ACONST_NULL); visitor.visitLabel(notNullLabel); }
From source file:com.nginious.http.xsp.ForEachTagPart.java
License:Apache License
/** * Creates bytecode in a separate method for evaluating this for each tag part. * /*from w ww . j av a 2s . c om*/ * @param intClassName the binary class name of the class being created * @param writer the class writer * @throws XspException if unable to create bytecode */ void compileMethod(String intClassName, ClassWriter writer) throws XspException { String[] exceptions = { "com/nginious/http/xsp/XspException" }; MethodVisitor visitor = writer.visitMethod(Opcodes.ACC_PRIVATE, this.methodName, "(Lcom/nginious/http/HttpRequest;Lcom/nginious/http/HttpResponse;Ljava/lang/StringBuffer;)V", null, exceptions); visitor.visitCode(); Label tryLabel = new Label(); Label startCatchLabel = new Label(); // Start try block visitor.visitTryCatchBlock(tryLabel, startCatchLabel, startCatchLabel, "java/lang/Exception"); visitor.visitLabel(tryLabel); try { String expression = setValue.getExpressionContent(); ExpressionParser parser = new ExpressionParser(); TreeExpression expr = parser.parse(expression); if (expr.getType() != Type.ANY) { throw new XspException("Expression in attribute set in tag " + getName() + " is not an attribute or bean property " + " at line " + getLocationDescriptor()); } expr.compile(visitor, Type.ANY); visitor.visitTypeInsn(Opcodes.CHECKCAST, "java/util/Collection"); visitor.visitVarInsn(Opcodes.ASTORE, 4); } catch (ExpressionException e) { throw new XspException("Invalid expression in attribute set in tag " + getName() + " at line " + getLocationDescriptor(), e); } Label labelOut = new Label(); visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitJumpInsn(Opcodes.IFNULL, labelOut); // Start if (this.start != null) { start.compile(visitor, Type.INT); } else { visitor.visitLdcInsn((int) 0); } visitor.visitVarInsn(Opcodes.ISTORE, 5); // End if (this.end != null) { end.compile(visitor, Type.INT); } else { visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "size", "()I"); } visitor.visitVarInsn(Opcodes.ISTORE, 6); // Step if (this.step != null) { step.compile(visitor, Type.INT); } else { visitor.visitLdcInsn((int) 1); } visitor.visitVarInsn(Opcodes.ISTORE, 7); // Current pos visitor.visitLdcInsn(0); visitor.visitVarInsn(Opcodes.ISTORE, 8); visitor.visitVarInsn(Opcodes.ALOAD, 4); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Collection", "iterator", "()Ljava/util/Iterator;"); visitor.visitVarInsn(Opcodes.ASTORE, 9); Label labelStart = new Label(); // Start of loop visitor.visitLabel(labelStart); // iterator.hasNext(); visitor.visitVarInsn(Opcodes.ALOAD, 9); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z"); visitor.visitJumpInsn(Opcodes.IFEQ, labelOut); // iterator.next(); visitor.visitVarInsn(Opcodes.ALOAD, 9); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;"); visitor.visitVarInsn(Opcodes.ASTORE, 10); // pos >= start && pos <= end && (pos - start) % step == 0 Label labelIncr = new Label(); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 5); visitor.visitJumpInsn(Opcodes.IF_ICMPLT, labelIncr); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 6); visitor.visitJumpInsn(Opcodes.IF_ICMPGT, labelIncr); visitor.visitVarInsn(Opcodes.ILOAD, 8); visitor.visitVarInsn(Opcodes.ILOAD, 5); visitor.visitInsn(Opcodes.ISUB); visitor.visitVarInsn(Opcodes.ILOAD, 7); visitor.visitInsn(Opcodes.IREM); visitor.visitJumpInsn(Opcodes.IFNE, labelIncr); visitor.visitVarInsn(Opcodes.ALOAD, 1); varName.compile(visitor, Type.STRING); visitor.visitVarInsn(Opcodes.ALOAD, 10); visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, "com/nginious/http/HttpRequest", "setAttribute", "(Ljava/lang/String;Ljava/lang/Object;)V"); // Call sub parts for (XspPart part : this.contentParts) { part.compile(intClassName, writer, visitor); } // pos++ visitor.visitLabel(labelIncr); visitor.visitIincInsn(8, 1); visitor.visitJumpInsn(Opcodes.GOTO, labelStart); visitor.visitLabel(labelOut); visitor.visitInsn(Opcodes.RETURN); visitor.visitLabel(startCatchLabel); visitor.visitVarInsn(Opcodes.ASTORE, 3); visitor.visitTypeInsn(Opcodes.NEW, "com/nginious/http/xsp/XspException"); visitor.visitInsn(Opcodes.DUP); visitor.visitLdcInsn("Attribute set contains an invalid collection for tag " + getName() + " at " + getLocationDescriptor()); visitor.visitVarInsn(Opcodes.ALOAD, 3); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/nginious/http/xsp/XspException", "<init>", "(Ljava/lang/String;Ljava/lang/Throwable;)V"); visitor.visitInsn(Opcodes.ATHROW); visitor.visitMaxs(11, 11); visitor.visitEnd(); }