authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-21 17:19:08+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-21 20:32:39+01:00
log310a44d5be051a339b5739d59416a70604375541
tree585f13adadeb49cd50ffd85f0343921ebc203a02
parent7800ae05a2e00eb289a6fb8589e132ce8d30fa18

zir: add negate/negate_wrap, implement astgen

These were previously implemented as a sub/sub_wrap instruction with a lhs of 0. Making this separate instructions however allows us to save some memory as there is no need to store a lhs.

4 files changed, 26 insertions(+), 13 deletions(-)

src/Module.zig+2
......@@ -1462,6 +1462,8 @@ pub const WipZirCode = struct {
14621462 .str,
14631463 .sub,
14641464 .subwrap,
1465 .negate,
1466 .negate_wrap,
14651467 .typeof,
14661468 .xor,
14671469 .optional_type,
src/Sema.zig+2
......@@ -156,6 +156,8 @@ pub fn analyzeBody(sema: *Sema, block: *Scope.Block, body: []const zir.Inst.Inde
156156 .addwrap => try sema.zirArithmetic(block, zir_inst),
157157 .sub => try sema.zirArithmetic(block, zir_inst),
158158 .subwrap => try sema.zirArithmetic(block, zir_inst),
159 .negate => @panic("TODO"),
160 .negate_wrap => @panic("TODO"),
159161 .mul => try sema.zirArithmetic(block, zir_inst),
160162 .mulwrap => try sema.zirArithmetic(block, zir_inst),
161163 .div => try sema.zirArithmetic(block, zir_inst),
src/astgen.zig+10-13
......@@ -373,12 +373,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
373373 .bool_and => return boolBinOp(mod, scope, rl, node, true),
374374 .bool_or => return boolBinOp(mod, scope, rl, node, false),
375375
376 .negation => @panic("TODO"),
377 .negation_wrap => @panic("TODO"),
378376 .bool_not => return boolNot(mod, scope, rl, node),
379377 .bit_not => return bitNot(mod, scope, rl, node),
380 //.negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),
381 //.negation_wrap => return rvalue(mod, scope, rl, try negation(mod, scope, node, .subwrap)),
378
379 .negation => return negation(mod, scope, rl, node, .negate),
380 .negation_wrap => return negation(mod, scope, rl, node, .negate_wrap),
382381
383382 .identifier => return identifier(mod, scope, rl, node),
384383
......@@ -1318,26 +1317,24 @@ fn bitNot(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) Inne
13181317
13191318 const gz = scope.getGenZir();
13201319 const operand = try expr(mod, scope, .none, node_datas[node].lhs);
1321 const result = try gz.addUnTok(.bit_not, operand, node);
1320 const result = try gz.addUnNode(.bit_not, operand, node);
13221321 return rvalue(mod, scope, rl, result, node);
13231322}
13241323
13251324fn negation(
13261325 mod: *Module,
13271326 scope: *Scope,
1327 rl: ResultLoc,
13281328 node: ast.Node.Index,
1329 op_inst_tag: zir.Inst.Tag,
1329 tag: zir.Inst.Tag,
13301330) InnerError!zir.Inst.Ref {
13311331 const tree = scope.tree();
13321332 const node_datas = tree.nodes.items(.data);
1333 const main_tokens = tree.nodes.items(.main_token);
13341333
1335 const lhs = try addZIRInstConst(mod, scope, src, .{
1336 .ty = Type.initTag(.comptime_int),
1337 .val = Value.initTag(.zero),
1338 });
1339 const rhs = try expr(mod, scope, .none, node_datas[node].lhs);
1340 return addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
1334 const gz = scope.getGenZir();
1335 const operand = try expr(mod, scope, .none, node_datas[node].lhs);
1336 const result = try gz.addUnNode(tag, operand, node);
1337 return rvalue(mod, scope, rl, result, node);
13411338}
13421339
13431340fn ptrType(
src/zir.zig+12
......@@ -737,6 +737,14 @@ pub const Inst = struct {
737737 sub,
738738 /// Twos complement wrapping integer subtraction.
739739 subwrap,
740 /// Arithmetic negation. Asserts no integer overflow.
741 /// Same as sub with a lhs of 0, split into a separate instruction to save memory.
742 /// Uses `un_node`.
743 negate,
744 /// Twos complement wrapping integer negation.
745 /// Same as subwrap with a lhs of 0, split into a separate instruction to save memory.
746 /// Uses `un_node`.
747 negate_wrap,
740748 /// Returns the type of a value.
741749 /// Uses the `un_tok` field.
742750 typeof,
......@@ -944,6 +952,8 @@ pub const Inst = struct {
944952 .str,
945953 .sub,
946954 .subwrap,
955 .negate,
956 .negate_wrap,
947957 .typeof,
948958 .xor,
949959 .optional_type,
......@@ -1341,6 +1351,8 @@ const Writer = struct {
13411351 .@"await",
13421352 .bit_not,
13431353 .bool_not,
1354 .negate,
1355 .negate_wrap,
13441356 .call_none,
13451357 .compile_error,
13461358 .deref_node,