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...@@ -232,6 +232,11 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
232 .BoolAnd => return boolBinOp(mod, scope, rl, node.castTag(.BoolAnd).?),232 .BoolAnd => return boolBinOp(mod, scope, rl, node.castTag(.BoolAnd).?),
233 .BoolOr => return boolBinOp(mod, scope, rl, node.castTag(.BoolOr).?),233 .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
235 .Identifier => return try identifier(mod, scope, rl, node.castTag(.Identifier).?),240 .Identifier => return try identifier(mod, scope, rl, node.castTag(.Identifier).?),
236 .Asm => return rlWrap(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)),241 .Asm => return rlWrap(mod, scope, rl, try assembly(mod, scope, node.castTag(.Asm).?)),
237 .StringLiteral => return rlWrap(mod, scope, rl, try stringLiteral(mod, scope, node.castTag(.StringLiteral).?)),242 .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...@@ -244,7 +249,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
244 .While => return whileExpr(mod, scope, rl, node.castTag(.While).?),249 .While => return whileExpr(mod, scope, rl, node.castTag(.While).?),
245 .Period => return rlWrap(mod, scope, rl, try field(mod, scope, node.castTag(.Period).?)),250 .Period => return rlWrap(mod, scope, rl, try field(mod, scope, node.castTag(.Period).?)),
246 .Deref => return rlWrap(mod, scope, rl, try deref(mod, scope, node.castTag(.Deref).?)),251 .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).?)),
248 .AddressOf => return rlWrap(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),252 .AddressOf => return rlWrap(mod, scope, rl, try addressOf(mod, scope, node.castTag(.AddressOf).?)),
249 .FloatLiteral => return rlWrap(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),253 .FloatLiteral => return rlWrap(mod, scope, rl, try floatLiteral(mod, scope, node.castTag(.FloatLiteral).?)),
250 .UndefinedLiteral => return rlWrap(mod, scope, rl, try undefLiteral(mod, scope, node.castTag(.UndefinedLiteral).?)),254 .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...@@ -271,9 +275,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: *ast.Node) InnerEr
271 .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}),275 .Range => return mod.failNode(scope, node, "TODO implement astgen.expr for .Range", .{}),
272 .OrElse => return mod.failNode(scope, node, "TODO implement astgen.expr for .OrElse", .{}),276 .OrElse => return mod.failNode(scope, node, "TODO implement astgen.expr for .OrElse", .{}),
273 .Await => return mod.failNode(scope, node, "TODO implement astgen.expr for .Await", .{}),277 .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", .{}),
277 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),278 .Resume => return mod.failNode(scope, node, "TODO implement astgen.expr for .Resume", .{}),
278 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),279 .Try => return mod.failNode(scope, node, "TODO implement astgen.expr for .Try", .{}),
279 .Slice => return mod.failNode(scope, node, "TODO implement astgen.expr for .Slice", .{}),280 .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...@@ -554,6 +555,27 @@ fn boolNot(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerErr
554 return addZIRUnOp(mod, scope, src, .boolnot, operand);555 return addZIRUnOp(mod, scope, src, .boolnot, operand);
555}556}
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
557fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {579fn addressOf(mod: *Module, scope: *Scope, node: *ast.Node.SimplePrefixOp) InnerError!*zir.Inst {
558 return expr(mod, scope, .ref, node.rhs);580 return expr(mod, scope, .ref, node.rhs);
559}581}
src-self-hosted/zir.zig+2
...@@ -70,6 +70,8 @@ pub const Inst = struct {...@@ -70,6 +70,8 @@ pub const Inst = struct {
70 /// A typed result location pointer is bitcasted to a new result location pointer.70 /// A typed result location pointer is bitcasted to a new result location pointer.
71 /// The new result location pointer has an inferred type.71 /// The new result location pointer has an inferred type.
72 bitcast_result_ptr,72 bitcast_result_ptr,
73 /// Bitwise NOT. `~`
74 bitnot,
73 /// Bitwise OR. `|`75 /// Bitwise OR. `|`
74 bitor,76 bitor,
75 /// A labeled block of code, which can return a value.77 /// 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!...@@ -97,6 +97,7 @@ pub fn analyzeInst(mod: *Module, scope: *Scope, old_inst: *zir.Inst) InnerError!
97 .array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?),97 .array_cat => return analyzeInstArrayCat(mod, scope, old_inst.castTag(.array_cat).?),
98 .array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?),98 .array_mul => return analyzeInstArrayMul(mod, scope, old_inst.castTag(.array_mul).?),
99 .bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?),99 .bitand => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitand).?),
100 .bitnot => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitnot).?),
100 .bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?),101 .bitor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.bitor).?),
101 .xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?),102 .xor => return analyzeInstBitwise(mod, scope, old_inst.castTag(.xor).?),
102 .shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?),103 .shl => return analyzeInstShl(mod, scope, old_inst.castTag(.shl).?),