authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-03-07 00:25:21+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-11 14:31:21-05:00
logbdb917006c9920f2a0d2091cb0f3d52454e039f0
tree5ee944462e7096be770e34c51348d88a5aa95426
parentb1a22fdbab4a30a91cd628037f2c7e15978607a5

stage2 tzir: Add wrapping integer arithmetic instructions


4 files changed, 49 insertions(+), 4 deletions(-)

src/codegen.zig+30
......@@ -865,6 +865,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
865865 fn genFuncInst(self: *Self, inst: *ir.Inst) !MCValue {
866866 switch (inst.tag) {
867867 .add => return self.genAdd(inst.castTag(.add).?),
868 .addwrap => return self.genAddWrap(inst.castTag(.addwrap).?),
868869 .alloc => return self.genAlloc(inst.castTag(.alloc).?),
869870 .arg => return self.genArg(inst.castTag(.arg).?),
870871 .assembly => return self.genAsm(inst.castTag(.assembly).?),
......@@ -900,12 +901,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
900901 .loop => return self.genLoop(inst.castTag(.loop).?),
901902 .not => return self.genNot(inst.castTag(.not).?),
902903 .mul => return self.genMul(inst.castTag(.mul).?),
904 .mulwrap => return self.genMulWrap(inst.castTag(.mulwrap).?),
903905 .ptrtoint => return self.genPtrToInt(inst.castTag(.ptrtoint).?),
904906 .ref => return self.genRef(inst.castTag(.ref).?),
905907 .ret => return self.genRet(inst.castTag(.ret).?),
906908 .retvoid => return self.genRetVoid(inst.castTag(.retvoid).?),
907909 .store => return self.genStore(inst.castTag(.store).?),
908910 .sub => return self.genSub(inst.castTag(.sub).?),
911 .subwrap => return self.genSubWrap(inst.castTag(.subwrap).?),
909912 .switchbr => return self.genSwitch(inst.castTag(.switchbr).?),
910913 .unreach => return MCValue{ .unreach = {} },
911914 .optional_payload => return self.genOptionalPayload(inst.castTag(.optional_payload).?),
......@@ -1129,6 +1132,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
11291132 }
11301133 }
11311134
1135 fn genAddWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
1136 // No side effects, so if it's unreferenced, do nothing.
1137 if (inst.base.isUnused())
1138 return MCValue.dead;
1139 switch (arch) {
1140 else => return self.fail(inst.base.src, "TODO implement addwrap for {}", .{self.target.cpu.arch}),
1141 }
1142 }
1143
11321144 fn genMul(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
11331145 // No side effects, so if it's unreferenced, do nothing.
11341146 if (inst.base.isUnused())
......@@ -1139,6 +1151,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
11391151 }
11401152 }
11411153
1154 fn genMulWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
1155 // No side effects, so if it's unreferenced, do nothing.
1156 if (inst.base.isUnused())
1157 return MCValue.dead;
1158 switch (arch) {
1159 else => return self.fail(inst.base.src, "TODO implement mulwrap for {}", .{self.target.cpu.arch}),
1160 }
1161 }
1162
11421163 fn genBitAnd(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
11431164 // No side effects, so if it's unreferenced, do nothing.
11441165 if (inst.base.isUnused())
......@@ -1392,6 +1413,15 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
13921413 }
13931414 }
13941415
1416 fn genSubWrap(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
1417 // No side effects, so if it's unreferenced, do nothing.
1418 if (inst.base.isUnused())
1419 return MCValue.dead;
1420 switch (arch) {
1421 else => return self.fail(inst.base.src, "TODO implement subwrap for {}", .{self.target.cpu.arch}),
1422 }
1423 }
1424
13951425 fn genArmBinOp(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst, op: ir.Inst.Tag) !MCValue {
13961426 const lhs = try self.resolveInst(op_lhs);
13971427 const rhs = try self.resolveInst(op_rhs);
src/ir.zig+6
......@@ -53,6 +53,7 @@ pub const Inst = struct {
5353
5454 pub const Tag = enum {
5555 add,
56 addwrap,
5657 alloc,
5758 arg,
5859 assembly,
......@@ -105,8 +106,10 @@ pub const Inst = struct {
105106 /// Write a value to a pointer. LHS is pointer, RHS is value.
106107 store,
107108 sub,
109 subwrap,
108110 unreach,
109111 mul,
112 mulwrap,
110113 not,
111114 floatcast,
112115 intcast,
......@@ -165,8 +168,11 @@ pub const Inst = struct {
165168 => UnOp,
166169
167170 .add,
171 .addwrap,
168172 .sub,
173 .subwrap,
169174 .mul,
175 .mulwrap,
170176 .cmp_lt,
171177 .cmp_lte,
172178 .cmp_eq,
src/zir.zig+6
......@@ -1680,8 +1680,11 @@ const DumpTzir = struct {
16801680 },
16811681
16821682 .add,
1683 .addwrap,
16831684 .sub,
1685 .subwrap,
16841686 .mul,
1687 .mulwrap,
16851688 .cmp_lt,
16861689 .cmp_lte,
16871690 .cmp_eq,
......@@ -1803,8 +1806,11 @@ const DumpTzir = struct {
18031806 },
18041807
18051808 .add,
1809 .addwrap,
18061810 .sub,
1811 .subwrap,
18071812 .mul,
1813 .mulwrap,
18081814 .cmp_lt,
18091815 .cmp_lte,
18101816 .cmp_eq,
src/zir_sema.zig+7-4
......@@ -2179,10 +2179,13 @@ fn zirArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!
21792179 }
21802180
21812181 const b = try mod.requireRuntimeBlock(scope, inst.base.src);
2182 const ir_tag = switch (inst.base.tag) {
2183 .add => Inst.Tag.add,
2184 .sub => Inst.Tag.sub,
2185 .mul => Inst.Tag.mul,
2182 const ir_tag: Inst.Tag = switch (inst.base.tag) {
2183 .add => .add,
2184 .addwrap => .addwrap,
2185 .sub => .sub,
2186 .subwrap => .subwrap,
2187 .mul => .mul,
2188 .mulwrap => .mulwrap,
21862189 else => return mod.fail(scope, inst.base.src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}),
21872190 };
21882191