iter raw-buf.4 rawbuf-payload-termnew-desugar (DONE, drop-call deferred to .5): RawBuf works, prints 60 (refs #7)

Ships RawBuf end-to-end as a consumer of the raw-buf.3 scope-qualified
intrinsic mechanism, plus the general Term::New construction sugar.
Working subset committed; the drop-CALL ratification (no-leak) is
re-carved to raw-buf.5 (see b49f57d) — the flat drop FUNCTION ships here,
its call-insertion needs a codegen resolution mechanism.

What ships (all green):
- raw_buf kernel-tier submodule (crates/ailang-kernel/src/raw_buf/):
  RawBuf TypeDef (param-in {Int,Float,Bool}, ctor B a) + four
  (intrinsic) ops new/get/set/size. parse_raw_buf + workspace injection
  (kernel-tier auto-imported); workspace count 4 -> 5.
- 12 scope-qualified INTERCEPTS entries RawBuf_{new,get,set,size}__{Int,
  Float,Bool} + emit fns: new allocs an @ailang_rc_alloc slab
  (8-byte i64 size header + n*width element bytes), get/set
  getelementptr+load/store at offset 8 + i*width, size loads the header.
  Mechanical on the raw-buf.3 naming + bijection machinery; bijection
  green (4 markers -> 12 entries).
- Term::New desugar (crates/ailang-core/src/desugar.rs): (new T <types>
  <values>) -> (app T.new <values>), runs before check so the
  type-scoped callee flows through the raw-buf.3 scope threading to
  RawBuf_new__T; drops the NewArg::Type (element type inferred from
  use). Both codegen Term::New deferral arms removed (replaced with
  unreachable!). Ratified by new_stubt_builds_and_runs.
- Flat intrinsic-storage drop FUNCTION @drop_raw_buf_RawBuf (single
  @ailang_rc_dec on the slab), emitted for any TypeDef whose new op is
  (intrinsic)-bodied — distinguishes RawBuf (intrinsic new) from StubT
  (real-body new -> generic ADT drop).
- E2E: raw_buf_int (-> 60), raw_buf_float (-> 4.0), raw_buf_bool
  (-> 42), raw_buf_param_in_reject (param-not-in-restricted-set).

In-scope additions beyond the literal plan (both sound, ratified):
- qualify_workspace_term now normalises a monomorphic cross-module
  type-scoped callee (StubT.new) to <home>.f; without it
  new_stubt_builds_and_runs cannot build (StubT.new is monomorphic, so
  the raw-buf.3 poly-mono path mints no symbol). Same-module + polymorphic
  callees carved out.
- diagnostic-behaviour: (new T ..) missing-new-op now surfaces
  type-scoped-member-not-found (desugar runs before check, bypassing
  synth's Term::New arm); new-arg-kind-mismatch obsoleted. Two unit
  tests updated to the new behaviour. param-in reject unaffected.

The 5 .ll snapshots gained @drop_raw_buf_RawBuf (+ partial) — injecting
raw_buf emits its drop fn into every program; benign, regenerated to the
final IR. (The plan's "snapshots stay green" assumption was wrong.)

Verification (orchestrator, this session): cargo test --workspace 676
passed / 0 failed / 2 ignored. raw_buf_int_e2e prints 60; bijection,
round-trip, new_stubt, float/bool/reject all green. The raw_buf_no_leak
test is NOT in this commit — it moves to raw-buf.5 with the drop-call
resolution that makes it pass (the slab currently leaks at scope close;
tracked, fixed next iter; milestone not released until close).
This commit is contained in:
2026-05-30 00:49:51 +02:00
parent b49f57d9c6
commit 8ac8756682
23 changed files with 879 additions and 83 deletions
+293
View File
@@ -185,6 +185,97 @@ pub(crate) static INTERCEPTS: &[Intercept] = &[
wants_alwaysinline: false,
emit: emit_stubt_peek_float,
},
// raw-buf.4: the 12 scope-qualified RawBuf ops. Symbols are
// `RawBuf_{new,get,set,size}__{Int,Float,Bool}` (raw-buf.3
// scope-qualified mono mangling). Slab layout:
// `[ size:i64 @0 ][ elem_0 @8 ][ elem_1 @8+w ]…`; element widths
// Int/Float = 8, Bool = 1. `own`/`borrow (con RawBuf T)` both
// lower to `ptr`. The header is always i64, so `new`/`size` are
// element-type-independent; `get`/`set` carry the element type.
Intercept {
name: "RawBuf_new__Int",
expected_params: &["i64"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_new_int,
},
Intercept {
name: "RawBuf_get__Int",
expected_params: &["ptr", "i64"],
expected_ret: "i64",
wants_alwaysinline: false,
emit: emit_rawbuf_get_int,
},
Intercept {
name: "RawBuf_set__Int",
expected_params: &["ptr", "i64", "i64"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_set_int,
},
Intercept {
name: "RawBuf_size__Int",
expected_params: &["ptr"],
expected_ret: "i64",
wants_alwaysinline: false,
emit: emit_rawbuf_size_int,
},
Intercept {
name: "RawBuf_new__Float",
expected_params: &["i64"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_new_float,
},
Intercept {
name: "RawBuf_get__Float",
expected_params: &["ptr", "i64"],
expected_ret: "double",
wants_alwaysinline: false,
emit: emit_rawbuf_get_float,
},
Intercept {
name: "RawBuf_set__Float",
expected_params: &["ptr", "i64", "double"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_set_float,
},
Intercept {
name: "RawBuf_size__Float",
expected_params: &["ptr"],
expected_ret: "i64",
wants_alwaysinline: false,
emit: emit_rawbuf_size_float,
},
Intercept {
name: "RawBuf_new__Bool",
expected_params: &["i64"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_new_bool,
},
Intercept {
name: "RawBuf_get__Bool",
expected_params: &["ptr", "i64"],
expected_ret: "i1",
wants_alwaysinline: false,
emit: emit_rawbuf_get_bool,
},
Intercept {
name: "RawBuf_set__Bool",
expected_params: &["ptr", "i64", "i1"],
expected_ret: "ptr",
wants_alwaysinline: false,
emit: emit_rawbuf_set_bool,
},
Intercept {
name: "RawBuf_size__Bool",
expected_params: &["ptr"],
expected_ret: "i64",
wants_alwaysinline: false,
emit: emit_rawbuf_size_bool,
},
];
pub(crate) fn lookup(name: &str) -> Option<&'static Intercept> {
@@ -499,6 +590,207 @@ pub(crate) fn emit_stubt_peek_float(emitter: &mut Emitter<'_>) -> Result<()> {
Ok(())
}
// ---------------------------------------------------------------
// raw-buf.4: RawBuf op emits over an `@ailang_rc_alloc` slab.
// Slab layout `[ size:i64 @0 ][ elem_0 @8 ][ elem_1 @8+w ]…`. The
// rc-header is auto-prepended by `@ailang_rc_alloc` (it returns the
// payload ptr); the i64 size header lives at payload offset 0, the
// elements follow at offset 8. Element widths: Int/Float = 8, Bool
// = 1. `new`/`size` are byte-identical across element types (the
// header is always i64); `get`/`set` carry the element load/store
// type and offset width. Intercepts run only under `--alloc=rc`, so
// `@ailang_rc_alloc` is hardcoded (not `alloc.fn_name()`).
// ---------------------------------------------------------------
// --- Int variants (element type i64, width 8) ---
pub(crate) fn emit_rawbuf_new_int(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let cap = emitter.locals[n - 1].1.clone(); // i64 capacity
let elems_bytes = emitter.fresh_ssa();
let total = emitter.fresh_ssa();
let slab = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {elems_bytes} = mul i64 {cap}, 8\n"));
emitter.body.push_str(&format!(" {total} = add i64 {elems_bytes}, 8\n"));
emitter
.body
.push_str(&format!(" {slab} = call ptr @ailang_rc_alloc(i64 {total})\n"));
emitter.body.push_str(&format!(" store i64 {cap}, ptr {slab}\n"));
emitter.body.push_str(&format!(" ret ptr {slab}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_get_int(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 2].1.clone();
let i = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
let v = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 8\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" {v} = load i64, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret i64 {v}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_set_int(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 3].1.clone();
let i = emitter.locals[n - 2].1.clone();
let v = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 8\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" store i64 {v}, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret ptr {b}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_size_int(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 1].1.clone();
let sz = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {sz} = load i64, ptr {b}\n"));
emitter.body.push_str(&format!(" ret i64 {sz}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
// --- Float variants (element type double, width 8) ---
pub(crate) fn emit_rawbuf_new_float(emitter: &mut Emitter<'_>) -> Result<()> {
// Header always i64; element width 8 — byte-identical to Int's new.
emit_rawbuf_new_int(emitter)
}
pub(crate) fn emit_rawbuf_get_float(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 2].1.clone();
let i = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
let v = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 8\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" {v} = load double, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret double {v}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_set_float(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 3].1.clone();
let i = emitter.locals[n - 2].1.clone();
let v = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 8\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" store double {v}, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret ptr {b}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_size_float(emitter: &mut Emitter<'_>) -> Result<()> {
// Header always i64 — byte-identical to Int's size.
emit_rawbuf_size_int(emitter)
}
// --- Bool variants (element type i1, width 1) ---
pub(crate) fn emit_rawbuf_new_bool(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let cap = emitter.locals[n - 1].1.clone(); // i64 capacity
let elems_bytes = emitter.fresh_ssa();
let total = emitter.fresh_ssa();
let slab = emitter.fresh_ssa();
// element width 1 byte for Bool.
emitter.body.push_str(&format!(" {elems_bytes} = mul i64 {cap}, 1\n"));
emitter.body.push_str(&format!(" {total} = add i64 {elems_bytes}, 8\n"));
emitter
.body
.push_str(&format!(" {slab} = call ptr @ailang_rc_alloc(i64 {total})\n"));
emitter.body.push_str(&format!(" store i64 {cap}, ptr {slab}\n"));
emitter.body.push_str(&format!(" ret ptr {slab}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_get_bool(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 2].1.clone();
let i = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
let v = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 1\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" {v} = load i1, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret i1 {v}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_set_bool(emitter: &mut Emitter<'_>) -> Result<()> {
let n = emitter.locals.len();
let b = emitter.locals[n - 3].1.clone();
let i = emitter.locals[n - 2].1.clone();
let v = emitter.locals[n - 1].1.clone();
let off = emitter.fresh_ssa();
let byteoff = emitter.fresh_ssa();
let ptr = emitter.fresh_ssa();
emitter.body.push_str(&format!(" {off} = mul i64 {i}, 1\n"));
emitter.body.push_str(&format!(" {byteoff} = add i64 {off}, 8\n"));
emitter.body.push_str(&format!(
" {ptr} = getelementptr inbounds i8, ptr {b}, i64 {byteoff}\n"
));
emitter.body.push_str(&format!(" store i1 {v}, ptr {ptr}\n"));
emitter.body.push_str(&format!(" ret ptr {b}\n"));
emitter.body.push_str("}\n\n");
emitter.block_terminated = true;
Ok(())
}
pub(crate) fn emit_rawbuf_size_bool(emitter: &mut Emitter<'_>) -> Result<()> {
// Header always i64 — byte-identical to Int's size.
emit_rawbuf_size_int(emitter)
}
#[cfg(test)]
mod tests {
use super::{lookup, INTERCEPTS};
@@ -523,6 +815,7 @@ mod tests {
for module in [
ailang_surface::parse_prelude(),
ailang_surface::parse_kernel_stub(),
ailang_surface::parse_raw_buf(),
] {
for def in &module.defs {
match def {