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:
switch (exps) {
case 1: exp1; break;
case 2: exp2; break;
default: expd;
}
you can do:
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 TypeMethodDescriptionbooleanAdd a new case.booleanaddCaseGoto(int value, CodeAttr code, Label label) Optimization ofaddCase(value, code); emitGoto(label).voidaddDefault(CodeAttr code) voidexitSwitch(CodeAttr code) Break/exit from this switch.voidHandle the end of the switch statement.intintbooleaninsertCase(int value, Label label, CodeAttr code) Internal routine to add a new case.voidswitchValuePushed(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.
-