Commit | Line | Data |
---|---|---|
bb2ec1b3 TT |
1 | /* C language support for compilation. |
2 | ||
32d0add0 | 3 | Copyright (C) 2014-2015 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" | |
22 | #include "compile.h" | |
23 | #include "gdb-dlfcn.h" | |
24 | #include "c-lang.h" | |
25 | #include "macrotab.h" | |
26 | #include "macroscope.h" | |
27 | #include "regcache.h" | |
28 | ||
29 | /* See compile-internal.h. */ | |
30 | ||
31 | const char * | |
32 | c_get_mode_for_size (int size) | |
33 | { | |
34 | const char *mode = NULL; | |
35 | ||
36 | switch (size) | |
37 | { | |
38 | case 1: | |
39 | mode = "QI"; | |
40 | break; | |
41 | case 2: | |
42 | mode = "HI"; | |
43 | break; | |
44 | case 4: | |
45 | mode = "SI"; | |
46 | break; | |
47 | case 8: | |
48 | mode = "DI"; | |
49 | break; | |
50 | default: | |
51 | internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size); | |
52 | } | |
53 | ||
54 | return mode; | |
55 | } | |
56 | ||
57 | /* See compile-internal.h. */ | |
58 | ||
59 | char * | |
60 | c_get_range_decl_name (const struct dynamic_prop *prop) | |
61 | { | |
62 | return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop)); | |
63 | } | |
64 | ||
65 | \f | |
66 | ||
67 | #define STR(x) #x | |
68 | #define STRINGIFY(x) STR(x) | |
69 | ||
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 | { | |
77 | void *handle; | |
78 | gcc_c_fe_context_function *func; | |
79 | ||
80 | /* gdb_dlopen will call error () on an error, so no need to check | |
81 | value. */ | |
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)); | |
85 | ||
86 | if (func == NULL) | |
87 | error (_("could not find symbol %s in library %s"), | |
88 | STRINGIFY (GCC_C_FE_CONTEXT), | |
89 | STRINGIFY (GCC_C_FE_LIBCC)); | |
90 | return func; | |
91 | } | |
92 | ||
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. */ | |
96 | ||
97 | struct compile_instance * | |
98 | c_get_compile_context (void) | |
99 | { | |
100 | static gcc_c_fe_context_function *func; | |
101 | ||
102 | struct gcc_c_context *context; | |
103 | ||
104 | if (func == NULL) | |
105 | { | |
106 | func = load_libcc (); | |
107 | gdb_assert (func != NULL); | |
108 | } | |
109 | ||
110 | context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0); | |
111 | if (context == NULL) | |
112 | error (_("The loaded version of GCC does not support the required version " | |
113 | "of the API.")); | |
114 | ||
115 | return new_compile_instance (context); | |
116 | } | |
117 | ||
118 | \f | |
119 | ||
120 | /* Write one macro definition. */ | |
121 | ||
122 | static void | |
123 | print_one_macro (const char *name, const struct macro_definition *macro, | |
124 | struct macro_source_file *source, int line, | |
125 | void *user_data) | |
126 | { | |
127 | struct ui_file *file = user_data; | |
128 | ||
129 | /* Don't print command-line defines. They will be supplied another | |
130 | way. */ | |
131 | if (line == 0) | |
132 | return; | |
133 | ||
134 | fprintf_filtered (file, "#define %s", name); | |
135 | ||
136 | if (macro->kind == macro_function_like) | |
137 | { | |
138 | int i; | |
139 | ||
140 | fputs_filtered ("(", file); | |
141 | for (i = 0; i < macro->argc; i++) | |
142 | { | |
143 | fputs_filtered (macro->argv[i], file); | |
144 | if (i + 1 < macro->argc) | |
145 | fputs_filtered (", ", file); | |
146 | } | |
147 | fputs_filtered (")", file); | |
148 | } | |
149 | ||
150 | fprintf_filtered (file, " %s\n", macro->replacement); | |
151 | } | |
152 | ||
153 | /* Write macro definitions at PC to FILE. */ | |
154 | ||
155 | static void | |
156 | write_macro_definitions (const struct block *block, CORE_ADDR pc, | |
157 | struct ui_file *file) | |
158 | { | |
159 | struct macro_scope *scope; | |
160 | ||
161 | if (block != NULL) | |
162 | scope = sal_macro_scope (find_pc_line (pc, 0)); | |
163 | else | |
164 | scope = default_macro_scope (); | |
165 | if (scope == NULL) | |
166 | scope = user_macro_scope (); | |
167 | ||
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); | |
170 | } | |
171 | ||
172 | /* Helper function to construct a header scope for a block of code. | |
173 | Takes a scope argument which selects the correct header to | |
174 | insert into BUF. */ | |
175 | ||
176 | static void | |
177 | add_code_header (enum compile_i_scope_types type, struct ui_file *buf) | |
178 | { | |
179 | switch (type) | |
180 | { | |
181 | case COMPILE_I_SIMPLE_SCOPE: | |
182 | fputs_unfiltered ("void " | |
183 | GCC_FE_WRAPPER_FUNCTION | |
184 | " (struct " | |
185 | COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG | |
186 | " *" | |
187 | COMPILE_I_SIMPLE_REGISTER_ARG_NAME | |
188 | ") {\n", | |
189 | buf); | |
190 | break; | |
191 | case COMPILE_I_RAW_SCOPE: | |
192 | break; | |
193 | default: | |
194 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
195 | } | |
196 | } | |
197 | ||
198 | /* Helper function to construct a footer scope for a block of code. | |
199 | Takes a scope argument which selects the correct footer to | |
200 | insert into BUF. */ | |
201 | ||
202 | static void | |
203 | add_code_footer (enum compile_i_scope_types type, struct ui_file *buf) | |
204 | { | |
205 | switch (type) | |
206 | { | |
207 | case COMPILE_I_SIMPLE_SCOPE: | |
208 | fputs_unfiltered ("}\n", buf); | |
209 | break; | |
210 | case COMPILE_I_RAW_SCOPE: | |
211 | break; | |
212 | default: | |
213 | gdb_assert_not_reached (_("Unknown compiler scope reached.")); | |
214 | } | |
215 | } | |
216 | ||
217 | /* Generate a structure holding all the registers used by the function | |
218 | we're generating. */ | |
219 | ||
220 | static void | |
221 | generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, | |
222 | const unsigned char *registers_used) | |
223 | { | |
224 | int i; | |
225 | int seen = 0; | |
226 | ||
227 | fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n", | |
228 | stream); | |
229 | ||
230 | if (registers_used != NULL) | |
231 | for (i = 0; i < gdbarch_num_regs (gdbarch); ++i) | |
232 | { | |
233 | if (registers_used[i]) | |
234 | { | |
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); | |
238 | ||
239 | seen = 1; | |
240 | ||
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. */ | |
250 | ||
251 | fputs_unfiltered (" ", stream); | |
252 | switch (TYPE_CODE (regtype)) | |
253 | { | |
254 | case TYPE_CODE_PTR: | |
255 | fprintf_filtered (stream, "void *%s", regname); | |
256 | break; | |
257 | ||
258 | case TYPE_CODE_INT: | |
259 | { | |
260 | const char *mode | |
261 | = c_get_mode_for_size (TYPE_LENGTH (regtype)); | |
262 | ||
263 | if (mode != NULL) | |
264 | { | |
265 | if (TYPE_UNSIGNED (regtype)) | |
266 | fputs_unfiltered ("unsigned ", stream); | |
267 | fprintf_unfiltered (stream, | |
268 | "int %s" | |
269 | " __attribute__ ((__mode__(__%s__)))", | |
270 | regname, | |
271 | mode); | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | /* Fall through. */ | |
277 | ||
278 | default: | |
279 | fprintf_unfiltered (stream, | |
280 | " unsigned char %s[%d]" | |
281 | " __attribute__((__aligned__(" | |
282 | "__BIGGEST_ALIGNMENT__)))", | |
283 | regname, | |
284 | TYPE_LENGTH (regtype)); | |
285 | } | |
286 | fputs_unfiltered (";\n", stream); | |
287 | ||
288 | do_cleanups (cleanups); | |
289 | } | |
290 | } | |
291 | ||
292 | if (!seen) | |
293 | fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n", | |
294 | stream); | |
295 | ||
296 | fputs_unfiltered ("};\n\n", stream); | |
297 | } | |
298 | ||
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. */ | |
306 | ||
307 | char * | |
308 | c_compute_program (struct compile_instance *inst, | |
309 | const char *input, | |
310 | struct gdbarch *gdbarch, | |
311 | const struct block *expr_block, | |
312 | CORE_ADDR expr_pc) | |
313 | { | |
314 | struct ui_file *buf, *var_stream = NULL; | |
315 | char *code; | |
316 | struct cleanup *cleanup; | |
317 | struct compile_c_instance *context = (struct compile_c_instance *) inst; | |
318 | ||
319 | buf = mem_fileopen (); | |
320 | cleanup = make_cleanup_ui_file_delete (buf); | |
321 | ||
322 | write_macro_definitions (expr_block, expr_pc, buf); | |
323 | ||
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) | |
328 | { | |
329 | unsigned char *registers_used; | |
330 | int i; | |
331 | ||
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 | |
335 | temporary stream. */ | |
336 | var_stream = mem_fileopen (); | |
337 | make_cleanup_ui_file_delete (var_stream); | |
338 | registers_used = generate_c_for_variable_locations (context, | |
339 | var_stream, gdbarch, | |
340 | expr_block, expr_pc); | |
341 | make_cleanup (xfree, registers_used); | |
342 | ||
343 | generate_register_struct (buf, gdbarch, registers_used); | |
344 | ||
345 | fputs_unfiltered ("typedef unsigned int" | |
346 | " __attribute__ ((__mode__(__pointer__)))" | |
347 | " __gdb_uintptr;\n", | |
348 | buf); | |
349 | fputs_unfiltered ("typedef int" | |
350 | " __attribute__ ((__mode__(__pointer__)))" | |
351 | " __gdb_intptr;\n", | |
352 | buf); | |
353 | ||
bb2b33b9 | 354 | /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */ |
bb2ec1b3 TT |
355 | for (i = 0; i < 4; ++i) |
356 | { | |
357 | const char *mode = c_get_mode_for_size (1 << i); | |
358 | ||
359 | gdb_assert (mode != NULL); | |
360 | fprintf_unfiltered (buf, | |
361 | "typedef int" | |
362 | " __attribute__ ((__mode__(__%s__)))" | |
363 | " __gdb_int_%s;\n", | |
364 | mode, mode); | |
365 | } | |
366 | } | |
367 | ||
368 | add_code_header (inst->scope, buf); | |
369 | ||
370 | if (inst->scope == COMPILE_I_SIMPLE_SCOPE) | |
371 | { | |
372 | ui_file_put (var_stream, ui_file_write_for_put, buf); | |
373 | fputs_unfiltered ("#pragma GCC user_expression\n", buf); | |
374 | } | |
375 | ||
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 | |
379 | gdb. */ | |
380 | if (inst->scope != COMPILE_I_RAW_SCOPE) | |
381 | fputs_unfiltered ("{\n", buf); | |
382 | ||
383 | fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf); | |
384 | fputs_unfiltered (input, buf); | |
385 | fputs_unfiltered ("\n", buf); | |
386 | ||
387 | /* For larger user expressions the automatic semicolons may be | |
388 | confusing. */ | |
389 | if (strchr (input, '\n') == NULL) | |
390 | fputs_unfiltered (";\n", buf); | |
391 | ||
392 | if (inst->scope != COMPILE_I_RAW_SCOPE) | |
393 | fputs_unfiltered ("}\n", buf); | |
394 | ||
395 | add_code_footer (inst->scope, buf); | |
396 | code = ui_file_xstrdup (buf, NULL); | |
397 | do_cleanups (cleanup); | |
398 | return code; | |
399 | } |