1 #include "sljitLir.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 typedef long (*func3_t)(long a, long b, long c);
7
print_arr(long * a,long n)8 static long SLJIT_CALL print_arr(long *a, long n)
9 {
10 long i;
11 long sum = 0;
12 for (i = 0; i < n; ++i) {
13 sum += a[i];
14 printf("arr[%ld] = %ld\n", i, a[i]);
15 }
16 return sum;
17 }
18
19 /*
20 This example, we generate a function like this:
21
22 long func(long a, long b, long c)
23 {
24 long arr[3] = { a, b, c };
25 return print_arr(arr, 3);
26 }
27 */
28
temp_var(long a,long b,long c)29 static int temp_var(long a, long b, long c)
30 {
31 void *code;
32 unsigned long len;
33 func3_t func;
34
35 /* Create a SLJIT compiler */
36 struct sljit_compiler *C = sljit_create_compiler();
37
38 /* reserved space in stack for long arr[3] */
39 sljit_emit_enter(C, 0, 3, 2, 3, 0, 0, 3 * sizeof(long));
40 /* opt arg R S FR FS local_size */
41
42 /* arr[0] = S0, SLJIT_SP is the init address of local var */
43 sljit_emit_op1(C, SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), 0, SLJIT_S0, 0);
44 /* arr[1] = S1 */
45 sljit_emit_op1(C, SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), 1 * sizeof(long), SLJIT_S1, 0);
46 /* arr[2] = S2 */
47 sljit_emit_op1(C, SLJIT_MOV, SLJIT_MEM1(SLJIT_SP), 2 * sizeof(long), SLJIT_S2, 0);
48
49 /* R0 = arr; in fact SLJIT_SP is the address of arr, but can't do so in SLJIT */
50 sljit_get_local_base(C, SLJIT_R0, 0, 0); /* get the address of local variables */
51 sljit_emit_op1(C, SLJIT_MOV, SLJIT_R1, 0, SLJIT_IMM, 3); /* R1 = 3; */
52 sljit_emit_ijump(C, SLJIT_CALL2, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_arr));
53 sljit_emit_return(C, SLJIT_MOV, SLJIT_R0, 0);
54
55 /* Generate machine code */
56 code = sljit_generate_code(C);
57 len = sljit_get_generated_code_size(C);
58
59 /* Execute code */
60 func = (func3_t)code;
61 printf("func return %ld\n", func(a, b, c));
62
63 /* dump_code(code, len); */
64
65 /* Clean up */
66 sljit_free_compiler(C);
67 sljit_free_code(code);
68 return 0;
69 }
70
main()71 int main()
72 {
73 return temp_var(7, 8, 9);
74 }
75