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