parse,check,type,wwi: @packed struct attribute, both stages (#51)
Hare/harec @packed struct layout: no inter-field or trailing padding, align =
max field align (NOT forced to 1) — matches harec type_store.c + types.c:621
(packed{u8,u64}=size 9/align 8). Parser consumes inline @packed (loud-rejects
unknown struct attrs, both stages); layout gates padding on !packed; cstage
type_eq enforces packed type-distinctness; the .wwi producer round-trips
"struct @packed {". wwstage sets slotsize=size for packed so its composite-ABI
copy matches cstage byte-for-byte. cstage identity is faithful; wwstage identity
rides the deferred #224 nominal-resolvealias arc (#108). Both stages byte-id;
447 tests pass.
This commit is contained in:
7
Makefile
7
Makefile
@@ -241,6 +241,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_arr_elem_field_write \
|
||||
$(BIN)/test_arr_enum_elem \
|
||||
$(BIN)/test_arr_strslice_elem \
|
||||
$(BIN)/test_struct_packed \
|
||||
$(BIN)/test_arr_tagged_elem \
|
||||
$(BIN)/test_dup_main_reject \
|
||||
$(BIN)/test_tuple_trailing_comma_reject \
|
||||
@@ -1346,6 +1347,12 @@ $(BIN)/test_arr_strslice_elem: test/wcc/683_arr_strslice_elem.c $(BIN)/ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_struct_packed: test/wcc/671_struct_packed.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_arr_infer_len: test/wcc/684_arr_infer_len.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
|
||||
@@ -252,7 +252,9 @@ wwi_type(FILE *of, Node *t)
|
||||
wwi_type(of, t->lhs);
|
||||
break;
|
||||
case N_TSTRUCT:
|
||||
fputs("struct { ", of);
|
||||
/* re-emit the `@packed` attr so the flag round-trips
|
||||
* through sep-compile (harec unparse/type.ha:122-126). */
|
||||
fputs(t->packed ? "struct @packed { " : "struct { ", of);
|
||||
for (Node *f = t->list; f; f = f->next) {
|
||||
if (f != t->list) fputs(", ", of);
|
||||
if (f->str && f->str[0]) {
|
||||
|
||||
@@ -935,7 +935,11 @@ resolve_type(Checker *c, Node *n)
|
||||
if (!require_sized(c, ft, f->pos, "a struct field"))
|
||||
continue;
|
||||
if (ft->align > maxalign) maxalign = ft->align;
|
||||
off = (off + ft->align - 1) & ~(ft->align - 1);
|
||||
/* packed: no inter-field padding (harec
|
||||
* type_store.c:206-213); align still tracks the
|
||||
* max field align below. */
|
||||
if (!n->packed)
|
||||
off = (off + ft->align - 1) & ~(ft->align - 1);
|
||||
if (f->str != NULL) {
|
||||
/* regular named field */
|
||||
for (Tfield *e = head; e; e = e->next)
|
||||
@@ -985,8 +989,12 @@ resolve_type(Checker *c, Node *n)
|
||||
off = base + inner->size;
|
||||
}
|
||||
t->fields = head;
|
||||
t->packed = n->packed;
|
||||
t->align = maxalign;
|
||||
t->size = (off + maxalign - 1) & ~(maxalign - 1);
|
||||
/* packed: skip the trailing pad-to-align (harec
|
||||
* type_store.c:886 `!packed`); align value unchanged. */
|
||||
t->size = n->packed ? off
|
||||
: ((off + maxalign - 1) & ~(maxalign - 1));
|
||||
return t;
|
||||
}
|
||||
case N_TENUM: {
|
||||
|
||||
@@ -204,8 +204,25 @@ parsetype(Parser *p)
|
||||
}
|
||||
case TK_STRUCT: {
|
||||
advance(p);
|
||||
expect(p, TK_LBRACE);
|
||||
Node *n = newnode(p->a, N_TSTRUCT, pp);
|
||||
/* `@packed` is an inline struct TYPE attribute (harec
|
||||
* ast.h:95 `bool packed`), sitting after `struct` and
|
||||
* before `{` — NOT a fn-decl attr, so it does not route
|
||||
* through parseattrs. */
|
||||
if (p->cur.kind == TK_AT) {
|
||||
advance(p);
|
||||
const char *an = expectident(p);
|
||||
/* p->errs gates the build (cmd/w6c/main.c:82);
|
||||
* nerrors is advisory-only, so an errorf on a
|
||||
* clean-recovery path must bump p->errs itself. */
|
||||
if (strcmp(an, "packed") != 0) {
|
||||
errorf(p->cur.pos,
|
||||
"unknown struct attribute '@%s'", an);
|
||||
p->errs++;
|
||||
} else
|
||||
n->packed = 1;
|
||||
}
|
||||
expect(p, TK_LBRACE);
|
||||
Node *head = NULL, *tail = NULL;
|
||||
while (p->cur.kind != TK_RBRACE && p->cur.kind != TK_EOF) {
|
||||
Pos fp = p->cur.pos;
|
||||
|
||||
@@ -267,6 +267,9 @@ type_eq(Type *a, Type *b)
|
||||
return pa == NULL && pb == NULL;
|
||||
}
|
||||
case TY_STRUCT: {
|
||||
/* packed is part of struct identity: a packed struct is
|
||||
* not equal to its unpacked twin (harec types.c:621). */
|
||||
if (a->packed != b->packed) return 0;
|
||||
Tfield *fa = a->fields, *fb = b->fields;
|
||||
while (fa && fb) {
|
||||
if (strcmp(fa->name, fb->name) != 0) return 0;
|
||||
|
||||
@@ -345,6 +345,8 @@ struct Node {
|
||||
Node *next; /* sibling link inside `list` */
|
||||
Node *attr; /* @attribute chain (N_ATTR list) */
|
||||
int export;
|
||||
int packed; /* N_TSTRUCT: `struct @packed` — no field/
|
||||
* trailing padding (harec ast.h:95). */
|
||||
Type *type; /* filled in by checker */
|
||||
const char *tsuffix; /* typed numeric literal suffix */
|
||||
const char *module; /* `// MODULE: foo` directive at the
|
||||
@@ -487,6 +489,8 @@ struct Type {
|
||||
* stored as a single 8-byte pointer; null
|
||||
* is the void variant. Mirrors Hare's
|
||||
* `(*T | null)` folding. */
|
||||
int packed; /* TY_STRUCT laid out with no padding; part
|
||||
* of type identity (harec types.c:517/621). */
|
||||
};
|
||||
|
||||
extern Type *ty_void, *ty_bool, *ty_rune;
|
||||
|
||||
@@ -133,6 +133,7 @@ export type node = struct {
|
||||
next: *node,
|
||||
attr: *node,
|
||||
exported: i32, // bool — `export` keyword present
|
||||
packed: i32, // N_TSTRUCT: `struct @packed` — no padding (harec ast.h:95)
|
||||
type_: *void, // filled in by checker; type.ww treats it as *tinfo
|
||||
tsuffix: str, // typed numeric literal suffix ("i32", "u64", ...)
|
||||
nmod: str, // originating module from `// MODULE: foo`; "" if none
|
||||
@@ -146,7 +147,7 @@ export fn newnode(k: nkind, file: str, line: i32, col: i32) *node = {
|
||||
// fval cast-init: 990's wwdump TK_FLOAT diff requires this file
|
||||
// to tokenise identically through C and ww (lex.ww:382 has the
|
||||
// same workaround for the cstage %g-formats vs ww-skips divergence).
|
||||
let n: *node = alloc(node{kind=k, file=file, line=line, col=col, op=tkind.TK_NONE, str="", uval=0u64, fval=0: f64, lhs=nil, rhs=nil, cond=nil, body=nil, els=nil, list=nil, next=nil, attr=nil, exported=0, type_=nil, tsuffix="", nmod="", usepath="", imported=0})!;
|
||||
let n: *node = alloc(node{kind=k, file=file, line=line, col=col, op=tkind.TK_NONE, str="", uval=0u64, fval=0: f64, lhs=nil, rhs=nil, cond=nil, body=nil, els=nil, list=nil, next=nil, attr=nil, exported=0, packed=0, type_=nil, tsuffix="", nmod="", usepath="", imported=0})!;
|
||||
return n;
|
||||
};
|
||||
|
||||
|
||||
@@ -191,8 +191,21 @@ fn parsetype(p: *parser) *node = {
|
||||
|
||||
if (p.curkind == tkind.TK_STRUCT) {
|
||||
advance(p);
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' after struct");
|
||||
let n = newnode(nkind.N_TSTRUCT, pf, pl, pc);
|
||||
// `@packed` is an inline struct TYPE attribute (harec ast.h:95),
|
||||
// after `struct` and before `{` — NOT a fn-decl attr, so it does
|
||||
// not route through parseattrs.
|
||||
if (p.curkind == tkind.TK_AT) {
|
||||
advance(p);
|
||||
let an: str;
|
||||
expectident(p, &an);
|
||||
if (!streq(an, "packed")) {
|
||||
errmsg(p, "unknown struct attribute");
|
||||
} else {
|
||||
n.packed = 1;
|
||||
};
|
||||
};
|
||||
expecttok(p, tkind.TK_LBRACE, "expected '{' after struct");
|
||||
let fhead: *node = nil;
|
||||
let ftail: *node = nil;
|
||||
for (p.curkind != tkind.TK_RBRACE) {
|
||||
|
||||
@@ -144,6 +144,9 @@ export type tinfo = struct {
|
||||
// still lives at slotsize()'s read site;
|
||||
// graduating it here would break `[N]i32`
|
||||
// stride (4*N stays natural).
|
||||
packed: i32, // TY_STRUCT laid out with no padding; part of type
|
||||
// identity (harec types.c:517/621). Mirrors cstage
|
||||
// Type.packed (cmd/wcc/ww.h).
|
||||
};
|
||||
|
||||
// #61 audit §1.8 / Rob+Drew convergence 2026-05-20: memoizes
|
||||
@@ -202,7 +205,7 @@ export type tctx = struct {
|
||||
// ---- constructors -----------------------------------------------------
|
||||
|
||||
export fn newtype(k: tykind) *tinfo = {
|
||||
let t: *tinfo = alloc(tinfo{kind=k, size=0u64, align=0u64, sub=nil, alen=0u64, fields=nil, params=nil, tupleelems=nil, ret=nil, variadic=0, nullable=0, name="", under=nil, slotsize=0u64})!;
|
||||
let t: *tinfo = alloc(tinfo{kind=k, size=0u64, align=0u64, sub=nil, alen=0u64, fields=nil, params=nil, tupleelems=nil, ret=nil, variadic=0, nullable=0, name="", under=nil, slotsize=0u64, packed=0})!;
|
||||
return t;
|
||||
};
|
||||
|
||||
@@ -559,6 +562,9 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_STRUCT) {
|
||||
// packed is part of struct identity: a packed struct is not
|
||||
// equal to its unpacked twin (harec types.c:621).
|
||||
if (a.packed != b.packed) { return false; };
|
||||
let fa: *tfield = a.fields;
|
||||
let fb: *tfield = b.fields;
|
||||
for (true) {
|
||||
|
||||
@@ -1311,11 +1311,17 @@ fn astsize(c: *checker, t: *syntax.node) i64 = {
|
||||
if (f.kind == syntax.nkind.N_TFIELD) {
|
||||
let fa: i64 = astalign(c, f.lhs);
|
||||
if (fa > maxal) { maxal = fa; };
|
||||
off = (off + fa - 1i64) & ~(fa - 1i64);
|
||||
// packed: no inter-field padding (harec
|
||||
// type_store.c:206-213); align value unchanged.
|
||||
if (t.packed == 0) {
|
||||
off = (off + fa - 1i64) & ~(fa - 1i64);
|
||||
};
|
||||
off += astsize(c, f.lhs);
|
||||
};
|
||||
f = f.next;
|
||||
};
|
||||
// packed: skip trailing pad-to-align (harec type_store.c:886).
|
||||
if (t.packed != 0) { return off; };
|
||||
return (off + maxal - 1i64) & ~(maxal - 1i64);
|
||||
};
|
||||
if (k == syntax.nkind.N_TTAGGED) {
|
||||
@@ -1528,7 +1534,10 @@ fn astoffset(c: *checker, dot: *syntax.node) i64 = {
|
||||
for (f != nil) {
|
||||
if (f.kind == syntax.nkind.N_TFIELD) {
|
||||
let fa: i64 = astalign(c, f.lhs);
|
||||
off = (off + fa - 1i64) & ~(fa - 1i64);
|
||||
// packed: no inter-field padding (harec type_store.c:206-213).
|
||||
if (rtyp.packed == 0) {
|
||||
off = (off + fa - 1i64) & ~(fa - 1i64);
|
||||
};
|
||||
if (syntax.streq(f.str, dot.str)) { return off; };
|
||||
off += astsize(c, f.lhs);
|
||||
};
|
||||
@@ -2279,7 +2288,10 @@ fn tinfofornode(c: *checker, n: *syntax.node) *syntax.tinfo = {
|
||||
if (circularnamed(c, ft, f)) { ft = c.tc.tyerr; };
|
||||
if (ft != nil) {
|
||||
if (ft.align > maxalign) { maxalign = ft.align; };
|
||||
if (ft.align > 0u64) {
|
||||
// packed: no inter-field padding (harec
|
||||
// type_store.c:206-213); align still tracks the
|
||||
// max field align below.
|
||||
if (n.packed == 0 && ft.align > 0u64) {
|
||||
off = (off + ft.align - 1u64) & ~(ft.align - 1u64);
|
||||
};
|
||||
let fldoff: u64 = off;
|
||||
@@ -2303,14 +2315,29 @@ fn tinfofornode(c: *checker, n: *syntax.node) *syntax.tinfo = {
|
||||
f = f.next;
|
||||
};
|
||||
r.fields = fh;
|
||||
if (maxalign > 0u64) {
|
||||
r.size = (off + maxalign - 1u64) & ~(maxalign - 1u64);
|
||||
r.packed = n.packed;
|
||||
// packed: skip the trailing pad-to-align (harec
|
||||
// type_store.c:886 `!packed`); align value unchanged.
|
||||
if (n.packed != 0) {
|
||||
r.size = off;
|
||||
} else {
|
||||
if (maxalign > 0u64) {
|
||||
r.size = (off + maxalign - 1u64) & ~(maxalign - 1u64);
|
||||
};
|
||||
};
|
||||
r.align = maxalign;
|
||||
if ((soff & 7u64) != 0u64) {
|
||||
soff = (soff + 7u64) & ~7u64;
|
||||
// packed: slotsize == size — cstage has no slotsize, it uses the
|
||||
// (packed) size everywhere incl struct-ABI copy (cmd/w6c/cgen.c:
|
||||
// 15020). Aligning slotsize DOWN to size keeps every wwstage
|
||||
// slot-padded consumer byte-identical to cstage (rule 10).
|
||||
if (n.packed != 0) {
|
||||
r.slotsize = r.size;
|
||||
} else {
|
||||
if ((soff & 7u64) != 0u64) {
|
||||
soff = (soff + 7u64) & ~7u64;
|
||||
};
|
||||
r.slotsize = soff;
|
||||
};
|
||||
r.slotsize = soff;
|
||||
case syntax.nkind.N_TTAGGED:
|
||||
// THE tagged-union normalization SSoT (astsize/astalign delegate
|
||||
// here). Mirrors cstage resolve_type N_TTAGGED (cmd/wcc/check.c:
|
||||
|
||||
@@ -361,7 +361,13 @@ fn wwitype(fd: i32, t: *syntax.node) void = {
|
||||
wputs(fd, ") ");
|
||||
wwitype(fd, t.lhs);
|
||||
} else { if (t.kind == syntax.nkind.N_TSTRUCT) {
|
||||
wputs(fd, "struct { ");
|
||||
// re-emit `@packed` so the flag round-trips through
|
||||
// sep-compile (harec unparse/type.ha:122-126).
|
||||
if (t.packed != 0) {
|
||||
wputs(fd, "struct @packed { ");
|
||||
} else {
|
||||
wputs(fd, "struct { ");
|
||||
};
|
||||
let f: *syntax.node = t.list;
|
||||
for (f != nil) {
|
||||
if (f != t.list) { wputs(fd, ", "); };
|
||||
|
||||
453
test/wcc/671_struct_packed.c
Normal file
453
test/wcc/671_struct_packed.c
Normal file
@@ -0,0 +1,453 @@
|
||||
/*
|
||||
* 671_struct_packed — cstage and wwstage agree, byte-for-byte and at
|
||||
* runtime, on the `struct @packed` attribute (task #51): no inter-field
|
||||
* or trailing padding, alignment UNCHANGED (= max field align, NOT forced
|
||||
* to 1), packed flag part of type identity, and the flag round-trips
|
||||
* through the .wwi sep-compile interface.
|
||||
*
|
||||
* harec reference (verified): a packed struct field offset is the running
|
||||
* size with NO add_padding (ref/harec/src/type_store.c:206-213); the tail
|
||||
* pad-to-align is skipped (type_store.c:886 `!packed`); but `type->align`
|
||||
* is still the max field alignment (type_store.c:193 runs unconditionally)
|
||||
* — so packed `{u8, u64}` is size 9, align 8. Packedness is hashed
|
||||
* (types.c:517) and type-equated (types.c:621), and unparse re-emits
|
||||
* `struct @packed {` (ref/hare/hare/unparse/type.ha:122-126).
|
||||
*
|
||||
* row | shape | want
|
||||
* -----------------+-------------------------------------+-------------
|
||||
* unpacked_size | size(struct{u8,u64}) | 16 + byte-id
|
||||
* packed_size | size(struct @packed{u8,u64}) | 9 + byte-id
|
||||
* packed_off_b | offset(p.b), p: packed{u8,u64} | 1 + byte-id
|
||||
* unpacked_off_b | offset(u.b), u: {u8,u64} | 8 + byte-id
|
||||
* packed_align | align(packed{u8,u64}) — UNCHANGED | 8 + byte-id
|
||||
* packed5_size | size(packed{u8,u32}) | 5 + byte-id
|
||||
* packed5_off_b | offset(.b), packed{u8,u32} | 1 + byte-id
|
||||
* p3_packed_size | size(packed{u8,u16,u8}) | 4 + byte-id
|
||||
* p3_packed_off_c | offset(.c), packed{u8,u16,u8} | 3 + byte-id
|
||||
* p3_unpacked_size | size({u8,u16,u8}) — pads to align 2 | 6 + byte-id
|
||||
* field_roundtrip | packed{u8,u16,u8}, set+sum fields | 6 + byte-id
|
||||
* arr_stride_size | size([3]packed{u8,u32}) | 15 + byte-id
|
||||
* arr_stride_run | [3]packed{u8,u32}, write+read t[2].b| 33 + byte-id
|
||||
*
|
||||
* The runtime rows pin that CGEN reads the packed field offsets (a
|
||||
* regression to padded offsets corrupts the sum / strides into the wrong
|
||||
* element). The arr_stride rows pin the 5-byte (not padded-8) element
|
||||
* stride. The byte-id rows pin rule-10: every layout/offset source is the
|
||||
* type table, so cstage's single `size`/offset and wwstage's tinfo +
|
||||
* astsize/astoffset folds emit identical asm.
|
||||
*
|
||||
* IDENTITY (identity_reject, CSTAGE-asserted): assigning a packed struct
|
||||
* value to a structurally-identical UNPACKED twin is a type error — proves
|
||||
* packed is part of type identity (harec types.c:621). cstage rejects it
|
||||
* (structural type_eq honours packed). wwstage's struct-vs-struct
|
||||
* assignability is the pre-existing broadly-lenient nominal-lossy path
|
||||
* (#224/#10: a bare cross-module same-leaf type name can mis-resolve, so
|
||||
* wwstage cannot confidently reject ANY same-kind struct mismatch — it
|
||||
* already accepts `struct{a:u8}` into `struct{x:u64,y:u64}`); the packed
|
||||
* twin rides that same deferred arc. So this row asserts ONLY that cstage
|
||||
* rejects; wwstage's accept is the documented #224/#10 divergence, not a
|
||||
* #51 regression.
|
||||
*
|
||||
* WWI ROUND-TRIP (wwi_roundtrip): a library package exports
|
||||
* `type Ev = struct @packed {...}`; an importing package computes size(Ev)
|
||||
* and offset of a field ON THE IMPORTER SIDE. The .wwi producer re-emits
|
||||
* `struct @packed {`, the importer re-parses it, so the importer lays Ev
|
||||
* out packed. Both stages must build+run with the packed importer-side
|
||||
* size/offset (9*10+1 = 91; an unpacked Ev would give 16*10+8 = 168).
|
||||
*/
|
||||
#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 sentinels (outside any real exit code 0..255). */
|
||||
#define REJECT_CSTAGE (-2147483647 - 1) /* cstage must FAIL to build */
|
||||
|
||||
/* The packed/unpacked twins shared by the layout rows. Each row's `expr`
|
||||
* is spliced into a fn body that has `p` (packed) and `u` (unpacked) of
|
||||
* each shape in scope, so offset()/field reads have a receiver. */
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "unpacked_size",
|
||||
"package main;\n"
|
||||
"type U = struct { a: u8, b: u64 };\n"
|
||||
"export fn main() i32 = { return size(U): i32; };\n", 16 },
|
||||
|
||||
{ "packed_size",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u64 };\n"
|
||||
"export fn main() i32 = { return size(P): i32; };\n", 9 },
|
||||
|
||||
{ "packed_off_b",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet p: P = P { a = 1u8, b = 2u64 };\n"
|
||||
"\treturn offset(p.b): i32;\n"
|
||||
"};\n", 1 },
|
||||
|
||||
{ "unpacked_off_b",
|
||||
"package main;\n"
|
||||
"type U = struct { a: u8, b: u64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet u: U = U { a = 1u8, b = 2u64 };\n"
|
||||
"\treturn offset(u.b): i32;\n"
|
||||
"};\n", 8 },
|
||||
|
||||
/* harec-critical: packed removes PADDING, not the alignment VALUE. */
|
||||
{ "packed_align",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u64 };\n"
|
||||
"export fn main() i32 = { return align(P): i32; };\n", 8 },
|
||||
|
||||
{ "packed5_size",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u32 };\n"
|
||||
"export fn main() i32 = { return size(P): i32; };\n", 5 },
|
||||
|
||||
{ "packed5_off_b",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet p: P = P { a = 1u8, b = 2u32 };\n"
|
||||
"\treturn offset(p.b): i32;\n"
|
||||
"};\n", 1 },
|
||||
|
||||
{ "p3_packed_size",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u16, c: u8 };\n"
|
||||
"export fn main() i32 = { return size(P): i32; };\n", 4 },
|
||||
|
||||
{ "p3_packed_off_c",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u16, c: u8 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet p: P = P { a = 1u8, b = 2u16, c = 3u8 };\n"
|
||||
"\treturn offset(p.c): i32;\n"
|
||||
"};\n", 3 },
|
||||
|
||||
/* unpacked twin pads b to align 2 (@2), then trailing to align 2 → 6. */
|
||||
{ "p3_unpacked_size",
|
||||
"package main;\n"
|
||||
"type U = struct { a: u8, b: u16, c: u8 };\n"
|
||||
"export fn main() i32 = { return size(U): i32; };\n", 6 },
|
||||
|
||||
/* cgen reads packed offsets at runtime: a=1,b=2,c=3 → 6. */
|
||||
{ "field_roundtrip",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u16, c: u8 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet p: P = P { a = 1u8, b = 2u16, c = 3u8 };\n"
|
||||
"\treturn (p.a: i32) + (p.b: i32) + (p.c: i32);\n"
|
||||
"};\n", 6 },
|
||||
|
||||
{ "arr_stride_size",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u32 };\n"
|
||||
"export fn main() i32 = { return size([3]P): i32; };\n", 15 },
|
||||
|
||||
/* 5-byte stride: t[2].b lands at byte 10+1, not overlapping t[1]. */
|
||||
{ "arr_stride_run",
|
||||
"package main;\n"
|
||||
"type P = struct @packed { a: u8, b: u32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet t: [3]P;\n"
|
||||
"\tt[0].b = 11u32; t[1].b = 22u32; t[2].b = 33u32;\n"
|
||||
"\treturn t[2].b: i32;\n"
|
||||
"};\n", 33 },
|
||||
|
||||
/* by-value ABI (ken): a narrow packed struct (size 5) passed BY
|
||||
* VALUE exercises the sub-8 slotsize through the struct-arg
|
||||
* classify+copy. slotsize == size (5) for packed, so the eightbyte
|
||||
* count and copy width match cstage's size-driven ABI byte-for-byte.
|
||||
* (By-value RETURN of a size∉{8,16} struct is the pre-existing
|
||||
* cstage CALL-drop, task #107 — reproduces UNPACKED too — so the
|
||||
* return half is omitted here; the arg path is the sound oracle.) */
|
||||
{ "byval_arg",
|
||||
"package main;\n"
|
||||
"type Q = struct @packed { a: u8, b: u32 };\n"
|
||||
"fn useq(q: Q) i32 = { return (q.a: i32) + (q.b: i32); };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet q: Q = Q { a = 7u8, b = 100u32 };\n"
|
||||
"\treturn useq(q);\n"
|
||||
"};\n", 107 },
|
||||
|
||||
/* frame-offset proof (ken): a size-5 packed local FOLLOWED by another
|
||||
* local. localreserve rounds the frame cursor (frame+sz+7)&~7 with no
|
||||
* sub-8 floor (byte-id to cstage localslot), so `after` is not
|
||||
* misaligned by the packed slot. p.b write+read confirms the packed
|
||||
* field offset survives the slot. */
|
||||
{ "frame_offset",
|
||||
"package main;\n"
|
||||
"type Q = struct @packed { a: u8, b: u32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet p: Q = Q { a = 1u8, b = 5u32 };\n"
|
||||
"\tlet after: i32 = 77;\n"
|
||||
"\tp.b = 200u32;\n"
|
||||
"\treturn (p.b: i32) + after - (p.a: i32);\n"
|
||||
"};\n", 20 },
|
||||
|
||||
/* identity: packed twin not assignable to unpacked twin. cstage
|
||||
* rejects (structural type_eq honours packed); wwstage rides the
|
||||
* #224/#10 struct-leniency (see header). CSTAGE-asserted only. */
|
||||
{ "identity_reject",
|
||||
"package main;\n"
|
||||
"type A = struct @packed { x: u8, y: u64 };\n"
|
||||
"type B = struct { x: u8, y: u64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet a: A = A { x = 1u8, y = 2u64 };\n"
|
||||
"\tlet b: B = a;\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", REJECT_CSTAGE },
|
||||
|
||||
/* unknown struct attribute is a loud parse error (parse.c /
|
||||
* parse.ww `@%s != packed` branch). cstage-asserted (the reject
|
||||
* harness only pins cstage); wwstage errmsg rejects symmetrically. */
|
||||
{ "unknown_attr_reject",
|
||||
"package main;\n"
|
||||
"type P = struct @foo { a: u8 };\n"
|
||||
"export fn main() i32 = { return 0; };\n", REJECT_CSTAGE },
|
||||
};
|
||||
|
||||
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/pk51_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/pk51_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -2;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
|
||||
tmpdir, driver, src);
|
||||
int built = runwait(cmd);
|
||||
if (built != 0) {
|
||||
/* build failed (a reject, or a real error). Caller decides. */
|
||||
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 — w6c (cstage) vs w6c_ww (wwstage) .s diff. */
|
||||
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/pk51a_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/pk51a_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/pk51a_%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;
|
||||
}
|
||||
|
||||
/* wwi_roundtrip — a library pkg exports a packed type; the importing pkg
|
||||
* computes size+offset on it directly. The .wwi producer must re-emit
|
||||
* `struct @packed {` so the importer re-parses + lays Ev out packed.
|
||||
* Returns the app's exit code (importer-side size(Ev)*10 + offset(.data)
|
||||
* = 9*10 + 1 = 91; a dropped @packed would give 168), or -1 on build fail. */
|
||||
static int
|
||||
wwi_roundtrip(const char *driver, int idx)
|
||||
{
|
||||
char root[80], libdir[128], appww[160], cmd[1024], outbin[200];
|
||||
snprintf(root, sizeof root, "/tmp/pk51w_%d_%d", getpid(), idx);
|
||||
snprintf(libdir, sizeof libdir, "%s/lib/pk2", root);
|
||||
snprintf(appww, sizeof appww, "%s/app/main.ww", root);
|
||||
|
||||
char mk[256];
|
||||
snprintf(mk, sizeof mk, "mkdir -p %s/lib/pk2 %s/app", root, root);
|
||||
if (runwait(mk) != 0) return -1;
|
||||
|
||||
char pk2[256];
|
||||
snprintf(pk2, sizeof pk2, "%s/lib/pk2/pk2.ww", root);
|
||||
FILE *f = fopen(pk2, "wb");
|
||||
if (!f) return -1;
|
||||
/* pk2 exports ONLY the packed type — no helper fns. The size/offset
|
||||
* are computed on the IMPORTER side (below), so the value depends on
|
||||
* the .wwi re-parse laying Ev out packed. (If evsize()/evoff() lived
|
||||
* here the layout would be computed definer-side and the importer's
|
||||
* @packed re-parse would never be exercised.) */
|
||||
fputs("package pk2;\n"
|
||||
"export type Ev = struct @packed { tag: u8, data: u64 };\n", f);
|
||||
fclose(f);
|
||||
|
||||
f = fopen(appww, "wb");
|
||||
if (!f) return -1;
|
||||
/* importer computes layout from the re-parsed .wwi: packed → 9*10+1
|
||||
* = 91; a dropped @packed (unpacked Ev) would give 16*10+8 = 168. */
|
||||
fputs("package main;\n"
|
||||
"import pk2;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet e: pk2.Ev = pk2.Ev { tag = 1u8, data = 2u64 };\n"
|
||||
"\treturn size(pk2.Ev): i32 * 10 + offset(e.data): i32;\n"
|
||||
"};\n", f);
|
||||
fclose(f);
|
||||
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd %s/app && %s build -I %s/lib -o m main.ww 2>/dev/null",
|
||||
root, driver, root);
|
||||
if (runwait(cmd) != 0) {
|
||||
char rm[200];
|
||||
snprintf(rm, sizeof rm, "rm -rf %s", root);
|
||||
runwait(rm);
|
||||
return -1;
|
||||
}
|
||||
snprintf(outbin, sizeof outbin, "%s/app/m", root);
|
||||
int got = runwait(outbin);
|
||||
|
||||
char rm[200];
|
||||
snprintf(rm, sizeof rm, "rm -rf %s", root);
|
||||
runwait(rm);
|
||||
return got;
|
||||
}
|
||||
|
||||
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], wdrv[1024];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated; }
|
||||
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++) {
|
||||
int is_cstage = strcmp(drivers[d].name, "cstage") == 0;
|
||||
if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "struct_packed: 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;
|
||||
if (rows[i].want == REJECT_CSTAGE) {
|
||||
/* cstage must reject (build fail → got==-1). wwstage
|
||||
* rides the #224/#10 struct-leniency (header) — its
|
||||
* disposition is not asserted here. */
|
||||
if (is_cstage)
|
||||
bad = (got != -1);
|
||||
else
|
||||
bad = 0;
|
||||
} else {
|
||||
bad = (got != rows[i].want);
|
||||
}
|
||||
if (bad) {
|
||||
fprintf(stderr,
|
||||
"struct_packed[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
/* wwi cross-module round-trip per driver. */
|
||||
total++;
|
||||
int rt = wwi_roundtrip(drivers[d].path, d);
|
||||
if (rt != 91) {
|
||||
fprintf(stderr,
|
||||
"struct_packed[%s][wwi_roundtrip]: exit=%d want=91\n",
|
||||
drivers[d].name, rt);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* asm byte-id: only the value rows (REJECT_CSTAGE rows have no
|
||||
* cstage asm). Gated on ww_ww presence. */
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (rows[i].want == REJECT_CSTAGE) continue;
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "struct_packed: %d/%d fixtures failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("struct_packed: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user