wcc: str -> 24B {ptr,len,cap}, 3-reg ABI -- parity with []u8 (both stages)
A ww `str` becomes a 24-byte {ptr,len,cap} value, identical in layout to
[]u8 -- the enabling prerequisite for the Phase 2 `str == []u8` collapse.
Both stages, atomically:
- ty_str 16->24B; str value flows 3-reg AX/BX/CX (was 2-reg); str literals
emit cap (=len).
- str in a tagged union grows to a 32B slot, using the AX/DX/CX/R8 4th-word
path already used by 32B slice-variant unions -- str-variant is now
structurally identical.
- tuple (scalar,str) return: 4-reg AX/DX/CX/R8 + 32B receive, extending the
existing type-keyed return (no sret).
- str == []u8 for index and .ptr/.len/.cap, kind-gated where size-based
dispatch collided at 24B; cstage and wwstage mirror exactly.
- table-driven runtime coverage: test/wcc/928_str_abi_run.c.
Cannot be split (rule 10/11): a 24B str and a 16B str cannot coexist across
the two compiler stages without breaking byte-identity, so the size change
and every dependent ABI/codegen site land in one atomic commit, both stages.
Known follow-ups (zero corpus impact, tracked): str-literal global .cap
static-init; >16B struct by-value (pre-existing); tagged-union
match-scrutinee stage divergence (pre-existing).
This commit is contained in:
@@ -85,11 +85,14 @@ static const struct row rows[] = {
|
||||
"export fn main() i32 = { return 0; };\n",
|
||||
"\tMOVZBQ\t(AX), AX\n",
|
||||
"" },
|
||||
/* Write: `obj.arr[i] = v` where arr: [N](i64|str) — 24B tagged.
|
||||
/* Write: `obj.arr[i] = v` where arr: [N](i64|str) — tagged element.
|
||||
* Post-fix wwstage: cgassign N_DOT arm sets elemtn → tagged-store
|
||||
* path → IMULQ $24 + byte-copy from scratch. Scanlocals N_DOT arm
|
||||
* pre-reserves @tagscr in the frame. Pre-fix: scalar `MOVQ AX,
|
||||
* (BX)` over the 24B slot. */
|
||||
* path → IMULQ (stride) + byte-copy from scratch. Scanlocals N_DOT
|
||||
* arm pre-reserves @tagscr in the frame. Pre-fix: scalar `MOVQ AX,
|
||||
* (BX)` over the slot.
|
||||
* #1/Phase 3: str IS []u8 (24B), so the (i64|str) slot is
|
||||
* 8(tag)+24(str payload)=32B — stride is $32, not the 16B-world
|
||||
* $24. Byte-identical across stages. */
|
||||
{ "dotbase_array_tagged_write",
|
||||
"type T = (i64 | str);\n"
|
||||
"type S = struct{ pad: i64, arr: [4]T };\n"
|
||||
@@ -98,7 +101,7 @@ static const struct row rows[] = {
|
||||
" s.arr[i] = 42i64;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = { return 0; };\n",
|
||||
"\tMOVQ\t$24, CX\n",
|
||||
"\tMOVQ\t$32, CX\n",
|
||||
"" },
|
||||
};
|
||||
|
||||
|
||||
@@ -239,7 +239,10 @@ static const struct asm_disp_row asm_disp_rows[] = {
|
||||
"package main;\n"
|
||||
"type holder = struct { s: str };\n"
|
||||
"fn dummy() *holder = { return alloc(holder { s = \"x\" })!; };\n",
|
||||
"\tMOVQ\tAX, (CX)\n" },
|
||||
/* #1/Phase 3: str IS []u8 (24B), so the alloc-str-field store
|
||||
* routes the heap base through DX (CX now holds the cap) and
|
||||
* writes 3 words (ptr/len/cap). Was `(CX)` in the 16B world. */
|
||||
"\tMOVQ\tAX, (DX)\n" },
|
||||
/* str at non-zero foff. Pins that the displacement IS emitted
|
||||
* (`8(CX)`) when foff != 0 — emitdispreg must not suppress
|
||||
* non-zero offsets too. Pre-fix and post-fix both pass this; it
|
||||
@@ -250,7 +253,8 @@ static const struct asm_disp_row asm_disp_rows[] = {
|
||||
"fn dummy() *holder = {\n"
|
||||
" return alloc(holder { pad = 0, s = \"x\" })!;\n"
|
||||
"};\n",
|
||||
"\tMOVQ\tAX, 8(CX)\n" },
|
||||
/* #1/Phase 3: DX base (str IS []u8, cap in CX); was `8(CX)`. */
|
||||
"\tMOVQ\tAX, 8(DX)\n" },
|
||||
/* Generic 8-byte field at foff=0 (non-str, non-float path). Covers
|
||||
* the `else` branch's `MOVQ AX, (BX)` store via fieldstoreop. */
|
||||
{ "alloc_int_at_offset0",
|
||||
|
||||
236
test/wcc/928_str_abi_run.c
Normal file
236
test/wcc/928_str_abi_run.c
Normal file
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* 928_str_abi_run — end-to-end runtime coverage for the str->24B
|
||||
* {ptr,len,cap} 3-reg ABI (Commit #1 / Phase 3, str IS []u8).
|
||||
*
|
||||
* The scratch str-ABI probe matrix (task #3) only diffs asm byte-id;
|
||||
* byte-identity proves the two stages agree, NOT that the emitted code
|
||||
* is correct (a shared miscompile passes byte-id silently). This file
|
||||
* pins the *runtime* contract: build each fixture through both the
|
||||
* cstage `ww` and the wwstage `ww_ww` driver and confirm the program's
|
||||
* own assertions hold (exit 0).
|
||||
*
|
||||
* Covers the ABI dimensions that exercise the new cap word and the
|
||||
* AX/BX/CX value / AX:DX:CX:R8 tagged+tuple register layout: str
|
||||
* literal (cap=len), str arg, str return, str struct field, the
|
||||
* (i64,str) and (str,i64) tuple return shapes, deref-store `*p = s`,
|
||||
* and []str index write+read.
|
||||
*
|
||||
* Deliberately NOT covered here (known, separately-tracked gaps found
|
||||
* during Commit #1 review — both byte-identical across stages, so the
|
||||
* byte-id gates stay green):
|
||||
* - str-containing struct passed BY VALUE: now >16B, falls into the
|
||||
* general ">16B struct byval" limitation (a non-str 24B struct
|
||||
* byval mis-compiles the same way); not a str-specific defect.
|
||||
* (task #10)
|
||||
* - `.cap` VALUE of a top-level str GLOBAL reads 0, not len: the
|
||||
* str-literal global DATAW emits only the 16B {ptr,len} payload,
|
||||
* not the 24B header — byte-identical across stages, but the cap
|
||||
* word is never initialised. Distinct from the .cap field/global
|
||||
* link-error (task #11), which IS fixed: `.cap` on a str field
|
||||
* (stored value) and the global field-read now compile and agree.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* Literal: cap = len for a static literal (no spare storage),
|
||||
* and .cap on a LOCAL str reads back. The new third word. */
|
||||
{ "literal_len_cap",
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: str = \"hello\";\n"
|
||||
" if (s.len: i32 != 5) { return 1; };\n"
|
||||
" if (s.cap: i32 != 5) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Arg: str passed as a 3-word arg (ptr,len,cap), len read in
|
||||
* the callee. Both a let-bound str and a bare literal arg. */
|
||||
{ "arg_len",
|
||||
"fn slen(s: str) i32 = { return s.len: i32; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: str = \"hello\";\n"
|
||||
" if (slen(s) != 5) { return 1; };\n"
|
||||
" if (slen(\"hi\") != 2) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Return: callee returns a str in AX/BX/CX (no AX:DX shuffle —
|
||||
* str returns exactly like a slice now). */
|
||||
{ "return_str",
|
||||
"fn greet() str = { return \"hello world\"; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let g: str = greet();\n"
|
||||
" if (g.len: i32 != 11) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Struct field: store a str into a 3-word field, read .len and
|
||||
* .cap back (field-store routes the base through DX to dodge
|
||||
* CX=cap; the cap word is stored, so b.s.cap == len here). */
|
||||
{ "struct_field_store_load",
|
||||
"type box = struct { s: str, n: i32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let b: box;\n"
|
||||
" b.s = \"abcd\";\n"
|
||||
" b.n = 7i32;\n"
|
||||
" if (b.s.len: i32 != 4) { return 1; };\n"
|
||||
" if (b.n != 7) { return 2; };\n"
|
||||
" if (b.s.cap: i32 != 4) { return 3; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Tuple (i64, str) return: AX=scalar, DX=ptr, CX=len, R8=cap;
|
||||
* 32B receive slot. */
|
||||
{ "tuple_int_str",
|
||||
"fn pair() (i64, str) = { return (42i64, \"hello\"); };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let n, s = pair();\n"
|
||||
" if (n: i32 != 42) { return 1; };\n"
|
||||
" if (s.len: i32 != 5) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Tuple (str, i64) return: reversed order, registers keyed by
|
||||
* element type not position. */
|
||||
{ "tuple_str_int",
|
||||
"fn pair() (str, i64) = { return (\"hi\", 7i64); };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s, n = pair();\n"
|
||||
" if (s.len: i32 != 2) { return 1; };\n"
|
||||
" if (n: i32 != 7) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* Deref-store: `*p = s` writes all three words through the
|
||||
* pointer (cap stashed across the pointer eval). */
|
||||
{ "deref_store",
|
||||
"fn setit(p: *str, v: str) void = { *p = v; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: str = \"hello\";\n"
|
||||
" let d: str;\n"
|
||||
" setit(&d, s);\n"
|
||||
" if (d.len: i32 != 5) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* []str index write + read: the str-element store pushes
|
||||
* cap/len and writes 3 words; the read loads them back. Guards
|
||||
* the str-element gate against the slice=24B collision (#7/754,
|
||||
* write-side). */
|
||||
{ "index_write_read",
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]str;\n"
|
||||
" xs[0] = \"hi\";\n"
|
||||
" xs[1] = \"abc\";\n"
|
||||
" if (xs[0].len: i32 != 2) { return 1; };\n"
|
||||
" if (xs[1].len: i32 != 3) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
0 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/strabi_run_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/strabi_run_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[160];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[512];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[256];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[640];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[640];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr,
|
||||
"str_abi_run: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"str_abi_run[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "str_abi_run: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("str_abi_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user