Skip to content

Commit ec36ac8

Browse files
g-w1andrewrk
authored andcommitted
stage2 astgen: provide 3 more errors for invalid inline assembly
1 parent e2b954c commit ec36ac8

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/AstGen.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6622,7 +6622,17 @@ fn asmExpr(
66226622
// See https://github.com/ziglang/zig/issues/215 and related issues discussing
66236623
// possible inline assembly improvements. Until then here is status quo AstGen
66246624
// for assembly syntax. It's used by std lib crypto aesni.zig.
6625-
6625+
const is_container_asm = astgen.fn_block == null;
6626+
if (is_container_asm) {
6627+
if (full.volatile_token) |t|
6628+
return astgen.failTok(t, "volatile is meaningless on global assembly", .{});
6629+
if (full.outputs.len != 0 or full.inputs.len != 0 or full.first_clobber != null)
6630+
return astgen.failNode(node, "global assembly cannot have inputs, outputs, or clobbers", .{});
6631+
} else {
6632+
if (full.outputs.len == 0 and full.volatile_token == null) {
6633+
return astgen.failNode(node, "assembly expression with no output must be marked volatile", .{});
6634+
}
6635+
}
66266636
if (full.outputs.len > 32) {
66276637
return astgen.failNode(full.outputs[32], "too many asm outputs", .{});
66286638
}

test/behavior/syntax.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ fn asm_lists() void {
6060
:[a] "x" (x),);
6161
asm ("not real assembly"
6262
:[a] "x" (->i32),:[a] "x" (1),);
63-
asm ("still not real assembly"
63+
asm volatile ("still not real assembly"
6464
:::"a","b",);
6565
}
6666
}
67-

test/cases.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,39 @@ pub fn addCases(ctx: *TestContext) !void {
15061506
\\ _ = x;
15071507
\\}
15081508
, &[_][]const u8{":4:27: error: expected type, found comptime_int"});
1509+
case.addError(
1510+
\\const S = struct {
1511+
\\ comptime {
1512+
\\ asm volatile (
1513+
\\ \\zig_moment:
1514+
\\ \\syscall
1515+
\\ );
1516+
\\ }
1517+
\\};
1518+
\\pub fn main() void {
1519+
\\ _ = S;
1520+
\\}
1521+
, &.{":3:13: error: volatile is meaningless on global assembly"});
1522+
case.addError(
1523+
\\pub fn main() void {
1524+
\\ var bruh: u32 = 1;
1525+
\\ asm (""
1526+
\\ :
1527+
\\ : [bruh] "{rax}" (4)
1528+
\\ : "memory"
1529+
\\ );
1530+
\\}
1531+
, &.{":3:5: error: assembly expression with no output must be marked volatile"});
1532+
case.addError(
1533+
\\pub fn main() void {}
1534+
\\comptime {
1535+
\\ asm (""
1536+
\\ :
1537+
\\ : [bruh] "{rax}" (4)
1538+
\\ : "memory"
1539+
\\ );
1540+
\\}
1541+
, &.{":3:5: error: global assembly cannot have inputs, outputs, or clobbers"});
15091542
}
15101543
{
15111544
var case = ctx.exe("comptime var", linux_x64);

0 commit comments

Comments
 (0)