gdb/compile/compile-loc2c.c - gdb

Data types defined

Functions defined

Macros defined

Source code

  1. /* Convert a DWARF location expression to C

  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 "dwarf2.h"
  16. #include "dwarf2expr.h"
  17. #include "dwarf2loc.h"
  18. #include "ui-file.h"
  19. #include "utils.h"
  20. #include "compile-internal.h"
  21. #include "compile.h"
  22. #include "block.h"
  23. #include "dwarf2-frame.h"
  24. #include "gdb_vecs.h"
  25. #include "value.h"



  26. /* Information about a given instruction.  */

  27. struct insn_info
  28. {
  29.   /* Stack depth at entry.  */

  30.   unsigned int depth;

  31.   /* Whether this instruction has been visited.  */

  32.   unsigned int visited : 1;

  33.   /* Whether this instruction needs a label.  */

  34.   unsigned int label : 1;

  35.   /* Whether this instruction is DW_OP_GNU_push_tls_address.  This is
  36.      a hack until we can add a feature to glibc to let us properly
  37.      generate code for TLS.  */

  38.   unsigned int is_tls : 1;
  39. };

  40. /* A helper function for compute_stack_depth that does the work.  This
  41.    examines the DWARF expression starting from START and computes
  42.    stack effects.

  43.    NEED_TEMPVAR is an out parameter which is set if this expression
  44.    needs a special temporary variable to be emitted (see the code
  45.    generator).
  46.    INFO is an array of insn_info objects, indexed by offset from the
  47.    start of the DWARF expression.
  48.    TO_DO is a list of bytecodes which must be examined; it may be
  49.    added to by this function.
  50.    BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
  51.    OP_PTR and OP_END are the bounds of the DWARF expression.  */

  52. static void
  53. compute_stack_depth_worker (int start, int *need_tempvar,
  54.                             struct insn_info *info,
  55.                             VEC (int) **to_do,
  56.                             enum bfd_endian byte_order, unsigned int addr_size,
  57.                             const gdb_byte *op_ptr, const gdb_byte *op_end)
  58. {
  59.   const gdb_byte * const base = op_ptr;
  60.   int stack_depth;

  61.   op_ptr += start;
  62.   gdb_assert (info[start].visited);
  63.   stack_depth = info[start].depth;

  64.   while (op_ptr < op_end)
  65.     {
  66.       enum dwarf_location_atom op = *op_ptr;
  67.       uint64_t reg;
  68.       int64_t offset;
  69.       int ndx = op_ptr - base;

  70. #define SET_CHECK_DEPTH(WHERE)                                \
  71.       if (info[WHERE].visited)                                \
  72.         {                                                \
  73.           if (info[WHERE].depth != stack_depth)                \
  74.             error (_("inconsistent stack depths"));        \
  75.         }                                                \
  76.       else                                                \
  77.         {                                                \
  78.           /* Stack depth not set, so set it.  */        \
  79.           info[WHERE].visited = 1;                        \
  80.           info[WHERE].depth = stack_depth;                \
  81.         }

  82.       SET_CHECK_DEPTH (ndx);

  83.       ++op_ptr;

  84.       switch (op)
  85.         {
  86.         case DW_OP_lit0:
  87.         case DW_OP_lit1:
  88.         case DW_OP_lit2:
  89.         case DW_OP_lit3:
  90.         case DW_OP_lit4:
  91.         case DW_OP_lit5:
  92.         case DW_OP_lit6:
  93.         case DW_OP_lit7:
  94.         case DW_OP_lit8:
  95.         case DW_OP_lit9:
  96.         case DW_OP_lit10:
  97.         case DW_OP_lit11:
  98.         case DW_OP_lit12:
  99.         case DW_OP_lit13:
  100.         case DW_OP_lit14:
  101.         case DW_OP_lit15:
  102.         case DW_OP_lit16:
  103.         case DW_OP_lit17:
  104.         case DW_OP_lit18:
  105.         case DW_OP_lit19:
  106.         case DW_OP_lit20:
  107.         case DW_OP_lit21:
  108.         case DW_OP_lit22:
  109.         case DW_OP_lit23:
  110.         case DW_OP_lit24:
  111.         case DW_OP_lit25:
  112.         case DW_OP_lit26:
  113.         case DW_OP_lit27:
  114.         case DW_OP_lit28:
  115.         case DW_OP_lit29:
  116.         case DW_OP_lit30:
  117.         case DW_OP_lit31:
  118.           ++stack_depth;
  119.           break;

  120.         case DW_OP_addr:
  121.           op_ptr += addr_size;
  122.           ++stack_depth;
  123.           break;

  124.         case DW_OP_const1u:
  125.         case DW_OP_const1s:
  126.           op_ptr += 1;
  127.           ++stack_depth;
  128.           break;
  129.         case DW_OP_const2u:
  130.         case DW_OP_const2s:
  131.           op_ptr += 2;
  132.           ++stack_depth;
  133.           break;
  134.         case DW_OP_const4u:
  135.         case DW_OP_const4s:
  136.           op_ptr += 4;
  137.           ++stack_depth;
  138.           break;
  139.         case DW_OP_const8u:
  140.         case DW_OP_const8s:
  141.           op_ptr += 8;
  142.           ++stack_depth;
  143.           break;
  144.         case DW_OP_constu:
  145.         case DW_OP_consts:
  146.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  147.           ++stack_depth;
  148.           break;

  149.         case DW_OP_reg0:
  150.         case DW_OP_reg1:
  151.         case DW_OP_reg2:
  152.         case DW_OP_reg3:
  153.         case DW_OP_reg4:
  154.         case DW_OP_reg5:
  155.         case DW_OP_reg6:
  156.         case DW_OP_reg7:
  157.         case DW_OP_reg8:
  158.         case DW_OP_reg9:
  159.         case DW_OP_reg10:
  160.         case DW_OP_reg11:
  161.         case DW_OP_reg12:
  162.         case DW_OP_reg13:
  163.         case DW_OP_reg14:
  164.         case DW_OP_reg15:
  165.         case DW_OP_reg16:
  166.         case DW_OP_reg17:
  167.         case DW_OP_reg18:
  168.         case DW_OP_reg19:
  169.         case DW_OP_reg20:
  170.         case DW_OP_reg21:
  171.         case DW_OP_reg22:
  172.         case DW_OP_reg23:
  173.         case DW_OP_reg24:
  174.         case DW_OP_reg25:
  175.         case DW_OP_reg26:
  176.         case DW_OP_reg27:
  177.         case DW_OP_reg28:
  178.         case DW_OP_reg29:
  179.         case DW_OP_reg30:
  180.         case DW_OP_reg31:
  181.           ++stack_depth;
  182.           break;

  183.         case DW_OP_regx:
  184.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  185.           ++stack_depth;
  186.           break;

  187.         case DW_OP_breg0:
  188.         case DW_OP_breg1:
  189.         case DW_OP_breg2:
  190.         case DW_OP_breg3:
  191.         case DW_OP_breg4:
  192.         case DW_OP_breg5:
  193.         case DW_OP_breg6:
  194.         case DW_OP_breg7:
  195.         case DW_OP_breg8:
  196.         case DW_OP_breg9:
  197.         case DW_OP_breg10:
  198.         case DW_OP_breg11:
  199.         case DW_OP_breg12:
  200.         case DW_OP_breg13:
  201.         case DW_OP_breg14:
  202.         case DW_OP_breg15:
  203.         case DW_OP_breg16:
  204.         case DW_OP_breg17:
  205.         case DW_OP_breg18:
  206.         case DW_OP_breg19:
  207.         case DW_OP_breg20:
  208.         case DW_OP_breg21:
  209.         case DW_OP_breg22:
  210.         case DW_OP_breg23:
  211.         case DW_OP_breg24:
  212.         case DW_OP_breg25:
  213.         case DW_OP_breg26:
  214.         case DW_OP_breg27:
  215.         case DW_OP_breg28:
  216.         case DW_OP_breg29:
  217.         case DW_OP_breg30:
  218.         case DW_OP_breg31:
  219.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  220.           ++stack_depth;
  221.           break;
  222.         case DW_OP_bregx:
  223.           {
  224.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  225.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  226.             ++stack_depth;
  227.           }
  228.           break;
  229.         case DW_OP_fbreg:
  230.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  231.           ++stack_depth;
  232.           break;

  233.         case DW_OP_dup:
  234.           ++stack_depth;
  235.           break;

  236.         case DW_OP_drop:
  237.           --stack_depth;
  238.           break;

  239.         case DW_OP_pick:
  240.           ++op_ptr;
  241.           ++stack_depth;
  242.           break;

  243.         case DW_OP_rot:
  244.         case DW_OP_swap:
  245.           *need_tempvar = 1;
  246.           break;

  247.         case DW_OP_over:
  248.           ++stack_depth;
  249.           break;

  250.         case DW_OP_abs:
  251.         case DW_OP_neg:
  252.         case DW_OP_not:
  253.         case DW_OP_deref:
  254.           break;

  255.         case DW_OP_deref_size:
  256.           ++op_ptr;
  257.           break;

  258.         case DW_OP_plus_uconst:
  259.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  260.           break;

  261.         case DW_OP_div:
  262.         case DW_OP_shra:
  263.         case DW_OP_and:
  264.         case DW_OP_minus:
  265.         case DW_OP_mod:
  266.         case DW_OP_mul:
  267.         case DW_OP_or:
  268.         case DW_OP_plus:
  269.         case DW_OP_shl:
  270.         case DW_OP_shr:
  271.         case DW_OP_xor:
  272.         case DW_OP_le:
  273.         case DW_OP_ge:
  274.         case DW_OP_eq:
  275.         case DW_OP_lt:
  276.         case DW_OP_gt:
  277.         case DW_OP_ne:
  278.           --stack_depth;
  279.           break;

  280.         case DW_OP_call_frame_cfa:
  281.           ++stack_depth;
  282.           break;

  283.         case DW_OP_GNU_push_tls_address:
  284.           info[ndx].is_tls = 1;
  285.           break;

  286.         case DW_OP_skip:
  287.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  288.           op_ptr += 2;
  289.           offset = op_ptr + offset - base;
  290.           /* If the destination has not been seen yet, add it to the
  291.              to-do list.  */
  292.           if (!info[offset].visited)
  293.             VEC_safe_push (int, *to_do, offset);
  294.           SET_CHECK_DEPTH (offset);
  295.           info[offset].label = 1;
  296.           /* We're done with this line of code.  */
  297.           return;

  298.         case DW_OP_bra:
  299.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  300.           op_ptr += 2;
  301.           offset = op_ptr + offset - base;
  302.           --stack_depth;
  303.           /* If the destination has not been seen yet, add it to the
  304.              to-do list.  */
  305.           if (!info[offset].visited)
  306.             VEC_safe_push (int, *to_do, offset);
  307.           SET_CHECK_DEPTH (offset);
  308.           info[offset].label = 1;
  309.           break;

  310.         case DW_OP_nop:
  311.           break;

  312.         default:
  313.           error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
  314.         }
  315.     }

  316.   gdb_assert (op_ptr == op_end);

  317. #undef SET_CHECK_DEPTH
  318. }

  319. /* Compute the maximum needed stack depth of a DWARF expression, and
  320.    some other information as well.

  321.    BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
  322.    NEED_TEMPVAR is an out parameter which is set if this expression
  323.    needs a special temporary variable to be emitted (see the code
  324.    generator).
  325.    IS_TLS is an out parameter which is set if this expression refers
  326.    to a TLS variable.
  327.    OP_PTR and OP_END are the bounds of the DWARF expression.
  328.    INITIAL_DEPTH is the initial depth of the DWARF expression stack.
  329.    INFO is an array of insn_info objects, indexed by offset from the
  330.    start of the DWARF expression.

  331.    This returns the maximum stack depth.  */

  332. static int
  333. compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
  334.                      int *need_tempvar, int *is_tls,
  335.                      const gdb_byte *op_ptr, const gdb_byte *op_end,
  336.                      int initial_depth,
  337.                      struct insn_info **info)
  338. {
  339.   unsigned char *set;
  340.   struct cleanup *outer_cleanup, *cleanup;
  341.   VEC (int) *to_do = NULL;
  342.   int stack_depth, i;

  343.   *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
  344.   outer_cleanup = make_cleanup (xfree, *info);

  345.   cleanup = make_cleanup (VEC_cleanup (int), &to_do);

  346.   VEC_safe_push (int, to_do, 0);
  347.   (*info)[0].depth = initial_depth;
  348.   (*info)[0].visited = 1;

  349.   while (!VEC_empty (int, to_do))
  350.     {
  351.       int ndx = VEC_pop (int, to_do);

  352.       compute_stack_depth_worker (ndx, need_tempvar, *info, &to_do,
  353.                                   byte_order, addr_size,
  354.                                   op_ptr, op_end);
  355.     }

  356.   stack_depth = 0;
  357.   *is_tls = 0;
  358.   for (i = 0; i < op_end - op_ptr; ++i)
  359.     {
  360.       if ((*info)[i].depth > stack_depth)
  361.         stack_depth = (*info)[i].depth;
  362.       if ((*info)[i].is_tls)
  363.         *is_tls = 1;
  364.     }

  365.   do_cleanups (cleanup);
  366.   discard_cleanups (outer_cleanup);
  367.   return stack_depth + 1;
  368. }



  369. #define GCC_UINTPTR "__gdb_uintptr"
  370. #define GCC_INTPTR "__gdb_intptr"

  371. /* Emit code to push a constant.  */

  372. static void
  373. push (int indent, struct ui_file *stream, ULONGEST l)
  374. {
  375.   fprintfi_filtered (indent, stream, "__gdb_stack[++__gdb_tos] = %s;\n",
  376.                      hex_string (l));
  377. }

  378. /* Emit code to push an arbitrary expression.  This works like
  379.    printf.  */

  380. static void
  381. pushf (int indent, struct ui_file *stream, const char *format, ...)
  382. {
  383.   va_list args;

  384.   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
  385.   va_start (args, format);
  386.   vfprintf_filtered (stream, format, args);
  387.   va_end (args);
  388.   fprintf_filtered (stream, ";\n");

  389.   fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
  390. }

  391. /* Emit code for a unary expression -- one which operates in-place on
  392.    the top-of-stack.  This works like printf.  */

  393. static void
  394. unary (int indent, struct ui_file *stream, const char *format, ...)
  395. {
  396.   va_list args;

  397.   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
  398.   va_start (args, format);
  399.   vfprintf_filtered (stream, format, args);
  400.   va_end (args);
  401.   fprintf_filtered (stream, ";\n");
  402. }

  403. /* Emit code for a unary expression -- one which uses the top two
  404.    stack items, popping the topmost one.  This works like printf.  */

  405. static void
  406. binary (int indent, struct ui_file *stream, const char *format, ...)
  407. {
  408.   va_list args;

  409.   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
  410.   va_start (args, format);
  411.   vfprintf_filtered (stream, format, args);
  412.   va_end (args);
  413.   fprintf_filtered (stream, ";\n");
  414.   fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
  415. }

  416. /* Print the name of a label given its "SCOPE", an arbitrary integer
  417.    used for uniqueness, and its TARGET, the bytecode offset
  418.    corresponding to the label's point of definition.  */

  419. static void
  420. print_label (struct ui_file *stream, unsigned int scope, int target)
  421. {
  422.   fprintf_filtered (stream, "__label_%u_%s",
  423.                     scope, pulongest (target));
  424. }

  425. /* Emit code that pushes a register's address on the stack.
  426.    REGISTERS_USED is an out parameter which is updated to note which
  427.    register was needed by this expression.  */

  428. static void
  429. pushf_register_address (int indent, struct ui_file *stream,
  430.                         unsigned char *registers_used,
  431.                         struct gdbarch *gdbarch, int regnum)
  432. {
  433.   char *regname = compile_register_name_mangled (gdbarch, regnum);
  434.   struct cleanup *cleanups = make_cleanup (xfree, regname);

  435.   registers_used[regnum] = 1;
  436.   pushf (indent, stream, "&" COMPILE_I_SIMPLE_REGISTER_ARG_NAME         "->%s",
  437.          regname);

  438.   do_cleanups (cleanups);
  439. }

  440. /* Emit code that pushes a register's value on the stack.
  441.    REGISTERS_USED is an out parameter which is updated to note which
  442.    register was needed by this expression.  OFFSET is added to the
  443.    register's value before it is pushed.  */

  444. static void
  445. pushf_register (int indent, struct ui_file *stream,
  446.                 unsigned char *registers_used,
  447.                 struct gdbarch *gdbarch, int regnum, uint64_t offset)
  448. {
  449.   char *regname = compile_register_name_mangled (gdbarch, regnum);
  450.   struct cleanup *cleanups = make_cleanup (xfree, regname);

  451.   registers_used[regnum] = 1;
  452.   if (offset == 0)
  453.     pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
  454.            regname);
  455.   else
  456.     pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + %s",
  457.            regname, hex_string (offset));

  458.   do_cleanups (cleanups);
  459. }

  460. /* Compile a DWARF expression to C code.

  461.    INDENT is the indentation level to use.
  462.    STREAM is the stream where the code should be written.

  463.    TYPE_NAME names the type of the result of the DWARF expression.
  464.    For locations this is "void *" but for array bounds it will be an
  465.    integer type.

  466.    RESULT_NAME is the name of a variable in the resulting C code.  The
  467.    result of the expression will be assigned to this variable.

  468.    SYM is the symbol corresponding to this expression.
  469.    PC is the location at which the expression is being evaluated.
  470.    ARCH is the architecture to use.

  471.    REGISTERS_USED is an out parameter which is updated to note which
  472.    registers were needed by this expression.

  473.    ADDR_SIZE is the DWARF address size to use.

  474.    OPT_PTR and OP_END are the bounds of the DWARF expression.

  475.    If non-NULL, INITIAL points to an initial value to write to the
  476.    stack.  If NULL, no initial value is written.

  477.    PER_CU is the per-CU object used for looking up various other
  478.    things.  */

  479. static void
  480. do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
  481.                             const char *type_name,
  482.                             const char *result_name,
  483.                             struct symbol *sym, CORE_ADDR pc,
  484.                             struct gdbarch *arch,
  485.                             unsigned char *registers_used,
  486.                             unsigned int addr_size,
  487.                             const gdb_byte *op_ptr, const gdb_byte *op_end,
  488.                             CORE_ADDR *initial,
  489.                             struct dwarf2_per_cu_data *per_cu)
  490. {
  491.   /* We keep a counter so that labels and other objects we create have
  492.      unique names.  */
  493.   static unsigned int scope;

  494.   enum bfd_endian byte_order = gdbarch_byte_order (arch);
  495.   const gdb_byte * const base = op_ptr;
  496.   int need_tempvar = 0;
  497.   int is_tls = 0;
  498.   struct cleanup *cleanup;
  499.   struct insn_info *info;
  500.   int stack_depth;

  501.   ++scope;

  502.   fprintfi_filtered (indent, stream, "%s%s;\n", type_name, result_name);
  503.   fprintfi_filtered (indent, stream, "{\n");
  504.   indent += 2;

  505.   stack_depth = compute_stack_depth (byte_order, addr_size,
  506.                                      &need_tempvar, &is_tls,
  507.                                      op_ptr, op_end, initial != NULL,
  508.                                      &info);
  509.   cleanup = make_cleanup (xfree, info);

  510.   /* This is a hack until we can add a feature to glibc to let us
  511.      properly generate code for TLS.  You might think we could emit
  512.      the address in the ordinary course of translating
  513.      DW_OP_GNU_push_tls_address, but since the operand appears on the
  514.      stack, it is relatively hard to find, and the idea of calling
  515.      target_translate_tls_address with OFFSET==0 and then adding the
  516.      offset by hand seemed too hackish.  */
  517.   if (is_tls)
  518.     {
  519.       struct frame_info *frame = get_selected_frame (NULL);
  520.       struct value *val;

  521.       if (frame == NULL)
  522.         error (_("Symbol \"%s\" cannot be used because "
  523.                  "there is no selected frame"),
  524.                SYMBOL_PRINT_NAME (sym));

  525.       val = read_var_value (sym, frame);
  526.       if (VALUE_LVAL (val) != lval_memory)
  527.         error (_("Symbol \"%s\" cannot be used for compilation evaluation "
  528.                  "as its address has not been found."),
  529.                SYMBOL_PRINT_NAME (sym));

  530.       warning (_("Symbol \"%s\" is thread-local and currently can only "
  531.                  "be referenced from the current thread in "
  532.                  "compiled code."),
  533.                SYMBOL_PRINT_NAME (sym));

  534.       fprintfi_filtered (indent, stream, "%s = %s;\n",
  535.                          result_name,
  536.                          core_addr_to_string (value_address (val)));
  537.       fprintfi_filtered (indent - 2, stream, "}\n");
  538.       do_cleanups (cleanup);
  539.       return;
  540.     }

  541.   fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
  542.                      stack_depth);

  543.   if (need_tempvar)
  544.     fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
  545.   fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");

  546.   if (initial != NULL)
  547.     pushf (indent, stream, core_addr_to_string (*initial));

  548.   while (op_ptr < op_end)
  549.     {
  550.       enum dwarf_location_atom op = *op_ptr;
  551.       uint64_t uoffset, reg;
  552.       int64_t offset;

  553.       print_spaces (indent - 2, stream);
  554.       if (info[op_ptr - base].label)
  555.         {
  556.           print_label (stream, scope, op_ptr - base);
  557.           fprintf_filtered (stream, ":;");
  558.         }
  559.       fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));

  560.       /* This is handy for debugging the generated code:
  561.       fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
  562.                         (int) info[op_ptr - base].depth - 1);
  563.       */

  564.       ++op_ptr;

  565.       switch (op)
  566.         {
  567.         case DW_OP_lit0:
  568.         case DW_OP_lit1:
  569.         case DW_OP_lit2:
  570.         case DW_OP_lit3:
  571.         case DW_OP_lit4:
  572.         case DW_OP_lit5:
  573.         case DW_OP_lit6:
  574.         case DW_OP_lit7:
  575.         case DW_OP_lit8:
  576.         case DW_OP_lit9:
  577.         case DW_OP_lit10:
  578.         case DW_OP_lit11:
  579.         case DW_OP_lit12:
  580.         case DW_OP_lit13:
  581.         case DW_OP_lit14:
  582.         case DW_OP_lit15:
  583.         case DW_OP_lit16:
  584.         case DW_OP_lit17:
  585.         case DW_OP_lit18:
  586.         case DW_OP_lit19:
  587.         case DW_OP_lit20:
  588.         case DW_OP_lit21:
  589.         case DW_OP_lit22:
  590.         case DW_OP_lit23:
  591.         case DW_OP_lit24:
  592.         case DW_OP_lit25:
  593.         case DW_OP_lit26:
  594.         case DW_OP_lit27:
  595.         case DW_OP_lit28:
  596.         case DW_OP_lit29:
  597.         case DW_OP_lit30:
  598.         case DW_OP_lit31:
  599.           push (indent, stream, op - DW_OP_lit0);
  600.           break;

  601.         case DW_OP_addr:
  602.           op_ptr += addr_size;
  603.           /* Some versions of GCC emit DW_OP_addr before
  604.              DW_OP_GNU_push_tls_address.  In this case the value is an
  605.              index, not an address.  We don't support things like
  606.              branching between the address and the TLS op.  */
  607.           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
  608.             uoffset += dwarf2_per_cu_text_offset (per_cu);
  609.           push (indent, stream, uoffset);
  610.           break;

  611.         case DW_OP_const1u:
  612.           push (indent, stream,
  613.                 extract_unsigned_integer (op_ptr, 1, byte_order));
  614.           op_ptr += 1;
  615.           break;
  616.         case DW_OP_const1s:
  617.           push (indent, stream,
  618.                 extract_signed_integer (op_ptr, 1, byte_order));
  619.           op_ptr += 1;
  620.           break;
  621.         case DW_OP_const2u:
  622.           push (indent, stream,
  623.                 extract_unsigned_integer (op_ptr, 2, byte_order));
  624.           op_ptr += 2;
  625.           break;
  626.         case DW_OP_const2s:
  627.           push (indent, stream,
  628.                 extract_signed_integer (op_ptr, 2, byte_order));
  629.           op_ptr += 2;
  630.           break;
  631.         case DW_OP_const4u:
  632.           push (indent, stream,
  633.                 extract_unsigned_integer (op_ptr, 4, byte_order));
  634.           op_ptr += 4;
  635.           break;
  636.         case DW_OP_const4s:
  637.           push (indent, stream,
  638.                 extract_signed_integer (op_ptr, 4, byte_order));
  639.           op_ptr += 4;
  640.           break;
  641.         case DW_OP_const8u:
  642.           push (indent, stream,
  643.                 extract_unsigned_integer (op_ptr, 8, byte_order));
  644.           op_ptr += 8;
  645.           break;
  646.         case DW_OP_const8s:
  647.           push (indent, stream,
  648.                 extract_signed_integer (op_ptr, 8, byte_order));
  649.           op_ptr += 8;
  650.           break;
  651.         case DW_OP_constu:
  652.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
  653.           push (indent, stream, uoffset);
  654.           break;
  655.         case DW_OP_consts:
  656.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  657.           push (indent, stream, offset);
  658.           break;

  659.         case DW_OP_reg0:
  660.         case DW_OP_reg1:
  661.         case DW_OP_reg2:
  662.         case DW_OP_reg3:
  663.         case DW_OP_reg4:
  664.         case DW_OP_reg5:
  665.         case DW_OP_reg6:
  666.         case DW_OP_reg7:
  667.         case DW_OP_reg8:
  668.         case DW_OP_reg9:
  669.         case DW_OP_reg10:
  670.         case DW_OP_reg11:
  671.         case DW_OP_reg12:
  672.         case DW_OP_reg13:
  673.         case DW_OP_reg14:
  674.         case DW_OP_reg15:
  675.         case DW_OP_reg16:
  676.         case DW_OP_reg17:
  677.         case DW_OP_reg18:
  678.         case DW_OP_reg19:
  679.         case DW_OP_reg20:
  680.         case DW_OP_reg21:
  681.         case DW_OP_reg22:
  682.         case DW_OP_reg23:
  683.         case DW_OP_reg24:
  684.         case DW_OP_reg25:
  685.         case DW_OP_reg26:
  686.         case DW_OP_reg27:
  687.         case DW_OP_reg28:
  688.         case DW_OP_reg29:
  689.         case DW_OP_reg30:
  690.         case DW_OP_reg31:
  691.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  692.           pushf_register_address (indent, stream, registers_used, arch,
  693.                                   dwarf2_reg_to_regnum_or_error (arch,
  694.                                                               op - DW_OP_reg0));
  695.           break;

  696.         case DW_OP_regx:
  697.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  698.           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
  699.           pushf_register_address (indent, stream, registers_used, arch,
  700.                                   dwarf2_reg_to_regnum_or_error (arch, reg));
  701.           break;

  702.         case DW_OP_breg0:
  703.         case DW_OP_breg1:
  704.         case DW_OP_breg2:
  705.         case DW_OP_breg3:
  706.         case DW_OP_breg4:
  707.         case DW_OP_breg5:
  708.         case DW_OP_breg6:
  709.         case DW_OP_breg7:
  710.         case DW_OP_breg8:
  711.         case DW_OP_breg9:
  712.         case DW_OP_breg10:
  713.         case DW_OP_breg11:
  714.         case DW_OP_breg12:
  715.         case DW_OP_breg13:
  716.         case DW_OP_breg14:
  717.         case DW_OP_breg15:
  718.         case DW_OP_breg16:
  719.         case DW_OP_breg17:
  720.         case DW_OP_breg18:
  721.         case DW_OP_breg19:
  722.         case DW_OP_breg20:
  723.         case DW_OP_breg21:
  724.         case DW_OP_breg22:
  725.         case DW_OP_breg23:
  726.         case DW_OP_breg24:
  727.         case DW_OP_breg25:
  728.         case DW_OP_breg26:
  729.         case DW_OP_breg27:
  730.         case DW_OP_breg28:
  731.         case DW_OP_breg29:
  732.         case DW_OP_breg30:
  733.         case DW_OP_breg31:
  734.           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  735.           pushf_register (indent, stream, registers_used, arch,
  736.                           dwarf2_reg_to_regnum_or_error (arch,
  737.                                                          op - DW_OP_breg0),
  738.                           offset);
  739.           break;
  740.         case DW_OP_bregx:
  741.           {
  742.             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  743.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
  744.             pushf_register (indent, stream, registers_used, arch,
  745.                             dwarf2_reg_to_regnum_or_error (arch, reg), offset);
  746.           }
  747.           break;
  748.         case DW_OP_fbreg:
  749.           {
  750.             const gdb_byte *datastart;
  751.             size_t datalen;
  752.             const struct block *b;
  753.             struct symbol *framefunc;
  754.             char fb_name[50];

  755.             b = block_for_pc (pc);

  756.             if (!b)
  757.               error (_("No block found for address"));

  758.             framefunc = block_linkage_function (b);

  759.             if (!framefunc)
  760.               error (_("No function found for block"));

  761.             func_get_frame_base_dwarf_block (framefunc, pc,
  762.                                              &datastart, &datalen);

  763.             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);

  764.             /* Generate a unique-enough name, in case the frame base
  765.                is computed multiple times in this expression.  */
  766.             xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
  767.                        (long) (op_ptr - base));

  768.             do_compile_dwarf_expr_to_c (indent, stream,
  769.                                         "void *", fb_name,
  770.                                         sym, pc,
  771.                                         arch, registers_used, addr_size,
  772.                                         datastart, datastart + datalen,
  773.                                         NULL, per_cu);

  774.             pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
  775.           }
  776.           break;

  777.         case DW_OP_dup:
  778.           pushf (indent, stream, "__gdb_stack[__gdb_tos]");
  779.           break;

  780.         case DW_OP_drop:
  781.           fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
  782.           break;

  783.         case DW_OP_pick:
  784.           offset = *op_ptr++;
  785.           pushf (indent, stream, "__gdb_stack[__gdb_tos - %d]", offset);
  786.           break;

  787.         case DW_OP_swap:
  788.           fprintfi_filtered (indent, stream,
  789.                              "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
  790.           fprintfi_filtered (indent, stream,
  791.                              "__gdb_stack[__gdb_tos - 1] = "
  792.                              "__gdb_stack[__gdb_tos];\n");
  793.           fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
  794.                                               "__gdb_tmp;\n"));
  795.           break;

  796.         case DW_OP_over:
  797.           pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
  798.           break;

  799.         case DW_OP_rot:
  800.           fprintfi_filtered (indent, stream, ("__gdb_tmp = "
  801.                                               "__gdb_stack[__gdb_tos];\n"));
  802.           fprintfi_filtered (indent, stream,
  803.                              "__gdb_stack[__gdb_tos] = "
  804.                              "__gdb_stack[__gdb_tos - 1];\n");
  805.           fprintfi_filtered (indent, stream,
  806.                              "__gdb_stack[__gdb_tos - 1] = "
  807.                              "__gdb_stack[__gdb_tos -2];\n");
  808.           fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
  809.                              "__gdb_tmp;\n");
  810.           break;

  811.         case DW_OP_deref:
  812.         case DW_OP_deref_size:
  813.           {
  814.             int size;
  815.             const char *mode;

  816.             if (op == DW_OP_deref_size)
  817.               size = *op_ptr++;
  818.             else
  819.               size = addr_size;

  820.             mode = c_get_mode_for_size (size);
  821.             if (mode == NULL)
  822.               error (_("Unsupported size %d in %s"),
  823.                      size, get_DW_OP_name (op));

  824.             /* Cast to a pointer of the desired type, then
  825.                dereference.  */
  826.             fprintfi_filtered (indent, stream,
  827.                                "__gdb_stack[__gdb_tos] = "
  828.                                "*((__gdb_int_%s *) "
  829.                                "__gdb_stack[__gdb_tos]);\n",
  830.                                mode);
  831.           }
  832.           break;

  833.         case DW_OP_abs:
  834.           unary (indent, stream,
  835.                  "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
  836.                  "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
  837.           break;

  838.         case DW_OP_neg:
  839.           unary (indent, stream, "-__gdb_stack[__gdb_tos]");
  840.           break;

  841.         case DW_OP_not:
  842.           unary (indent, stream, "~__gdb_stack[__gdb_tos]");
  843.           break;

  844.         case DW_OP_plus_uconst:
  845.           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
  846.           unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
  847.                  hex_string (reg));
  848.           break;

  849.         case DW_OP_div:
  850.           binary (indent, stream, ("((" GCC_INTPTR
  851.                                    ") __gdb_stack[__gdb_tos-1]) / (("
  852.                                    GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
  853.           break;

  854.         case DW_OP_shra:
  855.           binary (indent, stream,
  856.                   "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
  857.                   "__gdb_stack[__gdb_tos]");
  858.           break;

  859. #define BINARY(OP)                                                        \
  860.           binary (indent, stream, ("__gdb_stack[__gdb_tos-1] " #OP        \
  861.                                    " __gdb_stack[__gdb_tos]"));        \
  862.           break

  863.         case DW_OP_and:
  864.           BINARY (&);
  865.         case DW_OP_minus:
  866.           BINARY (-);
  867.         case DW_OP_mod:
  868.           BINARY (%);
  869.         case DW_OP_mul:
  870.           BINARY (*);
  871.         case DW_OP_or:
  872.           BINARY (|);
  873.         case DW_OP_plus:
  874.           BINARY (+);
  875.         case DW_OP_shl:
  876.           BINARY (<<);
  877.         case DW_OP_shr:
  878.           BINARY (>>);
  879.         case DW_OP_xor:
  880.           BINARY (^);
  881. #undef BINARY

  882. #define COMPARE(OP)                                                        \
  883.           binary (indent, stream,                                        \
  884.                   "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP        \
  885.                   " ((" GCC_INTPTR                                        \
  886.                   ") __gdb_stack[__gdb_tos]))");                        \
  887.           break

  888.         case DW_OP_le:
  889.           COMPARE (<=);
  890.         case DW_OP_ge:
  891.           COMPARE (>=);
  892.         case DW_OP_eq:
  893.           COMPARE (==);
  894.         case DW_OP_lt:
  895.           COMPARE (<);
  896.         case DW_OP_gt:
  897.           COMPARE (>);
  898.         case DW_OP_ne:
  899.           COMPARE (!=);
  900. #undef COMPARE

  901.         case DW_OP_call_frame_cfa:
  902.           {
  903.             int regnum;
  904.             CORE_ADDR text_offset;
  905.             LONGEST off;
  906.             const gdb_byte *cfa_start, *cfa_end;

  907.             if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
  908.                                        &regnum, &off,
  909.                                        &text_offset, &cfa_start, &cfa_end))
  910.               {
  911.                 /* Register.  */
  912.                 pushf_register (indent, stream, registers_used, arch, regnum,
  913.                                 off);
  914.               }
  915.             else
  916.               {
  917.                 /* Another expression.  */
  918.                 char cfa_name[50];

  919.                 /* Generate a unique-enough name, in case the CFA is
  920.                    computed multiple times in this expression.  */
  921.                 xsnprintf (cfa_name, sizeof (cfa_name),
  922.                            "__cfa_%ld", (long) (op_ptr - base));

  923.                 do_compile_dwarf_expr_to_c (indent, stream,
  924.                                             "void *", cfa_name,
  925.                                             sym, pc, arch, registers_used,
  926.                                             addr_size,
  927.                                             cfa_start, cfa_end,
  928.                                             &text_offset, per_cu);
  929.                 pushf (indent, stream, cfa_name);
  930.               }
  931.           }

  932.           break;

  933.         case DW_OP_skip:
  934.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  935.           op_ptr += 2;
  936.           fprintfi_filtered (indent, stream, "goto ");
  937.           print_label (stream, scope, op_ptr + offset - base);
  938.           fprintf_filtered (stream, ";\n");
  939.           break;

  940.         case DW_OP_bra:
  941.           offset = extract_signed_integer (op_ptr, 2, byte_order);
  942.           op_ptr += 2;
  943.           fprintfi_filtered (indent, stream,
  944.                              "if ((( " GCC_INTPTR
  945.                              ") __gdb_stack[__gdb_tos--]) != 0) goto ");
  946.           print_label (stream, scope, op_ptr + offset - base);
  947.           fprintf_filtered (stream, ";\n");
  948.           break;

  949.         case DW_OP_nop:
  950.           break;

  951.         default:
  952.           error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
  953.         }
  954.     }

  955.   fprintfi_filtered (indent, stream, "%s = (%s) __gdb_stack[__gdb_tos];\n",
  956.                      result_name, type_name);
  957.   fprintfi_filtered (indent - 2, stream, "}\n");

  958.   do_cleanups (cleanup);
  959. }

  960. /* See compile.h.  */

  961. void
  962. compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
  963.                          struct symbol *sym, CORE_ADDR pc,
  964.                          struct gdbarch *arch, unsigned char *registers_used,
  965.                          unsigned int addr_size,
  966.                          const gdb_byte *op_ptr, const gdb_byte *op_end,
  967.                          struct dwarf2_per_cu_data *per_cu)
  968. {
  969.   do_compile_dwarf_expr_to_c (2, stream, "void *", result_name, sym, pc,
  970.                               arch, registers_used, addr_size, op_ptr, op_end,
  971.                               NULL, per_cu);
  972. }

  973. /* See compile.h.  */

  974. void
  975. compile_dwarf_bounds_to_c (struct ui_file *stream,
  976.                            const char *result_name,
  977.                            const struct dynamic_prop *prop,
  978.                            struct symbol *sym, CORE_ADDR pc,
  979.                            struct gdbarch *arch, unsigned char *registers_used,
  980.                            unsigned int addr_size,
  981.                            const gdb_byte *op_ptr, const gdb_byte *op_end,
  982.                            struct dwarf2_per_cu_data *per_cu)
  983. {
  984.   do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
  985.                               sym, pc, arch, registers_used,
  986.                               addr_size, op_ptr, op_end, NULL, per_cu);
  987. }