Commit | Line | Data |
---|---|---|
078a0207 | 1 | /* C/C++ language support for compilation. |
bb2ec1b3 | 2 | |
42a4f53d | 3 | Copyright (C) 2014-2019 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" |
078a0207 | 23 | #include "compile-cplus.h" |
bb2ec1b3 | 24 | #include "compile.h" |
bb2ec1b3 TT |
25 | #include "c-lang.h" |
26 | #include "macrotab.h" | |
27 | #include "macroscope.h" | |
28 | #include "regcache.h" | |
268a13a5 | 29 | #include "gdbsupport/function-view.h" |
2d41fa11 | 30 | #include "gdbsupport/gdb-dlfcn.h" |
268a13a5 | 31 | #include "gdbsupport/preprocessor.h" |
0d12e84c | 32 | #include "gdbarch.h" |
bb2ec1b3 TT |
33 | |
34 | /* See compile-internal.h. */ | |
35 | ||
36 | const char * | |
37 | c_get_mode_for_size (int size) | |
38 | { | |
39 | const char *mode = NULL; | |
40 | ||
41 | switch (size) | |
42 | { | |
43 | case 1: | |
44 | mode = "QI"; | |
45 | break; | |
46 | case 2: | |
47 | mode = "HI"; | |
48 | break; | |
49 | case 4: | |
50 | mode = "SI"; | |
51 | break; | |
52 | case 8: | |
53 | mode = "DI"; | |
54 | break; | |
55 | default: | |
56 | internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size); | |
57 | } | |
58 | ||
59 | return mode; | |
60 | } | |
61 | ||
62 | /* See compile-internal.h. */ | |
63 | ||
8f84fb0e | 64 | std::string |
bb2ec1b3 TT |
65 | c_get_range_decl_name (const struct dynamic_prop *prop) |
66 | { | |
8f84fb0e | 67 | return string_printf ("__gdb_prop_%s", host_address_to_string (prop)); |
bb2ec1b3 TT |
68 | } |
69 | ||
70 | \f | |
71 | ||
078a0207 KS |
72 | /* Load the plug-in library FE_LIBCC and return the initialization function |
73 | FE_CONTEXT. */ | |
bb2ec1b3 | 74 | |
078a0207 KS |
75 | template <typename FUNCTYPE> |
76 | FUNCTYPE * | |
77 | load_libcompile (const char *fe_libcc, const char *fe_context) | |
bb2ec1b3 | 78 | { |
078a0207 | 79 | FUNCTYPE *func; |
bb2ec1b3 | 80 | |
078a0207 KS |
81 | /* gdb_dlopen will call error () on an error, so no need to check |
82 | value. */ | |
83 | gdb_dlhandle_up handle = gdb_dlopen (fe_libcc); | |
84 | func = (FUNCTYPE *) gdb_dlsym (handle, fe_context); | |
bb2ec1b3 TT |
85 | |
86 | if (func == NULL) | |
078a0207 | 87 | error (_("could not find symbol %s in library %s"), fe_context, fe_libcc); |
0e8621a0 TT |
88 | |
89 | /* Leave the library open. */ | |
90 | handle.release (); | |
bb2ec1b3 TT |
91 | return func; |
92 | } | |
93 | ||
94 | /* Return the compile instance associated with the current context. | |
078a0207 KS |
95 | This function calls the symbol returned from the load_libcompile |
96 | function. FE_LIBCC is the library to load. BASE_VERSION is the | |
97 | base compile plug-in version we support. API_VERSION is the | |
98 | API version supported. */ | |
bb2ec1b3 | 99 | |
078a0207 KS |
100 | template <typename INSTTYPE, typename FUNCTYPE, typename CTXTYPE, |
101 | typename BASE_VERSION_TYPE, typename API_VERSION_TYPE> | |
9cdfd9a2 | 102 | compile_instance * |
078a0207 KS |
103 | get_compile_context (const char *fe_libcc, const char *fe_context, |
104 | BASE_VERSION_TYPE base_version, | |
105 | API_VERSION_TYPE api_version) | |
bb2ec1b3 | 106 | { |
078a0207 KS |
107 | static FUNCTYPE *func; |
108 | static CTXTYPE *context; | |
bb2ec1b3 TT |
109 | |
110 | if (func == NULL) | |
111 | { | |
078a0207 | 112 | func = load_libcompile<FUNCTYPE> (fe_libcc, fe_context); |
bb2ec1b3 TT |
113 | gdb_assert (func != NULL); |
114 | } | |
115 | ||
078a0207 | 116 | context = (*func) (base_version, api_version); |
bb2ec1b3 TT |
117 | if (context == NULL) |
118 | error (_("The loaded version of GCC does not support the required version " | |
119 | "of the API.")); | |
120 | ||
078a0207 KS |
121 | return new INSTTYPE (context); |
122 | } | |
123 | ||
124 | /* A C-language implementation of get_compile_context. */ | |
125 | ||
126 | compile_instance * | |
127 | c_get_compile_context () | |
128 | { | |
129 | return get_compile_context | |
130 | <compile_c_instance, gcc_c_fe_context_function, gcc_c_context, | |
131 | gcc_base_api_version, gcc_c_api_version> | |
132 | (STRINGIFY (GCC_C_FE_LIBCC), STRINGIFY (GCC_C_FE_CONTEXT), | |
133 | GCC_FE_VERSION_0, GCC_C_FE_VERSION_0); | |
134 | } | |
135 | ||
136 | /* A C++-language implementation of get_compile_context. */ | |
137 | ||
138 | compile_instance * | |
139 | cplus_get_compile_context () | |
140 | { | |
141 | return get_compile_context | |
142 | <compile_cplus_instance, gcc_cp_fe_context_function, gcc_cp_context, | |
143 | gcc_base_api_version, gcc_cp_api_version> | |
144 | (STRINGIFY (GCC_CP_FE_LIBCC), STRINGIFY (GCC_CP_FE_CONTEXT), | |
145 | GCC_FE_VERSION_0, GCC_CP_FE_VERSION_0); | |
bb2ec1b3 TT |
146 | } |
147 | ||
148 | \f | |
149 | ||
150 | /* Write one macro definition. */ | |
151 | ||
152 | static void | |
153 | print_one_macro (const char *name, const struct macro_definition *macro, | |
154 | struct macro_source_file *source, int line, | |
14bc53a8 | 155 | ui_file *file) |
bb2ec1b3 | 156 | { |
bb2ec1b3 TT |
157 | /* Don't print command-line defines. They will be supplied another |
158 | way. */ | |
159 | if (line == 0) | |
160 | return; | |
161 | ||
3a9558c4 JK |
162 | /* None of -Wno-builtin-macro-redefined, #undef first |
163 | or plain #define of the same value would avoid a warning. */ | |
164 | fprintf_filtered (file, "#ifndef %s\n# define %s", name, name); | |
bb2ec1b3 TT |
165 | |
166 | if (macro->kind == macro_function_like) | |
167 | { | |
168 | int i; | |
169 | ||
170 | fputs_filtered ("(", file); | |
171 | for (i = 0; i < macro->argc; i++) | |
172 | { | |
173 | fputs_filtered (macro->argv[i], file); | |
174 | if (i + 1 < macro->argc) | |
175 | fputs_filtered (", ", file); | |
176 | } | |
177 | fputs_filtered (")", file); | |
178 | } | |
179 | ||
3a9558c4 | 180 | fprintf_filtered (file, " %s\n#endif\n", macro->replacement); |
bb2ec1b3 TT |
181 | } |
182 | ||
183 | /* Write macro definitions at PC to FILE. */ | |
184 | ||
185 | static void | |
186 | write_macro_definitions (const struct block *block, CORE_ADDR pc, | |
187 | struct ui_file *file) | |
188 | { | |
f6c2623e | 189 | gdb::unique_xmalloc_ptr<struct macro_scope> scope; |
bb2ec1b3 TT |
190 | |
191 | if (block != NULL) | |
192 | scope = sal_macro_scope (find_pc_line (pc, 0)); | |
193 | else | |
194 | scope = default_macro_scope (); | |
195 | if (scope == NULL) | |
196 | scope = user_macro_scope (); | |
197 | ||
198 | if (scope != NULL && scope->file != NULL && scope->file->table != NULL) | |
14bc53a8 PA |
199 | { |
200 | macro_for_each_in_scope (scope->file, scope->line, | |
201 | [&] (const char *name, | |
202 | const macro_definition *macro, | |
203 | macro_source_file *source, | |
204 | int line) | |
205 | { | |
206 | print_one_macro (name, macro, source, line, file); | |
207 | }); | |
208 | } | |
bb2ec1b3 TT |
209 | } |
210 | ||
bb2ec1b3 TT |
211 | /* Generate a structure holding all the registers used by the function |
212 | we're generating. */ | |
213 | ||
214 | static void | |
215 | generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, | |
216 | const unsigned char *registers_used) | |
217 | { | |
218 | int i; | |
219 | int seen = 0; | |
220 | ||
221 | fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n", | |
222 | stream); | |
223 | ||
224 | if (registers_used != NULL) | |
225 | for (i = 0; i < gdbarch_num_regs (gdbarch); ++i) | |
226 | { | |
227 | if (registers_used[i]) | |
228 | { | |
229 | struct type *regtype = check_typedef (register_type (gdbarch, i)); | |
8f84fb0e | 230 | std::string regname = compile_register_name_mangled (gdbarch, i); |
bb2ec1b3 TT |
231 | |
232 | seen = 1; | |
233 | ||
234 | /* You might think we could use type_print here. However, | |
235 | target descriptions often use types with names like | |
236 | "int64_t", which may not be defined in the inferior | |
237 | (and in any case would not be looked up due to the | |
238 | #pragma business). So, we take a much simpler | |
239 | approach: for pointer- or integer-typed registers, emit | |
240 | the field in the most direct way; and for other | |
241 | register types (typically flags or vectors), emit a | |
242 | maximally-aligned array of the correct size. */ | |
243 | ||
244 | fputs_unfiltered (" ", stream); | |
245 | switch (TYPE_CODE (regtype)) | |
246 | { | |
247 | case TYPE_CODE_PTR: | |
8f84fb0e TT |
248 | fprintf_filtered (stream, "__gdb_uintptr %s", |
249 | regname.c_str ()); | |
bb2ec1b3 TT |
250 | break; |
251 | ||
252 | case TYPE_CODE_INT: | |
253 | { | |
254 | const char *mode | |
255 | = c_get_mode_for_size (TYPE_LENGTH (regtype)); | |
256 | ||
257 | if (mode != NULL) | |
258 | { | |
259 | if (TYPE_UNSIGNED (regtype)) | |
260 | fputs_unfiltered ("unsigned ", stream); | |
261 | fprintf_unfiltered (stream, | |
262 | "int %s" | |
263 | " __attribute__ ((__mode__(__%s__)))", | |
8f84fb0e | 264 | regname.c_str (), |
bb2ec1b3 TT |
265 | mode); |
266 | break; | |
267 | } | |
268 | } | |
269 | ||
270 | /* Fall through. */ | |
271 | ||
272 | default: | |
273 | fprintf_unfiltered (stream, | |
cc1defb1 | 274 | " unsigned char %s[%s]" |
bb2ec1b3 TT |
275 | " __attribute__((__aligned__(" |
276 | "__BIGGEST_ALIGNMENT__)))", | |
8f84fb0e | 277 | regname.c_str (), |
cc1defb1 | 278 | pulongest (TYPE_LENGTH (regtype))); |
bb2ec1b3 TT |
279 | } |
280 | fputs_unfiltered (";\n", stream); | |
bb2ec1b3 TT |
281 | } |
282 | } | |
283 | ||
284 | if (!seen) | |
285 | fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n", | |
286 | stream); | |
287 | ||
288 | fputs_unfiltered ("};\n\n", stream); | |
289 | } | |
290 | ||
ad3a68e9 | 291 | /* C-language policy to emit a push user expression pragma into BUF. */ |
bb2ec1b3 | 292 | |
ad3a68e9 | 293 | struct c_push_user_expression |
bb2ec1b3 | 294 | { |
ad3a68e9 KS |
295 | void push_user_expression (struct ui_file *buf) |
296 | { | |
297 | fputs_unfiltered ("#pragma GCC user_expression\n", buf); | |
298 | } | |
299 | }; | |
bb2ec1b3 | 300 | |
ad3a68e9 KS |
301 | /* C-language policy to emit a pop user expression pragma into BUF. |
302 | For C, this is a nop. */ | |
bb2ec1b3 | 303 | |
ad3a68e9 KS |
304 | struct pop_user_expression_nop |
305 | { | |
306 | void pop_user_expression (struct ui_file *buf) | |
307 | { | |
308 | /* Nothing to do. */ | |
309 | } | |
310 | }; | |
311 | ||
312 | /* C-language policy to construct a code header for a block of code. | |
313 | Takes a scope TYPE argument which selects the correct header to | |
314 | insert into BUF. */ | |
bb2ec1b3 | 315 | |
ad3a68e9 KS |
316 | struct c_add_code_header |
317 | { | |
318 | void add_code_header (enum compile_i_scope_types type, struct ui_file *buf) | |
319 | { | |
320 | switch (type) | |
321 | { | |
322 | case COMPILE_I_SIMPLE_SCOPE: | |
323 | fputs_unfiltered ("void " | |
324 | GCC_FE_WRAPPER_FUNCTION | |
325 | " (struct " | |
326 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
327 | " *" | |
328 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
329 | ") {\n", | |
330 | buf); | |
331 | break; | |
332 | ||
333 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
334 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
335 | /* <string.h> is needed for a memcpy call below. */ | |
336 | fputs_unfiltered ("#include <string.h>\n" | |
337 | "void " | |
338 | GCC_FE_WRAPPER_FUNCTION | |
339 | " (struct " | |
340 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
341 | " *" | |
342 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
343 | ", " | |
344 | COMPILE_I_PRINT_OUT_ARG_TYPE | |
345 | " " | |
346 | COMPILE_I_PRINT_OUT_ARG | |
347 | ") {\n", | |
348 | buf); | |
349 | break; | |
350 | ||
351 | case COMPILE_I_RAW_SCOPE: | |
352 | break; | |
353 | ||
354 | default: | |
355 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
356 | } | |
357 | } | |
358 | }; | |
bb2ec1b3 | 359 | |
ad3a68e9 KS |
360 | /* C-language policy to construct a code footer for a block of code. |
361 | Takes a scope TYPE which selects the correct footer to insert into BUF. */ | |
bb2ec1b3 | 362 | |
ad3a68e9 KS |
363 | struct c_add_code_footer |
364 | { | |
365 | void add_code_footer (enum compile_i_scope_types type, struct ui_file *buf) | |
366 | { | |
367 | switch (type) | |
368 | { | |
369 | case COMPILE_I_SIMPLE_SCOPE: | |
370 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
371 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
372 | fputs_unfiltered ("}\n", buf); | |
373 | break; | |
3a9558c4 | 374 | |
ad3a68e9 KS |
375 | case COMPILE_I_RAW_SCOPE: |
376 | break; | |
bb2ec1b3 | 377 | |
ad3a68e9 KS |
378 | default: |
379 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
380 | } | |
381 | } | |
382 | }; | |
bb2ec1b3 | 383 | |
ad3a68e9 KS |
384 | /* C-language policy to emit the user code snippet INPUT into BUF based on the |
385 | scope TYPE. */ | |
bb2ec1b3 | 386 | |
ad3a68e9 KS |
387 | struct c_add_input |
388 | { | |
389 | void add_input (enum compile_i_scope_types type, const char *input, | |
390 | struct ui_file *buf) | |
391 | { | |
392 | switch (type) | |
393 | { | |
394 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
395 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
396 | fprintf_unfiltered (buf, | |
397 | "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n" | |
398 | "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n" | |
399 | "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" | |
400 | COMPILE_I_EXPR_VAL ",\n" | |
401 | "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n" | |
402 | , input, input, | |
403 | (type == COMPILE_I_PRINT_ADDRESS_SCOPE | |
404 | ? "&" : "")); | |
405 | break; | |
406 | ||
407 | default: | |
408 | fputs_unfiltered (input, buf); | |
409 | break; | |
410 | } | |
411 | fputs_unfiltered ("\n", buf); | |
412 | } | |
413 | }; | |
bb2ec1b3 | 414 | |
078a0207 KS |
415 | /* C++-language policy to emit a push user expression pragma into |
416 | BUF. */ | |
417 | ||
418 | struct cplus_push_user_expression | |
419 | { | |
420 | void push_user_expression (struct ui_file *buf) | |
421 | { | |
422 | fputs_unfiltered ("#pragma GCC push_user_expression\n", buf); | |
423 | } | |
424 | }; | |
425 | ||
426 | /* C++-language policy to emit a pop user expression pragma into BUF. */ | |
427 | ||
428 | struct cplus_pop_user_expression | |
429 | { | |
430 | void pop_user_expression (struct ui_file *buf) | |
431 | { | |
432 | fputs_unfiltered ("#pragma GCC pop_user_expression\n", buf); | |
433 | } | |
434 | }; | |
435 | ||
436 | /* C++-language policy to construct a code header for a block of code. | |
437 | Takes a scope TYPE argument which selects the correct header to | |
438 | insert into BUF. */ | |
439 | ||
440 | struct cplus_add_code_header | |
441 | { | |
442 | void add_code_header (enum compile_i_scope_types type, struct ui_file *buf) | |
443 | { | |
444 | switch (type) | |
445 | { | |
446 | case COMPILE_I_SIMPLE_SCOPE: | |
447 | fputs_unfiltered ("void " | |
448 | GCC_FE_WRAPPER_FUNCTION | |
449 | " (struct " | |
450 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
451 | " *" | |
452 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
453 | ") {\n", | |
454 | buf); | |
455 | break; | |
456 | ||
457 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
458 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
459 | fputs_unfiltered ( | |
460 | "#include <cstring>\n" | |
461 | "#include <bits/move.h>\n" | |
462 | "void " | |
463 | GCC_FE_WRAPPER_FUNCTION | |
464 | " (struct " | |
465 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
466 | " *" | |
467 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
468 | ", " | |
469 | COMPILE_I_PRINT_OUT_ARG_TYPE | |
470 | " " | |
471 | COMPILE_I_PRINT_OUT_ARG | |
472 | ") {\n", | |
473 | buf); | |
474 | break; | |
475 | ||
476 | case COMPILE_I_RAW_SCOPE: | |
477 | break; | |
478 | ||
479 | default: | |
480 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
481 | } | |
482 | } | |
483 | }; | |
484 | ||
485 | /* C++-language policy to emit the user code snippet INPUT into BUF based on | |
486 | the scope TYPE. */ | |
487 | ||
488 | struct cplus_add_input | |
489 | { | |
490 | void add_input (enum compile_i_scope_types type, const char *input, | |
491 | struct ui_file *buf) | |
492 | { | |
493 | switch (type) | |
494 | { | |
495 | case COMPILE_I_PRINT_VALUE_SCOPE: | |
496 | case COMPILE_I_PRINT_ADDRESS_SCOPE: | |
497 | fprintf_unfiltered | |
498 | (buf, | |
499 | /* "auto" strips ref- and cv- qualifiers, so we need to also strip | |
500 | those from COMPILE_I_EXPR_PTR_TYPE. */ | |
501 | "auto " COMPILE_I_EXPR_VAL " = %s;\n" | |
502 | "typedef " | |
503 | "std::add_pointer<std::remove_cv<decltype (%s)>::type>::type " | |
504 | " __gdb_expr_ptr;\n" | |
505 | "__gdb_expr_ptr " COMPILE_I_EXPR_PTR_TYPE ";\n" | |
506 | "std::memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s (" | |
507 | COMPILE_I_EXPR_VAL "),\n" | |
508 | "\tsizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n" | |
509 | ,input, input, | |
510 | (type == COMPILE_I_PRINT_ADDRESS_SCOPE | |
511 | ? "__builtin_addressof" : "")); | |
512 | break; | |
513 | ||
514 | default: | |
515 | fputs_unfiltered (input, buf); | |
516 | break; | |
517 | } | |
518 | fputs_unfiltered ("\n", buf); | |
519 | } | |
520 | }; | |
521 | ||
ad3a68e9 | 522 | /* A host class representing a compile program. |
36de76f9 | 523 | |
ad3a68e9 KS |
524 | CompileInstanceType is the type of the compile_instance for the |
525 | language. | |
526 | ||
527 | PushUserExpressionPolicy and PopUserExpressionPolicy are used to | |
528 | push and pop user expression pragmas to the compile plug-in. | |
529 | ||
530 | AddCodeHeaderPolicy and AddCodeFooterPolicy are used to add the appropriate | |
531 | code header and footer, respectively. | |
36de76f9 | 532 | |
ad3a68e9 | 533 | AddInputPolicy adds the actual user code. */ |
bb2ec1b3 | 534 | |
ad3a68e9 KS |
535 | template <class CompileInstanceType, class PushUserExpressionPolicy, |
536 | class PopUserExpressionPolicy, class AddCodeHeaderPolicy, | |
537 | class AddCodeFooterPolicy, class AddInputPolicy> | |
538 | class compile_program | |
539 | : private PushUserExpressionPolicy, private PopUserExpressionPolicy, | |
540 | private AddCodeHeaderPolicy, private AddCodeFooterPolicy, | |
541 | private AddInputPolicy | |
542 | { | |
543 | public: | |
544 | ||
545 | /* Construct a compile_program using the compiler instance INST | |
546 | using the architecture given by GDBARCH. */ | |
547 | compile_program (CompileInstanceType *inst, struct gdbarch *gdbarch) | |
548 | : m_instance (inst), m_arch (gdbarch) | |
549 | { | |
550 | } | |
551 | ||
552 | /* Take the source code provided by the user with the 'compile' | |
553 | command and compute the additional wrapping, macro, variable and | |
554 | register operations needed. INPUT is the source code derived from | |
555 | the 'compile' command, EXPR_BLOCK denotes the block relevant contextually | |
556 | to the inferior when the expression was created, and EXPR_PC | |
557 | indicates the value of $PC. | |
558 | ||
559 | Returns the text of the program to compile. */ | |
560 | std::string compute (const char *input, const struct block *expr_block, | |
561 | CORE_ADDR expr_pc) | |
562 | { | |
563 | string_file var_stream; | |
564 | string_file buf; | |
565 | ||
566 | /* Do not generate local variable information for "raw" | |
567 | compilations. In this case we aren't emitting our own function | |
568 | and the user's code may only refer to globals. */ | |
569 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
570 | { | |
571 | /* Generate the code to compute variable locations, but do it | |
572 | before generating the function header, so we can define the | |
573 | register struct before the function body. This requires a | |
574 | temporary stream. */ | |
575 | gdb::unique_xmalloc_ptr<unsigned char> registers_used | |
d82b3862 | 576 | = generate_c_for_variable_locations (m_instance, &var_stream, m_arch, |
ad3a68e9 KS |
577 | expr_block, expr_pc); |
578 | ||
579 | buf.puts ("typedef unsigned int" | |
580 | " __attribute__ ((__mode__(__pointer__)))" | |
581 | " __gdb_uintptr;\n"); | |
582 | buf.puts ("typedef int" | |
583 | " __attribute__ ((__mode__(__pointer__)))" | |
584 | " __gdb_intptr;\n"); | |
585 | ||
586 | /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */ | |
587 | for (int i = 0; i < 4; ++i) | |
588 | { | |
589 | const char *mode = c_get_mode_for_size (1 << i); | |
590 | ||
591 | gdb_assert (mode != NULL); | |
592 | buf.printf ("typedef int" | |
593 | " __attribute__ ((__mode__(__%s__)))" | |
594 | " __gdb_int_%s;\n", | |
595 | mode, mode); | |
596 | } | |
597 | ||
598 | generate_register_struct (&buf, m_arch, registers_used.get ()); | |
599 | } | |
600 | ||
601 | AddCodeHeaderPolicy::add_code_header (m_instance->scope (), &buf); | |
602 | ||
603 | if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE | |
604 | || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE | |
605 | || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE) | |
606 | { | |
607 | buf.write (var_stream.c_str (), var_stream.size ()); | |
608 | PushUserExpressionPolicy::push_user_expression (&buf); | |
609 | } | |
bb2ec1b3 | 610 | |
ad3a68e9 KS |
611 | write_macro_definitions (expr_block, expr_pc, &buf); |
612 | ||
613 | /* The user expression has to be in its own scope, so that "extern" | |
614 | works properly. Otherwise gcc thinks that the "extern" | |
615 | declaration is in the same scope as the declaration provided by | |
616 | gdb. */ | |
617 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
618 | buf.puts ("{\n"); | |
619 | ||
620 | buf.puts ("#line 1 \"gdb command line\"\n"); | |
621 | ||
622 | AddInputPolicy::add_input (m_instance->scope (), input, &buf); | |
623 | ||
624 | /* For larger user expressions the automatic semicolons may be | |
625 | confusing. */ | |
626 | if (strchr (input, '\n') == NULL) | |
627 | buf.puts (";\n"); | |
628 | ||
629 | if (m_instance->scope () != COMPILE_I_RAW_SCOPE) | |
630 | buf.puts ("}\n"); | |
631 | ||
632 | if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE | |
633 | || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE | |
634 | || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE) | |
635 | PopUserExpressionPolicy::pop_user_expression (&buf); | |
636 | ||
637 | AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf); | |
638 | return buf.string (); | |
639 | } | |
640 | ||
641 | private: | |
642 | ||
643 | /* The compile instance to be used for compilation and | |
644 | type-conversion. */ | |
645 | CompileInstanceType *m_instance; | |
646 | ||
647 | /* The architecture to be used. */ | |
648 | struct gdbarch *m_arch; | |
649 | }; | |
650 | ||
078a0207 | 651 | /* The types used for C and C++ program computations. */ |
ad3a68e9 KS |
652 | |
653 | typedef compile_program<compile_c_instance, | |
654 | c_push_user_expression, pop_user_expression_nop, | |
655 | c_add_code_header, c_add_code_footer, | |
656 | c_add_input> c_compile_program; | |
657 | ||
078a0207 KS |
658 | typedef compile_program<compile_cplus_instance, |
659 | cplus_push_user_expression, cplus_pop_user_expression, | |
660 | cplus_add_code_header, c_add_code_footer, | |
661 | cplus_add_input> cplus_compile_program; | |
662 | ||
ad3a68e9 KS |
663 | /* The la_compute_program method for C. */ |
664 | ||
665 | std::string | |
666 | c_compute_program (compile_instance *inst, | |
667 | const char *input, | |
668 | struct gdbarch *gdbarch, | |
669 | const struct block *expr_block, | |
670 | CORE_ADDR expr_pc) | |
671 | { | |
672 | compile_c_instance *c_inst = static_cast<compile_c_instance *> (inst); | |
673 | c_compile_program program (c_inst, gdbarch); | |
bb2ec1b3 | 674 | |
ad3a68e9 | 675 | return program.compute (input, expr_block, expr_pc); |
bb2ec1b3 | 676 | } |
078a0207 KS |
677 | |
678 | /* The la_compute_program method for C++. */ | |
679 | ||
680 | std::string | |
681 | cplus_compute_program (compile_instance *inst, | |
682 | const char *input, | |
683 | struct gdbarch *gdbarch, | |
684 | const struct block *expr_block, | |
685 | CORE_ADDR expr_pc) | |
686 | { | |
687 | compile_cplus_instance *cplus_inst | |
688 | = static_cast<compile_cplus_instance *> (inst); | |
689 | cplus_compile_program program (cplus_inst, gdbarch); | |
690 | ||
691 | return program.compute (input, expr_block, expr_pc); | |
692 | } |