Forced landscape mode on android
[d2df-sdl.git] / src / lib / libjit / t1.dpr
blobc0e13cb9a5a37690234a65d61c0201f34575529b
1 (*
2 Tutorial 1 - mul_add
4 Builds and compiles the following function:
6 int mul_add(int x, int y, int z)
8   return x * y + z;
11 {$IFDEF WIN32}
12   {$DEFINE MSWINDOWS}
13 {$ENDIF}
15 {$IFDEF FPC}
16   {$MODE OBJFPC}
17   {$PACKRECORDS C}
18 {$ENDIF}
19 program t1;
21 uses
22   SysUtils, Classes,
23   libjit in 'libjit.pas',
24   libjit_types in 'libjit_types.pas';
27 var
28   context: jit_context_t;
29   params: array [0..2] of jit_type_t;
30   signature: jit_type_t;
31   function_: jit_function_t;
32   x, y, z: jit_value_t;
33   temp1, temp2: jit_value_t;
34   arg1, arg2, arg3: jit_int;
35   args: array [0..2] of Pointer;
36   res: jit_int;
37 begin
38   if (jit_type_int = nil) then raise Exception.Create('fuuuuu (0)');
40   (* Create a context to hold the JIT's primary state *)
41   writeln('creating context...');
42   context := jit_context_create();
43   if (context = nil) then raise Exception.Create('cannot create jit context');
45   (* Lock the context while we build and compile the function *)
46   writeln('starting builder...');
47   jit_context_build_start(context);
49   (* Build the function signature *)
50   writeln('creating signature...');
51   params[0] := jit_type_int;
52   params[1] := jit_type_int;
53   params[2] := jit_type_int;
54   signature := jit_type_create_signature(jit_abi_cdecl, jit_type_int, @params[0], 3, 1);
56   (* Create the function object *)
57   writeln('creating function object...');
58   function_ := jit_function_create(context, signature);
59   writeln('freeing signature...');
60   jit_type_free(signature);
62   (* Construct the function body *)
63   writeln('creating function body...');
64   x := jit_value_get_param(function_, 0);
65   y := jit_value_get_param(function_, 1);
66   z := jit_value_get_param(function_, 2);
67   temp1 := jit_insn_mul(function_, x, y);
68   temp2 := jit_insn_add(function_, temp1, z);
69   jit_insn_return(function_, temp2);
71   (* Compile the function *)
72   writeln('compiling function...');
73   jit_function_compile(function_);
75   (* Unlock the context *)
76   writeln('finalizing builder...');
77   jit_context_build_end(context);
79   (* Execute the function and print the result *)
80   writeln('executing function...');
81   arg1 := 3;
82   arg2 := 5;
83   arg3 := 2;
84   args[0] := @arg1;
85   args[1] := @arg2;
86   args[2] := @arg3;
87   jit_function_apply(function_, @args[0], @res);
88   writeln('mul_add(3, 5, 2) = ', Integer(res));
90   (* Clean up *)
91   jit_context_destroy(context);
92 end.