Package org.codehaus.groovy.classgen.asm
Class BinaryExpressionHelper
java.lang.Object
org.codehaus.groovy.classgen.asm.BinaryExpressionHelper
- Direct Known Subclasses:
BinaryExpressionMultiTypeDispatcher,IndyBinHelper
Emits bytecode for dynamic binary expressions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final WriterControllerCoordinates the active bytecode-generation state. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a binary expression helper for the supplied controller. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidassignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader, boolean safe) Emits an array-style assignment using the dynamicputAtprotocol.voideval(BinaryExpression expression) Evaluates the supplied binary expression.protected voidevaluateArrayAssignmentWithOperator(String method, BinaryExpression expression, BinaryExpression leftBinExpr) Evaluates a compound assignment whose left-hand side is a subscript expression.protected voidevaluateBinaryExpression(String message, BinaryExpression expression) Evaluates a dynamic binary operator by invoking the named helper method.protected voidevaluateBinaryExpressionWithAssignment(String method, BinaryExpression expression) Evaluates an operator-assignment expression such as+=.protected voidevaluateCompareExpression(MethodCaller compareMethod, BinaryExpression expression) Evaluates a comparison expression, using primitive helpers when possible.protected voidevaluateCompoundAssign(String assignName, String baseName, BinaryExpression expression) GEP-15: dynamic-mode compound-assign codegen.voidevaluateElvisEqual(BinaryExpression expression) Rewrites and evaluates the Elvis-assignment form.voidevaluateEqual(BinaryExpression expression, boolean defineVariable) Evaluates an assignment expression.voidevaluatePostfixMethod(PostfixExpression expression) Evaluates a postfix increment or decrement expression.voidevaluatePrefixMethod(PrefixExpression expression) Evaluates a prefix increment or decrement expression.voidevaluateTernary(TernaryExpression expression) Evaluates a ternary or Elvis expression.protected voidfinishPreservedRhs(Expression rhs, boolean keepValue, int[] slot, ClassNode[] keptType) Leaves the assignment value on the operand stack whenkeepValueis set, and clears a temporary allocated bypreserveSubscriptRhs(org.codehaus.groovy.ast.expr.Expression, boolean, int[], org.codehaus.groovy.ast.ClassNode[]).protected ExpressionpreserveSubscriptRhs(Expression rhs, boolean keepValue, int[] slot, ClassNode[] keptType) Returns the expression to pass as theputAtvalue.protected voidreleaseTemporary(ClassNode type, int index) Drops a temporary.voidwriteIsCase(int selectorIndex, ClassNode selectorType, Expression caseValue) Loads the stored switch selector, evaluatescaseValue, and invokesScriptBytecodeAdapter.isCase(Object, Object).protected voidwritePostOrPrefixMethod(int op, String method, Expression expression, Expression orig) Emits the method call used to implement a prefix or postfix operation.
-
Field Details
-
controller
Coordinates the active bytecode-generation state.
-
-
Constructor Details
-
BinaryExpressionHelper
Creates a binary expression helper for the supplied controller.- Parameters:
wc- the active writer controller
-
-
Method Details
-
getController
- Returns:
- the active writer controller
-
getIsCaseMethod
- Returns:
- the helper used for
isCasedispatch
-
writeIsCase
Loads the stored switch selector, evaluatescaseValue, and invokesScriptBytecodeAdapter.isCase(Object, Object). Shared by switch statements and switch expressions so both keep the operand stack consistent.- Parameters:
selectorIndex- local slot holding the selectorselectorType- type stored in that slotcaseValue- the case label expression
-
eval
Evaluates the supplied binary expression.- Parameters:
expression- the expression to compile
-
assignToArray
protected void assignToArray(Expression parent, Expression receiver, Expression index, Expression rhsValueLoader, boolean safe) Emits an array-style assignment using the dynamicputAtprotocol. The receiver, the index and the right-hand side are evaluated in that order. When the assignment value is required, it is left on the operand stack. A right-hand side that is not already a slot is kept in one temporary across the call, and that temporary is cleared before this method returns. SeepreserveSubscriptRhs(org.codehaus.groovy.ast.expr.Expression, boolean, int[], org.codehaus.groovy.ast.ClassNode[])for why a stack duplicate is not used here.- Parameters:
parent- the original assignment expressionreceiver- the array or indexable receiverindex- the subscript expressionrhsValueLoader- the right-hand side, or a loader that reloads itsafe- whether the receiver access is null-safe
-
preserveSubscriptRhs
protected Expression preserveSubscriptRhs(Expression rhs, boolean keepValue, int[] slot, ClassNode[] keptType) Returns the expression to pass as theputAtvalue. When the assignment value is required and the right-hand side is not already a slot, the wrapper evaluates it once and stores a temporary.An array store can duplicate that value under its two operands (
OperandStack.dupUnderTwo()). AputAtcall cannot share that duplicate. The classic call site pushes aCallSiteunder the receiver and the index, three words below the value, and there is noDUP_X3. A copy inserted only under the receiver and the index would become theinvokeinterfacereceiver. The invokedynamic backend has no call-site operand, but the copy would have to be emitted inside that backend after the arguments are on the stack. One reference temporary, cleared byfinishPreservedRhs(org.codehaus.groovy.ast.expr.Expression, boolean, int[], org.codehaus.groovy.ast.ClassNode[]), is the path shared by both backends. -
finishPreservedRhs
protected void finishPreservedRhs(Expression rhs, boolean keepValue, int[] slot, ClassNode[] keptType) Leaves the assignment value on the operand stack whenkeepValueis set, and clears a temporary allocated bypreserveSubscriptRhs(org.codehaus.groovy.ast.expr.Expression, boolean, int[], org.codehaus.groovy.ast.ClassNode[]). -
evaluateElvisEqual
Rewrites and evaluates the Elvis-assignment form.- Parameters:
expression- the Elvis-assignment expression
-
releaseTemporary
Drops a temporary. A reference slot is cleared first so the value does not stay reachable afterremoveVar, which only forgets the slot. Primitive slots are not null stores; they do not keep a heap object live. -
evaluateEqual
Evaluates an assignment expression.- Parameters:
expression- the assignment expressiondefineVariable- whether the left-hand side declares a new variable
-
evaluateCompareExpression
Evaluates a comparison expression, using primitive helpers when possible.- Parameters:
compareMethod- the dynamic comparison helperexpression- the comparison expression
-
evaluateBinaryExpression
Evaluates a dynamic binary operator by invoking the named helper method.- Parameters:
message- the operator method nameexpression- the expression to compile
-
evaluateArrayAssignmentWithOperator
protected void evaluateArrayAssignmentWithOperator(String method, BinaryExpression expression, BinaryExpression leftBinExpr) Evaluates a compound assignment whose left-hand side is a subscript expression.- Parameters:
method- the operator method nameexpression- the compound assignment expressionleftBinExpr- the indexed left-hand side
-
evaluateBinaryExpressionWithAssignment
Evaluates an operator-assignment expression such as+=.- Parameters:
method- the operator method nameexpression- the assignment expression
-
evaluateCompoundAssign
protected void evaluateCompoundAssign(String assignName, String baseName, BinaryExpression expression) GEP-15: dynamic-mode compound-assign codegen. Routes throughScriptBytecodeAdapter.compoundAssign(Object, Object, String, String)which dispatches toassignNamewhen the receiver responds to it, and falls back tobaseNameotherwise. The caller stores the helper's return value into the LHS — for the in-place branch this is a no-op store of the receiver back to itself; for the fallback branch it is the usual "x = x.op(y)" assignment. -
evaluatePostfixMethod
Evaluates a postfix increment or decrement expression.- Parameters:
expression- the postfix expression
-
evaluatePrefixMethod
Evaluates a prefix increment or decrement expression.- Parameters:
expression- the prefix expression
-
writePostOrPrefixMethod
protected void writePostOrPrefixMethod(int op, String method, Expression expression, Expression orig) Emits the method call used to implement a prefix or postfix operation.- Parameters:
op- the token typemethod- the helper method nameexpression- the receiver expressionorig- the original prefix/postfix expression
-
evaluateTernary
Evaluates a ternary or Elvis expression.- Parameters:
expression- the expression to compile
-