l0_backend_lowering.py
Module: l0_backend_lowering
Source: compiler/stage1_py/l0_backend_lowering.py Language: Python
Symbols
Namespace l0_backend_lowering
Class l0_backend_lowering::Lowering
Mutually recursive expression, statement, control-flow, and scheduled cleanup lowering.
Member Data l0_backend_lowering.Lowering.convert
1
OwnershipConversion convert
Member Data l0_backend_lowering.Lowering.lifetime
1
ValueLifetime lifetime
Member Data l0_backend_lowering.Lowering.state
1
BackendState state
Function l0_backend_lowering.Lowering._emit_cleanup_for_return
1
None l0_backend_lowering.Lowering._emit_cleanup_for_return(self, str|None returned_var=None)
Emit cleanup logic for a return statement.
Walks up scope chain, executes any with-statement cleanup data, then cleans ALL owned variables (except return value). The with-cleanup runs first because user cleanup code may reference variables whose owned resources (e.g. string refcounts) are released by the automatic owned-var cleanup.
Parameters:
returned_var: Mangled name of variable being returned (to skip cleanup).Function
l0_backend_lowering.Lowering._emit_cleanup_for_loop_exit
1
None l0_backend_lowering.Lowering._emit_cleanup_for_loop_exit(self, *bool is_break)
Emit cleanup for break/continue.
Walks from current scope up to and including the innermost loop cleanup target, executing any with-statement cleanup data along the way. The with-cleanup runs before owned-var cleanup (see _emit_cleanup_for_return for rationale).
Parameters:
is_break: True if cleaning for ‘break’, False for ‘continue’.Function
l0_backend_lowering.Lowering._emit_with_cleanup_from_scope
1
None l0_backend_lowering.Lowering._emit_with_cleanup_from_scope(self, ScopeContext scope, str module_name)
Emit with-statement cleanup for a scope.
Parameters:
scope: The scope containing cleanup logic.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_block_sequence
1
None l0_backend_lowering.Lowering._emit_block_sequence(self, Block block, str module_name)
Emit statements in a block.
Parameters:
block: The Block AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_stmt
1
None l0_backend_lowering.Lowering._emit_stmt(self, Stmt stmt, str module_name)
Emit a single statement.
Parameters:
stmt: The Stmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_block
1
Any l0_backend_lowering.Lowering._emit_block(self, Block stmt, str module_name)
Emit a block statement with its own scope.
Parameters:
stmt: The Block AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_return
1
Any l0_backend_lowering.Lowering._emit_return(self, ReturnStmt stmt, Callable[[], None]|None before_cleanup=None)
Emit a return statement with cleanup.
Parameters:
stmt: The ReturnStmt AST node.before_cleanup: Optional hook to run after the return value is evaluated and before scope cleanup is emitted.Function
l0_backend_lowering.Lowering._register_inline_with_cleanup
1
None l0_backend_lowering.Lowering._register_inline_with_cleanup(self, ScopeContext scope, WithItem item)
Register one inline with-item cleanup in LIFO order.
Function l0_backend_lowering.Lowering._emit_inline_with_header_item
1
None l0_backend_lowering.Lowering._emit_inline_with_header_item(self, WithItem item, str module_name, ScopeContext scope)
Emit one inline with header item and register its cleanup at the committed point.
Function l0_backend_lowering.Lowering._emit_condition_branch
1
None l0_backend_lowering.Lowering._emit_condition_branch(self, Expr expr, str true_label, str false_label)
Emit control flow for one condition expression with short-circuit semantics.
This path is used only for statement conditions so ARC temps emitted by expression lowering stay inside the correct structural block instead of being hoisted into an enclosing “if (…)” or “while (…)” header.
Parameters:
expr: Condition expression to lower.true_label: Jump target when the condition is true.false_label: Jump target when the condition is false.Function
l0_backend_lowering.Lowering._emit_condition_expr
1
str l0_backend_lowering.Lowering._emit_condition_expr(self, Expr expr)
Emit a top-level condition expression for direct statement headers.
Function l0_backend_lowering.Lowering._emit_condition_value
1
str l0_backend_lowering.Lowering._emit_condition_value(self, Expr expr)
Evaluate a statement condition into a stable boolean temporary.
The returned temp is safe to reference from an if/while header because any ARC temps created while evaluating the condition are scoped to the emitted condition block and cleaned before control continues.
Parameters:
expr: Condition expression to lower.
Returns: Name of the generated boolean temp.
Function l0_backend_lowering.Lowering._emit_while
1
Any l0_backend_lowering.Lowering._emit_while(self, WhileStmt stmt, str module_name)
Emit a while loop.
Parameters:
stmt: The WhileStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_for
1
Any l0_backend_lowering.Lowering._emit_for(self, ForStmt stmt, str module_name)
Emit a for loop.
Parameters:
stmt: The ForStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_if_else
1
Any l0_backend_lowering.Lowering._emit_if_else(self, IfStmt stmt, str module_name)
Emit an if-else statement.
Parameters:
stmt: The IfStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._gen_if_else_branch
1
bool l0_backend_lowering.Lowering._gen_if_else_branch(self, Stmt stmt, str module_name)
Emit a branch of an if/else.
Parameters:
stmt: The statement in the branch.module_name: Name of current module.
Returns: True if branch is unreachable at end.
Function l0_backend_lowering.Lowering._emit_reassignment
1
None l0_backend_lowering.Lowering._emit_reassignment(self, AssignStmt stmt)
Emit an assignment statement.
Parameters:
1
str l0_backend_lowering.Lowering._emit_lvalue_with_caching(self, Expr target)
Emit an lvalue expression, caching sub-expressions with side effects.
For targets like * (func_call()), the pointer expression func_call() must be evaluated exactly once, not multiple times during release/assign/retain.
Parameters:
target: The lvalue expression.
Returns: A C lvalue expression string.
Function l0_backend_lowering.Lowering._emit_let
1
Any l0_backend_lowering.Lowering._emit_let(self, LetStmt stmt, str module_name)
Emit a local ‘let’ declaration.
Parameters:
stmt: The LetStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_with_cleanup_header_let_predecl
1
Type | None l0_backend_lowering.Lowering._emit_with_cleanup_header_let_predecl(self, LetStmt stmt, str module_name)
Predeclare a nullable with-header let for cleanup-block form.
Nullable lets are predeclared as null so cleanup code can reference them on header ? failure paths.
Non-nullable lets use the normal declaration+initializer path and return None here.
Parameters:
stmt: The LetStmt AST node.module_name: Name of current module.
Returns: The Type if it was a nullable let, otherwise None.
Function l0_backend_lowering.Lowering._emit_with_cleanup_header_let_assign
1
None l0_backend_lowering.Lowering._emit_with_cleanup_header_let_assign(self, LetStmt stmt, Type var_ty)
Emit initializer assignment for a predeclared cleanup-block let.
Parameters:
stmt: The LetStmt AST node.var_ty: The resolved type of the let.Function
l0_backend_lowering.Lowering._emit_match
1
None l0_backend_lowering.Lowering._emit_match(self, MatchStmt stmt, str module_name)
Emit a match statement as a switch on the tag field.
Parameters:
stmt: The MatchStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_case
1
None l0_backend_lowering.Lowering._emit_case(self, CaseStmt stmt, str module_name)
Emit a case statement as a scalar switch or string if/else chain.
Parameters:
stmt: The CaseStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._emit_case_literal
1
str l0_backend_lowering.Lowering._emit_case_literal(self, Expr expr)
Emit a constant literal for a case statement.
Parameters:
expr: The literal expression.
Returns: A C constant string.
Function l0_backend_lowering.Lowering._emit_pattern_bindings
1
None l0_backend_lowering.Lowering._emit_pattern_bindings(self, VariantPattern pattern, EnumType enum_type, ScopeContext arm_scope)
Emit pattern variable bindings and add them to arm scope.
Parameters:
pattern: The variant pattern.enum_type: The enum type.arm_scope: The scope for the match arm.Function
l0_backend_lowering.Lowering._emit_with
1
None l0_backend_lowering.Lowering._emit_with(self, WithStmt stmt, str module_name)
Emit a with statement.
Inline => form (LIFO cleanup): Emit init statements, then body, then cleanup statements in reverse order.
Cleanup block form: Emit init statements, then body, then cleanup block statements.
Cleanup is emitted at block end and before every early exit (return, break, continue). The scope stores cleanup data so _emit_cleanup_for_return and _emit_cleanup_for_loop_exit can emit it before leaving.
The body and cleanup block are each emitted as real nested C blocks so that any declarations inside them do not collide with the header scope (e.g., legal L0 shadowing like “let x” in both the header and body).
Parameters:
stmt: The WithStmt AST node.module_name: Name of current module.Function
l0_backend_lowering.Lowering._try_emit_intrinsic
1
str | None l0_backend_lowering.Lowering._try_emit_intrinsic(self, CallExpr expr)
Expand compiler intrinsics inline.
Parameters:
expr: The call expression to check.
Returns: C code string if it is an intrinsic, otherwise None.
Function l0_backend_lowering.Lowering._emit_ord_intrinsic
1
str l0_backend_lowering.Lowering._emit_ord_intrinsic(self, CallExpr expr)
Emit ord(enum_value) intrinsic.
Returns 0-based ordinal of enum variant.
Parameters:
expr: The ord call expression.
Returns: A C expression string for the ordinal value.
Function l0_backend_lowering.Lowering._try_emit_constructor
1
str | None l0_backend_lowering.Lowering._try_emit_constructor(self, CallExpr expr)
Check if expr is a constructor call and emit appropriate initialization.
Struct: Point(1, 2) -> { .x = 1, .y = 2 } Enum: Int(42) -> { .tag = Expr_Int, .data = { .Int = { .value = 42 } } }
Parameters:
expr: The call expression to check.
Returns: C code string if this is a constructor, otherwise None.
Function l0_backend_lowering.Lowering._emit_struct_constructor
1
str l0_backend_lowering.Lowering._emit_struct_constructor(self, CallExpr expr, StructType struct_type)
Emit struct constructor as C designated initializer.
Point(1, 2) -> (struct l0_modulename_Point){ .x = 1, .y = 2 }
Parameters:
expr: The constructor call expression.struct_type: The struct type.
Returns: A C struct initializer expression string.
Function l0_backend_lowering.Lowering._emit_variant_constructor
1
str l0_backend_lowering.Lowering._emit_variant_constructor(self, CallExpr expr, EnumType enum_type)
Emit enum variant constructor as C tagged union initializer.
Example: Int(42) -> (struct l0_modulename_Int){ .tag = l0_modulename_Int_Int, .data.Int.value = 42 }
Parameters:
expr: The variant call expression.enum_type: The enum type.
Returns: A C variant initializer expression string.
Function l0_backend_lowering.Lowering._emit_new_expr
1
str l0_backend_lowering.Lowering._emit_new_expr(self, NewExpr expr)
Emit a heap allocation new expression.
Parameters:
expr: The NewExpr AST node.
Returns: A C expression string for the newly allocated pointer.
Function l0_backend_lowering.Lowering._emit_expr_with_expected_type
1
str l0_backend_lowering.Lowering._emit_expr_with_expected_type(self, Expr e, Type expected)
Emit expression with implicit type conversion to expected type.
Parameters:
e: The expression to emit.expected: The expected type.
Returns: A C expression string.
Function l0_backend_lowering.Lowering._emit_owned_expr_with_expected_type
1
str l0_backend_lowering.Lowering._emit_owned_expr_with_expected_type(self, Expr e, Type expected)
Emit expression for contexts that create a new owner.
This applies retain-on-copy when a place expression is copied into an owned destination, while delegating regular type conversion to _emit_expr_with_expected_type .
Parameters:
e: The expression to emit.expected: The expected type.
Returns: A C expression string.
Function l0_backend_lowering.Lowering._emit_expr
1
str l0_backend_lowering.Lowering._emit_expr(self, Expr expr, *bool is_statement=False)
Emit an expression and return the C code.
Parameters:
expr: The expression to emit.is_statement: True if the expression is used as a statement.
Returns: A C expression string, or empty string if used as a statement and no-op.
Function l0_backend_lowering.Lowering._emit_binary_op
1
str l0_backend_lowering.Lowering._emit_binary_op(self, Expr expr_node, str expr_op, Expr expr_left, Expr expr_right, *bool for_condition=False)
Emit code for a binary operation.
Parameters:
expr_node: The BinaryOp AST node.expr_op: The operator string.expr_left: The left operand.expr_right: The right operand.for_condition: Whether to preserve condition-context code generation.
Returns: C code string for the operation.