#! @file printer.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the printing of code as text, which is how the tests say what code #! they expect, and how code is read while debugging. module sakura::compiler; #! Prints the code. Each block is written as its name and parameters, then its #! instructions, one to a line: #! #! ``` #! b0(v0: i64): #! v1 = iconst 1 #! v2 = add v0, v1 #! branch v2, b1(v2), b2 #! ``` #! #! Blocks are written in reverse postorder, and values and blocks are numbered #! in the order they are written, so code that is the same is printed the same #! however it was made. The cells of the frame are listed first, as `c0: 16, 8` #! for one of 16 bytes aligned to 8. #! @param this The code. #! @returns The text. pub def code::print(&this): string { var out := string::new(nothing {}); var names := code_names { values = vector::::new(), blocks = vector::::new(), next = 0 }; names.values.resize(this.origins.length); names.blocks.resize(this.blocks.length); # Blocks are written in reverse postorder, each before those it goes to but # along the edges going back, as control mostly flows. var order := vector::::new(); var number := vector::::new(); this.reverse_postorder(&mut order, &mut number); for i in 0..order.length { names.blocks[order[i]] = i as u32; } for i in 0..this.cells.length { val held: cell = this.cells[i]; out.append($"c{i}: {held.size}, {held.align}\n"); } for &b in order { continue if this.blocks[b].gone; out.append($"b{names.blocks[b]}"); val params := this.blocks[b].params.length; if params != 0 { out.append("("); for i in 0..params { if i != 0 { out.append(", "); } val value: u32 = this.blocks[b].params[i]; this.print_value(value, &mut names, &mut out); out.append(": "); out.append(type_name(this.type_of(value))); } out.append(")"); } out.append(":\n"); var walk := this.walk(b); while walk.more() { out.append(" "); this.print_inst(walk.next(), &mut names, &mut out); out.append("\n"); } } return out; } #! How the printer numbers what it writes. struct code_names { var values: vector; #! One more than the number of each value, or 0 before it is written. var blocks: vector; #! The number of each block. var next: u32; #! The number the next value written takes. } #! The name of a type, as the printed code writes it. #! @param kind The type. #! @returns Its name. def type_name(kind: vt): strview { return match kind { vt::word => "i64", vt::real => "f64", vt::v128 => "v128", vt::v256 => "v256", vt::v512 => "v512", else => "none" }; } #! Writes a value's name, numbering it where it is new. #! @param this The code. #! @param value The value. #! @param names How the printer numbers what it writes. #! @param out Where it is written. def code::print_value(&this, value: u32, names: &mut code_names, out: &mut string): void { if value == 0 || value as u64 >= names.values.length { out.append("v?"); return; } if names.values[value] == 0 { names.next++; names.values[value] = names.next; } out.append($"v{names.values[value] - 1}"); } #! Writes the values of a run of operands, separated by commas. #! @param this The code. #! @param index The instruction. #! @param from The first operand to write. #! @param names How the printer numbers what it writes. #! @param out Where it is written. def code::print_args(&this, index: u32, from: u32, names: &mut code_names, out: &mut string): void { val held: inst = this.insts[index]; for i in from..held.argc { if i != from { out.append(", "); } this.print_value(this.arg(index, i as u32), names, out); } } #! Writes an edge: the block it goes to, and its arguments where it gives any. #! @param this The code. #! @param edge The edge. #! @param names How the printer numbers what it writes. #! @param out Where it is written. def code::print_edge(&this, edge: u32, names: &mut code_names, out: &mut string): void { val target: u32 = this.edges[edge].target; out.append($"b{names.blocks[target]}"); val count := this.edges[edge].args.length; return if count == 0; out.append("("); for i in 0..count { if i != 0 { out.append(", "); } this.print_value(this.edges[edge].args[i], names, out); } out.append(")"); } #! Writes an instruction. #! @param this The code. #! @param index The instruction. #! @param names How the printer numbers what it writes. #! @param out Where it is written. def code::print_inst(&this, index: u32, names: &mut code_names, out: &mut string): void { val held: inst = this.insts[index]; for i in 0..held.count { if i != 0 { out.append(", "); } this.print_value(held.result + i as u32, names, out); } if held.count != 0 { out.append(" = "); } out.append(held.kind.name()); match held.kind { op::iconst => out.append($" {held.imm as i64}"); op::fconst => out.append($" {real_of(held.imm)}"); op::wrap => { out.append(" "); this.print_args(index, 0, names, out); out.append(held.imm != 0 ? $", {held.aux}, signed" : $", {held.aux}, unsigned"); } op::cell => out.append($" c{held.aux}"); op::global => out.append($" {held.imm}"); op::datum => out.append($" d{held.aux}+{held.imm}"); op::load => { out.append($".{(held.aux as access).name()} "); this.print_args(index, 0, names, out); if held.imm != 0 { out.append($"+{held.imm}"); } } op::store => { out.append($".{(held.aux as access).name()} "); this.print_value(this.arg(index, 0), names, out); if held.imm != 0 { out.append($"+{held.imm}"); } out.append(", "); this.print_value(this.arg(index, 1), names, out); } op::copy, op::clear, op::forget => { out.append(" "); this.print_args(index, 0, names, out); out.append($", {held.imm}"); } op::call => { out.append($" f{held.aux}("); this.print_args(index, 0, names, out); out.append(")"); } op::call_trait => { out.append($" s{held.aux}[{held.imm}]("); this.print_args(index, 0, names, out); out.append(")"); } op::call_value, op::call_c => { out.append($" s{held.aux}("); this.print_args(index, 0, names, out); out.append(")"); } op::c_address => out.append($" x{held.aux}"); op::callback, op::safepoint => out.append($" f{held.aux}"); op::equal => { out.append(" "); this.print_args(index, 0, names, out); out.append($", l{held.aux}"); } op::retrait => { out.append(" "); this.print_args(index, 0, names, out); out.append($", r{held.aux}"); } op::check => { out.append(" "); this.print_args(index, 0, names, out); out.append($", {error_name(held.aux)}"); } op::format_text => out.append($" d{held.aux}"); op::ready => out.append($" e{held.aux}"); op::reach => { out.append(" "); this.print_args(index, 0, names, out); out.append($", {held.imm}"); } op::lanes => { out.append($".{held.aux}.{held.imm & 0xFF}.{held.imm >> 8} "); this.print_args(index, 0, names, out); } op::atomic => { out.append($".{atomic_names[held.aux as u64]}.{atomic_access(held.imm).name()}"); if (held.imm & atomic_unsigned) != 0 { out.append(".unsigned"); } out.append(" "); this.print_args(index, 0, names, out); } op::format_value => { out.append($" l{held.aux}"); if held.argc != 0 { out.append(", "); this.print_args(index, 0, names, out); } } op::jump => { out.append(" "); this.print_edge(held.aux, names, out); } op::branch => { out.append(" "); this.print_args(index, 0, names, out); out.append(", "); this.print_edge(held.aux, names, out); out.append(", "); this.print_edge(held.aux + 1, names, out); } op::switch => { out.append(" "); this.print_args(index, 0, names, out); out.append(", ["); val cases := held.imm as u32 - 1; for i in 0..cases { if i != 0 { out.append(", "); } val edge := held.aux + i as u32; out.append($"{this.edges[edge].case}: "); this.print_edge(edge, names, out); } out.append("], "); this.print_edge(held.aux + cases, names, out); } op::trap => { out.append(" "); if held.argc != 0 { this.print_args(index, 0, names, out); } else { out.append(error_name(held.aux)); } } else => { if held.argc != 0 { out.append(" "); this.print_args(index, 0, names, out); } } } } #! The name of an error the machine stops a program with, as printed. #! @param code The error, as the machine numbers them. #! @returns Its name. pub def error_name(code: u32): strview { return match code { fault::out_of_bounds as u32 => "out_of_bounds", fault::null_reference as u32 => "null_reference", fault::division as u32 => "division", fault::not_held as u32 => "not_held", fault::unreachable as u32 => "unreachable", fault::not_member as u32 => "not_member", fault::not_constant as u32 => "not_constant", else => "error" }; }