#! @file cells.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains which cells of a function's frame are zeroed as it begins, by an #! executable's prologue and by the machine alike. The code reads a cell as #! zeroed where nothing wrote it yet, so a cell is zeroed as the function #! begins unless every word of it some path reads is written first on every #! path to the read. A cell whose address is given anywhere but to reading, #! writing, copying, and clearing it at offsets the code knows may be read #! anywhere from there on, so where it is given is a read of every word of it: #! a cell written whole before its address goes anywhere is not zeroed, as #! one a statement makes before handing it to a call. A call writing what it #! returns to a cell, at the address its last operand gives, writes it whole. module sakura::compiler; #! The most words of a cell the analysis follows; a larger one is zeroed. const followed_words: u64 = 64; #! Finds which cells a function's prologue zeroes. #! @param body The code. #! @param order Its blocks, in reverse postorder. #! @param number Each block's place in that order. #! @param writes Of each instruction, the bytes a call writes what it #! returns to at its last operand, or 0, as `result_writes` #! gives them. #! @returns Whether each cell is zeroed. pub def zeroed_cells(body: &code, order: span, number: span, writes: span): vector { val cells := body.cells.length; var zeroed := vector::::new(scratch()); zeroed.resize(cells); return zeroed if cells == 0; # A cell too large to follow is zeroed. for c in 0..cells { if (body.cells[c].size as u64 + 7) / 8 > followed_words { zeroed[c] = true; } } val addresses := body.cell_table(); var reads := body.reader(); # The words written for sure where each block ends, by cell, a word a bit, # until they no longer change; each starts as every word, but at the entry. var outs := vector::::new(scratch()); outs.resize(body.blocks.length * cells, 0xFFFFFFFFFFFFFFFF); var state := vector::::new(scratch()); state.resize(cells); var changed := true; var rounds := 0; while changed && rounds < 64 { changed = false; rounds++; for k in 0..order.length { val block: u32 = order[k]; for c in 0..cells { state[c] = block == 0 ? 0 : 0xFFFFFFFFFFFFFFFF; } for p in 0..body.blocks[block].preds.length { val from := body.edge_source(body.blocks[block].preds[p]); continue if number[from] == 0xFFFFFFFF; for c in 0..cells { state[c] = state[c] & outs[from as u64 * cells + c]; } } walk_block(body, block, &addresses, &mut reads, writes, &mut state, &mut zeroed); for c in 0..cells { val at := block as u64 * cells + c; if outs[at] != state[c] { outs[at] = state[c]; changed = true; } } } } if changed { for c in 0..cells { zeroed[c] = true; } } return zeroed; } #! The bits of the words a run of bytes covers wholly, from the first word. #! @param offset Where the run begins. #! @param size How many bytes it takes. #! @returns The bits. def cell_words_whole(offset: u64, size: u64): u64 { val first := (offset + 7) / 8; val end := (offset + size) / 8; return 0 if end <= first || first >= 64; return bits_between(first, end > 64 ? 64 : end); } #! The bits of the words a run of bytes touches, from the first word. #! @param offset Where the run begins. #! @param size How many bytes it takes. #! @returns The bits. def cell_words_touched(offset: u64, size: u64): u64 { return 0 if size == 0; val first := offset / 8; val end := (offset + size + 7) / 8; return 0 if first >= 64; return bits_between(first, end > 64 ? 64 : end); } #! The bits from one up to another. #! @param from The first bit. #! @param to Past the last, at most 64. #! @returns The bits. def bits_between(from: u64, to: u64): u64 { val high := to >= 64 ? 0xFFFFFFFFFFFFFFFF : (1u << to) - 1; val low := (1u << from) - 1; return high & ~low; } #! Walks a block's instructions, from what is written for sure where it begins #! to where it ends, noting each cell a read finds a word of not yet written. #! @param body The code. #! @param block The block. #! @param addresses Which cell each value is the address of. #! @param reads A walk of what the code's instructions read. #! @param writes Of each instruction, the bytes a call writes what it #! returns to at its last operand, or 0. #! @param state What is written of each cell, a word a bit. #! @param zeroed Whether each cell is zeroed. def walk_block( body: &code, block: u32, addresses: &cell_table, reads: &mut read_walk, writes: span, state: &mut vector, zeroed: &mut vector ): void { var walk := body.walk(block); while walk.more() { val at := walk.next(); val held: inst = body.insts[at]; # A cell's address given anywhere but to the reads and writes below, and # to where a call writes what it returns, may be read anywhere from here, # as an edge's argument always may be, so every word of the cell is read # here. val returned: u64 = (at as u64) < writes.length ? writes[at] : 0; var written: u32 = 0; reads.start(at); while reads.more() { val cell := addresses.cell(reads.next()); continue if cell == 0; val a := reads.operand(); val known := a != no_operand && match held.kind { op::load, op::clear, op::store => a == 0, op::copy => a < 2, else => false }; continue if known; if a != no_operand && returned != 0 && a + 1 == held.argc { written = cell; continue; } if (cell_words_touched(0, body.cells[cell - 1].size as u64) & ~state[cell - 1]) != 0 { zeroed[cell - 1] = true; } } if written != 0 { state[written - 1] = state[written - 1] | cell_words_whole(0, returned); } match held.kind { op::load => { val cell := addresses.cell(body.operands[held.args as u64]); continue if cell == 0; val read := cell_words_touched(held.imm, (held.aux as access).width()); if (read & ~state[cell - 1]) != 0 { zeroed[cell - 1] = true; } } op::store => { val cell := addresses.cell(body.operands[held.args as u64]); continue if cell == 0; state[cell - 1] = state[cell - 1] | cell_words_whole(held.imm, (held.aux as access).width()); } op::clear => { val cell := addresses.cell(body.operands[held.args as u64]); continue if cell == 0; state[cell - 1] = state[cell - 1] | cell_words_whole(0, held.imm); } op::copy => { val from := addresses.cell(body.operands[held.args as u64 + 1]); if from != 0 && (cell_words_touched(0, held.imm) & ~state[from - 1]) != 0 { zeroed[from - 1] = true; } val to := addresses.cell(body.operands[held.args as u64]); if to != 0 { state[to - 1] = state[to - 1] | cell_words_whole(0, held.imm); } } else => {} } } }