Rename tail call to call return and minor ARM improvements.
[sljit.git] / doc / tutorial / first_program.c
blob3fa685dd1df53f646387b9d873ec12390f89ce48
1 #include "sljitLir.h"
3 #include <stdio.h>
4 #include <stdlib.h>
6 typedef long (SLJIT_FUNC *func3_t)(long a, long b, long c);
8 static int add3(long a, long b, long c)
10 void *code;
11 unsigned long len;
12 func3_t func;
14 /* Create a SLJIT compiler */
15 struct sljit_compiler *C = sljit_create_compiler(NULL, NULL);
17 /* Start a context(function entry), have 3 arguments, discuss later */
18 sljit_emit_enter(C, 0, SLJIT_ARGS3(W, W, W, W), 1, 3, 0, 0, 0);
20 /* The first arguments of function is register SLJIT_S0, 2nd, SLJIT_S1, etc. */
21 /* R0 = first */
22 sljit_emit_op1(C, SLJIT_MOV, SLJIT_R0, 0, SLJIT_S0, 0);
24 /* R0 = R0 + second */
25 sljit_emit_op2(C, SLJIT_ADD, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_S1, 0);
27 /* R0 = R0 + third */
28 sljit_emit_op2(C, SLJIT_ADD, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_S2, 0);
30 /* This statement mov R0 to RETURN REG and return */
31 /* in fact, R0 is RETURN REG itself */
32 sljit_emit_return(C, SLJIT_MOV, SLJIT_R0, 0);
34 /* Generate machine code */
35 code = sljit_generate_code(C);
36 len = sljit_get_generated_code_size(C);
38 /* Execute code */
39 func = (func3_t)code;
40 printf("func return %ld\n", func(a, b, c));
42 /* dump_code(code, len); */
44 /* Clean up */
45 sljit_free_compiler(C);
46 sljit_free_code(code, NULL);
47 return 0;
50 int main()
52 return add3(4, 5, 6);