diff options
| author | Pavel Verigo <paul.verigo@gmail.com> | 2024-06-21 22:26:21 +0200 |
|---|---|---|
| committer | Luuk de Gram <luuk@degram.dev> | 2024-06-22 21:35:36 +0200 |
| commit | 6026bbd0adb445a0edd6defb0b378937dcce7a9b (patch) | |
| tree | bfd570b423f20b1f061bc04d8ee2d8339520f6f3 /src/arch | |
| parent | 9be9b8ca902bb7c250a6836c6090af8525464749 (diff) | |
| download | zig-6026bbd0adb445a0edd6defb0b378937dcce7a9b.tar.gz zig-6026bbd0adb445a0edd6defb0b378937dcce7a9b.zip | |
stage2-wasm: fix div and rem
Diffstat (limited to 'src/arch')
| -rw-r--r-- | src/arch/wasm/CodeGen.zig | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index d4cba71bf6..b33f5052e7 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -1841,7 +1841,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { .bit_or => func.airBinOp(inst, .@"or"), .bool_and => func.airBinOp(inst, .@"and"), .bool_or => func.airBinOp(inst, .@"or"), - .rem => func.airBinOp(inst, .rem), + .rem => func.airRem(inst), .mod => func.airMod(inst), .shl => func.airWrapBinOp(inst, .shl), .shl_exact => func.airBinOp(inst, .shl), @@ -6917,13 +6917,54 @@ fn divSigned(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type) InnerError!WVal try func.emitWValue(lhs); try func.emitWValue(rhs); } - try func.addTag(.i32_div_s); + switch (wasm_bits) { + 32 => try func.addTag(.i32_div_s), + 64 => try func.addTag(.i64_div_s), + else => unreachable, + } + _ = try func.wrapOperand(.stack, ty); const result = try func.allocLocal(ty); try func.addLabel(.local_set, result.local.value); return result; } +fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { + const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; + + const mod = func.bin_file.base.comp.module.?; + const ty = func.typeOfIndex(inst); + const lhs = try func.resolveInst(bin_op.lhs); + const rhs = try func.resolveInst(bin_op.rhs); + + const result = if (ty.isSignedInt(mod)) result: { + const int_bits = ty.intInfo(mod).bits; + const wasm_bits = toWasmBits(int_bits) orelse { + return func.fail("TODO: `@rem` for signed integers larger than 128 bits ({d} bits requested)", .{int_bits}); + }; + + if (wasm_bits > 64) { + return func.fail("TODO: `@rem` for signed integers larger than 64 bits ({d} bits requested)", .{int_bits}); + } + + const lhs_wasm = if (wasm_bits != int_bits) + try (try func.signExtendInt(lhs, ty)).toLocal(func, ty) + else + lhs; + + const rhs_wasm = if (wasm_bits != int_bits) + try (try func.signExtendInt(rhs, ty)).toLocal(func, ty) + else + rhs; + + _ = try func.binOp(lhs_wasm, rhs_wasm, ty, .rem); + break :result try func.wrapOperand(.stack, ty); + } else try func.binOp(lhs, rhs, ty, .rem); + + const return_local = try result.toLocal(func, ty); + func.finishAir(inst, return_local, &.{ bin_op.lhs, bin_op.rhs }); +} + /// Remainder after floor division, defined by: /// @divFloor(a, b) * b + @mod(a, b) = a fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
