authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-27 15:51:58+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-27 18:33:23+03:00
log509bb82b20e9417d8b0ff604cd87f70fd1fcf4e9
tree01263a314e35da6bbe6e65727822fdac842a89c9
parent83fa216c8d2375476ee02ccf53bf6b5a9ed7480e

Sema: refactor common code to its own function


1 files changed, 39 insertions(+), 183 deletions(-)

src/Sema.zig+39-183
......@@ -606,6 +606,21 @@ fn resolveBody(
606606 return try sema.resolveInst(break_data.operand);
607607}
608608
609fn analyzeBodyRuntimeBreak(sema: *Sema, block: *Block, body: []const Zir.Inst.Index) !void {
610 _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
611 error.ComptimeBreak => {
612 const zir_datas = sema.code.instructions.items(.data);
613 const break_data = zir_datas[sema.comptime_break_inst].@"break";
614 try sema.addRuntimeBreak(block, .{
615 .block_inst = break_data.block_inst,
616 .operand = break_data.operand,
617 .inst = sema.comptime_break_inst,
618 });
619 },
620 else => |e| return e,
621 };
622}
623
609624pub fn analyzeBody(
610625 sema: *Sema,
611626 block: *Block,
......@@ -10005,18 +10020,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1000510020 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand)) {
1000610021 // nothing to do here
1000710022 } else if (analyze_body) {
10008 _ = sema.analyzeBodyInner(&case_block, body) catch |err| switch (err) {
10009 error.ComptimeBreak => {
10010 const zir_datas = sema.code.instructions.items(.data);
10011 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10012 try sema.addRuntimeBreak(&case_block, .{
10013 .block_inst = break_data.block_inst,
10014 .operand = break_data.operand,
10015 .inst = sema.comptime_break_inst,
10016 });
10017 },
10018 else => |e| return e,
10019 };
10023 try sema.analyzeBodyRuntimeBreak(&case_block, body);
1002010024 } else {
1002110025 _ = try case_block.addNoOp(.unreach);
1002210026 }
......@@ -10069,16 +10073,16 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1006910073 extra_index += 1;
1007010074
1007110075 const item_first_ref = try sema.resolveInst(first_ref);
10072 var item_first = sema.resolveConstValue(block, .unneeded, item_first_ref, undefined) catch unreachable;
10076 var item = sema.resolveConstValue(block, .unneeded, item_first_ref, undefined) catch unreachable;
1007310077 const item_last_ref = try sema.resolveInst(last_ref);
1007410078 const item_last = sema.resolveConstValue(block, .unneeded, item_last_ref, undefined) catch unreachable;
1007510079
10076 while (item_first.compare(.lte, item_last, operand_ty, sema.mod)) : ({
10077 item_first = try sema.intAddScalar(block, case_src, item_first, Value.one);
10080 while (item.compare(.lte, item_last, operand_ty, sema.mod)) : ({
10081 item = try sema.intAddScalar(block, case_src, item, Value.one);
1007810082 }) {
1007910083 cases_len += 1;
1008010084
10081 const item_ref = try sema.addConstant(operand_ty, item_first);
10085 const item_ref = try sema.addConstant(operand_ty, item);
1008210086 case_block.inline_case_capture = item_ref;
1008310087
1008410088 case_block.instructions.shrinkRetainingCapacity(0);
......@@ -10087,25 +10091,12 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1008710091 if (emit_bb) try sema.emitBackwardBranch(block, case_src);
1008810092 emit_bb = true;
1008910093
10090 _ = sema.analyzeBodyInner(&case_block, body) catch |err| switch (err) {
10091 error.ComptimeBreak => {
10092 const zir_datas = sema.code.instructions.items(.data);
10093 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10094 try sema.addRuntimeBreak(&case_block, .{
10095 .block_inst = break_data.block_inst,
10096 .operand = break_data.operand,
10097 .inst = sema.comptime_break_inst,
10098 });
10099 },
10100 else => |e| return e,
10101 };
10102
10103 // try wip_captures.finalize();
10094 try sema.analyzeBodyRuntimeBreak(&case_block, body);
1010410095
1010510096 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1010610097 cases_extra.appendAssumeCapacity(1); // items_len
1010710098 cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len));
10108 cases_extra.appendAssumeCapacity(@enumToInt(item_ref));
10099 cases_extra.appendAssumeCapacity(@enumToInt(case_block.inline_case_capture));
1010910100 cases_extra.appendSliceAssumeCapacity(case_block.instructions.items);
1011010101 }
1011110102 }
......@@ -10129,28 +10120,15 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1012910120 emit_bb = true;
1013010121
1013110122 if (analyze_body) {
10132 _ = sema.analyzeBodyInner(&case_block, body) catch |err| switch (err) {
10133 error.ComptimeBreak => {
10134 const zir_datas = sema.code.instructions.items(.data);
10135 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10136 try sema.addRuntimeBreak(&case_block, .{
10137 .block_inst = break_data.block_inst,
10138 .operand = break_data.operand,
10139 .inst = sema.comptime_break_inst,
10140 });
10141 },
10142 else => |e| return e,
10143 };
10123 try sema.analyzeBodyRuntimeBreak(&case_block, body);
1014410124 } else {
1014510125 _ = try case_block.addNoOp(.unreach);
1014610126 }
1014710127
10148 // try wip_captures.finalize();
10149
1015010128 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1015110129 cases_extra.appendAssumeCapacity(1); // items_len
1015210130 cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len));
10153 cases_extra.appendAssumeCapacity(@enumToInt(item));
10131 cases_extra.appendAssumeCapacity(@enumToInt(case_block.inline_case_capture));
1015410132 cases_extra.appendSliceAssumeCapacity(case_block.instructions.items);
1015510133 }
1015610134
......@@ -10181,18 +10159,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1018110159 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand)) {
1018210160 // nothing to do here
1018310161 } else if (analyze_body) {
10184 _ = sema.analyzeBodyInner(&case_block, body) catch |err| switch (err) {
10185 error.ComptimeBreak => {
10186 const zir_datas = sema.code.instructions.items(.data);
10187 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10188 try sema.addRuntimeBreak(&case_block, .{
10189 .block_inst = break_data.block_inst,
10190 .operand = break_data.operand,
10191 .inst = sema.comptime_break_inst,
10192 });
10193 },
10194 else => |e| return e,
10195 };
10162 try sema.analyzeBodyRuntimeBreak(&case_block, body);
1019610163 } else {
1019710164 _ = try case_block.addNoOp(.unreach);
1019810165 }
......@@ -10273,18 +10240,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1027310240 if (err_set and try sema.maybeErrorUnwrap(&case_block, body, operand)) {
1027410241 // nothing to do here
1027510242 } else {
10276 _ = sema.analyzeBodyInner(&case_block, body) catch |err| switch (err) {
10277 error.ComptimeBreak => {
10278 const zir_datas = sema.code.instructions.items(.data);
10279 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10280 try sema.addRuntimeBreak(&case_block, .{
10281 .block_inst = break_data.block_inst,
10282 .operand = break_data.operand,
10283 .inst = sema.comptime_break_inst,
10284 });
10285 },
10286 else => |e| return e,
10287 };
10243 try sema.analyzeBodyRuntimeBreak(&case_block, body);
1028810244 }
1028910245
1029010246 try wip_captures.finalize();
......@@ -10315,8 +10271,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1031510271
1031610272 var final_else_body: []const Air.Inst.Index = &.{};
1031710273 if (special.body.len != 0 or !is_first or case_block.wantSafety()) {
10318 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope);
10319 defer wip_captures.deinit();
10274 var emit_bb = false;
1032010275 if (special.is_inline) switch (operand_ty.zigTypeTag()) {
1032110276 .Enum => {
1032210277 if (operand_ty.isNonexhaustiveEnum() and !union_originally) {
......@@ -10324,7 +10279,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1032410279 operand_ty.fmt(sema.mod),
1032510280 });
1032610281 }
10327 var emit_bb = false;
1032810282 for (seen_enum_fields) |f, i| {
1032910283 if (f != null) continue;
1033010284 cases_len += 1;
......@@ -10345,24 +10299,11 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1034510299 emit_bb = true;
1034610300
1034710301 if (analyze_body) {
10348 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10349 error.ComptimeBreak => {
10350 const zir_datas = sema.code.instructions.items(.data);
10351 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10352 try sema.addRuntimeBreak(&case_block, .{
10353 .block_inst = break_data.block_inst,
10354 .operand = break_data.operand,
10355 .inst = sema.comptime_break_inst,
10356 });
10357 },
10358 else => |e| return e,
10359 };
10302 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1036010303 } else {
1036110304 _ = try case_block.addNoOp(.unreach);
1036210305 }
1036310306
10364 // try wip_captures.finalize();
10365
1036610307 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1036710308 cases_extra.appendAssumeCapacity(1); // items_len
1036810309 cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len));
......@@ -10376,7 +10317,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1037610317 operand_ty.fmt(sema.mod),
1037710318 });
1037810319 }
10379 var emit_bb = false;
1038010320 for (operand_ty.errorSetNames()) |error_name| {
1038110321 if (seen_errors.contains(error_name)) continue;
1038210322 cases_len += 1;
......@@ -10391,20 +10331,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1039110331 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1039210332 emit_bb = true;
1039310333
10394 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10395 error.ComptimeBreak => {
10396 const zir_datas = sema.code.instructions.items(.data);
10397 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10398 try sema.addRuntimeBreak(&case_block, .{
10399 .block_inst = break_data.block_inst,
10400 .operand = break_data.operand,
10401 .inst = sema.comptime_break_inst,
10402 });
10403 },
10404 else => |e| return e,
10405 };
10406
10407 // try wip_captures.finalize();
10334 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1040810335
1040910336 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1041010337 cases_extra.appendAssumeCapacity(1); // items_len
......@@ -10415,7 +10342,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1041510342 },
1041610343 .Int => {
1041710344 var it = try RangeSetUnhandledIterator.init(sema, block, special_prong_src, operand_ty, range_set);
10418 var emit_bb = false;
1041910345 while (try it.next()) |cur| {
1042010346 cases_len += 1;
1042110347
......@@ -10428,30 +10354,16 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1042810354 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1042910355 emit_bb = true;
1043010356
10431 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10432 error.ComptimeBreak => {
10433 const zir_datas = sema.code.instructions.items(.data);
10434 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10435 try sema.addRuntimeBreak(&case_block, .{
10436 .block_inst = break_data.block_inst,
10437 .operand = break_data.operand,
10438 .inst = sema.comptime_break_inst,
10439 });
10440 },
10441 else => |e| return e,
10442 };
10443
10444 // try wip_captures.finalize();
10357 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1044510358
1044610359 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1044710360 cases_extra.appendAssumeCapacity(1); // items_len
1044810361 cases_extra.appendAssumeCapacity(@intCast(u32, case_block.instructions.items.len));
10449 cases_extra.appendAssumeCapacity(@enumToInt(item_ref));
10362 cases_extra.appendAssumeCapacity(@enumToInt(case_block.inline_case_capture));
1045010363 cases_extra.appendSliceAssumeCapacity(case_block.instructions.items);
1045110364 }
1045210365 },
1045310366 .Bool => {
10454 var emit_bb = false;
1045510367 if (true_count == 0) {
1045610368 cases_len += 1;
1045710369 case_block.inline_case_capture = Air.Inst.Ref.bool_true;
......@@ -10462,20 +10374,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1046210374 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1046310375 emit_bb = true;
1046410376
10465 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10466 error.ComptimeBreak => {
10467 const zir_datas = sema.code.instructions.items(.data);
10468 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10469 try sema.addRuntimeBreak(&case_block, .{
10470 .block_inst = break_data.block_inst,
10471 .operand = break_data.operand,
10472 .inst = sema.comptime_break_inst,
10473 });
10474 },
10475 else => |e| return e,
10476 };
10477
10478 // try wip_captures.finalize();
10377 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1047910378
1048010379 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1048110380 cases_extra.appendAssumeCapacity(1); // items_len
......@@ -10493,20 +10392,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1049310392 if (emit_bb) try sema.emitBackwardBranch(block, special_prong_src);
1049410393 emit_bb = true;
1049510394
10496 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10497 error.ComptimeBreak => {
10498 const zir_datas = sema.code.instructions.items(.data);
10499 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10500 try sema.addRuntimeBreak(&case_block, .{
10501 .block_inst = break_data.block_inst,
10502 .operand = break_data.operand,
10503 .inst = sema.comptime_break_inst,
10504 });
10505 },
10506 else => |e| return e,
10507 };
10508
10509 // try wip_captures.finalize();
10395 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1051010396
1051110397 try cases_extra.ensureUnusedCapacity(gpa, 3 + case_block.instructions.items.len);
1051210398 cases_extra.appendAssumeCapacity(1); // items_len
......@@ -10520,6 +10406,9 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1052010406 }),
1052110407 };
1052210408
10409 var wip_captures = try WipCaptureScope.init(gpa, sema.perm_arena, child_block.wip_capture_scope);
10410 defer wip_captures.deinit();
10411
1052310412 case_block.instructions.shrinkRetainingCapacity(0);
1052410413 case_block.wip_capture_scope = wip_captures.scope;
1052510414 case_block.inline_case_capture = .none;
......@@ -10538,18 +10427,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
1053810427 {
1053910428 // nothing to do here
1054010429 } else if (special.body.len != 0 and analyze_body and !special.is_inline) {
10541 _ = sema.analyzeBodyInner(&case_block, special.body) catch |err| switch (err) {
10542 error.ComptimeBreak => {
10543 const zir_datas = sema.code.instructions.items(.data);
10544 const break_data = zir_datas[sema.comptime_break_inst].@"break";
10545 try sema.addRuntimeBreak(&case_block, .{
10546 .block_inst = break_data.block_inst,
10547 .operand = break_data.operand,
10548 .inst = sema.comptime_break_inst,
10549 });
10550 },
10551 else => |e| return e,
10552 };
10430 try sema.analyzeBodyRuntimeBreak(&case_block, special.body);
1055310431 } else {
1055410432 // We still need a terminator in this block, but we have proven
1055510433 // that it is unreachable.
......@@ -15716,18 +15594,7 @@ fn zirCondbr(
1571615594 sub_block.runtime_index.increment();
1571715595 defer sub_block.instructions.deinit(gpa);
1571815596
15719 _ = sema.analyzeBodyInner(&sub_block, then_body) catch |err| switch (err) {
15720 error.ComptimeBreak => {
15721 const zir_datas = sema.code.instructions.items(.data);
15722 const break_data = zir_datas[sema.comptime_break_inst].@"break";
15723 try sema.addRuntimeBreak(&sub_block, .{
15724 .block_inst = break_data.block_inst,
15725 .operand = break_data.operand,
15726 .inst = sema.comptime_break_inst,
15727 });
15728 },
15729 else => |e| return e,
15730 };
15597 try sema.analyzeBodyRuntimeBreak(&sub_block, then_body);
1573115598 const true_instructions = sub_block.instructions.toOwnedSlice(gpa);
1573215599 defer gpa.free(true_instructions);
1573315600
......@@ -15746,18 +15613,7 @@ fn zirCondbr(
1574615613 if (err_cond != null and try sema.maybeErrorUnwrap(&sub_block, else_body, err_cond.?)) {
1574715614 // nothing to do
1574815615 } else {
15749 _ = sema.analyzeBodyInner(&sub_block, else_body) catch |err| switch (err) {
15750 error.ComptimeBreak => {
15751 const zir_datas = sema.code.instructions.items(.data);
15752 const break_data = zir_datas[sema.comptime_break_inst].@"break";
15753 try sema.addRuntimeBreak(&sub_block, .{
15754 .block_inst = break_data.block_inst,
15755 .operand = break_data.operand,
15756 .inst = sema.comptime_break_inst,
15757 });
15758 },
15759 else => |e| return e,
15760 };
15616 try sema.analyzeBodyRuntimeBreak(&sub_block, else_body);
1576115617 }
1576215618 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.CondBr).Struct.fields.len +
1576315619 true_instructions.len + sub_block.instructions.items.len);