1 /* This code is shared by various test-compile-to-*.c test cases
2    that ultimately generate a standalone executable
3    (all of them apart from test-compile-to-dynamic-library.c).  */
4 
5 void
create_code(gcc_jit_context * ctxt,void * user_data)6 create_code (gcc_jit_context *ctxt, void *user_data)
7 {
8   /* Let's try to inject the equivalent of:
9      static void
10      hello_world (const char *name)
11      {
12        // a test comment
13        printf ("hello from %s\n", name);
14      }
15 
16      extern int
17      main (int argc, char **argv)
18      {
19        hello_world (argv[0]);
20        return 0;
21      }
22   */
23   gcc_jit_type *void_type =
24     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
25   gcc_jit_type *const_char_ptr_type =
26     gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CONST_CHAR_PTR);
27   gcc_jit_param *param_name =
28     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "name");
29   gcc_jit_function *func =
30     gcc_jit_context_new_function (ctxt, NULL,
31 				  GCC_JIT_FUNCTION_INTERNAL,
32 				  void_type,
33 				  "hello_world",
34 				  1, &param_name,
35 				  0);
36 
37   gcc_jit_param *param_format =
38     gcc_jit_context_new_param (ctxt, NULL, const_char_ptr_type, "format");
39   gcc_jit_function *printf_func =
40     gcc_jit_context_new_function (ctxt, NULL,
41 				  GCC_JIT_FUNCTION_IMPORTED,
42 				  gcc_jit_context_get_type (
43 				     ctxt, GCC_JIT_TYPE_INT),
44 				  "printf",
45 				  1, &param_format,
46 				  1);
47   gcc_jit_rvalue *args[2];
48   args[0] = gcc_jit_context_new_string_literal (ctxt, "hello from %s\n");
49   args[1] = gcc_jit_param_as_rvalue (param_name);
50 
51   gcc_jit_block *block = gcc_jit_function_new_block (func, NULL);
52 
53   gcc_jit_block_add_comment (
54     block, NULL,
55     "a test comment");
56 
57   gcc_jit_block_add_eval (
58     block, NULL,
59     gcc_jit_context_new_call (ctxt,
60 			      NULL,
61 			      printf_func,
62 			      2, args));
63   gcc_jit_block_end_with_void_return (block, NULL);
64 
65   gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
66   gcc_jit_param *param_argc =
67     gcc_jit_context_new_param (ctxt, NULL, int_type, "argc");
68   gcc_jit_type *char_ptr_ptr_type =
69     gcc_jit_type_get_pointer (
70       gcc_jit_type_get_pointer (
71 	gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR)));
72   gcc_jit_param *param_argv =
73     gcc_jit_context_new_param (ctxt, NULL, char_ptr_ptr_type, "argv");
74   gcc_jit_param *params[2] = {param_argc, param_argv};
75   gcc_jit_function *func_main =
76     gcc_jit_context_new_function (ctxt, NULL,
77 				  GCC_JIT_FUNCTION_EXPORTED,
78 				  int_type,
79 				  "main",
80 				  2, params,
81 				  0);
82   block = gcc_jit_function_new_block (func_main, NULL);
83   gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type);
84   args[0] = gcc_jit_context_new_cast (
85 	ctxt,
86 	NULL,
87 	gcc_jit_lvalue_as_rvalue (
88 	  gcc_jit_context_new_array_access (
89 	    ctxt,
90 	    NULL,
91 	    gcc_jit_param_as_rvalue (param_argv),
92 	    zero)),
93 	const_char_ptr_type);
94   gcc_jit_block_add_eval (
95     block, NULL,
96     gcc_jit_context_new_call (ctxt,
97 			      NULL,
98 			      func,
99 			      1, args));
100   gcc_jit_block_end_with_return (block, NULL, zero);
101 }
102