1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "libgccjit.h"
5 #include "harness.h"
6 
7 #ifndef LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option
8 #error LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option was not defined
9 #endif
10 
11 void
create_code(gcc_jit_context * ctxt,void * user_data)12 create_code (gcc_jit_context *ctxt, void *user_data)
13 {
14 
15   gcc_jit_context_add_driver_option (ctxt, "-L./");
16   gcc_jit_context_add_driver_option (ctxt, "-ladd-driver-options-testlib");
17 
18   /* Let's try to inject the equivalent of:
19 
20       int caller_function (void)
21       {
22         return callee_function ();
23       }
24   */
25   gcc_jit_type *int_type =
26     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
27 
28   gcc_jit_function *caller_func =
29     gcc_jit_context_new_function (ctxt, NULL,
30                                   GCC_JIT_FUNCTION_EXPORTED,
31                                   int_type,
32                                   "caller_function",
33                                   0, NULL,
34                                   0);
35 
36   gcc_jit_block *block =
37     gcc_jit_function_new_block (caller_func, NULL);
38 
39   gcc_jit_function *callee_func =
40     gcc_jit_context_new_function(ctxt, NULL,
41 				 GCC_JIT_FUNCTION_IMPORTED,
42 				 int_type,
43 				 "callee_function",
44 				 0, NULL,
45 				 1);
46 
47   gcc_jit_block_end_with_return (block, NULL,
48 				 gcc_jit_context_new_call(ctxt,
49 							  NULL,
50 							  callee_func,
51 							  0,
52 							  0));
53 }
54 
55 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)56 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
57 {
58   typedef int (*my_caller_fn_type) (void);
59 
60   CHECK_NON_NULL (result);
61   my_caller_fn_type callee_function_ptr =
62     (my_caller_fn_type)gcc_jit_result_get_code (result, "callee_function");
63   CHECK_NON_NULL (callee_function_ptr);
64 
65   int res = callee_function_ptr ();
66 
67   CHECK_VALUE (res, 1978);
68 }
69