Class ElvisCoalescingOperatorAsmGenerator


final class ElvisCoalescingOperatorAsmGenerator extends AbstractControlFlowAsmGenerator
Specialized generator for EK9 Elvis coalescing operator (:?). Handles CONTROL_FLOW_CHAIN with chain_type: "ELVIS_COALESCING_OPERATOR".

EK9 Elvis Coalescing Operator Pattern: lhs :? rhs

  • If LHS is null → returns RHS
  • If LHS is not null BUT unset → returns RHS
  • If LHS is not null AND set → returns LHS

Key Difference from Null Coalescing (??):

  • ?? checks ONLY null (IS_NULL instruction) - 1 case
  • :? checks BOTH null AND set (IS_NULL + IS_SET instructions) - 2 cases

IR Structure:

CONTROL_FLOW_CHAIN [chain_type: "ELVIS_COALESCING_OPERATOR"]
  condition_chain:
    [case 1: case_type: "NULL_CHECK"]
      condition_evaluation: [LOAD lhs, IS_NULL lhs]
      primitive_condition: temp_is_null
      body_evaluation: [LOAD rhs]  // Returns RHS if null
      body_result: rhs
    [case 2: case_type: "EXPRESSION"]
      condition_evaluation: [CALL lhs._isSet(), CALL result._false()]
      condition_result: isSetResult (EK9 Boolean)
      primitive_condition: invertedPrimitive (NOT set)
      body_evaluation: [LOAD rhs]  // Returns RHS if unset
      body_result: rhs
  default_body_evaluation: []  // LHS already evaluated
  default_result: lhs

Safety: NULL_CHECK always precedes _isSet() call to prevent accessing unallocated memory.

Stack Frame Invariant: All paths arrive at 'end' label with empty stack. Result is stored in local variable (overall_result).

  • Constructor Details

  • Method Details

    • generate

      public void generate(ControlFlowChainInstr instr)
      Generate bytecode for Elvis coalescing operator (:?).

      Uses the common coalescing pattern from AbstractControlFlowAsmGenerator.

      Stack Frame Invariant: Pre-condition: stack is empty Post-condition: stack is empty (result in local variable)

      Parameters:
      instr - CONTROL_FLOW_CHAIN instruction with ELVIS_COALESCING_OPERATOR type