Add some more casts (1/2)
[deliverable/binutils-gdb.git] / gdb / compile / compile-c-support.c
CommitLineData
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
31const char *
32c_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
59char *
60c_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
74static gcc_c_fe_context_function *
75load_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
97struct compile_instance *
98c_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
122static void
123print_one_macro (const char *name, const struct macro_definition *macro,
124 struct macro_source_file *source, int line,
125 void *user_data)
126{
9a3c8263 127 struct ui_file *file = (struct ui_file *) user_data;
bb2ec1b3
TT
128
129 /* Don't print command-line defines. They will be supplied another
130 way. */
131 if (line == 0)
132 return;
133
3a9558c4
JK
134 /* None of -Wno-builtin-macro-redefined, #undef first
135 or plain #define of the same value would avoid a warning. */
136 fprintf_filtered (file, "#ifndef %s\n# define %s", name, name);
bb2ec1b3
TT
137
138 if (macro->kind == macro_function_like)
139 {
140 int i;
141
142 fputs_filtered ("(", file);
143 for (i = 0; i < macro->argc; i++)
144 {
145 fputs_filtered (macro->argv[i], file);
146 if (i + 1 < macro->argc)
147 fputs_filtered (", ", file);
148 }
149 fputs_filtered (")", file);
150 }
151
3a9558c4 152 fprintf_filtered (file, " %s\n#endif\n", macro->replacement);
bb2ec1b3
TT
153}
154
155/* Write macro definitions at PC to FILE. */
156
157static void
158write_macro_definitions (const struct block *block, CORE_ADDR pc,
159 struct ui_file *file)
160{
161 struct macro_scope *scope;
162
163 if (block != NULL)
164 scope = sal_macro_scope (find_pc_line (pc, 0));
165 else
166 scope = default_macro_scope ();
167 if (scope == NULL)
168 scope = user_macro_scope ();
169
170 if (scope != NULL && scope->file != NULL && scope->file->table != NULL)
171 macro_for_each_in_scope (scope->file, scope->line, print_one_macro, file);
172}
173
174/* Helper function to construct a header scope for a block of code.
175 Takes a scope argument which selects the correct header to
176 insert into BUF. */
177
178static void
179add_code_header (enum compile_i_scope_types type, struct ui_file *buf)
180{
181 switch (type)
182 {
183 case COMPILE_I_SIMPLE_SCOPE:
184 fputs_unfiltered ("void "
185 GCC_FE_WRAPPER_FUNCTION
186 " (struct "
187 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
188 " *"
189 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
190 ") {\n",
191 buf);
192 break;
36de76f9
JK
193 case COMPILE_I_PRINT_ADDRESS_SCOPE:
194 case COMPILE_I_PRINT_VALUE_SCOPE:
195 /* <string.h> is needed for a memcpy call below. */
196 fputs_unfiltered ("#include <string.h>\n"
197 "void "
198 GCC_FE_WRAPPER_FUNCTION
199 " (struct "
200 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG
201 " *"
202 COMPILE_I_SIMPLE_REGISTER_ARG_NAME
203 ", "
204 COMPILE_I_PRINT_OUT_ARG_TYPE
205 " "
206 COMPILE_I_PRINT_OUT_ARG
207 ") {\n",
208 buf);
209 break;
210
bb2ec1b3
TT
211 case COMPILE_I_RAW_SCOPE:
212 break;
213 default:
214 gdb_assert_not_reached (_("Unknown compiler scope reached."));
215 }
216}
217
218/* Helper function to construct a footer scope for a block of code.
219 Takes a scope argument which selects the correct footer to
220 insert into BUF. */
221
222static void
223add_code_footer (enum compile_i_scope_types type, struct ui_file *buf)
224{
225 switch (type)
226 {
227 case COMPILE_I_SIMPLE_SCOPE:
36de76f9
JK
228 case COMPILE_I_PRINT_ADDRESS_SCOPE:
229 case COMPILE_I_PRINT_VALUE_SCOPE:
bb2ec1b3
TT
230 fputs_unfiltered ("}\n", buf);
231 break;
232 case COMPILE_I_RAW_SCOPE:
233 break;
234 default:
235 gdb_assert_not_reached (_("Unknown compiler scope reached."));
236 }
237}
238
239/* Generate a structure holding all the registers used by the function
240 we're generating. */
241
242static void
243generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch,
244 const unsigned char *registers_used)
245{
246 int i;
247 int seen = 0;
248
249 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n",
250 stream);
251
252 if (registers_used != NULL)
253 for (i = 0; i < gdbarch_num_regs (gdbarch); ++i)
254 {
255 if (registers_used[i])
256 {
257 struct type *regtype = check_typedef (register_type (gdbarch, i));
258 char *regname = compile_register_name_mangled (gdbarch, i);
259 struct cleanup *cleanups = make_cleanup (xfree, regname);
260
261 seen = 1;
262
263 /* You might think we could use type_print here. However,
264 target descriptions often use types with names like
265 "int64_t", which may not be defined in the inferior
266 (and in any case would not be looked up due to the
267 #pragma business). So, we take a much simpler
268 approach: for pointer- or integer-typed registers, emit
269 the field in the most direct way; and for other
270 register types (typically flags or vectors), emit a
271 maximally-aligned array of the correct size. */
272
273 fputs_unfiltered (" ", stream);
274 switch (TYPE_CODE (regtype))
275 {
276 case TYPE_CODE_PTR:
3a9558c4 277 fprintf_filtered (stream, "__gdb_uintptr %s", regname);
bb2ec1b3
TT
278 break;
279
280 case TYPE_CODE_INT:
281 {
282 const char *mode
283 = c_get_mode_for_size (TYPE_LENGTH (regtype));
284
285 if (mode != NULL)
286 {
287 if (TYPE_UNSIGNED (regtype))
288 fputs_unfiltered ("unsigned ", stream);
289 fprintf_unfiltered (stream,
290 "int %s"
291 " __attribute__ ((__mode__(__%s__)))",
292 regname,
293 mode);
294 break;
295 }
296 }
297
298 /* Fall through. */
299
300 default:
301 fprintf_unfiltered (stream,
302 " unsigned char %s[%d]"
303 " __attribute__((__aligned__("
304 "__BIGGEST_ALIGNMENT__)))",
305 regname,
306 TYPE_LENGTH (regtype));
307 }
308 fputs_unfiltered (";\n", stream);
309
310 do_cleanups (cleanups);
311 }
312 }
313
314 if (!seen)
315 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n",
316 stream);
317
318 fputs_unfiltered ("};\n\n", stream);
319}
320
321/* Take the source code provided by the user with the 'compile'
322 command, and compute the additional wrapping, macro, variable and
323 register operations needed. INPUT is the source code derived from
324 the 'compile' command, GDBARCH is the architecture to use when
325 computing above, EXPR_BLOCK denotes the block relevant contextually
326 to the inferior when the expression was created, and EXPR_PC
327 indicates the value of $PC. */
328
329char *
330c_compute_program (struct compile_instance *inst,
331 const char *input,
332 struct gdbarch *gdbarch,
333 const struct block *expr_block,
334 CORE_ADDR expr_pc)
335{
336 struct ui_file *buf, *var_stream = NULL;
337 char *code;
338 struct cleanup *cleanup;
339 struct compile_c_instance *context = (struct compile_c_instance *) inst;
340
341 buf = mem_fileopen ();
342 cleanup = make_cleanup_ui_file_delete (buf);
343
344 write_macro_definitions (expr_block, expr_pc, buf);
345
346 /* Do not generate local variable information for "raw"
347 compilations. In this case we aren't emitting our own function
348 and the user's code may only refer to globals. */
349 if (inst->scope != COMPILE_I_RAW_SCOPE)
350 {
351 unsigned char *registers_used;
352 int i;
353
354 /* Generate the code to compute variable locations, but do it
355 before generating the function header, so we can define the
356 register struct before the function body. This requires a
357 temporary stream. */
358 var_stream = mem_fileopen ();
359 make_cleanup_ui_file_delete (var_stream);
360 registers_used = generate_c_for_variable_locations (context,
361 var_stream, gdbarch,
362 expr_block, expr_pc);
363 make_cleanup (xfree, registers_used);
364
bb2ec1b3
TT
365 fputs_unfiltered ("typedef unsigned int"
366 " __attribute__ ((__mode__(__pointer__)))"
367 " __gdb_uintptr;\n",
368 buf);
369 fputs_unfiltered ("typedef int"
370 " __attribute__ ((__mode__(__pointer__)))"
371 " __gdb_intptr;\n",
372 buf);
373
bb2b33b9 374 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */
bb2ec1b3
TT
375 for (i = 0; i < 4; ++i)
376 {
377 const char *mode = c_get_mode_for_size (1 << i);
378
379 gdb_assert (mode != NULL);
380 fprintf_unfiltered (buf,
381 "typedef int"
382 " __attribute__ ((__mode__(__%s__)))"
383 " __gdb_int_%s;\n",
384 mode, mode);
385 }
3a9558c4
JK
386
387 generate_register_struct (buf, gdbarch, registers_used);
bb2ec1b3
TT
388 }
389
390 add_code_header (inst->scope, buf);
391
36de76f9
JK
392 if (inst->scope == COMPILE_I_SIMPLE_SCOPE
393 || inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
394 || inst->scope == COMPILE_I_PRINT_VALUE_SCOPE)
bb2ec1b3
TT
395 {
396 ui_file_put (var_stream, ui_file_write_for_put, buf);
397 fputs_unfiltered ("#pragma GCC user_expression\n", buf);
398 }
399
400 /* The user expression has to be in its own scope, so that "extern"
401 works properly. Otherwise gcc thinks that the "extern"
402 declaration is in the same scope as the declaration provided by
403 gdb. */
404 if (inst->scope != COMPILE_I_RAW_SCOPE)
405 fputs_unfiltered ("{\n", buf);
406
407 fputs_unfiltered ("#line 1 \"gdb command line\"\n", buf);
36de76f9
JK
408
409 switch (inst->scope)
410 {
411 case COMPILE_I_PRINT_ADDRESS_SCOPE:
412 case COMPILE_I_PRINT_VALUE_SCOPE:
413 fprintf_unfiltered (buf,
414"__auto_type " COMPILE_I_EXPR_VAL " = %s;\n"
415"typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n"
416"memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" COMPILE_I_EXPR_VAL ",\n"
417 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n"
418 , input, input,
419 (inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE
420 ? "&" : ""));
421 break;
422 default:
423 fputs_unfiltered (input, buf);
424 break;
425 }
426
bb2ec1b3
TT
427 fputs_unfiltered ("\n", buf);
428
429 /* For larger user expressions the automatic semicolons may be
430 confusing. */
431 if (strchr (input, '\n') == NULL)
432 fputs_unfiltered (";\n", buf);
433
434 if (inst->scope != COMPILE_I_RAW_SCOPE)
435 fputs_unfiltered ("}\n", buf);
436
437 add_code_footer (inst->scope, buf);
438 code = ui_file_xstrdup (buf, NULL);
439 do_cleanups (cleanup);
440 return code;
441}
This page took 0.150401 seconds and 4 git commands to generate.