From ebafdb958c1aa6b41c28fc7d45f44e38a69a3bd5 Mon Sep 17 00:00:00 2001 From: Daniele Cocca Date: Sun, 20 Mar 2022 20:57:56 +0000 Subject: [PATCH] AstGen: don't coerce inputs to usize in asmExpr Instead, use ResultLoc.none to allow for the expression type to be inferred [^1]. This effectively moves the type coercion to Sema, in order to turn comptime values into usable values for the backends to consume. Right now the coercion is applies as comptime_int -> usize and comptime_float -> f64, as an arbitrary choice. [^1]: https://github.com/ziglang/zig/blob/9f25c8140cb859fcea7023362afcb29b1e4df41f/src/AstGen.zig#L207-L208 --- src/AstGen.zig | 2 +- src/Sema.zig | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/AstGen.zig b/src/AstGen.zig index 296eb80e7c..b66f7b868b 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -6842,7 +6842,7 @@ fn asmExpr( const name = try astgen.identAsString(symbolic_name); const constraint_token = symbolic_name + 2; const constraint = (try astgen.strLitAsString(constraint_token)).index; - const operand = try expr(gz, scope, .{ .ty = .usize_type }, node_datas[input_node].lhs); + const operand = try expr(gz, scope, .none, node_datas[input_node].lhs); inputs[i] = .{ .name = name, .constraint = constraint, diff --git a/src/Sema.zig b/src/Sema.zig index f13f97bbbb..112939c995 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -10304,7 +10304,14 @@ fn zirAsm( const name = sema.code.nullTerminatedString(input.data.name); _ = name; // TODO: use the name - arg.* = sema.resolveInst(input.data.operand); + const uncasted_arg = sema.resolveInst(input.data.operand); + const uncasted_arg_ty = sema.typeOf(uncasted_arg); + switch (uncasted_arg_ty.zigTypeTag()) { + .ComptimeInt => arg.* = try sema.coerce(block, Type.initTag(.usize), uncasted_arg, src), + .ComptimeFloat => arg.* = try sema.coerce(block, Type.initTag(.f64), uncasted_arg, src), + else => arg.* = uncasted_arg, + } + const constraint = sema.code.nullTerminatedString(input.data.constraint); needed_capacity += constraint.len / 4 + 1; inputs[arg_i] = constraint;