Package gnu.bytecode

Class SwitchState


  • public class SwitchState
    extends Object
    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 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 by CodeAttr.startSwitch().
      • addCase

        public boolean addCase​(int value,
                               CodeAttr code)
        Add a new case.
        Parameters:
        value - the case value to match against at run-time
        code - 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 of addCase(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-time
        label - the location to go to if the value matches
        code - 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 an ExitableBlock.
      • 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.