ir done'ish

This commit is contained in:
agra
2026-03-01 22:38:41 +02:00
parent 6a920dbd2c
commit f763765ea2
17 changed files with 1443 additions and 15017 deletions

View File

@@ -3,13 +3,13 @@ const ast = @import("ast.zig");
const parser = @import("parser.zig");
const imports = @import("imports.zig");
const sema = @import("sema.zig");
const codegen = @import("codegen.zig");
const errors = @import("errors.zig");
const c_import = @import("c_import.zig");
const ir = @import("ir/ir.zig");
const target_mod = @import("target.zig");
const Node = ast.Node;
pub const TargetConfig = codegen.TargetConfig;
pub const TargetConfig = target_mod.TargetConfig;
pub const Compilation = struct {
allocator: std.mem.Allocator,
@@ -24,7 +24,6 @@ pub const Compilation = struct {
resolved_root: ?*Node = null,
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 {
@@ -41,7 +40,6 @@ 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();
}
@@ -95,21 +93,8 @@ pub const Compilation = struct {
}
}
pub fn generateCode(self: *Compilation) !void {
const root = self.resolved_root orelse self.root orelse return error.CompileError;
var cg = codegen.CodeGen.init(self.allocator, "sx_module", self.target_config);
cg.diagnostics = &self.diagnostics;
cg.import_sources = &self.import_sources;
if (self.sema_result) |*sr| {
cg.sema_result = sr;
}
cg.generate(root) catch return error.CompileError;
self.cg = cg;
}
/// Generate code via the IR pipeline: lower AST → IR → LLVM.
pub fn generateCodeViaIR(self: *Compilation) !void {
pub fn generateCode(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();
@@ -122,7 +107,6 @@ pub const Compilation = struct {
}
/// 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 {
const root = self.resolved_root orelse self.root orelse return &.{};
return c_import.collectCImportSources(self.allocator, root);