Class ForRangePolymorphicInstr
java.lang.Object
org.ek9lang.compiler.ir.instructions.IRInstr
org.ek9lang.compiler.ir.instructions.ForRangePolymorphicInstr
- All Implemented Interfaces:
INode
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 <=> endto 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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordDispatch case for ascending loops (direction < 0).static final recordDispatch case for descending loops (direction > 0).static final recordContainer for all three dispatch cases in a for-range loop.static final recordDispatch case for equal loops (direction == 0).static final recordNon-executable metadata about the loop structure.static final recordScope identifiers for different parts of the loop structure. -
Method Summary
Modifier and TypeMethodDescriptionstatic ForRangePolymorphicInstrforRangePolymorphic(List<IRInstr> initializationInstructions, ForRangePolymorphicInstr.DispatchCases dispatchCases, ForRangePolymorphicInstr.LoopMetadata metadata, List<IRInstr> bodyInstructions, ForRangePolymorphicInstr.ScopeMetadata scopeMetadata, DebugInfo debugInfo) Create a polymorphic for-range loop instruction.toString()Methods inherited from class IRInstr
accept, addOperand, addOperands, equals, getDebugInfo, getEscapeMetaData, getOpcode, getOperands, getResult, hasEscapeMetaData, hashCode, hasResult, isControlFlow, isLabel, isMemoryManagement, isMethodCall, setEscapeMetaData
-
Method Details
-
forRangePolymorphic
public static ForRangePolymorphicInstr forRangePolymorphic(List<IRInstr> initializationInstructions, ForRangePolymorphicInstr.DispatchCases dispatchCases, ForRangePolymorphicInstr.LoopMetadata metadata, List<IRInstr> bodyInstructions, ForRangePolymorphicInstr.ScopeMetadata scopeMetadata, DebugInfo debugInfo) Create a polymorphic for-range loop instruction. -
getInitializationInstructions
-
getDispatchCases
-
getMetadata
-
getBodyInstructions
-
getScopeMetadata
-
toString
-