authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-08-22 15:48:41+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-24 15:36:42-07:00
logd62c12e077e4e3993864317bf8af5f0fcc631515
tree996c9faa447460ab4caff697faf4b5b9122c7d6d
parentfd9f509d6dcad43f5b1bf17e57b1f0b200755df8

stage2: astgen prefix ops


3 files changed, 29 insertions(+), 4 deletions(-)

src-self-hosted/astgen.zig+26-4
......@@ -232,6 +232,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
232232 .BoolAnd => return boolBinOp(mod, scope, rl, node.castTag(.BoolAnd).?),
233233 .BoolOr => return boolBinOp(mod, scope, rl, node.castTag(.BoolOr).?),
234234
235 .BoolNot => return rlWrap(mod, scope, rl, try boolNot(mod, scope, node.castTag(.BoolNot).?)),
236 .BitNot => return rlWrap(mod, scope, rl, try bitNot(mod, scope, node.castTag(.BitNot).?)),
237 .Negation => return rlWrap(mod, scope, rl, try negation(mod, scope, node.castTag(.Negation).?, .sub)),
238 .NegationWrap => return rlWrap(mod, scope, rl, try negation(mod, scope, node.castTag(.NegationWrap).?, .subwrap)),
239
235240 .Identifier => return try identifier(mod, scope, rl, node.castTag(.Identifier).?),
236241 .Asm => return rlWrap(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)),
237242 .StringLiteral => return rlWrap(mod, scope, rl, try stringLiteral(mod, scope, node.castTag(.StringLiteral).?)),
......@@ -244,7 +249,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
244249 .While => return whileExpr(mod, scope, rl, node.castTag(.While).?),
245250 .Period => return rlWrap(mod, scope, rl, try field(mod, scope, node.castTag(.Period).?)),
246251 .Deref => return rlWrap(mod, scope, rl, try deref(mod, scope, node.castTag(.Deref).?)),
247 .BoolNot => return rlWrap(mod, scope, rl, try boolNot(mod, scope, node.castTag(.BoolNot).?)),
248252 .AddressOf => return rlWrap(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),
249253 .FloatLiteral => return rlWrap(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),
250254 .UndefinedLiteral => return rlWrap(mod, scope, rl, try undefLiteral(mod, scope, node.castTag(.UndefinedLiteral).?)),
......@@ -271,9 +275,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
271275 .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}),
272276 .OrElse => return mod.failNode(scope, node, "TODO implement astgen.expr for .OrElse", .{}),
273277 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),
274 .BitNot => return mod.failNode(scope, node, "TODO implement astgen.expr for .BitNot", .{}),
275 .Negation => return mod.failNode(scope, node, "TODO implement astgen.expr for .Negation", .{}),
276 .NegationWrap => return mod.failNode(scope, node, "TODO implement astgen.expr for .NegationWrap", .{}),
277278 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),
278279 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),
279280 .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}),
......@@ -554,6 +555,27 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr
554555 return addZIRUnOp(mod, scope, src, .boolnot, operand);
555556}
556557
558fn bitNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
559 const tree = scope.tree();
560 const src = tree.token_locs[node.op_token].start;
561 const operand = try expr(mod, scope, .none, node.rhs);
562 return addZIRUnOp(mod, scope, src, .bitnot, operand);
563}
564
565fn negation(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp, op_inst_tag: zir.Inst.Tag) InnerError!*zir.Inst {
566 const tree = scope.tree();
567 const src = tree.token_locs[node.op_token].start;
568
569 const lhs = addZIRInstConst(mod, scope, src, .{
570 .ty = Type.initTag(.comptime_int),
571 .val = Value.initTag(.zero),
572 });
573 const rhs = try expr(mod, scope, .none, node.rhs);
574
575 const result = try addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
576 return rlWrap(mod, scope, rl, result);
577}
578
557579fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
558580 return expr(mod, scope, .ref, node.rhs);
559581}
src-self-hosted/zir.zig+2
......@@ -70,6 +70,8 @@ pub const Inst = struct {
7070 /// A typed result location pointer is bitcasted to a new result location pointer.
7171 /// The new result location pointer has an inferred type.
7272 bitcast_result_ptr,
73 /// Bitwise NOT. `~`
74 bitnot,
7375 /// Bitwise OR. `|`
7476 bitor,
7577 /// A labeled block of code, which can return a value.
src-self-hosted/zir_sema.zig+1
......@@ -97,6 +97,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
9797 .array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?),
9898 .array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?),
9999 .bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?),
100 .bitnot => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitnot).?),
100101 .bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?),
101102 .xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?),
102103 .shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?),