6 typedef long (SLJIT_FUNC
*func2_t
)(long a
, long b
);
9 This example, we generate a function like this:
11 long func(long a, long b)
15 for (i = 0; i < a; ++i) {
22 static int loop(long a
, long b
)
28 struct sljit_label
*loopstart
;
29 struct sljit_jump
*out
;
31 /* Create a SLJIT compiler */
32 struct sljit_compiler
*C
= sljit_create_compiler(NULL
, NULL
);
34 /* 2 arg, 2 temp reg, 2 saved reg */
35 sljit_emit_enter(C
, 0, SLJIT_ARGS2(W
, W
, W
), 2, 2, 0, 0, 0);
38 sljit_emit_op2(C
, SLJIT_XOR
, SLJIT_R1
, 0, SLJIT_R1
, 0, SLJIT_R1
, 0);
40 sljit_emit_op1(C
, SLJIT_MOV
, SLJIT_RETURN_REG
, 0, SLJIT_IMM
, 0);
42 loopstart
= sljit_emit_label(C
);
43 /* R1 >= a --> jump out */
44 out
= sljit_emit_cmp(C
, SLJIT_GREATER_EQUAL
, SLJIT_R1
, 0, SLJIT_S0
, 0);
46 sljit_emit_op2(C
, SLJIT_ADD
, SLJIT_RETURN_REG
, 0, SLJIT_RETURN_REG
, 0, SLJIT_S1
, 0);
48 sljit_emit_op2(C
, SLJIT_ADD
, SLJIT_R1
, 0, SLJIT_R1
, 0, SLJIT_IMM
, 1);
50 sljit_set_label(sljit_emit_jump(C
, SLJIT_JUMP
), loopstart
);
52 sljit_set_label(out
, sljit_emit_label(C
));
55 sljit_emit_return(C
, SLJIT_MOV
, SLJIT_RETURN_REG
, 0);
57 /* Generate machine code */
58 code
= sljit_generate_code(C
);
59 len
= sljit_get_generated_code_size(C
);
63 printf("func return %ld\n", func(a
, b
));
65 /* dump_code(code, len); */
68 sljit_free_compiler(C
);
69 sljit_free_code(code
, NULL
);