1 #include "sljitLir.h" 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 typedef long SLJIT_CALL (*func3_t)(long a, long b, long c); 7 8 static long SLJIT_CALL print_num(long a) 9 { 10 printf("a = %ld\n", a); 11 return a + 1; 12 } 13 14 /* 15 This example, we generate a function like this: 16 17 long func(long a, long b, long c) 18 { 19 if ((a & 1) == 0) 20 return print_num(c); 21 return print_num(b); 22 } 23 */ 24 25 static int func_call(long a, long b, long c) 26 { 27 void *code; 28 unsigned long len; 29 func3_t func; 30 31 struct sljit_jump *out; 32 struct sljit_jump *print_c; 33 34 /* Create a SLJIT compiler */ 35 struct sljit_compiler *C = sljit_create_compiler(); 36 37 sljit_emit_enter(C, 0, 3, 1, 3, 0, 0, 0); 38 39 /* a & 1 --> R0 */ 40 sljit_emit_op2(C, SLJIT_AND, SLJIT_R0, 0, SLJIT_S0, 0, SLJIT_IMM, 1); 41 /* R0 == 0 --> jump print_c */ 42 print_c = sljit_emit_cmp(C, SLJIT_EQUAL, SLJIT_R0, 0, SLJIT_IMM, 0); 43 44 /* R0 = S1; print_num(R0) */ 45 sljit_emit_op1(C, SLJIT_MOV, SLJIT_R0, 0, SLJIT_S1, 0); 46 sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); 47 48 /* jump out */ 49 out = sljit_emit_jump(C, SLJIT_JUMP); 50 /* print_c: */ 51 sljit_set_label(print_c, sljit_emit_label(C)); 52 53 /* R0 = c; print_num(R0); */ 54 sljit_emit_op1(C, SLJIT_MOV, SLJIT_R0, 0, SLJIT_S2, 0); 55 sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); 56 57 /* out: */ 58 sljit_set_label(out, sljit_emit_label(C)); 59 sljit_emit_return(C, SLJIT_MOV, SLJIT_R0, 0); 60 61 /* Generate machine code */ 62 code = sljit_generate_code(C); 63 len = sljit_get_generated_code_size(C); 64 65 /* Execute code */ 66 func = (func3_t)code; 67 printf("func return %ld\n", func(a, b, c)); 68 69 /* dump_code(code, len); */ 70 71 /* Clean up */ 72 sljit_free_compiler(C); 73 sljit_free_code(code); 74 return 0; 75 } 76 77 int main() 78 { 79 return func_call(4, 5, 6); 80 } 81