lib/io: empty() stream; wcc/cgen: #129 sretretsize + #130 global tagged-field store

io.empty (discard+EOF stream, ref/hare/io/empty.ha:4-17) — needed by getopt's
two-pass printusage width measurement. Diverges from Hare's `const empty: *stream`:
a `let _empty_vt` + `fn empty()` that wires the fn-ptr slots per call, because
const-init of a vtable struct with fn-ptr fields is blocked (#118, ruled accept).

Co-discovered while making empty() byte-identical across stages: three
wwstage-only cgen fixes (cstage was already correct; wwstage aligned down):
- #129 sretretsize: consult the same-module pointer-alias before structlookup's
  any-module struct fallback (io.stream = *vtable was mis-sized as memio's 56B
  struct -> spurious sret save).
- #129 callsretsize: swap curmod to the callee's module before sret-size
  classification (cross-module callee context).
- #130 cgassign global-struct tagged-union field store: add the missing arm
  (was a 1-word store) mirroring cstage cgen.c:4893-4912.

The three are inseparable from io.empty here — splitting them out leaves a
divergent-asm intermediate (993/995 red), so they ride one commit per the
one-class gate-repair carve-out (#133-expanded precedent). Regenerates the
embedded combined.ww; 989_lib_byteid pins bufio + fmt graduated to M_ID.
(cgenexpr.ww fix-3 inline comment cites the #129 cluster; narrow to #130 on
next touch to avoid a regen for a comment.)
This commit is contained in:
2026-06-07 06:19:13 +09:00
parent 3ae24c22b8
commit f3750ae3ce
7 changed files with 251 additions and 28 deletions

View File

@@ -6,12 +6,12 @@
// tagged unions instead of errno-style integer sentinels.
//
// This file owns the eof / underread variant tags; lib/io/stream.ww
// owns the `vtable` + `stream` + read/write/close dispatchers, and
// lib/io/types.ww owns the error union, mode/whence enums, and the
// reader/writer/closer fn-type aliases. #94 fold-eFinal collapsed the
// pre-vtable `stream` struct + `closed` tag into the single vtable
// surface; the dispatchers are read/write/close (over `stream`),
// final over `handle` at io fold-2 (#5).
// owns the `vtable` + `stream` + read/write/close dispatchers and the
// [[empty]] singleton, and lib/io/types.ww owns the error union,
// mode/whence enums, and the reader/writer/closer fn-type aliases.
// #94 fold-eFinal collapsed the pre-vtable `stream` struct + `closed`
// tag into the single vtable surface; the dispatchers are read/write/
// close (over `stream`), final over `handle` at io fold-2 (#5).
package io;
// eof — read past the end of the stream. Hare uses the `done`

View File

@@ -1,7 +1,7 @@
// stream — Hare-shaped vtable surface. Project #94 fold-eFinal.
//
// The single io stream surface (the fold-eFinal collapse retired the
// pre-vtable `stream` struct + `closed` tag). Three exports:
// pre-vtable `stream` struct + `closed` tag). Exports:
//
// vtable a struct of optional fn-pointer slots — reader/writer/
// closer per ref/hare/io/stream.ha:36-42. Hare spells the
@@ -21,6 +21,9 @@
// public `read`/`write`/`close`; ww has no `handle` yet
// (io fold-2, #5), so the dispatchers ARE the public
// surface and grow the `handle` match when #5 lands.
// empty the discard+EOF stream (ref/hare/io/empty.ha:13). Lives
// here rather than io.ww so that 900_stdlib can compile
// io.ww standalone (io.ww has no cross-file type refs).
//
// Deferrals (drew-signed): `seeker` lands with io fold-2 (#5) once `off`
// + `whence` plug into the signature. Hare's `?`-propagating `close`
@@ -183,3 +186,37 @@ export fn seek(h: handle, off: off, w: whence) (off | error) = {
export fn tell(h: handle) (off | error) = {
return seek(h, 0, whence.CUR);
};
// ---- empty stream -------------------------------------------------------
//
// ref/hare/io/empty.ha:4-17.
//
// Hare uses `const _empty_vt: vtable = { ... }` + `const empty: *stream`.
// ww can't const-init a vtable struct with fn-ptr fields (#118), so the
// vtable is a module-level `let` and [[empty]] is a function that wires
// the fn-ptr slots on every call and returns the stream pointer.
// Single-assignment on the same words: idempotent under re-entry.
// Lives in stream.ww (not io.ww) so that 900_stdlib can compile io.ww
// standalone without referencing the cross-file vtable/reader/writer types.
fn _empty_read(s: stream, buf: []u8) (size | eof | error) = {
let e: eof;
return e;
};
fn _empty_write(s: stream, buf: []u8) (size | error) = {
return buf.len: size;
};
let _empty_vt: vtable;
// empty — a stream that discards all writes (returning their size) and
// returns EOF on every read. Mirrors ref/hare/io/empty.ha:13.
// #118: const vtable init with fn-ptr fields is unwired (emit_struct_data
// needs a two-pass reloc extension, node_fnptr_sym reusable). Using a
// mutable let + per-call wiring until #118 lands.
export fn empty() stream = {
_empty_vt.reader = (&_empty_read): *reader;
_empty_vt.writer = (&_empty_write): *writer;
return &_empty_vt;
};