authorgravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2023-11-24 12:53:10+11:00
committergravatar for 4678790+dweiller@users.noreply.github.comDominic <4678790+dweiller@users.noreply.github.com> 2024-01-09 14:42:12+11:00
log6bf319ebbb0bce945e9598a03783bc5a26f70e15
treed17a2948ed920921a229d98a1dd0a9221fd46772
parentae19f699ab3a831d48a18c75d062fff17ab9268e

sema: extract runtime switch AIR generation to function


1 files changed, 77 insertions(+), 17 deletions(-)

src/Sema.zig+77-17
...@@ -11355,7 +11355,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -11355,7 +11355,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
1135511355
11356 const mod = sema.mod;11356 const mod = sema.mod;
11357 const gpa = sema.gpa;11357 const gpa = sema.gpa;
11358 const ip = &mod.intern_pool;
11359 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;11358 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
11360 const src = inst_data.src();11359 const src = inst_data.src();
11361 const src_node_offset = inst_data.src_node;11360 const src_node_offset = inst_data.src_node;
...@@ -11944,6 +11943,71 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -11944,6 +11943,71 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
11944 unreachable;11943 unreachable;
11945 }11944 }
1194611945
11946 _ = try sema.analyzeSwitchRuntimeBlock(
11947 spa,
11948 &child_block,
11949 src,
11950 operand,
11951 operand_ty,
11952 operand_src,
11953 case_vals,
11954 special,
11955 scalar_cases_len,
11956 multi_cases_len,
11957 union_originally,
11958 maybe_union_ty,
11959 err_set,
11960 src_node_offset,
11961 special_prong_src,
11962 seen_enum_fields,
11963 seen_errors,
11964 range_set,
11965 true_count,
11966 false_count,
11967 cond_dbg_node_index,
11968 );
11969
11970 return sema.analyzeBlockBody(block, src, &child_block, merges);
11971}
11972
11973const SpecialProng = struct {
11974 body: []const Zir.Inst.Index,
11975 end: usize,
11976 capture: Zir.Inst.SwitchBlock.ProngInfo.Capture,
11977 is_inline: bool,
11978 has_tag_capture: bool,
11979};
11980
11981fn analyzeSwitchRuntimeBlock(
11982 sema: *Sema,
11983 spa: SwitchProngAnalysis,
11984 child_block: *Block,
11985 src: LazySrcLoc,
11986 operand: Air.Inst.Ref,
11987 operand_ty: Type,
11988 operand_src: LazySrcLoc,
11989 case_vals: std.ArrayListUnmanaged(Air.Inst.Ref),
11990 special: SpecialProng,
11991 scalar_cases_len: usize,
11992 multi_cases_len: usize,
11993 union_originally: bool,
11994 maybe_union_ty: Type,
11995 err_set: bool,
11996 src_node_offset: i32,
11997 special_prong_src: LazySrcLoc,
11998 seen_enum_fields: []?Module.SwitchProngSrc,
11999 seen_errors: SwitchErrorSet,
12000 range_set: RangeSet,
12001 true_count: u8,
12002 false_count: u8,
12003 cond_dbg_node_index: Zir.Inst.Index,
12004) CompileError!Air.Inst.Ref {
12005 const mod = sema.mod;
12006 const gpa = sema.gpa;
12007 const ip = &mod.intern_pool;
12008
12009 const block = child_block.parent.?;
12010
11947 const estimated_cases_extra = (scalar_cases_len + multi_cases_len) *12011 const estimated_cases_extra = (scalar_cases_len + multi_cases_len) *
11948 @typeInfo(Air.SwitchBr.Case).Struct.fields.len + 2;12012 @typeInfo(Air.SwitchBr.Case).Struct.fields.len + 2;
11949 var cases_extra = try std.ArrayListUnmanaged(u32).initCapacity(gpa, estimated_cases_extra);12013 var cases_extra = try std.ArrayListUnmanaged(u32).initCapacity(gpa, estimated_cases_extra);
...@@ -12534,27 +12598,23 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r...@@ -12534,27 +12598,23 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r
12534 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr).Struct.fields.len +12598 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.SwitchBr).Struct.fields.len +
12535 cases_extra.items.len + final_else_body.len);12599 cases_extra.items.len + final_else_body.len);
1253612600
12537 _ = try child_block.addInst(.{ .tag = .switch_br, .data = .{ .pl_op = .{12601 const payload_index = sema.addExtraAssumeCapacity(Air.SwitchBr{
12538 .operand = operand,12602 .cases_len = @intCast(cases_len),
12539 .payload = sema.addExtraAssumeCapacity(Air.SwitchBr{12603 .else_body_len = @intCast(final_else_body.len),
12540 .cases_len = @intCast(cases_len),12604 });
12541 .else_body_len = @intCast(final_else_body.len),12605
12542 }),
12543 } } });
12544 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(cases_extra.items));12606 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(cases_extra.items));
12545 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(final_else_body));12607 sema.air_extra.appendSliceAssumeCapacity(@ptrCast(final_else_body));
1254612608
12547 return sema.analyzeBlockBody(block, src, &child_block, merges);12609 return try child_block.addInst(.{
12610 .tag = .switch_br,
12611 .data = .{ .pl_op = .{
12612 .operand = operand,
12613 .payload = payload_index,
12614 } },
12615 });
12548}12616}
1254912617
12550const SpecialProng = struct {
12551 body: []const Zir.Inst.Index,
12552 end: usize,
12553 capture: Zir.Inst.SwitchBlock.ProngInfo.Capture,
12554 is_inline: bool,
12555 has_tag_capture: bool,
12556};
12557
12558fn resolveSwitchComptime(12618fn resolveSwitchComptime(
12559 sema: *Sema,12619 sema: *Sema,
12560 spa: SwitchProngAnalysis,12620 spa: SwitchProngAnalysis,