aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/programs/o3_freelist.zc84
-rwxr-xr-xtests/test.sh6
2 files changed, 89 insertions, 1 deletions
diff --git a/tests/programs/o3_freelist.zc b/tests/programs/o3_freelist.zc
new file mode 100644
index 0000000..7836987
--- /dev/null
+++ b/tests/programs/o3_freelist.zc
@@ -0,0 +1,84 @@
+namespace;
+include rt;
+
+// allocation inside a compiled o3 region. on the x86-64 jit a constant-size
+// new(T) pops its size-class list inline and a leaf's free pushes it back
+// inline; anything the inline code can't serve (an empty list, a mismatch)
+// takes the ordinary rt.new_k / rt.recycle_leaf call. every line must print the
+// same with ZYNOFASTALLOC=1, under ZYO3INTERP=1 and on the other backends.
+struct point { int x, y; }; // 2 words: class 3
+struct wide { int a, b, c, d, e, f, g, h, i, j; }; // 10 words: class 4, eleven zeroed words
+struct cell { cell* next; int v; }; // has a pointer field: not a leaf
+
+// a block freed at the end of a pass comes back on the next: the sums show the
+// payload was zeroed every time
+o3 int churn(int n) {
+ int total = 0, i = 0;
+ while (i < n) {
+ point* p = rt.new(point);
+ total = total + p.x + p.y;
+ p.x = i; p.y = 7;
+ i++;
+ }
+ return total;
+}
+o3 int churn_wide(int n) {
+ int total = 0, i = 0;
+ while (i < n) {
+ wide* w = rt.new(wide);
+ total = total + w.a + w.e + w.j;
+ w.a = 1; w.e = 2; w.j = 3;
+ i++;
+ }
+ return total;
+}
+// the second new gets the block the first one freed
+o3 int addr(point* p) { int a = 0; unchecked { a = p; } return a; }
+o3 int reuse() {
+ int a = 0, b = 0, z = 0;
+ { point* p = rt.new(point); a = addr(p); p.x = 41; p.y = 42; }
+ { point* q = rt.new(point); b = addr(q); z = q.x + q.y; }
+ if (a == b && z == 0) return 1;
+ return 0;
+}
+// more blocks than the class list holds: the list runs empty and new falls back
+// to the general allocator, then the whole chain goes back
+o3 cell*.u build(int n) {
+ cell* c = rt.new(cell);
+ c.v = n;
+ if (n > 0) c.next = build(n - 1);
+ return u*c;
+}
+o3 int sum(cell* c) {
+ if (!c.next) return c.v;
+ return c.v + sum(c.next);
+}
+o3 int chain(int n) {
+ cell* head = build(n);
+ int s = sum(head);
+ rt.free(d*head);
+ return s;
+}
+// a header the free can't vouch for: n words of untyped new have no type stamp,
+// so this free is the general recycle, and a typed new after it still works
+o3 int mixed(int n) {
+ int i = 0, s = 0;
+ while (i < n) {
+ point* p = rt.new(point);
+ int* w = rt.new(2);
+ p.x = i; w[0] = i; w[1] = 1;
+ s = s + p.x - w[0] + w[1];
+ i++;
+ }
+ return s;
+}
+
+int main() {
+ rt.put_n(churn(1000)); rt.put_c(' ');
+ rt.put_n(churn_wide(1000)); rt.put_c(' ');
+ rt.put_n(reuse()); rt.put_c(' ');
+ rt.put_n(chain(5000)); rt.put_c(' ');
+ rt.put_n(mixed(500)); rt.put_c(' ');
+ rt.put_n(churn(1000) + reuse()); rt.put_c(10);
+ return 0;
+}
diff --git a/tests/test.sh b/tests/test.sh
index 6992c0d..8bf7a9b 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -103,6 +103,8 @@ run(){ # $1 = vm command, $2 = label
chk "$($vm < examples/typed.zc | tr '\n' '/')" "last: 4/point is 2 words: x int, y int/vec<point*> is 3 words: len int, cap int, items point**/push is bool(vec<T>*, T.d)/the heap says v is a vec<point*>, and v.items[0] is a point/" "$l typed example"
chk "$($vm < examples/wiring.zc)" "16 1234 25 4/9" "$l enhancement example"
chk "$($vm < tests/programs/allocfast.zc)" "0 0 499500 0" "$l constant-size allocation fast path"
+ chk "$($vm < tests/programs/o3_freelist.zc)" "0 0 1 12502500 500 1" "$l o3 new and free: class-list reuse, zeroing, an empty list, untyped neighbours"
+ chk "$(sed 's/^include rt;$/include rt;\nmemdebug on;/' tests/programs/o3_freelist.zc | $vm)" "0 0 1 12502500 500 1" "$l o3 new and free under memdebug take the general path"
chk "$($vm < tests/programs/basics.zc | tr '\n' '/')" "285 610 42 512/hey 65 -3 -2 1024 -8/12456/757/9 100-101/5/ok/" "$l language basics"
chk "$($vm < tests/programs/umd.zc)" "100 6 0 15 99 5 5" "$l u m d lifetimes"
chk "$($vm < tests/programs/names.zc)" "12 1 8" "$l u, m and d are ordinary names"
@@ -523,7 +525,9 @@ s_jit() {
chk $r 0 "x86-64 jit agrees with the interpreter: $(tail -n 1 $T/jit_x64.out)"
chk "$(ZYNOENH=1 $T/zyx < tests/programs/enh_ops.zc | md5sum)" "$(md5sum < tests/programs/enh_ops.expected)" "x86-64 jit with enhancements called through xrun (ZYNOENH=1)"
chk "$(ZYTRACE=1 $T/zyx < tests/programs/o3.zc 2>&1 | grep -c 'bytes of x86-64')" "7" "x86-64 jit compiles the o3 regions it can (one has too many live values)"
- for p in o3 o3copy o3licm o3push o3bytes fo3 o3call o3deep; do
+ chk "$(ZYTRACE=1 $T/zyx < tests/programs/o3_freelist.zc 2>&1 | grep -c ' inline at ')" "11" "x86-64 jit inlines new_k and recycle_leaf in compiled o3 regions"
+ chk "$(ZYNOFASTALLOC=1 $T/zyx < tests/programs/o3_freelist.zc 2>&1)" "$($T/zyx < tests/programs/o3_freelist.zc 2>&1)" "x86-64 o3 inline allocation matches the plain calls (ZYNOFASTALLOC=1)"
+ for p in o3 o3copy o3licm o3push o3bytes fo3 o3call o3deep o3_freelist; do
chk "$($T/zyx < tests/programs/$p.zc 2>&1)" "$(ZYO3INTERP=1 $T/zyx < tests/programs/$p.zc 2>&1)" "x86-64 o3 code matches the region interpreter (ZYO3INTERP=1): $p"
done
fi