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