llvm: unify lowering via Resolver and Cursor; remove non-sealed PHI wiring; apply Resolver to extern/call/boxcall/arrays/maps/mem; add llvmlite harness docs; add LLVM layer overview; add LoopForm preheader

This commit is contained in:
Selfhosting Dev
2025-09-12 20:40:48 +09:00
parent d5af6b1d48
commit 38aea59fc1
20 changed files with 986 additions and 79 deletions

View File

@ -0,0 +1,67 @@
"""
Const instruction lowering
Handles integer, float, string, and void constants
"""
import llvmlite.ir as ir
from typing import Dict, Any
def lower_const(
builder: ir.IRBuilder,
module: ir.Module,
dst: int,
value: Dict[str, Any],
vmap: Dict[int, ir.Value]
) -> None:
"""
Lower MIR Const instruction
Args:
builder: Current LLVM IR builder
module: LLVM module
dst: Destination value ID
value: Const value dict with 'type' and 'value' fields
vmap: Value map (value_id -> llvm value)
"""
const_type = value.get('type', 'void')
const_val = value.get('value')
if const_type == 'i64':
# Integer constant
i64 = ir.IntType(64)
llvm_val = ir.Constant(i64, int(const_val))
vmap[dst] = llvm_val
elif const_type == 'f64':
# Float constant
f64 = ir.DoubleType()
llvm_val = ir.Constant(f64, float(const_val))
vmap[dst] = llvm_val
elif const_type == 'string':
# String constant - create global and get pointer
i8 = ir.IntType(8)
str_val = str(const_val)
str_const = ir.Constant.literal_string(str_val.encode('utf-8') + b'\0')
# Create global string constant
global_name = f".str.{dst}"
global_str = ir.GlobalVariable(module, str_const.type, name=global_name)
global_str.initializer = str_const
global_str.linkage = 'private'
global_str.global_constant = True
# Get pointer to first element
indices = [ir.Constant(ir.IntType(32), 0), ir.Constant(ir.IntType(32), 0)]
ptr = builder.gep(global_str, indices, name=f"str_ptr_{dst}")
vmap[dst] = ptr
elif const_type == 'void':
# Void/null constant - use i64 zero
i64 = ir.IntType(64)
vmap[dst] = ir.Constant(i64, 0)
else:
# Unknown type - default to i64 zero
i64 = ir.IntType(64)
vmap[dst] = ir.Constant(i64, 0)