diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-08-31 21:59:48 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-31 21:59:48 -0400 |
| commit | 3b9ec4e4df634b17268034a6a5527c11cf67e54b (patch) | |
| tree | b2234a4d097f1586cf32540fa0ae967c33906874 /src/Sema.zig | |
| parent | d522f925b7f2f7f9d4782bb42eed95d5da4f3e0f (diff) | |
| parent | cf9684ce75d4f9a4dc576d9c2cd490edcb8002df (diff) | |
| download | zig-3b9ec4e4df634b17268034a6a5527c11cf67e54b.tar.gz zig-3b9ec4e4df634b17268034a6a5527c11cf67e54b.zip | |
Merge pull request #9655 from nektro/stage2-rem
stage2: implement runtime `%` and `@rem`
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index eccf22d02d..b05eba18f9 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -5819,7 +5819,7 @@ fn analyzeArithmetic( try lhs_val.floatMul(rhs_val, scalar_type, sema.arena); break :blk val; }, - else => return sema.mod.fail(&block.base, src, "TODO Implement arithmetic operand '{s}'", .{@tagName(zir_tag)}), + else => return sema.mod.fail(&block.base, src, "TODO implement comptime arithmetic for operand '{s}'", .{@tagName(zir_tag)}), }; log.debug("{s}({}, {}) result: {}", .{ @tagName(zir_tag), lhs_val, rhs_val, value }); @@ -5832,6 +5832,14 @@ fn analyzeArithmetic( try sema.requireRuntimeBlock(block, lhs_src); } + if (zir_tag == .mod_rem) { + const dirty_lhs = lhs_ty.isSignedInt() or lhs_ty.isFloat(); + const dirty_rhs = rhs_ty.isSignedInt() or rhs_ty.isFloat(); + if (dirty_lhs or dirty_rhs) { + return sema.mod.fail(&block.base, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ lhs_ty, rhs_ty }); + } + } + const air_tag: Air.Inst.Tag = switch (zir_tag) { .add => .add, .addwrap => .addwrap, @@ -5840,7 +5848,9 @@ fn analyzeArithmetic( .mul => .mul, .mulwrap => .mulwrap, .div => .div, - else => return sema.mod.fail(&block.base, src, "TODO implement arithmetic for operand '{s}''", .{@tagName(zir_tag)}), + .mod_rem => .rem, + .rem => .rem, + else => return sema.mod.fail(&block.base, src, "TODO implement arithmetic for operand '{s}'", .{@tagName(zir_tag)}), }; return block.addBinOp(air_tag, casted_lhs, casted_rhs); @@ -7302,9 +7312,7 @@ fn zirMod(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!A } fn zirRem(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { - const inst_data = sema.code.instructions.items(.data)[inst].pl_node; - const src = inst_data.src(); - return sema.mod.fail(&block.base, src, "TODO: Sema.zirRem", .{}); + return sema.zirArithmetic(block, inst); } fn zirShlExact(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
