Class SwitchAsmGenerator


final class SwitchAsmGenerator extends AbstractControlFlowAsmGenerator
Specialized generator for EK9 switch statements. Handles CONTROL_FLOW_CHAIN with chain_type: "SWITCH".

EK9 Switch Semantics: - Sequential condition evaluation (if/else-if pattern) - Supports rich operators: equality, contains, matches, comparison - Works with Integer, String, Float, Character types - Multiple case literals per case - Optional default clause

Bytecode Pattern (Sequential Evaluation):

; Evaluation variable setup (switch expression)
[evaluation variable instructions]

; Case 1
[condition evaluation]
iload primitive_condition
ifeq case_2                    ; Branch if false
[case 1 body]
goto end                       ; Exit switch

case_2:
[condition evaluation]
iload primitive_condition
ifeq default_label             ; Branch if false
[case 2 body]
goto end

default_label:
[default body]                 ; Fall through to end

end:
; All paths converge here

Stack Frame Invariant: All paths arrive at 'end' label with empty stack. Statement form has no result variable (unlike expression form which would).

Design Note: EK9 switch uses sequential evaluation rather than JVM tableswitch/lookupswitch because it supports rich operator semantics (contains, matches) and multiple types (String, Float) that don't map to JVM switch instructions. This provides maximum flexibility while maintaining correct semantics.

  • Constructor Details

  • Method Details

    • generate

      public void generate(ControlFlowChainInstr instr)
      Generate bytecode for switch statement.

      Control Flow:

      1. Process evaluation variable setup (switch expression)
      2. For each case: evaluate condition, branch if false, execute body, jump to end
      3. Process default case (if present)
      4. Place end label where all paths converge

      Stack Frame Invariant: Pre-condition: stack is empty Post-condition: stack is empty

      Parameters:
      instr - CONTROL_FLOW_CHAIN instruction with SWITCH type