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
|
#include <stdio.h>
#include <time.h>
#include "../include/ejit/ejit.h"
struct ejit_func *compile(bool try_jit, bool im_scawed)
{
struct ejit_operand args[1] = {
EJIT_OPERAND_GPR(0, EJIT_INT32) /* loc 0 contains n */
};
struct ejit_func *f = ejit_create_func(EJIT_INT32, 1, args);
struct ejit_reloc recurse = ejit_bgti(f, EJIT_GPR(0), 2); /* n <= 2 */
ejit_reti(f, 1);
struct ejit_label label = ejit_label(f);
ejit_patch(f, recurse, label);
/* fib(n - 1) */
ejit_subi(f, EJIT_GPR(0), EJIT_GPR(0), 1);
struct ejit_operand arg[1] = {
EJIT_OPERAND_GPR(0, EJIT_INT32)
};
ejit_calli_i(f, f, 1, arg);
ejit_retval(f, EJIT_GPR(1)); /* loc 1 contains temp result */
/* fib(n - 2) */
ejit_subi(f, EJIT_GPR(0), EJIT_GPR(0), 1);
ejit_calli_i(f, f, 1, arg);
ejit_retval(f, EJIT_GPR(0)); /* loc 0 now contains second temp result */
ejit_addr(f, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); /* add results */
ejit_retr(f, EJIT_GPR(0));
/* the highest location we used was 1, so we need to request 2 locations
* for general purpose registers in total. No floating point registers,
* so 0. */
ejit_select_compile_func(f, 2, 0, EJIT_USE64(int32_t), try_jit, im_scawed);
return f;
}
int main(int argc, char *argv[])
{
if(argc != 4){
fprintf(stderr, "Usage: %s compile_num loop_num jit\n", argv[0]);
return -1;
}
int jit_level = strtoull(argv[3], 0, 0);
size_t compile_num = strtoull(argv[1], 0, 0);
struct ejit_func **info = calloc(compile_num, sizeof(struct ejit_func *));
clock_t t = clock();
for(size_t i = 0; i < compile_num; ++i){
info[i] = compile(jit_level > 0, jit_level > 1);
}
t = clock() - t;
double compile_time_total = ((double)t) / CLOCKS_PER_SEC;
double compile_time_one = compile_time_total / compile_num;
printf("Compilation for n = %zu took %fs (1/%f).\n",
compile_num, compile_time_total, compile_time_one);
size_t run_num = strtoull(argv[2], 0, 0);
t = clock();
struct ejit_arg arg[1] = {
(struct ejit_arg){.type = EJIT_INT32, .l = run_num}
};
int32_t result = ejit_run_func_i(info[0], 1, arg);
t = clock() - t;
double run_time_total = ((double)t) / CLOCKS_PER_SEC;
printf("Running loop for n = %zu took %fs with res %ld\n",
run_num, run_time_total, (long)result);
for(size_t i = 0; i < compile_num; ++i)
ejit_destroy_func(info[i]);
free(info);
return 0;
}
|