comptime compiler-API: Phase 1 foundation + Phase 2.1 weld plan
Introduce the welded comptime `compiler` library (`#library "compiler"` +
`abi(.zig) extern compiler`), per design/comptime-compiler-api.md, and unify
`callconv(...)` into the new `abi(...)` annotation.
abi(...) replaces callconv(...):
- New ABI enum { default, c, zig, pure }; `abi(.c|.zig|.pure)` parses in the
postfix slot before extern/export (and standalone). `kw_callconv` -> `kw_abi`.
- Migrated 52 sx files, the call-convention-mismatch diagnostic, and docs
(readme/specs) from `callconv(.c)` to `abi(.c)`.
Phase 1 — welded compiler library (parse -> registry -> validation -> bridge):
- `abi(.zig) extern compiler` parses on fn decls (carries abi/extern_lib) and
struct decls (StructDecl.abi/extern_lib).
- `#library "compiler"` is the comptime-only internal surface — never dlopen'd.
- src/ir/compiler_lib.zig: the binding registry (the safety boundary). `Field`
welded to StructInfo.Field with layout baked from the real Zig type
(@offsetOf/@sizeOf); `findType`/`findFn`. Welded structs are layout-validated
at registration (field set + total size) as a header checked against the impl.
- Host-call bridge: a `fn abi(.zig) extern compiler` dispatches under the
comptime interp to its registered Zig handler (intern/text_of round-trip),
never dlsym. IR Function.compiler_welded; validated in declareFunction.
- Comptime-only enforcement: a runtime call to a welded fn is a clean
build-gating error (emitCall), not an undefined-symbol link failure.
Phase 2.1 — byte-layout weld foundation:
- Decision: full byte-layout weld (sx struct laid out byte-identically to the
bound Zig type). Registered StructInfo (first non-natural / Zig-reordered
layout). `computeWeldPlan` — pure offset-ordered element plan + padding +
sx-field->LLVM-element remap; unit-tested. Emit/interp wiring is the next
sub-step (2.2+, see current/CHECKPOINT-COMPILER-API.md).
Examples: 0625/0626 (welded struct + fn round-trip), 1183/1184/1185
(layout-mismatch, unexported-fn, runtime-call diagnostics).
This commit is contained in:
177
src/ir/compiler_lib.test.zig
Normal file
177
src/ir/compiler_lib.test.zig
Normal file
@@ -0,0 +1,177 @@
|
||||
// Tests for the comptime `compiler` library's binding registry.
|
||||
|
||||
const std = @import("std");
|
||||
const compiler_lib = @import("compiler_lib.zig");
|
||||
const types = @import("types.zig");
|
||||
|
||||
// Lock: `findType("Field")` resolves to the welded `StructInfo.Field` type, and
|
||||
// its baked layout EQUALS the real Zig type's `@sizeOf`/`@alignOf`/`@offsetOf`.
|
||||
// This is the foundation the layout sub-step builds on — the welded record's
|
||||
// offsets come from the implementation, so they can't drift.
|
||||
test "compiler_lib: Field welds to StructInfo.Field's real layout" {
|
||||
const FieldZig = types.TypeInfo.StructInfo.Field;
|
||||
|
||||
const bt = compiler_lib.findType("Field") orelse return error.MissingBoundType;
|
||||
|
||||
try std.testing.expectEqualStrings("Field", bt.sx_name);
|
||||
try std.testing.expectEqual(@sizeOf(FieldZig), bt.size);
|
||||
try std.testing.expectEqual(@alignOf(FieldZig), bt.alignment);
|
||||
|
||||
// Two u32 fields, in declaration order.
|
||||
try std.testing.expectEqual(@as(usize, 2), bt.fields.len);
|
||||
|
||||
try std.testing.expectEqualStrings("name", bt.fields[0].name);
|
||||
try std.testing.expectEqual(@offsetOf(FieldZig, "name"), bt.fields[0].offset);
|
||||
try std.testing.expectEqual(@as(usize, 4), bt.fields[0].size);
|
||||
|
||||
try std.testing.expectEqualStrings("ty", bt.fields[1].name);
|
||||
try std.testing.expectEqual(@offsetOf(FieldZig, "ty"), bt.fields[1].offset);
|
||||
try std.testing.expectEqual(@as(usize, 4), bt.fields[1].size);
|
||||
|
||||
// Sanity: the concrete shape the design calls out — two u32s, 8 bytes.
|
||||
try std.testing.expectEqual(@as(usize, 8), bt.size);
|
||||
try std.testing.expectEqual(@as(usize, 0), bt.fields[0].offset);
|
||||
try std.testing.expectEqual(@as(usize, 4), bt.fields[1].offset);
|
||||
}
|
||||
|
||||
// Lock: a name NOT on the export list is unreachable — `findType` returns null
|
||||
// (the safety boundary; the welded-decl path falls through to a clean error,
|
||||
// never a silent default).
|
||||
test "compiler_lib: unexported name returns null" {
|
||||
try std.testing.expect(compiler_lib.findType("NotExported") == null);
|
||||
try std.testing.expect(compiler_lib.findType("") == null);
|
||||
}
|
||||
|
||||
// Lock: a faithful sx header for `Field` validates clean (the natural two-u32
|
||||
// layout matches the welded type).
|
||||
test "compiler_lib: validateStructLayout accepts a faithful Field header" {
|
||||
const bt = compiler_lib.findType("Field").?;
|
||||
const sx = [_]compiler_lib.SxField{
|
||||
.{ .name = "name", .size = 4 },
|
||||
.{ .name = "ty", .size = 4 },
|
||||
};
|
||||
try std.testing.expect(compiler_lib.validateStructLayout(bt, &sx, 8) == null);
|
||||
}
|
||||
|
||||
// Lock: every drift the assertion is meant to catch surfaces as the right
|
||||
// `LayoutMismatch` variant (field count / name / size / total), and the first
|
||||
// mismatch wins.
|
||||
test "compiler_lib: validateStructLayout flags each kind of drift" {
|
||||
const bt = compiler_lib.findType("Field").?;
|
||||
|
||||
// Wrong field count (one field instead of two).
|
||||
{
|
||||
const sx = [_]compiler_lib.SxField{.{ .name = "name", .size = 4 }};
|
||||
const m = compiler_lib.validateStructLayout(bt, &sx, 4).?;
|
||||
try std.testing.expect(m == .field_count);
|
||||
try std.testing.expectEqual(@as(usize, 2), m.field_count.expected);
|
||||
try std.testing.expectEqual(@as(usize, 1), m.field_count.got);
|
||||
}
|
||||
// Wrong field name (reorder / rename) at index 1.
|
||||
{
|
||||
const sx = [_]compiler_lib.SxField{
|
||||
.{ .name = "name", .size = 4 },
|
||||
.{ .name = "kind", .size = 4 },
|
||||
};
|
||||
const m = compiler_lib.validateStructLayout(bt, &sx, 8).?;
|
||||
try std.testing.expect(m == .field_name);
|
||||
try std.testing.expectEqual(@as(usize, 1), m.field_name.index);
|
||||
try std.testing.expectEqualStrings("ty", m.field_name.expected);
|
||||
try std.testing.expectEqualStrings("kind", m.field_name.got);
|
||||
}
|
||||
// Wrong field size (retype to an 8-byte field).
|
||||
{
|
||||
const sx = [_]compiler_lib.SxField{
|
||||
.{ .name = "name", .size = 4 },
|
||||
.{ .name = "ty", .size = 8 },
|
||||
};
|
||||
const m = compiler_lib.validateStructLayout(bt, &sx, 12).?;
|
||||
try std.testing.expect(m == .field_size);
|
||||
try std.testing.expectEqual(@as(usize, 1), m.field_size.index);
|
||||
try std.testing.expectEqual(@as(usize, 4), m.field_size.expected);
|
||||
try std.testing.expectEqual(@as(usize, 8), m.field_size.got);
|
||||
}
|
||||
// Right fields, wrong total (padding drift).
|
||||
{
|
||||
const sx = [_]compiler_lib.SxField{
|
||||
.{ .name = "name", .size = 4 },
|
||||
.{ .name = "ty", .size = 4 },
|
||||
};
|
||||
const m = compiler_lib.validateStructLayout(bt, &sx, 16).?;
|
||||
try std.testing.expect(m == .total_size);
|
||||
try std.testing.expectEqual(@as(usize, 8), m.total_size.expected);
|
||||
try std.testing.expectEqual(@as(usize, 16), m.total_size.got);
|
||||
}
|
||||
}
|
||||
|
||||
// Lock: `Field` (natural two-u32 layout) has the trivial weld plan — two field
|
||||
// elements in declaration order, no padding, identity remap.
|
||||
test "compiler_lib: weld plan for Field is the identity (no reorder, no pad)" {
|
||||
const alloc = std.testing.allocator;
|
||||
const bt = compiler_lib.findType("Field").?;
|
||||
var plan = try compiler_lib.computeWeldPlan(alloc, bt.fields, bt.size);
|
||||
defer plan.deinit(alloc);
|
||||
|
||||
try std.testing.expectEqual(@as(usize, 8), plan.total_size);
|
||||
try std.testing.expectEqual(@as(usize, 2), plan.elements.len);
|
||||
// Identity remap.
|
||||
try std.testing.expectEqual(@as(usize, 0), plan.sx_to_llvm[0]);
|
||||
try std.testing.expectEqual(@as(usize, 1), plan.sx_to_llvm[1]);
|
||||
// Both elements are real fields at 0 and 4.
|
||||
try std.testing.expectEqual(@as(?usize, 0), plan.elements[0].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 0), plan.elements[0].offset);
|
||||
try std.testing.expectEqual(@as(?usize, 1), plan.elements[1].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 4), plan.elements[1].offset);
|
||||
}
|
||||
|
||||
// Lock: `StructInfo` is the first NON-natural layout — Zig reorders it to
|
||||
// (fields@0, name@16, nominal_id@20, is_protocol@24) with a 7-byte alignment
|
||||
// tail. The plan must reproduce exactly that order + the sx→element remap, so the
|
||||
// LLVM type built from it is byte-identical to the Zig type. sx declaration order
|
||||
// is (name, fields, is_protocol, nominal_id) = sx indices 0,1,2,3.
|
||||
test "compiler_lib: weld plan for StructInfo reorders + pads to the Zig layout" {
|
||||
const alloc = std.testing.allocator;
|
||||
const bt = compiler_lib.findType("StructInfo").?;
|
||||
var plan = try compiler_lib.computeWeldPlan(alloc, bt.fields, bt.size);
|
||||
defer plan.deinit(alloc);
|
||||
|
||||
try std.testing.expectEqual(@as(usize, 32), plan.total_size);
|
||||
|
||||
// Elements in ascending offset order: fields, name, nominal_id, is_protocol,
|
||||
// then a trailing 7-byte pad (25 → 32).
|
||||
try std.testing.expectEqual(@as(usize, 5), plan.elements.len);
|
||||
|
||||
// elem 0: fields (sx index 1) @ 0, size 16
|
||||
try std.testing.expectEqual(@as(?usize, 1), plan.elements[0].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 0), plan.elements[0].offset);
|
||||
try std.testing.expectEqual(@as(usize, 16), plan.elements[0].size);
|
||||
// elem 1: name (sx index 0) @ 16, size 4
|
||||
try std.testing.expectEqual(@as(?usize, 0), plan.elements[1].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 16), plan.elements[1].offset);
|
||||
// elem 2: nominal_id (sx index 3) @ 20, size 4
|
||||
try std.testing.expectEqual(@as(?usize, 3), plan.elements[2].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 20), plan.elements[2].offset);
|
||||
// elem 3: is_protocol (sx index 2) @ 24, size 1
|
||||
try std.testing.expectEqual(@as(?usize, 2), plan.elements[3].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 24), plan.elements[3].offset);
|
||||
// elem 4: trailing pad @ 25, size 7
|
||||
try std.testing.expectEqual(@as(?usize, null), plan.elements[4].sx_field);
|
||||
try std.testing.expectEqual(@as(usize, 25), plan.elements[4].offset);
|
||||
try std.testing.expectEqual(@as(usize, 7), plan.elements[4].size);
|
||||
|
||||
// sx → element remap: name→1, fields→0, is_protocol→3, nominal_id→2.
|
||||
try std.testing.expectEqual(@as(usize, 1), plan.sx_to_llvm[0]);
|
||||
try std.testing.expectEqual(@as(usize, 0), plan.sx_to_llvm[1]);
|
||||
try std.testing.expectEqual(@as(usize, 3), plan.sx_to_llvm[2]);
|
||||
try std.testing.expectEqual(@as(usize, 2), plan.sx_to_llvm[3]);
|
||||
}
|
||||
|
||||
// Lock: the welded-function export list resolves the round-trip readers and
|
||||
// rejects unexported names (the boundary the interp's dispatch consults).
|
||||
test "compiler_lib: findFn resolves exported functions, rejects others" {
|
||||
try std.testing.expect(compiler_lib.findFn("intern") != null);
|
||||
try std.testing.expect(compiler_lib.findFn("text_of") != null);
|
||||
try std.testing.expectEqualStrings("intern", compiler_lib.findFn("intern").?.sx_name);
|
||||
try std.testing.expect(compiler_lib.findFn("not_exported") == null);
|
||||
try std.testing.expect(compiler_lib.findFn("") == null);
|
||||
}
|
||||
Reference in New Issue
Block a user