1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#include "sljitLir.h"
#include <stdio.h>
#include <stdlib.h>
struct point_st {
long x;
int y;
short z;
char d;
char e;
};
typedef long (*point_func_t)(struct point_st *point);;
static long SLJIT_CALL print_num(long a)
{
printf("a = %ld\n", a);
return a + 1;
}
/*
This example, we generate a function like this:
long func(struct point_st *point)
{
print_num(point->x);
print_num(point->y);
print_num(point->z);
print_num(point->d);
return point->x;
}
*/
static int struct_access()
{
void *code;
unsigned long len;
point_func_t func;
struct point_st point = {
-5, -20, 5, ' ', 'a'
};
/* Create a SLJIT compiler */
struct sljit_compiler *C = sljit_create_compiler();
sljit_emit_enter(C, 0, 1, 1, 1, 0, 0, 0);
/* opt arg R S FR FS local_size */
sljit_emit_op1(C, SLJIT_MOV, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(struct point_st, x)); // S0->x --> R0
sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); // print_num(R0);
sljit_emit_op1(C, SLJIT_MOV_SI, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(struct point_st, y)); // S0->y --> R0
sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); // print_num(R0);
sljit_emit_op1(C, SLJIT_MOV_SH, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(struct point_st, z)); // S0->z --> R0
sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); // print_num(R0);
sljit_emit_op1(C, SLJIT_MOV_SB, SLJIT_R0, 0, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(struct point_st, d)); // S0->z --> R0
sljit_emit_ijump(C, SLJIT_CALL1, SLJIT_IMM, SLJIT_FUNC_OFFSET(print_num)); // print_num(R0);
sljit_emit_return(C, SLJIT_MOV, SLJIT_MEM1(SLJIT_S0), SLJIT_OFFSETOF(struct point_st, x)); // return S0->x
/* Generate machine code */
code = sljit_generate_code(C);
len = sljit_get_generated_code_size(C);
/* Execute code */
func = (point_func_t)code;
printf("func return %ld\n", func(&point));
/* dump_code(code, len); */
/* Clean up */
sljit_free_compiler(C);
sljit_free_code(code);
return 0;
}
int main()
{
return struct_access();
}
|