code wiki / _hdl_build / nx_pattern_emit10.nx

nx_pattern_emit10.nx source

↩ module page · 136 lines · 8405 B

1// nx_pattern_emit10.nx -- PATTERN EMITTER: WIRE_MARKER (shape 15 -- grown FROM a measured race 2// loss: race 4a runs the team's closest covered pattern, WIRE_TLV, against a JPEG marker stream 3// and loses; THIS emitter closes exactly the three spec gaps that loss names): 4// prefix every segment starts with a prefix byte (JPEG 0xFF) -- TLV has none 5// standalone a TABLE of marker values with NO length field (SOI/EOI/RSTn) -- TLV has none 6// terminator a marker after which entropy/wire data follows (SOS): count/find STOP there 7// inclusive the 2-byte BE length INCLUDES itself (JPEG) -- TLV lengths exclude the header 8// Authored fns: <name>_count(b,n) -> segments through the terminator, -1 = bad prefix/short/ 9// len<2 REFUSED; <name>_find(b,n,m) -> content offset (standalone: after marker; lengthed: 10// after length), -1 absent (or behind the terminator), -2 malformed. All spec values BAKED at 11// emit time (rule 11). Test KATs computed by the emitter from a synthetic marker image it 12// constructs to the same spec (count, two finds, absent, truncation + garbage refusals). 13// REFUSAL RAILS: prefix outside 1..255, terminator listed standalone, standalone table empty 14// or >12 -> spec REFUSED. Extends ..emit7(WIRE_TLV)/emit8/emit9. Covers B5's JPEG segment walk 15// (SOF2 progressive incl.) + any prefix-marker wire format. 16// LAWS: struct-free, integer-only, flat ifs, no &&/||, <=6 args per func. license_tier: ORIGINAL 17import "nx_syscalls.nx" 18 19func pa_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd, s, n); return 0 } 20func pa_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 21 22// spec rail: sa[0]=count, values at sa[1..]; terminator must NOT be standalone 23func pa_spec_ok(prefix: i64, sa: *i64, term: i64) -> i64 { 24 if prefix < 1 { return 0 } 25 if prefix > 255 { return 0 } 26 if term < 0 { return 0 } 27 if term > 255 { return 0 } 28 let n: i64 = sa[0] 29 if n < 1 { return 0 } 30 if n > 12 { return 0 } 31 var i: i64 = 0 32 while i < n { 33 if sa[1 + i] < 0 { return 0 } 34 if sa[1 + i] > 255 { return 0 } 35 if sa[1 + i] == term { return 0 } 36 i = i + 1 37 } 38 return 1 39} 40 41// emit the standalone-membership flat ifs ("if m == V { sa = 1 }") 42func pa_emit_sa_ifs(fd: i64, sa: *i64) -> i64 { 43 var i: i64 = 0 44 while i < sa[0] { 45 pa_w(fd, " if m == " as *u8); pa_wn(fd, sa[1 + i]); pa_w(fd, " { sa = 1 }\n" as *u8) 46 i = i + 1 47 } 48 return 0 49} 50 51// author the WIRE_MARKER core 52func pe10_emit_wire_marker(fd: i64, name: *u8, prefix: i64, sa: *i64, term: i64) -> i64 { 53 pa_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_MARKER) -- prefix-marker wire walk, no Claude logic\n" as *u8) 54 pa_w(fd, "import \"nx_syscalls.nx\"\n" as *u8) 55 56 pa_w(fd, "func " as *u8); pa_w(fd, name); pa_w(fd, "_count(b: *u8, n: i64) -> i64 {\n" as *u8) 57 pa_w(fd, " var off: i64 = 0\n var c: i64 = 0\n while off < n {\n" as *u8) 58 pa_w(fd, " if off + 2 > n { return 0 - 1 }\n" as *u8) 59 pa_w(fd, " if (b[off] & 0xff) != " as *u8); pa_wn(fd, prefix); pa_w(fd, " { return 0 - 1 }\n" as *u8) 60 pa_w(fd, " let m: i64 = b[off + 1] & 0xff\n var sa: i64 = 0\n" as *u8) 61 pa_emit_sa_ifs(fd, sa) 62 pa_w(fd, " if sa == 1 { off = off + 2; c = c + 1 }\n" as *u8) 63 pa_w(fd, " if sa == 0 {\n" as *u8) 64 pa_w(fd, " if off + 4 > n { return 0 - 1 }\n" as *u8) 65 pa_w(fd, " let l: i64 = ((b[off + 2] & 0xff) << 8) | (b[off + 3] & 0xff)\n" as *u8) 66 pa_w(fd, " if l < 2 { return 0 - 1 }\n" as *u8) 67 pa_w(fd, " if off + 2 + l > n { return 0 - 1 }\n" as *u8) 68 pa_w(fd, " c = c + 1\n" as *u8) 69 pa_w(fd, " if m == " as *u8); pa_wn(fd, term); pa_w(fd, " { return c }\n" as *u8) 70 pa_w(fd, " off = off + 2 + l\n" as *u8) 71 pa_w(fd, " }\n }\n return c\n}\n" as *u8) 72 73 pa_w(fd, "func " as *u8); pa_w(fd, name); pa_w(fd, "_find(b: *u8, n: i64, t: i64) -> i64 {\n" as *u8) 74 pa_w(fd, " var off: i64 = 0\n while off < n {\n" as *u8) 75 pa_w(fd, " if off + 2 > n { return 0 - 2 }\n" as *u8) 76 pa_w(fd, " if (b[off] & 0xff) != " as *u8); pa_wn(fd, prefix); pa_w(fd, " { return 0 - 2 }\n" as *u8) 77 pa_w(fd, " let m: i64 = b[off + 1] & 0xff\n var sa: i64 = 0\n" as *u8) 78 pa_emit_sa_ifs(fd, sa) 79 pa_w(fd, " if sa == 1 {\n" as *u8) 80 pa_w(fd, " if m == t { return off + 2 }\n" as *u8) 81 pa_w(fd, " off = off + 2\n }\n" as *u8) 82 pa_w(fd, " if sa == 0 {\n" as *u8) 83 pa_w(fd, " if off + 4 > n { return 0 - 2 }\n" as *u8) 84 pa_w(fd, " let l: i64 = ((b[off + 2] & 0xff) << 8) | (b[off + 3] & 0xff)\n" as *u8) 85 pa_w(fd, " if l < 2 { return 0 - 2 }\n" as *u8) 86 pa_w(fd, " if off + 2 + l > n { return 0 - 2 }\n" as *u8) 87 pa_w(fd, " if m == t { return off + 4 }\n" as *u8) 88 pa_w(fd, " if m == " as *u8); pa_wn(fd, term); pa_w(fd, " { return 0 - 1 }\n" as *u8) 89 pa_w(fd, " off = off + 2 + l\n }\n }\n return 0 - 1\n}\n" as *u8) 90 return 1 91} 92 93// author the test: the emitter constructs a synthetic marker image to its OWN spec and bakes 94// the computed KATs (first standalone, lengthed 0xE0 len6, lengthed 0xC4 len4, terminator len4) 95func pe10_emit_wire_marker_test(fd: i64, name: *u8, prefix: i64, sa: *i64, term: i64) -> i64 { 96 let img: *u8 = sys_mmap(64) 97 let s0: i64 = sa[1] 98 img[0] = prefix as u8; img[1] = s0 as u8 99 img[2] = prefix as u8; img[3] = 0xE0 as u8; img[4] = 0 as u8; img[5] = 6 as u8 100 img[6] = 0x11 as u8; img[7] = 0x22 as u8; img[8] = 0x33 as u8; img[9] = 0x44 as u8 101 img[10] = prefix as u8; img[11] = 0xC4 as u8; img[12] = 0 as u8; img[13] = 4 as u8 102 img[14] = 0x55 as u8; img[15] = 0x66 as u8 103 img[16] = prefix as u8; img[17] = term as u8; img[18] = 0 as u8; img[19] = 4 as u8 104 img[20] = 0x77 as u8; img[21] = 0x88 as u8 105 let total: i64 = 22 106 let f_e0: i64 = 6 107 let f_c4: i64 = 14 108 let cnt: i64 = 4 109 pa_w(fd, "// AUTHORED BY THE NISHI BUILDER (pattern: WIRE_MARKER test) -- KATs computed from the spec at emit time\n" as *u8) 110 pa_w(fd, "import \"" as *u8); pa_w(fd, name); pa_w(fd, ".nx\"\nimport \"nx_syscalls.nx\"\n" as *u8) 111 pa_w(fd, "func main() -> i64 {\n let b: *u8 = sys_mmap(64)\n" as *u8) 112 var i: i64 = 0 113 while i < total { 114 pa_w(fd, " b[" as *u8); pa_wn(fd, i); pa_w(fd, "] = " as *u8); pa_wn(fd, img[i] & 0xff); pa_w(fd, " as u8\n" as *u8) 115 i = i + 1 116 } 117 pa_w(fd, " let g: *u8 = sys_mmap(16)\n" as *u8) 118 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_count(b, " as *u8); pa_wn(fd, total); pa_w(fd, ") == " as *u8); pa_wn(fd, cnt); pa_w(fd, " {\n" as *u8) 119 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_find(b, " as *u8); pa_wn(fd, total); pa_w(fd, ", 224) == " as *u8); pa_wn(fd, f_e0); pa_w(fd, " {\n" as *u8) 120 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_find(b, " as *u8); pa_wn(fd, total); pa_w(fd, ", 196) == " as *u8); pa_wn(fd, f_c4); pa_w(fd, " {\n" as *u8) 121 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_find(b, " as *u8); pa_wn(fd, total); pa_w(fd, ", 153) == 0 - 1 {\n" as *u8) 122 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_count(b, " as *u8); pa_wn(fd, total - 1); pa_w(fd, ") == 0 - 1 {\n" as *u8) 123 pa_w(fd, " if " as *u8); pa_w(fd, name); pa_w(fd, "_count(g, 4) == 0 - 1 { sys_exit(0) } } } } } }\n" as *u8) 124 pa_w(fd, " sys_exit(1)\n return 1\n}\n" as *u8) 125 return 1 126} 127 128// author module+test; REFUSES a bad spec 129func pe10_author_wire_marker(name: *u8, modpath: *u8, testpath: *u8, prefix: i64, sa: *i64, term: i64) -> i64 { 130 if pa_spec_ok(prefix, sa, term) != 1 { return 0 } 131 let mf: i64 = sys_openat_wr(modpath, 0x1a4); if mf < 0 { return 0 } 132 pe10_emit_wire_marker(mf, name, prefix, sa, term); sys_close(mf) 133 let tf: i64 = sys_openat_wr(testpath, 0x1a4); if tf < 0 { return 0 } 134 pe10_emit_wire_marker_test(tf, name, prefix, sa, term); sys_close(tf) 135 return 1 136}