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:
agra
2026-06-17 13:31:11 +03:00
parent 3a9b508502
commit cd5b958d19
100 changed files with 1490 additions and 298 deletions

View File

@@ -53,7 +53,7 @@ objc_msgSend :: (recv: *void, sel: *void) -> *void extern objc;
//
// IMPs (method implementations) are function pointers with the implicit
// Obj-C method shape: `(self: *void, _cmd: *void, ...args) -> ret` with
// `callconv(.c)` so they land args in the standard C registers.
// `abi(.c)` so they land args in the standard C registers.
//
// Method type encoding strings follow Apple's runtime DSL:
// v = void c = char/BOOL i = int l = long f = float d = double
@@ -120,7 +120,7 @@ impl Into(*NSString) for string {
convert :: (self: string) -> *NSString {
cls := objc_getClass("NSString".ptr);
sel := sel_registerName("stringWithUTF8String:".ptr);
msg : (*void, *void, [*]u8) -> *void callconv(.c) = xx objc_msgSend;
msg : (*void, *void, [*]u8) -> *void abi(.c) = xx objc_msgSend;
return xx msg(cls, sel, self.ptr);
}
}

View File

@@ -63,7 +63,7 @@ __sx_block_descriptor : BlockDescriptor = .{
// `$args` is the bound pack of arg types and `$R` is the bound
// return type. The `#insert` evaluates `build_block_convert` at
// comptime and substitutes the resulting source — a nested
// `callconv(.c)` trampoline + the Block literal that points its
// `abi(.c)` trampoline + the Block literal that points its
// `invoke` slot at it. One impl in stdlib replaces every per-
// signature hand-rolled `__block_invoke_*` + `Into(Block)` pair.
impl Into(Block) for Closure(..$args) -> $R {
@@ -77,7 +77,7 @@ impl Into(Block) for Closure(..$args) -> $R {
// (`args`, a comptime `[]Type`) and the closure's return type
// (`$ret`), emits source that:
//
// 1. Declares a nested `__invoke` `callconv(.c)` trampoline whose
// 1. Declares a nested `__invoke` `abi(.c)` trampoline whose
// signature matches the per-shape Apple Block ABI: first arg is
// `block_self: *Block`, then the pack types verbatim. The body
// reconstructs a typed fn-pointer from `block_self.sx_fn`,
@@ -105,7 +105,7 @@ build_block_convert :: (args: []Type, $ret: Type) -> string {
}
code = concat(code, ") -> ");
code = concat(code, ret_name);
code = concat(code, " callconv(.c) { typed_fn : (*void");
code = concat(code, " abi(.c) { typed_fn : (*void");
i = 0;
while i < args.len {
code = concat(code, ", ");

View File

@@ -25,39 +25,39 @@ GL_LINE :u32: 0x1B01;
GL_FILL :u32: 0x1B02;
// Function pointer variables (mutable, loaded at runtime)
glClearColor : (f32, f32, f32, f32) -> void callconv(.c) = ---;
glClear : (u32) -> void callconv(.c) = ---;
glEnable : (u32) -> void callconv(.c) = ---;
glDisable : (u32) -> void callconv(.c) = ---;
glViewport : (i32, i32, i32, i32) -> void callconv(.c) = ---;
glFlush : () -> void callconv(.c) = ---;
glDrawArrays : (u32, i32, i32) -> void callconv(.c) = ---;
glPolygonMode : (u32, u32) -> void callconv(.c) = ---;
glLineWidth : (f32) -> void callconv(.c) = ---;
glCreateShader : (u32) -> u32 callconv(.c) = ---;
glShaderSource : (u32, i32, *[:0]u8, *i32) -> void callconv(.c) = ---;
glCompileShader : (u32) -> void callconv(.c) = ---;
glGetShaderiv : (u32, u32, *i32) -> void callconv(.c) = ---;
glGetShaderInfoLog : (u32, i32, *i32, [*]u8) -> void callconv(.c) = ---;
glCreateProgram : () -> u32 callconv(.c) = ---;
glAttachShader : (u32, u32) -> void callconv(.c) = ---;
glLinkProgram : (u32) -> void callconv(.c) = ---;
glGetProgramiv : (u32, u32, *i32) -> void callconv(.c) = ---;
glGetProgramInfoLog : (u32, i32, *i32, [*]u8) -> void callconv(.c) = ---;
glUseProgram : (u32) -> void callconv(.c) = ---;
glDeleteShader : (u32) -> void callconv(.c) = ---;
glGenVertexArrays : (i32, *u32) -> void callconv(.c) = ---;
glGenBuffers : (i32, *u32) -> void callconv(.c) = ---;
glBindVertexArray : (u32) -> void callconv(.c) = ---;
glBindBuffer : (u32, u32) -> void callconv(.c) = ---;
glBufferData : (u32, isize, *void, u32) -> void callconv(.c) = ---;
glVertexAttribPointer : (u32, i32, u32, u8, i32, *void) -> void callconv(.c) = ---;
glEnableVertexAttribArray : (u32) -> void callconv(.c) = ---;
glGetUniformLocation : (u32, [*]u8) -> i32 callconv(.c) = ---;
glUniformMatrix4fv : (i32, i32, u8, [*]f32) -> void callconv(.c) = ---;
glUniform3f : (i32, f32, f32, f32) -> void callconv(.c) = ---;
glDepthFunc : (u32) -> void callconv(.c) = ---;
glUniform1f : (i32, f32) -> void callconv(.c) = ---;
glClearColor : (f32, f32, f32, f32) -> void abi(.c) = ---;
glClear : (u32) -> void abi(.c) = ---;
glEnable : (u32) -> void abi(.c) = ---;
glDisable : (u32) -> void abi(.c) = ---;
glViewport : (i32, i32, i32, i32) -> void abi(.c) = ---;
glFlush : () -> void abi(.c) = ---;
glDrawArrays : (u32, i32, i32) -> void abi(.c) = ---;
glPolygonMode : (u32, u32) -> void abi(.c) = ---;
glLineWidth : (f32) -> void abi(.c) = ---;
glCreateShader : (u32) -> u32 abi(.c) = ---;
glShaderSource : (u32, i32, *[:0]u8, *i32) -> void abi(.c) = ---;
glCompileShader : (u32) -> void abi(.c) = ---;
glGetShaderiv : (u32, u32, *i32) -> void abi(.c) = ---;
glGetShaderInfoLog : (u32, i32, *i32, [*]u8) -> void abi(.c) = ---;
glCreateProgram : () -> u32 abi(.c) = ---;
glAttachShader : (u32, u32) -> void abi(.c) = ---;
glLinkProgram : (u32) -> void abi(.c) = ---;
glGetProgramiv : (u32, u32, *i32) -> void abi(.c) = ---;
glGetProgramInfoLog : (u32, i32, *i32, [*]u8) -> void abi(.c) = ---;
glUseProgram : (u32) -> void abi(.c) = ---;
glDeleteShader : (u32) -> void abi(.c) = ---;
glGenVertexArrays : (i32, *u32) -> void abi(.c) = ---;
glGenBuffers : (i32, *u32) -> void abi(.c) = ---;
glBindVertexArray : (u32) -> void abi(.c) = ---;
glBindBuffer : (u32, u32) -> void abi(.c) = ---;
glBufferData : (u32, isize, *void, u32) -> void abi(.c) = ---;
glVertexAttribPointer : (u32, i32, u32, u8, i32, *void) -> void abi(.c) = ---;
glEnableVertexAttribArray : (u32) -> void abi(.c) = ---;
glGetUniformLocation : (u32, [*]u8) -> i32 abi(.c) = ---;
glUniformMatrix4fv : (i32, i32, u8, [*]f32) -> void abi(.c) = ---;
glUniform3f : (i32, f32, f32, f32) -> void abi(.c) = ---;
glDepthFunc : (u32) -> void abi(.c) = ---;
glUniform1f : (i32, f32) -> void abi(.c) = ---;
GL_LESS :u32: 0x0201;
GL_LEQUAL :u32: 0x0203;
GL_SCISSOR_TEST :u32: 0x0C11;
@@ -76,27 +76,27 @@ GL_RED :u32: 0x1903;
GL_R8 :u32: 0x8229;
GL_UNPACK_ALIGNMENT :u32: 0x0CF5;
glScissor : (i32, i32, i32, i32) -> void callconv(.c) = ---;
glBufferSubData : (u32, isize, isize, *void) -> void callconv(.c) = ---;
glGenTextures : (i32, *u32) -> void callconv(.c) = ---;
glBindTexture : (u32, u32) -> void callconv(.c) = ---;
glTexImage2D : (u32, i32, i32, i32, i32, i32, u32, u32, *void) -> void callconv(.c) = ---;
glTexParameteri : (u32, u32, i32) -> void callconv(.c) = ---;
glBlendFunc : (u32, u32) -> void callconv(.c) = ---;
glReadPixels : (i32, i32, i32, i32, u32, u32, *void) -> void callconv(.c) = ---;
glActiveTexture : (u32) -> void callconv(.c) = ---;
glUniform1i : (i32, i32) -> void callconv(.c) = ---;
glPixelStorei : (u32, i32) -> void callconv(.c) = ---;
glTexSubImage2D : (u32, i32, i32, i32, i32, i32, u32, u32, *void) -> void callconv(.c) = ---;
glDeleteTextures : (i32, *u32) -> void callconv(.c) = ---;
glScissor : (i32, i32, i32, i32) -> void abi(.c) = ---;
glBufferSubData : (u32, isize, isize, *void) -> void abi(.c) = ---;
glGenTextures : (i32, *u32) -> void abi(.c) = ---;
glBindTexture : (u32, u32) -> void abi(.c) = ---;
glTexImage2D : (u32, i32, i32, i32, i32, i32, u32, u32, *void) -> void abi(.c) = ---;
glTexParameteri : (u32, u32, i32) -> void abi(.c) = ---;
glBlendFunc : (u32, u32) -> void abi(.c) = ---;
glReadPixels : (i32, i32, i32, i32, u32, u32, *void) -> void abi(.c) = ---;
glActiveTexture : (u32) -> void abi(.c) = ---;
glUniform1i : (i32, i32) -> void abi(.c) = ---;
glPixelStorei : (u32, i32) -> void abi(.c) = ---;
glTexSubImage2D : (u32, i32, i32, i32, i32, i32, u32, u32, *void) -> void abi(.c) = ---;
glDeleteTextures : (i32, *u32) -> void abi(.c) = ---;
glGenFramebuffers : (i32, *u32) -> void callconv(.c) = ---;
glGenRenderbuffers : (i32, *u32) -> void callconv(.c) = ---;
glBindFramebuffer : (u32, u32) -> void callconv(.c) = ---;
glBindRenderbuffer : (u32, u32) -> void callconv(.c) = ---;
glFramebufferRenderbuffer : (u32, u32, u32, u32) -> void callconv(.c) = ---;
glGetRenderbufferParameteriv : (u32, u32, *i32) -> void callconv(.c) = ---;
glCheckFramebufferStatus : (u32) -> u32 callconv(.c) = ---;
glGenFramebuffers : (i32, *u32) -> void abi(.c) = ---;
glGenRenderbuffers : (i32, *u32) -> void abi(.c) = ---;
glBindFramebuffer : (u32, u32) -> void abi(.c) = ---;
glBindRenderbuffer : (u32, u32) -> void abi(.c) = ---;
glFramebufferRenderbuffer : (u32, u32, u32, u32) -> void abi(.c) = ---;
glGetRenderbufferParameteriv : (u32, u32, *i32) -> void abi(.c) = ---;
glCheckFramebufferStatus : (u32) -> u32 abi(.c) = ---;
GL_TEXTURE_WRAP_S :u32: 0x2802;
GL_TEXTURE_WRAP_T :u32: 0x2803;
@@ -104,7 +104,7 @@ GL_CLAMP_TO_EDGE :u32: 0x812F;
// Loader: call once after creating GL context
// Pass in a proc loader (e.g. SDL_GL_GetProcAddress)
load_gl :: (get_proc: ([*]u8) -> *void callconv(.c)) {
load_gl :: (get_proc: ([*]u8) -> *void abi(.c)) {
glClearColor = xx get_proc("glClearColor");
glClear = xx get_proc("glClear");
glEnable = xx get_proc("glEnable");