aboutsummaryrefslogtreecommitdiff
path: root/tests/bench/selfmod.zc
blob: 7e16fdbad7e00596a77cde37b7c367c1933c14f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// self-modifying code in a loop: each pass rewrites a literal inside g, so the
// translation of g is dropped and made again, while a pile of other code stays
// translated. prints the sum so the engines can be compared.
int litw(int v) { return v + v + 1; }
int g() { return 1000; }
int work(int n) { int i = 0, s = 0; while (i < n) { s = s + i % 7; i++; } return s; }
int a1() { return work(50); } int a2() { return work(60); } int a3() { return work(70); } int a4() { return a1() + a2(); }
int main() {
	word p = g; int i = 0, tot = 0;
	while (*p != litw(1000)) p++;
	a4(); a3();
	while (i < 20000) {
		*p = litw(i);
		tot = tot + g() + a3();
		i++;
	}
	put_n(tot); put_c(10);
	return 0;
}