1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "libgccjit.h"
6
7 #include "harness.h"
8
9 void
10 create_code (gcc_jit_context *ctxt, void *user_data)
11 {
12 /* Let's try to inject the equivalent of:
13 char
14 my_casts (int x)
15 {
16 return (char)(long) x;
17 }
18 */
19 gcc_jit_type *int_type =
20 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
21 gcc_jit_type *long_type =
22 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
23 gcc_jit_type *return_type =
24 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
25
26 gcc_jit_param *x =
27 gcc_jit_context_new_param (
28 ctxt,
29 NULL,
30 int_type, "x");
31 gcc_jit_param *params[1] = {x};
32 gcc_jit_function *func =
33 gcc_jit_context_new_function (ctxt,
34 NULL,
35 GCC_JIT_FUNCTION_EXPORTED,
36 return_type,
37 "my_casts",
38 1, params, 0);
39
40 gcc_jit_block *initial =
41 gcc_jit_function_new_block (func, "initial");
42
43 gcc_jit_block_end_with_return(initial, NULL,
44 gcc_jit_context_new_cast(ctxt,
45 NULL,
46 gcc_jit_context_new_cast(ctxt,
47 NULL,
48 gcc_jit_param_as_rvalue(x),
49 long_type
50 ),
51 return_type
52 ));
53 }
54
55 void
56 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
57 {
58 typedef int (*my_casts_fn_type) (int);
59 CHECK_NON_NULL (result);
60 my_casts_fn_type my_casts =
61 (my_casts_fn_type)gcc_jit_result_get_code (result, "my_casts");
62 CHECK_NON_NULL (my_casts);
63 char val = my_casts (10);
64 note ("my_casts returned: %d", val);
65 CHECK_VALUE (val, 10);
66 }