authorgravatar for pentuppup@noreply.codeberg.orgpentuppup <pentuppup@noreply.codeberg.org> 2025-10-15 00:33:12-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-12-06 09:42:51+01:00
log28c5cc390c909db06782eb2366d315c5da501a5f
treea3edbd02ec35d150cb6848c7783843182265a17e
parentdbb4c8d1514f351bbef4a6977a0b188a3c6b81dc

detect `comptime var` references in asm input/output and improve errors


6 files changed, 100 insertions(+), 45 deletions(-)

lib/std/zig/AstGen.zig+1-1
......@@ -13000,9 +13000,9 @@ const GenZir = struct {
1300013000 }
1300113001
1300213002 const small: Zir.Inst.Asm.Small = .{
13003 .is_volatile = args.is_volatile,
1300313004 .outputs_len = @intCast(args.outputs.len),
1300413005 .inputs_len = @intCast(args.inputs.len),
13005 .is_volatile = args.is_volatile,
1300613006 };
1300713007
1300813008 const new_index: Zir.Inst.Index = @enumFromInt(astgen.instructions.len);
src/Sema.zig+25-9
......@@ -16388,6 +16388,7 @@ fn zirAsm(
1638816388
1638916389 const pt = sema.pt;
1639016390 const zcu = pt.zcu;
16391 const ip = &zcu.intern_pool;
1639116392 const extra = sema.code.extraData(Zir.Inst.Asm, extended.operand);
1639216393 const src = block.nodeOffset(extra.data.src_node);
1639316394 const ret_ty_src = block.src(.{ .node_offset_asm_ret_ty = extra.data.src_node });
......@@ -16396,7 +16397,6 @@ fn zirAsm(
1639616397 const inputs_len = small.inputs_len;
1639716398 const is_volatile = small.is_volatile;
1639816399 const is_global_assembly = sema.func_index == .none;
16399 const zir_tags = sema.code.instructions.items(.tag);
1640016400
1640116401 const asm_source: []const u8 = if (tmpl_is_expr) s: {
1640216402 const tmpl: Zir.Inst.Ref = @enumFromInt(@intFromEnum(extra.data.asm_source));
......@@ -16426,29 +16426,37 @@ fn zirAsm(
1642616426
1642716427 for (out_args, 0..) |*arg, out_i| {
1642816428 const output = sema.code.extraData(Zir.Inst.Asm.Output, extra_i);
16429 const output_src = block.src(.{ .asm_output = .{
16430 .offset = src.offset.node_offset.x,
16431 .output_index = @intCast(out_i),
16432 } });
1642916433 extra_i = output.end;
1643016434
1643116435 const is_type = @as(u1, @truncate(output_type_bits)) != 0;
1643216436 output_type_bits >>= 1;
1643316437
16438 const name = sema.code.nullTerminatedString(output.data.name);
16439
1643416440 if (is_type) {
1643516441 // Indicate the output is the asm instruction return value.
1643616442 arg.* = .none;
1643716443 const out_ty = try sema.resolveType(block, ret_ty_src, output.data.operand);
1643816444 expr_ty = Air.internedToRef(out_ty.toIntern());
1643916445 } else {
16440 arg.* = try sema.resolveInst(output.data.operand);
16446 const inst = try sema.resolveInst(output.data.operand);
16447 if (!sema.checkRuntimeValue(inst)) {
16448 const output_name = try ip.getOrPutString(sema.gpa, pt.tid, name, .no_embedded_nulls);
16449 return sema.failWithContainsReferenceToComptimeVar(block, output_src, output_name, "assembly output", .fromInterned(inst.toInterned().?));
16450 }
16451 arg.* = inst;
1644116452 }
1644216453
1644316454 const constraint = sema.code.nullTerminatedString(output.data.constraint);
16444 const name = sema.code.nullTerminatedString(output.data.name);
1644516455 needed_capacity += (constraint.len + name.len + (2 + 3)) / 4;
1644616456
16447 if (output.data.operand.toIndex()) |index| {
16448 if (zir_tags[@intFromEnum(index)] == .ref) {
16449 // TODO: better error location; it would be even nicer if there were notes that pointed at the output and the variable definition
16450 return sema.fail(block, src, "asm cannot output to const local '{s}'", .{name});
16451 }
16457 // AstGen gives us a reference to a variable
16458 if (arg.* != .none and sema.typeOf(arg.*).isConstPtr(zcu)) {
16459 return sema.fail(block, output_src, "asm cannot output to const '{s}'", .{name});
1645216460 }
1645316461
1645416462 outputs[out_i] = .{ .c = constraint, .n = name };
......@@ -16459,9 +16467,18 @@ fn zirAsm(
1645916467
1646016468 for (args, 0..) |*arg, arg_i| {
1646116469 const input = sema.code.extraData(Zir.Inst.Asm.Input, extra_i);
16470 const input_src = block.src(.{ .asm_input = .{
16471 .offset = src.offset.node_offset.x,
16472 .input_index = @intCast(arg_i),
16473 } });
1646216474 extra_i = input.end;
1646316475
1646416476 const uncasted_arg = try sema.resolveInst(input.data.operand);
16477 const name = sema.code.nullTerminatedString(input.data.name);
16478 if (!sema.checkRuntimeValue(uncasted_arg)) {
16479 const input_name = try ip.getOrPutString(sema.gpa, pt.tid, name, .no_embedded_nulls);
16480 return sema.failWithContainsReferenceToComptimeVar(block, input_src, input_name, "assembly input", .fromInterned(uncasted_arg.toInterned().?));
16481 }
1646516482 const uncasted_arg_ty = sema.typeOf(uncasted_arg);
1646616483 switch (uncasted_arg_ty.zigTypeTag(zcu)) {
1646716484 .comptime_int => arg.* = try sema.coerce(block, .usize, uncasted_arg, src),
......@@ -16472,7 +16489,6 @@ fn zirAsm(
1647216489 }
1647316490
1647416491 const constraint = sema.code.nullTerminatedString(input.data.constraint);
16475 const name = sema.code.nullTerminatedString(input.data.name);
1647616492 needed_capacity += (constraint.len + name.len + (2 + 3)) / 4;
1647716493 inputs[arg_i] = .{ .c = constraint, .n = name };
1647816494 }
src/Zcu.zig+31
......@@ -1542,6 +1542,25 @@ pub const SrcLoc = struct {
15421542 };
15431543 return tree.nodeToSpan(src_node);
15441544 },
1545 .asm_input => |input| {
1546 const tree = try src_loc.file_scope.getTree(zcu);
1547 const node = input.offset.toAbsolute(src_loc.base_node);
1548 const full = tree.fullAsm(node).?;
1549 const asm_input = full.inputs[input.input_index];
1550 return tree.nodeToSpan(tree.nodeData(asm_input).node_and_token[0]);
1551 },
1552 .asm_output => |output| {
1553 const tree = try src_loc.file_scope.getTree(zcu);
1554 const node = output.offset.toAbsolute(src_loc.base_node);
1555 const full = tree.fullAsm(node).?;
1556 const asm_output = full.outputs[output.output_index];
1557 const data = tree.nodeData(asm_output).opt_node_and_token;
1558 return if (data[0].unwrap()) |output_node|
1559 tree.nodeToSpan(output_node)
1560 else
1561 // token points to the ')'
1562 tree.tokenToSpan(data[1] - 1);
1563 },
15451564 .for_input => |for_input| {
15461565 const tree = try src_loc.file_scope.getTree(zcu);
15471566 const node = for_input.for_node_offset.toAbsolute(src_loc.base_node);
......@@ -2507,6 +2526,18 @@ pub const LazySrcLoc = struct {
25072526 /// The source location points to the operand of a `return` statement, or
25082527 /// the `return` itself if there is no explicit operand.
25092528 node_offset_return_operand: Ast.Node.Offset,
2529 /// The source location points to an assembly input
2530 asm_input: struct {
2531 /// Points to the assembly node
2532 offset: Ast.Node.Offset,
2533 input_index: u32,
2534 },
2535 /// The source location points to an assembly output
2536 asm_output: struct {
2537 /// Points to the assembly node
2538 offset: Ast.Node.Offset,
2539 output_index: u32,
2540 },
25102541 /// The source location points to a for loop input.
25112542 for_input: struct {
25122543 /// Points to the for loop AST node.
src/print_zir.zig+11-30
......@@ -1267,18 +1267,14 @@ const Writer = struct {
12671267 tmpl_is_expr: bool,
12681268 ) !void {
12691269 const extra = self.code.extraData(Zir.Inst.Asm, extended.operand);
1270 const outputs_len = @as(u5, @truncate(extended.small));
1271 const inputs_len = @as(u5, @truncate(extended.small >> 5));
1272 const clobbers_len = @as(u5, @truncate(extended.small >> 10));
1273 const is_volatile = @as(u1, @truncate(extended.small >> 15)) != 0;
1270 const small: Zir.Inst.Asm.Small = @bitCast(extended.small);
12741271
1275 try self.writeFlag(stream, "volatile, ", is_volatile);
1272 try self.writeFlag(stream, "volatile, ", small.is_volatile);
12761273 if (tmpl_is_expr) {
12771274 try self.writeInstRef(stream, @enumFromInt(@intFromEnum(extra.data.asm_source)));
1278 try stream.writeAll(", ");
12791275 } else {
12801276 const asm_source = self.code.nullTerminatedString(extra.data.asm_source);
1281 try stream.print("\"{f}\", ", .{std.zig.fmtString(asm_source)});
1277 try stream.print("\"{f}\"", .{std.zig.fmtString(asm_source)});
12821278 }
12831279 try stream.writeAll(", ");
12841280
......@@ -1286,7 +1282,7 @@ const Writer = struct {
12861282 var output_type_bits = extra.data.output_type_bits;
12871283 {
12881284 var i: usize = 0;
1289 while (i < outputs_len) : (i += 1) {
1285 while (i < small.outputs_len) : (i += 1) {
12901286 const output = self.code.extraData(Zir.Inst.Asm.Output, extra_i);
12911287 extra_i = output.end;
12921288
......@@ -1298,17 +1294,14 @@ const Writer = struct {
12981294 try stream.print("output({f}, \"{f}\", ", .{
12991295 std.zig.fmtIdP(name), std.zig.fmtString(constraint),
13001296 });
1301 try self.writeFlag(stream, "->", is_type);
1297 try self.writeFlag(stream, "-> ", is_type);
13021298 try self.writeInstRef(stream, output.data.operand);
1303 try stream.writeAll(")");
1304 if (i + 1 < outputs_len) {
1305 try stream.writeAll("), ");
1306 }
1299 try stream.writeAll("), ");
13071300 }
13081301 }
13091302 {
13101303 var i: usize = 0;
1311 while (i < inputs_len) : (i += 1) {
1304 while (i < small.inputs_len) : (i += 1) {
13121305 const input = self.code.extraData(Zir.Inst.Asm.Input, extra_i);
13131306 extra_i = input.end;
13141307
......@@ -1318,24 +1311,12 @@ const Writer = struct {
13181311 std.zig.fmtIdP(name), std.zig.fmtString(constraint),
13191312 });
13201313 try self.writeInstRef(stream, input.data.operand);
1321 try stream.writeAll(")");
1322 if (i + 1 < inputs_len) {
1323 try stream.writeAll(", ");
1324 }
1325 }
1326 }
1327 {
1328 var i: usize = 0;
1329 while (i < clobbers_len) : (i += 1) {
1330 const str_index = self.code.extra[extra_i];
1331 extra_i += 1;
1332 const clobber = self.code.nullTerminatedString(@enumFromInt(str_index));
1333 try stream.print("{f}", .{std.zig.fmtIdP(clobber)});
1334 if (i + 1 < clobbers_len) {
1335 try stream.writeAll(", ");
1336 }
1314 try stream.writeAll("), ");
13371315 }
13381316 }
1317
1318 try self.writeInstRef(stream, extra.data.clobbers);
1319
13391320 try stream.writeAll(")) ");
13401321 try self.writeSrcNode(stream, extra.data.src_node);
13411322 }
test/cases/compile_errors/asm_output_to_const.zig+11-5
......@@ -1,12 +1,18 @@
11export fn foo() void {
2 const f: i64 = 1000;
2 const local: usize = 0;
3 asm volatile (""
4 : [_] "=r" (local),
5 );
6}
37
4 asm volatile (
5 \\ movq $10, %[f]
6 : [f] "=r" (f),
8const global: usize = 0;
9export fn bar() void {
10 asm volatile (""
11 : [_] "=r" (global),
712 );
813}
914
1015// error
1116//
12// :4:5: error: asm cannot output to const local 'f'
17// :4:21: error: asm cannot output to const '_'
18// :11:21: error: asm cannot output to const '_'
test/cases/compile_errors/comptime_var_referenced_by_asm.zig created+21
......@@ -0,0 +1,21 @@
1export fn foo() void {
2 comptime var a: u32 = 0;
3 asm volatile (""
4 :
5 : [in] "r" (&a),
6 );
7}
8
9export fn bar() void {
10 comptime var a: u32 = 0;
11 asm volatile (""
12 : [out] "=r" (a),
13 );
14}
15
16// error
17//
18// :5:21: error: assembly input contains reference to comptime var
19// :2:14: note: 'in' points to comptime var declared here
20// :12:23: error: assembly output contains reference to comptime var
21// :10:14: note: 'out' points to comptime var declared here