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