1 /* Test of using the API with very long names. */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "libgccjit.h"
7
8 #include "harness.h"
9
10 /* 65KB */
11 #define NAME_LENGTH (65 * 1024)
12
13 static struct long_names
14 {
15 char struct_name[NAME_LENGTH];
16 char fn_name[NAME_LENGTH];
17 char local_name[NAME_LENGTH];
18 char block_name[NAME_LENGTH];
19 } long_names;
20
21 static void
populate_name(const char * prefix,char * buffer)22 populate_name (const char *prefix, char *buffer)
23 {
24 int i;
25
26 /* Begin with the given prefix: */
27 sprintf (buffer, "%s", prefix);
28
29 /* Populate the rest of the buffer with 0123456789 repeatedly: */
30 for (i = strlen (prefix); i < NAME_LENGTH - 1; i++)
31 buffer[i] = '0' + (i % 10);
32
33 /* NIL-terminate the buffer: */
34 buffer[NAME_LENGTH - 1] = '\0';
35 }
36
37 static void
populate_names(void)38 populate_names (void)
39 {
40 populate_name ("struct_", long_names.struct_name);
41 populate_name ("test_fn_", long_names.fn_name);
42 populate_name ("local_", long_names.local_name);
43 populate_name ("block_", long_names.block_name);
44 }
45
46 void
create_code(gcc_jit_context * ctxt,void * user_data)47 create_code (gcc_jit_context *ctxt, void *user_data)
48 {
49 /* Where "ETC" is a very long suffix, let's try to inject the
50 equivalent of:
51
52 struct struct_ETC;
53
54 int
55 test_fn_ETC ()
56 {
57 int local_ETC;
58 local_ETC = 42;
59 return local_ETC;
60 }
61
62 to verify that the API copes with such long names. */
63
64 populate_names ();
65
66 gcc_jit_type *int_type =
67 gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
68
69 /* We don't yet use this struct. */
70 (void)gcc_jit_context_new_opaque_struct (ctxt, NULL,
71 long_names.struct_name);
72
73 gcc_jit_function *test_fn =
74 gcc_jit_context_new_function (ctxt, NULL,
75 GCC_JIT_FUNCTION_EXPORTED,
76 int_type,
77 long_names.fn_name,
78 0, NULL,
79 0);
80 gcc_jit_lvalue *local =
81 gcc_jit_function_new_local (test_fn,
82 NULL,
83 int_type,
84 long_names.local_name);
85
86 gcc_jit_block *block =
87 gcc_jit_function_new_block (test_fn, long_names.block_name);
88
89 gcc_jit_block_add_assignment (
90 block,
91 NULL,
92 local,
93 gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
94
95 gcc_jit_block_end_with_return (
96 block, NULL,
97 gcc_jit_lvalue_as_rvalue (local));
98 }
99
100 void
verify_code(gcc_jit_context * ctxt,gcc_jit_result * result)101 verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
102 {
103 CHECK_NON_NULL (result);
104
105 typedef int (*my_fn_type) (void);
106 CHECK_NON_NULL (result);
107 my_fn_type my_fn =
108 (my_fn_type)gcc_jit_result_get_code (result, long_names.fn_name);
109 CHECK_NON_NULL (my_fn);
110 int val = my_fn ();
111 CHECK_VALUE (val, 42);
112 }
113