Package gnu.bytecode
Class SwitchState
- java.lang.Object
-
- gnu.bytecode.SwitchState
-
public class SwitchState extends Object
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 Constructor Description SwitchState(CodeAttr code)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddCase(int value, CodeAttr code)Add 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.voidfinish(CodeAttr code)Handle the end of the switch statement.intgetMaxValue()intgetNumCases()booleaninsertCase(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 Detail
-
SwitchState
public SwitchState(CodeAttr code)
-
-
Method Detail
-
getMaxValue
public int getMaxValue()
-
getNumCases
public int getNumCases()
-
switchValuePushed
public void switchValuePushed(CodeAttr code)
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
public boolean addCase(int value, CodeAttr code)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
public boolean addCaseGoto(int value, CodeAttr code, Label label)Optimization ofaddCase(value, code); emitGoto(label).
-
addDefault
public void addDefault(CodeAttr code)
-
insertCase
public boolean insertCase(int value, Label label, CodeAttr code)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
public void exitSwitch(CodeAttr code)
Break/exit from this switch. Doesn't allow exiting through a try - if you need that, use anExitableBlock.
-
finish
public void finish(CodeAttr code)
Handle the end of the switch statement. Assume the case value is on the stack; go to the matching case label.
-
-