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:
agra
2026-06-15 22:22:29 +03:00
parent d3c6ffed5a
commit 4d75b9323c
14 changed files with 146 additions and 19 deletions

View File

@@ -104,6 +104,13 @@ pub const Parser = struct {
return try self.createNode(start, .{ .import_decl = .{ .path = path, .name = null } });
}
// Top-level (module-scope) global assembly: `asm { "tmpl", };`
// (template only — no operands/volatile/clobbers). The in-function
// `asm { … }` expression form is parsed in `parsePrimary` instead.
if (self.current.tag == .kw_asm) {
return self.parseAsmGlobal(start);
}
// Top-level #run directive
if (self.current.tag == .hash_run) {
self.advance();
@@ -2801,6 +2808,24 @@ pub const Parser = struct {
} });
}
/// Top-level global assembly `asm { "tmpl", };` — template only. Rejects
/// `volatile` and any operands/clobbers (design §II.2 Deviation 6).
fn parseAsmGlobal(self: *Parser, start: u32) anyerror!*Node {
self.advance(); // consume `asm`
if (self.isContextualWord("volatile")) {
return self.fail("global (top-level) asm cannot be `volatile`");
}
try self.expect(.l_brace);
const template = try self.parseExpr();
if (self.current.tag == .comma) self.advance(); // optional trailing comma
if (self.current.tag != .r_brace) {
return self.fail("global (top-level) asm takes no operands, inputs, or clobbers — only a template string");
}
try self.expect(.r_brace);
try self.expect(.semicolon);
return try self.createNode(start, .{ .asm_global = .{ .template = template } });
}
fn parsePrimary(self: *Parser) anyerror!*Node {
const start = self.current.loc.start;
// Pack references in expression position: