List of usage examples for org.objectweb.asm Opcodes ICONST_0
int ICONST_0
To view the source code for org.objectweb.asm Opcodes ICONST_0.
Click Source Link
From source file:org.jboss.arquillian.extension.jacoco.container.ArquillianRuntime.java
License:Apache License
public int generateDataAccessor(long classid, String classname, int probecount, MethodVisitor mv) { // 1. Create parameter array: generateArgumentArray(classid, classname, probecount, mv); // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.DUP);//from w w w. java 2 s .c o m // stack[1]: [Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; // 2. Invoke ArquillianRuntime: mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/jboss/arquillian/extension/jacoco/container/ArquillianRuntime", "getInstance", "()Lorg/jboss/arquillian/extension/jacoco/container/ArquillianRuntime;"); // stack[2]: LArquillianRuntime; // stack[1]: [Ljava/lang/Object; // stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.SWAP); // stack[2]: [Ljava/lang/Object; // stack[1]: LArquillianRuntime; // stack[0]: [Ljava/lang/Object; // 3. Invoke ArquillianRuntime swapExecutionData, gets the boolean[] in Object[0]: mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/jboss/arquillian/extension/jacoco/container/ArquillianRuntime", "swapExecutionData", "([Ljava/lang/Object;)V"); // Stack[0]: [Ljava/lang/Object; mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.AALOAD); mv.visitTypeInsn(Opcodes.CHECKCAST, InstrSupport.DATAFIELD_DESC); // Stack[0]: [Z; return 5; }
From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java
License:Open Source License
/** * Generates the instruction to push the given value on the stack. * * @param value the value to be pushed on the stack. *//*from ww w .j a v a 2 s.co m*/ public void push(final int value) { if (value >= -1 && value <= 5) { visitInsn(Opcodes.ICONST_0 + value); } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) { visitIntInsn(Opcodes.BIPUSH, value); } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) { visitIntInsn(Opcodes.SIPUSH, value); } else { visitLdcInsn(new Integer(value)); } }
From source file:org.jboss.byteman.rule.expression.NumericLiteral.java
License:Open Source License
public void compile(MethodVisitor mv, CompileContext compileContext) throws CompileException { // make sure we are at the right source line compileContext.notifySourceLine(line); if (type == Type.I) { int val = value.intValue(); // compile code to stack int value if (val >= -1 && val <= 5) { // we can use an iconst instruction mv.visitInsn(Opcodes.ICONST_0 + val); } else {/* www . j av a2s. c o m*/ // we have to add an integer constant to the constants pool mv.visitLdcInsn(value); } // we have only added 1 to the stack height compileContext.addStackCount(1); } else { // type = type.F float val = value.floatValue(); if (val == 0.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_0); } else if (val == 1.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_1); } else if (val == 2.0) { // we can use an fconst instruction mv.visitInsn(Opcodes.FCONST_2); } else { // we have to add a float constant to the constants pool mv.visitLdcInsn(value); } // we have only added 1 to the stack height compileContext.addStackCount(1); } }
From source file:org.jooby.internal.apitool.BytecodeRouteParser.java
License:Apache License
private Object paramValue(final ClassLoader loader, final ClassNode owner, final MethodNode method, final AbstractInsnNode n) { if (n instanceof LdcInsnNode) { Object cst = ((LdcInsnNode) n).cst; if (cst instanceof Type) { boolean typePresent = new Insn<>(method, n).next().filter(is(MethodInsnNode.class)).findFirst() .map(MethodInsnNode.class::cast).filter(mutantToSomething().or(getOrCreateKotlinClass())) .isPresent();//from w w w. j a v a2s.com if (typePresent) { return null; } return loadType(loader, ((Type) cst).getClassName()); } return cst; } else if (n instanceof InsnNode) { InsnNode insn = (InsnNode) n; switch (insn.getOpcode()) { case Opcodes.ICONST_0: return 0; case Opcodes.ICONST_1: return 1; case Opcodes.ICONST_2: return 2; case Opcodes.ICONST_3: return 3; case Opcodes.ICONST_4: return 4; case Opcodes.ICONST_5: return 5; case Opcodes.LCONST_0: return 0L; case Opcodes.LCONST_1: return 1L; case Opcodes.FCONST_0: return 0f; case Opcodes.FCONST_1: return 1f; case Opcodes.FCONST_2: return 2f; case Opcodes.DCONST_0: return 0d; case Opcodes.DCONST_1: return 1d; case Opcodes.ICONST_M1: return -1; case Opcodes.ACONST_NULL: return null; } } else if (n instanceof IntInsnNode) { IntInsnNode intinsn = (IntInsnNode) n; return intinsn.operand; } else if (n instanceof FieldInsnNode) { // toEnum FieldInsnNode finsn = (FieldInsnNode) n; if (finsn.getOpcode() == GETSTATIC) { java.lang.reflect.Type possibleEnum = loadType(loader, finsn.owner); if (MoreTypes.getRawType(possibleEnum).isEnum()) { return finsn.name; } } } return n; }
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
License:Open Source License
/** * Reads the current {@link InsnNode} instruction and returns a {@link Statement} or {@code null} * if the instruction is not a full statement (in that case, the instruction is stored in the * given Expression {@link Stack})./*w ww . ja v a 2 s .c o m*/ * * @param insnNode the instruction to read * @param expressionStack the expression stack to put on or pop from. * @param localVariables the local variables * @return a {@link List} of {@link Statement} or empty list if no {@link Statement} was created * after reading the current instruction. * @see <a href="https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings">Java bytcode * instruction listings on Wikipedia</a> */ private List<Statement> readInstruction(final InsnCursor insnCursor, final Stack<Expression> expressionStack, final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) { final List<Statement> statements = new ArrayList<>(); final AbstractInsnNode insnNode = insnCursor.getCurrent(); switch (insnNode.getOpcode()) { // return a reference from a method case Opcodes.ARETURN: // return an integer from a method case Opcodes.IRETURN: statements.add(new ReturnStatement(expressionStack.pop())); break; // return void from method case Opcodes.RETURN: // wrap all pending expressions into ExpressionStatements while (!expressionStack.isEmpty()) { final Expression pendingExpression = expressionStack.pop(); statements.add(new ExpressionStatement(pendingExpression)); } break; // push a null reference onto the stack case Opcodes.ACONST_NULL: expressionStack.add(new NullLiteral()); break; // load the int value 0 onto the stack case Opcodes.ICONST_0: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(0)); break; // load the int value 1 onto the stack case Opcodes.ICONST_1: // applies for byte, short, int and boolean expressionStack.add(new NumberLiteral(1)); break; // load the int value 2 onto the stack case Opcodes.ICONST_2: expressionStack.add(new NumberLiteral(2)); break; // load the int value 3 onto the stack case Opcodes.ICONST_3: expressionStack.add(new NumberLiteral(3)); break; // load the int value 4 onto the stack case Opcodes.ICONST_4: expressionStack.add(new NumberLiteral(4)); break; // load the int value 5 onto the stack case Opcodes.ICONST_5: expressionStack.add(new NumberLiteral(5)); break; // push the long 0 onto the stack case Opcodes.LCONST_0: expressionStack.add(new NumberLiteral(0L)); break; // push the long 1 onto the stack case Opcodes.LCONST_1: expressionStack.add(new NumberLiteral(1L)); break; // push the 0.0f onto the stack case Opcodes.FCONST_0: expressionStack.add(new NumberLiteral(0f)); break; // push the 1.0f onto the stack case Opcodes.FCONST_1: expressionStack.add(new NumberLiteral(1f)); break; // push the 2.0f onto the stack case Opcodes.FCONST_2: expressionStack.add(new NumberLiteral(2f)); break; // push the constant 0.0 onto the stack case Opcodes.DCONST_0: expressionStack.add(new NumberLiteral(0d)); break; // push the constant 1.0 onto the stack case Opcodes.DCONST_1: expressionStack.add(new NumberLiteral(1d)); break; // compare two longs values case Opcodes.LCMP: // compare two doubles case Opcodes.DCMPL: // compare two doubles case Opcodes.DCMPG: // compare two floats case Opcodes.FCMPL: // compare two floats case Opcodes.FCMPG: statements.addAll( readJumpInstruction(insnCursor.next(), expressionStack, capturedArguments, localVariables)); break; // add 2 ints case Opcodes.IADD: expressionStack.add(readOperation(Operator.ADD, expressionStack)); break; // int subtract case Opcodes.ISUB: expressionStack.add(readOperation(Operator.SUBTRACT, expressionStack)); break; // multiply 2 integers case Opcodes.IMUL: expressionStack.add(readOperation(Operator.MULTIPLY, expressionStack)); break; // divide 2 integers case Opcodes.IDIV: expressionStack.add(readOperation(Operator.DIVIDE, expressionStack)); break; // negate int case Opcodes.INEG: expressionStack.add(inverseInteger(expressionStack)); break; // discard the top value on the stack case Opcodes.POP: statements.add(new ExpressionStatement(expressionStack.pop())); break; // duplicate the value on top of the stack case Opcodes.DUP: expressionStack.push(expressionStack.peek()); break; // insert a copy of the top value into the stack two values from the top. case Opcodes.DUP_X1: expressionStack.add(expressionStack.size() - 2, expressionStack.peek()); break; // store into a reference in an array case Opcodes.AASTORE: readArrayStoreInstruction(insnNode, expressionStack); break; // converts Float to Double -> ignored. case Opcodes.F2D: break; default: throw new AnalyzeException( "Bytecode instruction with OpCode '" + insnNode.getOpcode() + "' is not supported."); } return statements; }
From source file:org.mbte.groovypp.compiler.asm.LdcImproverMethodAdapter.java
License:Apache License
public void visitLdcInsn(Object cst) { if (cst instanceof Integer) { Integer value = (Integer) cst; switch (value) { case -1:/*from w w w .ja v a 2 s .c o m*/ super.visitInsn(Opcodes.ICONST_M1); break; case 0: super.visitInsn(Opcodes.ICONST_0); break; case 1: super.visitInsn(Opcodes.ICONST_1); break; case 2: super.visitInsn(Opcodes.ICONST_2); break; case 3: super.visitInsn(Opcodes.ICONST_3); break; case 4: super.visitInsn(Opcodes.ICONST_4); break; case 5: super.visitInsn(Opcodes.ICONST_5); break; default: if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) { super.visitIntInsn(Opcodes.BIPUSH, value); } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) { super.visitIntInsn(Opcodes.SIPUSH, value); } else { super.visitLdcInsn(Integer.valueOf(value)); } } } else if (cst instanceof BigDecimal) { super.visitTypeInsn(NEW, "java/math/BigDecimal"); super.visitInsn(DUP); super.visitLdcInsn(cst.toString()); super.visitMethodInsn(INVOKESPECIAL, "java/math/BigDecimal", "<init>", "(Ljava/lang/String;)V"); } else if (cst instanceof BigInteger) { super.visitTypeInsn(NEW, "java/math/BigInteger"); super.visitInsn(DUP); super.visitLdcInsn(cst.toString()); super.visitMethodInsn(INVOKESPECIAL, "java/math/BigInteger", "<init>", "(Ljava/lang/String;)V"); } else if (cst instanceof Double) { Double aDouble = (Double) cst; if (aDouble == 1.0d) super.visitInsn(DCONST_1); else super.visitLdcInsn(cst); } else if (cst instanceof Long) { Long aLong = (Long) cst; if (aLong == 0L) super.visitInsn(LCONST_0); else if (aLong == 1L) super.visitInsn(LCONST_1); else super.visitLdcInsn(cst); } else if (cst instanceof Float) { Float aFloat = (Float) cst; if (aFloat == 1.0f) super.visitInsn(FCONST_1); else if (aFloat == 2.0f) super.visitInsn(FCONST_2); else super.visitLdcInsn(cst); } else if (cst == null) { super.visitInsn(ACONST_NULL); } else super.visitLdcInsn(cst); }
From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java
License:Apache License
protected void pushConstant(boolean value, MethodVisitor mv) { if (value) {/*from ww w . ja v a2 s .c o m*/ mv.visitInsn(Opcodes.ICONST_1); } else { mv.visitInsn(Opcodes.ICONST_0); } }
From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java
License:Apache License
public void pushConstant(int value, MethodVisitor mv) { switch (value) { case 0:/*from w w w. j a v a 2 s . c om*/ mv.visitInsn(Opcodes.ICONST_0); break; case 1: mv.visitInsn(Opcodes.ICONST_1); break; case 2: mv.visitInsn(Opcodes.ICONST_2); break; case 3: mv.visitInsn(Opcodes.ICONST_3); break; case 4: mv.visitInsn(Opcodes.ICONST_4); break; case 5: mv.visitInsn(Opcodes.ICONST_5); break; default: if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) { mv.visitIntInsn(Opcodes.BIPUSH, value); } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) { mv.visitIntInsn(Opcodes.SIPUSH, value); } else { mv.visitLdcInsn(Integer.valueOf(value)); } } }
From source file:org.mbte.groovypp.compiler.bytecode.BytecodeExpr.java
License:Apache License
/** * negate a boolean on stack. true->false, false->true * @param mv/*from w ww . j ava2s.com*/ */ public void negateBoolean(MethodVisitor mv) { // code to negate the primitive boolean Label endLabel = new Label(); Label falseLabel = new Label(); mv.visitJumpInsn(Opcodes.IFNE, falseLabel); mv.visitInsn(Opcodes.ICONST_1); mv.visitJumpInsn(Opcodes.GOTO, endLabel); mv.visitLabel(falseLabel); mv.visitInsn(Opcodes.ICONST_0); mv.visitLabel(endLabel); }
From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java
License:Open Source License
/** * Add the <clinit> method. This initializes the static fields that have initializers. * @param classRep// w w w . j a v a 2 s . com * @param cv * @param initializeForAsserts * @throws JavaGenerationException */ private static void encodeClassInitializer(JavaClassRep classRep, ClassVisitor cv, boolean initializeForAsserts) throws JavaGenerationException { // Add initializers for the statically-initialized fields. final int nFields = classRep.getNFieldDeclarations(); if (!classRep.hasInitializedStaticField() && !initializeForAsserts) { //we don't need to bother adding a static initializer if there are no static fields to initialize. //note that javac also has this optimization. return; } MethodVisitor mv = cv.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); // Start the method's code. mv.visitCode(); final JavaTypeName classRepTypeName = classRep.getClassName(); /* * If this class contains assert statements we need to initialize the static final synthetic boolean field * '$assertionsDisabled'. * This is done by loading the Class constant for the top level class. The method 'desiredAssertionStatus()' * is then invoked on this Class object and the returned value is used to initialize $assertionsDisabled. */ if (initializeForAsserts) { // Get the fully-qualified internal class name. final String className = classRepTypeName.getJVMInternalName(); // Get the top level class name. String topLevelClassName = getTopLevelClassJVMInternalName(className); // Load the Class constant for the top level class mv.visitLdcInsn(Type.getType("L" + topLevelClassName + ";")); // Now that we have the Class constant for the top level class we invoke the method // desiredAssertionStatus on it and use the resulting value to initialize the static field $assertionsDisabled in this class. mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "desiredAssertionStatus", "()Z"); Label l0 = new Label(); mv.visitJumpInsn(Opcodes.IFNE, l0); mv.visitInsn(Opcodes.ICONST_1); Label l1 = new Label(); mv.visitJumpInsn(Opcodes.GOTO, l1); mv.visitLabel(l0); mv.visitInsn(Opcodes.ICONST_0); mv.visitLabel(l1); mv.visitFieldInsn(Opcodes.PUTSTATIC, className, "$assertionsDisabled", "Z"); } for (int i = 0; i < nFields; ++i) { JavaFieldDeclaration fieldDecl = classRep.getFieldDeclaration(i); JavaExpression initializer = fieldDecl.getInitializer(); if (initializer != null && Modifier.isStatic(fieldDecl.getModifiers())) { GenerationContext context = new GenerationContext(new HashMap<String, JavaTypeName>(), new HashMap<String, Integer>(), 0, mv, classRepTypeName); //evaluate the initializer encodeExpr(initializer, context); //do the assignment mv.visitFieldInsn(Opcodes.PUTSTATIC, classRep.getClassName().getJVMInternalName(), fieldDecl.getFieldName(), fieldDecl.getFieldType().getJVMDescriptor()); } } // return. mv.visitInsn(Opcodes.RETURN); //mark the end of the method with a call to visitMaxs mv.visitMaxs(0, 0); // End the method. mv.visitEnd(); }