use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class StaticTypesUnaryExpressionHelper method writeBitwiseNegate.
@Override
public void writeBitwiseNegate(final BitwiseNegationExpression expression) {
expression.getExpression().visit(controller.getAcg());
if (isPrimitiveOnTop()) {
final ClassNode top = getTopOperand();
if (top == int_TYPE || top == short_TYPE || top == byte_TYPE || top == char_TYPE || top == long_TYPE) {
BytecodeExpression bytecodeExpression = new BytecodeExpression() {
@Override
public void visit(final MethodVisitor mv) {
if (long_TYPE == top) {
mv.visitLdcInsn(-1);
mv.visitInsn(LXOR);
} else {
mv.visitInsn(ICONST_M1);
mv.visitInsn(IXOR);
if (byte_TYPE == top) {
mv.visitInsn(I2B);
} else if (char_TYPE == top) {
mv.visitInsn(I2C);
} else if (short_TYPE == top) {
mv.visitInsn(I2S);
}
}
}
};
bytecodeExpression.visit(controller.getAcg());
controller.getOperandStack().remove(1);
return;
}
}
super.writeBitwiseNegate(EMPTY_BITWISE_NEGATE);
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class StaticTypesUnaryExpressionHelper method writeUnaryMinus.
@Override
public void writeUnaryMinus(final UnaryMinusExpression expression) {
expression.getExpression().visit(controller.getAcg());
if (isPrimitiveOnTop()) {
final ClassNode top = getTopOperand();
if (top != boolean_TYPE) {
BytecodeExpression bytecodeExpression = new BytecodeExpression() {
@Override
public void visit(final MethodVisitor mv) {
if (int_TYPE == top || short_TYPE == top || byte_TYPE == top || char_TYPE == top) {
mv.visitInsn(INEG);
if (byte_TYPE == top) {
mv.visitInsn(I2B);
} else if (char_TYPE == top) {
mv.visitInsn(I2C);
} else if (short_TYPE == top) {
mv.visitInsn(I2S);
}
} else if (long_TYPE == top) {
mv.visitInsn(LNEG);
} else if (float_TYPE == top) {
mv.visitInsn(FNEG);
} else if (double_TYPE == top) {
mv.visitInsn(DNEG);
}
}
};
bytecodeExpression.visit(controller.getAcg());
controller.getOperandStack().remove(1);
return;
}
}
// we already visited the sub expression
super.writeUnaryMinus(EMPTY_UNARY_MINUS);
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class MopWriter method generateMopCalls.
/**
* generates a Meta Object Protocol method, that is used to call a non public
* method, or to make a call to super.
*
* @param mopCalls list of methods a mop call method should be generated for
* @param useThis true if "this" should be used for the naming
*/
protected void generateMopCalls(LinkedList<MethodNode> mopCalls, boolean useThis) {
for (MethodNode method : mopCalls) {
String name = getMopMethodName(method, useThis);
Parameter[] parameters = method.getParameters();
String methodDescriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameters());
MethodVisitor mv = controller.getClassVisitor().visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, name, methodDescriptor, null, null);
controller.setMethodVisitor(mv);
mv.visitVarInsn(ALOAD, 0);
int newRegister = 1;
OperandStack operandStack = controller.getOperandStack();
for (Parameter parameter : parameters) {
ClassNode type = parameter.getType();
operandStack.load(parameter.getType(), newRegister);
// increment to next register, double/long are using two places
newRegister++;
if (type == ClassHelper.double_TYPE || type == ClassHelper.long_TYPE)
newRegister++;
}
operandStack.remove(parameters.length);
ClassNode declaringClass = method.getDeclaringClass();
// JDK 8 support for default methods in interfaces
// this should probably be strenghtened when we support the A.super.foo() syntax
int opcode = declaringClass.isInterface() ? INVOKEINTERFACE : INVOKESPECIAL;
mv.visitMethodInsn(opcode, BytecodeHelper.getClassInternalName(declaringClass), method.getName(), methodDescriptor, opcode == INVOKEINTERFACE);
BytecodeHelper.doReturn(mv, method.getReturnType());
mv.visitMaxs(0, 0);
mv.visitEnd();
controller.getClassNode().addMethod(name, ACC_PUBLIC | ACC_SYNTHETIC, method.getReturnType(), parameters, null, null);
}
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class OperandStack method popDownTo.
public void popDownTo(int elements) {
int last = stack.size();
MethodVisitor mv = controller.getMethodVisitor();
while (last > elements) {
last--;
ClassNode element = popWithMessage(last);
if (isTwoSlotType(element)) {
mv.visitInsn(POP2);
} else {
mv.visitInsn(POP);
}
}
}
use of org.objectweb.asm.MethodVisitor in project groovy by apache.
the class OperandStack method storeVar.
public void storeVar(BytecodeVariable variable) {
MethodVisitor mv = controller.getMethodVisitor();
int idx = variable.getIndex();
ClassNode type = variable.getType();
// value is on stack
if (variable.isHolder()) {
doGroovyCast(type);
box();
mv.visitVarInsn(ALOAD, idx);
mv.visitTypeInsn(CHECKCAST, "groovy/lang/Reference");
mv.visitInsn(SWAP);
mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
} else {
doGroovyCast(type);
if (type == ClassHelper.double_TYPE) {
mv.visitVarInsn(DSTORE, idx);
} else if (type == ClassHelper.float_TYPE) {
mv.visitVarInsn(FSTORE, idx);
} else if (type == ClassHelper.long_TYPE) {
mv.visitVarInsn(LSTORE, idx);
} else if (type == ClassHelper.boolean_TYPE || type == ClassHelper.char_TYPE || type == ClassHelper.byte_TYPE || type == ClassHelper.int_TYPE || type == ClassHelper.short_TYPE) {
mv.visitVarInsn(ISTORE, idx);
} else {
mv.visitVarInsn(ASTORE, idx);
}
}
// remove RHS value from operand stack
remove(1);
}
Aggregations