Home | History | Annotate | Line # | Download | only in tutorial
first_program.c revision 1.1.1.1.2.2
      1 #include "sljitLir.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 typedef long SLJIT_CALL (*func3_t)(long a, long b, long c);
      7 
      8 static int add3(long a, long b, long c)
      9 {
     10 	void *code;
     11 	unsigned long len;
     12 	func3_t func;
     13 
     14 	/* Create a SLJIT compiler */
     15 	struct sljit_compiler *C = sljit_create_compiler();
     16 
     17 	/* Start a context(function entry), have 3 arguments, discuss later */
     18 	sljit_emit_enter(C, 0,  3,  1, 3, 0, 0, 0);
     19 
     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);
     23 
     24 	/* R0 = R0 + second */
     25 	sljit_emit_op2(C, SLJIT_ADD, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_S1, 0);
     26 
     27 	/* R0 = R0 + third */
     28 	sljit_emit_op2(C, SLJIT_ADD, SLJIT_R0, 0, SLJIT_R0, 0, SLJIT_S2, 0);
     29 
     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);
     33 
     34 	/* Generate machine code */
     35 	code = sljit_generate_code(C);
     36 	len = sljit_get_generated_code_size(C);
     37 
     38 	/* Execute code */
     39 	func = (func3_t)code;
     40 	printf("func return %ld\n", func(a, b, c));
     41 
     42 	/* dump_code(code, len); */
     43 
     44 	/* Clean up */
     45 	sljit_free_compiler(C);
     46 	sljit_free_code(code);
     47 	return 0;
     48 }
     49 
     50 int main()
     51 {
     52 	return add3(4, 5, 6);
     53 }
     54