lib: graduate bytes/strings find-funcs to (i32 | void)
Replaces the -1 sentinel return on indexbyte/byteindex/rbyteindex/ index with Hare's optional-shaped tagged union. Callers `match` on the result and bind the index from the i32 variant. Two cgen fixes were needed first: 1. resolve_type for N_TTAGGED rounded value payload up to an 8-byte multiple. (i32 | void) was sized 12 — tag (8) + payload (4) — which made the reg-passing ABI compute size/8 = 1 word and drop the value word. 2. The call-arg push path special-cased struct and slice args but not tagged-return calls. A nested `f(g())` where g returns a tagged union pushed only AX (tag); the matching pop loaded a stale DX/SI for the value. Now pushes AX/DX[/CX] in order so the pop side drains tag → arg-reg[0], value(s) → arg-reg[1..]. strings.contains rewritten to match on the new tagged result. No other callers existed in lib/ — bufio/io still use their own shapes.
This commit is contained in:
@@ -784,17 +784,24 @@ static const struct row rows[] = {
|
||||
" };\n"
|
||||
" return acc;\n"
|
||||
"};", 143 }, /* 123 + 20 */
|
||||
/* strings.byteindex (Plan 9 -1) and strings.index (substring). */
|
||||
/* strings.byteindex and strings.index: now (i32 | void). */
|
||||
{ "use strings;\n"
|
||||
"fn pick(r: (i32 | void), miss: i32) i32 = {\n"
|
||||
" match (r) {\n"
|
||||
" case let i: i32 => return i;\n"
|
||||
" case void => return miss;\n"
|
||||
" };\n"
|
||||
" return 0;\n"
|
||||
"};\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let s: str = \"hello, world\";\n"
|
||||
" let i1: i32 = strings.byteindex(s, 44u8);\n"
|
||||
" let i2: i32 = strings.byteindex(s, 122u8);\n"
|
||||
" let i3: i32 = strings.index(s, \"world\");\n"
|
||||
" let i4: i32 = strings.index(s, \"nope\");\n"
|
||||
" let i1: i32 = pick(strings.byteindex(s, 44u8), -1);\n"
|
||||
" let i2: i32 = pick(strings.byteindex(s, 122u8), -1);\n"
|
||||
" let i3: i32 = pick(strings.index(s, \"world\"), -1);\n"
|
||||
" let i4: i32 = pick(strings.index(s, \"nope\"), -1);\n"
|
||||
" return i1 + i2 + i3 + i4;\n"
|
||||
"};", 10 }, /* 5 + (-1) + 7 + (-1) */
|
||||
/* bytes.index: substring search over []u8. */
|
||||
/* bytes.index: substring search over []u8, (i32 | void). */
|
||||
{ "use bytes;\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let buf: [12]u8;\n"
|
||||
@@ -803,7 +810,12 @@ static const struct row rows[] = {
|
||||
" buf[8] = 111u8; buf[9] = 114u8; buf[10] = 108u8; buf[11] = 100u8;\n"
|
||||
" let needle: [3]u8;\n"
|
||||
" needle[0] = 119u8; needle[1] = 111u8; needle[2] = 114u8;\n"
|
||||
" return bytes.index(buf[0:12], needle[0:3]);\n"
|
||||
" let r: (i32 | void) = bytes.index(buf[0:12], needle[0:3]);\n"
|
||||
" match (r) {\n"
|
||||
" case let i: i32 => return i;\n"
|
||||
" case void => return -1;\n"
|
||||
" };\n"
|
||||
" return 0;\n"
|
||||
"};", 7 },
|
||||
/* errors.equal — sentinel comparison through a (T | error) union.
|
||||
* Sets up two errors, dispatches each, and confirms the matching
|
||||
|
||||
Reference in New Issue
Block a user