This commit is contained in:
agra
2026-02-26 14:46:21 +02:00
parent dd14f1206b
commit 2552882ce6
14 changed files with 6433 additions and 159 deletions

View File

@@ -25,6 +25,7 @@ pub const Compilation = struct {
import_sources: std.StringHashMap([:0]const u8),
sema_result: ?sema.SemaResult = null,
cg: ?codegen.CodeGen = null,
ir_emitter: ?ir.LLVMEmitter = null,
pub fn init(allocator: std.mem.Allocator, io: std.Io, file_path: []const u8, source: [:0]const u8, target_config: TargetConfig) Compilation {
return .{
@@ -39,6 +40,7 @@ pub const Compilation = struct {
}
pub fn deinit(self: *Compilation) void {
if (self.ir_emitter) |*e| e.deinit();
if (self.cg) |*cg| cg.deinit();
self.diagnostics.deinit();
}
@@ -106,6 +108,19 @@ pub const Compilation = struct {
self.cg = cg;
}
/// Generate code via the IR pipeline: lower AST → IR → LLVM.
pub fn generateCodeViaIR(self: *Compilation) !void {
// Heap-allocate the IR module so its address is stable during emit
const ir_mod_ptr = try self.allocator.create(ir.Module);
ir_mod_ptr.* = self.lowerToIR();
var emitter = ir.LLVMEmitter.init(self.allocator, ir_mod_ptr, "sx_module", self.target_config);
emitter.emit();
// IR module is no longer needed after LLVM IR has been generated
ir_mod_ptr.deinit();
self.allocator.destroy(ir_mod_ptr);
self.ir_emitter = emitter;
}
/// Collect C import source info from the resolved AST.
/// Called after generateCode() to compile C sources natively (not merged into LLVM module).
pub fn collectCImportSources(self: *Compilation) ![]c_import.CImportInfo {
@@ -118,6 +133,8 @@ pub const Compilation = struct {
const root = self.resolved_root orelse self.root orelse return ir.Module.init(self.allocator);
var module = ir.Module.init(self.allocator);
var lowering = ir.Lowering.init(&module);
lowering.main_file = self.file_path;
lowering.resolved_root = root;
lowering.lowerRoot(root);
return module;
}