Package gnu.bytecode
Class SwitchState
java.lang.Object
gnu.bytecode.SwitchState
Maintains the state for generating a switch statement or expression.
Simple example
To translate:
you can do:switch (exps) { case 1: exp1; break; case 2: exp2; break; default: expd; }
compile[exps] SwitchState sw = code.startSwitch(); sw.addCase(1, code); compile[exp1]; sw.exitSwitch(code); sw.addCase(2, code); compile[exp2]; sw.exitSwitch(code); sw.addDefault(code); compile[expd]; sw.finish(code);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
Add a new case.boolean
addCaseGoto
(int value, CodeAttr code, Label label) Optimization ofaddCase(value, code); emitGoto(label)
.void
addDefault
(CodeAttr code) void
exitSwitch
(CodeAttr code) Break/exit from this switch.void
Handle the end of the switch statement.int
int
boolean
insertCase
(int value, Label label, CodeAttr code) Internal routine to add a new case.void
switchValuePushed
(CodeAttr code) Needs to be called after the switch value has been pushed.
-
Constructor Details
-
SwitchState
-
-
Method Details
-
getMaxValue
public int getMaxValue() -
getNumCases
public int getNumCases() -
switchValuePushed
Needs to be called after the switch value has been pushed. I.e. in execution order, just before the actual switch instruction. Called implicitly byCodeAttr.startSwitch()
. -
addCase
Add a new case.- Parameters:
value
- the case value to match against at run-timecode
- the CodeAttr of the Method we are generating code for- Returns:
- true on success; false if value duplicates an existing value
-
addCaseGoto
Optimization ofaddCase(value, code); emitGoto(label)
. -
addDefault
-
insertCase
Internal routine to add a new case.- Parameters:
value
- the case value to match against at run-timelabel
- the location to go to if the value matchescode
- the CodeAttr of the Method we are generating code for- Returns:
- true on success; false if value duplicates an existing value
-
exitSwitch
Break/exit from this switch. Doesn't allow exiting through a try - if you need that, use anExitableBlock
. -
finish
Handle the end of the switch statement. Assume the case value is on the stack; go to the matching case label.
-