Commit | Line | Data |
---|---|---|
4a71579b PC |
1 | #include <stdio.h> |
2 | #include <lightning.h> | |
3 | ||
4 | static jit_state_t *_jit; | |
5 | ||
6 | typedef int (*pifi)(int); /* Pointer to Int Function of Int */ | |
7 | ||
8 | int main(int argc, char *argv[]) | |
9 | { | |
10 | pifi fib; | |
11 | jit_node_t *in; /* offset of the argument */ | |
12 | jit_node_t *ref; /* to patch the forward reference */ | |
13 | jit_node_t *zero; /* to patch the forward reference */ | |
14 | jit_node_t *jump; /* jump to start of loop */ | |
15 | jit_node_t *loop; /* start of the loop */ | |
16 | ||
17 | init_jit(argv[0]); | |
18 | _jit = jit_new_state(); | |
19 | ||
20 | jit_prolog (); | |
21 | in = jit_arg (); | |
22 | jit_getarg (JIT_R0, in); /* R0 = n */ | |
23 | zero = jit_beqi (JIT_R0, 0); | |
24 | jit_movr (JIT_R1, JIT_R0); | |
25 | jit_movi (JIT_R0, 1); | |
26 | ref = jit_blei (JIT_R1, 2); | |
27 | jit_subi (JIT_R2, JIT_R1, 2); | |
28 | jit_movr (JIT_R1, JIT_R0); | |
29 | ||
30 | loop= jit_label(); | |
31 | jit_subi (JIT_R2, JIT_R2, 1); /* decr. counter */ | |
32 | jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */ | |
33 | jit_addr (JIT_R0, JIT_R0, JIT_R1); /* R0 = R0 + R1 */ | |
34 | jit_movr (JIT_R1, JIT_V0); /* R1 = V0 */ | |
35 | jump= jit_bnei (JIT_R2, 0); /* if (R2) goto loop; */ | |
36 | jit_patch_at(jump, loop); | |
37 | ||
38 | jit_patch(ref); /* patch forward jump */ | |
39 | jit_patch(zero); /* patch forward jump */ | |
40 | jit_retr (JIT_R0); | |
41 | ||
42 | /* call the generated code, passing 36 as an argument */ | |
43 | fib = jit_emit(); | |
44 | jit_clear_state(); | |
45 | printf("fib(%d) = %d\n", 36, fib(36)); | |
46 | jit_destroy_state(); | |
47 | finish_jit(); | |
48 | return 0; | |
49 | } |