Commit ebafdb958c

Daniele Cocca <daniele.cocca@gmail.com>
2022-03-20 21:57:56
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
1 parent 633fe41
Changed files (2)
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,
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;