4 typedef int (*pifi)(int); /* Pointer to Int Function of Int */
6 static jit_state_t *_jit;
8 void stack_push(int reg, int *sp)
10 jit_stxi_i (*sp, JIT_FP, reg);
14 void stack_pop(int reg, int *sp)
17 jit_ldxi_i (reg, JIT_FP, *sp);
20 jit_node_t *compile_rpn(char *expr)
23 int stack_base, stack_ptr;
25 fn = jit_note(NULL, 0);
28 stack_ptr = stack_base = jit_allocai (32 * sizeof (int));
30 jit_getarg_i(JIT_R2, in);
35 if (sscanf(expr, "%[0-9]%n", buf, &n)) {
37 stack_push(JIT_R0, &stack_ptr);
38 jit_movi(JIT_R0, atoi(buf));
39 } else if (*expr == 'x') {
40 stack_push(JIT_R0, &stack_ptr);
41 jit_movr(JIT_R0, JIT_R2);
42 } else if (*expr == '+') {
43 stack_pop(JIT_R1, &stack_ptr);
44 jit_addr(JIT_R0, JIT_R1, JIT_R0);
45 } else if (*expr == '-') {
46 stack_pop(JIT_R1, &stack_ptr);
47 jit_subr(JIT_R0, JIT_R1, JIT_R0);
48 } else if (*expr == '*') {
49 stack_pop(JIT_R1, &stack_ptr);
50 jit_mulr(JIT_R0, JIT_R1, JIT_R0);
51 } else if (*expr == '/') {
52 stack_pop(JIT_R1, &stack_ptr);
53 jit_divr(JIT_R0, JIT_R1, JIT_R0);
55 fprintf(stderr, "cannot compile: %s\n", expr);
65 int main(int argc, char *argv[])
72 _jit = jit_new_state();
74 nc = compile_rpn("32x9*5/+");
75 nf = compile_rpn("x32-5*9/");
77 c2f = (pifi)jit_address(nc);
78 f2c = (pifi)jit_address(nf);
82 for (i = 0; i <= 100; i += 10) printf("%3d ", i);
84 for (i = 0; i <= 100; i += 10) printf("%3d ", c2f(i));
88 for (i = 32; i <= 212; i += 18) printf("%3d ", i);
90 for (i = 32; i <= 212; i += 18) printf("%3d ", f2c(i));