Skip to content

Commit 1b432b5

Browse files
committed
stage2: implement global assembly
So far it's supported by the LLVM backend only. I recommend for the other backends to wait for the resolution of #10761 before adding support for this feature.
1 parent 0bebb68 commit 1b432b5

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

src/Module.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ allocated_decls: std.SegmentedList(Decl, 0) = .{},
151151
/// When a Decl object is freed from `allocated_decls`, it is pushed into this stack.
152152
decls_free_list: std.ArrayListUnmanaged(Decl.Index) = .{},
153153

154+
global_assembly: std.AutoHashMapUnmanaged(Decl.Index, []u8) = .{},
155+
154156
const MonomorphedFuncsSet = std.HashMapUnmanaged(
155157
*Fn,
156158
void,
@@ -2831,6 +2833,7 @@ pub fn deinit(mod: *Module) void {
28312833

28322834
mod.decls_free_list.deinit(gpa);
28332835
mod.allocated_decls.deinit(gpa);
2836+
mod.global_assembly.deinit(gpa);
28342837
}
28352838

28362839
pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
@@ -2842,6 +2845,9 @@ pub fn destroyDecl(mod: *Module, decl_index: Decl.Index) void {
28422845
if (decl.deletion_flag) {
28432846
assert(mod.deletion_set.swapRemove(decl_index));
28442847
}
2848+
if (mod.global_assembly.fetchRemove(decl_index)) |kv| {
2849+
gpa.free(kv.value);
2850+
}
28452851
if (decl.has_tv) {
28462852
if (decl.getInnerNamespace()) |namespace| {
28472853
namespace.destroyDecls(mod);
@@ -5714,3 +5720,12 @@ pub fn markDeclAlive(mod: *Module, decl: *Decl) void {
57145720
fn markDeclIndexAlive(mod: *Module, decl_index: Decl.Index) void {
57155721
return mod.markDeclAlive(mod.declPtr(decl_index));
57165722
}
5723+
5724+
pub fn addGlobalAssembly(mod: *Module, decl_index: Decl.Index, source: []const u8) !void {
5725+
try mod.global_assembly.ensureUnusedCapacity(mod.gpa, 1);
5726+
5727+
const duped_source = try mod.gpa.dupe(u8, source);
5728+
errdefer mod.gpa.free(duped_source);
5729+
5730+
mod.global_assembly.putAssumeCapacityNoClobber(decl_index, duped_source);
5731+
}

src/Sema.zig

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10517,16 +10517,35 @@ fn zirAsm(
1051710517
const is_volatile = @truncate(u1, extended.small >> 15) != 0;
1051810518
const is_global_assembly = sema.func == null;
1051910519

10520-
if (block.is_comptime and !is_global_assembly) {
10521-
try sema.requireRuntimeBlock(block, src);
10522-
}
10523-
1052410520
if (extra.data.asm_source == 0) {
1052510521
// This can move to become an AstGen error after inline assembly improvements land
1052610522
// and stage1 code matches stage2 code.
1052710523
return sema.fail(block, src, "assembly code must use string literal syntax", .{});
1052810524
}
1052910525

10526+
const asm_source = sema.code.nullTerminatedString(extra.data.asm_source);
10527+
10528+
if (is_global_assembly) {
10529+
if (outputs_len != 0) {
10530+
return sema.fail(block, src, "module-level assembly does not support outputs", .{});
10531+
}
10532+
if (inputs_len != 0) {
10533+
return sema.fail(block, src, "module-level assembly does not support inputs", .{});
10534+
}
10535+
if (clobbers_len != 0) {
10536+
return sema.fail(block, src, "module-level assembly does not support clobbers", .{});
10537+
}
10538+
if (is_volatile) {
10539+
return sema.fail(block, src, "volatile keyword is redundant on module-level assembly", .{});
10540+
}
10541+
try sema.mod.addGlobalAssembly(sema.owner_decl_index, asm_source);
10542+
return Air.Inst.Ref.void_value;
10543+
}
10544+
10545+
if (block.is_comptime) {
10546+
try sema.requireRuntimeBlock(block, src);
10547+
}
10548+
1053010549
if (outputs_len > 1) {
1053110550
return sema.fail(block, src, "TODO implement Sema for asm with more than 1 output", .{});
1053210551
}
@@ -10591,7 +10610,6 @@ fn zirAsm(
1059110610
needed_capacity += name.*.len / 4 + 1;
1059210611
}
1059310612

10594-
const asm_source = sema.code.nullTerminatedString(extra.data.asm_source);
1059510613
needed_capacity += (asm_source.len + 3) / 4;
1059610614

1059710615
const gpa = sema.gpa;

src/codegen/llvm.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,19 @@ pub const Object = struct {
476476
_ = builder.buildRet(is_lt);
477477
}
478478

479+
fn genModuleLevelAssembly(object: *Object, comp: *Compilation) !void {
480+
const mod = comp.bin_file.options.module.?;
481+
if (mod.global_assembly.count() == 0) return;
482+
var buffer = std.ArrayList(u8).init(comp.gpa);
483+
defer buffer.deinit();
484+
var it = mod.global_assembly.iterator();
485+
while (it.next()) |kv| {
486+
try buffer.appendSlice(kv.value_ptr.*);
487+
try buffer.append('\n');
488+
}
489+
object.llvm_module.setModuleInlineAsm2(buffer.items.ptr, buffer.items.len - 1);
490+
}
491+
479492
pub fn flushModule(self: *Object, comp: *Compilation, prog_node: *std.Progress.Node) !void {
480493
var sub_prog_node = prog_node.start("LLVM Emit Object", 0);
481494
sub_prog_node.activate();
@@ -484,6 +497,7 @@ pub const Object = struct {
484497

485498
try self.genErrorNameTable(comp);
486499
try self.genCmpLtErrorsLenFunction(comp);
500+
try self.genModuleLevelAssembly(comp);
487501

488502
if (self.di_builder) |dib| {
489503
// When lowering debug info for pointers, we emitted the element types as

src/codegen/llvm/bindings.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ pub const Module = opaque {
381381

382382
pub const createDIBuilder = ZigLLVMCreateDIBuilder;
383383
extern fn ZigLLVMCreateDIBuilder(module: *const Module, allow_unresolved: bool) *DIBuilder;
384+
385+
pub const setModuleInlineAsm2 = LLVMSetModuleInlineAsm2;
386+
extern fn LLVMSetModuleInlineAsm2(M: *const Module, Asm: [*]const u8, Len: usize) void;
384387
};
385388

386389
pub const lookupIntrinsicID = LLVMLookupIntrinsicID;

test/behavior/asm.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ test "module level assembly" {
2323
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
2424
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
2525
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
26-
if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // TODO
2726

2827
if (is_x86_64_linux) {
2928
try expect(this_is_my_alias() == 1234);

0 commit comments

Comments
 (0)