jit: handle equality of function pointer types
[official-gcc.git] / gcc / testsuite / jit.dg / test-accessing-struct.c
blob109a0d7f2cdd90041c8091ea26814dc036626637
1 #include <stdlib.h>
2 #include <stdio.h>
4 #include "libgccjit.h"
6 #include "harness.h"
8 struct foo
10 int x;
11 int y;
12 int z;
15 void
16 create_code (gcc_jit_context *ctxt, void *user_data)
18 /* Let's try to inject the equivalent of:
19 void
20 test_access (struct foo *f)
22 f->z = f->x * f->y;
25 gcc_jit_type *void_type =
26 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
27 gcc_jit_type *int_type =
28 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
29 gcc_jit_field *x =
30 gcc_jit_context_new_field (ctxt,
31 NULL,
32 int_type,
33 "x");
34 gcc_jit_field *y =
35 gcc_jit_context_new_field (ctxt,
36 NULL,
37 int_type,
38 "y");
39 gcc_jit_field *z =
40 gcc_jit_context_new_field (ctxt,
41 NULL,
42 int_type,
43 "z");
44 gcc_jit_field *fields[] = {x, y, z};
45 gcc_jit_struct *struct_type =
46 gcc_jit_context_new_struct_type (ctxt, NULL, "foo", 3, fields);
47 gcc_jit_type *ptr_type =
48 gcc_jit_type_get_pointer (gcc_jit_struct_as_type (struct_type));
50 /* Build the test function. */
51 gcc_jit_param *param_f =
52 gcc_jit_context_new_param (ctxt, NULL, ptr_type, "f");
53 gcc_jit_function *test_fn =
54 gcc_jit_context_new_function (ctxt, NULL,
55 GCC_JIT_FUNCTION_EXPORTED,
56 void_type,
57 "test_access",
58 1, &param_f,
59 0);
61 /* f->x * f->y */
62 gcc_jit_rvalue *sum =
63 gcc_jit_context_new_binary_op (
64 ctxt, NULL,
65 GCC_JIT_BINARY_OP_MULT,
66 int_type,
67 gcc_jit_lvalue_as_rvalue (
68 gcc_jit_rvalue_dereference_field (
69 gcc_jit_param_as_rvalue (param_f),
70 NULL,
71 x)),
72 gcc_jit_lvalue_as_rvalue (
73 gcc_jit_rvalue_dereference_field (
74 gcc_jit_param_as_rvalue (param_f),
75 NULL,
76 y)));
78 /* f->z = ... */
79 gcc_jit_block *block = gcc_jit_function_new_block (test_fn, NULL);
80 gcc_jit_block_add_assignment (
81 block,
82 NULL,
83 gcc_jit_rvalue_dereference_field (
84 gcc_jit_param_as_rvalue (param_f),
85 NULL,
86 z),
87 sum);
88 gcc_jit_block_end_with_void_return (block, NULL);
91 void
92 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
94 typedef void (*fn_type) (struct foo *);
95 CHECK_NON_NULL (result);
97 fn_type test_access =
98 (fn_type)gcc_jit_result_get_code (result, "test_access");
99 CHECK_NON_NULL (test_access);
101 struct foo tmp;
102 tmp.x = 5;
103 tmp.y = 7;
104 tmp.z = 0;
106 /* Call the JIT-generated function. */
107 test_access (&tmp);
109 /* Verify that the code correctly modified the field "z". */
110 CHECK_VALUE (tmp.z, 35);