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

@@ -1,6 +1,9 @@
const std = @import("std");
const sx = @import("sx");
/// Feature flag: use the IR pipeline (parse → lower → IR → LLVM) instead of AST-based codegen.
const USE_IR_PIPELINE = true;
pub fn main(init: std.process.Init) !void {
const allocator = init.arena.allocator();
const io = init.io;
@@ -137,12 +140,25 @@ pub fn main(init: std.process.Init) !void {
}
// Cache MISS — codegen + emit .o to memory (verify skipped: JIT catches errors)
comp.generateCode() catch { comp.renderErrors(); return; };
if (USE_IR_PIPELINE) {
comp.generateCodeViaIR() catch { comp.renderErrors(); return; };
} else {
comp.generateCode() catch { comp.renderErrors(); return; };
}
timer.record("codegen");
timer.mark();
var cg = &comp.cg.?;
const buf = cg.emitObjectToMemory() catch { comp.renderErrors(); return; };
const buf = if (USE_IR_PIPELINE) blk2: {
comp.ir_emitter.?.verifyWithMessage() catch return;
break :blk2 comp.ir_emitter.?.emitObjectToMemory() catch return;
} else
(emit_blk: {
var cg = &comp.cg.?;
break :emit_blk cg.emitObjectToMemory() catch {
comp.renderErrors();
return;
};
});
timer.record("emit");
// Save .o to cache (extract data before JIT takes ownership)
@@ -295,12 +311,20 @@ fn compilePipeline(allocator: std.mem.Allocator, io: std.Io, input_path: []const
timer.record("imports");
timer.mark();
comp.generateCode() catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.generateCodeViaIR() catch { comp.renderErrors(); return error.CompileError; };
} else {
comp.generateCode() catch { comp.renderErrors(); return error.CompileError; };
}
timer.record("codegen");
timer.mark();
var cg = &comp.cg.?;
cg.verify() catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.ir_emitter.?.verifyWithMessage() catch return error.CompileError;
} else {
var cg = &comp.cg.?;
cg.verify() catch { comp.renderErrors(); return error.CompileError; };
}
timer.record("verify");
return comp;
@@ -328,7 +352,11 @@ fn emitIR(allocator: std.mem.Allocator, io: std.Io, input_path: []const u8, targ
var timer = Timing.init(false);
var comp = try compilePipeline(allocator, io, input_path, target_config, &timer);
defer comp.deinit();
comp.cg.?.printIR();
if (USE_IR_PIPELINE) {
comp.ir_emitter.?.printIR();
} else {
comp.cg.?.printIR();
}
}
fn emitAsm(allocator: std.mem.Allocator, io: std.Io, input_path: []const u8, target_config: sx.codegen.TargetConfig) !void {
@@ -340,7 +368,11 @@ fn emitAsm(allocator: std.mem.Allocator, io: std.Io, input_path: []const u8, tar
break :blk try std.fmt.allocPrint(allocator, "{s}.s", .{name});
};
const asm_path_z = try allocator.dupeZ(u8, asm_path);
comp.cg.?.emitAssembly(asm_path_z.ptr) catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.ir_emitter.?.emitAssembly(asm_path_z.ptr) catch return error.CompileError;
} else {
comp.cg.?.emitAssembly(asm_path_z.ptr) catch { comp.renderErrors(); return error.CompileError; };
}
std.debug.print("emitted: {s}\n", .{asm_path});
}
@@ -398,16 +430,29 @@ fn compileWithTimer(allocator: std.mem.Allocator, io: std.Io, input_path: []cons
} else {
// Cache MISS — full codegen + emit
timer.mark();
comp.generateCode() catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.generateCodeViaIR() catch { comp.renderErrors(); return error.CompileError; };
} else {
comp.generateCode() catch { comp.renderErrors(); return error.CompileError; };
}
timer.record("codegen");
timer.mark();
var cg = &comp.cg.?;
cg.verify() catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.ir_emitter.?.verifyWithMessage() catch return error.CompileError;
} else {
var cg = &comp.cg.?;
cg.verify() catch { comp.renderErrors(); return error.CompileError; };
}
timer.record("verify");
timer.mark();
cg.emitObject(obj_path.ptr) catch { comp.renderErrors(); return error.CompileError; };
if (USE_IR_PIPELINE) {
comp.ir_emitter.?.emitObject(obj_path.ptr) catch return error.CompileError;
} else {
var cg = &comp.cg.?;
cg.emitObject(obj_path.ptr) catch { comp.renderErrors(); return error.CompileError; };
}
timer.record("emit");
// Save .o to cache