Package org.codehaus.groovy.classgen.asm
Class OperandStack
java.lang.Object
org.codehaus.groovy.classgen.asm.OperandStack
Tracks the JVM operand stack during bytecode generation, maintaining a
parallel list of
ClassNode type descriptors for each slot.
Used to emit correct pop/cast/box instructions and to verify stack discipline.-
Constructor Summary
ConstructorsConstructorDescriptionOperandStack(WriterController controller) Creates an operand stack tracker backed by the given controller. -
Method Summary
Modifier and TypeMethodDescriptionbox()Boxes the top operand if it is a primitive type, emitting the appropriate wrapper-creation bytecode, and returns the resulting (possibly wrapped) type.voidcastToBool(int mark, boolean emptyDefault) ensure last marked parameter on the stack is a primitive boolean if mark==stack size, we assume an empty expression or statement.voidPerforms anas-coercion of the top operand totargetType.voiddoGroovyCast(ClassNode targetType) do Groovy cast for top level elementvoidPerforms a Groovy cast of the top operand to the declared origin type ofv.voiddup()duplicate top elementintReturns the number of tracked entries on the operand stack.Returns the type of the top element on the tracked operand stack without removing it.org.objectweb.asm.Labeljump(int ifIns) Emits a conditional jump instruction and returns the newly created target label.voidjump(int ifIns, org.objectweb.asm.Label label) Emits a conditional jump to the given label.voidEmits a load instruction for the given type from the specified local variable slot and pushes the type onto the tracked stack.voidloadOrStoreVariable(BytecodeVariable variable, boolean useReferenceDirectly) Either loads or stores a local variable depending on the current LHS flag.voidpop()remove operand stack top element using bytecode popvoidpopDownTo(int elements) Pops all stack entries aboveelements, emitting the appropriatePOPorPOP2instruction for each popped value.voidpush operand on stackvoidpushBool(boolean value) Pushes an integer literal (0or1) representing a boolean value and tracks it asbooleanon the operand stack.voidpushConstant(ConstantExpression expression) load the constant on the operand stack.voidpushDynamicName(Expression name) Pushes the name expression as aStringonto the operand stack.voidremove(int amount) Remove amount elements from the operand stack, without using pop.voidreplace top level element with new element of given typevoidreplace n top level elements with new element of given typevoidstoreVar(BytecodeVariable variable) Stores the top operand stack value intovariable, casting to the variable's declared type.voidswap()swap two top level operandstoString()
-
Constructor Details
-
OperandStack
Creates an operand stack tracker backed by the given controller.- Parameters:
controller- the writer controller for the current compilation
-
-
Method Details
-
getStackLength
public int getStackLength()Returns the number of tracked entries on the operand stack. -
popDownTo
public void popDownTo(int elements) Pops all stack entries aboveelements, emitting the appropriatePOPorPOP2instruction for each popped value.- Parameters:
elements- the target stack depth to reduce to
-
castToBool
public void castToBool(int mark, boolean emptyDefault) ensure last marked parameter on the stack is a primitive boolean if mark==stack size, we assume an empty expression or statement. was used and we will use the value given in emptyDefault as boolean if mark==stack.size()-1 the top element will be cast to boolean using Groovy truth. In other cases we throw a GroovyBugError -
pop
public void pop()remove operand stack top element using bytecode pop -
jump
public org.objectweb.asm.Label jump(int ifIns) Emits a conditional jump instruction and returns the newly created target label. The boolean operand is removed from the tracked stack.- Parameters:
ifIns- the branch opcode (e.g.IFEQ,IFNE)- Returns:
- the label that the jump will branch to
-
jump
public void jump(int ifIns, org.objectweb.asm.Label label) Emits a conditional jump to the given label. The boolean operand is removed from the tracked stack.- Parameters:
ifIns- the branch opcode (e.g.IFEQ,IFNE)label- the branch target
-
dup
public void dup()duplicate top element -
box
Boxes the top operand if it is a primitive type, emitting the appropriate wrapper-creation bytecode, and returns the resulting (possibly wrapped) type.- Returns:
- the type of the top operand after boxing
-
remove
public void remove(int amount) Remove amount elements from the operand stack, without using pop. For example after a method invocation -
push
push operand on stack -
swap
public void swap()swap two top level operands -
replace
replace top level element with new element of given type -
replace
replace n top level elements with new element of given type -
doGroovyCast
do Groovy cast for top level element -
doGroovyCast
Performs a Groovy cast of the top operand to the declared origin type ofv.- Parameters:
v- the variable whose origin type is the cast target
-
doAsType
Performs anas-coercion of the top operand totargetType.- Parameters:
targetType- the target type
-
pushConstant
load the constant on the operand stack. -
pushDynamicName
Pushes the name expression as aStringonto the operand stack. Constant string expressions are pushed as LDC literals; other expressions are cast toStringvia the Groovy runtime.- Parameters:
name- the expression representing the method or property name
-
loadOrStoreVariable
Either loads or stores a local variable depending on the current LHS flag. If in LHS context, stores the top of the operand stack intovariable; otherwise loads the variable's value onto the stack.- Parameters:
variable- the bytecode variable to load or storeuseReferenceDirectly- iftrue, loads theReferencewrapper directly rather than unboxing it
-
storeVar
Stores the top operand stack value intovariable, casting to the variable's declared type. Handles both plain variables andReference-wrapped closure-shared variables.- Parameters:
variable- the bytecode variable slot to store into
-
load
Emits a load instruction for the given type from the specified local variable slot and pushes the type onto the tracked stack.- Parameters:
type- the type of the value to loadidx- the local variable slot index
-
pushBool
public void pushBool(boolean value) Pushes an integer literal (0or1) representing a boolean value and tracks it asbooleanon the operand stack.- Parameters:
value- the boolean value to push
-
toString
-
getTopOperand
Returns the type of the top element on the tracked operand stack without removing it.
-