[gdb/cli] Fix return from frame containing inline frame
[binutils-gdb.git] / gdb / compile / compile-c-support.c
bloba152e5ace61449bdb640fd39e20f21980bcc2a53
1 /* C/C++ language support for compilation.
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "compile-internal.h"
21 #include "compile-c.h"
22 #include "compile-cplus.h"
23 #include "compile.h"
24 #include "c-lang.h"
25 #include "macrotab.h"
26 #include "macroscope.h"
27 #include "regcache.h"
28 #include "gdbsupport/function-view.h"
29 #include "gdbsupport/gdb-dlfcn.h"
30 #include "gdbsupport/preprocessor.h"
31 #include "gdbarch.h"
33 /* See compile-internal.h. */
35 const char *
36 c_get_mode_for_size (int size)
38 const char *mode = NULL;
40 switch (size)
42 case 1:
43 mode = "QI";
44 break;
45 case 2:
46 mode = "HI";
47 break;
48 case 4:
49 mode = "SI";
50 break;
51 case 8:
52 mode = "DI";
53 break;
54 default:
55 internal_error (_("Invalid GCC mode size %d."), size);
58 return mode;
61 /* See compile-internal.h. */
63 std::string
64 c_get_range_decl_name (const struct dynamic_prop *prop)
66 return string_printf ("__gdb_prop_%s", host_address_to_string (prop));
71 /* Load the plug-in library FE_LIBCC and return the initialization function
72 FE_CONTEXT. */
74 template <typename FUNCTYPE>
75 FUNCTYPE *
76 load_libcompile (const char *fe_libcc, const char *fe_context)
78 FUNCTYPE *func;
80 /* gdb_dlopen will call error () on an error, so no need to check
81 value. */
82 gdb_dlhandle_up handle = gdb_dlopen (fe_libcc);
83 func = (FUNCTYPE *) gdb_dlsym (handle, fe_context);
85 if (func == NULL)
86 error (_("could not find symbol %s in library %s"), fe_context, fe_libcc);
88 /* Leave the library open. */
89 handle.release ();
90 return func;
93 /* Return the compile instance associated with the current context.
94 This function calls the symbol returned from the load_libcompile
95 function. FE_LIBCC is the library to load. BASE_VERSION is the
96 base compile plug-in version we support. API_VERSION is the
97 API version supported. */
99 template <typename INSTTYPE, typename FUNCTYPE, typename CTXTYPE,
100 typename BASE_VERSION_TYPE, typename API_VERSION_TYPE>
101 std::unique_ptr<compile_instance>
102 get_compile_context (const char *fe_libcc, const char *fe_context,
103 BASE_VERSION_TYPE base_version,
104 API_VERSION_TYPE api_version)
106 static FUNCTYPE *func;
107 static CTXTYPE *context;
109 if (func == NULL)
111 func = load_libcompile<FUNCTYPE> (fe_libcc, fe_context);
112 gdb_assert (func != NULL);
115 context = (*func) (base_version, api_version);
116 if (context == NULL)
117 error (_("The loaded version of GCC does not support the required version "
118 "of the API."));
120 return std::make_unique<INSTTYPE> (context);
123 /* A C-language implementation of get_compile_context. */
125 std::unique_ptr<compile_instance>
126 c_get_compile_context ()
128 return get_compile_context
129 <compile_c_instance, gcc_c_fe_context_function, gcc_c_context,
130 gcc_base_api_version, gcc_c_api_version>
131 (STRINGIFY (GCC_C_FE_LIBCC), STRINGIFY (GCC_C_FE_CONTEXT),
132 GCC_FE_VERSION_0, GCC_C_FE_VERSION_0);
135 /* A C++-language implementation of get_compile_context. */
137 std::unique_ptr<compile_instance>
138 cplus_get_compile_context ()
140 return get_compile_context
141 <compile_cplus_instance, gcc_cp_fe_context_function, gcc_cp_context,
142 gcc_base_api_version, gcc_cp_api_version>
143 (STRINGIFY (GCC_CP_FE_LIBCC), STRINGIFY (GCC_CP_FE_CONTEXT),
144 GCC_FE_VERSION_0, GCC_CP_FE_VERSION_0);
149 /* Write one macro definition. */
151 static void
152 print_one_macro (const char *name, const struct macro_definition *macro,
153 struct macro_source_file *source, int line,
154 ui_file *file)
156 /* Don't print command-line defines. They will be supplied another
157 way. */
158 if (line == 0)
159 return;
161 /* None of -Wno-builtin-macro-redefined, #undef first
162 or plain #define of the same value would avoid a warning. */
163 gdb_printf (file, "#ifndef %s\n# define %s", name, name);
165 if (macro->kind == macro_function_like)
167 int i;
169 gdb_puts ("(", file);
170 for (i = 0; i < macro->argc; i++)
172 gdb_puts (macro->argv[i], file);
173 if (i + 1 < macro->argc)
174 gdb_puts (", ", file);
176 gdb_puts (")", file);
179 gdb_printf (file, " %s\n#endif\n", macro->replacement);
182 /* Write macro definitions at PC to FILE. */
184 static void
185 write_macro_definitions (const struct block *block, CORE_ADDR pc,
186 struct ui_file *file)
188 gdb::unique_xmalloc_ptr<struct macro_scope> scope;
190 if (block != NULL)
191 scope = sal_macro_scope (find_pc_line (pc, 0));
192 else
193 scope = default_macro_scope ();
194 if (scope == NULL)
195 scope = user_macro_scope ();
197 if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
199 macro_for_each_in_scope (scope->file, scope->line,
200 [&] (const char *name,
201 const macro_definition *macro,
202 macro_source_file *source,
203 int line)
205 print_one_macro (name, macro, source, line, file);
210 /* Generate a structure holding all the registers used by the function
211 we're generating. */
213 static void
214 generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch,
215 const std::vector<bool> &registers_used)
217 int i;
218 int seen = 0;
220 gdb_puts ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n",
221 stream);
223 if (!registers_used.empty ())
224 for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
226 if (registers_used[i])
228 struct type *regtype = check_typedef (register_type (gdbarch, i));
229 std::string regname = compile_register_name_mangled (gdbarch, i);
231 seen = 1;
233 /* You might think we could use type_print here. However,
234 target descriptions often use types with names like
235 "int64_t", which may not be defined in the inferior
236 (and in any case would not be looked up due to the
237 #pragma business). So, we take a much simpler
238 approach: for pointer- or integer-typed registers, emit
239 the field in the most direct way; and for other
240 register types (typically flags or vectors), emit a
241 maximally-aligned array of the correct size. */
243 gdb_puts (" ", stream);
244 switch (regtype->code ())
246 case TYPE_CODE_PTR:
247 gdb_printf (stream, "__gdb_uintptr %s",
248 regname.c_str ());
249 break;
251 case TYPE_CODE_INT:
253 const char *mode
254 = c_get_mode_for_size (regtype->length ());
256 if (mode != NULL)
258 if (regtype->is_unsigned ())
259 gdb_puts ("unsigned ", stream);
260 gdb_printf (stream,
261 "int %s"
262 " __attribute__ ((__mode__(__%s__)))",
263 regname.c_str (),
264 mode);
265 break;
269 [[fallthrough]];
271 default:
272 gdb_printf (stream,
273 " unsigned char %s[%s]"
274 " __attribute__((__aligned__("
275 "__BIGGEST_ALIGNMENT__)))",
276 regname.c_str (),
277 pulongest (regtype->length ()));
279 gdb_puts (";\n", stream);
283 if (!seen)
284 gdb_puts (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n",
285 stream);
287 gdb_puts ("};\n\n", stream);
290 /* C-language policy to emit a push user expression pragma into BUF. */
292 struct c_push_user_expression
294 void push_user_expression (struct ui_file *buf)
296 gdb_puts ("#pragma GCC user_expression\n", buf);
300 /* C-language policy to emit a pop user expression pragma into BUF.
301 For C, this is a nop. */
303 struct pop_user_expression_nop
305 void pop_user_expression (struct ui_file *buf)
307 /* Nothing to do. */
311 /* C-language policy to construct a code header for a block of code.
312 Takes a scope TYPE argument which selects the correct header to
313 insert into BUF. */
315 struct c_add_code_header
317 void add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
319 switch (type)
321 case COMPILE_I_SIMPLE_SCOPE:
322 gdb_puts ("void "
323 GCC_FE_WRAPPER_FUNCTION
324 " (struct "
325 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
326 " *"
327 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
328 ") {\n",
329 buf);
330 break;
332 case COMPILE_I_PRINT_ADDRESS_SCOPE:
333 case COMPILE_I_PRINT_VALUE_SCOPE:
334 /* <string.h> is needed for a memcpy call below. */
335 gdb_puts ("#include <string.h>\n"
336 "void "
337 GCC_FE_WRAPPER_FUNCTION
338 " (struct "
339 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
340 " *"
341 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
342 ", "
343 COMPILE_I_PRINT_OUT_ARG_TYPE
345 COMPILE_I_PRINT_OUT_ARG
346 ") {\n",
347 buf);
348 break;
350 case COMPILE_I_RAW_SCOPE:
351 break;
353 default:
354 gdb_assert_not_reached ("Unknown compiler scope reached.");
359 /* C-language policy to construct a code footer for a block of code.
360 Takes a scope TYPE which selects the correct footer to insert into BUF. */
362 struct c_add_code_footer
364 void add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
366 switch (type)
368 case COMPILE_I_SIMPLE_SCOPE:
369 case COMPILE_I_PRINT_ADDRESS_SCOPE:
370 case COMPILE_I_PRINT_VALUE_SCOPE:
371 gdb_puts ("}\n", buf);
372 break;
374 case COMPILE_I_RAW_SCOPE:
375 break;
377 default:
378 gdb_assert_not_reached ("Unknown compiler scope reached.");
383 /* C-language policy to emit the user code snippet INPUT into BUF based on the
384 scope TYPE. */
386 struct c_add_input
388 void add_input (enum compile_i_scope_types type, const char *input,
389 struct ui_file *buf)
391 switch (type)
393 case COMPILE_I_PRINT_ADDRESS_SCOPE:
394 case COMPILE_I_PRINT_VALUE_SCOPE:
395 gdb_printf (buf,
396 "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n"
397 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n"
398 "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s"
399 COMPILE_I_EXPR_VAL ",\n"
400 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
401 , input, input,
402 (type == COMPILE_I_PRINT_ADDRESS_SCOPE
403 ? "&" : ""));
404 break;
406 default:
407 gdb_puts (input, buf);
408 break;
410 gdb_puts ("\n", buf);
414 /* C++-language policy to emit a push user expression pragma into
415 BUF. */
417 struct cplus_push_user_expression
419 void push_user_expression (struct ui_file *buf)
421 gdb_puts ("#pragma GCC push_user_expression\n", buf);
425 /* C++-language policy to emit a pop user expression pragma into BUF. */
427 struct cplus_pop_user_expression
429 void pop_user_expression (struct ui_file *buf)
431 gdb_puts ("#pragma GCC pop_user_expression\n", buf);
435 /* C++-language policy to construct a code header for a block of code.
436 Takes a scope TYPE argument which selects the correct header to
437 insert into BUF. */
439 struct cplus_add_code_header
441 void add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
443 switch (type)
445 case COMPILE_I_SIMPLE_SCOPE:
446 gdb_puts ("void "
447 GCC_FE_WRAPPER_FUNCTION
448 " (struct "
449 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
450 " *"
451 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
452 ") {\n",
453 buf);
454 break;
456 case COMPILE_I_PRINT_ADDRESS_SCOPE:
457 case COMPILE_I_PRINT_VALUE_SCOPE:
458 gdb_puts (
459 "#include <cstring>\n"
460 "#include <bits/move.h>\n"
461 "void "
462 GCC_FE_WRAPPER_FUNCTION
463 " (struct "
464 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
465 " *"
466 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
467 ", "
468 COMPILE_I_PRINT_OUT_ARG_TYPE
470 COMPILE_I_PRINT_OUT_ARG
471 ") {\n",
472 buf);
473 break;
475 case COMPILE_I_RAW_SCOPE:
476 break;
478 default:
479 gdb_assert_not_reached ("Unknown compiler scope reached.");
484 /* C++-language policy to emit the user code snippet INPUT into BUF based on
485 the scope TYPE. */
487 struct cplus_add_input
489 void add_input (enum compile_i_scope_types type, const char *input,
490 struct ui_file *buf)
492 switch (type)
494 case COMPILE_I_PRINT_VALUE_SCOPE:
495 case COMPILE_I_PRINT_ADDRESS_SCOPE:
496 gdb_printf
497 (buf,
498 /* "auto" strips ref- and cv- qualifiers, so we need to also strip
499 those from COMPILE_I_EXPR_PTR_TYPE. */
500 "auto " COMPILE_I_EXPR_VAL " = %s;\n"
501 "typedef "
502 "std::add_pointer<std::remove_cv<decltype (%s)>::type>::type "
503 " __gdb_expr_ptr;\n"
504 "__gdb_expr_ptr " COMPILE_I_EXPR_PTR_TYPE ";\n"
505 "std::memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s ("
506 COMPILE_I_EXPR_VAL "),\n"
507 "\tsizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
508 ,input, input,
509 (type == COMPILE_I_PRINT_ADDRESS_SCOPE
510 ? "__builtin_addressof" : ""));
511 break;
513 default:
514 gdb_puts (input, buf);
515 break;
517 gdb_puts ("\n", buf);
521 /* A host class representing a compile program.
523 CompileInstanceType is the type of the compile_instance for the
524 language.
526 PushUserExpressionPolicy and PopUserExpressionPolicy are used to
527 push and pop user expression pragmas to the compile plug-in.
529 AddCodeHeaderPolicy and AddCodeFooterPolicy are used to add the appropriate
530 code header and footer, respectively.
532 AddInputPolicy adds the actual user code. */
534 template <class CompileInstanceType, class PushUserExpressionPolicy,
535 class PopUserExpressionPolicy, class AddCodeHeaderPolicy,
536 class AddCodeFooterPolicy, class AddInputPolicy>
537 class compile_program
538 : private PushUserExpressionPolicy, private PopUserExpressionPolicy,
539 private AddCodeHeaderPolicy, private AddCodeFooterPolicy,
540 private AddInputPolicy
542 public:
544 /* Construct a compile_program using the compiler instance INST
545 using the architecture given by GDBARCH. */
546 compile_program (CompileInstanceType *inst, struct gdbarch *gdbarch)
547 : m_instance (inst), m_arch (gdbarch)
551 /* Take the source code provided by the user with the 'compile'
552 command and compute the additional wrapping, macro, variable and
553 register operations needed. INPUT is the source code derived from
554 the 'compile' command, EXPR_BLOCK denotes the block relevant contextually
555 to the inferior when the expression was created, and EXPR_PC
556 indicates the value of $PC.
558 Returns the text of the program to compile. */
559 std::string compute (const char *input, const struct block *expr_block,
560 CORE_ADDR expr_pc)
562 string_file var_stream;
563 string_file buf;
565 /* Do not generate local variable information for "raw"
566 compilations. In this case we aren't emitting our own function
567 and the user's code may only refer to globals. */
568 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
570 /* Generate the code to compute variable locations, but do it
571 before generating the function header, so we can define the
572 register struct before the function body. This requires a
573 temporary stream. */
574 std::vector<bool> registers_used
575 = generate_c_for_variable_locations (m_instance, &var_stream, m_arch,
576 expr_block, expr_pc);
578 buf.puts ("typedef unsigned int"
579 " __attribute__ ((__mode__(__pointer__)))"
580 " __gdb_uintptr;\n");
581 buf.puts ("typedef int"
582 " __attribute__ ((__mode__(__pointer__)))"
583 " __gdb_intptr;\n");
585 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
586 for (int i = 0; i < 4; ++i)
588 const char *mode = c_get_mode_for_size (1 << i);
590 gdb_assert (mode != NULL);
591 buf.printf ("typedef int"
592 " __attribute__ ((__mode__(__%s__)))"
593 " __gdb_int_%s;\n",
594 mode, mode);
597 generate_register_struct (&buf, m_arch, registers_used);
600 AddCodeHeaderPolicy::add_code_header (m_instance->scope (), &buf);
602 if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE
603 || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE
604 || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE)
606 buf.write (var_stream.c_str (), var_stream.size ());
607 PushUserExpressionPolicy::push_user_expression (&buf);
610 write_macro_definitions (expr_block, expr_pc, &buf);
612 /* The user expression has to be in its own scope, so that "extern"
613 works properly. Otherwise gcc thinks that the "extern"
614 declaration is in the same scope as the declaration provided by
615 gdb. */
616 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
617 buf.puts ("{\n");
619 buf.puts ("#line 1 \"gdb command line\"\n");
621 AddInputPolicy::add_input (m_instance->scope (), input, &buf);
623 /* For larger user expressions the automatic semicolons may be
624 confusing. */
625 if (strchr (input, '\n') == NULL)
626 buf.puts (";\n");
628 if (m_instance->scope () != COMPILE_I_RAW_SCOPE)
629 buf.puts ("}\n");
631 if (m_instance->scope () == COMPILE_I_SIMPLE_SCOPE
632 || m_instance->scope () == COMPILE_I_PRINT_ADDRESS_SCOPE
633 || m_instance->scope () == COMPILE_I_PRINT_VALUE_SCOPE)
634 PopUserExpressionPolicy::pop_user_expression (&buf);
636 AddCodeFooterPolicy::add_code_footer (m_instance->scope (), &buf);
637 return buf.release ();
640 private:
642 /* The compile instance to be used for compilation and
643 type-conversion. */
644 CompileInstanceType *m_instance;
646 /* The architecture to be used. */
647 struct gdbarch *m_arch;
650 /* The types used for C and C++ program computations. */
652 typedef compile_program<compile_c_instance,
653 c_push_user_expression, pop_user_expression_nop,
654 c_add_code_header, c_add_code_footer,
655 c_add_input> c_compile_program;
657 typedef compile_program<compile_cplus_instance,
658 cplus_push_user_expression, cplus_pop_user_expression,
659 cplus_add_code_header, c_add_code_footer,
660 cplus_add_input> cplus_compile_program;
662 /* The compute_program method for C. */
664 std::string
665 c_compute_program (compile_instance *inst,
666 const char *input,
667 struct gdbarch *gdbarch,
668 const struct block *expr_block,
669 CORE_ADDR expr_pc)
671 compile_c_instance *c_inst = static_cast<compile_c_instance *> (inst);
672 c_compile_program program (c_inst, gdbarch);
674 return program.compute (input, expr_block, expr_pc);
677 /* The compute_program method for C++. */
679 std::string
680 cplus_compute_program (compile_instance *inst,
681 const char *input,
682 struct gdbarch *gdbarch,
683 const struct block *expr_block,
684 CORE_ADDR expr_pc)
686 compile_cplus_instance *cplus_inst
687 = static_cast<compile_cplus_instance *> (inst);
688 cplus_compile_program program (cplus_inst, gdbarch);
690 return program.compute (input, expr_block, expr_pc);