code wiki / _hdl_build / _enc_replace_gate.nx
_enc_replace_gate.nx source
↩ module page · 60 lines · 2929 B
1// _enc_replace_gate.nx -- DIFFERENTIAL-REPLACEMENT of a COMPILER-INTERNAL (eve "inside
2// like a compiler"): is the SEARCH-derived RV64 U-type encoder (nx_evo_enc's champion)
3// behaviorally IDENTICAL to the Claude-authored assembler encoder? The operator's test on
4// the toolchain's GUTS: "turn off Claude's encoder, emit ours, function the same." GREEN
5// across the full in-range domain (every 7-bit opcode x every 5-bit register x an imm20
6// sample) = the team derived a verified drop-in for a piece of the assembler's inside.
7// evo_enc_u is the SEARCH-emitted structure (_evo_enc_champ.nx), not hand-written.
8// enc_u_ref is the REAL Claude encoder inlined VERBATIM from nxasm/asm_enc.nx:39-41
9// (cross-dir import doesn't resolve into _hdl_build; this is the production formula, citable).
10// license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "_evo_enc_champ.nx"
13
14func enc_u_ref(opcode: i64, rd: i64, imm20: i64) -> i64 {
15 return ((imm20 & 0xFFFFF) << 12) | (rd << 7) | opcode
16}
17
18func erg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
19func erg_n(v: i64) -> i64 {
20 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
21 var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0
22 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
23 while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) }
24 return 0
25}
26func erg_imm(i: i64) -> i64 {
27 if i == 0 { return 0 } if i == 1 { return 1 } if i == 2 { return 0xFFFFF } if i == 3 { return 0x80000 }
28 if i == 4 { return 0x7FFFF } if i == 5 { return 0xFFFFE } if i == 6 { return 0x12345 } if i == 7 { return 0xABCDE }
29 if i == 8 { return 0xF0F0F } if i == 9 { return 0x0F0F0 } if i == 10 { return 0x55555 } if i == 11 { return 0xAAAAA }
30 if i == 12 { return 0x13579 } if i == 13 { return 0x2468A } if i == 14 { return 0xBEEF0 } return 0xC0DE0
31}
32
33func main() -> i64 {
34 var bad: i64 = 0
35 var total: i64 = 0
36 var fo: i64 = 0 - 1; var fr: i64 = 0 - 1; var fm: i64 = 0 - 1
37 var o: i64 = 0
38 while o <= 0x7F {
39 var r: i64 = 0
40 while r <= 0x1F {
41 var mi: i64 = 0
42 while mi < 16 {
43 let m: i64 = erg_imm(mi)
44 total = total + 1
45 if evo_enc_u(o, r, m) != enc_u_ref(o, r, m) { bad = bad + 1; if fo < 0 { fo = o; fr = r; fm = m } }
46 mi = mi + 1
47 }
48 r = r + 1
49 }
50 o = o + 1
51 }
52 if bad == 0 {
53 erg_w("ENC-REPLACE GREEN: search-derived evo_enc_u == Claude enc_u (asm_enc.nx:39) on " as *u8); erg_n(total); erg_w(" in-range triples\n" as *u8)
54 } else {
55 erg_w("ENC-REPLACE RED mismatches=" as *u8); erg_n(bad); erg_w("/" as *u8); erg_n(total)
56 erg_w(" first@op=" as *u8); erg_n(fo); erg_w(" rd=" as *u8); erg_n(fr); erg_w(" imm=" as *u8); erg_n(fm); erg_w("\n" as *u8)
57 }
58 sys_exit(bad)
59 return bad
60}