_offc_xfile_struct.nx source
↩ module page · 60 lines · 1876 B
1import "syscalls.nx"
2import "xfile_struct_types.nx"
3import "xfile_struct_writer.nx"
4
5// Minimal repro for T#selfhost-006.
6//
7// xfile_struct_types.nx defines `struct G { id, a, b, c, d, e, f, g }`
8// at offsets 0,8,16,24,32,40,48,56 (8 i64 fields, total 64 bytes).
9// We allocate an array of N entries with stride 80 (matching nxc.nx's
10// Global stride; trailing 16 bytes is padding).
11//
12// xfile_struct_writer.nx writes id=i, a=100+i, b=200+i for entries 0..2.
13// Here in main we read the same slots back via `g.id`, `g.a`, `g.b`.
14//
15// If C anchor's struct field-offset assignment is stable across imports,
16// expected stdout:
17// 0 100 200
18// 1 101 201
19// 2 102 202
20// If T#selfhost-006 root cause is per-file Type divergence, the read
21// values will diverge from the written ones and this minimal program
22// makes the bug 20 lines instead of 6KB.
23
24func print_i64(n: i64) -> i64 {
25 let buf: *u8 = sys_mmap(32)
26 var k: i64 = 0
27 var v: i64 = n
28 if v < 0 { sys_write(1, "-" as *u8, 1); v = 0 - v }
29 if v == 0 { buf[k] = 0x30; k = 1 }
30 while v > 0 {
31 buf[k] = 0x30 + (v - (v / 10) * 10)
32 v = v / 10
33 k = k + 1
34 }
35 var j: i64 = k - 1
36 while j >= 0 {
37 sys_write(1, (((buf as i64) + j) as *u8), 1)
38 j = j - 1
39 }
40 return 0
41}
42
43func main() -> i64 {
44 let region: *u8 = sys_mmap(80 * 4)
45 let base: i64 = region as i64
46
47 // Writer fills slots 0,1,2.
48 xfile_writer_fill(base, 3)
49
50 // Reader: walk slots 0..3 and print id, name_len, len.
51 var i: i64 = 0
52 while i < 3 {
53 let g: *G = (base + i * 80) as *G
54 print_i64(g.id); sys_write(1, " " as *u8, 1)
55 print_i64(g.name_len); sys_write(1, " " as *u8, 1)
56 print_i64(g.len); sys_write(1, "\n" as *u8, 1)
57 i = i + 1
58 }
59 return 0
60}