authorgravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-21 19:02:27+01:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2021-03-21 20:32:39+01:00
log4cfd5f6a300a97778ec515319656c6f1c0ca6ec5
tree5d772d83baed4449be0c780d493ef0846b74dfb6
parent310a44d5be051a339b5739d59416a70604375541

astgen: implement simple binary operators


2 files changed, 27 insertions(+), 8 deletions(-)

src/astgen.zig+6-6
......@@ -1789,15 +1789,15 @@ fn simpleBinOp(
17891789 infix_node: ast.Node.Index,
17901790 op_inst_tag: zir.Inst.Tag,
17911791) InnerError!zir.Inst.Ref {
1792 if (true) @panic("TODO update for zir-memory-layout");
17931792 const tree = scope.tree();
17941793 const node_datas = tree.nodes.items(.data);
1795 const main_tokens = tree.nodes.items(.main_token);
17961794
1797 const lhs = try expr(mod, scope, .none, node_datas[infix_node].lhs);
1798 const rhs = try expr(mod, scope, .none, node_datas[infix_node].rhs);
1799 const result = try addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);
1800 return rvalue(mod, scope, rl, result);
1795 const gz = scope.getGenZir();
1796 const result = try gz.addPlNode(op_inst_tag, infix_node, zir.Inst.Bin{
1797 .lhs = try expr(mod, scope, .none, node_datas[infix_node].lhs),
1798 .rhs = try expr(mod, scope, .none, node_datas[infix_node].rhs),
1799 });
1800 return rvalue(mod, scope, rl, result, infix_node);
18011801}
18021802
18031803fn boolBinOp(
src/zir.zig+21-2
......@@ -392,8 +392,10 @@ pub const Inst = struct {
392392 /// These names are used directly as the instruction names in the text format.
393393 pub const Tag = enum {
394394 /// Arithmetic addition, asserts no integer overflow.
395 /// Uses the `pl_node` union field. Payload is `Bin`.
395396 add,
396397 /// Twos complement wrapping integer addition.
398 /// Uses the `pl_node` union field. Payload is `Bin`.
397399 addwrap,
398400 /// Allocates stack local memory.
399401 /// Uses the `un_node` union field. The operand is the type of the allocated object.
......@@ -411,8 +413,10 @@ pub const Inst = struct {
411413 /// Uses the `un_node` field. AST node is the `anyframe->T` syntax. Operand is the type.
412414 anyframe_type,
413415 /// Array concatenation. `a ++ b`
416 /// Uses the `pl_node` union field. Payload is `Bin`.
414417 array_cat,
415418 /// Array multiplication `a ** b`
419 /// Uses the `pl_node` union field. Payload is `Bin`.
416420 array_mul,
417421 /// `[N]T` syntax. No source location provided.
418422 /// Uses the `bin` union field. lhs is length, rhs is element type.
......@@ -468,13 +472,13 @@ pub const Inst = struct {
468472 /// Same as `block_flat` but additionally makes the inner instructions execute at comptime.
469473 block_comptime_flat,
470474 /// Boolean AND. See also `bit_and`.
471 /// Uses the `bin` field.
475 /// Uses the `pl_node` union field. Payload is `Bin`.
472476 bool_and,
473477 /// Boolean NOT. See also `bit_not`.
474478 /// Uses the `un_node` field.
475479 bool_not,
476480 /// Boolean OR. See also `bit_or`.
477 /// Uses the `bin` field.
481 /// Uses the `pl_node` union field. Payload is `Bin`.
478482 bool_or,
479483 /// Return a value from a block.
480484 /// Uses the `bin` union field: `lhs` is `Index` to the block (*not* `Ref`!),
......@@ -501,16 +505,22 @@ pub const Inst = struct {
501505 /// Uses the `un_node` field. Operand is callee. AST node is the function call.
502506 call_none,
503507 /// `<`
508 /// Uses the `pl_node` union field. Payload is `Bin`.
504509 cmp_lt,
505510 /// `<=`
511 /// Uses the `pl_node` union field. Payload is `Bin`.
506512 cmp_lte,
507513 /// `==`
514 /// Uses the `pl_node` union field. Payload is `Bin`.
508515 cmp_eq,
509516 /// `>=`
517 /// Uses the `pl_node` union field. Payload is `Bin`.
510518 cmp_gte,
511519 /// `>`
520 /// Uses the `pl_node` union field. Payload is `Bin`.
512521 cmp_gt,
513522 /// `!=`
523 /// Uses the `pl_node` union field. Payload is `Bin`.
514524 cmp_neq,
515525 /// Coerces a result location pointer to a new element type. It is evaluated "backwards"-
516526 /// as type coercion from the new element type to the old element type.
......@@ -543,6 +553,7 @@ pub const Inst = struct {
543553 /// Uses `un_node` field. AST node is the `x.*` syntax.
544554 deref_node,
545555 /// Arithmetic division. Asserts no integer overflow.
556 /// Uses the `pl_node` union field. Payload is `Bin`.
546557 div,
547558 /// Given a pointer to an array, slice, or pointer, returns a pointer to the element at
548559 /// the provided index. Uses the `bin` union field. Source location is implied
......@@ -646,10 +657,13 @@ pub const Inst = struct {
646657 /// Ambiguously remainder division or modulus. If the computation would possibly have
647658 /// a different value depending on whether the operation is remainder division or modulus,
648659 /// a compile error is emitted. Otherwise the computation is performed.
660 /// Uses the `pl_node` union field. Payload is `Bin`.
649661 mod_rem,
650662 /// Arithmetic multiplication. Asserts no integer overflow.
663 /// Uses the `pl_node` union field. Payload is `Bin`.
651664 mul,
652665 /// Twos complement wrapping integer multiplication.
666 /// Uses the `pl_node` union field. Payload is `Bin`.
653667 mulwrap,
654668 /// An await inside a nosuspend scope.
655669 nosuspend_await,
......@@ -697,8 +711,10 @@ pub const Inst = struct {
697711 /// Uses the `un_node` union field.
698712 set_eval_branch_quota,
699713 /// Integer shift-left. Zeroes are shifted in from the right hand side.
714 /// Uses the `pl_node` union field. Payload is `Bin`.
700715 shl,
701716 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
717 /// Uses the `pl_node` union field. Payload is `Bin`.
702718 shr,
703719 /// Create a pointer type that does not have a sentinel, alignment, or bit range specified.
704720 /// Uses the `ptr_type_simple` union field.
......@@ -734,8 +750,10 @@ pub const Inst = struct {
734750 /// Uses the `str` union field.
735751 str,
736752 /// Arithmetic subtraction. Asserts no integer overflow.
753 /// Uses the `pl_node` union field. Payload is `Bin`.
737754 sub,
738755 /// Twos complement wrapping integer subtraction.
756 /// Uses the `pl_node` union field. Payload is `Bin`.
739757 subwrap,
740758 /// Arithmetic negation. Asserts no integer overflow.
741759 /// Same as sub with a lhs of 0, split into a separate instruction to save memory.
......@@ -756,6 +774,7 @@ pub const Inst = struct {
756774 /// Uses the `unreachable` union field.
757775 @"unreachable",
758776 /// Bitwise XOR. `^`
777 /// Uses the `pl_node` union field. Payload is `Bin`.
759778 xor,
760779 /// Create an optional type '?T'
761780 /// Uses the `un_node` field.