Summary
Implement the IR builder in Gradient. Convert typed AST to Intermediate Representation for code generation.
Background
File: compiler/ir_builder.gr (~489 lines currently, stubs). Expand to ~2,000 lines with actual IR generation.
Target State
Full IR generation with:
- Expression lowering
- Statement lowering
- Function lowering
- SSA form construction
- Type conversion
Implementation Requirements
Core Functions
-
build_module(typed: TypedModule) -> IrModule
- Lower entire module to IR
- Generate all functions
-
build_function(func: TypedFunction) -> IrFunction
- Lower function to IR
- Create entry block
- Generate parameters
-
build_expr(builder: IrBuilder, expr: TypedExpr) -> (IrBuilder, IrValue)
- Lower expression to IR instructions
- Handle all expression kinds
- Return generated value
-
build_stmt(builder: IrBuilder, stmt: TypedStmt) -> IrBuilder
- Lower statement to IR
- Update builder state
-
create_block(builder: IrBuilder, name: String) -> (IrBuilder, BlockId)
- Create new basic block
- For control flow
IR Instructions to Generate
- Literals:
const_int, const_float, const_bool
- Arithmetic:
add, sub, mul, div
- Memory:
alloca, load, store
- Control:
br, br_cond, ret
- Calls:
call, call_indirect
- Conversions:
trunc, zext, inttoptr
Acceptance Criteria
Part Of
- Epic: Full Self-Hosting with Rust Kernel
- Phase: 6 of 7
- Blocks: Phase 7 (Codegen)
Effort
~5 days, ~1,500 lines of Gradient
Dependencies
Summary
Implement the IR builder in Gradient. Convert typed AST to Intermediate Representation for code generation.
Background
File:
compiler/ir_builder.gr(~489 lines currently, stubs). Expand to ~2,000 lines with actual IR generation.Target State
Full IR generation with:
Implementation Requirements
Core Functions
build_module(typed: TypedModule) -> IrModule
build_function(func: TypedFunction) -> IrFunction
build_expr(builder: IrBuilder, expr: TypedExpr) -> (IrBuilder, IrValue)
build_stmt(builder: IrBuilder, stmt: TypedStmt) -> IrBuilder
create_block(builder: IrBuilder, name: String) -> (IrBuilder, BlockId)
IR Instructions to Generate
const_int,const_float,const_booladd,sub,mul,divalloca,load,storebr,br_cond,retcall,call_indirecttrunc,zext,inttoptrAcceptance Criteria
Part Of
Effort
~5 days, ~1,500 lines of Gradient
Dependencies