code wiki / _hdl_build / nx_js_jit_keystone.nx

nx_js_jit_keystone.nx source

↩ module page · 119 lines · 5901 B

1// nx_js_jit_keystone.nx -- P5 BASELINE-JIT KEYSTONE (SOTA ladder, after P4 ICs; mirrors how the VM 2// arc itself began: nx_js_vm_bench proved 376x BEFORE the compiler was built). QUESTION: can the 3// sovereign stack EMIT x86-64 at runtime and EXECUTE it as the third engine tier, and which class 4// band does native code hit on the SAME kernel the SOTA bench measures? Machinery already probed 5// GREEN by nx_nxe_exec_probe (RWX mmap prot=7; SysV: arg0 rdi, ret rax; cast fn-ptr call). 6// This emits the K1 dispatch kernel (sum 0..N-1, a REAL counted loop) as 18 bytes of x86-64: 7// 31 c0 xor eax, eax ; sum = 0 8// 31 c9 xor ecx, ecx ; i = 0 9// 48 39 f9 loop: cmp rcx, rdi ; i vs N 10// 7d 08 jge end 11// 48 01 c8 add rax, rcx ; sum += i 12// 48 ff c1 inc rcx ; i++ 13// eb f3 jmp loop 14// c3 end: ret 15// and races it against the SAME semantics through the bytecode VM (compile_run) and the tree-walker. 16// HONEST FRAME: this is a KEYSTONE -- a hand-templated kernel proving emission+execution+the band -- 17// NOT a JIT. The Sparkplug-tier build (per-opcode x86 templates off our bytecode) rides this proof. 18// expect_exit: 0 license_tier: ORIGINAL 19import "nx_js_vm.nx" 20const K_MAGIC_4096: i64 = 4096 21const K_MAGIC_499500: i64 = 499500 22const K_MAGIC_10000000: i64 = 10000000 23const K_MAGIC_49999995000000: i64 = 49999995000000 24const K_MAGIC_1000000: i64 = 1000000 25const K_MAGIC_499999500000: i64 = 499999500000 26const K_MAGIC_199990000: i64 = 199990000 27const K_MAGIC_20000: i64 = 20000 28const K_MAGIC_500000000: i64 = 500000000 29const K_MAGIC_100000000: i64 = 100000000 30 31// PROT_READ|PROT_WRITE|PROT_EXEC = 7, MAP_PRIVATE|MAP_ANONYMOUS = 0x22 (probe-proven) 32func jk_mmap_rwx(size: i64) -> *u8 { let r: i64 = __syscall(SYS_MMAP, 0, size, 7, 0x22, -1, 0); return r as *u8 } 33 34func main(argc: i64, argv: *i64) -> i64 { 35 bw("=== nx_js_jit_keystone: EMIT x86 at runtime -> the third tier (tree -> VM -> native) ===\n" as *u8) 36 let code: *u8 = jk_mmap_rwx(K_MAGIC_4096) 37 if (code as i64) < 0 { bw("RED: RWX mmap failed\n" as *u8); return 1 } 38 code[0] = 0x31 as u8 39 code[1] = 0xc0 as u8 40 code[2] = 0x31 as u8 41 code[3] = 0xc9 as u8 42 code[4] = 0x48 as u8 43 code[5] = 0x39 as u8 44 code[6] = 0xf9 as u8 45 code[7] = 0x7d as u8 46 code[8] = 0x08 as u8 47 code[9] = 0x48 as u8 48 code[10] = 0x01 as u8 49 code[11] = 0xc8 as u8 50 code[12] = 0x48 as u8 51 code[13] = 0xff as u8 52 code[14] = 0xc1 as u8 53 code[15] = 0xeb as u8 54 code[16] = 0xf3 as u8 55 code[17] = 0xc3 as u8 56 let fp: func(i64) -> i64 = (code as i64) as func(i64) -> i64 57 // correctness first: small-N cross-check against the interpreter on the SAME source shape 58 let warm: i64 = fp(1000) 59 if warm != K_MAGIC_499500 { bw("RED: emitted code wrong (f(1000)=" as *u8); bn(warm); bw(" want 499500)\n" as *u8); return 1 } 60 // ---- tier 3: NATIVE (emitted x86), 10M iterations ---- 61 let jn: i64 = K_MAGIC_10000000 62 let j0: i64 = sys_now_us() 63 let jr: i64 = fp(jn) 64 let j1: i64 = sys_now_us() 65 var jus: i64 = j1 - j0 66 if jus < 1 { jus = 1 } 67 if jr != K_MAGIC_49999995000000 { bw("RED: emitted code wrong at 10M (got " as *u8); bn(jr); bw(")\n" as *u8); return 1 } 68 let jrate: i64 = jn * K_MAGIC_1000000 / jus 69 // ---- tier 2: bytecode VM, 1M iterations (same semantics, rate is scale-invariant) ---- 70 let vsrc: *u8 = "var s=0;var i=0;while(i<1000000){s=s+i;i++;}s" as *u8 71 let vout: *i64 = sys_mmap(16) as *i64 72 let v0: i64 = sys_now_us() 73 let vrc: i64 = compile_run(vsrc, vout) 74 let v1: i64 = sys_now_us() 75 var vus: i64 = v1 - v0 76 if vus < 1 { vus = 1 } 77 var vok: i64 = 0 78 if vrc == 0 { if vout[0] == VAL_NUM { if vout[1] == K_MAGIC_499999500000 { vok = 1 } } } 79 if vok == 0 { bw("RED: VM kernel wrong\n" as *u8); return 1 } 80 let vrate: i64 = K_MAGIC_1000000 * K_MAGIC_1000000 / vus 81 // ---- tier 1: tree-walker, 20K iterations (full-N pages OOM; rate scale-invariant) ---- 82 let tsrc: *u8 = "var s=0;var i=0;while(i<20000){s=s+i;i++;}s" as *u8 83 let tout: *i64 = sys_mmap(16) as *i64 84 let t0: i64 = sys_now_us() 85 let trc: i64 = js_run_source(tsrc, bsl(tsrc), tout) 86 let t1: i64 = sys_now_us() 87 var tus: i64 = t1 - t0 88 if tus < 1 { tus = 1 } 89 var tok: i64 = 0 90 if trc == 0 { if tout[0] == VAL_NUM { if tout[1] == K_MAGIC_199990000 { tok = 1 } } } 91 if tok == 0 { bw("RED: tree kernel wrong\n" as *u8); return 1 } 92 let trate: i64 = K_MAGIC_20000 * K_MAGIC_1000000 / tus 93 // ---- the three-tier ladder, one box, one run ---- 94 bw(" tier1 TREE-WALKER : " as *u8) 95 bn(trate) 96 bw(" iters/s\n" as *u8) 97 bw(" tier2 BYTECODE VM : " as *u8) 98 bn(vrate) 99 bw(" iters/s (" as *u8) 100 bn(vrate / trate) 101 bw("x tree)\n" as *u8) 102 bw(" tier3 EMITTED x86 : " as *u8) 103 bn(jrate) 104 bw(" iters/s (" as *u8) 105 bn(jrate / vrate) 106 bw("x VM, " as *u8) 107 bn(jrate / trate) 108 bw("x tree)\n" as *u8) 109 bw(" band: native lands in " as *u8) 110 if jrate >= K_MAGIC_500000000 { bw("optimizing-JIT-class (>=500M)" as *u8) } 111 else { if jrate >= K_MAGIC_100000000 { bw("baseline-JIT-class (>=100M)" as *u8) } else { bw("below the JIT bands (unexpected -- investigate)" as *u8) } } 112 bw("\n" as *u8) 113 bw(" KEYSTONE means: runtime x86 EMISSION + EXECUTION works sovereignly (RWX+SysV fn-ptr),\n" as *u8) 114 bw(" and the JIT bands are PHYSICALLY REACHABLE on this box for the same measured kernel.\n" as *u8) 115 bw(" NOT YET a JIT: the Sparkplug-tier build = per-opcode x86 templates off nx_js_vm bytecode.\n" as *u8) 116 if jrate > vrate { bw("=== GREEN: three-tier ladder PROVEN (tree < VM < emitted-native, all correct) ===\n" as *u8); return 0 } 117 bw("=== RED: native not faster than VM (timer/emission problem) ===\n" as *u8) 118 return 1 119}