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...@@ -1212,7 +1212,7 @@ fn zirOptionalType(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
1212 const tracy = trace(@src());1212 const tracy = trace(@src());
1213 defer tracy.end();1213 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;
1216 const src = inst_data.src();1216 const src = inst_data.src();
1217 const child_type = try sema.resolveType(block, src, inst_data.operand);1217 const child_type = try sema.resolveType(block, src, inst_data.operand);
1218 const opt_type = try sema.mod.optionalType(sema.arena, child_type);1218 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...@@ -1224,7 +1224,7 @@ fn zirOptionalTypeFromPtrElem(sema: *Sema, block: *Scope.Block, inst: zir.Inst.I
1224 const tracy = trace(@src());1224 const tracy = trace(@src());
1225 defer tracy.end();1225 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;
1228 const ptr = try sema.resolveInst(inst_data.operand);1228 const ptr = try sema.resolveInst(inst_data.operand);
1229 const elem_ty = ptr.ty.elemType();1229 const elem_ty = ptr.ty.elemType();
1230 const opt_ty = try sema.mod.optionalType(sema.arena, elem_ty);1230 const opt_ty = try sema.mod.optionalType(sema.arena, elem_ty);
...@@ -1459,7 +1459,7 @@ fn zirOptionalPayloadPtr(...@@ -1459,7 +1459,7 @@ fn zirOptionalPayloadPtr(
1459 const tracy = trace(@src());1459 const tracy = trace(@src());
1460 defer tracy.end();1460 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;
1463 const optional_ptr = try sema.resolveInst(inst_data.operand);1463 const optional_ptr = try sema.resolveInst(inst_data.operand);
1464 assert(optional_ptr.ty.zigTypeTag() == .Pointer);1464 assert(optional_ptr.ty.zigTypeTag() == .Pointer);
1465 const src = inst_data.src();1465 const src = inst_data.src();
...@@ -1502,7 +1502,7 @@ fn zirOptionalPayload(...@@ -1502,7 +1502,7 @@ fn zirOptionalPayload(
1502 const tracy = trace(@src());1502 const tracy = trace(@src());
1503 defer tracy.end();1503 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;
1506 const src = inst_data.src();1506 const src = inst_data.src();
1507 const operand = try sema.resolveInst(inst_data.operand);1507 const operand = try sema.resolveInst(inst_data.operand);
1508 const opt_type = operand.ty;1508 const opt_type = operand.ty;
...@@ -1540,7 +1540,7 @@ fn zirErrUnionPayload(...@@ -1540,7 +1540,7 @@ fn zirErrUnionPayload(
1540 const tracy = trace(@src());1540 const tracy = trace(@src());
1541 defer tracy.end();1541 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;
1544 const src = inst_data.src();1544 const src = inst_data.src();
1545 const operand = try sema.resolveInst(inst_data.operand);1545 const operand = try sema.resolveInst(inst_data.operand);
1546 if (operand.ty.zigTypeTag() != .ErrorUnion)1546 if (operand.ty.zigTypeTag() != .ErrorUnion)
...@@ -1574,7 +1574,7 @@ fn zirErrUnionPayloadPtr(...@@ -1574,7 +1574,7 @@ fn zirErrUnionPayloadPtr(
1574 const tracy = trace(@src());1574 const tracy = trace(@src());
1575 defer tracy.end();1575 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;
1578 const src = inst_data.src();1578 const src = inst_data.src();
1579 const operand = try sema.resolveInst(inst_data.operand);1579 const operand = try sema.resolveInst(inst_data.operand);
1580 assert(operand.ty.zigTypeTag() == .Pointer);1580 assert(operand.ty.zigTypeTag() == .Pointer);
...@@ -1613,7 +1613,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner...@@ -1613,7 +1613,7 @@ fn zirErrUnionCode(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) Inner
1613 const tracy = trace(@src());1613 const tracy = trace(@src());
1614 defer tracy.end();1614 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;
1617 const src = inst_data.src();1617 const src = inst_data.src();
1618 const operand = try sema.resolveInst(inst_data.operand);1618 const operand = try sema.resolveInst(inst_data.operand);
1619 if (operand.ty.zigTypeTag() != .ErrorUnion)1619 if (operand.ty.zigTypeTag() != .ErrorUnion)
...@@ -1637,7 +1637,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In...@@ -1637,7 +1637,7 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) In
1637 const tracy = trace(@src());1637 const tracy = trace(@src());
1638 defer tracy.end();1638 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;
1641 const src = inst_data.src();1641 const src = inst_data.src();
1642 const operand = try sema.resolveInst(inst_data.operand);1642 const operand = try sema.resolveInst(inst_data.operand);
1643 assert(operand.ty.zigTypeTag() == .Pointer);1643 assert(operand.ty.zigTypeTag() == .Pointer);
...@@ -2710,7 +2710,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError...@@ -2710,7 +2710,7 @@ fn zirBoolNot(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError
2710 const tracy = trace(@src());2710 const tracy = trace(@src());
2711 defer tracy.end();2711 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;
2714 const src = inst_data.src();2714 const src = inst_data.src();
2715 const uncasted_operand = try sema.resolveInst(inst_data.operand);2715 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...@@ -376,8 +376,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
376376
377 .negation => @panic("TODO"),377 .negation => @panic("TODO"),
378 .negation_wrap => @panic("TODO"),378 .negation_wrap => @panic("TODO"),
379 .bool_not => return rvalue(mod, scope, rl, try boolNot(mod, scope, node), node),379 .bool_not => return boolNot(mod, scope, rl, node),
380 .bit_not => return rvalue(mod, scope, rl, try bitNot(mod, scope, node), node),380 .bit_not => return bitNot(mod, scope, rl, node),
381 //.negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),381 //.negation => return rvalue(mod, scope, rl, try negation(mod, scope, node, .sub)),
382 //.negation_wrap => return rvalue(mod, scope, rl, try negation(mod, scope, node, .subwrap)),382 //.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...@@ -466,27 +466,21 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
466 return rvalue(mod, scope, rl, result, node);466 return rvalue(mod, scope, rl, result, node);
467 },467 },
468 .optional_type => {468 .optional_type => {
469 const src_token = tree.firstToken(node);
470
471 const operand = try typeExpr(mod, scope, node_datas[node].lhs);469 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);
473 return rvalue(mod, scope, rl, result, node);471 return rvalue(mod, scope, rl, result, node);
474 },472 },
475 .unwrap_optional => {473 .unwrap_optional => switch (rl) {
476 const src_token = tree.firstToken(node);474 .ref => return gz.addUnNode(
477475 .optional_payload_safe_ptr,
478 switch (rl) {476 try expr(mod, scope, .ref, node_datas[node].lhs),
479 .ref => return gz.addUnTok(477 node,
480 .optional_payload_safe_ptr,478 ),
481 try expr(mod, scope, .ref, node_datas[node].lhs),479 else => return rvalue(mod, scope, rl, try gz.addUnNode(
482 src_token,480 .optional_payload_safe,
483 ),481 try expr(mod, scope, .none, node_datas[node].lhs),
484 else => return rvalue(mod, scope, rl, try gz.addUnTok(482 node,
485 .optional_payload_safe,483 ), node),
486 try expr(mod, scope, .none, node_datas[node].lhs),
487 src_token,
488 ), node),
489 }
490 },484 },
491 .block_two, .block_two_semicolon => {485 .block_two, .block_two_semicolon => {
492 const statements = [2]ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };486 const statements = [2]ast.Node.Index{ node_datas[node].lhs, node_datas[node].rhs };
...@@ -1315,27 +1309,24 @@ fn assignOp(...@@ -1315,27 +1309,24 @@ fn assignOp(
1315 _ = try addZIRBinOp(mod, scope, src, .store, lhs_ptr, result);1309 _ = try addZIRBinOp(mod, scope, src, .store, lhs_ptr, result);
1316}1310}
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 {
1319 const tree = scope.tree();1313 const tree = scope.tree();
1320 const node_datas = tree.nodes.items(.data);1314 const node_datas = tree.nodes.items(.data);
1321 const src_token = tree.firstToken(node);
1322
1323 const gz = scope.getGenZir();
13241315
1325 const operand = try expr(mod, scope, .{ .ty = @enumToInt(zir.Const.bool_type) }, node_datas[node].lhs);1316 const operand = try expr(mod, scope, .{ .ty = @enumToInt(zir.Const.bool_type) }, node_datas[node].lhs);
13261317 const gz = scope.getGenZir();
1327 return gz.addUnTok(.bool_not, operand, src_token);1318 const result = try gz.addUnNode(.bool_not, operand, node);
1319 return rvalue(mod, scope, rl, result, node);
1328}1320}
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 {
1331 const tree = scope.tree();1323 const tree = scope.tree();
1332 const node_datas = tree.nodes.items(.data);1324 const node_datas = tree.nodes.items(.data);
1333 const src_token = tree.firstToken(node);
13341325
1335 const gz = scope.getGenZir();1326 const gz = scope.getGenZir();
1336
1337 const operand = try expr(mod, scope, .none, node_datas[node].lhs);1327 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);
1339}1330}
13401331
1341fn negation(1332fn negation(
src/zir.zig+27-27
...@@ -445,7 +445,7 @@ pub const Inst = struct {...@@ -445,7 +445,7 @@ pub const Inst = struct {
445 /// The new result location pointer has an inferred type.445 /// The new result location pointer has an inferred type.
446 bitcast_result_ptr,446 bitcast_result_ptr,
447 /// Bitwise NOT. `~`447 /// Bitwise NOT. `~`
448 /// uses `un_tok`448 /// Uses `un_node`.
449 bit_not,449 bit_not,
450 /// Bitwise OR. `|`450 /// Bitwise OR. `|`
451 bit_or,451 bit_or,
...@@ -464,7 +464,7 @@ pub const Inst = struct {...@@ -464,7 +464,7 @@ pub const Inst = struct {
464 /// Uses the `bin` field.464 /// Uses the `bin` field.
465 bool_and,465 bool_and,
466 /// Boolean NOT. See also `bit_not`.466 /// Boolean NOT. See also `bit_not`.
467 /// Uses the `un_tok` field.467 /// Uses the `un_node` field.
468 bool_not,468 bool_not,
469 /// Boolean OR. See also `bit_or`.469 /// Boolean OR. See also `bit_or`.
470 /// Uses the `bin` field.470 /// Uses the `bin` field.
...@@ -749,57 +749,57 @@ pub const Inst = struct {...@@ -749,57 +749,57 @@ pub const Inst = struct {
749 /// Bitwise XOR. `^`749 /// Bitwise XOR. `^`
750 xor,750 xor,
751 /// Create an optional type '?T'751 /// Create an optional type '?T'
752 /// Uses the `un_tok` field.752 /// Uses the `un_node` field.
753 optional_type,753 optional_type,
754 /// Create an optional type '?T'. The operand is a pointer value. The optional type will754 /// Create an optional type '?T'. The operand is a pointer value. The optional type will
755 /// be the type of the pointer element, wrapped in an optional.755 /// be the type of the pointer element, wrapped in an optional.
756 /// Uses the `un_tok` field.756 /// Uses the `un_node` field.
757 optional_type_from_ptr_elem,757 optional_type_from_ptr_elem,
758 /// ?T => T with safety.758 /// ?T => T with safety.
759 /// Given an optional value, returns the payload value, with a safety check that759 /// Given an optional value, returns the payload value, with a safety check that
760 /// the value is non-null. Used for `orelse`, `if` and `while`.760 /// the value is non-null. Used for `orelse`, `if` and `while`.
761 /// Uses the `un_tok` field.761 /// Uses the `un_node` field.
762 optional_payload_safe,762 optional_payload_safe,
763 /// ?T => T without safety.763 /// ?T => T without safety.
764 /// Given an optional value, returns the payload value. No safety checks.764 /// Given an optional value, returns the payload value. No safety checks.
765 /// Uses the `un_tok` field.765 /// Uses the `un_node` field.
766 optional_payload_unsafe,766 optional_payload_unsafe,
767 /// *?T => *T with safety.767 /// *?T => *T with safety.
768 /// Given a pointer to an optional value, returns a pointer to the payload value,768 /// Given a pointer to an optional value, returns a pointer to the payload value,
769 /// with a safety check that the value is non-null. Used for `orelse`, `if` and `while`.769 /// 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.
771 optional_payload_safe_ptr,771 optional_payload_safe_ptr,
772 /// *?T => *T without safety.772 /// *?T => *T without safety.
773 /// Given a pointer to an optional value, returns a pointer to the payload value.773 /// Given a pointer to an optional value, returns a pointer to the payload value.
774 /// No safety checks.774 /// No safety checks.
775 /// Uses the `un_tok` field.775 /// Uses the `un_node` field.
776 optional_payload_unsafe_ptr,776 optional_payload_unsafe_ptr,
777 /// E!T => T with safety.777 /// E!T => T with safety.
778 /// Given an error union value, returns the payload value, with a safety check778 /// Given an error union value, returns the payload value, with a safety check
779 /// that the value is not an error. Used for catch, if, and while.779 /// 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.
781 err_union_payload_safe,781 err_union_payload_safe,
782 /// E!T => T without safety.782 /// E!T => T without safety.
783 /// Given an error union value, returns the payload value. No safety checks.783 /// Given an error union value, returns the payload value. No safety checks.
784 /// Uses the `un_tok` field.784 /// Uses the `un_node` field.
785 err_union_payload_unsafe,785 err_union_payload_unsafe,
786 /// *E!T => *T with safety.786 /// *E!T => *T with safety.
787 /// Given a pointer to an error union value, returns a pointer to the payload value,787 /// Given a pointer to an error union value, returns a pointer to the payload value,
788 /// with a safety check that the value is not an error. Used for catch, if, and while.788 /// 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.
790 err_union_payload_safe_ptr,790 err_union_payload_safe_ptr,
791 /// *E!T => *T without safety.791 /// *E!T => *T without safety.
792 /// Given a pointer to a error union value, returns a pointer to the payload value.792 /// Given a pointer to a error union value, returns a pointer to the payload value.
793 /// No safety checks.793 /// No safety checks.
794 /// Uses the `un_tok` field.794 /// Uses the `un_node` field.
795 err_union_payload_unsafe_ptr,795 err_union_payload_unsafe_ptr,
796 /// E!T => E without safety.796 /// E!T => E without safety.
797 /// Given an error union value, returns the error code. No safety checks.797 /// Given an error union value, returns the error code. No safety checks.
798 /// Uses the `un_tok` field.798 /// Uses the `un_node` field.
799 err_union_code,799 err_union_code,
800 /// *E!T => E without safety.800 /// *E!T => E without safety.
801 /// Given a pointer to an error union value, returns the error code. No safety checks.801 /// 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.
803 err_union_code_ptr,803 err_union_code_ptr,
804 /// Takes a *E!T and raises a compiler error if T != void804 /// Takes a *E!T and raises a compiler error if T != void
805 /// Uses the `un_tok` field.805 /// Uses the `un_tok` field.
...@@ -1326,6 +1326,7 @@ const Writer = struct {...@@ -1326,6 +1326,7 @@ const Writer = struct {
1326 .indexable_ptr_len,1326 .indexable_ptr_len,
1327 .@"await",1327 .@"await",
1328 .bit_not,1328 .bit_not,
1329 .bool_not,
1329 .call_none,1330 .call_none,
1330 .compile_error,1331 .compile_error,
1331 .deref_node,1332 .deref_node,
...@@ -1337,9 +1338,20 @@ const Writer = struct {...@@ -1337,9 +1338,20 @@ const Writer = struct {
1337 .set_eval_branch_quota,1338 .set_eval_branch_quota,
1338 .resolve_inferred_alloc,1339 .resolve_inferred_alloc,
1339 .suspend_block_one,1340 .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,
1340 => try self.writeUnNode(stream, inst),1353 => try self.writeUnNode(stream, inst),
13411354
1342 .bool_not,
1343 .break_void_tok,1355 .break_void_tok,
1344 .is_non_null,1356 .is_non_null,
1345 .is_null,1357 .is_null,
...@@ -1351,18 +1363,6 @@ const Writer = struct {...@@ -1351,18 +1363,6 @@ const Writer = struct {
1351 .ret_tok,1363 .ret_tok,
1352 .ret_coerce,1364 .ret_coerce,
1353 .typeof,1365 .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,
1366 .ensure_err_payload_void,1366 .ensure_err_payload_void,
1367 => try self.writeUnTok(stream, inst),1367 => try self.writeUnTok(stream, inst),
13681368