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 | ||
9cdfd9a2 | 99 | compile_instance * |
bb2ec1b3 TT |
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 | ||
9cdfd9a2 | 117 | return new compile_c_instance (context); |
bb2ec1b3 TT |
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 | ||
bb2ec1b3 TT |
183 | /* Generate a structure holding all the registers used by the function |
184 | we're generating. */ | |
185 | ||
186 | static void | |
187 | generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, | |
188 | const unsigned char *registers_used) | |
189 | { | |
190 | int i; | |
191 | int seen = 0; | |
192 | ||
193 | fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n", | |
194 | stream); | |
195 | ||
196 | if (registers_used != NULL) | |
197 | for (i = 0; i < gdbarch_num_regs (gdbarch); ++i) | |
198 | { | |
199 | if (registers_used[i]) | |
200 | { | |
201 | struct type *regtype = check_typedef (register_type (gdbarch, i)); | |
8f84fb0e | 202 | std::string regname = compile_register_name_mangled (gdbarch, i); |
bb2ec1b3 TT |
203 | |
204 | seen = 1; | |
205 | ||
206 | /* You might think we could use type_print here. However, | |
207 | target descriptions often use types with names like | |
208 | "int64_t", which may not be defined in the inferior | |
209 | (and in any case would not be looked up due to the | |
210 | #pragma business). So, we take a much simpler | |
211 | approach: for pointer- or integer-typed registers, emit | |
212 | the field in the most direct way; and for other | |
213 | register types (typically flags or vectors), emit a | |
214 | maximally-aligned array of the correct size. */ | |
215 | ||
216 | fputs_unfiltered (" ", stream); | |
217 | switch (TYPE_CODE (regtype)) | |
218 | { | |
219 | case TYPE_CODE_PTR: | |
8f84fb0e TT |
220 | fprintf_filtered (stream, "__gdb_uintptr %s", |
221 | regname.c_str ()); | |
bb2ec1b3 TT |
222 | break; |
223 | ||
224 | case TYPE_CODE_INT: | |
225 | { | |
226 | const char *mode | |
227 | = c_get_mode_for_size (TYPE_LENGTH (regtype)); | |
228 | ||
229 | if (mode != NULL) | |
230 | { | |
231 | if (TYPE_UNSIGNED (regtype)) | |
232 | fputs_unfiltered ("unsigned ", stream); | |
233 | fprintf_unfiltered (stream, | |
234 | "int %s" | |
235 | " __attribute__ ((__mode__(__%s__)))", | |
8f84fb0e | 236 | regname.c_str (), |
bb2ec1b3 TT |
237 | mode); |
238 | break; | |
239 | } | |
240 | } | |
241 | ||
242 | /* Fall through. */ | |
243 | ||
244 | default: | |
245 | fprintf_unfiltered (stream, | |
246 | " unsigned char %s[%d]" | |
247 | " __attribute__((__aligned__(" | |
248 | "__BIGGEST_ALIGNMENT__)))", | |
8f84fb0e | 249 | regname.c_str (), |
bb2ec1b3 TT |
250 | TYPE_LENGTH (regtype)); |
251 | } | |
252 | fputs_unfiltered (";\n", stream); | |
bb2ec1b3 TT |
253 | } |
254 | } | |
255 | ||
256 | if (!seen) | |
257 | fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n", | |
258 | stream); | |
259 | ||
260 | fputs_unfiltered ("};\n\n", stream); | |
261 | } | |
262 | ||
ad3a68e9 | 263 | /* C-language policy to emit a push user expression pragma into BUF. */ |
bb2ec1b3 | 264 | |
ad3a68e9 | 265 | struct c_push_user_expression |
bb2ec1b3 | 266 | { |
ad3a68e9 KS |
267 | void push_user_expression (struct ui_file *buf) |
268 | { | |
269 | fputs_unfiltered ("#pragma GCC user_expression\n", buf); | |
270 | } | |
271 | }; | |
bb2ec1b3 | 272 | |
ad3a68e9 KS |
273 | /* C-language policy to emit a pop user expression pragma into BUF. |
274 | For C, this is a nop. */ | |
bb2ec1b3 | 275 | |
ad3a68e9 KS |
276 | struct pop_user_expression_nop |
277 | { | |
278 | void pop_user_expression (struct ui_file *buf) | |
279 | { | |
280 | /* Nothing to do. */ | |
281 | } | |
282 | }; | |
283 | ||
284 | /* C-language policy to construct a code header for a block of code. | |
285 | Takes a scope TYPE argument which selects the correct header to | |
286 | insert into BUF. */ | |
bb2ec1b3 | 287 | |
ad3a68e9 KS |
288 | struct c_add_code_header |
289 | { | |
290 | void add_code_header (enum compile_i_scope_types type, struct ui_file *buf) | |
291 | { | |
292 | switch (type) | |
293 | { | |
294 | case COMPILE_I_SIMPLE_SCOPE: | |
295 | fputs_unfiltered ("void " | |
296 | GCC_FE_WRAPPER_FUNCTION | |
297 | " (struct " | |
298 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
299 | " *" | |
300 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
301 | ") {\n", | |
302 | buf); | |
303 | break; | |
304 | ||
305 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
306 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
307 | /* <string.h> is needed for a memcpy call below. */ | |
308 | fputs_unfiltered ("#include <string.h>\n" | |
309 | "void " | |
310 | GCC_FE_WRAPPER_FUNCTION | |
311 | " (struct " | |
312 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
313 | " *" | |
314 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
315 | ", " | |
316 | COMPILE_I_PRINT_OUT_ARG_TYPE | |
317 | " " | |
318 | COMPILE_I_PRINT_OUT_ARG | |
319 | ") {\n", | |
320 | buf); | |
321 | break; | |
322 | ||
323 | case COMPILE_I_RAW_SCOPE: | |
324 | break; | |
325 | ||
326 | default: | |
327 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
328 | } | |
329 | } | |
330 | }; | |
bb2ec1b3 | 331 | |
ad3a68e9 KS |
332 | /* C-language policy to construct a code footer for a block of code. |
333 | Takes a scope TYPE which selects the correct footer to insert into BUF. */ | |
bb2ec1b3 | 334 | |
ad3a68e9 KS |
335 | struct c_add_code_footer |
336 | { | |
337 | void add_code_footer (enum compile_i_scope_types type, struct ui_file *buf) | |
338 | { | |
339 | switch (type) | |
340 | { | |
341 | case COMPILE_I_SIMPLE_SCOPE: | |
342 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
343 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
344 | fputs_unfiltered ("}\n", buf); | |
345 | break; | |
3a9558c4 | 346 | |
ad3a68e9 KS |
347 | case COMPILE_I_RAW_SCOPE: |
348 | break; | |
bb2ec1b3 | 349 | |
ad3a68e9 KS |
350 | default: |
351 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
352 | } | |
353 | } | |
354 | }; | |
bb2ec1b3 | 355 | |
ad3a68e9 KS |
356 | /* C-language policy to emit the user code snippet INPUT into BUF based on the |
357 | scope TYPE. */ | |
bb2ec1b3 | 358 | |
ad3a68e9 KS |
359 | struct c_add_input |
360 | { | |
361 | void add_input (enum compile_i_scope_types type, const char *input, | |
362 | struct ui_file *buf) | |
363 | { | |
364 | switch (type) | |
365 | { | |
366 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
367 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
368 | fprintf_unfiltered (buf, | |
369 | "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n" | |
370 | "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n" | |
371 | "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" | |
372 | COMPILE_I_EXPR_VAL ",\n" | |
373 | "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n" | |
374 | , input, input, | |
375 | (type == COMPILE_I_PRINT_ADDRESS_SCOPE | |
376 | ? "&" : "")); | |
377 | break; | |
378 | ||
379 | default: | |
380 | fputs_unfiltered (input, buf); | |
381 | break; | |
382 | } | |
383 | fputs_unfiltered ("\n", buf); | |
384 | } | |
385 | }; | |
bb2ec1b3 | 386 | |
ad3a68e9 | 387 | /* A host class representing a compile program. |
36de76f9 | 388 | |
ad3a68e9 KS |
389 | CompileInstanceType is the type of the compile_instance for the |
390 | language. | |
391 | ||
392 | PushUserExpressionPolicy and PopUserExpressionPolicy are used to | |
393 | push and pop user expression pragmas to the compile plug-in. | |
394 | ||
395 | AddCodeHeaderPolicy and AddCodeFooterPolicy are used to add the appropriate | |
396 | code header and footer, respectively. | |
36de76f9 | 397 | |
ad3a68e9 | 398 | AddInputPolicy adds the actual user code. */ |
bb2ec1b3 | 399 | |
ad3a68e9 KS |
400 | template <class CompileInstanceType, class PushUserExpressionPolicy, |
401 | class PopUserExpressionPolicy, class AddCodeHeaderPolicy, | |
402 | class AddCodeFooterPolicy, class AddInputPolicy> | |
403 | class compile_program | |
404 | : private PushUserExpressionPolicy, private PopUserExpressionPolicy, | |
405 | private AddCodeHeaderPolicy, private AddCodeFooterPolicy, | |
406 | private AddInputPolicy | |
407 | { | |
408 | public: | |
409 | ||
410 | /* Construct a compile_program using the compiler instance INST | |
411 | using the architecture given by GDBARCH. */ | |
412 | compile_program (CompileInstanceType *inst, struct gdbarch *gdbarch) | |
413 | : m_instance (inst), m_arch (gdbarch) | |
414 | { | |
415 | } | |
416 | ||
417 | /* Take the source code provided by the user with the 'compile' | |
418 | command and compute the additional wrapping, macro, variable and | |
419 | register operations needed. INPUT is the source code derived from | |
420 | the 'compile' command, EXPR_BLOCK denotes the block relevant contextually | |
421 | to the inferior when the expression was created, and EXPR_PC | |
422 | indicates the value of $PC. | |
423 | ||
424 | Returns the text of the program to compile. */ | |
425 | std::string compute (const char *input, const struct block *expr_block, | |
426 | CORE_ADDR expr_pc) | |
427 | { | |
428 | string_file var_stream; | |
429 | string_file buf; | |
430 | ||
431 | /* Do not generate local variable information for "raw" | |
432 | compilations. In this case we aren't emitting our own function | |
433 | and the user's code may only refer to globals. */ | |
434 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
435 | { | |
436 | /* Generate the code to compute variable locations, but do it | |
437 | before generating the function header, so we can define the | |
438 | register struct before the function body. This requires a | |
439 | temporary stream. */ | |
440 | gdb::unique_xmalloc_ptr<unsigned char> registers_used | |
441 | = generate_c_for_variable_locations (m_instance, var_stream, m_arch, | |
442 | expr_block, expr_pc); | |
443 | ||
444 | buf.puts ("typedef unsigned int" | |
445 | " __attribute__ ((__mode__(__pointer__)))" | |
446 | " __gdb_uintptr;\n"); | |
447 | buf.puts ("typedef int" | |
448 | " __attribute__ ((__mode__(__pointer__)))" | |
449 | " __gdb_intptr;\n"); | |
450 | ||
451 | /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */ | |
452 | for (int i = 0; i < 4; ++i) | |
453 | { | |
454 | const char *mode = c_get_mode_for_size (1 << i); | |
455 | ||
456 | gdb_assert (mode != NULL); | |
457 | buf.printf ("typedef int" | |
458 | " __attribute__ ((__mode__(__%s__)))" | |
459 | " __gdb_int_%s;\n", | |
460 | mode, mode); | |
461 | } | |
462 | ||
463 | generate_register_struct (&buf, m_arch, registers_used.get ()); | |
464 | } | |
465 | ||
466 | AddCodeHeaderPolicy::add_code_header (m_instance->scope (), &buf); | |
467 | ||
468 | if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE | |
469 | || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE | |
470 | || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE) | |
471 | { | |
472 | buf.write (var_stream.c_str (), var_stream.size ()); | |
473 | PushUserExpressionPolicy::push_user_expression (&buf); | |
474 | } | |
bb2ec1b3 | 475 | |
ad3a68e9 KS |
476 | write_macro_definitions (expr_block, expr_pc, &buf); |
477 | ||
478 | /* The user expression has to be in its own scope, so that "extern" | |
479 | works properly. Otherwise gcc thinks that the "extern" | |
480 | declaration is in the same scope as the declaration provided by | |
481 | gdb. */ | |
482 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
483 | buf.puts ("{\n"); | |
484 | ||
485 | buf.puts ("#line 1 \"gdb command line\"\n"); | |
486 | ||
487 | AddInputPolicy::add_input (m_instance->scope (), input, &buf); | |
488 | ||
489 | /* For larger user expressions the automatic semicolons may be | |
490 | confusing. */ | |
491 | if (strchr (input, '\n') == NULL) | |
492 | buf.puts (";\n"); | |
493 | ||
494 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
495 | buf.puts ("}\n"); | |
496 | ||
497 | if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE | |
498 | || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE | |
499 | || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE) | |
500 | PopUserExpressionPolicy::pop_user_expression (&buf); | |
501 | ||
502 | AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf); | |
503 | return buf.string (); | |
504 | } | |
505 | ||
506 | private: | |
507 | ||
508 | /* The compile instance to be used for compilation and | |
509 | type-conversion. */ | |
510 | CompileInstanceType *m_instance; | |
511 | ||
512 | /* The architecture to be used. */ | |
513 | struct gdbarch *m_arch; | |
514 | }; | |
515 | ||
516 | /* Type used for C program computations. */ | |
517 | ||
518 | typedef compile_program<compile_c_instance, | |
519 | c_push_user_expression, pop_user_expression_nop, | |
520 | c_add_code_header, c_add_code_footer, | |
521 | c_add_input> c_compile_program; | |
522 | ||
523 | /* The la_compute_program method for C. */ | |
524 | ||
525 | std::string | |
526 | c_compute_program (compile_instance *inst, | |
527 | const char *input, | |
528 | struct gdbarch *gdbarch, | |
529 | const struct block *expr_block, | |
530 | CORE_ADDR expr_pc) | |
531 | { | |
532 | compile_c_instance *c_inst = static_cast<compile_c_instance *> (inst); | |
533 | c_compile_program program (c_inst, gdbarch); | |
bb2ec1b3 | 534 | |
ad3a68e9 | 535 | return program.compute (input, expr_block, expr_pc); |
bb2ec1b3 | 536 | } |