1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
|
4 |
typedef int int32_t; |
5 |
|
6 |
struct cpu; |
7 |
|
8 |
struct instr_call { |
9 |
void (*f)(struct cpu *cpu, struct instr_call *ic); |
10 |
/* int instr_len; */ |
11 |
void *arg[3]; |
12 |
}; |
13 |
|
14 |
struct cpu { |
15 |
void *curpage; |
16 |
int nloops; |
17 |
|
18 |
struct instr_call *next_instr_call; |
19 |
}; |
20 |
|
21 |
void r(struct cpu *cpu) |
22 |
{ |
23 |
struct instr_call *ic; |
24 |
|
25 |
for (;;) { |
26 |
ic = cpu->next_instr_call++; |
27 |
ic->f(cpu, ic); |
28 |
|
29 |
ic = cpu->next_instr_call++; |
30 |
ic->f(cpu, ic); |
31 |
|
32 |
ic = cpu->next_instr_call++; |
33 |
ic->f(cpu, ic); |
34 |
|
35 |
ic = cpu->next_instr_call++; |
36 |
ic->f(cpu, ic); |
37 |
|
38 |
ic = cpu->next_instr_call++; |
39 |
ic->f(cpu, ic); |
40 |
|
41 |
ic = cpu->next_instr_call++; |
42 |
ic->f(cpu, ic); |
43 |
|
44 |
ic = cpu->next_instr_call++; |
45 |
ic->f(cpu, ic); |
46 |
|
47 |
ic = cpu->next_instr_call++; |
48 |
ic->f(cpu, ic); |
49 |
} |
50 |
} |
51 |
|
52 |
|
53 |
void f_add(struct cpu *cpu, struct instr_call *ic) |
54 |
{ |
55 |
int32_t *a = (int32_t *) ic->arg[0]; |
56 |
int32_t *b = (int32_t *) ic->arg[1]; |
57 |
int32_t *c = (int32_t *) ic->arg[2]; |
58 |
|
59 |
*a = (*b) + (*c); |
60 |
} |
61 |
|
62 |
|
63 |
void f_end(struct cpu *cpu, struct instr_call *ic) |
64 |
{ |
65 |
cpu->nloops--; |
66 |
if (cpu->nloops > 0) { |
67 |
cpu->next_instr_call = cpu->curpage; |
68 |
return; |
69 |
} |
70 |
/* printf(" %i", cpu->nloops); fflush(stdout); */ |
71 |
printf("Exiting correctly\n"); |
72 |
exit(1); |
73 |
} |
74 |
|
75 |
int main(int argc, char *argv[]) |
76 |
{ |
77 |
int32_t tmp_a, tmp_b, tmp_c; |
78 |
struct instr_call *call_array; |
79 |
int i, ncalls; |
80 |
struct cpu *cpu = malloc(sizeof(struct cpu)); |
81 |
|
82 |
if (argc <= 1) { |
83 |
fprintf(stderr, "usage: %s n\n", argv[0]); |
84 |
exit(1); |
85 |
} |
86 |
|
87 |
cpu->nloops = atoi(argv[1]); |
88 |
ncalls = 1024 + 1; |
89 |
|
90 |
/* Fill a range of nonsense calls: */ |
91 |
call_array = malloc(sizeof(struct instr_call) * ncalls); |
92 |
cpu->curpage = call_array; |
93 |
|
94 |
printf("ncalls = %i\n", ncalls); |
95 |
for (i=0; i<ncalls; i++) { |
96 |
if (i == ncalls-1) { |
97 |
call_array[i].f = f_end; |
98 |
} else { |
99 |
call_array[i].f = f_add; |
100 |
call_array[i].arg[0] = &tmp_a; |
101 |
call_array[i].arg[1] = &tmp_b; |
102 |
call_array[i].arg[2] = &tmp_c; |
103 |
} |
104 |
} |
105 |
|
106 |
printf("running...\n"); |
107 |
cpu->next_instr_call = &call_array[0]; |
108 |
r(cpu); |
109 |
|
110 |
printf("ERROR!\n"); |
111 |
return 0; |
112 |
} |
113 |
|