use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy-core by groovy.
the class BinaryExpressionHelper method evaluateLogicalAndExpression.
private void evaluateLogicalAndExpression(BinaryExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
expression.getLeftExpression().visit(acg);
operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
Label falseCase = operandStack.jump(IFEQ);
expression.getRightExpression().visit(acg);
operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
operandStack.jump(IFEQ, falseCase);
ConstantExpression.PRIM_TRUE.visit(acg);
Label trueCase = new Label();
mv.visitJumpInsn(GOTO, trueCase);
mv.visitLabel(falseCase);
ConstantExpression.PRIM_FALSE.visit(acg);
mv.visitLabel(trueCase);
// have to remove 1 because of the GOTO
operandStack.remove(1);
}
use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy-core by groovy.
the class BinaryExpressionHelper method evaluateLogicalOrExpression.
private void evaluateLogicalOrExpression(BinaryExpression expression) {
MethodVisitor mv = controller.getMethodVisitor();
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
Label end = new Label();
expression.getLeftExpression().visit(acg);
operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
Label trueCase = operandStack.jump(IFNE);
expression.getRightExpression().visit(acg);
operandStack.doGroovyCast(ClassHelper.boolean_TYPE);
Label falseCase = operandStack.jump(IFEQ);
mv.visitLabel(trueCase);
ConstantExpression.PRIM_TRUE.visit(acg);
operandStack.jump(GOTO, end);
mv.visitLabel(falseCase);
ConstantExpression.PRIM_FALSE.visit(acg);
mv.visitLabel(end);
}
use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy-core by groovy.
the class BinaryExpressionHelper method evaluateArrayAssignmentWithOperator.
protected void evaluateArrayAssignmentWithOperator(String method, BinaryExpression expression, BinaryExpression leftBinExpr) {
CompileStack compileStack = getController().getCompileStack();
AsmClassGenerator acg = getController().getAcg();
OperandStack os = getController().getOperandStack();
// e.g. x[a] += b
// to avoid loading x and a twice we transform the expression to use
// ExpressionAsVariableSlot
// -> subscript=a, receiver=x, receiver[subscript]+b, =, receiver[subscript]
// -> subscript=a, receiver=x, receiver#getAt(subscript)#plus(b), =, receiver#putAt(subscript)
// -> subscript=a, receiver=x, receiver#putAt(subscript, receiver#getAt(subscript)#plus(b))
// the result of x[a] += b is x[a]+b, thus:
// -> subscript=a, receiver=x, receiver#putAt(subscript, ret=receiver#getAt(subscript)#plus(b)), ret
ExpressionAsVariableSlot subscript = new ExpressionAsVariableSlot(controller, leftBinExpr.getRightExpression(), "subscript");
ExpressionAsVariableSlot receiver = new ExpressionAsVariableSlot(controller, leftBinExpr.getLeftExpression(), "receiver");
MethodCallExpression getAt = new MethodCallExpression(receiver, "getAt", new ArgumentListExpression(subscript));
MethodCallExpression operation = new MethodCallExpression(getAt, method, expression.getRightExpression());
ExpressionAsVariableSlot ret = new ExpressionAsVariableSlot(controller, operation, "ret");
MethodCallExpression putAt = new MethodCallExpression(receiver, "putAt", new ArgumentListExpression(subscript, ret));
putAt.visit(acg);
os.pop();
os.load(ret.getType(), ret.getIndex());
compileStack.removeVar(ret.getIndex());
compileStack.removeVar(subscript.getIndex());
compileStack.removeVar(receiver.getIndex());
}
use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy-core by groovy.
the class BinaryExpressionMultiTypeDispatcher method doPrimitiveCompare.
protected boolean doPrimitiveCompare(ClassNode leftType, ClassNode rightType, BinaryExpression binExp) {
Expression leftExp = binExp.getLeftExpression();
Expression rightExp = binExp.getRightExpression();
int operation = binExp.getOperation().getType();
int operationType = getOperandConversionType(leftType, rightType);
BinaryExpressionWriter bew = binExpWriter[operationType];
if (!bew.write(operation, true))
return false;
AsmClassGenerator acg = getController().getAcg();
OperandStack os = getController().getOperandStack();
leftExp.visit(acg);
os.doGroovyCast(bew.getNormalOpResultType());
rightExp.visit(acg);
os.doGroovyCast(bew.getNormalOpResultType());
bew.write(operation, false);
return true;
}
use of org.codehaus.groovy.classgen.AsmClassGenerator in project groovy-core by groovy.
the class ClosureWriter method writeClosure.
public void writeClosure(ClosureExpression expression) {
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
ClassNode classNode = controller.getClassNode();
AsmClassGenerator acg = controller.getAcg();
ClassNode closureClass = getOrAddClosureClass(expression, 0);
String closureClassinternalName = BytecodeHelper.getClassInternalName(closureClass);
List constructors = closureClass.getDeclaredConstructors();
ConstructorNode node = (ConstructorNode) constructors.get(0);
Parameter[] localVariableParams = node.getParameters();
mv.visitTypeInsn(NEW, closureClassinternalName);
mv.visitInsn(DUP);
if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall()) {
(new ClassExpression(classNode)).visit(acg);
(new ClassExpression(controller.getOutermostClass())).visit(acg);
} else {
mv.visitVarInsn(ALOAD, 0);
controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
loadThis();
}
// on the stack
for (int i = 2; i < localVariableParams.length; i++) {
Parameter param = localVariableParams[i];
String name = param.getName();
loadReference(name, controller);
if (param.getNodeMetaData(ClosureWriter.UseExistingReference.class) == null) {
param.setNodeMetaData(ClosureWriter.UseExistingReference.class, Boolean.TRUE);
}
}
// we may need to pass in some other constructors
//cv.visitMethodInsn(INVOKESPECIAL, innerClassinternalName, "<init>", prototype + ")V");
mv.visitMethodInsn(INVOKESPECIAL, closureClassinternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, localVariableParams), false);
controller.getOperandStack().replace(ClassHelper.CLOSURE_TYPE, localVariableParams.length);
}
Aggregations