List of usage examples for org.objectweb.asm Opcodes LDC
int LDC
To view the source code for org.objectweb.asm Opcodes LDC.
Click Source Link
From source file:ch.eiafr.cojac.FloatReplacerMethodVisitor.java
License:Apache License
@Override public void visitLdcInsn(Object cst) { mv.visitLdcInsn(cst);/* w w w. j a va 2 s. co m*/ if (cst instanceof Float) { stats.incrementCounterValue(Opcodes.LDC); mv.visitMethodInsn(INVOKESTATIC, COJAC_FLOAT_WRAPPER_INTERNAL_NAME, "fromFloat", "(F)" + COJAC_FLOAT_WRAPPER_TYPE_DESCR, false); } if (cst instanceof Double) { stats.incrementCounterValue(Opcodes.LDC); mv.visitMethodInsn(INVOKESTATIC, COJAC_DOUBLE_WRAPPER_INTERNAL_NAME, "fromDouble", "(D)" + COJAC_DOUBLE_WRAPPER_TYPE_DESCR, false); } }
From source file:com.android.tools.lint.checks.ApiDetector.java
License:Apache License
private static void checkSimpleDateFormat(ClassContext context, MethodNode method, MethodInsnNode node, int minSdk) { if (minSdk >= 9) { // Already OK return;// ww w .j a v a 2 s . com } if (node.name.equals(CONSTRUCTOR_NAME) && !node.desc.equals("()V")) { //$NON-NLS-1$ // Check first argument AbstractInsnNode prev = LintUtils.getPrevInstruction(node); if (prev != null && !node.desc.equals("(Ljava/lang/String;)V")) { //$NON-NLS-1$ prev = LintUtils.getPrevInstruction(prev); } if (prev != null && prev.getOpcode() == Opcodes.LDC) { LdcInsnNode ldc = (LdcInsnNode) prev; Object cst = ldc.cst; if (cst instanceof String) { String pattern = (String) cst; boolean isEscaped = false; for (int i = 0; i < pattern.length(); i++) { char c = pattern.charAt(i); if (c == '\'') { isEscaped = !isEscaped; } else if (!isEscaped && (c == 'L' || c == 'c')) { String message = String .format("The pattern character '%1$c' requires API level 9 (current " + "min is %2$d) : \"%3$s\"", c, minSdk, pattern); report(context, message, node, method, pattern, null, SearchHints.create(FORWARD)); return; } } } } } }
From source file:com.android.tools.lint.checks.CordovaVersionDetector.java
License:Apache License
private void checkInstructionInternal(ClassContext context, ClassNode classNode, AbstractInsnNode instruction) { FieldInsnNode node = (FieldInsnNode) instruction; if (node.getOpcode() == Opcodes.PUTSTATIC && node.owner.equals(FQN_CORDOVA_DEVICE) && node.name.equals(FIELD_NAME_CORDOVA_VERSION)) { AbstractInsnNode prevInstruction = LintUtils.getPrevInstruction(node); if (prevInstruction == null || prevInstruction.getOpcode() != Opcodes.LDC) { return; }/*from w w w.j a v a 2 s .c om*/ LdcInsnNode ldcInsnNode = (LdcInsnNode) prevInstruction; if (ldcInsnNode.cst instanceof String) { mCordovaVersion = createVersion((String) ldcInsnNode.cst); if (mCordovaVersion != null) { validateCordovaVersion(context, mCordovaVersion, context.getLocation(classNode)); } } } }
From source file:com.android.tools.lint.checks.LocaleDetector.java
License:Apache License
@Override public void checkCall(@NonNull ClassContext context, @NonNull ClassNode classNode, @NonNull MethodNode method, @NonNull MethodInsnNode call) {//from ww w . j av a2 s . c o m String owner = call.owner; String desc = call.desc; String name = call.name; if (owner.equals(DATE_FORMAT_OWNER)) { if (!name.equals(CONSTRUCTOR_NAME)) { return; } if (desc.equals("(Ljava/lang/String;Ljava/text/DateFormatSymbols;)V") //$NON-NLS-1$ || desc.equals("()V") //$NON-NLS-1$ || desc.equals("(Ljava/lang/String;)V")) { //$NON-NLS-1$ Location location = context.getLocation(call); String message = "To get local formatting use getDateInstance(), getDateTimeInstance(), " + "or getTimeInstance(), or use new SimpleDateFormat(String template, " + "Locale locale) with for example Locale.US for ASCII dates."; context.report(DATE_FORMAT, method, call, location, message, null); } return; } else if (!owner.equals(STRING_OWNER)) { return; } if (name.equals(FORMAT_METHOD)) { // Only check the non-locale version of String.format if (!desc.equals("(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;")) { //$NON-NLS-1$ return; } // Find the formatting string Analyzer analyzer = new Analyzer(new SourceInterpreter() { @Override public SourceValue newOperation(AbstractInsnNode insn) { if (insn.getOpcode() == Opcodes.LDC) { Object cst = ((LdcInsnNode) insn).cst; if (cst instanceof String) { return new StringValue(1, (String) cst); } } return super.newOperation(insn); } }); try { Frame[] frames = analyzer.analyze(classNode.name, method); InsnList instructions = method.instructions; Frame frame = frames[instructions.indexOf(call)]; if (frame.getStackSize() == 0) { return; } SourceValue stackValue = (SourceValue) frame.getStack(0); if (stackValue instanceof StringValue) { String format = ((StringValue) stackValue).getString(); if (format != null && StringFormatDetector.isLocaleSpecific(format)) { Location location = context.getLocation(call); String message = "Implicitly using the default locale is a common source of bugs: " + "Use String.format(Locale, ...) instead"; context.report(STRING_LOCALE, method, call, location, message, null); } } } catch (AnalyzerException e) { context.log(e, null); } } else { if (desc.equals("()Ljava/lang/String;")) { //$NON-NLS-1$ Location location = context.getLocation(call); String message = String.format("Implicitly using the default locale is a common source of bugs: " + "Use %1$s(Locale) instead", name); context.report(STRING_LOCALE, method, call, location, message, null); } } }
From source file:com.android.tools.lint.checks.SecureRandomDetector.java
License:Apache License
private static void checkValidSetSeed(ClassContext context, MethodInsnNode call) { assert call.name.equals(SET_SEED); // Make sure the argument passed is not a literal AbstractInsnNode prev = LintUtils.getPrevInstruction(call); if (prev == null) { return;//from w w w . j a v a 2 s . c o m } int opcode = prev.getOpcode(); if (opcode == Opcodes.LCONST_0 || opcode == Opcodes.LCONST_1 || opcode == Opcodes.LDC) { context.report(ISSUE, context.getLocation(call), "Do not call setSeed() on a SecureRandom with a fixed seed: " + "it is not secure. Use getSeed().", null); } else if (opcode == Opcodes.INVOKESTATIC) { String methodName = ((MethodInsnNode) prev).name; if (methodName.equals("currentTimeMillis") || methodName.equals("nanoTime")) { context.report(ISSUE, context.getLocation(call), "It is dangerous to seed SecureRandom with the current time because " + "that value is more predictable to an attacker than the default seed.", null); } } }
From source file:com.android.tools.lint.checks.WakelockDetector.java
License:Apache License
@Override public void checkCall(@NonNull ClassContext context, @NonNull ClassNode classNode, @NonNull MethodNode method, @NonNull MethodInsnNode call) {//from ww w. java 2 s . c o m if (!context.getProject().getReportIssues()) { // If this is a library project not being analyzed, ignore it return; } if (call.owner.equals(WAKELOCK_OWNER)) { String name = call.name; if (name.equals(ACQUIRE_METHOD)) { mHasAcquire = true; if (context.getDriver().getPhase() == 2) { assert !mHasRelease; context.report(ISSUE, method, call, context.getLocation(call), "Found a wakelock acquire() but no release() calls anywhere", null); } else { assert context.getDriver().getPhase() == 1; // Perform flow analysis in this method to see if we're // performing an acquire/release block, where there are code paths // between the acquire and release which can result in the // release call not getting reached. checkFlow(context, classNode, method, call); } } else if (name.equals(RELEASE_METHOD)) { mHasRelease = true; // See if the release is happening in an onDestroy method, in an // activity. if ("onDestroy".equals(method.name) //$NON-NLS-1$ && context.getDriver().isSubclassOf(classNode, ANDROID_APP_ACTIVITY)) { context.report(ISSUE, method, call, context.getLocation(call), "Wakelocks should be released in onPause, not onDestroy", null); } } } else if (call.owner.equals(POWER_MANAGER)) { if (call.name.equals(NEW_WAKE_LOCK_METHOD)) { AbstractInsnNode prev = LintUtils.getPrevInstruction(call); if (prev == null) { return; } prev = LintUtils.getPrevInstruction(prev); if (prev == null || prev.getOpcode() != Opcodes.LDC) { return; } LdcInsnNode ldc = (LdcInsnNode) prev; Object constant = ldc.cst; if (constant instanceof Integer) { int flag = ((Integer) constant).intValue(); // Constant values are copied into the bytecode so we have to compare // values; however, that means the values are part of the API final int PARTIAL_WAKE_LOCK = 0x00000001; final int ACQUIRE_CAUSES_WAKEUP = 0x10000000; final int both = PARTIAL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP; if ((flag & both) == both) { context.report(ISSUE, method, call, context.getLocation(call), "Should not set both PARTIAL_WAKE_LOCK and ACQUIRE_CAUSES_WAKEUP. " + "If you do not want the screen to turn on, get rid of " + "ACQUIRE_CAUSES_WAKEUP", null); } } } } }
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 w ww .j a v a 2s. c om*/ 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.ArithmeticExpression.java
public static boolean isArg(Instruction instr) { if (instr instanceof ArithmeticExpression) { return true; }/*from w w w .j a v a2 s . c om*/ if (instr instanceof AssignableExpression) { StringBuilder dummy = new StringBuilder(); if (((AssignableExpression) instr).assignTo(null, dummy)) { return true; } } int opcode = instr.getOpcode(); switch (opcode) { case Opcodes.FLOAD: case Opcodes.DLOAD: case Opcodes.ILOAD: case Opcodes.LLOAD: case org.objectweb.asm.Opcodes.ICONST_0: case org.objectweb.asm.Opcodes.ICONST_1: case org.objectweb.asm.Opcodes.ICONST_2: case org.objectweb.asm.Opcodes.ICONST_3: case org.objectweb.asm.Opcodes.ICONST_4: case org.objectweb.asm.Opcodes.ICONST_5: case org.objectweb.asm.Opcodes.ICONST_M1: case org.objectweb.asm.Opcodes.LCONST_0: case org.objectweb.asm.Opcodes.LCONST_1: case Opcodes.DCONST_0: case Opcodes.DCONST_1: case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: case org.objectweb.asm.Opcodes.BIPUSH: case org.objectweb.asm.Opcodes.SIPUSH: case Opcodes.LDC: return true; } return false; }
From source file:com.codename1.tools.translator.bytecodes.ArithmeticExpression.java
public String getExpressionAsString() { Instruction instr = lastInstruction; int opcode = lastInstruction.getOpcode(); if (subExpression == null) { // This is the root of it... probably an FLOAD if (lastInstruction instanceof AssignableExpression && !(lastInstruction instanceof ArithmeticExpression)) { StringBuilder out = new StringBuilder(); if (((AssignableExpression) lastInstruction).assignTo(null, out)) { String strOut = out.toString(); if (strOut.trim().isEmpty()) { throw new RuntimeException("Instruction produces blank string output: " + lastInstruction); }/* ww w.ja v a 2 s. c o m*/ if (strOut == null || "null".equals(strOut)) { throw new RuntimeException( "ArithmeticExpression produced null value. This shouldn't happen: " + lastInstruction); } return strOut; } } if (lastInstruction instanceof VarOp) { VarOp var = (VarOp) lastInstruction; switch (opcode) { case Opcodes.FLOAD: { return "flocals_" + var.getIndex() + "_"; } case Opcodes.DLOAD: { return "dlocals_" + var.getIndex() + "_"; } case Opcodes.ILOAD: { return "ilocals_" + var.getIndex() + "_"; } case Opcodes.LLOAD: { return "llocals_" + var.getIndex() + "_"; } case org.objectweb.asm.Opcodes.ICONST_0: { return "0"; } case org.objectweb.asm.Opcodes.ICONST_1: { return "1"; } case org.objectweb.asm.Opcodes.ICONST_2: { return "2"; } case org.objectweb.asm.Opcodes.ICONST_3: { return "3"; } case org.objectweb.asm.Opcodes.ICONST_4: { return "4"; } case org.objectweb.asm.Opcodes.ICONST_5: { return "5"; } case org.objectweb.asm.Opcodes.ICONST_M1: { return "(-1)"; } case org.objectweb.asm.Opcodes.LCONST_0: { return "((JAVA_LONG)0)"; } case Opcodes.DCONST_0: { return "((JAVA_DOUBLE)0)"; } case Opcodes.DCONST_1: { return "((JAVA_DOUBLE)1)"; } case Opcodes.FCONST_0: { return "((JAVA_FLOAT)0)"; } case Opcodes.FCONST_1: { return "((JAVA_FLOAT)1)"; } case Opcodes.FCONST_2: { return "((JAVA_FLOAT)2"; } case org.objectweb.asm.Opcodes.LCONST_1: { return "((JAVA_LONG)1)"; } case org.objectweb.asm.Opcodes.BIPUSH: case org.objectweb.asm.Opcodes.SIPUSH: { return String.valueOf(var.getIndex()); } default: { throw new RuntimeException("Unsupported Opcode in ArithmeticExpression: " + opcode + " " + var); } } } else { switch (instr.getOpcode()) { case org.objectweb.asm.Opcodes.ICONST_0: { return "0"; } case org.objectweb.asm.Opcodes.ICONST_1: { return "1"; } case org.objectweb.asm.Opcodes.ICONST_2: { return "2"; } case org.objectweb.asm.Opcodes.ICONST_3: { return "3"; } case org.objectweb.asm.Opcodes.ICONST_4: { return "4"; } case org.objectweb.asm.Opcodes.ICONST_5: { return "5"; } case org.objectweb.asm.Opcodes.ICONST_M1: { return "(-1)"; } case org.objectweb.asm.Opcodes.LCONST_0: { return "((JAVA_LONG)0)"; } case org.objectweb.asm.Opcodes.LCONST_1: { return "(JAVA_LONG)1"; } case org.objectweb.asm.Opcodes.BIPUSH: { if (instr instanceof BasicInstruction) { return String.valueOf(((BasicInstruction) instr).getValue()); } break; } case org.objectweb.asm.Opcodes.LDC: { if (instr instanceof Ldc) { Ldc ldc = (Ldc) instr; return ldc.getValueAsString(); } break; } default: { throw new RuntimeException( "Unsupported Opcode in ArithmeticExpression: " + opcode + " " + instr); } } } } else { switch (opcode) { case Opcodes.ISHL: { return "BC_ISHL_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.ISHR: { return "BC_ISHR_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.LSHL: { return "BC_LSHL_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.LSHR: { return "BC_LSHR_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.IUSHR: { return "BC_IUSHR_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.LUSHR: { return "BC_LUSHR_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.DCMPG: case Opcodes.DCMPL: case Opcodes.FCMPG: case Opcodes.FCMPL: case Opcodes.LCMP: { return "CN1_CMP_EXPR(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.D2F: { return "((JAVA_FLOAT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.F2D: { return subExpression.getExpressionAsString().trim(); } case Opcodes.F2I: { return "((JAVA_INT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.F2L: { return "((JAVA_LONG)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.D2I: { return "((JAVA_INT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.D2L: { return "((JAVA_LONG)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.I2B: { return "((" + subExpression.getExpressionAsString() + " << 24) >> 24)"; } case Opcodes.I2C: { return "(" + subExpression.getExpressionAsString().trim() + " & 0xffff)"; } case Opcodes.I2D: { return "((JAVA_DOUBLE)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.I2F: { return "((JAVA_FLOAT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.I2L: { return "((JAVA_LONG)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.I2S: { return "((" + subExpression.getExpressionAsString().trim() + " << 16) >> 16)"; } case Opcodes.L2D: { return "((JAVA_DOUBLE)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.L2F: { return "((JAVA_FLOAT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.L2I: { return "((JAVA_INT)" + subExpression.getExpressionAsString().trim() + ")"; } case Opcodes.IAND: case Opcodes.LAND: { return "(" + subExpression.getExpressionAsString().trim() + " & " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.IOR: case Opcodes.LOR: { return "(" + subExpression.getExpressionAsString().trim() + " | " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.IXOR: case Opcodes.LXOR: { return "(" + subExpression.getExpressionAsString().trim() + " ^ " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.DADD: case Opcodes.IADD: case Opcodes.LADD: case Opcodes.FADD: { return "(" + subExpression.getExpressionAsString().trim() + " + " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.DSUB: case Opcodes.ISUB: case Opcodes.LSUB: case Opcodes.FSUB: { return "(" + subExpression.getExpressionAsString().trim() + " - " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.DMUL: case Opcodes.IMUL: case Opcodes.LMUL: case Opcodes.FMUL: { return "(" + subExpression.getExpressionAsString().trim() + " * " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.DDIV: case Opcodes.IDIV: case Opcodes.LDIV: case Opcodes.FDIV: { return "(" + subExpression.getExpressionAsString().trim() + " / " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.FREM: case Opcodes.DREM: { return "fmod(" + subExpression.getExpressionAsString().trim() + ", " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.LREM: case Opcodes.IREM: { if (subExpression2.getExpressionAsString() == null || "null".equals(subExpression2.getExpressionAsString())) { throw new RuntimeException("2nd param of REM is null. Should never happen. Expression is " + subExpression2 + " with last instruction " + subExpression2.lastInstruction); } return "(" + subExpression.getExpressionAsString().trim() + " % " + subExpression2.getExpressionAsString().trim() + ")"; } case Opcodes.FNEG: case Opcodes.INEG: case Opcodes.LNEG: case Opcodes.DNEG: return "(-(" + subExpression.getExpressionAsString().trim() + "))"; default: { throw new RuntimeException("Unsupported Opcode in ArithmeticExpression: " + opcode + " " + instr); } } } throw new RuntimeException("Did not return a value in getExpressionAsString() with lastInstruction " + lastInstruction + " subExpression " + subExpression + " and subExpression2 " + subExpression2); }
From source file:com.codename1.tools.translator.bytecodes.BasicInstruction.java
License:Open Source License
@Override public boolean isConstant() { switch (opcode) { case Opcodes.ACONST_NULL: case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: case Opcodes.ICONST_M1: case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: case Opcodes.DCONST_0: case Opcodes.DCONST_1: case Opcodes.LCONST_0: case Opcodes.LCONST_1: case Opcodes.LDC: return true; }//from w w w. j a v a 2 s .co m return super.isConstant(); }