jit: handle equality of function pointer types
[official-gcc.git] / gcc / testsuite / jit.dg / test-using-global.c
blob8ac9780d2b130d95206b4dc6fdb9caec4563860e
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 extern int imported_global;
14 #ifdef __cplusplus
16 #endif
18 void
19 create_code (gcc_jit_context *ctxt, void *user_data)
21 /* Let's try to inject the equivalent of:
23 int exported_global;
24 extern int imported_global;
25 static int internal_global;
27 int
28 test_using_global (void)
30 exported_global += 1;
31 imported_global += 1;
32 internal_global += 1;
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,
41 NULL,
42 GCC_JIT_GLOBAL_EXPORTED,
43 int_type,
44 "exported_global");
45 gcc_jit_lvalue *imported_global =
46 gcc_jit_context_new_global (ctxt,
47 NULL,
48 GCC_JIT_GLOBAL_IMPORTED,
49 int_type,
50 "imported_global");
51 gcc_jit_lvalue *internal_global =
52 gcc_jit_context_new_global (ctxt,
53 NULL,
54 GCC_JIT_GLOBAL_INTERNAL,
55 int_type,
56 "internal_global");
58 /* Build the test_fn. */
59 gcc_jit_function *test_fn =
60 gcc_jit_context_new_function (ctxt, NULL,
61 GCC_JIT_FUNCTION_EXPORTED,
62 int_type,
63 "test_using_global",
64 0, NULL,
65 0);
66 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
68 gcc_jit_block_add_assignment_op (
69 block, NULL,
70 exported_global,
71 GCC_JIT_BINARY_OP_PLUS,
72 gcc_jit_context_one (ctxt, int_type));
73 gcc_jit_block_add_assignment_op (
74 block, NULL,
75 imported_global,
76 GCC_JIT_BINARY_OP_PLUS,
77 gcc_jit_context_one (ctxt, int_type));
78 gcc_jit_block_add_assignment_op (
79 block, NULL,
80 internal_global,
81 GCC_JIT_BINARY_OP_PLUS,
82 gcc_jit_context_one (ctxt, int_type));
83 gcc_jit_block_end_with_return (block,
84 NULL,
85 gcc_jit_lvalue_as_rvalue (internal_global));
88 int imported_global;
90 void
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);