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(...@@ -1789,15 +1789,15 @@ fn simpleBinOp(
1789 infix_node: ast.Node.Index,1789 infix_node: ast.Node.Index,
1790 op_inst_tag: zir.Inst.Tag,1790 op_inst_tag: zir.Inst.Tag,
1791) InnerError!zir.Inst.Ref {1791) InnerError!zir.Inst.Ref {
1792 if (true) @panic("TODO update for zir-memory-layout");
1793 const tree = scope.tree();1792 const tree = scope.tree();
1794 const node_datas = tree.nodes.items(.data);1793 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);1795 const gz = scope.getGenZir();
1798 const rhs = try expr(mod, scope, .none, node_datas[infix_node].rhs);1796 const result = try gz.addPlNode(op_inst_tag, infix_node, zir.Inst.Bin{
1799 const result = try addZIRBinOp(mod, scope, src, op_inst_tag, lhs, rhs);1797 .lhs = try expr(mod, scope, .none, node_datas[infix_node].lhs),
1800 return rvalue(mod, scope, rl, result);1798 .rhs = try expr(mod, scope, .none, node_datas[infix_node].rhs),
1799 });
1800 return rvalue(mod, scope, rl, result, infix_node);
1801}1801}
18021802
1803fn boolBinOp(1803fn boolBinOp(
src/zir.zig+21-2
...@@ -392,8 +392,10 @@ pub const Inst = struct {...@@ -392,8 +392,10 @@ pub const Inst = struct {
392 /// These names are used directly as the instruction names in the text format.392 /// These names are used directly as the instruction names in the text format.
393 pub const Tag = enum {393 pub const Tag = enum {
394 /// Arithmetic addition, asserts no integer overflow.394 /// Arithmetic addition, asserts no integer overflow.
395 /// Uses the `pl_node` union field. Payload is `Bin`.
395 add,396 add,
396 /// Twos complement wrapping integer addition.397 /// Twos complement wrapping integer addition.
398 /// Uses the `pl_node` union field. Payload is `Bin`.
397 addwrap,399 addwrap,
398 /// Allocates stack local memory.400 /// Allocates stack local memory.
399 /// Uses the `un_node` union field. The operand is the type of the allocated object.401 /// Uses the `un_node` union field. The operand is the type of the allocated object.
...@@ -411,8 +413,10 @@ pub const Inst = struct {...@@ -411,8 +413,10 @@ pub const Inst = struct {
411 /// Uses the `un_node` field. AST node is the `anyframe->T` syntax. Operand is the type.413 /// Uses the `un_node` field. AST node is the `anyframe->T` syntax. Operand is the type.
412 anyframe_type,414 anyframe_type,
413 /// Array concatenation. `a ++ b`415 /// Array concatenation. `a ++ b`
416 /// Uses the `pl_node` union field. Payload is `Bin`.
414 array_cat,417 array_cat,
415 /// Array multiplication `a ** b`418 /// Array multiplication `a ** b`
419 /// Uses the `pl_node` union field. Payload is `Bin`.
416 array_mul,420 array_mul,
417 /// `[N]T` syntax. No source location provided.421 /// `[N]T` syntax. No source location provided.
418 /// Uses the `bin` union field. lhs is length, rhs is element type.422 /// Uses the `bin` union field. lhs is length, rhs is element type.
...@@ -468,13 +472,13 @@ pub const Inst = struct {...@@ -468,13 +472,13 @@ pub const Inst = struct {
468 /// Same as `block_flat` but additionally makes the inner instructions execute at comptime.472 /// Same as `block_flat` but additionally makes the inner instructions execute at comptime.
469 block_comptime_flat,473 block_comptime_flat,
470 /// Boolean AND. See also `bit_and`.474 /// Boolean AND. See also `bit_and`.
471 /// Uses the `bin` field.475 /// Uses the `pl_node` union field. Payload is `Bin`.
472 bool_and,476 bool_and,
473 /// Boolean NOT. See also `bit_not`.477 /// Boolean NOT. See also `bit_not`.
474 /// Uses the `un_node` field.478 /// Uses the `un_node` field.
475 bool_not,479 bool_not,
476 /// Boolean OR. See also `bit_or`.480 /// Boolean OR. See also `bit_or`.
477 /// Uses the `bin` field.481 /// Uses the `pl_node` union field. Payload is `Bin`.
478 bool_or,482 bool_or,
479 /// Return a value from a block.483 /// Return a value from a block.
480 /// Uses the `bin` union field: `lhs` is `Index` to the block (*not* `Ref`!),484 /// Uses the `bin` union field: `lhs` is `Index` to the block (*not* `Ref`!),
...@@ -501,16 +505,22 @@ pub const Inst = struct {...@@ -501,16 +505,22 @@ pub const Inst = struct {
501 /// Uses the `un_node` field. Operand is callee. AST node is the function call.505 /// Uses the `un_node` field. Operand is callee. AST node is the function call.
502 call_none,506 call_none,
503 /// `<`507 /// `<`
508 /// Uses the `pl_node` union field. Payload is `Bin`.
504 cmp_lt,509 cmp_lt,
505 /// `<=`510 /// `<=`
511 /// Uses the `pl_node` union field. Payload is `Bin`.
506 cmp_lte,512 cmp_lte,
507 /// `==`513 /// `==`
514 /// Uses the `pl_node` union field. Payload is `Bin`.
508 cmp_eq,515 cmp_eq,
509 /// `>=`516 /// `>=`
517 /// Uses the `pl_node` union field. Payload is `Bin`.
510 cmp_gte,518 cmp_gte,
511 /// `>`519 /// `>`
520 /// Uses the `pl_node` union field. Payload is `Bin`.
512 cmp_gt,521 cmp_gt,
513 /// `!=`522 /// `!=`
523 /// Uses the `pl_node` union field. Payload is `Bin`.
514 cmp_neq,524 cmp_neq,
515 /// Coerces a result location pointer to a new element type. It is evaluated "backwards"-525 /// Coerces a result location pointer to a new element type. It is evaluated "backwards"-
516 /// as type coercion from the new element type to the old element type.526 /// as type coercion from the new element type to the old element type.
...@@ -543,6 +553,7 @@ pub const Inst = struct {...@@ -543,6 +553,7 @@ pub const Inst = struct {
543 /// Uses `un_node` field. AST node is the `x.*` syntax.553 /// Uses `un_node` field. AST node is the `x.*` syntax.
544 deref_node,554 deref_node,
545 /// Arithmetic division. Asserts no integer overflow.555 /// Arithmetic division. Asserts no integer overflow.
556 /// Uses the `pl_node` union field. Payload is `Bin`.
546 div,557 div,
547 /// Given a pointer to an array, slice, or pointer, returns a pointer to the element at558 /// Given a pointer to an array, slice, or pointer, returns a pointer to the element at
548 /// the provided index. Uses the `bin` union field. Source location is implied559 /// the provided index. Uses the `bin` union field. Source location is implied
...@@ -646,10 +657,13 @@ pub const Inst = struct {...@@ -646,10 +657,13 @@ pub const Inst = struct {
646 /// Ambiguously remainder division or modulus. If the computation would possibly have657 /// Ambiguously remainder division or modulus. If the computation would possibly have
647 /// a different value depending on whether the operation is remainder division or modulus,658 /// a different value depending on whether the operation is remainder division or modulus,
648 /// a compile error is emitted. Otherwise the computation is performed.659 /// a compile error is emitted. Otherwise the computation is performed.
660 /// Uses the `pl_node` union field. Payload is `Bin`.
649 mod_rem,661 mod_rem,
650 /// Arithmetic multiplication. Asserts no integer overflow.662 /// Arithmetic multiplication. Asserts no integer overflow.
663 /// Uses the `pl_node` union field. Payload is `Bin`.
651 mul,664 mul,
652 /// Twos complement wrapping integer multiplication.665 /// Twos complement wrapping integer multiplication.
666 /// Uses the `pl_node` union field. Payload is `Bin`.
653 mulwrap,667 mulwrap,
654 /// An await inside a nosuspend scope.668 /// An await inside a nosuspend scope.
655 nosuspend_await,669 nosuspend_await,
...@@ -697,8 +711,10 @@ pub const Inst = struct {...@@ -697,8 +711,10 @@ pub const Inst = struct {
697 /// Uses the `un_node` union field.711 /// Uses the `un_node` union field.
698 set_eval_branch_quota,712 set_eval_branch_quota,
699 /// Integer shift-left. Zeroes are shifted in from the right hand side.713 /// Integer shift-left. Zeroes are shifted in from the right hand side.
714 /// Uses the `pl_node` union field. Payload is `Bin`.
700 shl,715 shl,
701 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.716 /// Integer shift-right. Arithmetic or logical depending on the signedness of the integer type.
717 /// Uses the `pl_node` union field. Payload is `Bin`.
702 shr,718 shr,
703 /// Create a pointer type that does not have a sentinel, alignment, or bit range specified.719 /// Create a pointer type that does not have a sentinel, alignment, or bit range specified.
704 /// Uses the `ptr_type_simple` union field.720 /// Uses the `ptr_type_simple` union field.
...@@ -734,8 +750,10 @@ pub const Inst = struct {...@@ -734,8 +750,10 @@ pub const Inst = struct {
734 /// Uses the `str` union field.750 /// Uses the `str` union field.
735 str,751 str,
736 /// Arithmetic subtraction. Asserts no integer overflow.752 /// Arithmetic subtraction. Asserts no integer overflow.
753 /// Uses the `pl_node` union field. Payload is `Bin`.
737 sub,754 sub,
738 /// Twos complement wrapping integer subtraction.755 /// Twos complement wrapping integer subtraction.
756 /// Uses the `pl_node` union field. Payload is `Bin`.
739 subwrap,757 subwrap,
740 /// Arithmetic negation. Asserts no integer overflow.758 /// Arithmetic negation. Asserts no integer overflow.
741 /// Same as sub with a lhs of 0, split into a separate instruction to save memory.759 /// Same as sub with a lhs of 0, split into a separate instruction to save memory.
...@@ -756,6 +774,7 @@ pub const Inst = struct {...@@ -756,6 +774,7 @@ pub const Inst = struct {
756 /// Uses the `unreachable` union field.774 /// Uses the `unreachable` union field.
757 @"unreachable",775 @"unreachable",
758 /// Bitwise XOR. `^`776 /// Bitwise XOR. `^`
777 /// Uses the `pl_node` union field. Payload is `Bin`.
759 xor,778 xor,
760 /// Create an optional type '?T'779 /// Create an optional type '?T'
761 /// Uses the `un_node` field.780 /// Uses the `un_node` field.