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"
26 #include "macroscope.h"
28 #include "gdbsupport/function-view.h"
29 #include "gdbsupport/gdb-dlfcn.h"
30 #include "gdbsupport/preprocessor.h"
33 /* See compile-internal.h. */
36 c_get_mode_for_size (int size
)
38 const char *mode
= NULL
;
55 internal_error (_("Invalid GCC mode size %d."), size
);
61 /* See compile-internal.h. */
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
74 template <typename FUNCTYPE
>
76 load_libcompile (const char *fe_libcc
, const char *fe_context
)
80 /* gdb_dlopen will call error () on an error, so no need to check
82 gdb_dlhandle_up handle
= gdb_dlopen (fe_libcc
);
83 func
= (FUNCTYPE
*) gdb_dlsym (handle
, fe_context
);
86 error (_("could not find symbol %s in library %s"), fe_context
, fe_libcc
);
88 /* Leave the library open. */
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
;
111 func
= load_libcompile
<FUNCTYPE
> (fe_libcc
, fe_context
);
112 gdb_assert (func
!= NULL
);
115 context
= (*func
) (base_version
, api_version
);
117 error (_("The loaded version of GCC does not support the required version "
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. */
152 print_one_macro (const char *name
, const struct macro_definition
*macro
,
153 struct macro_source_file
*source
, int line
,
156 /* Don't print command-line defines. They will be supplied another
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
)
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. */
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
;
191 scope
= sal_macro_scope (find_pc_line (pc
, 0));
193 scope
= default_macro_scope ();
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
,
205 print_one_macro (name
, macro
, source
, line
, file
);
210 /* Generate a structure holding all the registers used by the function
214 generate_register_struct (struct ui_file
*stream
, struct gdbarch
*gdbarch
,
215 const std::vector
<bool> ®isters_used
)
220 gdb_puts ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
" {\n",
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
);
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 ())
247 gdb_printf (stream
, "__gdb_uintptr %s",
254 = c_get_mode_for_size (regtype
->length ());
258 if (regtype
->is_unsigned ())
259 gdb_puts ("unsigned ", stream
);
262 " __attribute__ ((__mode__(__%s__)))",
273 " unsigned char %s[%s]"
274 " __attribute__((__aligned__("
275 "__BIGGEST_ALIGNMENT__)))",
277 pulongest (regtype
->length ()));
279 gdb_puts (";\n", stream
);
284 gdb_puts (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY
";\n",
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
)
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
315 struct c_add_code_header
317 void add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
321 case COMPILE_I_SIMPLE_SCOPE
:
323 GCC_FE_WRAPPER_FUNCTION
325 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
327 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
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"
337 GCC_FE_WRAPPER_FUNCTION
339 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
341 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
343 COMPILE_I_PRINT_OUT_ARG_TYPE
345 COMPILE_I_PRINT_OUT_ARG
350 case COMPILE_I_RAW_SCOPE
:
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
)
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
);
374 case COMPILE_I_RAW_SCOPE
:
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
388 void add_input (enum compile_i_scope_types type
, const char *input
,
393 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
394 case COMPILE_I_PRINT_VALUE_SCOPE
:
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"
402 (type
== COMPILE_I_PRINT_ADDRESS_SCOPE
407 gdb_puts (input
, buf
);
410 gdb_puts ("\n", buf
);
414 /* C++-language policy to emit a push user expression pragma into
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
439 struct cplus_add_code_header
441 void add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
445 case COMPILE_I_SIMPLE_SCOPE
:
447 GCC_FE_WRAPPER_FUNCTION
449 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
451 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
456 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
457 case COMPILE_I_PRINT_VALUE_SCOPE
:
459 "#include <cstring>\n"
460 "#include <bits/move.h>\n"
462 GCC_FE_WRAPPER_FUNCTION
464 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
466 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
468 COMPILE_I_PRINT_OUT_ARG_TYPE
470 COMPILE_I_PRINT_OUT_ARG
475 case COMPILE_I_RAW_SCOPE
:
479 gdb_assert_not_reached ("Unknown compiler scope reached.");
484 /* C++-language policy to emit the user code snippet INPUT into BUF based on
487 struct cplus_add_input
489 void add_input (enum compile_i_scope_types type
, const char *input
,
494 case COMPILE_I_PRINT_VALUE_SCOPE
:
495 case COMPILE_I_PRINT_ADDRESS_SCOPE
:
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"
502 "std::add_pointer<std::remove_cv<decltype (%s)>::type>::type "
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"
509 (type
== COMPILE_I_PRINT_ADDRESS_SCOPE
510 ? "__builtin_addressof" : ""));
514 gdb_puts (input
, buf
);
517 gdb_puts ("\n", buf
);
521 /* A host class representing a compile program.
523 CompileInstanceType is the type of the compile_instance for the
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
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
,
562 string_file var_stream
;
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
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__)))"
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__)))"
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
616 if (m_instance
->scope () != COMPILE_I_RAW_SCOPE
)
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
625 if (strchr (input
, '\n') == NULL
)
628 if (m_instance
->scope () != COMPILE_I_RAW_SCOPE
)
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 ();
642 /* The compile instance to be used for compilation and
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. */
665 c_compute_program (compile_instance
*inst
,
667 struct gdbarch
*gdbarch
,
668 const struct block
*expr_block
,
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++. */
680 cplus_compute_program (compile_instance
*inst
,
682 struct gdbarch
*gdbarch
,
683 const struct block
*expr_block
,
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
);