raz / test / genbench.sh

69 lines · 1.8 KiB · raw · history · last change f96277b9

1#!/bin/sh
2# Writes a large raz program to standard output, for timing the
3# compiler: n procedures of loops, conditions, records, slices and
4# calls, each calling the one before, so that none is left out.
5# sh test/genbench.sh n > build/Bench.raz
6n=$1
7cat <<'END'
8module Bench;
9import Out;
10
11type
12 Pair = record x, y: INT; end;
13 Shape = variant circle(r: INT); rect(w, h: INT); dot; end;
14
15var
16 g: [16]INT;
17 data: [64]INT;
18
19proc p0(a, b: INT; _s: []INT): INT;
20begin
21 return a + b;
22end;
23
24END
25# awk, not a loop of the shell, so that 12,000 procedures take a moment
26awk -v n="$n" 'BEGIN {
27 for (i = 1; i <= n; i++) {
28 printf "(* procedure %d *)\n", i
29 printf "proc p%d(a, b: INT; s: []INT): INT;\n", i
30 print "var i, t: INT; r: Pair; sh: Shape;"
31 print "begin"
32 print " t := 0;"
33 print " for i := 0 to LEN(s) - 1 do"
34 print " if s[i] > a then"
35 print " t := t + s[i] * b;"
36 print " elsif s[i] = b then"
37 print " t := t - 1;"
38 print " else"
39 print " t := t xor i;"
40 print " end;"
41 print " end;"
42 print " r.x := t;"
43 printf " r.y := a + b * %d;\n", i
44 print " while r.x > 100 do"
45 print " r.x := r.x div 2;"
46 print " end;"
47 print " sh := rect(r.x, r.y);"
48 print " match sh of"
49 print " | circle(c): t := t + c;"
50 print " | rect(w, h): t := t + w * h;"
51 print " | dot: t := t - 1;"
52 print " end;"
53 print " g[a & 15] := r.x + r.y;"
54 printf " return p%d(t & 1023, b, s[0..LEN(s) div 2]) + r.x;\n", i - 1
55 print "end;"
56 print ""
57 }
58}'
59cat <<END
60proc main*;
61var i: INT;
62begin
63 for i := 0 to 63 do data[i] := i; end;
64 Out.int(p$n(1, 2, data[0..4]));
65 Out.ln;
66end;
67
68end Bench.
69END