Files
ww/test/wcc/630_let_global.c
Hojun-Cho 97eb1fe20d w6c+selfhost: slice globals — 24B DATAW + (AX,BX,CX) load
Extend top-level mutable `let` to cover slices. Same shape as the
str work, with one more 8-byte field and the address holder CX
overwritten by the cap as the last load step:

  - emit_lets / emitletdataw: 24-byte zero DATAW for `let v: []u8;`
    (and the trivial `nil` init); no slice-literal syntax exists
    so the no-init path is the only supported shape;
  - cgident: LEAQ name(SB), CX → MOVQ (CX), AX → MOVQ 8(CX), BX →
    MOVQ 16(CX), CX, so the slice ABI triple lands in (AX, BX, CX);
  - cgdot: .cap delta 16 wired alongside .ptr / .len through the
    same &name(SB) base.

Slice reassignment (`v = some_slice;`) is still unsupported — slice
values don't yet flow as a full (AX, BX, CX) triple through general
expressions even for locals — so reads/`&` are the supported surface
today. Manual fill through `(&v): *u64` continues to work.

Tests 630 (10/10), 990, 994, 995 stay green; bootstrap fixed point
holds.
2026-05-12 12:16:52 +09:00

166 lines
3.7 KiB
C

/*
* 630_let_global — top-level mutable `let` end-to-end through the
* full Cstage pipeline (w6c → w6a → w6l). Each fixture is a tiny
* .ww program that exercises one path: literal-init read, plain
* assignment, compound assignment, and address-of.
*
* Exit code = the value the runtime feeds to `exit(2)`. main's
* return value flows into DI via rt/start.s and ends up as the
* shell's $?.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
static int
run_exit(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
static int
build_and_run(const char *bin, const char *prog)
{
/* `ww run` builds with the driver (w6c → w6a → w6l + libwwrt.a)
* and executes the result. The exit code propagates back as the
* shell's $? so we just compare against the fixture's want. */
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wwt_lg_%d.ww", getpid());
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(prog, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/ww run %s", bin, src);
int rc = run_exit(cmd);
unlink(src);
return rc;
}
struct fixture {
const char *label;
const char *prog;
int want;
};
static const struct fixture fixtures[] = {
{
"read",
"let counter: i32 = 42;\n"
"fn main() i32 = { return counter; };\n",
42,
},
{
"assign",
"let counter: i32 = 0;\n"
"fn main() i32 = { counter = 99; return counter; };\n",
99,
},
{
"compound",
"let counter: i32 = 1;\n"
"fn inc() void = { counter += 1; };\n"
"fn main() i32 = { inc(); inc(); inc(); return counter; };\n",
4,
},
{
"zero-init",
/* No initialiser → zero-filled DATAW slot. */
"let counter: i32;\n"
"fn main() i32 = { counter = 7; return counter; };\n",
7,
},
{
"addr-of",
"let counter: i32 = 11;\n"
"fn main() i32 = {\n"
"\tlet p: *i32 = &counter;\n"
"\t*p = 55;\n"
"\treturn counter;\n"
"};\n",
55,
},
{
"u64-read",
"let val: u64 = 123u64;\n"
"fn main() i32 = { return val: i32; };\n",
123,
},
{
"str-zeroinit",
/* `let msg: str;` zero-inits the {ptr,len} slot. Assigning
* a strlit at runtime updates both halves; .len then reads
* the stored length. */
"let msg: str;\n"
"fn main() i32 = {\n"
"\tmsg = \"hello\";\n"
"\treturn msg.len: i32;\n"
"};\n",
5,
},
{
"str-reassign",
"let msg: str;\n"
"fn set(s: str) void = { msg = s; };\n"
"fn main() i32 = {\n"
"\tset(\"abcdefg\");\n"
"\treturn msg.len: i32;\n"
"};\n",
7,
},
{
"slice-zeroinit",
/* Slice globals start as {nil, 0, 0}. .len and .cap both
* read zero; .ptr is nil but we don't dereference it. */
"let buf: []u8;\n"
"fn main() i32 = {\n"
"\treturn (buf.len + buf.cap): i32;\n"
"};\n",
0,
},
{
"slice-cap-via-raw-pointer",
/* Drive the slice header through a raw u64 pointer cast
* — the language has no slice-reassignment expression
* yet, so this is the only way to fill the header from
* ww source today. .cap reads back as the value we
* wrote. */
"let buf: []u8;\n"
"fn main() i32 = {\n"
"\tlet p: *u64 = (&buf): *u64;\n"
"\tp[2] = 99u64;\n"
"\treturn buf.cap: i32;\n"
"};\n",
99,
},
{ NULL, NULL, 0 }
};
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
int fail = 0, ran = 0;
for (int i = 0; fixtures[i].label; i++, ran++) {
int got = build_and_run(bin, fixtures[i].prog);
if (got != fixtures[i].want) {
fprintf(stderr, "let_global[%s]: exit=%d, want %d\n",
fixtures[i].label, got, fixtures[i].want);
fail++;
}
}
if (fail) {
fprintf(stderr, "let_global: %d/%d fixtures failed\n", fail, ran);
return 1;
}
printf("let_global: %d/%d ok\n", ran, ran);
return 0;
}