mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-20 09:02:32 +00:00
311 lines
13 KiB
Odin
311 lines
13 KiB
Odin
// rexcode · Brendan Punsky (dotbmp@github), original author
|
|
|
|
package rexcode_mos6502_tablegen
|
|
|
|
// =============================================================================
|
|
// MOS 6502 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 + DECODE_INDEX_OPCODE
|
|
// generated/writer.odin Stage B: serialize those globals to ../tables/*.bin
|
|
//
|
|
// It also re-emits the library loader ../tables.odin. Run:
|
|
// odin run mos6502/tablegen # Stage A
|
|
// odin run mos6502/tablegen/generated # Stage B
|
|
//
|
|
// 6502 paradigm note: dispatch is by a single first opcode byte, so the decode
|
|
// side is ONE 256-slot opcode->range index (DECODE_INDEX_OPCODE), not the
|
|
// multi-bucket scheme MIPS uses. Each bucket is filtered by the caller's CPU
|
|
// tier at decode time; within a bucket entries are ordered (cpu, mnemonic).
|
|
//
|
|
// Decode_Entry is NOT the same shape as Encoding: it carries the per-instruction
|
|
// `length` and only [3] operand slots (no real 6502 instruction uses the 4th),
|
|
// so it has a dedicated row writer (write_entry) separate from write_row.
|
|
|
|
import "core:fmt"
|
|
import "core:os"
|
|
import "core:strings"
|
|
import "core:slice"
|
|
import "core:reflect"
|
|
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", "mos6502.encode_forms.bin", "Encoding"},
|
|
{"ENCODE_RUNS", "mos6502.encode_runs.bin", "Encode_Run"},
|
|
{"DECODE_ENTRIES", "mos6502.entries.bin", "Decode_Entry"},
|
|
{"DECODE_INDEX_OPCODE", "mos6502.idx_opcode.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,
|
|
opcode: u8,
|
|
length: u8,
|
|
cpu: lib.CPU,
|
|
flags: lib.Encoding_Flags,
|
|
}
|
|
|
|
Range :: struct { start: u16, count: u16 }
|
|
|
|
main :: proc() {
|
|
n := emit_encode_tables()
|
|
ne := emit_decode_tables()
|
|
emit_writer()
|
|
emit_loader()
|
|
fmt.printfln("mos6502 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_mos6502_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.opcode, f.length, f.cpu, 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..<run_w-len(name) { strings.write_byte(&sb, ' ') }
|
|
fmt.sbprintfln(&sb, " = {{% 5d, % 3d}},", start, c)
|
|
start += c
|
|
}
|
|
strings.write_string(&sb, "}\n")
|
|
emit_file(DIR_GEN + "encode_tables.odin", &sb)
|
|
return
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Decode side
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// Ported from the old tools/gen_decode_tables.odin: flatten every form into a
|
|
// single list, sort by (opcode asc, cpu asc, mnemonic asc), then build one
|
|
// 256-slot opcode->range index. Sorting CPU within an opcode bucket puts the
|
|
// NMOS-official entry before NMOS_UNDOC / 65C02 / HuC, so the decoder's CPU
|
|
// tier filter selects the right meaning for a given target.
|
|
|
|
emit_decode_tables :: proc() -> (total: int) {
|
|
all: [dynamic]Entry
|
|
defer delete(all)
|
|
for m in Mnemonic {
|
|
for f in ENCODING_TABLE[m] {
|
|
append(&all, Entry{f.mnemonic, f.ops, f.enc, f.opcode, f.length, f.cpu, f.flags})
|
|
}
|
|
}
|
|
slice.sort_by(all[:], proc(a, b: Entry) -> bool {
|
|
if a.opcode != b.opcode { return a.opcode < b.opcode }
|
|
if a.cpu != b.cpu { return u8(a.cpu) < u8(b.cpu) }
|
|
return u16(a.mnemonic) < u16(b.mnemonic)
|
|
})
|
|
|
|
opcode_idx: [256]Range
|
|
for e, i in all {
|
|
push(&opcode_idx[e.opcode], u16(i))
|
|
}
|
|
|
|
sb := strings.builder_make()
|
|
strings.write_string(&sb, "package rexcode_mos6502_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 the first opcode byte.\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_entry(&sb, e.mnemonic, e.ops, e.enc, e.opcode, e.length, e.cpu, e.flags)
|
|
}
|
|
strings.write_string(&sb, "}\n\n")
|
|
|
|
emit_range(&sb, "DECODE_INDEX_OPCODE", opcode_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 mos6502's original generators)
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Encoding row: full [4] operand slots (matches lib.Encoding, 14 bytes).
|
|
write_row :: proc(sb: ^strings.Builder, mn: lib.Mnemonic, ops: [4]lib.Operand_Type,
|
|
enc: [4]lib.Operand_Encoding, opcode, length: u8, cpu: lib.CPU, flags: lib.Encoding_Flags) {
|
|
fmt.sbprintf(sb, "\t{{ .%v, {{.%v,.%v,.%v,.%v}}, {{.%v,.%v,.%v,.%v}}, 0x%02X, %d, .%v, {{%s}} }},\n",
|
|
mn, ops[0], ops[1], ops[2], ops[3], enc[0], enc[1], enc[2], enc[3], opcode, length, cpu, flags_lit(flags))
|
|
}
|
|
|
|
// Decode entry row: only [3] operand slots (matches lib.Decode_Entry, 12 bytes;
|
|
// the 4th operand slot is always .NONE on the 6502 and is dropped).
|
|
write_entry :: proc(sb: ^strings.Builder, mn: lib.Mnemonic, ops: [4]lib.Operand_Type,
|
|
enc: [4]lib.Operand_Encoding, opcode, length: u8, cpu: lib.CPU, flags: lib.Encoding_Flags) {
|
|
fmt.sbprintf(sb, "\t{{ .%v, {{.%v,.%v,.%v}}, {{.%v,.%v,.%v}}, 0x%02X, %d, .%v, {{%s}} }},\n",
|
|
mn, ops[0], ops[1], ops[2], enc[0], enc[1], enc[2], opcode, length, cpu, flags_lit(flags))
|
|
}
|
|
|
|
// Emit the FULL Encoding_Flags -- dropping any field silently corrupts encoding.
|
|
flags_lit :: proc(f: lib.Encoding_Flags) -> string {
|
|
parts: [dynamic]string
|
|
defer delete(parts)
|
|
if f.decimal { append(&parts, "decimal=true") }
|
|
if f.page_cross { append(&parts, "page_cross=true") }
|
|
if f.branch { append(&parts, "branch=true") }
|
|
if f.cond_branch { append(&parts, "cond_branch=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_mos6502_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,
|
|
}
|
|
|
|
// One reverse-decode entry. Differs from Encoding: carries the instruction's
|
|
// total byte length and only [3] operand slots (no 6502 instruction uses 4).
|
|
Decode_Entry :: struct #packed {
|
|
mnemonic: Mnemonic, // 2
|
|
ops: [3]Operand_Type, // 3
|
|
enc: [3]Operand_Encoding, // 3
|
|
opcode: u8, // 1
|
|
length: u8, // 1
|
|
cpu: CPU, // 1
|
|
flags: Encoding_Flags, // 1
|
|
}
|
|
#assert(size_of(Decode_Entry) == 12)
|
|
|
|
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_mos6502\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..<gmax-len(b.global) { strings.write_byte(&sb, ' ') }
|
|
path := fmt.tprintf("\"tables/%s\",", b.file)
|
|
fmt.sbprintf(&sb, " := #load(%s", path)
|
|
for _ in 0..<fmax-len(b.file) { strings.write_byte(&sb, ' ') }
|
|
fmt.sbprintfln(&sb, " []%s)", b.typ)
|
|
}
|
|
strings.write_string(&sb, "\n")
|
|
strings.write_string(&sb, LOADER_ACCESSORS)
|
|
emit_file(PATH_LOADER, &sb)
|
|
}
|
|
|
|
GEN_ATTRIB :: "// rexcode · Brendan Punsky (dotbmp@github), original author\n\n"
|
|
|
|
emit_file :: proc(path: string, sb: ^strings.Builder) {
|
|
if err := os.write_entire_file(path, transmute([]u8)strings.concatenate({GEN_ATTRIB, strings.to_string(sb^)})); err != nil {
|
|
fmt.eprintfln("rexcode tablegen: failed to write %s: %v", path, err)
|
|
os.exit(1)
|
|
}
|
|
}
|