wcc+w6c+w6c_ww: delete() builtin — single-element slice removal (part of #35)
Hare's delete(xs[i]) (ref/harec/src/check.c:1981-2027): checker accepts an N_INDEX over a slice-typed base, stamps void; loud-rejects the range form delete(xs[i..j]) (stays filed on #35 — regex fold-2b's consumers are all single-element), non-index operands, array bases, wrong arity. Lowering (both stages, converged byte-identical by construction): ascending word-copy loop shifts [i+1..len) down one esz stride, then hdr.len -= 1; cap unchanged. The move is a same-type whole-stride byte copy — src and dst are elements of the SAME slice, so no boxing exists for any element kind; one loop serves scalar/narrow/str/struct/tagged. esz off the STAMPED base type (#34/#48 discipline). Base shapes: local slice ident (LEAQ) and deref-of-local ptr-to-slice (MOVQ — the fold-2b delete_thread shape); others rule-7 loud-stop. test/804: 38 fixtures — first/middle/last/to-empty, esz 1/4/8/24/56 (MOVB/MOVL tails + 7-qword tagged), cap-unchanged, (*threads)[i], 4 checker reject rows; every accept row cs==ww asm byte-id.
This commit is contained in:
422
test/wcc/804_delete_elem.c
Normal file
422
test/wcc/804_delete_elem.c
Normal file
@@ -0,0 +1,422 @@
|
||||
/*
|
||||
* 804_delete_elem — cstage and wwstage agree, byte-for-byte and at
|
||||
* runtime, that `delete(xs[i])` removes element i from a slice: the
|
||||
* tail [i+1..len) shifts down one stride, len -= 1, cap unchanged
|
||||
* (the delete-half of task #35; insert() stays deferred; regex
|
||||
* fold-2b's delete_thread/failed-thread/PC-dedup consumers).
|
||||
*
|
||||
* Lowering (BOTH stages, converged byte-identical by construction —
|
||||
* there was no pre-existing reference side): push i and &hdr, then an
|
||||
* ascending word-copy loop moves element j+1 onto element j until
|
||||
* j >= len-1, then hdr.len -= 1. The element move is a same-type
|
||||
* whole-stride byte copy — src and dst are elements of the SAME
|
||||
* slice, so no boxing/coercion exists for any element kind; the rows
|
||||
* below prove that across scalar (8B), narrow (4B/1B — the MOVL/MOVB
|
||||
* copy tails), str (24B header), struct (16B) and tagged (56B) esz.
|
||||
* esz comes off the STAMPED base type (the #34/#48 discipline), so a
|
||||
* named-alias element cannot mis-stride.
|
||||
*
|
||||
* Hare's delete also accepts the range form delete(xs[i..j])
|
||||
* (ref/harec/src/check.c:1981-2027 EXPR_SLICE). Out of scope here —
|
||||
* checker loud-rejects it (reject_range row); filed on #35.
|
||||
*
|
||||
* row | shape | want
|
||||
* ----------------+----------------------------------------+------
|
||||
* i64_first | [7,11,13], delete(xs[0]) | 44
|
||||
* i64_middle | [7,11,13], delete(xs[1]) | 40
|
||||
* i64_last | [7,11,13], delete(xs[2]) (no copy, | 38
|
||||
* | loop body never runs) |
|
||||
* i32_narrow | []i32 esz=4 — the MOVL copy tail | 229
|
||||
* u8_narrow | []u8 esz=1 — the MOVB copy tail | 28
|
||||
* cap_unchanged | manual {ptr,len=3,cap=8} header over a | 91
|
||||
* | stack backing; delete middle; cap must |
|
||||
* | still read 8, tail value intact |
|
||||
* delete_to_empty | 1-element slice, delete(xs[0]), len=0 | 7
|
||||
* str_elem | []str esz=24 — 3-qword header moves | 234
|
||||
* | whole |
|
||||
* tagged_56b | [](s6|bool) esz=56 — 7-qword raw move, | 231
|
||||
* | tag+payload survive; match head + |
|
||||
* | raw-byte tail readback (#43 caveat) |
|
||||
* regex_shape | delete((*threads)[i]) behind *[]thread |
|
||||
* | — the fold-2b delete_thread shape | 24
|
||||
* | (deref-of-local base, struct element) |
|
||||
* reject_array | delete(t[0]) on [3]i64 — "delete must | BUILD_FAIL
|
||||
* | operate on a slice" |
|
||||
* reject_nonindex | delete(xs) — operand must be xs[i] | BUILD_FAIL
|
||||
* reject_range | delete(xs[0:2]) — range form deferred | BUILD_FAIL
|
||||
* reject_arity | delete(xs[0], xs[1]) | BUILD_FAIL
|
||||
*
|
||||
* Every non-BUILD_FAIL row also asserts cstage/wwstage asm byte-id,
|
||||
* which subsumes the frame canary (TEXT main,$N) and the del_l/del_e
|
||||
* label-counter symmetry.
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
/* want == BUILD_FAIL: the row must FAIL to build on both stages (the
|
||||
* checker reject set is part of the contract — rule 7, never a silent
|
||||
* acceptance). */
|
||||
#define BUILD_FAIL (-2147483647 - 1)
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "i64_first",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []i64 = [];\n"
|
||||
"\tappend(xs, 7);\n"
|
||||
"\tappend(xs, 11);\n"
|
||||
"\tappend(xs, 13);\n"
|
||||
"\tdelete(xs[0]);\n"
|
||||
"\treturn (xs[0] + xs[1]): i32 + len(xs)*10;\n"
|
||||
"};\n",
|
||||
44 },
|
||||
|
||||
{ "i64_middle",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []i64 = [];\n"
|
||||
"\tappend(xs, 7);\n"
|
||||
"\tappend(xs, 11);\n"
|
||||
"\tappend(xs, 13);\n"
|
||||
"\tdelete(xs[1]);\n"
|
||||
"\treturn (xs[0] + xs[1]): i32 + len(xs)*10;\n"
|
||||
"};\n",
|
||||
40 },
|
||||
|
||||
{ "i64_last",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []i64 = [];\n"
|
||||
"\tappend(xs, 7);\n"
|
||||
"\tappend(xs, 11);\n"
|
||||
"\tappend(xs, 13);\n"
|
||||
"\tdelete(xs[2]);\n"
|
||||
"\treturn (xs[0] + xs[1]): i32 + len(xs)*10;\n"
|
||||
"};\n",
|
||||
38 },
|
||||
|
||||
{ "i32_narrow",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []i32 = [];\n"
|
||||
"\tappend(xs, 4i32);\n"
|
||||
"\tappend(xs, 9i32);\n"
|
||||
"\tappend(xs, 2i32);\n"
|
||||
"\tdelete(xs[0]);\n"
|
||||
"\treturn xs[0] + xs[1]*10 + len(xs)*100;\n"
|
||||
"};\n",
|
||||
229 },
|
||||
|
||||
{ "u8_narrow",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []u8 = [];\n"
|
||||
"\tappend(xs, 5u8);\n"
|
||||
"\tappend(xs, 9u8);\n"
|
||||
"\tappend(xs, 3u8);\n"
|
||||
"\tdelete(xs[1]);\n"
|
||||
"\treturn (xs[0] + xs[1]): i32 + len(xs)*10;\n"
|
||||
"};\n",
|
||||
28 },
|
||||
|
||||
{ "cap_unchanged",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet hb: [8]u8; hb[0] = 4u8; hb[1] = 6u8; hb[2] = 9u8;\n"
|
||||
"\tlet xs: []u8; xs.ptr = &hb[0]; xs.len = 3; xs.cap = 8;\n"
|
||||
"\tdelete(xs[1]);\n"
|
||||
"\treturn (xs.cap*10 + xs.len): i32 + xs[1]: i32;\n"
|
||||
"};\n",
|
||||
91 },
|
||||
|
||||
{ "delete_to_empty",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []u8 = [];\n"
|
||||
"\tappend(xs, 5u8);\n"
|
||||
"\tdelete(xs[0]);\n"
|
||||
"\treturn len(xs) + 7;\n"
|
||||
"};\n",
|
||||
7 },
|
||||
|
||||
{ "str_elem",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []str = [];\n"
|
||||
"\tlet a: str = \"abc\";\n"
|
||||
"\tlet b: str = \"de\";\n"
|
||||
"\tlet g: str = \"fghi\";\n"
|
||||
"\tappend(xs, a);\n"
|
||||
"\tappend(xs, b);\n"
|
||||
"\tappend(xs, g);\n"
|
||||
"\tdelete(xs[1]);\n"
|
||||
"\treturn (xs[0].len*10 + xs[1].len): i32 + len(xs)*100;\n"
|
||||
"};\n",
|
||||
234 },
|
||||
|
||||
/* 56B element: tag qword + 48B struct payload — seven whole-qword
|
||||
* moves; tag AND payload must both survive the raw move (a tagged
|
||||
* element at rest is just bytes; no boxing on a same-slice move).
|
||||
* Readback is split: match proves tag + head (s.a), and the tail
|
||||
* qword (f, element byte 48 -> absolute byte 104 of element 1) is
|
||||
* read RAW via a *u8 over xs.ptr — the indexed-match payload
|
||||
* cursor itself truncates past 32B (pre-existing, task #43), so a
|
||||
* match on s.f would mis-fail regardless of the move. Verified
|
||||
* truncation-free against a no-delete control. */
|
||||
{ "tagged_56b",
|
||||
"package main;\n"
|
||||
"type s6 = struct { a: i64, b: i64, c: i64, d: i64, e: i64, f: i64 };\n"
|
||||
"type cell = (s6 | bool);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []cell = [];\n"
|
||||
"\tlet p0: s6 = s6 { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6 };\n"
|
||||
"\tlet p1: s6 = s6 { a = 7, b = 8, c = 9, d = 10, e = 11, f = 12 };\n"
|
||||
"\tlet p2: s6 = s6 { a = 13, b = 14, c = 15, d = 16, e = 17, f = 18 };\n"
|
||||
"\tappend(xs, p0);\n"
|
||||
"\tappend(xs, p1);\n"
|
||||
"\tappend(xs, p2);\n"
|
||||
"\tdelete(xs[1]);\n"
|
||||
"\tlet r: i32 = 0;\n"
|
||||
"\tmatch (xs[1]) {\n"
|
||||
"\tcase let s: s6 => r = s.a: i32;\n"
|
||||
"\tcase bool => r = 99;\n"
|
||||
"\t};\n"
|
||||
"\tlet bp: *u8 = xs.ptr: *u8;\n"
|
||||
"\tr += bp[104]: i32;\n"
|
||||
"\treturn r + len(xs)*100;\n"
|
||||
"};\n",
|
||||
231 },
|
||||
|
||||
/* The fold-2b consumer shape: regex.ha:547-551 delete_thread takes
|
||||
* threads: *[]thread and deletes through the pointer. ww spells the
|
||||
* autoderef explicitly: delete((*threads)[i]). */
|
||||
{ "regex_shape",
|
||||
"package main;\n"
|
||||
"type thread = struct { pc: i64, matched: bool };\n"
|
||||
"fn delete_thread(i: i64, threads: *[]thread) void = {\n"
|
||||
"\tdelete((*threads)[i]);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []thread = [];\n"
|
||||
"\tappend(xs, thread { pc = 1, matched = false });\n"
|
||||
"\tappend(xs, thread { pc = 2, matched = false });\n"
|
||||
"\tappend(xs, thread { pc = 3, matched = false });\n"
|
||||
"\tdelete_thread(1, &xs);\n"
|
||||
"\tlet r: i32 = (xs[0].pc + xs[1].pc): i32;\n"
|
||||
"\treturn r + len(xs)*10;\n"
|
||||
"};\n",
|
||||
24 },
|
||||
|
||||
{ "reject_array",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet t: [3]i64 = [1, 2, 3];\n"
|
||||
"\tdelete(t[0]);\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
BUILD_FAIL },
|
||||
|
||||
{ "reject_nonindex",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []u8 = [];\n"
|
||||
"\tappend(xs, 5u8);\n"
|
||||
"\tdelete(xs);\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
BUILD_FAIL },
|
||||
|
||||
{ "reject_range",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []u8 = [];\n"
|
||||
"\tappend(xs, 5u8);\n"
|
||||
"\tappend(xs, 6u8);\n"
|
||||
"\tdelete(xs[0:2]);\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
BUILD_FAIL },
|
||||
|
||||
{ "reject_arity",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet xs: []u8 = [];\n"
|
||||
"\tappend(xs, 5u8);\n"
|
||||
"\tappend(xs, 6u8);\n"
|
||||
"\tdelete(xs[0], xs[1]);\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
BUILD_FAIL },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/dele_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/dele_%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 2>/dev/null",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
if (r->want != BUILD_FAIL)
|
||||
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[128];
|
||||
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;
|
||||
}
|
||||
|
||||
/* asm_byte_identical — generate .s via cstage's w6c and wwstage's
|
||||
* w6c_ww and diff. Both delete lowerings are written fresh, so this is
|
||||
* the converged-by-construction gate: any drift in the shift loop, the
|
||||
* del_l/del_e label sequence, or the esz word-copy shows here. */
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[64], cs[64], ws[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/dele_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/dele_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/dele_asm_%d_%d_w.s", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
||||
unlink(src);
|
||||
return -1;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
||||
bin, ws, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
||||
unlink(src); unlink(cs);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *fc = fopen(cs, "rb");
|
||||
FILE *fw = fopen(ws, "rb");
|
||||
int rc = 0;
|
||||
if (!fc || !fw) {
|
||||
rc = -1;
|
||||
} else {
|
||||
for (;;) {
|
||||
int a = fgetc(fc);
|
||||
int b = fgetc(fw);
|
||||
if (a != b) { rc = -1; break; }
|
||||
if (a == EOF) break;
|
||||
}
|
||||
}
|
||||
if (fc) fclose(fc);
|
||||
if (fw) fclose(fw);
|
||||
if (rc != 0)
|
||||
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
||||
r->label);
|
||||
unlink(src); unlink(cs); unlink(ws);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[1024];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[1024];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
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, "delete_elem: 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++;
|
||||
int bad = rows[i].want == BUILD_FAIL
|
||||
? (got != -1) : (got != rows[i].want);
|
||||
if (bad) {
|
||||
fprintf(stderr,
|
||||
"delete_elem[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (rows[i].want == BUILD_FAIL)
|
||||
continue;
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"delete_elem: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("delete_elem: %d fixtures passed\n", total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user