Commit | Line | Data |
---|---|---|
bb2ec1b3 TT |
1 | /* Convert symbols from GDB to GCC |
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 | ||
21 | #include "defs.h" | |
22 | #include "compile-internal.h" | |
bb2ec1b3 TT |
23 | #include "symtab.h" |
24 | #include "parser-defs.h" | |
25 | #include "block.h" | |
26 | #include "objfiles.h" | |
27 | #include "compile.h" | |
28 | #include "value.h" | |
29 | #include "exceptions.h" | |
30 | #include "gdbtypes.h" | |
31 | #include "dwarf2loc.h" | |
32 | ||
33 | \f | |
34 | ||
35 | /* Object of this type are stored in the compiler's symbol_err_map. */ | |
36 | ||
37 | struct symbol_error | |
38 | { | |
39 | /* The symbol. */ | |
40 | ||
41 | const struct symbol *sym; | |
42 | ||
43 | /* The error message to emit. This is malloc'd and owned by the | |
44 | hash table. */ | |
45 | ||
46 | char *message; | |
47 | }; | |
48 | ||
49 | /* Hash function for struct symbol_error. */ | |
50 | ||
51 | static hashval_t | |
52 | hash_symbol_error (const void *a) | |
53 | { | |
54 | const struct symbol_error *se = a; | |
55 | ||
56 | return htab_hash_pointer (se->sym); | |
57 | } | |
58 | ||
59 | /* Equality function for struct symbol_error. */ | |
60 | ||
61 | static int | |
62 | eq_symbol_error (const void *a, const void *b) | |
63 | { | |
64 | const struct symbol_error *sea = a; | |
65 | const struct symbol_error *seb = b; | |
66 | ||
67 | return sea->sym == seb->sym; | |
68 | } | |
69 | ||
70 | /* Deletion function for struct symbol_error. */ | |
71 | ||
72 | static void | |
73 | del_symbol_error (void *a) | |
74 | { | |
75 | struct symbol_error *se = a; | |
76 | ||
77 | xfree (se->message); | |
78 | xfree (se); | |
79 | } | |
80 | ||
81 | /* Associate SYMBOL with some error text. */ | |
82 | ||
83 | static void | |
84 | insert_symbol_error (htab_t hash, const struct symbol *sym, const char *text) | |
85 | { | |
86 | struct symbol_error e; | |
87 | void **slot; | |
88 | ||
89 | e.sym = sym; | |
90 | slot = htab_find_slot (hash, &e, INSERT); | |
91 | if (*slot == NULL) | |
92 | { | |
93 | struct symbol_error *e = XNEW (struct symbol_error); | |
94 | ||
95 | e->sym = sym; | |
96 | e->message = xstrdup (text); | |
97 | *slot = e; | |
98 | } | |
99 | } | |
100 | ||
101 | /* Emit the error message corresponding to SYM, if one exists, and | |
102 | arrange for it not to be emitted again. */ | |
103 | ||
104 | static void | |
105 | error_symbol_once (struct compile_c_instance *context, | |
106 | const struct symbol *sym) | |
107 | { | |
108 | struct symbol_error search; | |
109 | struct symbol_error *err; | |
110 | char *message; | |
111 | ||
112 | if (context->symbol_err_map == NULL) | |
113 | return; | |
114 | ||
115 | search.sym = sym; | |
116 | err = htab_find (context->symbol_err_map, &search); | |
117 | if (err == NULL || err->message == NULL) | |
118 | return; | |
119 | ||
120 | message = err->message; | |
121 | err->message = NULL; | |
122 | make_cleanup (xfree, message); | |
123 | error (_("%s"), message); | |
124 | } | |
125 | ||
126 | \f | |
127 | ||
128 | /* Compute the name of the pointer representing a local symbol's | |
129 | address. */ | |
130 | ||
131 | static char * | |
132 | symbol_substitution_name (struct symbol *sym) | |
133 | { | |
134 | return concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL); | |
135 | } | |
136 | ||
137 | /* Convert a given symbol, SYM, to the compiler's representation. | |
138 | CONTEXT is the compiler instance. IS_GLOBAL is true if the | |
139 | symbol came from the global scope. IS_LOCAL is true if the symbol | |
140 | came from a local scope. (Note that the two are not strictly | |
141 | inverses because the symbol might have come from the static | |
142 | scope.) */ | |
143 | ||
144 | static void | |
145 | convert_one_symbol (struct compile_c_instance *context, | |
146 | struct symbol *sym, | |
147 | int is_global, | |
148 | int is_local) | |
149 | { | |
150 | gcc_type sym_type; | |
08be3fe3 | 151 | const char *filename = symbol_symtab (sym)->filename; |
bb2ec1b3 TT |
152 | unsigned short line = SYMBOL_LINE (sym); |
153 | ||
154 | error_symbol_once (context, sym); | |
155 | ||
156 | if (SYMBOL_CLASS (sym) == LOC_LABEL) | |
157 | sym_type = 0; | |
158 | else | |
159 | sym_type = convert_type (context, SYMBOL_TYPE (sym)); | |
160 | ||
161 | if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN) | |
162 | { | |
163 | /* Binding a tag, so we don't need to build a decl. */ | |
164 | C_CTX (context)->c_ops->tagbind (C_CTX (context), | |
165 | SYMBOL_NATURAL_NAME (sym), | |
166 | sym_type, filename, line); | |
167 | } | |
168 | else | |
169 | { | |
170 | gcc_decl decl; | |
171 | enum gcc_c_symbol_kind kind; | |
172 | CORE_ADDR addr = 0; | |
173 | char *symbol_name = NULL; | |
174 | ||
175 | switch (SYMBOL_CLASS (sym)) | |
176 | { | |
177 | case LOC_TYPEDEF: | |
178 | kind = GCC_C_SYMBOL_TYPEDEF; | |
179 | break; | |
180 | ||
181 | case LOC_LABEL: | |
182 | kind = GCC_C_SYMBOL_LABEL; | |
183 | addr = SYMBOL_VALUE_ADDRESS (sym); | |
184 | break; | |
185 | ||
186 | case LOC_BLOCK: | |
187 | kind = GCC_C_SYMBOL_FUNCTION; | |
188 | addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); | |
081a1c2c JK |
189 | if (is_global && TYPE_GNU_IFUNC (SYMBOL_TYPE (sym))) |
190 | addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr); | |
bb2ec1b3 TT |
191 | break; |
192 | ||
193 | case LOC_CONST: | |
194 | if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM) | |
195 | { | |
196 | /* Already handled by convert_enum. */ | |
197 | return; | |
198 | } | |
199 | C_CTX (context)->c_ops->build_constant (C_CTX (context), sym_type, | |
200 | SYMBOL_NATURAL_NAME (sym), | |
201 | SYMBOL_VALUE (sym), | |
202 | filename, line); | |
203 | return; | |
204 | ||
205 | case LOC_CONST_BYTES: | |
206 | error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."), | |
207 | SYMBOL_PRINT_NAME (sym)); | |
208 | ||
209 | case LOC_UNDEF: | |
210 | internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."), | |
211 | SYMBOL_PRINT_NAME (sym)); | |
212 | ||
213 | case LOC_COMMON_BLOCK: | |
214 | error (_("Fortran common block is unsupported for compilation " | |
215 | "evaluaton of symbol \"%s\"."), | |
216 | SYMBOL_PRINT_NAME (sym)); | |
217 | ||
218 | case LOC_OPTIMIZED_OUT: | |
219 | error (_("Symbol \"%s\" cannot be used for compilation evaluation " | |
220 | "as it is optimized out."), | |
221 | SYMBOL_PRINT_NAME (sym)); | |
222 | ||
223 | case LOC_COMPUTED: | |
224 | if (is_local) | |
225 | goto substitution; | |
226 | /* Probably TLS here. */ | |
227 | warning (_("Symbol \"%s\" is thread-local and currently can only " | |
228 | "be referenced from the current thread in " | |
229 | "compiled code."), | |
230 | SYMBOL_PRINT_NAME (sym)); | |
231 | /* FALLTHROUGH */ | |
232 | case LOC_UNRESOLVED: | |
233 | /* 'symbol_name' cannot be used here as that one is used only for | |
234 | local variables from compile_dwarf_expr_to_c. | |
235 | Global variables can be accessed by GCC only by their address, not | |
236 | by their name. */ | |
237 | { | |
238 | struct value *val; | |
239 | struct frame_info *frame = NULL; | |
240 | ||
241 | if (symbol_read_needs_frame (sym)) | |
242 | { | |
243 | frame = get_selected_frame (NULL); | |
244 | if (frame == NULL) | |
245 | error (_("Symbol \"%s\" cannot be used because " | |
246 | "there is no selected frame"), | |
247 | SYMBOL_PRINT_NAME (sym)); | |
248 | } | |
249 | ||
250 | val = read_var_value (sym, frame); | |
251 | if (VALUE_LVAL (val) != lval_memory) | |
252 | error (_("Symbol \"%s\" cannot be used for compilation " | |
253 | "evaluation as its address has not been found."), | |
254 | SYMBOL_PRINT_NAME (sym)); | |
255 | ||
256 | kind = GCC_C_SYMBOL_VARIABLE; | |
257 | addr = value_address (val); | |
258 | } | |
259 | break; | |
260 | ||
261 | ||
262 | case LOC_REGISTER: | |
263 | case LOC_ARG: | |
264 | case LOC_REF_ARG: | |
265 | case LOC_REGPARM_ADDR: | |
266 | case LOC_LOCAL: | |
267 | substitution: | |
268 | kind = GCC_C_SYMBOL_VARIABLE; | |
269 | symbol_name = symbol_substitution_name (sym); | |
270 | break; | |
271 | ||
272 | case LOC_STATIC: | |
273 | kind = GCC_C_SYMBOL_VARIABLE; | |
274 | addr = SYMBOL_VALUE_ADDRESS (sym); | |
275 | break; | |
276 | ||
277 | case LOC_FINAL_VALUE: | |
278 | default: | |
279 | gdb_assert_not_reached ("Unreachable case in convert_one_symbol."); | |
280 | ||
281 | } | |
282 | ||
283 | /* Don't emit local variable decls for a raw expression. */ | |
284 | if (context->base.scope != COMPILE_I_RAW_SCOPE | |
285 | || symbol_name == NULL) | |
286 | { | |
287 | decl = C_CTX (context)->c_ops->build_decl (C_CTX (context), | |
288 | SYMBOL_NATURAL_NAME (sym), | |
289 | kind, | |
290 | sym_type, | |
291 | symbol_name, addr, | |
292 | filename, line); | |
293 | ||
294 | C_CTX (context)->c_ops->bind (C_CTX (context), decl, is_global); | |
295 | } | |
296 | ||
297 | xfree (symbol_name); | |
298 | } | |
299 | } | |
300 | ||
301 | /* Convert a full symbol to its gcc form. CONTEXT is the compiler to | |
302 | use, IDENTIFIER is the name of the symbol, SYM is the symbol | |
303 | itself, and DOMAIN is the domain which was searched. */ | |
304 | ||
305 | static void | |
306 | convert_symbol_sym (struct compile_c_instance *context, const char *identifier, | |
d12307c1 | 307 | struct block_symbol sym, domain_enum domain) |
bb2ec1b3 | 308 | { |
d12307c1 | 309 | const struct block *static_block; |
bb2ec1b3 TT |
310 | int is_local_symbol; |
311 | ||
bb2ec1b3 TT |
312 | /* If we found a symbol and it is not in the static or global |
313 | scope, then we should first convert any static or global scope | |
314 | symbol of the same name. This lets this unusual case work: | |
315 | ||
316 | int x; // Global. | |
317 | int func(void) | |
318 | { | |
319 | int x; | |
320 | // At this spot, evaluate "extern int x; x" | |
321 | } | |
322 | */ | |
323 | ||
d12307c1 | 324 | static_block = block_static_block (sym.block); |
bb2ec1b3 | 325 | /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */ |
d12307c1 | 326 | is_local_symbol = (sym.block != static_block && static_block != NULL); |
bb2ec1b3 TT |
327 | if (is_local_symbol) |
328 | { | |
d12307c1 | 329 | struct block_symbol global_sym; |
bb2ec1b3 TT |
330 | |
331 | global_sym = lookup_symbol (identifier, NULL, domain, NULL); | |
332 | /* If the outer symbol is in the static block, we ignore it, as | |
333 | it cannot be referenced. */ | |
d12307c1 PMR |
334 | if (global_sym.symbol != NULL |
335 | && global_sym.block != block_static_block (global_sym.block)) | |
bb2ec1b3 TT |
336 | { |
337 | if (compile_debug) | |
a4063588 | 338 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
339 | "gcc_convert_symbol \"%s\": global symbol\n", |
340 | identifier); | |
d12307c1 | 341 | convert_one_symbol (context, global_sym.symbol, 1, 0); |
bb2ec1b3 TT |
342 | } |
343 | } | |
344 | ||
345 | if (compile_debug) | |
a4063588 | 346 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
347 | "gcc_convert_symbol \"%s\": local symbol\n", |
348 | identifier); | |
d12307c1 | 349 | convert_one_symbol (context, sym.symbol, 0, is_local_symbol); |
bb2ec1b3 TT |
350 | } |
351 | ||
352 | /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler | |
353 | to use and BMSYM is the minimal symbol to convert. */ | |
354 | ||
355 | static void | |
356 | convert_symbol_bmsym (struct compile_c_instance *context, | |
357 | struct bound_minimal_symbol bmsym) | |
358 | { | |
359 | struct minimal_symbol *msym = bmsym.minsym; | |
360 | struct objfile *objfile = bmsym.objfile; | |
361 | struct type *type; | |
362 | enum gcc_c_symbol_kind kind; | |
363 | gcc_type sym_type; | |
364 | gcc_decl decl; | |
365 | CORE_ADDR addr; | |
366 | ||
081a1c2c JK |
367 | addr = MSYMBOL_VALUE_ADDRESS (objfile, msym); |
368 | ||
bb2ec1b3 TT |
369 | /* Conversion copied from write_exp_msymbol. */ |
370 | switch (MSYMBOL_TYPE (msym)) | |
371 | { | |
372 | case mst_text: | |
373 | case mst_file_text: | |
374 | case mst_solib_trampoline: | |
375 | type = objfile_type (objfile)->nodebug_text_symbol; | |
376 | kind = GCC_C_SYMBOL_FUNCTION; | |
377 | break; | |
378 | ||
379 | case mst_text_gnu_ifunc: | |
081a1c2c JK |
380 | /* nodebug_text_gnu_ifunc_symbol would cause: |
381 | function return type cannot be function */ | |
382 | type = objfile_type (objfile)->nodebug_text_symbol; | |
bb2ec1b3 | 383 | kind = GCC_C_SYMBOL_FUNCTION; |
081a1c2c | 384 | addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr); |
bb2ec1b3 TT |
385 | break; |
386 | ||
387 | case mst_data: | |
388 | case mst_file_data: | |
389 | case mst_bss: | |
390 | case mst_file_bss: | |
391 | type = objfile_type (objfile)->nodebug_data_symbol; | |
392 | kind = GCC_C_SYMBOL_VARIABLE; | |
393 | break; | |
394 | ||
395 | case mst_slot_got_plt: | |
396 | type = objfile_type (objfile)->nodebug_got_plt_symbol; | |
397 | kind = GCC_C_SYMBOL_FUNCTION; | |
398 | break; | |
399 | ||
400 | default: | |
401 | type = objfile_type (objfile)->nodebug_unknown_symbol; | |
402 | kind = GCC_C_SYMBOL_VARIABLE; | |
403 | break; | |
404 | } | |
405 | ||
406 | sym_type = convert_type (context, type); | |
bb2ec1b3 TT |
407 | decl = C_CTX (context)->c_ops->build_decl (C_CTX (context), |
408 | MSYMBOL_NATURAL_NAME (msym), | |
409 | kind, sym_type, NULL, addr, | |
410 | NULL, 0); | |
411 | C_CTX (context)->c_ops->bind (C_CTX (context), decl, 1 /* is_global */); | |
412 | } | |
413 | ||
414 | /* See compile-internal.h. */ | |
415 | ||
416 | void | |
417 | gcc_convert_symbol (void *datum, | |
418 | struct gcc_c_context *gcc_context, | |
419 | enum gcc_c_oracle_request request, | |
420 | const char *identifier) | |
421 | { | |
422 | struct compile_c_instance *context = datum; | |
423 | domain_enum domain; | |
bb2ec1b3 TT |
424 | int found = 0; |
425 | ||
426 | switch (request) | |
427 | { | |
428 | case GCC_C_ORACLE_SYMBOL: | |
429 | domain = VAR_DOMAIN; | |
430 | break; | |
431 | case GCC_C_ORACLE_TAG: | |
432 | domain = STRUCT_DOMAIN; | |
433 | break; | |
434 | case GCC_C_ORACLE_LABEL: | |
435 | domain = LABEL_DOMAIN; | |
436 | break; | |
437 | default: | |
438 | gdb_assert_not_reached ("Unrecognized oracle request."); | |
439 | } | |
440 | ||
441 | /* We can't allow exceptions to escape out of this callback. Safest | |
442 | is to simply emit a gcc error. */ | |
492d29ea | 443 | TRY |
bb2ec1b3 | 444 | { |
d12307c1 | 445 | struct block_symbol sym; |
bb2ec1b3 TT |
446 | |
447 | sym = lookup_symbol (identifier, context->base.block, domain, NULL); | |
d12307c1 | 448 | if (sym.symbol != NULL) |
bb2ec1b3 TT |
449 | { |
450 | convert_symbol_sym (context, identifier, sym, domain); | |
451 | found = 1; | |
452 | } | |
453 | else if (domain == VAR_DOMAIN) | |
454 | { | |
455 | struct bound_minimal_symbol bmsym; | |
456 | ||
457 | bmsym = lookup_minimal_symbol (identifier, NULL, NULL); | |
458 | if (bmsym.minsym != NULL) | |
459 | { | |
460 | convert_symbol_bmsym (context, bmsym); | |
461 | found = 1; | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
492d29ea PA |
466 | CATCH (e, RETURN_MASK_ALL) |
467 | { | |
468 | C_CTX (context)->c_ops->error (C_CTX (context), e.message); | |
469 | } | |
470 | END_CATCH | |
bb2ec1b3 TT |
471 | |
472 | if (compile_debug && !found) | |
a4063588 | 473 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
474 | "gcc_convert_symbol \"%s\": lookup_symbol failed\n", |
475 | identifier); | |
476 | return; | |
477 | } | |
478 | ||
479 | /* See compile-internal.h. */ | |
480 | ||
481 | gcc_address | |
482 | gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context, | |
483 | const char *identifier) | |
484 | { | |
485 | struct compile_c_instance *context = datum; | |
bb2ec1b3 TT |
486 | gcc_address result = 0; |
487 | int found = 0; | |
488 | ||
489 | /* We can't allow exceptions to escape out of this callback. Safest | |
490 | is to simply emit a gcc error. */ | |
492d29ea | 491 | TRY |
bb2ec1b3 TT |
492 | { |
493 | struct symbol *sym; | |
494 | ||
495 | /* We only need global functions here. */ | |
d12307c1 | 496 | sym = lookup_symbol (identifier, NULL, VAR_DOMAIN, NULL).symbol; |
bb2ec1b3 TT |
497 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_BLOCK) |
498 | { | |
499 | if (compile_debug) | |
a4063588 | 500 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
501 | "gcc_symbol_address \"%s\": full symbol\n", |
502 | identifier); | |
503 | result = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); | |
081a1c2c JK |
504 | if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym))) |
505 | result = gnu_ifunc_resolve_addr (target_gdbarch (), result); | |
bb2ec1b3 TT |
506 | found = 1; |
507 | } | |
508 | else | |
509 | { | |
510 | struct bound_minimal_symbol msym; | |
511 | ||
512 | msym = lookup_bound_minimal_symbol (identifier); | |
513 | if (msym.minsym != NULL) | |
514 | { | |
515 | if (compile_debug) | |
a4063588 | 516 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
517 | "gcc_symbol_address \"%s\": minimal " |
518 | "symbol\n", | |
519 | identifier); | |
520 | result = BMSYMBOL_VALUE_ADDRESS (msym); | |
081a1c2c JK |
521 | if (MSYMBOL_TYPE (msym.minsym) == mst_text_gnu_ifunc) |
522 | result = gnu_ifunc_resolve_addr (target_gdbarch (), result); | |
bb2ec1b3 TT |
523 | found = 1; |
524 | } | |
525 | } | |
526 | } | |
527 | ||
492d29ea PA |
528 | CATCH (e, RETURN_MASK_ERROR) |
529 | { | |
530 | C_CTX (context)->c_ops->error (C_CTX (context), e.message); | |
531 | } | |
532 | END_CATCH | |
bb2ec1b3 TT |
533 | |
534 | if (compile_debug && !found) | |
a4063588 | 535 | fprintf_unfiltered (gdb_stdlog, |
bb2ec1b3 TT |
536 | "gcc_symbol_address \"%s\": failed\n", |
537 | identifier); | |
538 | return result; | |
539 | } | |
540 | ||
541 | \f | |
542 | ||
543 | /* A hash function for symbol names. */ | |
544 | ||
545 | static hashval_t | |
546 | hash_symname (const void *a) | |
547 | { | |
548 | const struct symbol *sym = a; | |
549 | ||
550 | return htab_hash_string (SYMBOL_NATURAL_NAME (sym)); | |
551 | } | |
552 | ||
553 | /* A comparison function for hash tables that just looks at symbol | |
554 | names. */ | |
555 | ||
556 | static int | |
557 | eq_symname (const void *a, const void *b) | |
558 | { | |
559 | const struct symbol *syma = a; | |
560 | const struct symbol *symb = b; | |
561 | ||
562 | return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0; | |
563 | } | |
564 | ||
565 | /* If a symbol with the same name as SYM is already in HASHTAB, return | |
566 | 1. Otherwise, add SYM to HASHTAB and return 0. */ | |
567 | ||
568 | static int | |
569 | symbol_seen (htab_t hashtab, struct symbol *sym) | |
570 | { | |
571 | void **slot; | |
572 | ||
573 | slot = htab_find_slot (hashtab, sym, INSERT); | |
574 | if (*slot != NULL) | |
575 | return 1; | |
576 | ||
577 | *slot = sym; | |
578 | return 0; | |
579 | } | |
580 | ||
581 | /* Generate C code to compute the length of a VLA. */ | |
582 | ||
583 | static void | |
584 | generate_vla_size (struct compile_c_instance *compiler, | |
585 | struct ui_file *stream, | |
586 | struct gdbarch *gdbarch, | |
587 | unsigned char *registers_used, | |
588 | CORE_ADDR pc, | |
589 | struct type *type, | |
590 | struct symbol *sym) | |
591 | { | |
592 | type = check_typedef (type); | |
593 | ||
594 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
595 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
596 | ||
597 | switch (TYPE_CODE (type)) | |
598 | { | |
599 | case TYPE_CODE_RANGE: | |
600 | { | |
601 | if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR | |
602 | || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST) | |
603 | { | |
604 | const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high; | |
605 | char *name = c_get_range_decl_name (prop); | |
606 | struct cleanup *cleanup = make_cleanup (xfree, name); | |
607 | ||
608 | dwarf2_compile_property_to_c (stream, name, | |
609 | gdbarch, registers_used, | |
610 | prop, pc, sym); | |
611 | do_cleanups (cleanup); | |
612 | } | |
613 | } | |
614 | break; | |
615 | ||
616 | case TYPE_CODE_ARRAY: | |
617 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, | |
618 | TYPE_INDEX_TYPE (type), sym); | |
619 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, | |
620 | TYPE_TARGET_TYPE (type), sym); | |
621 | break; | |
622 | ||
623 | case TYPE_CODE_UNION: | |
624 | case TYPE_CODE_STRUCT: | |
625 | { | |
626 | int i; | |
627 | ||
628 | for (i = 0; i < TYPE_NFIELDS (type); ++i) | |
629 | if (!field_is_static (&TYPE_FIELD (type, i))) | |
630 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, | |
631 | TYPE_FIELD_TYPE (type, i), sym); | |
632 | } | |
633 | break; | |
634 | } | |
635 | } | |
636 | ||
637 | /* Generate C code to compute the address of SYM. */ | |
638 | ||
639 | static void | |
640 | generate_c_for_for_one_variable (struct compile_c_instance *compiler, | |
641 | struct ui_file *stream, | |
642 | struct gdbarch *gdbarch, | |
643 | unsigned char *registers_used, | |
644 | CORE_ADDR pc, | |
645 | struct symbol *sym) | |
646 | { | |
bb2ec1b3 | 647 | |
492d29ea | 648 | TRY |
bb2ec1b3 TT |
649 | { |
650 | if (is_dynamic_type (SYMBOL_TYPE (sym))) | |
651 | { | |
652 | struct ui_file *size_file = mem_fileopen (); | |
653 | struct cleanup *cleanup = make_cleanup_ui_file_delete (size_file); | |
654 | ||
655 | generate_vla_size (compiler, size_file, gdbarch, registers_used, pc, | |
656 | SYMBOL_TYPE (sym), sym); | |
657 | ui_file_put (size_file, ui_file_write_for_put, stream); | |
658 | ||
659 | do_cleanups (cleanup); | |
660 | } | |
661 | ||
662 | if (SYMBOL_COMPUTED_OPS (sym) != NULL) | |
663 | { | |
664 | char *generated_name = symbol_substitution_name (sym); | |
665 | struct cleanup *cleanup = make_cleanup (xfree, generated_name); | |
666 | /* We need to emit to a temporary buffer in case an error | |
667 | occurs in the middle. */ | |
668 | struct ui_file *local_file = mem_fileopen (); | |
669 | ||
670 | make_cleanup_ui_file_delete (local_file); | |
671 | SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file, | |
672 | gdbarch, | |
673 | registers_used, | |
674 | pc, generated_name); | |
675 | ui_file_put (local_file, ui_file_write_for_put, stream); | |
676 | ||
677 | do_cleanups (cleanup); | |
678 | } | |
679 | else | |
680 | { | |
681 | switch (SYMBOL_CLASS (sym)) | |
682 | { | |
683 | case LOC_REGISTER: | |
684 | case LOC_ARG: | |
685 | case LOC_REF_ARG: | |
686 | case LOC_REGPARM_ADDR: | |
687 | case LOC_LOCAL: | |
688 | error (_("Local symbol unhandled when generating C code.")); | |
689 | ||
690 | case LOC_COMPUTED: | |
691 | gdb_assert_not_reached (_("LOC_COMPUTED variable " | |
692 | "missing a method.")); | |
693 | ||
694 | default: | |
695 | /* Nothing to do for all other cases, as they don't represent | |
696 | local variables. */ | |
697 | break; | |
698 | } | |
699 | } | |
700 | } | |
701 | ||
492d29ea | 702 | CATCH (e, RETURN_MASK_ERROR) |
7556d4a4 PA |
703 | { |
704 | if (compiler->symbol_err_map == NULL) | |
705 | compiler->symbol_err_map = htab_create_alloc (10, | |
706 | hash_symbol_error, | |
707 | eq_symbol_error, | |
708 | del_symbol_error, | |
709 | xcalloc, | |
710 | xfree); | |
711 | insert_symbol_error (compiler->symbol_err_map, sym, e.message); | |
712 | } | |
492d29ea | 713 | END_CATCH |
bb2ec1b3 TT |
714 | } |
715 | ||
716 | /* See compile-internal.h. */ | |
717 | ||
718 | unsigned char * | |
719 | generate_c_for_variable_locations (struct compile_c_instance *compiler, | |
720 | struct ui_file *stream, | |
721 | struct gdbarch *gdbarch, | |
722 | const struct block *block, | |
723 | CORE_ADDR pc) | |
724 | { | |
725 | struct cleanup *cleanup, *outer; | |
726 | htab_t symhash; | |
727 | const struct block *static_block = block_static_block (block); | |
728 | unsigned char *registers_used; | |
729 | ||
730 | /* If we're already in the static or global block, there is nothing | |
731 | to write. */ | |
732 | if (static_block == NULL || block == static_block) | |
733 | return NULL; | |
734 | ||
735 | registers_used = XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch)); | |
736 | outer = make_cleanup (xfree, registers_used); | |
737 | ||
738 | /* Ensure that a given name is only entered once. This reflects the | |
739 | reality of shadowing. */ | |
740 | symhash = htab_create_alloc (1, hash_symname, eq_symname, NULL, | |
741 | xcalloc, xfree); | |
742 | cleanup = make_cleanup_htab_delete (symhash); | |
743 | ||
744 | while (1) | |
745 | { | |
746 | struct symbol *sym; | |
747 | struct block_iterator iter; | |
748 | ||
749 | /* Iterate over symbols in this block, generating code to | |
750 | compute the location of each local variable. */ | |
751 | for (sym = block_iterator_first (block, &iter); | |
752 | sym != NULL; | |
753 | sym = block_iterator_next (&iter)) | |
754 | { | |
755 | if (!symbol_seen (symhash, sym)) | |
756 | generate_c_for_for_one_variable (compiler, stream, gdbarch, | |
757 | registers_used, pc, sym); | |
758 | } | |
759 | ||
760 | /* If we just finished the outermost block of a function, we're | |
761 | done. */ | |
762 | if (BLOCK_FUNCTION (block) != NULL) | |
763 | break; | |
764 | block = BLOCK_SUPERBLOCK (block); | |
765 | } | |
766 | ||
767 | do_cleanups (cleanup); | |
768 | discard_cleanups (outer); | |
769 | return registers_used; | |
770 | } |