authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-24 16:14:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-24 16:14:11-07:00
logd73a4940e0d907e017ce60d0a7183cd1618b3b39
treed93dccb6abb70e09d69a3c09d30b8fdedc5a200f
parent3543373fd4531ca7f3158b0a2884fd5e5a2fedb2

stage2: cleanups from previous commits

Change some ZIR instructions from un_tok to un_node. Idea here is to avoid needlessly accessing the tokens array. Go ahead and finish the catch code in orelseCatchExpr. Otherwise it would be an easy mistake to get the scopes wrong when updating that code and introduce a bug. Delete a function that is now dead code.

3 files changed, 34 insertions(+), 55 deletions(-)

src/Sema.zig+4-4
...@@ -2913,7 +2913,7 @@ fn zirIsNull(...@@ -2913,7 +2913,7 @@ fn zirIsNull(
2913 const tracy = trace(@src());2913 const tracy = trace(@src());
2914 defer tracy.end();2914 defer tracy.end();
29152915
2916 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;2916 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2917 const src = inst_data.src();2917 const src = inst_data.src();
2918 const operand = try sema.resolveInst(inst_data.operand);2918 const operand = try sema.resolveInst(inst_data.operand);
2919 return sema.analyzeIsNull(block, src, operand, invert_logic);2919 return sema.analyzeIsNull(block, src, operand, invert_logic);
...@@ -2928,7 +2928,7 @@ fn zirIsNullPtr(...@@ -2928,7 +2928,7 @@ fn zirIsNullPtr(
2928 const tracy = trace(@src());2928 const tracy = trace(@src());
2929 defer tracy.end();2929 defer tracy.end();
29302930
2931 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;2931 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2932 const src = inst_data.src();2932 const src = inst_data.src();
2933 const ptr = try sema.resolveInst(inst_data.operand);2933 const ptr = try sema.resolveInst(inst_data.operand);
2934 const loaded = try sema.analyzeDeref(block, src, ptr, src);2934 const loaded = try sema.analyzeDeref(block, src, ptr, src);
...@@ -2939,7 +2939,7 @@ fn zirIsErr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*...@@ -2939,7 +2939,7 @@ fn zirIsErr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!*
2939 const tracy = trace(@src());2939 const tracy = trace(@src());
2940 defer tracy.end();2940 defer tracy.end();
29412941
2942 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;2942 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2943 const operand = try sema.resolveInst(inst_data.operand);2943 const operand = try sema.resolveInst(inst_data.operand);
2944 return sema.analyzeIsErr(block, inst_data.src(), operand);2944 return sema.analyzeIsErr(block, inst_data.src(), operand);
2945}2945}
...@@ -2948,7 +2948,7 @@ fn zirIsErrPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro...@@ -2948,7 +2948,7 @@ fn zirIsErrPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro
2948 const tracy = trace(@src());2948 const tracy = trace(@src());
2949 defer tracy.end();2949 defer tracy.end();
29502950
2951 const inst_data = sema.code.instructions.items(.data)[inst].un_tok;2951 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
2952 const src = inst_data.src();2952 const src = inst_data.src();
2953 const ptr = try sema.resolveInst(inst_data.operand);2953 const ptr = try sema.resolveInst(inst_data.operand);
2954 const loaded = try sema.analyzeDeref(block, src, ptr, src);2954 const loaded = try sema.analyzeDeref(block, src, ptr, src);
src/astgen.zig+22-43
...@@ -526,8 +526,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -526,8 +526,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
526 mod,526 mod,
527 scope,527 scope,
528 rl,528 rl,
529 node,
529 node_datas[node].lhs,530 node_datas[node].lhs,
530 main_tokens[node],
531 .is_err_ptr,531 .is_err_ptr,
532 .err_union_payload_unsafe_ptr,532 .err_union_payload_unsafe_ptr,
533 .err_union_code_ptr,533 .err_union_code_ptr,
...@@ -538,8 +538,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -538,8 +538,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
538 mod,538 mod,
539 scope,539 scope,
540 rl,540 rl,
541 node,
541 node_datas[node].lhs,542 node_datas[node].lhs,
542 main_tokens[node],
543 .is_err,543 .is_err,
544 .err_union_payload_unsafe,544 .err_union_payload_unsafe,
545 .err_union_code,545 .err_union_code,
...@@ -555,7 +555,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -555,7 +555,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
555 rl,555 rl,
556 node,556 node,
557 node_datas[node].lhs,557 node_datas[node].lhs,
558 main_tokens[node],
559 .is_null_ptr,558 .is_null_ptr,
560 .optional_payload_unsafe_ptr,559 .optional_payload_unsafe_ptr,
561 undefined,560 undefined,
...@@ -568,7 +567,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In...@@ -568,7 +567,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
568 rl,567 rl,
569 node,568 node,
570 node_datas[node].lhs,569 node_datas[node].lhs,
571 main_tokens[node],
572 .is_null,570 .is_null,
573 .optional_payload_unsafe,571 .optional_payload_unsafe,
574 undefined,572 undefined,
...@@ -1637,7 +1635,6 @@ fn orelseCatchExpr(...@@ -1637,7 +1635,6 @@ fn orelseCatchExpr(
1637 rl: ResultLoc,1635 rl: ResultLoc,
1638 node: ast.Node.Index,1636 node: ast.Node.Index,
1639 lhs: ast.Node.Index,1637 lhs: ast.Node.Index,
1640 op_token: ast.TokenIndex,
1641 cond_op: zir.Inst.Tag,1638 cond_op: zir.Inst.Tag,
1642 unwrap_op: zir.Inst.Tag,1639 unwrap_op: zir.Inst.Tag,
1643 unwrap_code_op: zir.Inst.Tag,1640 unwrap_code_op: zir.Inst.Tag,
...@@ -1645,6 +1642,8 @@ fn orelseCatchExpr(...@@ -1645,6 +1642,8 @@ fn orelseCatchExpr(
1645 payload_token: ?ast.TokenIndex,1642 payload_token: ?ast.TokenIndex,
1646) InnerError!zir.Inst.Ref {1643) InnerError!zir.Inst.Ref {
1647 const parent_gz = scope.getGenZir();1644 const parent_gz = scope.getGenZir();
1645 const tree = parent_gz.tree();
1646
1648 var block_scope: Scope.GenZir = .{1647 var block_scope: Scope.GenZir = .{
1649 .parent = scope,1648 .parent = scope,
1650 .zir_code = parent_gz.zir_code,1649 .zir_code = parent_gz.zir_code,
...@@ -1674,8 +1673,7 @@ fn orelseCatchExpr(...@@ -1674,8 +1673,7 @@ fn orelseCatchExpr(
1674 },1673 },
1675 };1674 };
1676 const operand = try expr(mod, &block_scope.base, operand_rl, lhs);1675 const operand = try expr(mod, &block_scope.base, operand_rl, lhs);
1677 const cond = try block_scope.addUnTok(cond_op, operand, op_token);1676 const cond = try block_scope.addUnNode(cond_op, operand, node);
1678
1679 const condbr = try block_scope.addCondBr(node);1677 const condbr = try block_scope.addCondBr(node);
16801678
1681 const block = try parent_gz.addBlock(.block, node);1679 const block = try parent_gz.addBlock(.block, node);
...@@ -1690,25 +1688,25 @@ fn orelseCatchExpr(...@@ -1690,25 +1688,25 @@ fn orelseCatchExpr(
1690 };1688 };
1691 defer then_scope.instructions.deinit(mod.gpa);1689 defer then_scope.instructions.deinit(mod.gpa);
16921690
1693 if (payload_token != null) @panic("TODO handle catch");1691 var err_val_scope: Scope.LocalVal = undefined;
1694 // var err_val_scope: Scope.LocalVal = undefined;1692 const then_sub_scope = blk: {
1695 // const then_sub_scope = blk: {1693 const payload = payload_token orelse break :blk &then_scope.base;
1696 // const payload = payload_token orelse break :blk &then_scope.base;1694 if (mem.eql(u8, tree.tokenSlice(payload), "_")) {
1697 // if (mem.eql(u8, tree.tokenSlice(payload), "_")) {1695 return mod.failTok(&then_scope.base, payload, "discard of error capture; omit it instead", .{});
1698 // return mod.failTok(&then_scope.base, payload, "discard of error capture; omit it instead", .{});1696 }
1699 // }1697 const err_name = try mod.identifierTokenString(scope, payload);
1700 // const err_name = try mod.identifierTokenString(scope, payload);1698 err_val_scope = .{
1701 // err_val_scope = .{1699 .parent = &then_scope.base,
1702 // .parent = &then_scope.base,1700 .gen_zir = &then_scope,
1703 // .gen_zir = &then_scope,1701 .name = err_name,
1704 // .name = err_name,1702 .inst = try then_scope.addUnNode(unwrap_code_op, operand, node),
1705 // .inst = try addZIRUnOp(mod, &then_scope.base, src, unwrap_code_op, operand),1703 .src = parent_gz.tokSrcLoc(payload),
1706 // };1704 };
1707 // break :blk &err_val_scope.base;1705 break :blk &err_val_scope.base;
1708 // };1706 };
17091707
1710 block_scope.break_count += 1;1708 block_scope.break_count += 1;
1711 const then_result = try expr(mod, &then_scope.base, block_scope.break_result_loc, rhs);1709 const then_result = try expr(mod, then_sub_scope, block_scope.break_result_loc, rhs);
1712 // We hold off on the break instructions as well as copying the then/else1710 // We hold off on the break instructions as well as copying the then/else
1713 // instructions into place until we know whether to keep store_to_block_ptr1711 // instructions into place until we know whether to keep store_to_block_ptr
1714 // instructions or not.1712 // instructions or not.
...@@ -3861,25 +3859,6 @@ fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZir) ResultLoc.Strategy {...@@ -3861,25 +3859,6 @@ fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZir) ResultLoc.Strategy {
3861 }3859 }
3862}3860}
38633861
3864/// If the input ResultLoc is ref, returns ResultLoc.ref. Otherwise:
3865/// Returns ResultLoc.ty, where the type is determined by the input
3866/// ResultLoc type, wrapped in an optional type. If the input ResultLoc
3867/// has no type, .none is returned.
3868fn makeOptionalTypeResultLoc(mod: *Module, scope: *Scope, src: usize, rl: ResultLoc) !ResultLoc {
3869 switch (rl) {
3870 .ref => return ResultLoc.ref,
3871 .discard, .none, .block_ptr, .inferred_ptr, .bitcasted_ptr => return ResultLoc.none,
3872 .ty => |elem_ty| {
3873 const wrapped_ty = try addZIRUnOp(mod, scope, src, .optional_type, elem_ty);
3874 return ResultLoc{ .ty = wrapped_ty };
3875 },
3876 .ptr => |ptr_ty| {
3877 const wrapped_ty = try addZIRUnOp(mod, scope, src, .optional_type_from_ptr_elem, ptr_ty);
3878 return ResultLoc{ .ty = wrapped_ty };
3879 },
3880 }
3881}
3882
3883fn setBlockResultLoc(block_scope: *Scope.GenZir, parent_rl: ResultLoc) void {3862fn setBlockResultLoc(block_scope: *Scope.GenZir, parent_rl: ResultLoc) void {
3884 // Depending on whether the result location is a pointer or value, different3863 // Depending on whether the result location is a pointer or value, different
3885 // ZIR needs to be generated. In the former case we rely on storing to the3864 // ZIR needs to be generated. In the former case we rely on storing to the
src/zir.zig+8-8
...@@ -362,22 +362,22 @@ pub const Inst = struct {...@@ -362,22 +362,22 @@ pub const Inst = struct {
362 /// Payload is `int_type`362 /// Payload is `int_type`
363 int_type,363 int_type,
364 /// Return a boolean false if an optional is null. `x != null`364 /// Return a boolean false if an optional is null. `x != null`
365 /// Uses the `un_tok` field.365 /// Uses the `un_node` field.
366 is_non_null,366 is_non_null,
367 /// Return a boolean true if an optional is null. `x == null`367 /// Return a boolean true if an optional is null. `x == null`
368 /// Uses the `un_tok` field.368 /// Uses the `un_node` field.
369 is_null,369 is_null,
370 /// Return a boolean false if an optional is null. `x.* != null`370 /// Return a boolean false if an optional is null. `x.* != null`
371 /// Uses the `un_tok` field.371 /// Uses the `un_node` field.
372 is_non_null_ptr,372 is_non_null_ptr,
373 /// Return a boolean true if an optional is null. `x.* == null`373 /// Return a boolean true if an optional is null. `x.* == null`
374 /// Uses the `un_tok` field.374 /// Uses the `un_node` field.
375 is_null_ptr,375 is_null_ptr,
376 /// Return a boolean true if value is an error376 /// Return a boolean true if value is an error
377 /// Uses the `un_tok` field.377 /// Uses the `un_node` field.
378 is_err,378 is_err,
379 /// Return a boolean true if dereferenced pointer is an error379 /// Return a boolean true if dereferenced pointer is an error
380 /// Uses the `un_tok` field.380 /// Uses the `un_node` field.
381 is_err_ptr,381 is_err_ptr,
382 /// A labeled block of code that loops forever. At the end of the body will have either382 /// A labeled block of code that loops forever. At the end of the body will have either
383 /// a `repeat` instruction or a `repeat_inline` instruction.383 /// a `repeat` instruction or a `repeat_inline` instruction.
...@@ -1411,14 +1411,14 @@ const Writer = struct {...@@ -1411,14 +1411,14 @@ const Writer = struct {
1411 .err_union_code,1411 .err_union_code,
1412 .err_union_code_ptr,1412 .err_union_code_ptr,
1413 .break_flat,1413 .break_flat,
1414 => try self.writeUnNode(stream, inst),
1415
1416 .is_non_null,1414 .is_non_null,
1417 .is_null,1415 .is_null,
1418 .is_non_null_ptr,1416 .is_non_null_ptr,
1419 .is_null_ptr,1417 .is_null_ptr,
1420 .is_err,1418 .is_err,
1421 .is_err_ptr,1419 .is_err_ptr,
1420 => try self.writeUnNode(stream, inst),
1421
1422 .ref,1422 .ref,
1423 .ret_tok,1423 .ret_tok,
1424 .ret_coerce,1424 .ret_coerce,