-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_abi_expected.zig
More file actions
136 lines (119 loc) · 5.37 KB
/
Copy pathgen_abi_expected.zig
File metadata and controls
136 lines (119 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
//
// gen_abi_expected.zig — extract the canonical C-ABI layout numbers from the
// machine-checked Idris2 model (src/interface/abi/Layout.idr) and emit the Zig
// source file (src/interface/ffi/src/abi_layout_expected.zig) consumed by the
// Zig-side cross-check (abi_layout.zig).
//
// Layout.idr is the single source of truth: it carries the proofs (NoOverlap,
// AllFieldsAligned, SizeAligned, SizeCoversFields). This tool lifts the same
// `MkFieldDesc name offset size align` / `MkStructLayout [...] total align`
// constants into Zig so the compiler can assert the canonical extern structs
// agree with the proven model.
//
// Run from the repository root:
// zig run scripts/gen_abi_expected.zig
// (or `python3 -m ziglang run scripts/gen_abi_expected.zig`). CI re-runs this and
// `git diff --exit-code`s the result so the Zig table never drifts from Layout.idr.
const std = @import("std");
const LAYOUT_IDR = "src/interface/abi/Layout.idr";
const OUT_ZIG = "src/interface/ffi/src/abi_layout_expected.zig";
// Idris layout variable (without the "Layout" suffix) -> Zig struct type name.
const Struct = struct { idris: []const u8, zig: []const u8 };
const structs = [_]Struct{
.{ .idris = "serverHandle", .zig = "ServerHandle" },
.{ .idris = "probeResult", .zig = "ProbeResult" },
.{ .idris = "configField", .zig = "ConfigField" },
.{ .idris = "a2mlConfig", .zig = "A2MLConfig" },
.{ .idris = "gameProfile", .zig = "GameProfile" },
.{ .idris = "serverOctad", .zig = "ServerOctad" },
.{ .idris = "fingerprint", .zig = "Fingerprint" },
.{ .idris = "driftReport", .zig = "DriftReport" },
};
const HEADER =
\\// SPDX-License-Identifier: AGPL-3.0-or-later
\\// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
\\//
\\// @generated by scripts/gen_abi_expected.zig from src/interface/abi/Layout.idr
\\// DO NOT EDIT. Regenerate with: zig run scripts/gen_abi_expected.zig
\\//
\\// Canonical C-ABI layout numbers lifted from the machine-checked Idris2 model
\\// (GSA.ABI.Layout). Consumed by abi_layout.zig to assert that the Zig extern
\\// structs agree, byte-for-byte, with the proven Idris layout.
\\
\\pub const Field = struct {
\\ name: []const u8,
\\ offset: u32,
\\ size: u32,
\\ alignment: u32,
\\};
\\
\\pub const StructExpect = struct {
\\ name: []const u8,
\\ total_size: u32,
\\ struct_align: u32,
\\ fields: []const Field,
\\};
\\
\\
;
fn nextUint(it: *std.mem.TokenIterator(u8, .any)) !u32 {
return std.fmt.parseInt(u32, it.next() orelse return error.ParseFailed, 10);
}
fn emitStruct(out: *std.array_list.Managed(u8), src: []const u8, s: Struct) !void {
var nbuf: [128]u8 = undefined;
const needle = try std.fmt.bufPrint(&nbuf, "{s}Layout = MkStructLayout", .{s.idris});
const at = std.mem.indexOf(u8, src, needle) orelse return error.StructNotFound;
const after = src[at + needle.len ..];
const lb = std.mem.indexOfScalar(u8, after, '[') orelse return error.NoFieldList;
const rel_rb = std.mem.indexOfScalar(u8, after[lb..], ']') orelse return error.NoFieldList;
const body = after[lb + 1 .. lb + rel_rb];
const tail = after[lb + rel_rb + 1 ..]; // "<total> <align>" follow the ']'
var tit = std.mem.tokenizeAny(u8, tail, " \r\n\t");
const total = try nextUint(&tit);
const salign = try nextUint(&tit);
const w = out.writer();
try w.print(
"pub const {s} = StructExpect{{\n .name = \"{s}\",\n .total_size = {d},\n .struct_align = {d},\n .fields = &.{{\n",
.{ s.zig, s.zig, total, salign },
);
var rest = body;
while (std.mem.indexOf(u8, rest, "MkFieldDesc")) |fi| {
rest = rest[fi + "MkFieldDesc".len ..];
const q1 = std.mem.indexOfScalar(u8, rest, '"') orelse return error.BadField;
const q2 = std.mem.indexOfScalarPos(u8, rest, q1 + 1, '"') orelse return error.BadField;
const fname = rest[q1 + 1 .. q2];
rest = rest[q2 + 1 ..];
var fit = std.mem.tokenizeAny(u8, rest, " \r\n\t");
const foff = try nextUint(&fit);
const fsize = try nextUint(&fit);
const falign = try nextUint(&fit);
if (!std.mem.startsWith(u8, fname, "padding")) {
try w.print(
" .{{ .name = \"{s}\", .offset = {d}, .size = {d}, .alignment = {d} }},\n",
.{ fname, foff, fsize, falign },
);
}
}
try w.writeAll(" },\n};\n\n");
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
const src = try std.fs.cwd().readFileAlloc(alloc, LAYOUT_IDR, 4 * 1024 * 1024);
defer alloc.free(src);
var out = std.array_list.Managed(u8).init(alloc);
defer out.deinit();
try out.appendSlice(HEADER);
for (structs) |s| try emitStruct(&out, src, s);
try out.appendSlice("pub const all = [_]StructExpect{ ");
for (structs, 0..) |s, i| {
if (i != 0) try out.appendSlice(", ");
try out.appendSlice(s.zig);
}
try out.appendSlice(" };\n");
try std.fs.cwd().writeFile(.{ .sub_path = OUT_ZIG, .data = out.items });
std.debug.print("wrote {s} ({d} structs)\n", .{ OUT_ZIG, structs.len });
}