aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile50
-rw-r--r--README.md2
-rw-r--r--TODO.md19
-rw-r--r--src/compiler/builtins.zc2
-rw-r--r--src/compiler/decl.zc2
-rw-r--r--src/compiler/expr.zc7
-rw-r--r--src/compiler/stmt.zc4
-rw-r--r--src/runtime/rt.zc27
-rw-r--r--tests/conformance/4-statements/memdebug_scrub_fills.zc4
-rw-r--r--tests/conformance/4-statements/switch_tagged_empty_parens_with_fields.zc15
-rw-r--r--tests/conformance/5-declarations/global_names_global_b8.zc2
-rw-r--r--tests/conformance/known.txt19
-rw-r--r--tests/langerrors/bad_char_first_statement.zc3
-rw-r--r--tests/langerrors/bad_float_first_statement.zc3
-rw-r--r--tests/langerrors/params_25.zc3
-rw-r--r--tests/programs/memdebug_interior.expected1
-rw-r--r--tests/programs/memdebug_interior.zc18
-rwxr-xr-xtests/test.sh12
18 files changed, 143 insertions, 50 deletions
diff --git a/Makefile b/Makefile
index 8779cc3..b3b7d56 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,9 @@
# (src/tools/xref.sh): build/xref/report.txt and report.json;
# make xref XREF_ARGS=--check compares with tests/xref.baseline
# make genome build/core/genome.bin from src/, with the fixpoint check
+# make LLVM=1 on x86_64, build zy with the llvm backend instead of the x86-64 jit
+# make NOLLVM=1 on other hosts, leave out the llvm backend (in by default when a
+# usable llvm-config is on PATH, or LLVM_CONFIG names one)
# make build/bin/zyi the interpreter alone (zy has a jit on arm64 and on x86-64 linux)
# make lsp build/bin/zclsp, the language server
# make web build/web/: the page, its script and zc.wasm
@@ -95,9 +98,50 @@ $(B)/core/genome.bin: $(ZC_DEPS) $(BIN)/zc0$(EXE) $(BIN)/mkgenome$(EXE) src/core
cp $(B)/s1/genome.bin $@
@echo "fixpoint: $$(( $$(wc -c < $@) / 8 )) words, $@ updated"
-# the vm core with the compiler genome inside: a jit on arm64 and x86-64 linux, else the interpreter
-$(BIN)/zy$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) | $(BIN)
- $(CC) $(CFLAGS) -DJTYPE=int src/core/core.c $(IO) $(COREINC) -o $@
+# the llvm backend (-DZYLLVM) in zy. -DZYLLVM replaces the x86-64 jit, so on x86_64 hosts
+# (uname -m) it is built only when LLVM=1 is given; on other hosts it is in whenever an llvm
+# is found, and NOLLVM=1 leaves it out. the llvm is LLVM_CONFIG when given, else llvm-config,
+# else llvm-config-19 on PATH; it is used only when llvm-c/Core.h exists, --libs is not
+# empty and a tiny program links against it (llvm-config can exist without headers or libs).
+# otherwise the build goes on without it and prints a one-line note.
+HOST_ARCH := $(shell uname -m 2>/dev/null)
+ifeq ($(HOST_ARCH),x86_64)
+ WANT_LLVM := $(if $(filter 1,$(LLVM)),1,)
+else
+ WANT_LLVM := $(if $(filter 1,$(NOLLVM)),,1)
+endif
+ifeq ($(WANT_LLVM),1)
+ ifeq ($(origin LLVM_CONFIG),undefined)
+ LLVM_CONFIG := $(firstword $(shell command -v llvm-config llvm-config-19 2>/dev/null))
+ endif
+ ifneq ($(LLVM_CONFIG),)
+ ifneq ($(wildcard $(shell $(LLVM_CONFIG) --includedir 2>/dev/null)/llvm-c/Core.h),)
+ llvm_try_libs := $(shell $(LLVM_CONFIG) --libs 2>/dev/null)
+ ifneq ($(strip $(llvm_try_libs)),)
+ llvm_try_cflags := $(shell $(LLVM_CONFIG) --cflags 2>/dev/null)
+ llvm_try_ldflags := $(shell $(LLVM_CONFIG) --ldflags 2>/dev/null)
+ llvm_link_ok := $(shell d=$$(mktemp -d) && printf '\043include <llvm-c/Core.h>\nint main(void){LLVMContextDispose(LLVMContextCreate());return 0;}\n' > $$d/t.c && $(CC) $(llvm_try_cflags) $$d/t.c -o $$d/t $(llvm_try_ldflags) $(llvm_try_libs) >/dev/null 2>&1 && echo yes; rm -rf $$d)
+ ifeq ($(llvm_link_ok),yes)
+ LLVM_CFLAGS := -DZYLLVM $(llvm_try_cflags)
+ LLVM_LIBS := $(llvm_try_ldflags) $(llvm_try_libs)
+ endif
+ endif
+ endif
+ endif
+ ifeq ($(LLVM_CFLAGS),)
+ $(info note: no usable llvm (llvm-config with headers, libs and a test link), building zy without the llvm backend)
+ endif
+endif
+# the mode zy was built in; rewritten only when it changes, so switching rebuilds zy
+LLVM_MODE := $(if $(LLVM_CFLAGS),llvm,nollvm)
+ifneq ($(MAKECMDGOALS),clean)
+ $(shell mkdir -p $(B); [ "$$(cat $(B)/llvm.mode 2>/dev/null)" = "$(LLVM_MODE)" ] || echo $(LLVM_MODE) > $(B)/llvm.mode)
+endif
+
+# the vm core with the compiler genome inside: a jit on arm64 and x86-64 linux, else the
+# interpreter; plus the llvm backend when found (above)
+$(BIN)/zy$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) $(B)/llvm.mode | $(BIN)
+ $(CC) $(CFLAGS) -DJTYPE=int $(LLVM_CFLAGS) src/core/core.c $(IO) $(COREINC) -o $@ $(LLVM_LIBS)
# the interpreter alone, even on a host with a jit: make build/bin/zyi, to compare against zy
$(BIN)/zyi$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) | $(BIN)
$(CC) $(CFLAGS) -DZYINTERP -DJTYPE=int src/core/core.c $(IO) $(COREINC) -o $@
diff --git a/README.md b/README.md
index ed6e902..df8c5e9 100644
--- a/README.md
+++ b/README.md
@@ -72,7 +72,7 @@ windows needs clang or gcc and powershell for the tests. the batch file is the s
### native backends
-on arm64, `src/core/core.c` uses its arm64 jit by default, and on x86-64 linux its x86-64 jit. other hosts use the interpreter. `-DZYINTERP` forces interpretation (`make build/bin/zyi` builds that core beside `zy`); `-DZYLLVM` enables the llvm backend when its headers and libraries are available. direct builds from the root are:
+on arm64, `src/core/core.c` uses its arm64 jit by default, and on x86-64 linux its x86-64 jit. other hosts use the interpreter. `-DZYINTERP` forces interpretation (`make build/bin/zyi` builds that core beside `zy`); `-DZYLLVM` enables the llvm backend when its headers and libraries are available. on x86_64 hosts (`uname -m`) `make` keeps the x86-64 jit in `zy` and adds the llvm backend only with `make LLVM=1`, since `-DZYLLVM` replaces that jit; on other hosts `make` adds it whenever it finds a usable llvm, and `make NOLLVM=1` leaves it out. the llvm is `LLVM_CONFIG` if given, else `llvm-config`, else `llvm-config-19` on `PATH`, and it counts only when `llvm-c/Core.h` exists, `--libs` is not empty and a tiny test program links; otherwise the build prints a one-line note and goes on without it. the chosen mode is kept in `build/llvm.mode`, so switching rebuilds `zy` without `make clean`. direct builds from the root are:
```sh
make genome
diff --git a/TODO.md b/TODO.md
index 9a2f52f..4635ef0 100644
--- a/TODO.md
+++ b/TODO.md
@@ -241,9 +241,16 @@ the reasoning behind the open items survives the session.
translation validation of each compile over proving the compiler whole.
- native code: the hand-written jit exists for arm64 and (phase 1, no o3
regions or inlined enhancements yet) x86-64 linux; the llvm backend
- (`-DZYLLVM`) is the portable native tier but the default build leaves it
- out. cheap win: build the llvm core by default whenever `llvm-config` is
- found. the x86-64 port of the hand-written jit is worth it for compile
+ (`-DZYLLVM`) is the portable native tier. `make` puts it in `zy` on
+ non-x86_64 hosts whenever a usable llvm is found (`NOLLVM=1` turns it off);
+ on x86_64 it stays out unless `LLVM=1`, because `-DZYLLVM` replaces the
+ x86-64 jit (owner's call: keep the jit as the x86-64 default). usable means
+ `llvm-config` or `llvm-config-19` with `llvm-c/Core.h`, non-empty `--libs`
+ and a tiny test link; otherwise a one-line note. `build/llvm.mode` (llvm or
+ nollvm, rewritten only on change) makes switching rebuild `zy` without a
+ clean. checked on x86_64 with llvm 19: plain make uses the jit (no llvm
+ regions on o3.zc) and reaches the fixpoint, `make LLVM=1` rebuilds zy with
+ 8 llvm regions, plain make switches back. the non-x86_64 path is untested. the x86-64 port of the hand-written jit is worth it for compile
latency (microseconds per block against milliseconds for llvm), which
matters for short programs and the language server.
@@ -499,8 +506,8 @@ items with an architecture note carry the design worked out so far, enough to
brief someone without re-reading the code.
- [ ] remaining zc0 gaps: string literals over 4095 bytes, `embed` of an array type inside a function, `x as T` with more than a single name (`x as int*`), zc0 silently accepting `.u` on a parameter, and integer literals (`010` is 8 in zc0, 10 in the compiler; zc0 saturates on overflow). `docs/history/43-stdlib/notes.md` still says zc0 reserves u/m/d: add a pointer forward rather than rewriting history.
-- [x] verify both compilers against `docs/SPEC.md`: `tests/conformance/<section>/` (sections 1 to 8 plus `9-appendix-b`, 657 programs), runner `tests/conformance/run.sh` (`--zc0`, `--zym`, `--zy`, `-w`), understood failures in `tests/conformance/known.txt` (class a self-hosted bug, b zc0, c spec issue), wired into the interp section of `tests/test.sh`. zy 626 ok, 31 known; zc0 221 ok, 38 known, 398 skipped (outside appendix a). appendix b.2 items 1 to 16 confirmed except b.2.8, which contradicts 5.3.2 and 3.14.2 (the spec documents the address). new class a bugs: `case member():` on a member with fields is rejected (4.7.3); top-level `o3 { }` is rejected (4.10.2, class c). open: `rt.memreport()` reports buddy capacity (`rt.new(2)` is 3 words), whether that is the spec's W; whether top-level `unchecked;` is in the zc0 subset.
-- [ ] fix the implementation bugs listed in `docs/SPEC.md` appendix b, once confirmed: `memdebug scrub;` rejected; enum members can't reference earlier members; a plain function redefinition silently shadows the first; a prototype after the definition hides it; `?:` with one int and one float branch gives garbage; `bool` not normalised; float global initializers rejected; a global initializer naming another global uses its address; `include std;` inside a function body; syntax-error recovery hiding later errors; unterminated `/*` and string literals; `x as int * y`; prototypes needing parameter names; `len(char*)` with typecheck off; duplicate `case` labels; local `const` in constant expressions; constant expressions without precedence (`1+2*3` is 9). fix the prose items the appendix lists too.
+- [x] verify both compilers against `docs/SPEC.md`: `tests/conformance/<section>/` (sections 1 to 8 plus `9-appendix-b`, 657 programs), runner `tests/conformance/run.sh` (`--zc0`, `--zym`, `--zy`, `-w`), understood failures in `tests/conformance/known.txt` (class a self-hosted bug, b zc0, c spec issue), wired into the interp section of `tests/test.sh`. zy 626 ok, 31 known; zc0 221 ok, 38 known, 398 skipped (outside appendix a). appendix b.2 items 1 to 16 confirmed except b.2.8, which is documented behavior (5.3.2, 3.14.2; the spec wins). top-level `o3 { }` is rejected (4.10.2, class c, left alone). the seven class a bugs are fixed, one commit each: a bad literal in a function's first statement (capture_scan left panic set), `u*d` with visible `u`/`d`, the bstr flag surviving a cast, the 24-parameter limit, `return e;` in a void function (now dropped, yields 0), `memdebug scrub;`, and `case member():` on a member with fields. the conformance run is in `tests/test.sh` but not in `-q`. open: `rt.memreport()` reports buddy capacity (`rt.new(2)` is 3 words), whether that is the spec's W; whether top-level `unchecked;` is in the zc0 subset.
+- [ ] fix the implementation bugs listed in `docs/SPEC.md` appendix b, once confirmed: enum members can't reference earlier members; a plain function redefinition silently shadows the first; a prototype after the definition hides it; `?:` with one int and one float branch gives garbage; `bool` not normalised; float global initializers rejected; `include std;` inside a function body; syntax-error recovery hiding later errors; unterminated `/*` and string literals; `x as int * y`; prototypes needing parameter names; `len(char*)` with typecheck off; duplicate `case` labels; local `const` in constant expressions; constant expressions without precedence (`1+2*3` is 9). (`memdebug scrub;` is fixed; a global initializer naming another global giving its address is documented behavior, 5.3.2.) fix the prose items the appendix lists too.
- [ ] (partly done, see "agent work at the end of the session") a cross-reference report: for every global of the compiler, the functions that write it and the ones that only read it. build it from the language server's references (`textDocument/references` on `build/bin/zclsp`, so scoping is exact) and classify each reference as a write when an assignment operator, `++` or `--` follows it. output under `build/xref/`; a committed baseline and a `--check` mode that fails when a new writer of a global appears, so it can run in ci.
- [ ] optional, low priority: plain enums and `?:` in zc0 (both are small and produce ordinary code), so parity covers more programs. no structs in zc0 (decided).
- [ ] group the compiler's parallel per-local arrays (`lname`, `ltype`, `lstate`, `lhome`, `lown`, `lslot`, `ltag`, `lpos`, `lnsz`, `lleaf`, ...) without structs: declare each family together in one place, with one comment block stating each array's meaning and invariant (for example when `lhome` is meaningful), route snapshot and restore of the family through one pair of functions, and use the cross-reference report to confirm nothing else writes them. then symbols and types the same way. fixpoint and analyzer behavior unchanged.
@@ -514,7 +521,7 @@ brief someone without re-reading the code.
- [ ] borrow returns tied to an argument. inference already works for a callee defined before the call: `char* skip(char* s) { ...; return s + i; }` makes `let r = skip(t); rt.free(t); r[0]` an error. the gap: when the call comes before the callee's definition, nothing is reported and the program reads freed memory. extend the deferred effect requirements (`effect_require`, `effect_finish` at unit end) so a forward-called function whose return turns out to alias a parameter produces the same error. for bodyless declarations, externs and function types, add an unobtrusive spelling, for example a `borrows s` clause after the parameter list (`char* skip(char* s) borrows s;`), checked against the body when one exists.
- [ ] variadic arguments and `std.print`. design: a pack is passed as one hidden argument, a caller-built read-only growable-array handle (`fixed T[?]` view: [len, cap, items, element size] in the caller's frame, so `len(a)` and `a[i]` work unchanged and nothing is allocated). `...int a` is a typed pack; several packs per function when their element types differ (`void f(...int a, ...char b)`): each extra argument goes to the pack whose element type matches its static type, an ambiguous match is an error, packs come after the fixed parameters. `...any args` packs each value with a kind code (int, float, char, string, bstr, pointer, ...) and its type record: `any` is a small built-in record { kind, value, type }. `...*any args` makes the call comma-free: after the fixed arguments, the parser reads unary expressions until `)`, commas optional, so `std.print("x = " name ", age " age "\n");` works and binary expressions must be parenthesized. `std.print(...*any args)` prints each argument by its kind (`put_n`, the float printer, `put_s`, `put_bs`, a character for `char`, hex for pointers).
- [ ] x86-64 jit phase 2: compile o3 regions to x86-64 (port `rcompile` and the region register allocation; regions now run on the region interpreter and are 2 to 4 times slower than their plain versions on this jit) and inline enhancements (lower the optimized micro-ops to x86 registers as the arm64 path does, instead of calling `xrun`). validate with `tests/drivers/jit_x64_test.sh` and `tests/bench/bench.sh`, which now has an x86-64 jit column. add the driver to `tests/test.sh` on x86-64 linux.
-- [ ] `memdebug`'s `obj_base` searches only 64 words back for a block header, so under `memdebug on` an interior access more than 64 words into a block (`buf[255]` on a 256-word block) reports "bad heap pointer". find the header from the buddy allocator's size classes instead of a bounded scan, with a regression program.
+- [x] `memdebug`'s `obj_base` used to search only 64 words back for a block header, so `buf[255]` on a 256-word block reported "bad heap pointer". it now rounds the pointer down to each size class's alignment (largest first) and takes the first header whose check word, size and class agree and whose payload holds the pointer, so interiors at any depth work, a deep pointer into a freed block reports use after free, and header words and free space still report "bad heap pointer". regression: `tests/programs/memdebug_interior.zc` (in `tests/test.sh` beside the other memdebug checks). note that a stray pointer that lands inside another live block is accepted, since memdebug cannot know which block it came from.
- [ ] (partly done, see "agent work at the end of the session") `defer` follow-ups (small): accept `defer if (...) ...;` and other single statements besides an expression; decide whether deferred code may name an owner declared after the `defer` (now rejected by the lexical rule; one line in `defer_one`).
- [ ] build the llvm core by default when `llvm-config` is available (the makefile falls back to the interpreter otherwise), so x86 hosts get a native tier and the language server stops being interpreted; keep `ZYNOLLVM=1` and `-DZYINTERP` as the opt-outs.
- [ ] stack-effect checking: every function and `asm` block declares or is inferred to have a fixed data-stack effect, and the compiler reports a block whose paths leave the stack unbalanced. the first step of a proving scaffold.
diff --git a/src/compiler/builtins.zc b/src/compiler/builtins.zc
index dd01bd8..9a89052 100644
--- a/src/compiler/builtins.zc
+++ b/src/compiler/builtins.zc
@@ -7,7 +7,7 @@ int kw_memdebug() {
stmtonly();
next();
if (is_id(s_on)) { mdbg = 1; f = 5; }
- else if (is_id(s_scrub)) { mdbg = 1; f = 7; }
+ else if (tk == T_ID && rt.eq_w(tv, "scrub")) { mdbg = 1; f = 7; } // s_scrub is the runtime function rt.scrub, not this word
else if (is_id(s_off)) { mdbg = 0; f = 0; }
else die("memdebug on, memdebug scrub or memdebug off");
next();
diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc
index 36a307d..3a02b90 100644
--- a/src/compiler/decl.zc
+++ b/src/compiler/decl.zc
@@ -328,8 +328,8 @@ int fndef(int chk, int rty, int ret, int n) {
pt[np] = t;
if (np < 12) tags = tags + (t << (np + np));
np++;
- if (np >= 24) die("too many parameters");
if (!accept(s_comma)) break;
+ if (np >= 24) { die("too many parameters"); break; } // a 25th would not fit pn, pt, pty
}
}
expect(s_rp);
diff --git a/src/compiler/expr.zc b/src/compiler/expr.zc
index 1536744..4c77ceb 100644
--- a/src/compiler/expr.zc
+++ b/src/compiler/expr.zc
@@ -538,7 +538,7 @@ int enhcall(int s) {
// scan with the compiler lexer, restoring its source and lookahead. only functions containing a closure
// need cells; their scalars get cells from declaration time, so loop back edges and earlier references agree.
int capture_scan() {
- word old, f; int depth = 1, found = 0, docs = docs_on, record = grec;
+ word old, f; int depth = 1, found = 0, docs = docs_on, record = grec, sp = panic;
if (isp < 0 || R_SRC[isp * 32] == 0 || !is_op(s_lb)) return 0;
old = R_SRC + isp * 32;
src_push(1, capture_dummy, 0, 0); old = R_SRC + (isp - 1) * 32; f = R_SRC + isp * 32;
@@ -550,7 +550,7 @@ int capture_scan() {
else if (is_op(s_rb)) depth--;
else if (is_id(s_fn)) { peek(); if (pk_tk == T_OP && (pk_tv == s_lp || pk_tv == s_colon)) found = 1; }
}
- dry--; src_pop(); docs_on = docs; grec = record; return found;
+ dry--; src_pop(); docs_on = docs; grec = record; panic = sp; return found; // a lexer error in the dry scan must not leave panic set for the real parse
}
int capture_cell(int i, int copy) {
if (!capture_enabled || o3on || (ltag[i] & 16) || larr[i] || local_cell[i]) return 0;
@@ -1566,7 +1566,7 @@ int postfix_tail(int c) {
// --, and any unary operator a program added (- ! ~ built in).
int unary() {
int c, un, t, place, constant, value; word storage, dependency;
- if (tk == T_ID && (tv == s_u || tv == s_m || tv == s_d)) { // u*x, d*x, m*x: recast only when * is glued
+ if (tk == T_ID && (tv == s_u || tv == s_m || tv == s_d) && findloc(tv) < 0) { // u*x, d*x, m*x: recast only when * is glued and the tag is not a local's name
peek();
if (pk_tk == T_OP && pk_tv == s_star && pq_gl[0]) {
t = S_M; if (tv == s_u) t = S_U; if (tv == s_d) t = S_D;
@@ -1590,6 +1590,7 @@ int unary() {
else if (xfloat && isnum(t)) { op(OP_F2I); xfloat = 0; }
else xfloat = 0;
if (tbytes(t) && !samesz(t, xty)) { tnorm(t); xconst = 0; } // (u8)x cuts the value to size
+ xstr = t == tbstr; // (int*)s is a word pointer, not a byte string
if_value = 0;
xty = t; lv = 0; lvk = LV_NONE; xzero = 0; xbor = -1;
return C_PLAIN;
diff --git a/src/compiler/stmt.zc b/src/compiler/stmt.zc
index 505e466..a67b931 100644
--- a/src/compiler/stmt.zc
+++ b/src/compiler/stmt.zc
@@ -573,7 +573,7 @@ int kw_switch() {
if (!accept(s_comma)) break;
}
expect(s_rp);
- if (nb != pm_nf[gi]) serr("a case binds every field of its member");
+ if (nb && nb != pm_nf[gi]) serr("a case binds every field of its member"); // member() with no names matches by tag (spec 4.7.3)
v = pm_tag[gi];
if (v < 63) cov = cov | (1 << v);
laddr(lslot[tmp]); op(O_LD); op(O_LD); lit(v); op(O_SUB);
@@ -651,7 +651,7 @@ int kw_return() {
}
if_ret(xty, xzero);
if (fty && fty != tvoid) fcoerce(fty);
- if (fty == tvoid) tcerr("return value", tvoid, xty);
+ if (fty == tvoid) { tcerr("return value", tvoid, xty); op(O_DROP); lit(0); } // typecheck off: the value is ignored (spec 4.8.3)
else if (fty && !compat(fty, xty)) tcerr("return value", fty, xty);
}
if (nclo) {
diff --git a/src/runtime/rt.zc b/src/runtime/rt.zc
index 8c26161..7089e26 100644
--- a/src/runtime/rt.zc
+++ b/src/runtime/rt.zc
@@ -1346,7 +1346,7 @@ int scrub() {
// dbg bits: 1 header checks and allocation sites, 2 poison the payload on free
// (the word 57005, which is 0xDEAD), 4 interior pointers keep a quarantined
-// block (the mark walks back at most 64 words to a real header). off unless
+// block (obj_base finds its header from the size classes). off unless
// a program requests. sites are p ^ MAGIC so the collector does not treat them as pointers.
const int NSITE = 64;
@@ -1355,17 +1355,26 @@ int site_drop;
word[64] site_p; word[64] site_line;
// true when p is a freed block still held in quarantine.
word quar(word p) { return p >= HEAP_LO + HDR && p < hbump && p[-3] == (p ^ MAGIC) && !(p[-4] & 1); }
-// the object whose payload contains w, or 0. exact pointers and interiors.
+// the object whose payload contains w, or 0. exact pointers and interiors, at any
+// depth. every block of class k starts at HEAP_LO plus a multiple of 2^k words, so
+// the block holding w, if any, starts at w rounded down to one of those. the
+// candidates are tried from the largest class down: a header counts only when its
+// check word, size and class agree with k, and the first that holds w in its
+// payload is the block. a merge clears the upper half's header and a split rewrites
+// the header at the same address, so a stale header can only sit inside the
+// payload of the real block, which is found first.
word obj_base(int w) {
- int i = 0; word p; int n;
+ int k = 31, off, sz; word b, p;
if (w < HEAP_LO + HDR || w >= hbump) return 0;
- while (i <= 64) {
- p = w - i;
- if (p >= HEAP_LO + HDR && p < hbump && p[-3] == (p ^ MAGIC)) {
- n = p[-5] - HDR;
- if (w >= p && w < p + n) return p;
+ off = w - HEAP_LO;
+ while (k >= 3) {
+ sz = pow2[k];
+ b = HEAP_LO + off - off % sz;
+ p = b + HDR;
+ if (sz <= hbump - HEAP_LO && p < hbump && b[0] == sz && p[-3] == (p ^ MAGIC) && bclass(b) == k) {
+ if (w >= p && w < b + sz) return p;
}
- i++;
+ k--;
}
return 0;
}
diff --git a/tests/conformance/4-statements/memdebug_scrub_fills.zc b/tests/conformance/4-statements/memdebug_scrub_fills.zc
index ad15e3d..eed4c2d 100644
--- a/tests/conformance/4-statements/memdebug_scrub_fills.zc
+++ b/tests/conformance/4-statements/memdebug_scrub_fills.zc
@@ -1,10 +1,10 @@
// expect: 57005/
-// spec 4.11: memdebug scrub; fills freed payloads with 57005
+// spec 4.11: memdebug scrub; fills freed payloads with 57005 (read through asm, which the use-after-free check does not see)
include rt;
void pn(int x) { rt.put_n(x); rt.put_c(10); }
memdebug scrub;
fn main() {
- unchecked { int* p = rt.new(int); *p = 5; rt.free(p); pn(*p); }
+ unchecked { int* p = rt.new(int); *p = 5; word a = p; rt.free(p); pn(asm { a @ @ }); }
return 0;
}
// zc0: skip
diff --git a/tests/conformance/4-statements/switch_tagged_empty_parens_with_fields.zc b/tests/conformance/4-statements/switch_tagged_empty_parens_with_fields.zc
new file mode 100644
index 0000000..789c650
--- /dev/null
+++ b/tests/conformance/4-statements/switch_tagged_empty_parens_with_fields.zc
@@ -0,0 +1,15 @@
+// expect: rect/circle/
+// spec 4.7.3: case member(): with no names matches by tag even when the member has fields
+include rt;
+enum shape { circle { int r; }, rect { int w; int h; }, dot };
+void t(shape* s) {
+ switch (s) { case circle(): rt.put_s("circle"); case rect(): rt.put_s("rect"); case dot: rt.put_s("dot"); }
+ rt.put_c(10);
+}
+fn main() {
+ shape* a = shape.rect(4, 5); shape* b = shape.circle(2);
+ t(a); t(b);
+ rt.free(a); rt.free(b);
+ return 0;
+}
+// zc0: skip
diff --git a/tests/conformance/5-declarations/global_names_global_b8.zc b/tests/conformance/5-declarations/global_names_global_b8.zc
index 7d678eb..6e11713 100644
--- a/tests/conformance/5-declarations/global_names_global_b8.zc
+++ b/tests/conformance/5-declarations/global_names_global_b8.zc
@@ -1,5 +1,5 @@
// expect: 1/
-// spec 5.3.2 and 3.14.2: a global named in an initializer is its address (appendix b.2.8 calls this a bug)
+// spec 5.3.2 and 3.14.2: a global named in an initializer is its address (documented behavior: the spec wins over appendix b.2.8)
include rt;
void pn(int x) { rt.put_n(x); rt.put_c(10); }
int a = 5; int b = a + 1;
diff --git a/tests/conformance/known.txt b/tests/conformance/known.txt
index c26b50e..13ef43c 100644
--- a/tests/conformance/known.txt
+++ b/tests/conformance/known.txt
@@ -1,22 +1,7 @@
# understood conformance failures: PATH IMPL CLASS reason
# class a: self-hosted compiler bug; b: zc0 deviation (appendix a/b); c: spec issue to settle
-1-source/ident_tags_as_variables_glued_star.zc zy a u*d with visible variables u and d is taken as a recast, not a multiplication (spec 1.6.3)
-1-source/lit_bstr_cast_to_int_pointer.zc zy a the bstr flag leaks into an int* local, so p[0] reads a byte
-1-source/lit_char_non_ascii.zc zy a bad character literal in the first statement of a body is swallowed by lookahead
-1-source/lit_char_two_bytes.zc zy a bad character literal in the first statement of a body is swallowed by lookahead
-1-source/lit_char_two_bytes_local_init.zc zy a bad character literal in the first statement of a body is swallowed by lookahead
-1-source/lit_float_bad_exponent.zc zy a bad float literal in the first statement of a body is swallowed by lookahead
-1-source/lit_float_bad_exponent_first_stmt_local.zc zy a bad float literal in the first statement of a body is swallowed by lookahead
-1-source/lit_float_bad_exponent_first_stmt_paren.zc zy a bad float literal in the first statement of a body is swallowed by lookahead
-1-source/lit_float_bad_exponent_in_helper.zc zy a bad float literal in the first statement of a body is swallowed by lookahead
-1-source/lit_float_bad_exponent_sign.zc zy a bad float literal in the first statement of a body is swallowed by lookahead
-2-types/typecheck_off_void_return_value_ok.zc zy a void f() { return 3; } yields 3, not 0 (spec 4.8.3)
-3-expressions/call_24_parameters.zc zy a the parameter limit is 23, not 24 (decl.zc; closures 19)
-3-expressions/call_24_parameters_prototype.zc zy a the parameter limit is 23, not 24 (decl.zc; closures 19)
-4-statements/memdebug_scrub_accepted.zc zy a memdebug scrub; is rejected (appendix b.1.1, b.2.1)
-4-statements/memdebug_scrub_fills.zc zy a memdebug scrub; is rejected (appendix b.1.1, b.2.1)
+# appendix b.2.8 (a global named in an initializer is its address) is documented behavior per 5.3.2 and 3.14.2: no entry
4-statements/o3_outside_function_ordinary_block.zc zy c top-level o3 { } is rejected; spec 4.10.2 says it is an ordinary block
-4-statements/switch_tagged_match_by_tag.zc zy a case member(): on a member with fields is rejected (a case binds every field); spec 4.7.3 says it matches by tag
5-declarations/enum_member_names_earlier_b2.zc zy a appendix b.2.2 confirmed: enum members can't name earlier members
5-declarations/fn_prototype_after_definition_b4.zc zy a appendix b.2.4 confirmed: a prototype after the definition hides it
5-declarations/fn_prototype_unnamed_params_b13.zc zy a appendix b.2.13 confirmed (message now: doesn't match its earlier declaration)
@@ -31,7 +16,7 @@
9-appendix-b/b2_14_len_char_ptr_typecheck_off.zc zy a appendix b.2.14 confirmed
9-appendix-b/b2_15_duplicate_case.zc zy c appendix b.2.15 confirmed; 4.7.2 says duplicates are allowed
9-appendix-b/b2_16_local_const_in_const_expr.zc zy c appendix b.2.16 confirmed; 3.14.2 documents it
-1-source/ident_tags_as_variables_glued_star.zc zc0 b recast u*d emits no code (same misparse as zy)
+1-source/ident_tags_as_variables_glued_star.zc zc0 b u*d with visible u and d is read as a recast (no code); zy follows 1.6.3
1-source/lit_hex.zc zc0 b literals read with strtoll base 0: octal and saturation (appendix b.1.11)
1-source/lit_hex_wide.zc zc0 b literals read with strtoll base 0: octal and saturation (appendix b.1.11)
1-source/lit_hex_wraps.zc zc0 b literals read with strtoll base 0: octal and saturation (appendix b.1.11)
diff --git a/tests/langerrors/bad_char_first_statement.zc b/tests/langerrors/bad_char_first_statement.zc
new file mode 100644
index 0000000..ca2ccfa
--- /dev/null
+++ b/tests/langerrors/bad_char_first_statement.zc
@@ -0,0 +1,3 @@
+// expect: bad character literal
+include rt;
+fn main() { int a = 'ab'; rt.put_n(a); return 0; }
diff --git a/tests/langerrors/bad_float_first_statement.zc b/tests/langerrors/bad_float_first_statement.zc
new file mode 100644
index 0000000..2dcca38
--- /dev/null
+++ b/tests/langerrors/bad_float_first_statement.zc
@@ -0,0 +1,3 @@
+// expect: bad float
+include rt;
+fn main() { rt.put_n(1e); return 0; }
diff --git a/tests/langerrors/params_25.zc b/tests/langerrors/params_25.zc
new file mode 100644
index 0000000..738cbcc
--- /dev/null
+++ b/tests/langerrors/params_25.zc
@@ -0,0 +1,3 @@
+// expect: too many parameters
+fn f(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25) { return 0; }
+fn main() { return 0; }
diff --git a/tests/programs/memdebug_interior.expected b/tests/programs/memdebug_interior.expected
new file mode 100644
index 0000000..580692a
--- /dev/null
+++ b/tests/programs/memdebug_interior.expected
@@ -0,0 +1 @@
+7 3 2
diff --git a/tests/programs/memdebug_interior.zc b/tests/programs/memdebug_interior.zc
new file mode 100644
index 0000000..8464f2c
--- /dev/null
+++ b/tests/programs/memdebug_interior.zc
@@ -0,0 +1,18 @@
+namespace;
+include rt;
+
+// memdebug finds the block of an interior pointer however deep it is: buf[255]
+// of a 256-word block is 255 words past its header, beyond the old 64-word scan.
+memdebug on;
+fn main() {
+ let buf = rt.new(256), small = rt.new(4);
+ buf[255] = 7;
+ buf[100] = 3;
+ small[3] = 2;
+ rt.put_n(buf[255]); rt.put_c(32);
+ rt.put_n(buf[100]); rt.put_c(32);
+ rt.put_n(small[3]); rt.put_c(10);
+ rt.free(small);
+ rt.free(buf);
+ return 0;
+}
diff --git a/tests/test.sh b/tests/test.sh
index 253f668..7a80c3c 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -188,6 +188,7 @@ ABC" "$l o3 lifts byte loads and stores"
chk "$($vm < tests/programs/strings.zc)" "hi! 3 3 104 i! 1 1 1 -42 42 -42 11 1 -9223372036854775808 0 1 3 10 1 1 1 W ab" "$l byte strings"
chk "$($vm < tests/programs/share.zc)" "1 2 1 4 1 1 1 1 42 1 1 1 6 0" "$l shared cons cells"
chk "$($vm < tests/programs/share_dbg.zc)" "7 1" "$l memdebug accepts a shared cell"
+ chk "$($vm < tests/programs/memdebug_interior.zc | md5sum)" "$(md5sum < tests/programs/memdebug_interior.expected)" "$l memdebug finds the block of a deep interior pointer"
chk "$($vm < tests/programs/global_transfer.zc)" "5 9" "$l an m or u owner stored in a global is transferred"
chk "$($vm < tests/programs/closure_result.zc)" "8 1.500000" "$l a closure with a declared result type"
chk "$($vm < examples/strings.zc)" "hello, zc
@@ -238,10 +239,13 @@ s_interp() {
T=$ROOT/interp
$CC -pthread -O2 -w -fwrapv -DJTYPE=int src/core/core.c src/core/io_linux.c $GI -o $T/zy
run $T/zy "interp"
- # docs/SPEC.md conformance programs; failures listed in tests/conformance/known.txt do not count
- bash tests/conformance/run.sh --zy $T/zy -w $T/conformance -q > $T/conformance.out 2>&1; r=$?
- grep '^FAIL' $T/conformance.out
- chk $r 0 "interp conformance suite: $(grep '^ total' $T/conformance.out | sed 's/ */ /g')"
+ # docs/SPEC.md conformance programs; failures listed in tests/conformance/known.txt do not count.
+ # not in the quick path (-q, make check): it takes about half a minute
+ if [ $QUICK = 0 ]; then
+ bash tests/conformance/run.sh --zy $T/zy -w $T/conformance -q > $T/conformance.out 2>&1; r=$?
+ grep '^FAIL' $T/conformance.out
+ chk $r 0 "interp conformance suite: $(grep '^ total' $T/conformance.out | sed 's/ */ /g')"
+ fi
python3 tests/drivers/array_methods_test.py $T/zy > $T/arrays.out 2>&1; r=$?
grep -v '^ok' $T/arrays.out
chk $r 0 "native array receiver, algorithm and ownership contracts"