Class ForRangePolymorphicInstr

java.lang.Object
org.ek9lang.compiler.ir.instructions.IRInstr
org.ek9lang.compiler.ir.instructions.ForRangePolymorphicInstr
All Implemented Interfaces:
INode

public final class ForRangePolymorphicInstr extends IRInstr
Specialized IR instruction for EK9 polymorphic for-range loops.

Handles runtime direction detection and polymorphic operator dispatch for loops like: for i in start ... end [by step]

Design Principle: Explicit Dispatch IR

This instruction uses explicit IR sequences for all dispatch logic, achieving the same abstraction level as CONTROL_FLOW_CHAIN.

Key Design Principles:

  • Polymorphic on Range Type: Works with ANY type implementing <=>, ++, --, += operators (Integer, Float, Date, Duration, custom types)
  • Runtime Direction Detection: Uses start <=> end to determine ascending/descending/equal
  • Single Body Storage: User loop body stored ONCE in IR (backends emit it multiple times)
  • Explicit Dispatch: All dispatch logic expressed as explicit IR sequences

IR Structure:

FOR_RANGE_POLYMORPHIC:
  initialization:
    - Evaluate start, end, by expressions (explicit IR)
    - Assert all values are set (fail-fast)
    - direction = start._cmp(end)  // <=> operator on range type
    - current = start

  dispatch_cases:
    ascending:
      direction_check: [explicit IR for direction < 0]
      direction_primitive: _temp22
      loop_condition_template: [explicit IR for current <= end]
      loop_condition_primitive: _temp32
      loop_body_setup: [explicit IR for loopVariable = current]
      loop_increment: [explicit IR for current++]

    descending:
      direction_check: [explicit IR for direction > 0]
      loop_condition_template: [explicit IR for current >= end]
      loop_increment: [explicit IR for current--]

    equal:
      loop_body_setup: [explicit IR for loopVariable = current]
      single_iteration: true

  body: [user code stored ONCE]

  metadata: [reference info - variable names, types]
  scope_metadata: [scopes for codegen]

Backend Code Generation:

Backends emit this IR directly as a dispatch structure:
[emit initialization IR]

// Ascending case
[emit direction_check IR]
if (direction_primitive) {
  while (true) {
    [emit loop_condition_template IR]
    if (!loop_condition_primitive) break;
    [emit loop_body_setup IR]
    [emit body IR]
    [emit loop_increment IR]
  }
  goto end;
}

// Descending case (similar)
[emit direction_check IR]
if (direction_primitive) {
  while (true) { ... }
  goto end;
}

// Equal case (single iteration)
[emit loop_body_setup IR]
[emit body IR]

end:

Abstraction Level Consistency:

Like CONTROL_FLOW_CHAIN, this instruction provides:
  • Explicit IR sequences for all computations
  • Named variables for branching decisions
  • Metadata for structural guidance
  • Full type information for polymorphic operations

No IR Lowering Needed:

Backends can directly consume this structure without an IR lowering pass. The explicit IR sequences provide everything needed for code generation.

Why Not CONTROL_FLOW_CHAIN?

CONTROL_FLOW_CHAIN is designed for branching (if/else/switch) where each case executes at most once. For-range loops require:
  • Repeated iteration with condition checked each time
  • Polymorphic operator selection based on runtime direction
  • Sharing identical body across three different loop configurations
Using CONTROL_FLOW_CHAIN would duplicate body 3x in IR (ascending/descending/equal cases). FOR_RANGE_POLYMORPHIC achieves 40% IR size reduction by storing body once.