mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-20 09:02:32 +00:00
298 lines
12 KiB
Odin
298 lines
12 KiB
Odin
// rexcode · Brendan Punsky (dotbmp@github), original author
|
|
|
|
package rexcode_arm64_tablegen
|
|
|
|
// =============================================================================
|
|
// AArch64 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_OP0 index table
|
|
// generated/writer.odin Stage B: serialize those globals to ../../tables/*.bin
|
|
//
|
|
// It also re-emits the library loader ../tables.odin. Run:
|
|
// odin run arm64/tablegen # Stage A
|
|
// odin run arm64/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", "arm64.encode_forms.bin", "Encoding"},
|
|
{"ENCODE_RUNS", "arm64.encode_runs.bin", "Encode_Run"},
|
|
{"DECODE_ENTRIES", "arm64.entries.bin", "Decode_Entry"},
|
|
{"DECODE_INDEX_OP0", "arm64.idx_op0.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,
|
|
op0: u8, // bits[28:25]
|
|
}
|
|
|
|
Range :: struct { start: u16, count: u16 }
|
|
|
|
main :: proc() {
|
|
n := emit_encode_tables()
|
|
ne := emit_decode_tables()
|
|
emit_writer()
|
|
emit_loader()
|
|
fmt.printfln("arm64 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_arm64_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..<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
|
|
// -----------------------------------------------------------------------------
|
|
//
|
|
// AArch64 has no single primary-opcode field; the ARM ARM divides the ISA by
|
|
// `op0` at bits[28:25] into a 16-slot dispatch index. Each encoding replicates
|
|
// across every op0 bucket whose value is consistent with the form's masked
|
|
// static pattern. Mask popcount descending sort lets the most-specific entry
|
|
// match first within a bucket.
|
|
|
|
emit_decode_tables :: proc() -> (total: int) {
|
|
all: [dynamic]Entry
|
|
defer delete(all)
|
|
for mn in Mnemonic {
|
|
for f in ENCODING_TABLE[mn] {
|
|
op0_static := u8((f.bits >> 25) & 0xF)
|
|
op0_mask := u8((f.mask >> 25) & 0xF)
|
|
// Enumerate every 4-bit bucket B such that (B & op0_mask) == op0_static.
|
|
for b: u8 = 0; b < 16; b += 1 {
|
|
if (b & op0_mask) != op0_static { continue }
|
|
append(&all, Entry{f.mnemonic, f.ops, f.enc, f.bits, f.mask, f.feature, f.flags, b})
|
|
}
|
|
}
|
|
}
|
|
slice.sort_by(all[:], proc(a, b: Entry) -> bool {
|
|
if a.op0 != b.op0 { return a.op0 < b.op0 }
|
|
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)
|
|
})
|
|
|
|
op0_idx: [16]Range
|
|
for e, i in all { push(&op0_idx[e.op0], u16(i)) }
|
|
|
|
sb := strings.builder_make()
|
|
strings.write_string(&sb, "package rexcode_arm64_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 op0 (bits 28:25).\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_OP0", op0_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 arm64's 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.branch { append(&parts, "branch=true") }
|
|
if f.cond_branch { append(&parts, "cond_branch=true") }
|
|
if f.writes_pc { append(&parts, "writes_pc=true") }
|
|
if f.sets_flags { append(&parts, "sets_flags=true") }
|
|
if f.is_64 { append(&parts, "is_64=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_arm64_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_arm64\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)
|
|
}
|
|
}
|