authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-20 17:18:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-20 17:18:44-07:00
log260c610708451624eab783bc4e99388e2b28a3ba
tree1ef81a5625b822807037d5044b1afac2df961054
parent50010447bde42ca96aff63e0a620f941464f2eae

ZIR: move some un_tok tags to un_node instead

Idea here is to prefer un_node to un_tok in order to avoid unnecessary calls to `tree.firstToken`.

3 files changed, 57 insertions(+), 66 deletions(-)

src/Sema.zig+9-9
......@@ -1212,7 +1212,7 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
12121212 const tracy = trace(@src());
12131213 defer tracy.end();
12141214
1215 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1215 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
12161216 const src = inst_data.src();
12171217 const child_type = try sema.resolveType(block, src, inst_data.operand);
12181218 const opt_type = try sema.mod.optionalType(sema.arena, child_type);
......@@ -1224,7 +1224,7 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.I
12241224 const tracy = trace(@src());
12251225 defer tracy.end();
12261226
1227 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1227 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
12281228 const ptr = try sema.resolveInst(inst_data.operand);
12291229 const elem_ty = ptr.ty.elemType();
12301230 const opt_ty = try sema.mod.optionalType(sema.arena, elem_ty);
......@@ -1459,7 +1459,7 @@ fn zirOptionalPayloadPtr(
14591459 const tracy = trace(@src());
14601460 defer tracy.end();
14611461
1462 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1462 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
14631463 const optional_ptr = try sema.resolveInst(inst_data.operand);
14641464 assert(optional_ptr.ty.zigTypeTag() == .Pointer);
14651465 const src = inst_data.src();
......@@ -1502,7 +1502,7 @@ fn zirOptionalPayload(
15021502 const tracy = trace(@src());
15031503 defer tracy.end();
15041504
1505 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1505 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15061506 const src = inst_data.src();
15071507 const operand = try sema.resolveInst(inst_data.operand);
15081508 const opt_type = operand.ty;
......@@ -1540,7 +1540,7 @@ fn zirErrUnionPayload(
15401540 const tracy = trace(@src());
15411541 defer tracy.end();
15421542
1543 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1543 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15441544 const src = inst_data.src();
15451545 const operand = try sema.resolveInst(inst_data.operand);
15461546 if (operand.ty.zigTypeTag() != .ErrorUnion)
......@@ -1574,7 +1574,7 @@ fn zirErrUnionPayloadPtr(
15741574 const tracy = trace(@src());
15751575 defer tracy.end();
15761576
1577 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1577 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
15781578 const src = inst_data.src();
15791579 const operand = try sema.resolveInst(inst_data.operand);
15801580 assert(operand.ty.zigTypeTag() == .Pointer);
......@@ -1613,7 +1613,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
16131613 const tracy = trace(@src());
16141614 defer tracy.end();
16151615
1616 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1616 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
16171617 const src = inst_data.src();
16181618 const operand = try sema.resolveInst(inst_data.operand);
16191619 if (operand.ty.zigTypeTag() != .ErrorUnion)
......@@ -1637,7 +1637,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
16371637 const tracy = trace(@src());
16381638 defer tracy.end();
16391639
1640 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
1640 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
16411641 const src = inst_data.src();
16421642 const operand = try sema.resolveInst(inst_data.operand);
16431643 assert(operand.ty.zigTypeTag() == .Pointer);
......@@ -2710,7 +2710,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
27102710 const tracy = trace(@src());
27112711 defer tracy.end();
27122712
2713 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;
2713 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
27142714 const src = inst_data.src();
27152715 const uncasted_operand = try sema.resolveInst(inst_data.operand);
27162716
src/astgen.zig+21-30
......@@ -376,8 +376,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
376376
377377 .negation => @panic("TODO"),
378378 .negation_wrap => @panic("TODO"),
379 .bool_not => return rvalue(mod, scope, rl, try boolNot(mod, scope, node), node),
380 .bit_not => return rvalue(mod, scope, rl, try bitNot(mod, scope, node), node),
379 .bool_not => return boolNot(mod, scope, rl, node),
380 .bit_not => return bitNot(mod, scope, rl, node),
381381 //.negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),
382382 //.negation_wrap => return rvalue(mod, scope, rl, try negation(mod, scope, node, .subwrap)),
383383
......@@ -466,27 +466,21 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
466466 return rvalue(mod, scope, rl, result, node);
467467 },
468468 .optional_type => {
469 const src_token = tree.firstToken(node);
470
471469 const operand = try typeExpr(mod, scope, node_datas[node].lhs);
472 const result = try gz.addUnTok(.optional_type, operand, src_token);
470 const result = try gz.addUnNode(.optional_type, operand, node);
473471 return rvalue(mod, scope, rl, result, node);
474472 },
475 .unwrap_optional => {
476 const src_token = tree.firstToken(node);
477
478 switch (rl) {
479 .ref => return gz.addUnTok(
480 .optional_payload_safe_ptr,
481 try expr(mod, scope, .ref, node_datas[node].lhs),
482 src_token,
483 ),
484 else => return rvalue(mod, scope, rl, try gz.addUnTok(
485 .optional_payload_safe,
486 try expr(mod, scope, .none, node_datas[node].lhs),
487 src_token,
488 ), node),
489 }
473 .unwrap_optional => switch (rl) {
474 .ref => return gz.addUnNode(
475 .optional_payload_safe_ptr,
476 try expr(mod, scope, .ref, node_datas[node].lhs),
477 node,
478 ),
479 else => return rvalue(mod, scope, rl, try gz.addUnNode(
480 .optional_payload_safe,
481 try expr(mod, scope, .none, node_datas[node].lhs),
482 node,
483 ), node),
490484 },
491485 .block_two, .block_two_semicolon => {
492486 const statements = [2]ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
......@@ -1315,27 +1309,24 @@ fn assignOp(
13151309 _ = try addZIRBinOp(mod, scope, src, .store, lhs_ptr, result);
13161310}
13171311
1318fn boolNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
1312fn boolNot(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
13191313 const tree = scope.tree();
13201314 const node_datas = tree.nodes.items(.data);
1321 const src_token = tree.firstToken(node);
1322
1323 const gz = scope.getGenZir();
13241315
13251316 const operand = try expr(mod, scope, .{ .ty = @enumToInt(zir.Const.bool_type) }, node_datas[node].lhs);
1326
1327 return gz.addUnTok(.bool_not, operand, src_token);
1317 const gz = scope.getGenZir();
1318 const result = try gz.addUnNode(.bool_not, operand, node);
1319 return rvalue(mod, scope, rl, result, node);
13281320}
13291321
1330fn bitNot(mod: *Module, scope: *Scope, node: ast.Node.Index) InnerError!zir.Inst.Ref {
1322fn bitNot(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) InnerError!zir.Inst.Ref {
13311323 const tree = scope.tree();
13321324 const node_datas = tree.nodes.items(.data);
1333 const src_token = tree.firstToken(node);
13341325
13351326 const gz = scope.getGenZir();
1336
13371327 const operand = try expr(mod, scope, .none, node_datas[node].lhs);
1338 return gz.addUnTok(.bit_not, operand, src_token);
1328 const result = try gz.addUnTok(.bit_not, operand, node);
1329 return rvalue(mod, scope, rl, result, node);
13391330}
13401331
13411332fn negation(
src/zir.zig+27-27
......@@ -445,7 +445,7 @@ pub const Inst = struct {
445445 /// The new result location pointer has an inferred type.
446446 bitcast_result_ptr,
447447 /// Bitwise NOT. `~`
448 /// uses `un_tok`
448 /// Uses `un_node`.
449449 bit_not,
450450 /// Bitwise OR. `|`
451451 bit_or,
......@@ -464,7 +464,7 @@ pub const Inst = struct {
464464 /// Uses the `bin` field.
465465 bool_and,
466466 /// Boolean NOT. See also `bit_not`.
467 /// Uses the `un_tok` field.
467 /// Uses the `un_node` field.
468468 bool_not,
469469 /// Boolean OR. See also `bit_or`.
470470 /// Uses the `bin` field.
......@@ -749,57 +749,57 @@ pub const Inst = struct {
749749 /// Bitwise XOR. `^`
750750 xor,
751751 /// Create an optional type '?T'
752 /// Uses the `un_tok` field.
752 /// Uses the `un_node` field.
753753 optional_type,
754754 /// Create an optional type '?T'. The operand is a pointer value. The optional type will
755755 /// be the type of the pointer element, wrapped in an optional.
756 /// Uses the `un_tok` field.
756 /// Uses the `un_node` field.
757757 optional_type_from_ptr_elem,
758758 /// ?T => T with safety.
759759 /// Given an optional value, returns the payload value, with a safety check that
760760 /// the value is non-null. Used for `orelse`, `if` and `while`.
761 /// Uses the `un_tok` field.
761 /// Uses the `un_node` field.
762762 optional_payload_safe,
763763 /// ?T => T without safety.
764764 /// Given an optional value, returns the payload value. No safety checks.
765 /// Uses the `un_tok` field.
765 /// Uses the `un_node` field.
766766 optional_payload_unsafe,
767767 /// *?T => *T with safety.
768768 /// Given a pointer to an optional value, returns a pointer to the payload value,
769769 /// with a safety check that the value is non-null. Used for `orelse`, `if` and `while`.
770 /// Uses the `un_tok` field.
770 /// Uses the `un_node` field.
771771 optional_payload_safe_ptr,
772772 /// *?T => *T without safety.
773773 /// Given a pointer to an optional value, returns a pointer to the payload value.
774774 /// No safety checks.
775 /// Uses the `un_tok` field.
775 /// Uses the `un_node` field.
776776 optional_payload_unsafe_ptr,
777777 /// E!T => T with safety.
778778 /// Given an error union value, returns the payload value, with a safety check
779779 /// that the value is not an error. Used for catch, if, and while.
780 /// Uses the `un_tok` field.
780 /// Uses the `un_node` field.
781781 err_union_payload_safe,
782782 /// E!T => T without safety.
783783 /// Given an error union value, returns the payload value. No safety checks.
784 /// Uses the `un_tok` field.
784 /// Uses the `un_node` field.
785785 err_union_payload_unsafe,
786786 /// *E!T => *T with safety.
787787 /// Given a pointer to an error union value, returns a pointer to the payload value,
788788 /// with a safety check that the value is not an error. Used for catch, if, and while.
789 /// Uses the `un_tok` field.
789 /// Uses the `un_node` field.
790790 err_union_payload_safe_ptr,
791791 /// *E!T => *T without safety.
792792 /// Given a pointer to a error union value, returns a pointer to the payload value.
793793 /// No safety checks.
794 /// Uses the `un_tok` field.
794 /// Uses the `un_node` field.
795795 err_union_payload_unsafe_ptr,
796796 /// E!T => E without safety.
797797 /// Given an error union value, returns the error code. No safety checks.
798 /// Uses the `un_tok` field.
798 /// Uses the `un_node` field.
799799 err_union_code,
800800 /// *E!T => E without safety.
801801 /// Given a pointer to an error union value, returns the error code. No safety checks.
802 /// Uses the `un_tok` field.
802 /// Uses the `un_node` field.
803803 err_union_code_ptr,
804804 /// Takes a *E!T and raises a compiler error if T != void
805805 /// Uses the `un_tok` field.
......@@ -1326,6 +1326,7 @@ const Writer = struct {
13261326 .indexable_ptr_len,
13271327 .@"await",
13281328 .bit_not,
1329 .bool_not,
13291330 .call_none,
13301331 .compile_error,
13311332 .deref_node,
......@@ -1337,9 +1338,20 @@ const Writer = struct {
13371338 .set_eval_branch_quota,
13381339 .resolve_inferred_alloc,
13391340 .suspend_block_one,
1341 .optional_type,
1342 .optional_type_from_ptr_elem,
1343 .optional_payload_safe,
1344 .optional_payload_unsafe,
1345 .optional_payload_safe_ptr,
1346 .optional_payload_unsafe_ptr,
1347 .err_union_payload_safe,
1348 .err_union_payload_unsafe,
1349 .err_union_payload_safe_ptr,
1350 .err_union_payload_unsafe_ptr,
1351 .err_union_code,
1352 .err_union_code_ptr,
13401353 => try self.writeUnNode(stream, inst),
13411354
1342 .bool_not,
13431355 .break_void_tok,
13441356 .is_non_null,
13451357 .is_null,
......@@ -1351,18 +1363,6 @@ const Writer = struct {
13511363 .ret_tok,
13521364 .ret_coerce,
13531365 .typeof,
1354 .optional_type,
1355 .optional_type_from_ptr_elem,
1356 .optional_payload_safe,
1357 .optional_payload_unsafe,
1358 .optional_payload_safe_ptr,
1359 .optional_payload_unsafe_ptr,
1360 .err_union_payload_safe,
1361 .err_union_payload_unsafe,
1362 .err_union_payload_safe_ptr,
1363 .err_union_payload_unsafe_ptr,
1364 .err_union_code,
1365 .err_union_code_ptr,
13661366 .ensure_err_payload_void,
13671367 => try self.writeUnTok(stream, inst),
13681368