Class NullCoalescingOperatorAsmGenerator


final class NullCoalescingOperatorAsmGenerator extends AbstractControlFlowAsmGenerator
Specialized generator for EK9 null coalescing operator (??). Handles CONTROL_FLOW_CHAIN with chain_type: "NULL_COALESCING_OPERATOR".

EK9 Null Coalescing Operator Pattern: lhs ?? rhs

  • If LHS is null → returns RHS
  • If LHS is not null → returns LHS (regardless of set/unset state)

Key Difference from Elvis Operator (:?):

  • ?? checks ONLY null (IS_NULL instruction)
  • :? checks BOTH null AND set (IS_NULL + IS_SET instructions)

IR Structure:

CONTROL_FLOW_CHAIN [chain_type: "NULL_COALESCING_OPERATOR"]
  condition_chain:
    [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
  default_body_evaluation: [LOAD lhs]  // Returns LHS if not null
  default_result: lhs

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 null coalescing operator (??).

      Bytecode Pattern:

      // Evaluate condition: LOAD lhs, IS_NULL
      ALOAD lhs_index
      IFNONNULL not_null_label
      
      // NULL case: load RHS
      ALOAD rhs_index
      ASTORE result_index
      GOTO end_label
      
      not_null_label:
      // NOT NULL case: load LHS
      ALOAD lhs_index
      ASTORE result_index
      
      end_label:
      // result now holds correct value
      

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

      Parameters:
      instr - CONTROL_FLOW_CHAIN instruction with NULL_COALESCING_OPERATOR type