authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-06-22 12:04:19-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-08 14:33:01-04:00
logec36ac8b21fa6c6d8514c60555aba8a96490a560
tree2a289989d4f8573eca918e32b8cbe2ffdcc0c8dc
parente2b954c2738c683a85b864eb33530f0e3dbbc480

stage2 astgen: provide 3 more errors for invalid inline assembly


3 files changed, 45 insertions(+), 3 deletions(-)

src/AstGen.zig+11-1
......@@ -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+1-2
......@@ -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+33
......@@ -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);