diff --git a/selfhost/cmd/w6a/main.combined.ww b/selfhost/cmd/w6a/main.combined.ww index 5c296b25..08768975 100644 --- a/selfhost/cmd/w6a/main.combined.ww +++ b/selfhost/cmd/w6a/main.combined.ww @@ -2149,7 +2149,7 @@ use parse; use asm; use obj; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -2168,8 +2168,8 @@ fn cstrlen(p: *u8) u64 = { }; // Slurp the whole file into a fresh buffer. -fn slurp(path_cs: *u8) (*u8, u64) = { - let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32); +fn slurp(path: *u8) (*u8, u64) = { + let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return nil, 0u64; }; let n: i64 = os.filesize(fd); if (n < 0i64) { os.close(fd); return nil, 0u64; }; @@ -2183,44 +2183,44 @@ fn slurp(path_cs: *u8) (*u8, u64) = { }; export fn main(argc: i32, argv: **u8) i32 = { - let src_cs: *u8 = nil; - let out_cs: *u8 = nil; + let src: *u8 = nil; + let out: *u8 = nil; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6a: -o requires arg\n".ptr, 20u64); return 2; }; - out_cs = argv[i]; + out = argv[i]; } else { if (a[0u64] == 45u8) { os.write(2, "w6a: unknown flag\n".ptr, 17u64); return 2; } else { - if (src_cs != nil) { + if (src != nil) { os.write(2, "w6a: only one input\n".ptr, 19u64); return 2; }; - src_cs = a; + src = a; }; }; i += 1; }; - if (src_cs == nil) { + if (src == nil) { os.write(2, "usage: w6a_ww -o file.o file.s\n".ptr, 30u64); return 2; }; - if (out_cs == nil) { + if (out == nil) { os.write(2, "w6a: missing -o\n".ptr, 15u64); return 2; }; let buf: *u8; let blen: u64; - buf, blen = slurp(src_cs); + buf, blen = slurp(src); if (buf == nil) { os.write(2, "w6a: cannot read input\n".ptr, 22u64); return 1; @@ -2228,15 +2228,15 @@ export fn main(argc: i32, argv: **u8) i32 = { let ar: *arena = newarena(); let asm: asm_; - let nlen: u64 = cstrlen(src_cs); - let fname: str = astrndup(ar, src_cs, nlen); + let nlen: u64 = cstrlen(src); + let fname: str = astrndup(ar, src, nlen); a_init(&asm, ar, fname, buf, blen); if (a_parse(&asm) != 0) { return 1; }; if (a_encode(&asm) != 0) { return 1; }; // Open output for write. - let fd: i32 = os.open(out_cs, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 + let fd: i32 = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 if (fd < 0) { os.write(2, "w6a: cannot open output\n".ptr, 23u64); return 1; diff --git a/selfhost/cmd/w6a/main.ww b/selfhost/cmd/w6a/main.ww index 7999bdd7..6575ff5c 100644 --- a/selfhost/cmd/w6a/main.ww +++ b/selfhost/cmd/w6a/main.ww @@ -12,7 +12,7 @@ use parse; use asm; use obj; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -31,8 +31,8 @@ fn cstrlen(p: *u8) u64 = { }; // Slurp the whole file into a fresh buffer. -fn slurp(path_cs: *u8) (*u8, u64) = { - let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32); +fn slurp(path: *u8) (*u8, u64) = { + let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return nil, 0u64; }; let n: i64 = os.filesize(fd); if (n < 0i64) { os.close(fd); return nil, 0u64; }; @@ -46,44 +46,44 @@ fn slurp(path_cs: *u8) (*u8, u64) = { }; export fn main(argc: i32, argv: **u8) i32 = { - let src_cs: *u8 = nil; - let out_cs: *u8 = nil; + let src: *u8 = nil; + let out: *u8 = nil; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6a: -o requires arg\n".ptr, 20u64); return 2; }; - out_cs = argv[i]; + out = argv[i]; } else { if (a[0u64] == 45u8) { os.write(2, "w6a: unknown flag\n".ptr, 17u64); return 2; } else { - if (src_cs != nil) { + if (src != nil) { os.write(2, "w6a: only one input\n".ptr, 19u64); return 2; }; - src_cs = a; + src = a; }; }; i += 1; }; - if (src_cs == nil) { + if (src == nil) { os.write(2, "usage: w6a_ww -o file.o file.s\n".ptr, 30u64); return 2; }; - if (out_cs == nil) { + if (out == nil) { os.write(2, "w6a: missing -o\n".ptr, 15u64); return 2; }; let buf: *u8; let blen: u64; - buf, blen = slurp(src_cs); + buf, blen = slurp(src); if (buf == nil) { os.write(2, "w6a: cannot read input\n".ptr, 22u64); return 1; @@ -91,15 +91,15 @@ export fn main(argc: i32, argv: **u8) i32 = { let ar: *arena = newarena(); let asm: asm_; - let nlen: u64 = cstrlen(src_cs); - let fname: str = astrndup(ar, src_cs, nlen); + let nlen: u64 = cstrlen(src); + let fname: str = astrndup(ar, src, nlen); a_init(&asm, ar, fname, buf, blen); if (a_parse(&asm) != 0) { return 1; }; if (a_encode(&asm) != 0) { return 1; }; // Open output for write. - let fd: i32 = os.open(out_cs, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 + let fd: i32 = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 if (fd < 0) { os.write(2, "w6a: cannot open output\n".ptr, 23u64); return 1; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 8af9b120..fb0bcbb0 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -6696,7 +6696,7 @@ use sym; use check; use cgen; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -6714,8 +6714,8 @@ fn cstrlen(p: *u8) u64 = { return n; }; -fn slurp(path_cs: *u8) (*u8, u64) = { - let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32); +fn slurp(path: *u8) (*u8, u64) = { + let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return nil, 0u64; }; let n: i64 = os.filesize(fd); if (n < 0i64) { os.close(fd); return nil, 0u64; }; @@ -6729,40 +6729,40 @@ fn slurp(path_cs: *u8) (*u8, u64) = { }; export fn main(argc: i32, argv: **u8) i32 = { - let src_cs: *u8 = nil; - let out_cs: *u8 = nil; + let src: *u8 = nil; + let out: *u8 = nil; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6c: -o requires arg\n".ptr, 20u64); return 2; }; - out_cs = argv[i]; + out = argv[i]; } else { if (a[0u64] == 45u8) { os.write(2, "w6c: unknown flag\n".ptr, 17u64); return 2; } else { - if (src_cs != nil) { + if (src != nil) { os.write(2, "w6c: only one input\n".ptr, 19u64); return 2; }; - src_cs = a; + src = a; }; }; i += 1; }; - if (src_cs == nil) { + if (src == nil) { os.write(2, "usage: w6c_ww [-o out.s] file.ww\n".ptr, 32u64); return 2; }; let buf: *u8; let blen: u64; - buf, blen = slurp(src_cs); + buf, blen = slurp(src); if (buf == nil) { os.write(2, "w6c: cannot read input\n".ptr, 22u64); return 1; @@ -6771,8 +6771,8 @@ export fn main(argc: i32, argv: **u8) i32 = { // Redirect fd 1 to the output file before any cgen emit runs. // cgen.ww writes directly to fd 1; dup2 lets us reuse it without // threading a file descriptor through the emit helpers. - if (out_cs != nil) { - let ofd: i32 = os.open(out_cs, + if (out != nil) { + let ofd: i32 = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 if (ofd < 0) { os.write(2, "w6c: cannot open output\n".ptr, 23u64); @@ -6787,8 +6787,8 @@ export fn main(argc: i32, argv: **u8) i32 = { }; let ar: *arena = newarena(); - let nlen: u64 = cstrlen(src_cs); - let fname: str = astrndup(ar, src_cs, nlen); + let nlen: u64 = cstrlen(src); + let fname: str = astrndup(ar, src, nlen); let l: lex; lexinit(&l, ar, fname, buf, blen); diff --git a/selfhost/cmd/w6c/main.ww b/selfhost/cmd/w6c/main.ww index 0766f0cf..cc638e6e 100644 --- a/selfhost/cmd/w6c/main.ww +++ b/selfhost/cmd/w6c/main.ww @@ -21,7 +21,7 @@ use sym; use check; use cgen; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -39,8 +39,8 @@ fn cstrlen(p: *u8) u64 = { return n; }; -fn slurp(path_cs: *u8) (*u8, u64) = { - let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32); +fn slurp(path: *u8) (*u8, u64) = { + let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return nil, 0u64; }; let n: i64 = os.filesize(fd); if (n < 0i64) { os.close(fd); return nil, 0u64; }; @@ -54,40 +54,40 @@ fn slurp(path_cs: *u8) (*u8, u64) = { }; export fn main(argc: i32, argv: **u8) i32 = { - let src_cs: *u8 = nil; - let out_cs: *u8 = nil; + let src: *u8 = nil; + let out: *u8 = nil; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6c: -o requires arg\n".ptr, 20u64); return 2; }; - out_cs = argv[i]; + out = argv[i]; } else { if (a[0u64] == 45u8) { os.write(2, "w6c: unknown flag\n".ptr, 17u64); return 2; } else { - if (src_cs != nil) { + if (src != nil) { os.write(2, "w6c: only one input\n".ptr, 19u64); return 2; }; - src_cs = a; + src = a; }; }; i += 1; }; - if (src_cs == nil) { + if (src == nil) { os.write(2, "usage: w6c_ww [-o out.s] file.ww\n".ptr, 32u64); return 2; }; let buf: *u8; let blen: u64; - buf, blen = slurp(src_cs); + buf, blen = slurp(src); if (buf == nil) { os.write(2, "w6c: cannot read input\n".ptr, 22u64); return 1; @@ -96,8 +96,8 @@ export fn main(argc: i32, argv: **u8) i32 = { // Redirect fd 1 to the output file before any cgen emit runs. // cgen.ww writes directly to fd 1; dup2 lets us reuse it without // threading a file descriptor through the emit helpers. - if (out_cs != nil) { - let ofd: i32 = os.open(out_cs, + if (out != nil) { + let ofd: i32 = os.open(out, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644 if (ofd < 0) { os.write(2, "w6c: cannot open output\n".ptr, 23u64); @@ -112,8 +112,8 @@ export fn main(argc: i32, argv: **u8) i32 = { }; let ar: *arena = newarena(); - let nlen: u64 = cstrlen(src_cs); - let fname: str = astrndup(ar, src_cs, nlen); + let nlen: u64 = cstrlen(src); + let fname: str = astrndup(ar, src, nlen); let l: lex; lexinit(&l, ar, fname, buf, blen); diff --git a/selfhost/cmd/w6l/main.combined.ww b/selfhost/cmd/w6l/main.combined.ww index c7edadc9..25569435 100644 --- a/selfhost/cmd/w6l/main.combined.ww +++ b/selfhost/cmd/w6l/main.combined.ww @@ -2290,13 +2290,13 @@ use out; def BASE: u64 = 4194304u64; // 0x400000 def CODE_VA_OFF: u64 = 4096u64; // .text starts at base + 0x1000 -fn make_lnk(a: *arena) *lnk = { +fn mklnk(a: *arena) *lnk = { let l: *lnk = amalloc(a, 112u64): *lnk; l.a = a; return l; }; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -2308,30 +2308,14 @@ fn streq_cs(a: *u8, lit: str) bool = { return true; }; -fn cstrlen_l(p: *u8) u64 = { - let n: u64 = 0u64; - for (p[n] != 0u8) { n += 1u64; }; - return n; -}; - -// strncmp(a, lit, n): true iff a's first n bytes == lit's first n bytes. -fn strn_eq(a: *u8, lit: str, n: u64) bool = { - if ((lit.len: u64) < n) { return false; }; - let i: u64 = 0u64; - for (i < n) { - let li: i32 = i: i32; - if (a[i] != lit[li]) { return false; }; - i += 1u64; - }; - return true; -}; +// `cstrlen` lives in obj.ww — same bundle, single definition. // Build "/lib." into dst (NUL-terminated). Returns total // length excluding NUL. dst must be large enough. -fn build_path(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { +fn buildpath(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { let i: u64 = 0u64; - let dn: u64 = cstrlen_l(dir); - let nn: u64 = cstrlen_l(name); + let dn: u64 = cstrlen(dir); + let nn: u64 = cstrlen(name); let k: u64 = 0u64; for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; }; dst[i] = 47u8; // '/' @@ -2356,7 +2340,7 @@ fn build_path(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { }; // Append decimal n to dst at offset i. Returns new offset. -fn append_dec(dst: *u8, i: u64, n: u64) u64 = { +fn appenddec(dst: *u8, i: u64, n: u64) u64 = { if (n == 0u64) { dst[i] = 48u8; return i + 1u64; @@ -2378,10 +2362,10 @@ fn append_dec(dst: *u8, i: u64, n: u64) u64 = { return oi; }; -fn build_path_v(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { +fn buildpathv(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { let i: u64 = 0u64; - let dn: u64 = cstrlen_l(dir); - let nn: u64 = cstrlen_l(name); + let dn: u64 = cstrlen(dir); + let nn: u64 = cstrlen(name); let k: u64 = 0u64; for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; }; dst[i] = 47u8; i += 1u64; @@ -2391,13 +2375,13 @@ fn build_path_v(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { k = 0u64; for (k < nn) { dst[i] = name[k]; i += 1u64; k += 1u64; }; dst[i] = 46u8; i += 1u64; dst[i] = 115u8; i += 1u64; dst[i] = 111u8; i += 1u64; dst[i] = 46u8; i += 1u64; - i = append_dec(dst, i, v); + i = appenddec(dst, i, v); dst[i] = 0u8; return i; }; -// is_linkable: read first 8 bytes; require !\n or \x7fELF. -fn is_linkable(path: *u8) bool = { +// islinkable: read first 8 bytes; require !\n or \x7fELF. +fn islinkable(path: *u8) bool = { let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return false; }; let mp: *u8 = os.alloc(8u64): *u8; @@ -2427,14 +2411,14 @@ fn is_linkable(path: *u8) bool = { // Walk libdirs[0..n) trying lib.so, then lib.so.{0..8}, // then lib.a. Return arena-owned NUL-terminated path on success, // nil on miss. -fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { +fn resolvelib(a: *arena, name: *u8, libdirs: **u8, nlibdirs: i32) *u8 = { let bufp: *u8 = os.alloc(1024u64): *u8; let i: i32 = 0; - for (i < n_libdirs) { + for (i < nlibdirs) { let dir: *u8 = libdirs[i]; - let _l1: u64 = build_path(bufp, dir, name, ".so"); - if (is_linkable(bufp)) { - let pl: u64 = cstrlen_l(bufp); + let _l1: u64 = buildpath(bufp, dir, name, ".so"); + if (islinkable(bufp)) { + let pl: u64 = cstrlen(bufp); let p: *u8 = amalloc(a, pl + 1u64): *u8; let k: u64 = 0u64; for (k <= pl) { p[k] = bufp[k]; k += 1u64; }; @@ -2442,9 +2426,9 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; let v: u64 = 0u64; for (v <= 8u64) { - let _l2: u64 = build_path_v(bufp, dir, name, v); - if (is_linkable(bufp)) { - let pl2: u64 = cstrlen_l(bufp); + let _l2: u64 = buildpathv(bufp, dir, name, v); + if (islinkable(bufp)) { + let pl2: u64 = cstrlen(bufp); let p2: *u8 = amalloc(a, pl2 + 1u64): *u8; let k2: u64 = 0u64; for (k2 <= pl2) { p2[k2] = bufp[k2]; k2 += 1u64; }; @@ -2452,9 +2436,9 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; v += 1u64; }; - let _l3: u64 = build_path(bufp, dir, name, ".a"); - if (is_linkable(bufp)) { - let pl3: u64 = cstrlen_l(bufp); + let _l3: u64 = buildpath(bufp, dir, name, ".a"); + if (islinkable(bufp)) { + let pl3: u64 = cstrlen(bufp); let p3: *u8 = amalloc(a, pl3 + 1u64): *u8; let k3: u64 = 0u64; for (k3 <= pl3) { p3[k3] = bufp[k3]; k3 += 1u64; }; @@ -2466,7 +2450,7 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; // Read first 20 bytes; return 1 for ET_DYN .so, 0 for ar/.o. -fn is_so(path: *u8) i32 = { +fn isso(path: *u8) i32 = { let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return 0; }; let mp: *u8 = os.alloc(20u64): *u8; @@ -2484,55 +2468,55 @@ fn is_so(path: *u8) i32 = { }; export fn main(argc: i32, argv: **u8) i32 = { - let out_path: *u8 = nil; - let max_inputs: i32 = 64; - let inputs: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; + let outpath: *u8 = nil; + let maxinputs: i32 = 64; + let inputs: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; let ninputs: i32 = 0; - let libdirs: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; - let n_libdirs: i32 = 0; - let lflags: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; - let n_lflags: i32 = 0; + let libdirs: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; + let nlibdirs: i32 = 0; + let lflags: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; + let nlflags: i32 = 0; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6l: -o requires argument\n".ptr, 25u64); return 2; }; - out_path = argv[i]; - } else { if (streq_cs(a, "-L")) { + outpath = argv[i]; + } else { if (cstreq(a, "-L")) { i += 1; if (i >= argc) { os.write(2, "w6l: -L requires argument\n".ptr, 25u64); return 2; }; - libdirs[n_libdirs] = argv[i]; - n_libdirs += 1; - } else { if (streq_cs(a, "-l")) { + libdirs[nlibdirs] = argv[i]; + nlibdirs += 1; + } else { if (cstreq(a, "-l")) { i += 1; if (i >= argc) { os.write(2, "w6l: -l requires argument\n".ptr, 25u64); return 2; }; - lflags[n_lflags] = argv[i]; - n_lflags += 1; + lflags[nlflags] = argv[i]; + nlflags += 1; } else { if (a[0u64] == 45u8) { // -L joined form. if (a[1u64] == 76u8) { if (a[2u64] != 0u8) { - libdirs[n_libdirs] = a + 2u64; - n_libdirs += 1; + libdirs[nlibdirs] = a + 2u64; + nlibdirs += 1; } else { os.write(2, "w6l: bare -L\n".ptr, 12u64); return 2; }; } else { if (a[1u64] == 108u8) { if (a[2u64] != 0u8) { - lflags[n_lflags] = a + 2u64; - n_lflags += 1; + lflags[nlflags] = a + 2u64; + nlflags += 1; } else { os.write(2, "w6l: bare -l\n".ptr, 12u64); return 2; @@ -2542,7 +2526,7 @@ export fn main(argc: i32, argv: **u8) i32 = { return 2; };}; } else { - if (ninputs >= max_inputs) { + if (ninputs >= maxinputs) { os.write(2, "w6l: too many inputs\n".ptr, 20u64); return 2; }; @@ -2552,7 +2536,7 @@ export fn main(argc: i32, argv: **u8) i32 = { i += 1; }; - if (out_path == nil) { + if (outpath == nil) { os.write(2, "usage: w6l_ww -o exe [-L...] [-l...] file1.o [file2.o...]\n".ptr, 68u64); return 2; }; @@ -2562,7 +2546,7 @@ export fn main(argc: i32, argv: **u8) i32 = { }; let a: *arena = newarena(); - let l: *lnk = make_lnk(a); + let l: *lnk = mklnk(a); // Seed _start so libwwrt-style start.o is recognised as wanted. l_intern(l, "_start"); @@ -2579,16 +2563,16 @@ export fn main(argc: i32, argv: **u8) i32 = { // Then resolve -l flags and load each. Archives append; shared // objects register their exports. let lf: i32 = 0; - for (lf < n_lflags) { - let p: *u8 = resolve_lib(a, lflags[lf], libdirs, n_libdirs); + for (lf < nlflags) { + let p: *u8 = resolvelib(a, lflags[lf], libdirs, nlibdirs); if (p == nil) { os.write(2, "w6l: cannot find -l".ptr, 18u64); let nm: *u8 = lflags[lf]; - os.write(2, nm, cstrlen_l(nm)); + os.write(2, nm, cstrlen(nm)); os.write(2, "\n".ptr, 1u64); return 1; }; - if (is_so(p) != 0) { + if (isso(p) != 0) { if (l_load_so(l, p) != 0) { return 1; }; } else { if (l_load(l, p) != 0) { return 1; }; @@ -2599,27 +2583,27 @@ export fn main(argc: i32, argv: **u8) i32 = { if (l_resolve(l) != 0) { return 1; }; if (l_relocate(l, BASE + CODE_VA_OFF) != 0) { return 1; }; - let entry_sym: *lsym = l_lookup(l, "_start"); - if (entry_sym == nil) { entry_sym = l_lookup(l, "main"); } - else { if (entry_sym.defined == 0) { entry_sym = l_lookup(l, "main"); }; }; - if (entry_sym == nil) { + let entrysym: *lsym = l_lookup(l, "_start"); + if (entrysym == nil) { entrysym = l_lookup(l, "main"); } + else { if (entrysym.defined == 0) { entrysym = l_lookup(l, "main"); }; }; + if (entrysym == nil) { os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64); return 1; }; - if (entry_sym.defined == 0) { + if (entrysym.defined == 0) { os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64); return 1; }; let flags: i32 = os.O_WRONLY | os.O_CREAT | os.O_TRUNC; - let fd: i32 = os.open(out_path, flags, 493i32); // 0o755 + let fd: i32 = os.open(outpath, flags, 493i32); // 0o755 if (fd < 0) { os.write(2, "w6l: cannot open output\n".ptr, 23u64); return 1; }; - let entry_va: u64 = BASE + CODE_VA_OFF + entry_sym.val; - let rc: i32 = l_emit_elf(l, fd, BASE, entry_va); + let entryva: u64 = BASE + CODE_VA_OFF + entrysym.val; + let rc: i32 = l_emit_elf(l, fd, BASE, entryva); os.close(fd); return rc; }; diff --git a/selfhost/cmd/w6l/main.ww b/selfhost/cmd/w6l/main.ww index da5450a0..32685ea5 100644 --- a/selfhost/cmd/w6l/main.ww +++ b/selfhost/cmd/w6l/main.ww @@ -17,13 +17,13 @@ use out; def BASE: u64 = 4194304u64; // 0x400000 def CODE_VA_OFF: u64 = 4096u64; // .text starts at base + 0x1000 -fn make_lnk(a: *arena) *lnk = { +fn mklnk(a: *arena) *lnk = { let l: *lnk = amalloc(a, 112u64): *lnk; l.a = a; return l; }; -fn streq_cs(a: *u8, lit: str) bool = { +fn cstreq(a: *u8, lit: str) bool = { let n: u64 = lit.len: u64; let i: u64 = 0u64; for (i < n) { @@ -35,30 +35,14 @@ fn streq_cs(a: *u8, lit: str) bool = { return true; }; -fn cstrlen_l(p: *u8) u64 = { - let n: u64 = 0u64; - for (p[n] != 0u8) { n += 1u64; }; - return n; -}; - -// strncmp(a, lit, n): true iff a's first n bytes == lit's first n bytes. -fn strn_eq(a: *u8, lit: str, n: u64) bool = { - if ((lit.len: u64) < n) { return false; }; - let i: u64 = 0u64; - for (i < n) { - let li: i32 = i: i32; - if (a[i] != lit[li]) { return false; }; - i += 1u64; - }; - return true; -}; +// `cstrlen` lives in obj.ww — same bundle, single definition. // Build "/lib." into dst (NUL-terminated). Returns total // length excluding NUL. dst must be large enough. -fn build_path(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { +fn buildpath(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { let i: u64 = 0u64; - let dn: u64 = cstrlen_l(dir); - let nn: u64 = cstrlen_l(name); + let dn: u64 = cstrlen(dir); + let nn: u64 = cstrlen(name); let k: u64 = 0u64; for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; }; dst[i] = 47u8; // '/' @@ -83,7 +67,7 @@ fn build_path(dst: *u8, dir: *u8, name: *u8, ext: str) u64 = { }; // Append decimal n to dst at offset i. Returns new offset. -fn append_dec(dst: *u8, i: u64, n: u64) u64 = { +fn appenddec(dst: *u8, i: u64, n: u64) u64 = { if (n == 0u64) { dst[i] = 48u8; return i + 1u64; @@ -105,10 +89,10 @@ fn append_dec(dst: *u8, i: u64, n: u64) u64 = { return oi; }; -fn build_path_v(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { +fn buildpathv(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { let i: u64 = 0u64; - let dn: u64 = cstrlen_l(dir); - let nn: u64 = cstrlen_l(name); + let dn: u64 = cstrlen(dir); + let nn: u64 = cstrlen(name); let k: u64 = 0u64; for (k < dn) { dst[i] = dir[k]; i += 1u64; k += 1u64; }; dst[i] = 47u8; i += 1u64; @@ -118,13 +102,13 @@ fn build_path_v(dst: *u8, dir: *u8, name: *u8, v: u64) u64 = { k = 0u64; for (k < nn) { dst[i] = name[k]; i += 1u64; k += 1u64; }; dst[i] = 46u8; i += 1u64; dst[i] = 115u8; i += 1u64; dst[i] = 111u8; i += 1u64; dst[i] = 46u8; i += 1u64; - i = append_dec(dst, i, v); + i = appenddec(dst, i, v); dst[i] = 0u8; return i; }; -// is_linkable: read first 8 bytes; require !\n or \x7fELF. -fn is_linkable(path: *u8) bool = { +// islinkable: read first 8 bytes; require !\n or \x7fELF. +fn islinkable(path: *u8) bool = { let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return false; }; let mp: *u8 = os.alloc(8u64): *u8; @@ -154,14 +138,14 @@ fn is_linkable(path: *u8) bool = { // Walk libdirs[0..n) trying lib.so, then lib.so.{0..8}, // then lib.a. Return arena-owned NUL-terminated path on success, // nil on miss. -fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { +fn resolvelib(a: *arena, name: *u8, libdirs: **u8, nlibdirs: i32) *u8 = { let bufp: *u8 = os.alloc(1024u64): *u8; let i: i32 = 0; - for (i < n_libdirs) { + for (i < nlibdirs) { let dir: *u8 = libdirs[i]; - let _l1: u64 = build_path(bufp, dir, name, ".so"); - if (is_linkable(bufp)) { - let pl: u64 = cstrlen_l(bufp); + let _l1: u64 = buildpath(bufp, dir, name, ".so"); + if (islinkable(bufp)) { + let pl: u64 = cstrlen(bufp); let p: *u8 = amalloc(a, pl + 1u64): *u8; let k: u64 = 0u64; for (k <= pl) { p[k] = bufp[k]; k += 1u64; }; @@ -169,9 +153,9 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; let v: u64 = 0u64; for (v <= 8u64) { - let _l2: u64 = build_path_v(bufp, dir, name, v); - if (is_linkable(bufp)) { - let pl2: u64 = cstrlen_l(bufp); + let _l2: u64 = buildpathv(bufp, dir, name, v); + if (islinkable(bufp)) { + let pl2: u64 = cstrlen(bufp); let p2: *u8 = amalloc(a, pl2 + 1u64): *u8; let k2: u64 = 0u64; for (k2 <= pl2) { p2[k2] = bufp[k2]; k2 += 1u64; }; @@ -179,9 +163,9 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; v += 1u64; }; - let _l3: u64 = build_path(bufp, dir, name, ".a"); - if (is_linkable(bufp)) { - let pl3: u64 = cstrlen_l(bufp); + let _l3: u64 = buildpath(bufp, dir, name, ".a"); + if (islinkable(bufp)) { + let pl3: u64 = cstrlen(bufp); let p3: *u8 = amalloc(a, pl3 + 1u64): *u8; let k3: u64 = 0u64; for (k3 <= pl3) { p3[k3] = bufp[k3]; k3 += 1u64; }; @@ -193,7 +177,7 @@ fn resolve_lib(a: *arena, name: *u8, libdirs: **u8, n_libdirs: i32) *u8 = { }; // Read first 20 bytes; return 1 for ET_DYN .so, 0 for ar/.o. -fn is_so(path: *u8) i32 = { +fn isso(path: *u8) i32 = { let fd: i32 = os.open(path, os.O_RDONLY, 0i32); if (fd < 0) { return 0; }; let mp: *u8 = os.alloc(20u64): *u8; @@ -211,55 +195,55 @@ fn is_so(path: *u8) i32 = { }; export fn main(argc: i32, argv: **u8) i32 = { - let out_path: *u8 = nil; - let max_inputs: i32 = 64; - let inputs: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; + let outpath: *u8 = nil; + let maxinputs: i32 = 64; + let inputs: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; let ninputs: i32 = 0; - let libdirs: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; - let n_libdirs: i32 = 0; - let lflags: **u8 = os.alloc((max_inputs: u64) * 8u64): **u8; - let n_lflags: i32 = 0; + let libdirs: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; + let nlibdirs: i32 = 0; + let lflags: **u8 = os.alloc((maxinputs: u64) * 8u64): **u8; + let nlflags: i32 = 0; let i: i32 = 1; for (i < argc) { let a: *u8 = argv[i]; - if (streq_cs(a, "-o")) { + if (cstreq(a, "-o")) { i += 1; if (i >= argc) { os.write(2, "w6l: -o requires argument\n".ptr, 25u64); return 2; }; - out_path = argv[i]; - } else { if (streq_cs(a, "-L")) { + outpath = argv[i]; + } else { if (cstreq(a, "-L")) { i += 1; if (i >= argc) { os.write(2, "w6l: -L requires argument\n".ptr, 25u64); return 2; }; - libdirs[n_libdirs] = argv[i]; - n_libdirs += 1; - } else { if (streq_cs(a, "-l")) { + libdirs[nlibdirs] = argv[i]; + nlibdirs += 1; + } else { if (cstreq(a, "-l")) { i += 1; if (i >= argc) { os.write(2, "w6l: -l requires argument\n".ptr, 25u64); return 2; }; - lflags[n_lflags] = argv[i]; - n_lflags += 1; + lflags[nlflags] = argv[i]; + nlflags += 1; } else { if (a[0u64] == 45u8) { // -L joined form. if (a[1u64] == 76u8) { if (a[2u64] != 0u8) { - libdirs[n_libdirs] = a + 2u64; - n_libdirs += 1; + libdirs[nlibdirs] = a + 2u64; + nlibdirs += 1; } else { os.write(2, "w6l: bare -L\n".ptr, 12u64); return 2; }; } else { if (a[1u64] == 108u8) { if (a[2u64] != 0u8) { - lflags[n_lflags] = a + 2u64; - n_lflags += 1; + lflags[nlflags] = a + 2u64; + nlflags += 1; } else { os.write(2, "w6l: bare -l\n".ptr, 12u64); return 2; @@ -269,7 +253,7 @@ export fn main(argc: i32, argv: **u8) i32 = { return 2; };}; } else { - if (ninputs >= max_inputs) { + if (ninputs >= maxinputs) { os.write(2, "w6l: too many inputs\n".ptr, 20u64); return 2; }; @@ -279,7 +263,7 @@ export fn main(argc: i32, argv: **u8) i32 = { i += 1; }; - if (out_path == nil) { + if (outpath == nil) { os.write(2, "usage: w6l_ww -o exe [-L...] [-l...] file1.o [file2.o...]\n".ptr, 68u64); return 2; }; @@ -289,7 +273,7 @@ export fn main(argc: i32, argv: **u8) i32 = { }; let a: *arena = newarena(); - let l: *lnk = make_lnk(a); + let l: *lnk = mklnk(a); // Seed _start so libwwrt-style start.o is recognised as wanted. l_intern(l, "_start"); @@ -306,16 +290,16 @@ export fn main(argc: i32, argv: **u8) i32 = { // Then resolve -l flags and load each. Archives append; shared // objects register their exports. let lf: i32 = 0; - for (lf < n_lflags) { - let p: *u8 = resolve_lib(a, lflags[lf], libdirs, n_libdirs); + for (lf < nlflags) { + let p: *u8 = resolvelib(a, lflags[lf], libdirs, nlibdirs); if (p == nil) { os.write(2, "w6l: cannot find -l".ptr, 18u64); let nm: *u8 = lflags[lf]; - os.write(2, nm, cstrlen_l(nm)); + os.write(2, nm, cstrlen(nm)); os.write(2, "\n".ptr, 1u64); return 1; }; - if (is_so(p) != 0) { + if (isso(p) != 0) { if (l_load_so(l, p) != 0) { return 1; }; } else { if (l_load(l, p) != 0) { return 1; }; @@ -326,27 +310,27 @@ export fn main(argc: i32, argv: **u8) i32 = { if (l_resolve(l) != 0) { return 1; }; if (l_relocate(l, BASE + CODE_VA_OFF) != 0) { return 1; }; - let entry_sym: *lsym = l_lookup(l, "_start"); - if (entry_sym == nil) { entry_sym = l_lookup(l, "main"); } - else { if (entry_sym.defined == 0) { entry_sym = l_lookup(l, "main"); }; }; - if (entry_sym == nil) { + let entrysym: *lsym = l_lookup(l, "_start"); + if (entrysym == nil) { entrysym = l_lookup(l, "main"); } + else { if (entrysym.defined == 0) { entrysym = l_lookup(l, "main"); }; }; + if (entrysym == nil) { os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64); return 1; }; - if (entry_sym.defined == 0) { + if (entrysym.defined == 0) { os.write(2, "w6l: no _start or main symbol\n".ptr, 29u64); return 1; }; let flags: i32 = os.O_WRONLY | os.O_CREAT | os.O_TRUNC; - let fd: i32 = os.open(out_path, flags, 493i32); // 0o755 + let fd: i32 = os.open(outpath, flags, 493i32); // 0o755 if (fd < 0) { os.write(2, "w6l: cannot open output\n".ptr, 23u64); return 1; }; - let entry_va: u64 = BASE + CODE_VA_OFF + entry_sym.val; - let rc: i32 = l_emit_elf(l, fd, BASE, entry_va); + let entryva: u64 = BASE + CODE_VA_OFF + entrysym.val; + let rc: i32 = l_emit_elf(l, fd, BASE, entryva); os.close(fd); return rc; };