12 extern int imported_global
;
19 create_code (gcc_jit_context
*ctxt
, void *user_data
)
21 /* Let's try to inject the equivalent of:
24 extern int imported_global;
25 static int internal_global;
28 test_using_global (void)
33 return internal_global;
36 gcc_jit_type
*int_type
=
37 gcc_jit_context_get_type (ctxt
, GCC_JIT_TYPE_INT
);
39 gcc_jit_lvalue
*exported_global
=
40 gcc_jit_context_new_global (ctxt
,
42 GCC_JIT_GLOBAL_EXPORTED
,
45 gcc_jit_lvalue
*imported_global
=
46 gcc_jit_context_new_global (ctxt
,
48 GCC_JIT_GLOBAL_IMPORTED
,
51 gcc_jit_lvalue
*internal_global
=
52 gcc_jit_context_new_global (ctxt
,
54 GCC_JIT_GLOBAL_INTERNAL
,
58 /* Build the test_fn. */
59 gcc_jit_function
*test_fn
=
60 gcc_jit_context_new_function (ctxt
, NULL
,
61 GCC_JIT_FUNCTION_EXPORTED
,
66 gcc_jit_block
*block
= gcc_jit_function_new_block (test_fn
, NULL
);
68 gcc_jit_block_add_assignment_op (
71 GCC_JIT_BINARY_OP_PLUS
,
72 gcc_jit_context_one (ctxt
, int_type
));
73 gcc_jit_block_add_assignment_op (
76 GCC_JIT_BINARY_OP_PLUS
,
77 gcc_jit_context_one (ctxt
, int_type
));
78 gcc_jit_block_add_assignment_op (
81 GCC_JIT_BINARY_OP_PLUS
,
82 gcc_jit_context_one (ctxt
, int_type
));
83 gcc_jit_block_end_with_return (block
,
85 gcc_jit_lvalue_as_rvalue (internal_global
));
91 verify_code (gcc_jit_context
*ctxt
, gcc_jit_result
*result
)
93 typedef int (*fn_type
) (void);
94 CHECK_NON_NULL (result
);
96 fn_type test_using_global
=
97 (fn_type
)gcc_jit_result_get_code (result
, "test_using_global");
98 CHECK_NON_NULL (test_using_global
);
100 /* The exported global should be visible. */
101 int *exported_global
= (int *)gcc_jit_result_get_global (result
, "exported_global");
102 CHECK_NON_NULL (exported_global
);
103 /* ...and should be zero-initialized. */
104 CHECK_VALUE (*exported_global
, 0);
106 /* Set some nonzero values. */
107 *exported_global
= 11;
108 imported_global
= 42;
110 /* The internal global shouldn't be visible. */
111 int *internal_global
= (int *)gcc_jit_result_get_global (result
, "internal_global");
112 CHECK_VALUE (internal_global
, NULL
);
114 /* Call the JIT-generated function. */
115 int call_count
= test_using_global ();
117 /* Verify that it correctly modified imported_global and exported_global. */
118 CHECK_VALUE (*exported_global
, 12);
119 CHECK_VALUE (imported_global
, 43);
120 CHECK_VALUE (call_count
, 1);
122 /* Try calling it again. */
123 call_count
= test_using_global ();
125 /* Verify the new values. */
126 CHECK_VALUE (*exported_global
, 13);
127 CHECK_VALUE (imported_global
, 44);
128 CHECK_VALUE (call_count
, 2);