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(
29132913 const tracy = trace(@src());
29142914 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;
29172917 const src = inst_data.src();
29182918 const operand = try sema.resolveInst(inst_data.operand);
29192919 return sema.analyzeIsNull(block, src, operand, invert_logic);
......@@ -2928,7 +2928,7 @@ fn zirIsNullPtr(
29282928 const tracy = trace(@src());
29292929 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;
29322932 const src = inst_data.src();
29332933 const ptr = try sema.resolveInst(inst_data.operand);
29342934 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!*
29392939 const tracy = trace(@src());
29402940 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;
29432943 const operand = try sema.resolveInst(inst_data.operand);
29442944 return sema.analyzeIsErr(block, inst_data.src(), operand);
29452945}
......@@ -2948,7 +2948,7 @@ fn zirIsErrPtr(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerErro
29482948 const tracy = trace(@src());
29492949 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;
29522952 const src = inst_data.src();
29532953 const ptr = try sema.resolveInst(inst_data.operand);
29542954 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
526526 mod,
527527 scope,
528528 rl,
529 node,
529530 node_datas[node].lhs,
530 main_tokens[node],
531531 .is_err_ptr,
532532 .err_union_payload_unsafe_ptr,
533533 .err_union_code_ptr,
......@@ -538,8 +538,8 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
538538 mod,
539539 scope,
540540 rl,
541 node,
541542 node_datas[node].lhs,
542 main_tokens[node],
543543 .is_err,
544544 .err_union_payload_unsafe,
545545 .err_union_code,
......@@ -555,7 +555,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
555555 rl,
556556 node,
557557 node_datas[node].lhs,
558 main_tokens[node],
559558 .is_null_ptr,
560559 .optional_payload_unsafe_ptr,
561560 undefined,
......@@ -568,7 +567,6 @@ pub fn expr(mod: *Module, scope: *Scope, rl: ResultLoc, node: ast.Node.Index) In
568567 rl,
569568 node,
570569 node_datas[node].lhs,
571 main_tokens[node],
572570 .is_null,
573571 .optional_payload_unsafe,
574572 undefined,
......@@ -1637,7 +1635,6 @@ fn orelseCatchExpr(
16371635 rl: ResultLoc,
16381636 node: ast.Node.Index,
16391637 lhs: ast.Node.Index,
1640 op_token: ast.TokenIndex,
16411638 cond_op: zir.Inst.Tag,
16421639 unwrap_op: zir.Inst.Tag,
16431640 unwrap_code_op: zir.Inst.Tag,
......@@ -1645,6 +1642,8 @@ fn orelseCatchExpr(
16451642 payload_token: ?ast.TokenIndex,
16461643) InnerError!zir.Inst.Ref {
16471644 const parent_gz = scope.getGenZir();
1645 const tree = parent_gz.tree();
1646
16481647 var block_scope: Scope.GenZir = .{
16491648 .parent = scope,
16501649 .zir_code = parent_gz.zir_code,
......@@ -1674,8 +1673,7 @@ fn orelseCatchExpr(
16741673 },
16751674 };
16761675 const operand = try expr(mod, &block_scope.base, operand_rl, lhs);
1677 const cond = try block_scope.addUnTok(cond_op, operand, op_token);
1678
1676 const cond = try block_scope.addUnNode(cond_op, operand, node);
16791677 const condbr = try block_scope.addCondBr(node);
16801678
16811679 const block = try parent_gz.addBlock(.block, node);
......@@ -1690,25 +1688,25 @@ fn orelseCatchExpr(
16901688 };
16911689 defer then_scope.instructions.deinit(mod.gpa);
16921690
1693 if (payload_token != null) @panic("TODO handle catch");
1694 // var err_val_scope: Scope.LocalVal = undefined;
1695 // const then_sub_scope = blk: {
1696 // const payload = payload_token orelse break :blk &then_scope.base;
1697 // if (mem.eql(u8, tree.tokenSlice(payload), "_")) {
1698 // return mod.failTok(&then_scope.base, payload, "discard of error capture; omit it instead", .{});
1699 // }
1700 // const err_name = try mod.identifierTokenString(scope, payload);
1701 // err_val_scope = .{
1702 // .parent = &then_scope.base,
1703 // .gen_zir = &then_scope,
1704 // .name = err_name,
1705 // .inst = try addZIRUnOp(mod, &then_scope.base, src, unwrap_code_op, operand),
1706 // };
1707 // break :blk &err_val_scope.base;
1708 // };
1691 var err_val_scope: Scope.LocalVal = undefined;
1692 const then_sub_scope = blk: {
1693 const payload = payload_token orelse break :blk &then_scope.base;
1694 if (mem.eql(u8, tree.tokenSlice(payload), "_")) {
1695 return mod.failTok(&then_scope.base, payload, "discard of error capture; omit it instead", .{});
1696 }
1697 const err_name = try mod.identifierTokenString(scope, payload);
1698 err_val_scope = .{
1699 .parent = &then_scope.base,
1700 .gen_zir = &then_scope,
1701 .name = err_name,
1702 .inst = try then_scope.addUnNode(unwrap_code_op, operand, node),
1703 .src = parent_gz.tokSrcLoc(payload),
1704 };
1705 break :blk &err_val_scope.base;
1706 };
17091707
17101708 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);
17121710 // We hold off on the break instructions as well as copying the then/else
17131711 // instructions into place until we know whether to keep store_to_block_ptr
17141712 // instructions or not.
......@@ -3861,25 +3859,6 @@ fn rlStrategy(rl: ResultLoc, block_scope: *Scope.GenZir) ResultLoc.Strategy {
38613859 }
38623860}
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
38833862fn setBlockResultLoc(block_scope: *Scope.GenZir, parent_rl: ResultLoc) void {
38843863 // Depending on whether the result location is a pointer or value, different
38853864 // 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 {
362362 /// Payload is `int_type`
363363 int_type,
364364 /// Return a boolean false if an optional is null. `x != null`
365 /// Uses the `un_tok` field.
365 /// Uses the `un_node` field.
366366 is_non_null,
367367 /// Return a boolean true if an optional is null. `x == null`
368 /// Uses the `un_tok` field.
368 /// Uses the `un_node` field.
369369 is_null,
370370 /// Return a boolean false if an optional is null. `x.* != null`
371 /// Uses the `un_tok` field.
371 /// Uses the `un_node` field.
372372 is_non_null_ptr,
373373 /// Return a boolean true if an optional is null. `x.* == null`
374 /// Uses the `un_tok` field.
374 /// Uses the `un_node` field.
375375 is_null_ptr,
376376 /// Return a boolean true if value is an error
377 /// Uses the `un_tok` field.
377 /// Uses the `un_node` field.
378378 is_err,
379379 /// Return a boolean true if dereferenced pointer is an error
380 /// Uses the `un_tok` field.
380 /// Uses the `un_node` field.
381381 is_err_ptr,
382382 /// A labeled block of code that loops forever. At the end of the body will have either
383383 /// a `repeat` instruction or a `repeat_inline` instruction.
......@@ -1411,14 +1411,14 @@ const Writer = struct {
14111411 .err_union_code,
14121412 .err_union_code_ptr,
14131413 .break_flat,
1414 => try self.writeUnNode(stream, inst),
1415
14161414 .is_non_null,
14171415 .is_null,
14181416 .is_non_null_ptr,
14191417 .is_null_ptr,
14201418 .is_err,
14211419 .is_err_ptr,
1420 => try self.writeUnNode(stream, inst),
1421
14221422 .ref,
14231423 .ret_tok,
14241424 .ret_coerce,