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:
10
src/ast.zig
10
src/ast.zig
@@ -96,6 +96,7 @@ pub const Node = struct {
|
||||
runtime_class_decl: RuntimeClassDecl,
|
||||
jni_env_block: JniEnvBlock,
|
||||
asm_expr: AsmExpr,
|
||||
asm_global: AsmGlobal,
|
||||
|
||||
pub fn declName(self: Data) ?[]const u8 {
|
||||
return switch (self) {
|
||||
@@ -259,6 +260,15 @@ pub const AsmOperand = struct {
|
||||
};
|
||||
};
|
||||
|
||||
/// Top-level (module-scope) global assembly: `asm { "tmpl", };` (ASM stream
|
||||
/// design §II.2 Deviation 6). Template only — no operands, no `volatile`, no
|
||||
/// `clobbers`, no `%` substitution. Lowers to `LLVMAppendModuleInlineAsm`;
|
||||
/// multiple blocks concatenate in source order. Symbols it defines are reached
|
||||
/// with a lib-less `extern` declaration.
|
||||
pub const AsmGlobal = struct {
|
||||
template: *Node, // string-literal / `#string` heredoc node
|
||||
};
|
||||
|
||||
pub const Identifier = struct {
|
||||
name: []const u8,
|
||||
/// True when written as a backtick raw identifier (`` `i2 ``). Carried so a
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -1367,6 +1367,7 @@ pub const Analyzer = struct {
|
||||
try self.analyzeNode(ae.template);
|
||||
for (ae.operands) |op| try self.analyzeNode(op.payload);
|
||||
},
|
||||
.asm_global => |ag| try self.analyzeNode(ag.template),
|
||||
.impl_block => |ib| {
|
||||
// Each impl block gets its own scope so methods don't conflict across impls
|
||||
try self.pushScope();
|
||||
@@ -1843,6 +1844,9 @@ pub fn findNodeAtOffset(node: *Node, offset: u32) ?*Node {
|
||||
if (findNodeAtOffset(op.payload, offset)) |found| return found;
|
||||
}
|
||||
},
|
||||
.asm_global => |ag| {
|
||||
if (findNodeAtOffset(ag.template, offset)) |found| return found;
|
||||
},
|
||||
}
|
||||
|
||||
return node;
|
||||
|
||||
Reference in New Issue
Block a user