#! @file check.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the checker of the register allocator, which replays what it chose #! of a function, symbolically: which value each register and slot holds as #! each move, call, and result changes them, block by block, confirming each #! operand is read where its value is, and that each edge leaves every value #! the block it goes to holds where that block expects it. The allocator and #! the moves it finds are where such mistakes hide, so tests and `--verify` #! run this on every function they build. import sakura::compiler; import binary::amd64; import std::containers; module sakura::native; #! What a register or slot holds as the replay goes, by place. struct holding { var gprs: u32[16]; #! The value each general register holds, or 0. var xmms: u32[16]; #! The value each SSE register holds, or 0. var slots: dict; #! The value each value's slot holds, where it holds one. } #! A copy of what is held. #! @param this What is held. #! @returns The copy. def holding::copied(&this): holding { var made := holding { gprs = this.gprs, xmms = this.xmms, slots = dict::::new(scratch()) }; for &each in this.slots { made.slots.set(each.key, each.value); } return made; } #! The value a place holds, or 0 where it holds none known. #! @param this What is held. #! @param spot The place. #! @returns The value. def holding::at(&this, spot: &loc): u32 { if spot.kind == place::slot { return this.slots.get_or(&spot.value, 0); } return match spot.kind { place::gpr => this.gprs[spot.number as u64], place::xmm => this.xmms[spot.number as u64], place::remade => spot.value, else => 0 }; } #! Says a place holds a value. #! @param this What is held. #! @param spot The place. #! @param value The value. def holding::set(&mut this, spot: &loc, value: u32): void { match spot.kind { place::gpr => this.gprs[spot.number as u64] = value; place::xmm => this.xmms[spot.number as u64] = value; place::slot => this.slots.set(spot.value, value); else => {} } } #! Forgets what every register a call clobbers holds, as a call leaves them: #! all but the general registers a call keeps. #! @param this What is held. def holding::clobber(&mut this): void { for r in 0..16u { if !keeps(r as u8) { this.gprs[r] = 0; } this.xmms[r] = 0; } } #! Makes moves all at once: each reads what its place held before any wrote. #! @param this What is held. #! @param moves The moves. def holding::parallel(&mut this, moves: span): void { var values := vector::::new(scratch()); for &each in moves { values.append(this.at(&each.from)); } for i in 0..moves.length { this.set(&moves[i].to, values[i]); } } #! Replays where a function's values were placed, giving what it finds wrong. #! @param body The code. #! @param p Where its values are. #! @returns What is wrong, one problem each. pub def check_placing(body: &code, p: &placing): vector { var problems := vector::::new(); for k in 0..p.order.length { val block: u32 = p.order[k]; var held := holding { slots = dict::::new(scratch()) }; # What the block holds where it begins, which each edge into it is checked # to leave. for ¶m in body.blocks[block].params { held.set(&p.made_at(param), param); } for &value in p.liveins.of(block) { held.set(&p.located(value, 4 * p.start_slots[block]), value); if p.slot_of[value] != never { held.set(&in_slot(value), value); } } replay_block(body, p, block, &mut held, &mut problems); } return problems; } #! Replays a block from what it holds where it begins, then each edge leaving #! it. #! @param body The code. #! @param p Where its values are. #! @param block The block. #! @param held What is held where it begins. #! @param problems Where what is wrong is written. def replay_block(body: &code, p: &placing, block: u32, held: &mut holding, problems: &mut vector): void { var operands := vector::::new(scratch()); var walk := body.walk(block); while walk.more() { val inst_at := walk.next(); val instruction: inst = body.insts[inst_at]; val slot: u32 = p.slots[inst_at]; held.parallel(p.gap_moves.of(slot)); continue if p.folded[inst_at]; # Each operand is where its value is, and what a select a comparison is # folded into chooses between. p.read_by(body, inst_at, &mut operands); for &value in operands { val spot := p.read_at(value, inst_at); if held.at(&spot) != value && spot.kind != place::none { problems.append($"b{block}: v{value}, read by i{inst_at}, is not spot it was placed"); } } if calls(instruction.kind) { held.clobber(); } for r in 0..instruction.count { val result := instruction.result + r as u32; held.set(&p.made_at(result), result); } } val end: u32 = p.end_slots[block]; held.parallel(p.gap_moves.of(end)); val last: u32 = body.blocks[block].last; return if last == 0; var outs := body.edges_of(last); while outs.more() { val edge := outs.next(); val target: u32 = body.edges[edge].target; continue if p.number[target] == never; var after := held.copied(); after.parallel(p.edge_moves_of(edge)); # Each parameter's place holds the argument the edge gives it. val params := &body.blocks[target].params; for i in 0..params.length { break if i >= body.edges[edge].args.length; val param: u32 = params[i]; val arg: u32 = body.edges[edge].args[i]; val spot := p.made_at(param); if spot.kind != place::none && after.at(&spot) != arg { problems.append($"b{block}: edge {edge} leaves v{arg}, for parameter v{param} of b{target}, out of place"); } } for &value in p.liveins.of(target) { val spot := p.located(value, 4 * p.start_slots[target]); if after.at(&spot) != value { problems.append($"b{block}: edge {edge} leaves v{value}, live into b{target}, out of place"); } } } }