4 static jit_state_t *_jit;
6 typedef long (*pwfw_t)(long); /* Pointer to Long Function of Long */
8 int main(int argc, char *argv[])
12 jit_node_t *ac; /* Accumulator */
13 jit_node_t *in; /* Argument */
17 jit_node_t *fact_entry;
21 _jit = jit_new_state();
23 /* declare a forward label */
26 jit_prolog(); /* Entry point of the factorial function */
27 in = jit_arg(); /* Receive an integer argument */
28 jit_getarg(JIT_R0, in); /* Move argument to RO */
30 jit_pushargi(1); /* This is the accumulator */
31 jit_pushargr(JIT_R0); /* This is the argument */
32 call = jit_finishi(NULL); /* Call the tail call optimized function */
33 jit_patch_at(call, fact); /* Patch call to forward defined function */
34 /* the above could have been written as:
35 * jit_patch_at(jit_finishi(NULL), fact);
37 jit_retval(JIT_R0); /* Fetch the result */
38 jit_retr(JIT_R0); /* Return it */
39 jit_epilog(); /* Epilog *before* label before prolog */
41 /* define the forward label */
42 jit_link(fact); /* Entry point of the helper function */
44 jit_frame(16); /* Reserve 16 bytes in the stack */
45 fact_entry = jit_label(); /* This is the tail call entry point */
46 ac = jit_arg(); /* The accumulator is the first argument */
47 in = jit_arg(); /* The factorial argument */
48 jit_getarg(JIT_R0, ac); /* Move the accumulator to R0 */
49 jit_getarg(JIT_R1, in); /* Move the argument to R1 */
50 fact_out = jit_blei(JIT_R1, 1); /* Done if argument is one or less */
51 jit_mulr(JIT_R0, JIT_R0, JIT_R1); /* accumulator *= argument */
52 jit_putargr(JIT_R0, ac); /* Update the accumulator */
53 jit_subi(JIT_R1, JIT_R1, 1); /* argument -= 1 */
54 jit_putargr(JIT_R1, in); /* Update the argument */
56 jit_patch_at(jump, fact_entry); /* Tail Call Optimize it! */
58 jit_retr(JIT_R0); /* Return the accumulator */
60 factorial = jit_emit();
61 /* no need to query information about resolved addresses */
69 /* call the generated code */
70 printf("factorial(%ld) = %ld\n", arg, factorial(arg));
71 /* release all memory associated with the _jit identifier */