authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-12-23 03:52:49-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-12 16:23:42-08:00
log3d6c26525f3914a139d0843307d7052bc3841177
treecb35357abed048541698a1b160fa7e40e02568be
parentd8b5831dc4f0666dceef671f47d23b5447b25912

sema: forbid asm output to const locals


2 files changed, 22 insertions(+), 0 deletions(-)

src/Sema.zig+8
......@@ -16614,6 +16614,7 @@ fn zirAsm(
1661416614 const clobbers_len: u5 = @truncate(extended.small >> 10);
1661516615 const is_volatile = @as(u1, @truncate(extended.small >> 15)) != 0;
1661616616 const is_global_assembly = sema.func_index == .none;
16617 const zir_tags = sema.code.instructions.items(.tag);
1661716618
1661816619 const asm_source: []const u8 = if (tmpl_is_expr) blk: {
1661916620 const tmpl: Zir.Inst.Ref = @enumFromInt(@intFromEnum(extra.data.asm_source));
......@@ -16674,6 +16675,13 @@ fn zirAsm(
1667416675 const name = sema.code.nullTerminatedString(output.data.name);
1667516676 needed_capacity += (constraint.len + name.len + (2 + 3)) / 4;
1667616677
16678 if (output.data.operand.toIndex()) |index| {
16679 if (zir_tags[@intFromEnum(index)] == .ref) {
16680 // TODO: better error location; it would be even nicer if there were notes that pointed at the output and the variable definition
16681 return sema.fail(block, src, "asm cannot output to const local '{s}'", .{name});
16682 }
16683 }
16684
1667716685 outputs[out_i] = .{ .c = constraint, .n = name };
1667816686 }
1667916687
test/cases/compile_errors/asm_output_to_const.zig created+14
......@@ -0,0 +1,14 @@
1export fn foo() void {
2 const f: i64 = 1000;
3
4 asm volatile (
5 \\ movq $10, %[f]
6 : [f] "=r" (f),
7 );
8}
9
10// error
11// backend=llvm
12// target=native
13//
14// :4:5: error: asm cannot output to const local 'f'