feat(asm): Phase F — global (module-scope) asm
A top-level `asm { "tmpl", };` block (template only) lowers to LLVM `module asm`;
a lib-less `extern` declaration calls into the symbols it defines (the import
direction reuses the existing C-FFI extern path — no new surface).
- ast.zig: asm_global node (AsmGlobal { template }).
- parser.zig: parseAsmGlobal, dispatched from parseTopLevel on kw_asm — rejects
`volatile` and any operands/clobbers (template only). The in-function asm
expression form stays in parsePrimary.
- module.zig: Module.global_asm list; lower/decl.zig captures each template in
lowerMainAndComptime (the real top-level pass — lowerDecls is dead for
top-level); emit_llvm.zig emit() appends each via LLVMAppendModuleInlineAsm in
source order.
- the new node forced asm_global arms in sema.zig (analyzeNode +
findNodeAtOffset) and semantic_diagnostics.zig (checkBindingNames).
Verified end-to-end: an aarch64 `_my_add` global routine, called via `extern`,
returns 42 — AOT only (the ORC JIT doesn't link module-asm symbols; global-asm
symbols live in the final linked binary). Locked with 1648-platform-asm-global
({ "aot": true, "target": "macos" } → AOT build+run on aarch64, ir-only else).
zig build test green (656 corpus, 446 unit).
This commit is contained in:
@@ -347,6 +347,14 @@ pub const LLVMEmitter = struct {
|
||||
// Must precede any DISubprogram (created per function below).
|
||||
self.debugInfo().initDebugInfo();
|
||||
|
||||
// Top-level global asm (ASM stream Phase F): append each block verbatim
|
||||
// to the module. Multiple blocks concatenate in source order; LLVM emits
|
||||
// them as module-level `module asm`. Symbols they define are reached via
|
||||
// lib-less `extern` declarations.
|
||||
for (self.ir_mod.global_asm.items) |asm_text| {
|
||||
c.LLVMAppendModuleInlineAsm(self.llvm_module, asm_text.ptr, asm_text.len);
|
||||
}
|
||||
|
||||
// Pass 0: Declare and initialize globals
|
||||
self.emitGlobals();
|
||||
|
||||
|
||||
@@ -1342,6 +1342,17 @@ pub fn lowerMainAndComptime(self: *Lowering, decls: []const *const Node) void {
|
||||
self.lowerMainAndComptime(ns.decls);
|
||||
}
|
||||
},
|
||||
// Top-level global asm (Phase F): capture the verbatim template; it
|
||||
// is appended to the LLVM module at emit time (source order). The
|
||||
// template must be a comptime-known string (parser guarantees a
|
||||
// string node here).
|
||||
.asm_global => |ag| {
|
||||
if (ag.template.data == .string_literal) {
|
||||
self.module.global_asm.append(self.alloc, ag.template.data.string_literal.raw) catch unreachable;
|
||||
} else if (self.diagnostics) |diags| {
|
||||
diags.addFmt(.err, decl.span, "global asm template must be a compile-time-known string", .{});
|
||||
}
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ pub const Module = struct {
|
||||
/// (trampoline emission, +alloc/-dealloc synthesis) can re-walk
|
||||
/// `members` for fields / methods / `#extends` / `#implements`.
|
||||
objc_defined_class_cache: std.ArrayList(ObjcDefinedClassEntry),
|
||||
/// Top-level `asm { … }` blocks (ASM stream Phase F), in source order.
|
||||
/// Each is verbatim assembly appended to the LLVM module via
|
||||
/// `LLVMAppendModuleInlineAsm` at emit time; multiple blocks concatenate.
|
||||
global_asm: std.ArrayList([]const u8),
|
||||
alloc: Allocator,
|
||||
/// Owns the per-instruction operand slices the Builder dupes (aggregate
|
||||
/// fields, call args, branch args, switch cases, block params). These live
|
||||
@@ -100,6 +104,7 @@ pub const Module = struct {
|
||||
.objc_selector_cache = std.ArrayList(ObjcSelectorEntry).empty,
|
||||
.objc_class_cache = std.ArrayList(ObjcClassEntry).empty,
|
||||
.objc_defined_class_cache = std.ArrayList(ObjcDefinedClassEntry).empty,
|
||||
.global_asm = std.ArrayList([]const u8).empty,
|
||||
.alloc = alloc,
|
||||
.slice_arena = std.heap.ArenaAllocator.init(alloc),
|
||||
};
|
||||
@@ -115,6 +120,7 @@ pub const Module = struct {
|
||||
self.objc_selector_cache.deinit(self.alloc);
|
||||
self.objc_class_cache.deinit(self.alloc);
|
||||
self.objc_defined_class_cache.deinit(self.alloc);
|
||||
self.global_asm.deinit(self.alloc);
|
||||
self.types.deinit();
|
||||
self.slice_arena.deinit();
|
||||
}
|
||||
|
||||
@@ -316,6 +316,7 @@ pub const UnknownTypeChecker = struct {
|
||||
self.checkBindingNames(ae.template);
|
||||
for (ae.operands) |op| self.checkBindingNames(op.payload);
|
||||
},
|
||||
.asm_global => |ag| self.checkBindingNames(ag.template),
|
||||
// ── Named type / alias / import declarations: a bare reserved
|
||||
// spelling as the declared name is rejected. These
|
||||
// have no nested binding sites, so only the name is checked. A
|
||||
|
||||
Reference in New Issue
Block a user