1 /* C language support for compilation.
3 Copyright (C) 2014-2015 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/>. */
21 #include "compile-internal.h"
23 #include "gdb-dlfcn.h"
26 #include "macroscope.h"
29 /* See compile-internal.h. */
32 c_get_mode_for_size (int size
)
34 const char *mode
= NULL
;
51 internal_error (__FILE__
, __LINE__
, _("Invalid GCC mode size %d."), size
);
57 /* See compile-internal.h. */
60 c_get_range_decl_name (const struct dynamic_prop
*prop
)
62 return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop
));
68 #define STRINGIFY(x) STR(x)
70 /* Helper function for c_get_compile_context. Open the GCC front-end
71 shared library and return the symbol specified by the current
74 static gcc_c_fe_context_function
*
78 gcc_c_fe_context_function
*func
;
80 /* gdb_dlopen will call error () on an error, so no need to check
82 handle
= gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC
));
83 func
= (gcc_c_fe_context_function
*) gdb_dlsym (handle
,
84 STRINGIFY (GCC_C_FE_CONTEXT
));
87 error (_("could not find symbol %s in library %s"),
88 STRINGIFY (GCC_C_FE_CONTEXT
),
89 STRINGIFY (GCC_C_FE_LIBCC
));
93 /* Return the compile instance associated with the current context.
94 This function calls the symbol returned from the load_libcc
95 function. This will provide the gcc_c_context. */
97 struct compile_instance
*
98 c_get_compile_context (void)
100 static gcc_c_fe_context_function
*func
;
102 struct gcc_c_context
*context
;
106 func
= load_libcc ();
107 gdb_assert (func
!= NULL
);
110 context
= (*func
) (GCC_FE_VERSION_0
, GCC_C_FE_VERSION_0
);
112 error (_("The loaded version of GCC does not support the required version "
115 return new_compile_instance (context
);
120 /* Write one macro definition. */
123 print_one_macro (const char *name
, const struct macro_definition
*macro
,
124 struct macro_source_file
*source
, int line
,
127 struct ui_file
*file
= user_data
;
129 /* Don't print command-line defines. They will be supplied another
134 fprintf_filtered (file
, "#define %s", name
);
136 if (macro
->kind
== macro_function_like
)
140 fputs_filtered ("(", file
);
141 for (i
= 0; i
< macro
->argc
; i
++)
143 fputs_filtered (macro
->argv
[i
], file
);
144 if (i
+ 1 < macro
->argc
)
145 fputs_filtered (", ", file
);
147 fputs_filtered (")", file
);
150 fprintf_filtered (file
, " %s\n", macro
->replacement
);
153 /* Write macro definitions at PC to FILE. */
156 write_macro_definitions (const struct block
*block
, CORE_ADDR pc
,
157 struct ui_file
*file
)
159 struct macro_scope
*scope
;
162 scope
= sal_macro_scope (find_pc_line (pc
, 0));
164 scope
= default_macro_scope ();
166 scope
= user_macro_scope ();
168 if (scope
!= NULL
&& scope
->file
!= NULL
&& scope
->file
->table
!= NULL
)
169 macro_for_each_in_scope (scope
->file
, scope
->line
, print_one_macro
, file
);
172 /* Helper function to construct a header scope for a block of code.
173 Takes a scope argument which selects the correct header to
177 add_code_header (enum compile_i_scope_types type
, struct ui_file
*buf
)
181 case COMPILE_I_SIMPLE_SCOPE
:
182 fputs_unfiltered ("void "
183 GCC_FE_WRAPPER_FUNCTION
185 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
187 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
191 case COMPILE_I_RAW_SCOPE
:
194 gdb_assert_not_reached (_("Unknown compiler scope reached."));
198 /* Helper function to construct a footer scope for a block of code.
199 Takes a scope argument which selects the correct footer to
203 add_code_footer (enum compile_i_scope_types type
, struct ui_file
*buf
)
207 case COMPILE_I_SIMPLE_SCOPE
:
208 fputs_unfiltered ("}\n", buf
);
210 case COMPILE_I_RAW_SCOPE
:
213 gdb_assert_not_reached (_("Unknown compiler scope reached."));
217 /* Generate a structure holding all the registers used by the function
221 generate_register_struct (struct ui_file
*stream
, struct gdbarch
*gdbarch
,
222 const unsigned char *registers_used
)
227 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
" {\n",
230 if (registers_used
!= NULL
)
231 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); ++i
)
233 if (registers_used
[i
])
235 struct type
*regtype
= check_typedef (register_type (gdbarch
, i
));
236 char *regname
= compile_register_name_mangled (gdbarch
, i
);
237 struct cleanup
*cleanups
= make_cleanup (xfree
, regname
);
241 /* You might think we could use type_print here. However,
242 target descriptions often use types with names like
243 "int64_t", which may not be defined in the inferior
244 (and in any case would not be looked up due to the
245 #pragma business). So, we take a much simpler
246 approach: for pointer- or integer-typed registers, emit
247 the field in the most direct way; and for other
248 register types (typically flags or vectors), emit a
249 maximally-aligned array of the correct size. */
251 fputs_unfiltered (" ", stream
);
252 switch (TYPE_CODE (regtype
))
255 fprintf_filtered (stream
, "void *%s", regname
);
261 = c_get_mode_for_size (TYPE_LENGTH (regtype
));
265 if (TYPE_UNSIGNED (regtype
))
266 fputs_unfiltered ("unsigned ", stream
);
267 fprintf_unfiltered (stream
,
269 " __attribute__ ((__mode__(__%s__)))",
279 fprintf_unfiltered (stream
,
280 " unsigned char %s[%d]"
281 " __attribute__((__aligned__("
282 "__BIGGEST_ALIGNMENT__)))",
284 TYPE_LENGTH (regtype
));
286 fputs_unfiltered (";\n", stream
);
288 do_cleanups (cleanups
);
293 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY
";\n",
296 fputs_unfiltered ("};\n\n", stream
);
299 /* Take the source code provided by the user with the 'compile'
300 command, and compute the additional wrapping, macro, variable and
301 register operations needed. INPUT is the source code derived from
302 the 'compile' command, GDBARCH is the architecture to use when
303 computing above, EXPR_BLOCK denotes the block relevant contextually
304 to the inferior when the expression was created, and EXPR_PC
305 indicates the value of $PC. */
308 c_compute_program (struct compile_instance
*inst
,
310 struct gdbarch
*gdbarch
,
311 const struct block
*expr_block
,
314 struct ui_file
*buf
, *var_stream
= NULL
;
316 struct cleanup
*cleanup
;
317 struct compile_c_instance
*context
= (struct compile_c_instance
*) inst
;
319 buf
= mem_fileopen ();
320 cleanup
= make_cleanup_ui_file_delete (buf
);
322 write_macro_definitions (expr_block
, expr_pc
, buf
);
324 /* Do not generate local variable information for "raw"
325 compilations. In this case we aren't emitting our own function
326 and the user's code may only refer to globals. */
327 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
329 unsigned char *registers_used
;
332 /* Generate the code to compute variable locations, but do it
333 before generating the function header, so we can define the
334 register struct before the function body. This requires a
336 var_stream
= mem_fileopen ();
337 make_cleanup_ui_file_delete (var_stream
);
338 registers_used
= generate_c_for_variable_locations (context
,
340 expr_block
, expr_pc
);
341 make_cleanup (xfree
, registers_used
);
343 generate_register_struct (buf
, gdbarch
, registers_used
);
345 fputs_unfiltered ("typedef unsigned int"
346 " __attribute__ ((__mode__(__pointer__)))"
349 fputs_unfiltered ("typedef int"
350 " __attribute__ ((__mode__(__pointer__)))"
354 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
355 for (i
= 0; i
< 4; ++i
)
357 const char *mode
= c_get_mode_for_size (1 << i
);
359 gdb_assert (mode
!= NULL
);
360 fprintf_unfiltered (buf
,
362 " __attribute__ ((__mode__(__%s__)))"
368 add_code_header (inst
->scope
, buf
);
370 if (inst
->scope
== COMPILE_I_SIMPLE_SCOPE
)
372 ui_file_put (var_stream
, ui_file_write_for_put
, buf
);
373 fputs_unfiltered ("#pragma GCC user_expression\n", buf
);
376 /* The user expression has to be in its own scope, so that "extern"
377 works properly. Otherwise gcc thinks that the "extern"
378 declaration is in the same scope as the declaration provided by
380 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
381 fputs_unfiltered ("{\n", buf
);
383 fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf
);
384 fputs_unfiltered (input
, buf
);
385 fputs_unfiltered ("\n", buf
);
387 /* For larger user expressions the automatic semicolons may be
389 if (strchr (input
, '\n') == NULL
)
390 fputs_unfiltered (";\n", buf
);
392 if (inst
->scope
!= COMPILE_I_RAW_SCOPE
)
393 fputs_unfiltered ("}\n", buf
);
395 add_code_footer (inst
->scope
, buf
);
396 code
= ui_file_xstrdup (buf
, NULL
);
397 do_cleanups (cleanup
);