[ARM,testsuite] Skip tests incompatible with -mpure-code.
[gcc.git] / gcc / testsuite / jit.dg / test-calling-external-function.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "libgccjit.h"
5
6 #include "harness.h"
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 extern void
13 called_function (int i, int j, int k);
14
15 #ifdef __cplusplus
16 }
17 #endif
18
19 void
20 create_code (gcc_jit_context *ctxt, void *user_data)
21 {
22 /* Let's try to inject the equivalent of:
23 extern void called_function (int i, int j, int k);
24
25 void
26 test_caller (int a)
27 {
28 called_function (a * 3, a * 4, a * 5);
29 }
30 */
31 int i;
32 gcc_jit_type *void_type =
33 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
34 gcc_jit_type *int_type =
35 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
36
37 /* Declare the imported function. */
38 gcc_jit_param *params[3];
39 params[0] =
40 gcc_jit_context_new_param (ctxt, NULL, int_type, "i");
41 params[1] =
42 gcc_jit_context_new_param (ctxt, NULL, int_type, "j");
43 params[2] =
44 gcc_jit_context_new_param (ctxt, NULL, int_type, "k");
45 gcc_jit_function *called_fn =
46 gcc_jit_context_new_function (ctxt, NULL,
47 GCC_JIT_FUNCTION_IMPORTED,
48 void_type,
49 "called_function",
50 3, params,
51 0);
52
53 /* Build the test_fn. */
54 gcc_jit_param *param_a =
55 gcc_jit_context_new_param (ctxt, NULL, int_type, "a");
56 gcc_jit_function *test_fn =
57 gcc_jit_context_new_function (ctxt, NULL,
58 GCC_JIT_FUNCTION_EXPORTED,
59 void_type,
60 "test_caller",
61 1, &param_a,
62 0);
63 /* "a * 3, a * 4, a * 5" */
64 gcc_jit_rvalue *args[3];
65 for (i = 0; i < 3; i++)
66 args[i] =
67 gcc_jit_context_new_binary_op (
68 ctxt, NULL,
69 GCC_JIT_BINARY_OP_MULT,
70 int_type,
71 gcc_jit_param_as_rvalue (param_a),
72 gcc_jit_context_new_rvalue_from_int (
73 ctxt,
74 int_type,
75 (i + 3) ));
76 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
77 gcc_jit_block_add_eval (
78 block, NULL,
79 gcc_jit_context_new_call (ctxt,
80 NULL,
81 called_fn,
82 3, args));
83 gcc_jit_block_end_with_void_return (block, NULL);
84 }
85
86 static int called_with[3];
87
88 extern void
89 called_function (int i, int j, int k)
90 {
91 called_with[0] = i;
92 called_with[1] = j;
93 called_with[2] = k;
94 }
95
96 void
97 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
98 {
99 typedef void (*fn_type) (int);
100 CHECK_NON_NULL (result);
101
102 fn_type test_caller =
103 (fn_type)gcc_jit_result_get_code (result, "test_caller");
104 CHECK_NON_NULL (test_caller);
105
106 called_with[0] = 0;
107 called_with[1] = 0;
108 called_with[2] = 0;
109
110 /* Call the JIT-generated function. */
111 test_caller (5);
112
113 /* Verify that it correctly called "called_function". */
114 CHECK_VALUE (called_with[0], 15);
115 CHECK_VALUE (called_with[1], 20);
116 CHECK_VALUE (called_with[2], 25);
117 }
118