-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvm_dispatch_compare.c
More file actions
242 lines (205 loc) · 6.14 KB
/
Copy pathvm_dispatch_compare.c
File metadata and controls
242 lines (205 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#if !defined(SWITCH) && !defined(THREADING)
#error "Compile com -DSWITCH ou -DTHREADING."
#endif
#if defined(SWITCH) && defined(THREADING)
#error "Escolha apenas uma estrategia por compilacao."
#endif
#if defined(THREADING) && !defined(__GNUC__)
#error "THREADING depende de computed goto (GCC/Clang GNU mode)."
#endif
#ifndef ITERATIONS
#define ITERATIONS 20000000u
#endif
enum {
OP_PUSH = 0,
OP_ADD = 1,
OP_SUB = 2,
OP_LOAD = 3,
OP_STORE = 4,
OP_HALT = 5
};
typedef struct {
uint64_t bytecodes;
uint64_t dispatch_branches_modeled;
uint64_t checksum;
int32_t final_mem0;
int32_t final_top;
} Metrics;
static __attribute__((noinline, noclone))
void build_program(int32_t *program) {
program[0] = OP_PUSH; program[1] = 10;
program[2] = OP_PUSH; program[3] = 20;
program[4] = OP_ADD;
program[5] = OP_PUSH; program[6] = 5;
program[7] = OP_SUB;
program[8] = OP_STORE; program[9] = 0;
program[10] = OP_LOAD; program[11] = 0;
program[12] = OP_HALT;
}
static inline uint64_t modeled_switch_dispatch_cost(int32_t opcode) {
switch (opcode) {
case OP_PUSH: return 1;
case OP_ADD: return 2;
case OP_SUB: return 3;
case OP_LOAD: return 4;
case OP_STORE: return 5;
case OP_HALT: return 6;
default: return 0;
}
}
#if defined(SWITCH)
static __attribute__((noinline, noclone))
Metrics run_switch(const int32_t *program, uint32_t iterations) {
Metrics m = {0};
for (uint32_t iter = 0; iter < iterations; ++iter) {
const int32_t *ip = program;
int32_t stack[8];
int sp = 0;
int32_t mem0 = 0;
for (;;) {
int32_t opcode = *ip++;
m.bytecodes++;
m.dispatch_branches_modeled += modeled_switch_dispatch_cost(opcode);
switch (opcode) {
case OP_PUSH:
stack[sp++] = *ip++;
break;
case OP_ADD: {
int32_t rhs = stack[--sp];
int32_t lhs = stack[--sp];
stack[sp++] = lhs + rhs;
break;
}
case OP_SUB: {
int32_t rhs = stack[--sp];
int32_t lhs = stack[--sp];
stack[sp++] = lhs - rhs;
break;
}
case OP_LOAD: {
int32_t addr = *ip++;
if (addr != 0) {
fprintf(stderr, "Endereco invalido: %d\n", addr);
exit(1);
}
stack[sp++] = mem0;
break;
}
case OP_STORE: {
int32_t addr = *ip++;
if (addr != 0) {
fprintf(stderr, "Endereco invalido: %d\n", addr);
exit(1);
}
mem0 = stack[--sp];
break;
}
case OP_HALT:
m.final_mem0 = mem0;
m.final_top = stack[sp - 1];
m.checksum += (uint64_t)(uint32_t)(mem0 + stack[sp - 1]);
goto end_iteration;
default:
fprintf(stderr, "Opcode invalido: %d\n", opcode);
exit(1);
}
}
end_iteration:
;
}
return m;
}
#endif
#if defined(THREADING)
static __attribute__((noinline, noclone))
Metrics run_threading(const int32_t *program, uint32_t iterations) {
Metrics m = {0};
for (uint32_t iter = 0; iter < iterations; ++iter) {
const int32_t *ip = program;
int32_t stack[8];
int sp = 0;
int32_t mem0 = 0;
int32_t opcode;
static void *dispatch_table[] = {
&&do_push, &&do_add, &&do_sub, &&do_load, &&do_store, &&do_halt
};
#define DISPATCH() \
do { \
opcode = *ip++; \
m.bytecodes++; \
m.dispatch_branches_modeled++; \
goto *dispatch_table[opcode]; \
} while (0)
DISPATCH();
do_push:
stack[sp++] = *ip++;
DISPATCH();
do_add: {
int32_t rhs = stack[--sp];
int32_t lhs = stack[--sp];
stack[sp++] = lhs + rhs;
DISPATCH();
}
do_sub: {
int32_t rhs = stack[--sp];
int32_t lhs = stack[--sp];
stack[sp++] = lhs - rhs;
DISPATCH();
}
do_load: {
int32_t addr = *ip++;
if (addr != 0) {
fprintf(stderr, "Endereco invalido: %d\n", addr);
exit(1);
}
stack[sp++] = mem0;
DISPATCH();
}
do_store: {
int32_t addr = *ip++;
if (addr != 0) {
fprintf(stderr, "Endereco invalido: %d\n", addr);
exit(1);
}
mem0 = stack[--sp];
DISPATCH();
}
do_halt:
m.final_mem0 = mem0;
m.final_top = stack[sp - 1];
m.checksum += (uint64_t)(uint32_t)(mem0 + stack[sp - 1]);
#undef DISPATCH
}
return m;
}
#endif
static void print_report(const Metrics *m) {
puts("============================================================");
#if defined(SWITCH)
puts("C benchmark - switch dispatch");
#else
puts("C benchmark - direct threading");
#endif
puts("============================================================");
printf("ITERATIONS: %" PRIu32 "\n", (uint32_t)ITERATIONS);
printf("Bytecodes executados: %" PRIu64 "\n", m->bytecodes);
printf("Branches de dispatch modelados: %" PRIu64 "\n", m->dispatch_branches_modeled);
printf("Resultado final mem[0]: %" PRId32 "\n", m->final_mem0);
printf("Topo final da pilha: %" PRId32 "\n", m->final_top);
printf("Checksum acumulado: %" PRIu64 "\n", m->checksum);
}
int main(void) {
int32_t program[13];
build_program(program);
#if defined(SWITCH)
Metrics m = run_switch(program, ITERATIONS);
#else
Metrics m = run_threading(program, ITERATIONS);
#endif
print_report(&m);
return 0;
}