Probe-first find for the path c2 appendlit (buf.buf[lo..hi]=bs): a slice-copy-assign into a struct-field array sub-range emitted ZERO code — silent NO-OP, both stages, both-wrong-identical (#263), so runtime is the only net. N_ASSIGN gains an N_SLICE-LHS arm (cgen.c + cgenexpr.ww slicebaseesz twin) reusing the N_SLICE-read base/esz cascade and copying (hi-lo)*esz bytes from rhs.ptr via a runtime loop (len is runtime; no REP/MOVSB). esz routed through the type table (rule 13; [N]u8->1). Hare len(bs)==hi-lo assert deferred to #149.
263 lines
8.4 KiB
C
263 lines
8.4 KiB
C
/*
|
|
* 952_slicecopy_assign_run — runtime + cs==ww byte-id net for #145
|
|
* (c1.5a): bulk slice-copy-assign into a range place `s.arr[lo:hi] = bs`
|
|
* (the LHS is an N_SLICE). No legacy N_ASSIGN arm caught an N_SLICE LHS,
|
|
* so the statement fell through to the scalar tail and emitted NOTHING —
|
|
* a silent no-op, BOTH stages, byte-id-green (#263-class). It blocked
|
|
* the path c2 port's appendlit (ref/hare/path/stack.ha:72
|
|
* `buf.buf[newend..newend+len(bs)] = bs`).
|
|
*
|
|
* The fix reuses the N_SLICE READ lowering (AX = base+lo*esz, BX = hi-lo,
|
|
* CX = cap), scales BX by the SAME read-path element width (esz; [N]u8 ->
|
|
* 1, [N]i32 -> 4), and emits a runtime-counted byte-granular copy loop
|
|
* from bs.ptr (the length is RUNTIME — w6a has no REP/MOVSB and the
|
|
* #265/#268 memcpy emitters are compile-time-sz unrolled). The Hare
|
|
* len(bs)==hi-lo assert is deferred to task #149 (documented, not a c2
|
|
* blocker — appendlit's lengths are equal by construction).
|
|
*
|
|
* WRITE-twin rows assert: the copy lands the right bytes in [lo,hi) AND
|
|
* leaves bytes OUTSIDE the range untouched (every row sums members both
|
|
* inside and outside the range, so a mis-strided or over-wide copy
|
|
* fails). esz coverage: [N]u8 (esz=1) and [N]i32 (esz=4). Base coverage:
|
|
* a struct-field array (N_DOT, the appendlit shape), a struct field via a
|
|
* *struct param (via_ptr), and a bare-local array (N_IDENT). READ-twin
|
|
* rows (drew's #5, already clean) lock the reslice-consume of the field
|
|
* array. byteid=1 throughout: post-fix the emission is byte-identical
|
|
* (the loop labels share the mklabel prefix/count/order across stages).
|
|
*/
|
|
#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_exit; int byteid; };
|
|
|
|
static const struct row rows[] = {
|
|
/* WRITE twin — struct-field [N]u8 base (the appendlit shape). */
|
|
{ "w_u8_lo0",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" let bs: [3]u8; bs[0]=10u8; bs[1]=20u8; bs[2]=30u8;\n"
|
|
" s.buf[0:3] = bs[0:3];\n"
|
|
" return (s.buf[0]+s.buf[1]+s.buf[2]+s.buf[3]\n"
|
|
" +s.buf[4]+s.buf[5]): i32;\n"
|
|
"};\n", 60, 1 },
|
|
{ "w_u8_lo2",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" let bs: [3]u8; bs[0]=10u8; bs[1]=20u8; bs[2]=30u8;\n"
|
|
" s.buf[2:5] = bs[0:3];\n"
|
|
" return (s.buf[0]+s.buf[1]+s.buf[2]+s.buf[3]\n"
|
|
" +s.buf[4]+s.buf[5]): i32;\n"
|
|
"};\n", 60, 1 },
|
|
{ "w_u8_single",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" let bs: [1]u8; bs[0]=42u8;\n"
|
|
" s.buf[3:4] = bs[0:1];\n"
|
|
" return (s.buf[2]+s.buf[3]+s.buf[4]): i32;\n"
|
|
"};\n", 42, 1 },
|
|
/* empty range hi==lo — count=0, the loop's CMPQ $0 guard must
|
|
* JLE-exit before the first MOVB (off-by-one underflow would
|
|
* clobber buf[lo] and run away). Nothing in buf may change. */
|
|
{ "w_empty",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer; s.buf[2]=5u8; s.buf[3]=7u8;\n"
|
|
" let bs: [3]u8; bs[0]=10u8; bs[1]=20u8; bs[2]=30u8;\n"
|
|
" s.buf[2:2] = bs[0:0];\n"
|
|
" return (s.buf[1]+s.buf[2]+s.buf[3]+s.buf[4]): i32;\n"
|
|
"};\n", 12, 1 },
|
|
/* esz=4 — [N]i32 field strides by the element width, not 1. */
|
|
{ "w_i32",
|
|
"package main;\n"
|
|
"type box = struct { o: [8]i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: box;\n"
|
|
" let bs: [2]i32; bs[0]=40; bs[1]=88;\n"
|
|
" s.o[1:3] = bs[0:2];\n"
|
|
" return (s.o[0]+s.o[1]+s.o[2]+s.o[3]): i32;\n"
|
|
"};\n", 128, 1 },
|
|
/* via_ptr — the field write through a *struct param (appendlit's
|
|
* `buf: *buffer` shape). */
|
|
{ "w_viaptr",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"fn wr(s: *buffer, src: []u8) void = { s.buf[1:4] = src; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" let bs: [3]u8; bs[0]=11u8; bs[1]=22u8; bs[2]=33u8;\n"
|
|
" wr(&s, bs[0:3]);\n"
|
|
" return (s.buf[0]+s.buf[1]+s.buf[2]+s.buf[3]\n"
|
|
" +s.buf[4]): i32;\n"
|
|
"};\n", 66, 1 },
|
|
/* N_IDENT base — a bare-local array place (slicebaseesz IDENT leg). */
|
|
{ "w_local",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let a: [8]u8;\n"
|
|
" let bs: [3]u8; bs[0]=10u8; bs[1]=20u8; bs[2]=30u8;\n"
|
|
" a[2:5] = bs[0:3];\n"
|
|
" return (a[0]+a[1]+a[2]+a[3]+a[4]+a[5]): i32;\n"
|
|
"};\n", 60, 1 },
|
|
/* READ twin (drew's #5, already CLEAN) — reslice the field array with
|
|
* a runtime hi bound, consume as []u8. Locks the read companion. */
|
|
{ "rd_reslice",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" let bs: [3]u8; bs[0]=10u8; bs[1]=20u8; bs[2]=30u8;\n"
|
|
" s.buf[0:3] = bs[0:3];\n"
|
|
" s.end = 3;\n"
|
|
" let v: []u8 = s.buf[0:s.end];\n"
|
|
" return (v[0]: i32 + v[1]: i32 + v[2]: i32 + (v.len: i32)*100);\n"
|
|
"};\n", 104, 1 },
|
|
{ "rd_len",
|
|
"package main;\n"
|
|
"type buffer = struct { buf: [8]u8, end: i32 };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let s: buffer;\n"
|
|
" s.end = 5;\n"
|
|
" let v: []u8 = s.buf[0:s.end];\n"
|
|
" return v.len: i32;\n"
|
|
"};\n", 5, 1 },
|
|
{ NULL, NULL, 0, 0 }
|
|
};
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
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 w6c[1100], w6c_ww[1100];
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
if (access(w6c_ww, X_OK) != 0) {
|
|
fprintf(stderr, "slicecopy: w6c_ww missing — cannot run the "
|
|
"cs==ww byte-id gate (the whole point of this test)\n");
|
|
return 1;
|
|
}
|
|
|
|
int n = 0, fail = 0;
|
|
for (int i = 0; rows[i].src; i++, n++) {
|
|
char src[64];
|
|
snprintf(src, sizeof src, "/tmp/wwsca_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwsca_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
|
tmpdir, bin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
unlink(src); rmdir(tmpdir);
|
|
continue;
|
|
}
|
|
|
|
char outbin[128];
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(outbin, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
|
|
int got = runwait(outbin);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
unlink(outbin); rmdir(tmpdir);
|
|
|
|
if (!rows[i].byteid) { unlink(src); continue; }
|
|
|
|
char cs_s[64], ws_s[64];
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/wwsca_%d_%d_cs.s",
|
|
getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/wwsca_%d_%d_ww.s",
|
|
getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
|
fail++; unlink(src); continue;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
|
rows[i].label);
|
|
fail++; unlink(src); unlink(cs_s); continue;
|
|
}
|
|
if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr,
|
|
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
|
"byte-id violation)\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
unlink(src); unlink(cs_s); unlink(ws_s);
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d slicecopy-assign tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("slicecopy: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
|
|
return 0;
|
|
}
|