// rexcode · Brendan Punsky (dotbmp@github), original author package rexcode_mips_tablegen // ============================================================================= // MIPS TABLE GENERATOR (Stage A) // ============================================================================= // // Reads the single-source-of-truth ENCODING_TABLE (encoding_table.odin, this // package) and emits human-readable, type-checked Odin into ./generated/: // // generated/encode_tables.odin ENCODE_FORMS + ENCODE_RUNS (flattened encode) // generated/decode_tables.odin DECODE_ENTRIES + primary/SPECIAL/REGIMM/COP1/ // SPECIAL2/SPECIAL3 index tables // generated/writer.odin Stage B: serialize those globals to ../tables/*.bin // // It also re-emits the library loader ../tables.odin. Run: // odin run mips/tablegen # Stage A // odin run mips/tablegen/generated # Stage B import "core:fmt" import "core:os" import "core:strings" import "core:slice" import "core:reflect" import "core:math/bits" import lib "../" // Package-scope aliases so the moved SoT resolves Mnemonic/Encoding unqualified. Encoding :: lib.Encoding Mnemonic :: lib.Mnemonic Blob :: struct { global, file, typ: string } BLOBS := [?]Blob{ {"ENCODE_FORMS", "mips.encode_forms.bin", "Encoding"}, {"ENCODE_RUNS", "mips.encode_runs.bin", "Encode_Run"}, {"DECODE_ENTRIES", "mips.entries.bin", "Decode_Entry"}, {"DECODE_INDEX_PRIMARY", "mips.idx_primary.bin", "Decode_Index"}, {"DECODE_INDEX_SPECIAL", "mips.idx_special.bin", "Decode_Index"}, {"DECODE_INDEX_REGIMM", "mips.idx_regimm.bin", "Decode_Index"}, {"DECODE_INDEX_COP1", "mips.idx_cop1.bin", "Decode_Index"}, {"DECODE_INDEX_SPECIAL2", "mips.idx_special2.bin", "Decode_Index"}, {"DECODE_INDEX_SPECIAL3", "mips.idx_special3.bin", "Decode_Index"}, } DIR_GEN :: #directory + "/generated/" PATH_LOADER :: #directory + "/../tables.odin" Entry :: struct { mnemonic: lib.Mnemonic, ops: [4]lib.Operand_Type, enc: [4]lib.Operand_Encoding, bits: u32, mask: u32, feature: lib.Feature, flags: lib.Encoding_Flags, primary_op: u8, sub_key: u8, } Range :: struct { start: u16, count: u16 } main :: proc() { n := emit_encode_tables() ne := emit_decode_tables() emit_writer() emit_loader() fmt.printfln("mips tablegen: %d encode forms, %d decode entries", n, ne) } // ----------------------------------------------------------------------------- // Encode side // ----------------------------------------------------------------------------- emit_encode_tables :: proc() -> (total: int) { sb := strings.builder_make() strings.write_string(&sb, "package rexcode_mips_generated\n\n") strings.write_string(&sb, "// GENERATED by ../gen.odin -- DO NOT EDIT.\n") strings.write_string(&sb, "// Flattened encode forms + per-mnemonic run index (source: ENCODING_TABLE).\n\n") strings.write_string(&sb, "import lib \"../..\"\n\n") for m in Mnemonic { total += len(ENCODING_TABLE[m]) } strings.write_string(&sb, "@(rodata)\n") fmt.sbprintfln(&sb, "ENCODE_FORMS := [%d]lib.Encoding{{", total) for m in Mnemonic { forms := ENCODING_TABLE[m] if len(forms) == 0 { continue } fmt.sbprintfln(&sb, "\t// .%v", m) for f in forms { write_row(&sb, f.mnemonic, f.ops, f.enc, f.bits, f.mask, f.feature, f.flags) } } strings.write_string(&sb, "}\n\n") run_w := 0 for m in Mnemonic { run_w = max(run_w, len(reflect.enum_string(m))) } strings.write_string(&sb, "@(rodata)\n") strings.write_string(&sb, "ENCODE_RUNS := [lib.Mnemonic]lib.Encode_Run{\n") start := 0 for m in Mnemonic { c := len(ENCODING_TABLE[m]) name := reflect.enum_string(m) fmt.sbprintf(&sb, "\t.%s", name) for _ in 0.. (total: int) { all: [dynamic]Entry defer delete(all) for m in Mnemonic { for f in ENCODING_TABLE[m] { primary := u8((f.bits >> 26) & 0x3F) sub: u8 switch primary { case 0x00, 0x1C, 0x1F: sub = u8(f.bits & 0x3F) case 0x01: sub = u8((f.bits >> 16) & 0x1F) case 0x11: sub = u8((f.bits >> 21) & 0x1F) } append(&all, Entry{f.mnemonic, f.ops, f.enc, f.bits, f.mask, f.feature, f.flags, primary, sub}) } } slice.sort_by(all[:], proc(a, b: Entry) -> bool { if a.primary_op != b.primary_op { return a.primary_op < b.primary_op } if a.sub_key != b.sub_key { return a.sub_key < b.sub_key } ac := bits.count_ones(a.mask); bc := bits.count_ones(b.mask) if ac != bc { return ac > bc } return u16(a.mnemonic) < u16(b.mnemonic) }) primary_idx: [64]Range special_idx: [64]Range regimm_idx: [32]Range cop1_idx: [32]Range special2_idx: [64]Range special3_idx: [64]Range for e, i in all { push(&primary_idx[e.primary_op], u16(i)) switch e.primary_op { case 0x00: push(&special_idx [e.sub_key], u16(i)) case 0x01: push(®imm_idx [e.sub_key], u16(i)) case 0x11: push(&cop1_idx [e.sub_key], u16(i)) case 0x1C: push(&special2_idx[e.sub_key], u16(i)) case 0x1F: push(&special3_idx[e.sub_key], u16(i)) } } sb := strings.builder_make() strings.write_string(&sb, "package rexcode_mips_generated\n\n") strings.write_string(&sb, "// GENERATED by ../gen.odin -- DO NOT EDIT.\n") strings.write_string(&sb, "// Reverse decode tables (source: ENCODING_TABLE), keyed by primary opcode + sub-field.\n\n") strings.write_string(&sb, "import lib \"../..\"\n\n") strings.write_string(&sb, "@(rodata)\n") fmt.sbprintfln(&sb, "DECODE_ENTRIES := [%d]lib.Decode_Entry{{", len(all)) for e in all { write_row(&sb, e.mnemonic, e.ops, e.enc, e.bits, e.mask, e.feature, e.flags) } strings.write_string(&sb, "}\n\n") emit_range(&sb, "DECODE_INDEX_PRIMARY", primary_idx[:]) emit_range(&sb, "DECODE_INDEX_SPECIAL", special_idx[:]) emit_range(&sb, "DECODE_INDEX_REGIMM", regimm_idx[:]) emit_range(&sb, "DECODE_INDEX_COP1", cop1_idx[:]) emit_range(&sb, "DECODE_INDEX_SPECIAL2", special2_idx[:]) emit_range(&sb, "DECODE_INDEX_SPECIAL3", special3_idx[:]) emit_file(DIR_GEN + "decode_tables.odin", &sb) return len(all) } push :: proc(r: ^Range, i: u16) { if r.count == 0 { r.start = i }; r.count += 1 } emit_range :: proc(sb: ^strings.Builder, name: string, ranges: []Range) { strings.write_string(sb, "@(rodata)\n") fmt.sbprintfln(sb, "%s := [%d]lib.Decode_Index{{", name, len(ranges)) for r, i in ranges { if r.count != 0 { fmt.sbprintfln(sb, "\t0x%02X = {{% 4d, % 3d}},", i, r.start, r.count) } } strings.write_string(sb, "}\n\n") } // ----------------------------------------------------------------------------- // Shared row + flags formatting (compact, matching mips' original generator) // ----------------------------------------------------------------------------- write_row :: proc(sb: ^strings.Builder, mn: lib.Mnemonic, ops: [4]lib.Operand_Type, enc: [4]lib.Operand_Encoding, bits, mask: u32, feature: lib.Feature, flags: lib.Encoding_Flags) { fmt.sbprintf(sb, "\t{{ .%v, {{.%v,.%v,.%v,.%v}}, {{.%v,.%v,.%v,.%v}}, 0x%08X, 0x%08X, .%v, {{%s}} }},\n", mn, ops[0], ops[1], ops[2], ops[3], enc[0], enc[1], enc[2], enc[3], bits, mask, feature, flags_lit(flags)) } flags_lit :: proc(f: lib.Encoding_Flags) -> string { parts: [dynamic]string defer delete(parts) if f.delay_slot { append(&parts, "delay_slot=true") } if f.likely { append(&parts, "likely=true") } if f.only_64 { append(&parts, "only_64=true") } if f.writes_hilo { append(&parts, "writes_hilo=true") } if f.compact { append(&parts, "compact=true") } return strings.join(parts[:], ", ", context.temp_allocator) } // ----------------------------------------------------------------------------- // Stage B writer + the library loader // ----------------------------------------------------------------------------- emit_writer :: proc() { sb := strings.builder_make() strings.write_string(&sb, "package rexcode_mips_generated\n\n") strings.write_string(&sb, "// GENERATED by ../gen.odin -- DO NOT EDIT.\n") strings.write_string(&sb, "// Stage B: serialize the typed tables above to raw blobs under ../../tables/.\n\n") strings.write_string(&sb, "import \"core:os\"\nimport \"core:fmt\"\n\n") strings.write_string(&sb, "TABLES :: #directory + \"/../../tables/\"\n\n") strings.write_string(&sb, "raw :: #force_inline proc \"contextless\" (p: rawptr, n: int) -> []u8 {\n\treturn (cast([^]u8)p)[:n]\n}\n\n") strings.write_string(&sb, "w :: proc(file: string, data: []u8) {\n") strings.write_string(&sb, "\tif err := os.write_entire_file(file, data); err != nil {\n") strings.write_string(&sb, "\t\tfmt.eprintfln(\"rexcode tablegen: failed to write %s: %v\", file, err)\n\t\tos.exit(1)\n\t}\n}\n\n") strings.write_string(&sb, "main :: proc() {\n") for b in BLOBS { fmt.sbprintfln(&sb, "\tw(TABLES + \"%s\", raw(&%s, size_of(%s)))", b.file, b.global, b.global) } strings.write_string(&sb, "}\n") emit_file(DIR_GEN + "writer.odin", &sb) } LOADER_TYPES :: `// ----------------------------------------------------------------------------- // Subsidiary table types (generated scaffolding) // ----------------------------------------------------------------------------- // Companion run index: ENCODE_RUNS[mnemonic] -> contiguous run in ENCODE_FORMS. Encode_Run :: struct { start: u32, count: u32, } Decode_Entry :: struct #packed { mnemonic: Mnemonic, // 2 ops: [4]Operand_Type, // 4 enc: [4]Operand_Encoding, // 4 bits: u32, // 4 mask: u32, // 4 feature: Feature, // 1 flags: Encoding_Flags, // 1 } #assert(size_of(Decode_Entry) == 20) Decode_Index :: struct #packed { start: u16, count: u16, } #assert(size_of(Decode_Index) == 4) ` LOADER_ACCESSORS :: `// ----------------------------------------------------------------------------- // Accessors // ----------------------------------------------------------------------------- // Per-mnemonic encode forms: the run of ENCODE_FORMS belonging to ` + "`m`" + `. // Replaces the old ENCODING_TABLE[m] slice; the returned view is into rodata. @(private, require_results) encoding_forms :: #force_inline proc "contextless" (m: Mnemonic) -> []Encoding { r := ENCODE_RUNS[u16(m)] return ENCODE_FORMS[r.start:][:r.count] } ` emit_loader :: proc() { sb := strings.builder_make() strings.write_string(&sb, "package rexcode_mips\n\n") strings.write_string(&sb, "// =============================================================================\n") strings.write_string(&sb, "// GENERATED FILE - DO NOT EDIT\n") strings.write_string(&sb, "// =============================================================================\n") strings.write_string(&sb, "//\n") strings.write_string(&sb, "// Loads the flat binary encode/decode tables into @(rodata). Produced by tablegen:\n") strings.write_string(&sb, "//\n") strings.write_string(&sb, "// odin run tablegen # Stage A: ENCODING_TABLE -> generated/ + this file\n") strings.write_string(&sb, "// odin run tablegen/generated # Stage B: typed Odin literals -> tables/*.bin\n") strings.write_string(&sb, "//\n") strings.write_string(&sb, "// The .bin blobs are raw, host-endian, packed struct images.\n\n") strings.write_string(&sb, LOADER_TYPES) strings.write_string(&sb, "\n// -----------------------------------------------------------------------------\n") strings.write_string(&sb, "// Loaded tables (rodata, embedded from tables/*.bin at compile time)\n") strings.write_string(&sb, "// -----------------------------------------------------------------------------\n\n") gmax, fmax := 0, 0 for b in BLOBS { gmax = max(gmax, len(b.global)); fmax = max(fmax, len(b.file)) } for b in BLOBS { fmt.sbprintf(&sb, "@(rodata) %s", b.global) for _ in 0..