Commit bdb917006c

joachimschmidt557 <joachim.schmidt557@outlook.com>
2021-03-07 00:25:21
stage2 tzir: Add wrapping integer arithmetic instructions
1 parent b1a22fd
src/codegen.zig
@@ -865,6 +865,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
         fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {
             switch (inst.tag) {
                 .add => return self.genAdd(inst.castTag(.add).?),
+                .addwrap => return self.genAddWrap(inst.castTag(.addwrap).?),
                 .alloc => return self.genAlloc(inst.castTag(.alloc).?),
                 .arg => return self.genArg(inst.castTag(.arg).?),
                 .assembly => return self.genAsm(inst.castTag(.assembly).?),
@@ -900,12 +901,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
                 .loop => return self.genLoop(inst.castTag(.loop).?),
                 .not => return self.genNot(inst.castTag(.not).?),
                 .mul => return self.genMul(inst.castTag(.mul).?),
+                .mulwrap => return self.genMulWrap(inst.castTag(.mulwrap).?),
                 .ptrtoint => return self.genPtrToInt(inst.castTag(.ptrtoint).?),
                 .ref => return self.genRef(inst.castTag(.ref).?),
                 .ret => return self.genRet(inst.castTag(.ret).?),
                 .retvoid => return self.genRetVoid(inst.castTag(.retvoid).?),
                 .store => return self.genStore(inst.castTag(.store).?),
                 .sub => return self.genSub(inst.castTag(.sub).?),
+                .subwrap => return self.genSubWrap(inst.castTag(.subwrap).?),
                 .switchbr => return self.genSwitch(inst.castTag(.switchbr).?),
                 .unreach => return MCValue{ .unreach = {} },
                 .optional_payload => return self.genOptionalPayload(inst.castTag(.optional_payload).?),
@@ -1129,6 +1132,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genAddWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
+            // No side effects, so if it's unreferenced, do nothing.
+            if (inst.base.isUnused())
+                return MCValue.dead;
+            switch (arch) {
+                else => return self.fail(inst.base.src, "TODO implement addwrap for {}", .{self.target.cpu.arch}),
+            }
+        }
+
         fn genMul(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
             // No side effects, so if it's unreferenced, do nothing.
             if (inst.base.isUnused())
@@ -1139,6 +1151,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genMulWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
+            // No side effects, so if it's unreferenced, do nothing.
+            if (inst.base.isUnused())
+                return MCValue.dead;
+            switch (arch) {
+                else => return self.fail(inst.base.src, "TODO implement mulwrap for {}", .{self.target.cpu.arch}),
+            }
+        }
+
         fn genBitAnd(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
             // No side effects, so if it's unreferenced, do nothing.
             if (inst.base.isUnused())
@@ -1392,6 +1413,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genSubWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
+            // No side effects, so if it's unreferenced, do nothing.
+            if (inst.base.isUnused())
+                return MCValue.dead;
+            switch (arch) {
+                else => return self.fail(inst.base.src, "TODO implement subwrap for {}", .{self.target.cpu.arch}),
+            }
+        }
+
         fn genArmBinOp(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst, op: ir.Inst.Tag) !MCValue {
             const lhs = try self.resolveInst(op_lhs);
             const rhs = try self.resolveInst(op_rhs);
src/ir.zig
@@ -53,6 +53,7 @@ pub const Inst = struct {
 
     pub const Tag = enum {
         add,
+        addwrap,
         alloc,
         arg,
         assembly,
@@ -105,8 +106,10 @@ pub const Inst = struct {
         /// Write a value to a pointer. LHS is pointer, RHS is value.
         store,
         sub,
+        subwrap,
         unreach,
         mul,
+        mulwrap,
         not,
         floatcast,
         intcast,
@@ -165,8 +168,11 @@ pub const Inst = struct {
                 => UnOp,
 
                 .add,
+                .addwrap,
                 .sub,
+                .subwrap,
                 .mul,
+                .mulwrap,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
src/zir.zig
@@ -1680,8 +1680,11 @@ const DumpTzir = struct {
                 },
 
                 .add,
+                .addwrap,
                 .sub,
+                .subwrap,
                 .mul,
+                .mulwrap,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
@@ -1803,8 +1806,11 @@ const DumpTzir = struct {
                 },
 
                 .add,
+                .addwrap,
                 .sub,
+                .subwrap,
                 .mul,
+                .mulwrap,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
src/zir_sema.zig
@@ -2179,10 +2179,13 @@ fn zirArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!
     }
 
     const b = try mod.requireRuntimeBlock(scope, inst.base.src);
-    const ir_tag = switch (inst.base.tag) {
-        .add => Inst.Tag.add,
-        .sub => Inst.Tag.sub,
-        .mul => Inst.Tag.mul,
+    const ir_tag: Inst.Tag = switch (inst.base.tag) {
+        .add => .add,
+        .addwrap => .addwrap,
+        .sub => .sub,
+        .subwrap => .subwrap,
+        .mul => .mul,
+        .mulwrap => .mulwrap,
         else => return mod.fail(scope, inst.base.src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}),
     };