gdb/compile/compile-c-symbols.c - gdb

Data types defined

Functions defined

Source code

  1. /* Convert symbols from GDB to GCC

  2.    Copyright (C) 2014-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */


  14. #include "defs.h"
  15. #include "compile-internal.h"
  16. #include "gdb_assert.h"
  17. #include "symtab.h"
  18. #include "parser-defs.h"
  19. #include "block.h"
  20. #include "objfiles.h"
  21. #include "compile.h"
  22. #include "value.h"
  23. #include "exceptions.h"
  24. #include "gdbtypes.h"
  25. #include "dwarf2loc.h"



  26. /* Object of this type are stored in the compiler's symbol_err_map.  */

  27. struct symbol_error
  28. {
  29.   /* The symbol.  */

  30.   const struct symbol *sym;

  31.   /* The error message to emit.  This is malloc'd and owned by the
  32.      hash table.  */

  33.   char *message;
  34. };

  35. /* Hash function for struct symbol_error.  */

  36. static hashval_t
  37. hash_symbol_error (const void *a)
  38. {
  39.   const struct symbol_error *se = a;

  40.   return htab_hash_pointer (se->sym);
  41. }

  42. /* Equality function for struct symbol_error.  */

  43. static int
  44. eq_symbol_error (const void *a, const void *b)
  45. {
  46.   const struct symbol_error *sea = a;
  47.   const struct symbol_error *seb = b;

  48.   return sea->sym == seb->sym;
  49. }

  50. /* Deletion function for struct symbol_error.  */

  51. static void
  52. del_symbol_error (void *a)
  53. {
  54.   struct symbol_error *se = a;

  55.   xfree (se->message);
  56.   xfree (se);
  57. }

  58. /* Associate SYMBOL with some error text.  */

  59. static void
  60. insert_symbol_error (htab_t hash, const struct symbol *sym, const char *text)
  61. {
  62.   struct symbol_error e;
  63.   void **slot;

  64.   e.sym = sym;
  65.   slot = htab_find_slot (hash, &e, INSERT);
  66.   if (*slot == NULL)
  67.     {
  68.       struct symbol_error *e = XNEW (struct symbol_error);

  69.       e->sym = sym;
  70.       e->message = xstrdup (text);
  71.       *slot = e;
  72.     }
  73. }

  74. /* Emit the error message corresponding to SYM, if one exists, and
  75.    arrange for it not to be emitted again.  */

  76. static void
  77. error_symbol_once (struct compile_c_instance *context,
  78.                    const struct symbol *sym)
  79. {
  80.   struct symbol_error search;
  81.   struct symbol_error *err;
  82.   char *message;

  83.   if (context->symbol_err_map == NULL)
  84.     return;

  85.   search.sym = sym;
  86.   err = htab_find (context->symbol_err_map, &search);
  87.   if (err == NULL || err->message == NULL)
  88.     return;

  89.   message = err->message;
  90.   err->message = NULL;
  91.   make_cleanup (xfree, message);
  92.   error (_("%s"), message);
  93. }



  94. /* Compute the name of the pointer representing a local symbol's
  95.    address.  */

  96. static char *
  97. symbol_substitution_name (struct symbol *sym)
  98. {
  99.   return concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL);
  100. }

  101. /* Convert a given symbol, SYM, to the compiler's representation.
  102.    CONTEXT is the compiler instance.  IS_GLOBAL is true if the
  103.    symbol came from the global scope.  IS_LOCAL is true if the symbol
  104.    came from a local scope.  (Note that the two are not strictly
  105.    inverses because the symbol might have come from the static
  106.    scope.)  */

  107. static void
  108. convert_one_symbol (struct compile_c_instance *context,
  109.                     struct symbol *sym,
  110.                     int is_global,
  111.                     int is_local)
  112. {
  113.   gcc_type sym_type;
  114.   const char *filename = symbol_symtab (sym)->filename;
  115.   unsigned short line = SYMBOL_LINE (sym);

  116.   error_symbol_once (context, sym);

  117.   if (SYMBOL_CLASS (sym) == LOC_LABEL)
  118.     sym_type = 0;
  119.   else
  120.     sym_type = convert_type (context, SYMBOL_TYPE (sym));

  121.   if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN)
  122.     {
  123.       /* Binding a tag, so we don't need to build a decl.  */
  124.       C_CTX (context)->c_ops->tagbind (C_CTX (context),
  125.                                        SYMBOL_NATURAL_NAME (sym),
  126.                                        sym_type, filename, line);
  127.     }
  128.   else
  129.     {
  130.       gcc_decl decl;
  131.       enum gcc_c_symbol_kind kind;
  132.       CORE_ADDR addr = 0;
  133.       char *symbol_name = NULL;

  134.       switch (SYMBOL_CLASS (sym))
  135.         {
  136.         case LOC_TYPEDEF:
  137.           kind = GCC_C_SYMBOL_TYPEDEF;
  138.           break;

  139.         case LOC_LABEL:
  140.           kind = GCC_C_SYMBOL_LABEL;
  141.           addr = SYMBOL_VALUE_ADDRESS (sym);
  142.           break;

  143.         case LOC_BLOCK:
  144.           kind = GCC_C_SYMBOL_FUNCTION;
  145.           addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  146.           break;

  147.         case LOC_CONST:
  148.           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_ENUM)
  149.             {
  150.               /* Already handled by convert_enum.  */
  151.               return;
  152.             }
  153.           C_CTX (context)->c_ops->build_constant (C_CTX (context), sym_type,
  154.                                                   SYMBOL_NATURAL_NAME (sym),
  155.                                                   SYMBOL_VALUE (sym),
  156.                                                   filename, line);
  157.           return;

  158.         case LOC_CONST_BYTES:
  159.           error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."),
  160.                  SYMBOL_PRINT_NAME (sym));

  161.         case LOC_UNDEF:
  162.           internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."),
  163.                           SYMBOL_PRINT_NAME (sym));

  164.         case LOC_COMMON_BLOCK:
  165.           error (_("Fortran common block is unsupported for compilation "
  166.                    "evaluaton of symbol \"%s\"."),
  167.                  SYMBOL_PRINT_NAME (sym));

  168.         case LOC_OPTIMIZED_OUT:
  169.           error (_("Symbol \"%s\" cannot be used for compilation evaluation "
  170.                    "as it is optimized out."),
  171.                  SYMBOL_PRINT_NAME (sym));

  172.         case LOC_COMPUTED:
  173.           if (is_local)
  174.             goto substitution;
  175.           /* Probably TLS here.  */
  176.           warning (_("Symbol \"%s\" is thread-local and currently can only "
  177.                      "be referenced from the current thread in "
  178.                      "compiled code."),
  179.                    SYMBOL_PRINT_NAME (sym));
  180.           /* FALLTHROUGH */
  181.         case LOC_UNRESOLVED:
  182.           /* 'symbol_name' cannot be used here as that one is used only for
  183.              local variables from compile_dwarf_expr_to_c.
  184.              Global variables can be accessed by GCC only by their address, not
  185.              by their name.  */
  186.           {
  187.             struct value *val;
  188.             struct frame_info *frame = NULL;

  189.             if (symbol_read_needs_frame (sym))
  190.               {
  191.                 frame = get_selected_frame (NULL);
  192.                 if (frame == NULL)
  193.                   error (_("Symbol \"%s\" cannot be used because "
  194.                            "there is no selected frame"),
  195.                          SYMBOL_PRINT_NAME (sym));
  196.               }

  197.             val = read_var_value (sym, frame);
  198.             if (VALUE_LVAL (val) != lval_memory)
  199.               error (_("Symbol \"%s\" cannot be used for compilation "
  200.                        "evaluation as its address has not been found."),
  201.                      SYMBOL_PRINT_NAME (sym));

  202.             kind = GCC_C_SYMBOL_VARIABLE;
  203.             addr = value_address (val);
  204.           }
  205.           break;


  206.         case LOC_REGISTER:
  207.         case LOC_ARG:
  208.         case LOC_REF_ARG:
  209.         case LOC_REGPARM_ADDR:
  210.         case LOC_LOCAL:
  211.         substitution:
  212.           kind = GCC_C_SYMBOL_VARIABLE;
  213.           symbol_name = symbol_substitution_name (sym);
  214.           break;

  215.         case LOC_STATIC:
  216.           kind = GCC_C_SYMBOL_VARIABLE;
  217.           addr = SYMBOL_VALUE_ADDRESS (sym);
  218.           break;

  219.         case LOC_FINAL_VALUE:
  220.         default:
  221.           gdb_assert_not_reached ("Unreachable case in convert_one_symbol.");

  222.         }

  223.       /* Don't emit local variable decls for a raw expression.  */
  224.       if (context->base.scope != COMPILE_I_RAW_SCOPE
  225.           || symbol_name == NULL)
  226.         {
  227.           decl = C_CTX (context)->c_ops->build_decl (C_CTX (context),
  228.                                                      SYMBOL_NATURAL_NAME (sym),
  229.                                                      kind,
  230.                                                      sym_type,
  231.                                                      symbol_name, addr,
  232.                                                      filename, line);

  233.           C_CTX (context)->c_ops->bind (C_CTX (context), decl, is_global);
  234.         }

  235.       xfree (symbol_name);
  236.     }
  237. }

  238. /* Convert a full symbol to its gcc form.  CONTEXT is the compiler to
  239.    use, IDENTIFIER is the name of the symbol, SYM is the symbol
  240.    itself, and DOMAIN is the domain which was searched.  */

  241. static void
  242. convert_symbol_sym (struct compile_c_instance *context, const char *identifier,
  243.                     struct symbol *sym, domain_enum domain)
  244. {
  245.   const struct block *static_block, *found_block;
  246.   int is_local_symbol;

  247.   found_block = block_found;

  248.   /* If we found a symbol and it is not in the  static or global
  249.      scope, then we should first convert any static or global scope
  250.      symbol of the same name.  This lets this unusual case work:

  251.      int x; // Global.
  252.      int func(void)
  253.      {
  254.      int x;
  255.      // At this spot, evaluate "extern int x; x"
  256.      }
  257.   */

  258.   static_block = block_static_block (found_block);
  259.   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
  260.   is_local_symbol = (found_block != static_block && static_block != NULL);
  261.   if (is_local_symbol)
  262.     {
  263.       struct symbol *global_sym;

  264.       global_sym = lookup_symbol (identifier, NULL, domain, NULL);
  265.       /* If the outer symbol is in the static block, we ignore it, as
  266.          it cannot be referenced.  */
  267.       if (global_sym != NULL
  268.           && block_found != block_static_block (block_found))
  269.         {
  270.           if (compile_debug)
  271.             fprintf_unfiltered (gdb_stdout,
  272.                                 "gcc_convert_symbol \"%s\": global symbol\n",
  273.                                 identifier);
  274.           convert_one_symbol (context, global_sym, 1, 0);
  275.         }
  276.     }

  277.   if (compile_debug)
  278.     fprintf_unfiltered (gdb_stdout,
  279.                         "gcc_convert_symbol \"%s\": local symbol\n",
  280.                         identifier);
  281.   convert_one_symbol (context, sym, 0, is_local_symbol);
  282. }

  283. /* Convert a minimal symbol to its gcc form.  CONTEXT is the compiler
  284.    to use and BMSYM is the minimal symbol to convert.  */

  285. static void
  286. convert_symbol_bmsym (struct compile_c_instance *context,
  287.                       struct bound_minimal_symbol bmsym)
  288. {
  289.   struct minimal_symbol *msym = bmsym.minsym;
  290.   struct objfile *objfile = bmsym.objfile;
  291.   struct type *type;
  292.   enum gcc_c_symbol_kind kind;
  293.   gcc_type sym_type;
  294.   gcc_decl decl;
  295.   CORE_ADDR addr;

  296.   /* Conversion copied from write_exp_msymbol.  */
  297.   switch (MSYMBOL_TYPE (msym))
  298.     {
  299.     case mst_text:
  300.     case mst_file_text:
  301.     case mst_solib_trampoline:
  302.       type = objfile_type (objfile)->nodebug_text_symbol;
  303.       kind = GCC_C_SYMBOL_FUNCTION;
  304.       break;

  305.     case mst_text_gnu_ifunc:
  306.       type = objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol;
  307.       kind = GCC_C_SYMBOL_FUNCTION;
  308.       break;

  309.     case mst_data:
  310.     case mst_file_data:
  311.     case mst_bss:
  312.     case mst_file_bss:
  313.       type = objfile_type (objfile)->nodebug_data_symbol;
  314.       kind = GCC_C_SYMBOL_VARIABLE;
  315.       break;

  316.     case mst_slot_got_plt:
  317.       type = objfile_type (objfile)->nodebug_got_plt_symbol;
  318.       kind = GCC_C_SYMBOL_FUNCTION;
  319.       break;

  320.     default:
  321.       type = objfile_type (objfile)->nodebug_unknown_symbol;
  322.       kind = GCC_C_SYMBOL_VARIABLE;
  323.       break;
  324.     }

  325.   sym_type = convert_type (context, type);
  326.   addr = MSYMBOL_VALUE_ADDRESS (objfile, msym);
  327.   decl = C_CTX (context)->c_ops->build_decl (C_CTX (context),
  328.                                              MSYMBOL_NATURAL_NAME (msym),
  329.                                              kind, sym_type, NULL, addr,
  330.                                              NULL, 0);
  331.   C_CTX (context)->c_ops->bind (C_CTX (context), decl, 1 /* is_global */);
  332. }

  333. /* See compile-internal.h.  */

  334. void
  335. gcc_convert_symbol (void *datum,
  336.                     struct gcc_c_context *gcc_context,
  337.                     enum gcc_c_oracle_request request,
  338.                     const char *identifier)
  339. {
  340.   struct compile_c_instance *context = datum;
  341.   domain_enum domain;
  342.   volatile struct gdb_exception e;
  343.   int found = 0;

  344.   switch (request)
  345.     {
  346.     case GCC_C_ORACLE_SYMBOL:
  347.       domain = VAR_DOMAIN;
  348.       break;
  349.     case GCC_C_ORACLE_TAG:
  350.       domain = STRUCT_DOMAIN;
  351.       break;
  352.     case GCC_C_ORACLE_LABEL:
  353.       domain = LABEL_DOMAIN;
  354.       break;
  355.     default:
  356.       gdb_assert_not_reached ("Unrecognized oracle request.");
  357.     }

  358.   /* We can't allow exceptions to escape out of this callback.  Safest
  359.      is to simply emit a gcc error.  */
  360.   TRY_CATCH (e, RETURN_MASK_ALL)
  361.     {
  362.       struct symbol *sym;

  363.       sym = lookup_symbol (identifier, context->base.block, domain, NULL);
  364.       if (sym != NULL)
  365.         {
  366.           convert_symbol_sym (context, identifier, sym, domain);
  367.           found = 1;
  368.         }
  369.       else if (domain == VAR_DOMAIN)
  370.         {
  371.           struct bound_minimal_symbol bmsym;

  372.           bmsym = lookup_minimal_symbol (identifier, NULL, NULL);
  373.           if (bmsym.minsym != NULL)
  374.             {
  375.               convert_symbol_bmsym (context, bmsym);
  376.               found = 1;
  377.             }
  378.         }
  379.     }

  380.   if (e.reason < 0)
  381.     C_CTX (context)->c_ops->error (C_CTX (context), e.message);

  382.   if (compile_debug && !found)
  383.     fprintf_unfiltered (gdb_stdout,
  384.                         "gcc_convert_symbol \"%s\": lookup_symbol failed\n",
  385.                         identifier);
  386.   return;
  387. }

  388. /* See compile-internal.h.  */

  389. gcc_address
  390. gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context,
  391.                     const char *identifier)
  392. {
  393.   struct compile_c_instance *context = datum;
  394.   volatile struct gdb_exception e;
  395.   gcc_address result = 0;
  396.   int found = 0;

  397.   /* We can't allow exceptions to escape out of this callback.  Safest
  398.      is to simply emit a gcc error.  */
  399.   TRY_CATCH (e, RETURN_MASK_ERROR)
  400.     {
  401.       struct symbol *sym;

  402.       /* We only need global functions here.  */
  403.       sym = lookup_symbol (identifier, NULL, VAR_DOMAIN, NULL);
  404.       if (sym != NULL && SYMBOL_CLASS (sym) == LOC_BLOCK)
  405.         {
  406.           if (compile_debug)
  407.             fprintf_unfiltered (gdb_stdout,
  408.                                 "gcc_symbol_address \"%s\": full symbol\n",
  409.                                 identifier);
  410.           result = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  411.           found = 1;
  412.         }
  413.       else
  414.         {
  415.           struct bound_minimal_symbol msym;

  416.           msym = lookup_bound_minimal_symbol (identifier);
  417.           if (msym.minsym != NULL)
  418.             {
  419.               if (compile_debug)
  420.                 fprintf_unfiltered (gdb_stdout,
  421.                                     "gcc_symbol_address \"%s\": minimal "
  422.                                     "symbol\n",
  423.                                     identifier);
  424.               result = BMSYMBOL_VALUE_ADDRESS (msym);
  425.               found = 1;
  426.             }
  427.         }
  428.     }

  429.   if (e.reason < 0)
  430.     C_CTX (context)->c_ops->error (C_CTX (context), e.message);

  431.   if (compile_debug && !found)
  432.     fprintf_unfiltered (gdb_stdout,
  433.                         "gcc_symbol_address \"%s\": failed\n",
  434.                         identifier);
  435.   return result;
  436. }



  437. /* A hash function for symbol names.  */

  438. static hashval_t
  439. hash_symname (const void *a)
  440. {
  441.   const struct symbol *sym = a;

  442.   return htab_hash_string (SYMBOL_NATURAL_NAME (sym));
  443. }

  444. /* A comparison function for hash tables that just looks at symbol
  445.    names.  */

  446. static int
  447. eq_symname (const void *a, const void *b)
  448. {
  449.   const struct symbol *syma = a;
  450.   const struct symbol *symb = b;

  451.   return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0;
  452. }

  453. /* If a symbol with the same name as SYM is already in HASHTAB, return
  454.    1.  Otherwise, add SYM to HASHTAB and return 0.  */

  455. static int
  456. symbol_seen (htab_t hashtab, struct symbol *sym)
  457. {
  458.   void **slot;

  459.   slot = htab_find_slot (hashtab, sym, INSERT);
  460.   if (*slot != NULL)
  461.     return 1;

  462.   *slot = sym;
  463.   return 0;
  464. }

  465. /* Generate C code to compute the length of a VLA.  */

  466. static void
  467. generate_vla_size (struct compile_c_instance *compiler,
  468.                    struct ui_file *stream,
  469.                    struct gdbarch *gdbarch,
  470.                    unsigned char *registers_used,
  471.                    CORE_ADDR pc,
  472.                    struct type *type,
  473.                    struct symbol *sym)
  474. {
  475.   type = check_typedef (type);

  476.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  477.     type = check_typedef (TYPE_TARGET_TYPE (type));

  478.   switch (TYPE_CODE (type))
  479.     {
  480.     case TYPE_CODE_RANGE:
  481.       {
  482.         if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR
  483.             || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST)
  484.           {
  485.             const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high;
  486.             char *name = c_get_range_decl_name (prop);
  487.             struct cleanup *cleanup = make_cleanup (xfree, name);

  488.             dwarf2_compile_property_to_c (stream, name,
  489.                                           gdbarch, registers_used,
  490.                                           prop, pc, sym);
  491.             do_cleanups (cleanup);
  492.           }
  493.       }
  494.       break;

  495.     case TYPE_CODE_ARRAY:
  496.       generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
  497.                          TYPE_INDEX_TYPE (type), sym);
  498.       generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
  499.                          TYPE_TARGET_TYPE (type), sym);
  500.       break;

  501.     case TYPE_CODE_UNION:
  502.     case TYPE_CODE_STRUCT:
  503.       {
  504.         int i;

  505.         for (i = 0; i < TYPE_NFIELDS (type); ++i)
  506.           if (!field_is_static (&TYPE_FIELD (type, i)))
  507.             generate_vla_size (compiler, stream, gdbarch, registers_used, pc,
  508.                                TYPE_FIELD_TYPE (type, i), sym);
  509.       }
  510.       break;
  511.     }
  512. }

  513. /* Generate C code to compute the address of SYM.  */

  514. static void
  515. generate_c_for_for_one_variable (struct compile_c_instance *compiler,
  516.                                  struct ui_file *stream,
  517.                                  struct gdbarch *gdbarch,
  518.                                  unsigned char *registers_used,
  519.                                  CORE_ADDR pc,
  520.                                  struct symbol *sym)
  521. {
  522.   volatile struct gdb_exception e;

  523.   TRY_CATCH (e, RETURN_MASK_ERROR)
  524.     {
  525.       if (is_dynamic_type (SYMBOL_TYPE (sym)))
  526.         {
  527.           struct ui_file *size_file = mem_fileopen ();
  528.           struct cleanup *cleanup = make_cleanup_ui_file_delete (size_file);

  529.           generate_vla_size (compiler, size_file, gdbarch, registers_used, pc,
  530.                              SYMBOL_TYPE (sym), sym);
  531.           ui_file_put (size_file, ui_file_write_for_put, stream);

  532.           do_cleanups (cleanup);
  533.         }

  534.       if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  535.         {
  536.           char *generated_name = symbol_substitution_name (sym);
  537.           struct cleanup *cleanup = make_cleanup (xfree, generated_name);
  538.           /* We need to emit to a temporary buffer in case an error
  539.              occurs in the middle.  */
  540.           struct ui_file *local_file = mem_fileopen ();

  541.           make_cleanup_ui_file_delete (local_file);
  542.           SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file,
  543.                                                           gdbarch,
  544.                                                           registers_used,
  545.                                                           pc, generated_name);
  546.           ui_file_put (local_file, ui_file_write_for_put, stream);

  547.           do_cleanups (cleanup);
  548.         }
  549.       else
  550.         {
  551.           switch (SYMBOL_CLASS (sym))
  552.             {
  553.             case LOC_REGISTER:
  554.             case LOC_ARG:
  555.             case LOC_REF_ARG:
  556.             case LOC_REGPARM_ADDR:
  557.             case LOC_LOCAL:
  558.               error (_("Local symbol unhandled when generating C code."));

  559.             case LOC_COMPUTED:
  560.               gdb_assert_not_reached (_("LOC_COMPUTED variable "
  561.                                         "missing a method."));

  562.             default:
  563.               /* Nothing to do for all other cases, as they don't represent
  564.                  local variables.  */
  565.               break;
  566.             }
  567.         }
  568.     }

  569.   if (e.reason >= 0)
  570.     return;

  571.   if (compiler->symbol_err_map == NULL)
  572.     compiler->symbol_err_map = htab_create_alloc (10,
  573.                                                   hash_symbol_error,
  574.                                                   eq_symbol_error,
  575.                                                   del_symbol_error,
  576.                                                   xcalloc,
  577.                                                   xfree);
  578.   insert_symbol_error (compiler->symbol_err_map, sym, e.message);
  579. }

  580. /* See compile-internal.h.  */

  581. unsigned char *
  582. generate_c_for_variable_locations (struct compile_c_instance *compiler,
  583.                                    struct ui_file *stream,
  584.                                    struct gdbarch *gdbarch,
  585.                                    const struct block *block,
  586.                                    CORE_ADDR pc)
  587. {
  588.   struct cleanup *cleanup, *outer;
  589.   htab_t symhash;
  590.   const struct block *static_block = block_static_block (block);
  591.   unsigned char *registers_used;

  592.   /* If we're already in the static or global block, there is nothing
  593.      to write.  */
  594.   if (static_block == NULL || block == static_block)
  595.     return NULL;

  596.   registers_used = XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch));
  597.   outer = make_cleanup (xfree, registers_used);

  598.   /* Ensure that a given name is only entered once.  This reflects the
  599.      reality of shadowing.  */
  600.   symhash = htab_create_alloc (1, hash_symname, eq_symname, NULL,
  601.                                xcalloc, xfree);
  602.   cleanup = make_cleanup_htab_delete (symhash);

  603.   while (1)
  604.     {
  605.       struct symbol *sym;
  606.       struct block_iterator iter;

  607.       /* Iterate over symbols in this block, generating code to
  608.          compute the location of each local variable.  */
  609.       for (sym = block_iterator_first (block, &iter);
  610.            sym != NULL;
  611.            sym = block_iterator_next (&iter))
  612.         {
  613.           if (!symbol_seen (symhash, sym))
  614.             generate_c_for_for_one_variable (compiler, stream, gdbarch,
  615.                                              registers_used, pc, sym);
  616.         }

  617.       /* If we just finished the outermost block of a function, we're
  618.          done.  */
  619.       if (BLOCK_FUNCTION (block) != NULL)
  620.         break;
  621.       block = BLOCK_SUPERBLOCK (block);
  622.     }

  623.   do_cleanups (cleanup);
  624.   discard_cleanups (outer);
  625.   return registers_used;
  626. }