Files
sx/src/llvm_api.zig
agra c32d694d57 ERR/E3.0 (slice 2): emit DWARF line-info
Attach LLVM debug metadata so a captured return-address PC resolves to
file:line:col (the runtime half E3.3 needs) and sx binaries become
debuggable in lldb/gdb.

- llvm_api.zig: bind llvm-c/DebugInfo.h (DIBuilder C API was unbound).
- emit_llvm.zig: DIBuilder + one DICompileUnit/DIFile on the main file,
  a DISubprogram per function (LLVMSetSubprogram), and a DILocation per
  instruction from Inst.span (errors.SourceLoc.compute, scoped to the
  subprogram). Plus the "Debug Info Version"/"Dwarf Version" module
  flags and LLVMDIBuilderFinalize.
- Gated on opt none/less + a wired source map (setDebugContext from
  core.zig), mirroring lower.zig's tracesEnabled; release strips it.

Verified: sx ir/sx asm --opt none show correct DILocations + .loc
directives; the 290-example JIT suite (-O0 -> debug on) verifies and
runs unchanged. +2 DWARF unit tests.
2026-06-01 13:14:00 +03:00

62 lines
1.6 KiB
Zig

pub const c = @cImport({
@cInclude("llvm-c/Core.h");
@cInclude("llvm-c/Analysis.h");
@cInclude("llvm-c/DebugInfo.h");
@cInclude("llvm-c/Target.h");
@cInclude("llvm-c/TargetMachine.h");
@cInclude("llvm-c/LLJIT.h");
@cInclude("llvm-c/Orc.h");
@cInclude("llvm-c/Error.h");
@cInclude("llvm-c/BitReader.h");
@cInclude("llvm-c/BitWriter.h");
@cInclude("llvm-c/Linker.h");
// Clang shim for C header parsing + source compilation
@cInclude("clang_shim.h");
});
extern fn sx_llvm_init_all_targets() void;
extern fn sx_llvm_init_native_target() void;
pub fn initAllTargets() void {
sx_llvm_init_all_targets();
}
pub fn initNativeTarget() void {
sx_llvm_init_native_target();
}
// Type aliases for ergonomics
pub const Context = c.LLVMContextRef;
pub const Module = c.LLVMModuleRef;
pub const Builder = c.LLVMBuilderRef;
pub const Value = c.LLVMValueRef;
pub const Type = c.LLVMTypeRef;
pub const BasicBlock = c.LLVMBasicBlockRef;
pub const TargetMachine = c.LLVMTargetMachineRef;
pub fn createContext() Context {
return c.LLVMContextCreate();
}
pub fn disposeContext(ctx: Context) void {
c.LLVMContextDispose(ctx);
}
pub fn moduleCreateWithName(name: [*:0]const u8) Module {
return c.LLVMModuleCreateWithNameInContext(name, c.LLVMGetGlobalContext());
}
pub fn disposeModule(module: Module) void {
c.LLVMDisposeModule(module);
}
pub fn createBuilderInContext(ctx: Context) Builder {
return c.LLVMCreateBuilderInContext(ctx);
}
pub fn disposeBuilder(builder: Builder) void {
c.LLVMDisposeBuilder(builder);
}