Commit | Line | Data |
---|---|---|
bb2ec1b3 TT |
1 | /* C language support for compilation. |
2 | ||
e2882c85 | 3 | Copyright (C) 2014-2018 Free Software Foundation, Inc. |
bb2ec1b3 TT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "compile-internal.h" | |
b7dc48b4 | 22 | #include "compile-c.h" |
bb2ec1b3 TT |
23 | #include "compile.h" |
24 | #include "gdb-dlfcn.h" | |
25 | #include "c-lang.h" | |
26 | #include "macrotab.h" | |
27 | #include "macroscope.h" | |
28 | #include "regcache.h" | |
14bc53a8 | 29 | #include "common/function-view.h" |
d269dfc6 | 30 | #include "common/preprocessor.h" |
bb2ec1b3 TT |
31 | |
32 | /* See compile-internal.h. */ | |
33 | ||
34 | const char * | |
35 | c_get_mode_for_size (int size) | |
36 | { | |
37 | const char *mode = NULL; | |
38 | ||
39 | switch (size) | |
40 | { | |
41 | case 1: | |
42 | mode = "QI"; | |
43 | break; | |
44 | case 2: | |
45 | mode = "HI"; | |
46 | break; | |
47 | case 4: | |
48 | mode = "SI"; | |
49 | break; | |
50 | case 8: | |
51 | mode = "DI"; | |
52 | break; | |
53 | default: | |
54 | internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size); | |
55 | } | |
56 | ||
57 | return mode; | |
58 | } | |
59 | ||
60 | /* See compile-internal.h. */ | |
61 | ||
8f84fb0e | 62 | std::string |
bb2ec1b3 TT |
63 | c_get_range_decl_name (const struct dynamic_prop *prop) |
64 | { | |
8f84fb0e | 65 | return string_printf ("__gdb_prop_%s", host_address_to_string (prop)); |
bb2ec1b3 TT |
66 | } |
67 | ||
68 | \f | |
69 | ||
bb2ec1b3 TT |
70 | /* Helper function for c_get_compile_context. Open the GCC front-end |
71 | shared library and return the symbol specified by the current | |
72 | GCC_C_FE_CONTEXT. */ | |
73 | ||
74 | static gcc_c_fe_context_function * | |
75 | load_libcc (void) | |
76 | { | |
bb2ec1b3 TT |
77 | gcc_c_fe_context_function *func; |
78 | ||
79 | /* gdb_dlopen will call error () on an error, so no need to check | |
80 | value. */ | |
0e8621a0 | 81 | gdb_dlhandle_up handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC)); |
bb2ec1b3 TT |
82 | func = (gcc_c_fe_context_function *) gdb_dlsym (handle, |
83 | STRINGIFY (GCC_C_FE_CONTEXT)); | |
84 | ||
85 | if (func == NULL) | |
86 | error (_("could not find symbol %s in library %s"), | |
87 | STRINGIFY (GCC_C_FE_CONTEXT), | |
88 | STRINGIFY (GCC_C_FE_LIBCC)); | |
0e8621a0 TT |
89 | |
90 | /* Leave the library open. */ | |
91 | handle.release (); | |
bb2ec1b3 TT |
92 | return func; |
93 | } | |
94 | ||
95 | /* Return the compile instance associated with the current context. | |
96 | This function calls the symbol returned from the load_libcc | |
97 | function. This will provide the gcc_c_context. */ | |
98 | ||
99 | struct compile_instance * | |
100 | c_get_compile_context (void) | |
101 | { | |
102 | static gcc_c_fe_context_function *func; | |
103 | ||
104 | struct gcc_c_context *context; | |
105 | ||
106 | if (func == NULL) | |
107 | { | |
108 | func = load_libcc (); | |
109 | gdb_assert (func != NULL); | |
110 | } | |
111 | ||
112 | context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0); | |
113 | if (context == NULL) | |
114 | error (_("The loaded version of GCC does not support the required version " | |
115 | "of the API.")); | |
116 | ||
117 | return new_compile_instance (context); | |
118 | } | |
119 | ||
120 | \f | |
121 | ||
122 | /* Write one macro definition. */ | |
123 | ||
124 | static void | |
125 | print_one_macro (const char *name, const struct macro_definition *macro, | |
126 | struct macro_source_file *source, int line, | |
14bc53a8 | 127 | ui_file *file) |
bb2ec1b3 | 128 | { |
bb2ec1b3 TT |
129 | /* Don't print command-line defines. They will be supplied another |
130 | way. */ | |
131 | if (line == 0) | |
132 | return; | |
133 | ||
3a9558c4 JK |
134 | /* None of -Wno-builtin-macro-redefined, #undef first |
135 | or plain #define of the same value would avoid a warning. */ | |
136 | fprintf_filtered (file, "#ifndef %s\n# define %s", name, name); | |
bb2ec1b3 TT |
137 | |
138 | if (macro->kind == macro_function_like) | |
139 | { | |
140 | int i; | |
141 | ||
142 | fputs_filtered ("(", file); | |
143 | for (i = 0; i < macro->argc; i++) | |
144 | { | |
145 | fputs_filtered (macro->argv[i], file); | |
146 | if (i + 1 < macro->argc) | |
147 | fputs_filtered (", ", file); | |
148 | } | |
149 | fputs_filtered (")", file); | |
150 | } | |
151 | ||
3a9558c4 | 152 | fprintf_filtered (file, " %s\n#endif\n", macro->replacement); |
bb2ec1b3 TT |
153 | } |
154 | ||
155 | /* Write macro definitions at PC to FILE. */ | |
156 | ||
157 | static void | |
158 | write_macro_definitions (const struct block *block, CORE_ADDR pc, | |
159 | struct ui_file *file) | |
160 | { | |
f6c2623e | 161 | gdb::unique_xmalloc_ptr<struct macro_scope> scope; |
bb2ec1b3 TT |
162 | |
163 | if (block != NULL) | |
164 | scope = sal_macro_scope (find_pc_line (pc, 0)); | |
165 | else | |
166 | scope = default_macro_scope (); | |
167 | if (scope == NULL) | |
168 | scope = user_macro_scope (); | |
169 | ||
170 | if (scope != NULL && scope->file != NULL && scope->file->table != NULL) | |
14bc53a8 PA |
171 | { |
172 | macro_for_each_in_scope (scope->file, scope->line, | |
173 | [&] (const char *name, | |
174 | const macro_definition *macro, | |
175 | macro_source_file *source, | |
176 | int line) | |
177 | { | |
178 | print_one_macro (name, macro, source, line, file); | |
179 | }); | |
180 | } | |
bb2ec1b3 TT |
181 | } |
182 | ||
183 | /* Helper function to construct a header scope for a block of code. | |
184 | Takes a scope argument which selects the correct header to | |
185 | insert into BUF. */ | |
186 | ||
187 | static void | |
188 | add_code_header (enum compile_i_scope_types type, struct ui_file *buf) | |
189 | { | |
190 | switch (type) | |
191 | { | |
192 | case COMPILE_I_SIMPLE_SCOPE: | |
193 | fputs_unfiltered ("void " | |
194 | GCC_FE_WRAPPER_FUNCTION | |
195 | " (struct " | |
196 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
197 | " *" | |
198 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
199 | ") {\n", | |
200 | buf); | |
201 | break; | |
36de76f9 JK |
202 | case COMPILE_I_PRINT_ADDRESS_SCOPE: |
203 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
204 | /* <string.h> is needed for a memcpy call below. */ | |
205 | fputs_unfiltered ("#include <string.h>\n" | |
206 | "void " | |
207 | GCC_FE_WRAPPER_FUNCTION | |
208 | " (struct " | |
209 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
210 | " *" | |
211 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
212 | ", " | |
213 | COMPILE_I_PRINT_OUT_ARG_TYPE | |
214 | " " | |
215 | COMPILE_I_PRINT_OUT_ARG | |
216 | ") {\n", | |
217 | buf); | |
218 | break; | |
219 | ||
bb2ec1b3 TT |
220 | case COMPILE_I_RAW_SCOPE: |
221 | break; | |
222 | default: | |
223 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
224 | } | |
225 | } | |
226 | ||
227 | /* Helper function to construct a footer scope for a block of code. | |
228 | Takes a scope argument which selects the correct footer to | |
229 | insert into BUF. */ | |
230 | ||
231 | static void | |
232 | add_code_footer (enum compile_i_scope_types type, struct ui_file *buf) | |
233 | { | |
234 | switch (type) | |
235 | { | |
236 | case COMPILE_I_SIMPLE_SCOPE: | |
36de76f9 JK |
237 | case COMPILE_I_PRINT_ADDRESS_SCOPE: |
238 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
bb2ec1b3 TT |
239 | fputs_unfiltered ("}\n", buf); |
240 | break; | |
241 | case COMPILE_I_RAW_SCOPE: | |
242 | break; | |
243 | default: | |
244 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
245 | } | |
246 | } | |
247 | ||
248 | /* Generate a structure holding all the registers used by the function | |
249 | we're generating. */ | |
250 | ||
251 | static void | |
252 | generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, | |
253 | const unsigned char *registers_used) | |
254 | { | |
255 | int i; | |
256 | int seen = 0; | |
257 | ||
258 | fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n", | |
259 | stream); | |
260 | ||
261 | if (registers_used != NULL) | |
262 | for (i = 0; i < gdbarch_num_regs (gdbarch); ++i) | |
263 | { | |
264 | if (registers_used[i]) | |
265 | { | |
266 | struct type *regtype = check_typedef (register_type (gdbarch, i)); | |
8f84fb0e | 267 | std::string regname = compile_register_name_mangled (gdbarch, i); |
bb2ec1b3 TT |
268 | |
269 | seen = 1; | |
270 | ||
271 | /* You might think we could use type_print here. However, | |
272 | target descriptions often use types with names like | |
273 | "int64_t", which may not be defined in the inferior | |
274 | (and in any case would not be looked up due to the | |
275 | #pragma business). So, we take a much simpler | |
276 | approach: for pointer- or integer-typed registers, emit | |
277 | the field in the most direct way; and for other | |
278 | register types (typically flags or vectors), emit a | |
279 | maximally-aligned array of the correct size. */ | |
280 | ||
281 | fputs_unfiltered (" ", stream); | |
282 | switch (TYPE_CODE (regtype)) | |
283 | { | |
284 | case TYPE_CODE_PTR: | |
8f84fb0e TT |
285 | fprintf_filtered (stream, "__gdb_uintptr %s", |
286 | regname.c_str ()); | |
bb2ec1b3 TT |
287 | break; |
288 | ||
289 | case TYPE_CODE_INT: | |
290 | { | |
291 | const char *mode | |
292 | = c_get_mode_for_size (TYPE_LENGTH (regtype)); | |
293 | ||
294 | if (mode != NULL) | |
295 | { | |
296 | if (TYPE_UNSIGNED (regtype)) | |
297 | fputs_unfiltered ("unsigned ", stream); | |
298 | fprintf_unfiltered (stream, | |
299 | "int %s" | |
300 | " __attribute__ ((__mode__(__%s__)))", | |
8f84fb0e | 301 | regname.c_str (), |
bb2ec1b3 TT |
302 | mode); |
303 | break; | |
304 | } | |
305 | } | |
306 | ||
307 | /* Fall through. */ | |
308 | ||
309 | default: | |
310 | fprintf_unfiltered (stream, | |
311 | " unsigned char %s[%d]" | |
312 | " __attribute__((__aligned__(" | |
313 | "__BIGGEST_ALIGNMENT__)))", | |
8f84fb0e | 314 | regname.c_str (), |
bb2ec1b3 TT |
315 | TYPE_LENGTH (regtype)); |
316 | } | |
317 | fputs_unfiltered (";\n", stream); | |
bb2ec1b3 TT |
318 | } |
319 | } | |
320 | ||
321 | if (!seen) | |
322 | fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n", | |
323 | stream); | |
324 | ||
325 | fputs_unfiltered ("};\n\n", stream); | |
326 | } | |
327 | ||
328 | /* Take the source code provided by the user with the 'compile' | |
329 | command, and compute the additional wrapping, macro, variable and | |
330 | register operations needed. INPUT is the source code derived from | |
331 | the 'compile' command, GDBARCH is the architecture to use when | |
332 | computing above, EXPR_BLOCK denotes the block relevant contextually | |
333 | to the inferior when the expression was created, and EXPR_PC | |
334 | indicates the value of $PC. */ | |
335 | ||
aaee65ae | 336 | std::string |
bb2ec1b3 TT |
337 | c_compute_program (struct compile_instance *inst, |
338 | const char *input, | |
339 | struct gdbarch *gdbarch, | |
340 | const struct block *expr_block, | |
341 | CORE_ADDR expr_pc) | |
342 | { | |
bb2ec1b3 TT |
343 | struct compile_c_instance *context = (struct compile_c_instance *) inst; |
344 | ||
d7e74731 PA |
345 | string_file buf; |
346 | string_file var_stream; | |
bb2ec1b3 | 347 | |
d7e74731 | 348 | write_macro_definitions (expr_block, expr_pc, &buf); |
bb2ec1b3 TT |
349 | |
350 | /* Do not generate local variable information for "raw" | |
351 | compilations. In this case we aren't emitting our own function | |
352 | and the user's code may only refer to globals. */ | |
353 | if (inst->scope != COMPILE_I_RAW_SCOPE) | |
354 | { | |
bb2ec1b3 TT |
355 | int i; |
356 | ||
357 | /* Generate the code to compute variable locations, but do it | |
358 | before generating the function header, so we can define the | |
359 | register struct before the function body. This requires a | |
360 | temporary stream. */ | |
bd923e51 KS |
361 | gdb::unique_xmalloc_ptr<unsigned char> registers_used |
362 | = generate_c_for_variable_locations (context, var_stream, gdbarch, | |
363 | expr_block, expr_pc); | |
bb2ec1b3 | 364 | |
d7e74731 PA |
365 | buf.puts ("typedef unsigned int" |
366 | " __attribute__ ((__mode__(__pointer__)))" | |
367 | " __gdb_uintptr;\n"); | |
368 | buf.puts ("typedef int" | |
369 | " __attribute__ ((__mode__(__pointer__)))" | |
370 | " __gdb_intptr;\n"); | |
bb2ec1b3 | 371 | |
bb2b33b9 | 372 | /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */ |
bb2ec1b3 TT |
373 | for (i = 0; i < 4; ++i) |
374 | { | |
375 | const char *mode = c_get_mode_for_size (1 << i); | |
376 | ||
377 | gdb_assert (mode != NULL); | |
d7e74731 PA |
378 | buf.printf ("typedef int" |
379 | " __attribute__ ((__mode__(__%s__)))" | |
380 | " __gdb_int_%s;\n", | |
381 | mode, mode); | |
bb2ec1b3 | 382 | } |
3a9558c4 | 383 | |
bd923e51 | 384 | generate_register_struct (&buf, gdbarch, registers_used.get ()); |
bb2ec1b3 TT |
385 | } |
386 | ||
d7e74731 | 387 | add_code_header (inst->scope, &buf); |
bb2ec1b3 | 388 | |
36de76f9 JK |
389 | if (inst->scope == COMPILE_I_SIMPLE_SCOPE |
390 | || inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE | |
391 | || inst->scope == COMPILE_I_PRINT_VALUE_SCOPE) | |
bb2ec1b3 | 392 | { |
d7e74731 PA |
393 | buf.write (var_stream.c_str (), var_stream.size ()); |
394 | buf.puts ("#pragma GCC user_expression\n"); | |
bb2ec1b3 TT |
395 | } |
396 | ||
397 | /* The user expression has to be in its own scope, so that "extern" | |
398 | works properly. Otherwise gcc thinks that the "extern" | |
399 | declaration is in the same scope as the declaration provided by | |
400 | gdb. */ | |
401 | if (inst->scope != COMPILE_I_RAW_SCOPE) | |
d7e74731 | 402 | buf.puts ("{\n"); |
bb2ec1b3 | 403 | |
d7e74731 | 404 | buf.puts ("#line 1 \"gdb command line\"\n"); |
36de76f9 JK |
405 | |
406 | switch (inst->scope) | |
407 | { | |
408 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
409 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
d7e74731 | 410 | buf.printf ( |
36de76f9 JK |
411 | "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n" |
412 | "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n" | |
413 | "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" COMPILE_I_EXPR_VAL ",\n" | |
414 | "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n" | |
415 | , input, input, | |
416 | (inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE | |
417 | ? "&" : "")); | |
418 | break; | |
419 | default: | |
d7e74731 | 420 | buf.puts (input); |
36de76f9 JK |
421 | break; |
422 | } | |
423 | ||
d7e74731 | 424 | buf.puts ("\n"); |
bb2ec1b3 TT |
425 | |
426 | /* For larger user expressions the automatic semicolons may be | |
427 | confusing. */ | |
428 | if (strchr (input, '\n') == NULL) | |
d7e74731 | 429 | buf.puts (";\n"); |
bb2ec1b3 TT |
430 | |
431 | if (inst->scope != COMPILE_I_RAW_SCOPE) | |
d7e74731 | 432 | buf.puts ("}\n"); |
bb2ec1b3 | 433 | |
d7e74731 PA |
434 | add_code_footer (inst->scope, &buf); |
435 | return std::move (buf.string ()); | |
bb2ec1b3 | 436 | } |