From ba8522e6c79b5c93cfbd8bdfbecabf8255848624 Mon Sep 17 00:00:00 2001 From: Pavel Verigo Date: Tue, 23 Jul 2024 21:17:04 +0200 Subject: [PATCH] stage2-wasm: fix bigint div and trunc --- src/arch/wasm/CodeGen.zig | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index c23b8f3f72..68b7f72938 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -2666,8 +2666,8 @@ fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) Inner switch (op) { .mul => return func.callIntrinsic("__multi3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), .div => switch (int_info.signedness) { - .signed => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), - .unsigned => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), + .signed => return func.callIntrinsic("__divti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), + .unsigned => return func.callIntrinsic("__udivti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), }, .rem => switch (int_info.signedness) { .signed => return func.callIntrinsic("__modti3", &.{ ty.toIntern(), ty.toIntern() }, ty, &.{ lhs, rhs }), @@ -4378,7 +4378,7 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { else try func.intcast(operand, operand_ty, ty); - return func.finishAir(inst, result, &.{}); + return func.finishAir(inst, result, &.{ty_op.operand}); } /// Upcasts or downcasts an integer based on the given and wanted types, @@ -4677,10 +4677,20 @@ fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; const operand = try func.resolveInst(ty_op.operand); - const wanted_ty = ty_op.ty.toType(); + const wanted_ty: Type = ty_op.ty.toType(); const op_ty = func.typeOf(ty_op.operand); + const pt = func.pt; + const mod = pt.zcu; + + if (wanted_ty.zigTypeTag(mod) == .Vector or op_ty.zigTypeTag(mod) == .Vector) { + return func.fail("TODO: trunc for vectors", .{}); + } + + const result = if (op_ty.bitSize(pt) == wanted_ty.bitSize(pt)) + func.reuseOperand(ty_op.operand, operand) + else + try func.trunc(operand, wanted_ty, op_ty); - const result = try func.trunc(operand, wanted_ty, op_ty); return func.finishAir(inst, result, &.{ty_op.operand}); }