zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit ebafdb958c1aa6b41c28fc7d45f44e38a69a3bd5 (tree)
parent 633fe41a2c2310d8cfab53ee9d87bb50ac4efc41
Author: Daniele Cocca <daniele.cocca@gmail.com>
Date:   Sun, 20 Mar 2022 20:57:56 +0000

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

Diffstat:
Msrc/AstGen.zig | 2+-
Msrc/Sema.zig | 9++++++++-
2 files changed, 9 insertions(+), 2 deletions(-)

diff --git 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 @@ -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;