GDB (xrefs)
compile-loc2c.c
Go to the documentation of this file.
1 /* Convert a DWARF location expression to C
2 
3  Copyright (C) 2014-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "dwarf2.h"
22 #include "dwarf2expr.h"
23 #include "dwarf2loc.h"
24 #include "ui-file.h"
25 #include "utils.h"
26 #include "compile-internal.h"
27 #include "compile.h"
28 #include "block.h"
29 #include "dwarf2-frame.h"
30 #include "gdb_vecs.h"
31 #include "value.h"
32 
33 
34 
35 /* Information about a given instruction. */
36 
37 struct insn_info
38 {
39  /* Stack depth at entry. */
40 
41  unsigned int depth;
42 
43  /* Whether this instruction has been visited. */
44 
45  unsigned int visited : 1;
46 
47  /* Whether this instruction needs a label. */
48 
49  unsigned int label : 1;
50 
51  /* Whether this instruction is DW_OP_GNU_push_tls_address or
52  DW_OP_form_tls_address. This is a hack until we can add a
53  feature to glibc to let us properly generate code for TLS. */
54 
55  unsigned int is_tls : 1;
56 };
57 
58 /* A helper function for compute_stack_depth that does the work. This
59  examines the DWARF expression starting from START and computes
60  stack effects.
61 
62  NEED_TEMPVAR is an out parameter which is set if this expression
63  needs a special temporary variable to be emitted (see the code
64  generator).
65  INFO is a vector of insn_info objects, indexed by offset from the
66  start of the DWARF expression.
67  TO_DO is a list of bytecodes which must be examined; it may be
68  added to by this function.
69  BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
70  OP_PTR and OP_END are the bounds of the DWARF expression. */
71 
72 static void
73 compute_stack_depth_worker (int start, int *need_tempvar,
74  std::vector<struct insn_info> *info,
75  std::vector<int> *to_do,
76  enum bfd_endian byte_order, unsigned int addr_size,
77  const gdb_byte *op_ptr, const gdb_byte *op_end)
78 {
79  const gdb_byte * const base = op_ptr;
80  int stack_depth;
81 
82  op_ptr += start;
83  gdb_assert ((*info)[start].visited);
84  stack_depth = (*info)[start].depth;
85 
86  while (op_ptr < op_end)
87  {
88  enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
89  uint64_t reg;
90  int64_t offset;
91  int ndx = op_ptr - base;
92 
93 #define SET_CHECK_DEPTH(WHERE) \
94  if ((*info)[WHERE].visited) \
95  { \
96  if ((*info)[WHERE].depth != stack_depth) \
97  error (_("inconsistent stack depths")); \
98  } \
99  else \
100  { \
101  /* Stack depth not set, so set it. */ \
102  (*info)[WHERE].visited = 1; \
103  (*info)[WHERE].depth = stack_depth; \
104  }
105 
106  SET_CHECK_DEPTH (ndx);
107 
108  ++op_ptr;
109 
110  switch (op)
111  {
112  case DW_OP_lit0:
113  case DW_OP_lit1:
114  case DW_OP_lit2:
115  case DW_OP_lit3:
116  case DW_OP_lit4:
117  case DW_OP_lit5:
118  case DW_OP_lit6:
119  case DW_OP_lit7:
120  case DW_OP_lit8:
121  case DW_OP_lit9:
122  case DW_OP_lit10:
123  case DW_OP_lit11:
124  case DW_OP_lit12:
125  case DW_OP_lit13:
126  case DW_OP_lit14:
127  case DW_OP_lit15:
128  case DW_OP_lit16:
129  case DW_OP_lit17:
130  case DW_OP_lit18:
131  case DW_OP_lit19:
132  case DW_OP_lit20:
133  case DW_OP_lit21:
134  case DW_OP_lit22:
135  case DW_OP_lit23:
136  case DW_OP_lit24:
137  case DW_OP_lit25:
138  case DW_OP_lit26:
139  case DW_OP_lit27:
140  case DW_OP_lit28:
141  case DW_OP_lit29:
142  case DW_OP_lit30:
143  case DW_OP_lit31:
144  ++stack_depth;
145  break;
146 
147  case DW_OP_addr:
148  op_ptr += addr_size;
149  ++stack_depth;
150  break;
151 
152  case DW_OP_const1u:
153  case DW_OP_const1s:
154  op_ptr += 1;
155  ++stack_depth;
156  break;
157  case DW_OP_const2u:
158  case DW_OP_const2s:
159  op_ptr += 2;
160  ++stack_depth;
161  break;
162  case DW_OP_const4u:
163  case DW_OP_const4s:
164  op_ptr += 4;
165  ++stack_depth;
166  break;
167  case DW_OP_const8u:
168  case DW_OP_const8s:
169  op_ptr += 8;
170  ++stack_depth;
171  break;
172  case DW_OP_constu:
173  case DW_OP_consts:
174  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
175  ++stack_depth;
176  break;
177 
178  case DW_OP_reg0:
179  case DW_OP_reg1:
180  case DW_OP_reg2:
181  case DW_OP_reg3:
182  case DW_OP_reg4:
183  case DW_OP_reg5:
184  case DW_OP_reg6:
185  case DW_OP_reg7:
186  case DW_OP_reg8:
187  case DW_OP_reg9:
188  case DW_OP_reg10:
189  case DW_OP_reg11:
190  case DW_OP_reg12:
191  case DW_OP_reg13:
192  case DW_OP_reg14:
193  case DW_OP_reg15:
194  case DW_OP_reg16:
195  case DW_OP_reg17:
196  case DW_OP_reg18:
197  case DW_OP_reg19:
198  case DW_OP_reg20:
199  case DW_OP_reg21:
200  case DW_OP_reg22:
201  case DW_OP_reg23:
202  case DW_OP_reg24:
203  case DW_OP_reg25:
204  case DW_OP_reg26:
205  case DW_OP_reg27:
206  case DW_OP_reg28:
207  case DW_OP_reg29:
208  case DW_OP_reg30:
209  case DW_OP_reg31:
210  ++stack_depth;
211  break;
212 
213  case DW_OP_regx:
214  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
215  ++stack_depth;
216  break;
217 
218  case DW_OP_breg0:
219  case DW_OP_breg1:
220  case DW_OP_breg2:
221  case DW_OP_breg3:
222  case DW_OP_breg4:
223  case DW_OP_breg5:
224  case DW_OP_breg6:
225  case DW_OP_breg7:
226  case DW_OP_breg8:
227  case DW_OP_breg9:
228  case DW_OP_breg10:
229  case DW_OP_breg11:
230  case DW_OP_breg12:
231  case DW_OP_breg13:
232  case DW_OP_breg14:
233  case DW_OP_breg15:
234  case DW_OP_breg16:
235  case DW_OP_breg17:
236  case DW_OP_breg18:
237  case DW_OP_breg19:
238  case DW_OP_breg20:
239  case DW_OP_breg21:
240  case DW_OP_breg22:
241  case DW_OP_breg23:
242  case DW_OP_breg24:
243  case DW_OP_breg25:
244  case DW_OP_breg26:
245  case DW_OP_breg27:
246  case DW_OP_breg28:
247  case DW_OP_breg29:
248  case DW_OP_breg30:
249  case DW_OP_breg31:
250  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
251  ++stack_depth;
252  break;
253  case DW_OP_bregx:
254  {
255  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
256  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
257  ++stack_depth;
258  }
259  break;
260  case DW_OP_fbreg:
261  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
262  ++stack_depth;
263  break;
264 
265  case DW_OP_dup:
266  ++stack_depth;
267  break;
268 
269  case DW_OP_drop:
270  --stack_depth;
271  break;
272 
273  case DW_OP_pick:
274  ++op_ptr;
275  ++stack_depth;
276  break;
277 
278  case DW_OP_rot:
279  case DW_OP_swap:
280  *need_tempvar = 1;
281  break;
282 
283  case DW_OP_over:
284  ++stack_depth;
285  break;
286 
287  case DW_OP_abs:
288  case DW_OP_neg:
289  case DW_OP_not:
290  case DW_OP_deref:
291  break;
292 
293  case DW_OP_deref_size:
294  ++op_ptr;
295  break;
296 
297  case DW_OP_plus_uconst:
298  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
299  break;
300 
301  case DW_OP_div:
302  case DW_OP_shra:
303  case DW_OP_and:
304  case DW_OP_minus:
305  case DW_OP_mod:
306  case DW_OP_mul:
307  case DW_OP_or:
308  case DW_OP_plus:
309  case DW_OP_shl:
310  case DW_OP_shr:
311  case DW_OP_xor:
312  case DW_OP_le:
313  case DW_OP_ge:
314  case DW_OP_eq:
315  case DW_OP_lt:
316  case DW_OP_gt:
317  case DW_OP_ne:
318  --stack_depth;
319  break;
320 
321  case DW_OP_call_frame_cfa:
322  ++stack_depth;
323  break;
324 
325  case DW_OP_GNU_push_tls_address:
326  case DW_OP_form_tls_address:
327  (*info)[ndx].is_tls = 1;
328  break;
329 
330  case DW_OP_skip:
331  offset = extract_signed_integer (op_ptr, 2, byte_order);
332  op_ptr += 2;
333  offset = op_ptr + offset - base;
334  /* If the destination has not been seen yet, add it to the
335  to-do list. */
336  if (!(*info)[offset].visited)
337  to_do->push_back (offset);
339  (*info)[offset].label = 1;
340  /* We're done with this line of code. */
341  return;
342 
343  case DW_OP_bra:
344  offset = extract_signed_integer (op_ptr, 2, byte_order);
345  op_ptr += 2;
346  offset = op_ptr + offset - base;
347  --stack_depth;
348  /* If the destination has not been seen yet, add it to the
349  to-do list. */
350  if (!(*info)[offset].visited)
351  to_do->push_back (offset);
353  (*info)[offset].label = 1;
354  break;
355 
356  case DW_OP_nop:
357  break;
358 
359  default:
360  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
361  }
362  }
363 
364  gdb_assert (op_ptr == op_end);
365 
366 #undef SET_CHECK_DEPTH
367 }
368 
369 /* Compute the maximum needed stack depth of a DWARF expression, and
370  some other information as well.
371 
372  BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
373  NEED_TEMPVAR is an out parameter which is set if this expression
374  needs a special temporary variable to be emitted (see the code
375  generator).
376  IS_TLS is an out parameter which is set if this expression refers
377  to a TLS variable.
378  OP_PTR and OP_END are the bounds of the DWARF expression.
379  INITIAL_DEPTH is the initial depth of the DWARF expression stack.
380  INFO is an array of insn_info objects, indexed by offset from the
381  start of the DWARF expression.
382 
383  This returns the maximum stack depth. */
384 
385 static int
386 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
387  int *need_tempvar, int *is_tls,
388  const gdb_byte *op_ptr, const gdb_byte *op_end,
389  int initial_depth,
390  std::vector<struct insn_info> *info)
391 {
392  std::vector<int> to_do;
393  int stack_depth, i;
394 
395  info->resize (op_end - op_ptr);
396 
397  to_do.push_back (0);
398  (*info)[0].depth = initial_depth;
399  (*info)[0].visited = 1;
400 
401  while (!to_do.empty ())
402  {
403  int ndx = to_do.back ();
404  to_do.pop_back ();
405 
406  compute_stack_depth_worker (ndx, need_tempvar, info, &to_do,
407  byte_order, addr_size,
408  op_ptr, op_end);
409  }
410 
411  stack_depth = 0;
412  *is_tls = 0;
413  for (i = 0; i < op_end - op_ptr; ++i)
414  {
415  if ((*info)[i].depth > stack_depth)
416  stack_depth = (*info)[i].depth;
417  if ((*info)[i].is_tls)
418  *is_tls = 1;
419  }
420 
421  return stack_depth + 1;
422 }
423 
424 
425 
426 #define GCC_UINTPTR "__gdb_uintptr"
427 #define GCC_INTPTR "__gdb_intptr"
428 
429 /* Emit code to push a constant. */
430 
431 static void
432 push (int indent, string_file &stream, ULONGEST l)
433 {
434  fprintfi_filtered (indent, &stream,
435  "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
436  hex_string (l));
437 }
438 
439 /* Emit code to push an arbitrary expression. This works like
440  printf. */
441 
442 static void pushf (int indent, string_file &stream, const char *format, ...)
443  ATTRIBUTE_PRINTF (3, 4);
444 
445 static void
446 pushf (int indent, string_file &stream, const char *format, ...)
447 {
448  va_list args;
449 
450  fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos + 1] = ");
451  va_start (args, format);
452  stream.vprintf (format, args);
453  va_end (args);
454  stream.puts (";\n");
455 
456  fprintfi_filtered (indent, &stream, "++__gdb_tos;\n");
457 }
458 
459 /* Emit code for a unary expression -- one which operates in-place on
460  the top-of-stack. This works like printf. */
461 
462 static void unary (int indent, string_file &stream, const char *format, ...)
463  ATTRIBUTE_PRINTF (3, 4);
464 
465 static void
466 unary (int indent, string_file &stream, const char *format, ...)
467 {
468  va_list args;
469 
470  fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos] = ");
471  va_start (args, format);
472  stream.vprintf (format, args);
473  va_end (args);
474  stream.puts (";\n");
475 }
476 
477 /* Emit code for a unary expression -- one which uses the top two
478  stack items, popping the topmost one. This works like printf. */
479 static void binary (int indent, string_file &stream, const char *format, ...)
480  ATTRIBUTE_PRINTF (3, 4);
481 
482 static void
483 binary (int indent, string_file &stream, const char *format, ...)
484 {
485  va_list args;
486 
487  fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 1] = ");
488  va_start (args, format);
489  stream.vprintf (format, args);
490  va_end (args);
491  stream.puts (";\n");
492  fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
493 }
494 
495 /* Print the name of a label given its "SCOPE", an arbitrary integer
496  used for uniqueness, and its TARGET, the bytecode offset
497  corresponding to the label's point of definition. */
498 
499 static void
500 print_label (string_file &stream, unsigned int scope, int target)
501 {
502  stream.printf ("__label_%u_%s", scope, pulongest (target));
503 }
504 
505 /* Emit code that pushes a register's address on the stack.
506  REGISTERS_USED is an out parameter which is updated to note which
507  register was needed by this expression. */
508 
509 static void
510 pushf_register_address (int indent, string_file &stream,
511  unsigned char *registers_used,
512  struct gdbarch *gdbarch, int regnum)
513 {
515 
516  registers_used[regnum] = 1;
517  pushf (indent, stream,
519  regname.c_str ());
520 }
521 
522 /* Emit code that pushes a register's value on the stack.
523  REGISTERS_USED is an out parameter which is updated to note which
524  register was needed by this expression. OFFSET is added to the
525  register's value before it is pushed. */
526 
527 static void
528 pushf_register (int indent, string_file &stream,
529  unsigned char *registers_used,
530  struct gdbarch *gdbarch, int regnum, uint64_t offset)
531 {
533 
534  registers_used[regnum] = 1;
535  if (offset == 0)
536  pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
537  regname.c_str ());
538  else
539  pushf (indent, stream,
541  regname.c_str (), hex_string (offset));
542 }
543 
544 /* Compile a DWARF expression to C code.
545 
546  INDENT is the indentation level to use.
547  STREAM is the stream where the code should be written.
548 
549  TYPE_NAME names the type of the result of the DWARF expression.
550  For locations this is "void *" but for array bounds it will be an
551  integer type.
552 
553  RESULT_NAME is the name of a variable in the resulting C code. The
554  result of the expression will be assigned to this variable.
555 
556  SYM is the symbol corresponding to this expression.
557  PC is the location at which the expression is being evaluated.
558  ARCH is the architecture to use.
559 
560  REGISTERS_USED is an out parameter which is updated to note which
561  registers were needed by this expression.
562 
563  ADDR_SIZE is the DWARF address size to use.
564 
565  OPT_PTR and OP_END are the bounds of the DWARF expression.
566 
567  If non-NULL, INITIAL points to an initial value to write to the
568  stack. If NULL, no initial value is written.
569 
570  PER_CU is the per-CU object used for looking up various other
571  things. */
572 
573 static void
575  const char *type_name,
576  const char *result_name,
577  struct symbol *sym, CORE_ADDR pc,
578  struct gdbarch *arch,
579  unsigned char *registers_used,
580  unsigned int addr_size,
581  const gdb_byte *op_ptr, const gdb_byte *op_end,
582  CORE_ADDR *initial,
583  struct dwarf2_per_cu_data *per_cu)
584 {
585  /* We keep a counter so that labels and other objects we create have
586  unique names. */
587  static unsigned int scope;
588 
589  enum bfd_endian byte_order = gdbarch_byte_order (arch);
590  const gdb_byte * const base = op_ptr;
591  int need_tempvar = 0;
592  int is_tls = 0;
593  std::vector<struct insn_info> info;
594  int stack_depth;
595 
596  ++scope;
597 
598  fprintfi_filtered (indent, &stream, "__attribute__ ((unused)) %s %s;\n",
599  type_name, result_name);
600  fprintfi_filtered (indent, &stream, "{\n");
601  indent += 2;
602 
603  stack_depth = compute_stack_depth (byte_order, addr_size,
604  &need_tempvar, &is_tls,
605  op_ptr, op_end, initial != NULL,
606  &info);
607 
608  /* This is a hack until we can add a feature to glibc to let us
609  properly generate code for TLS. You might think we could emit
610  the address in the ordinary course of translating
611  DW_OP_GNU_push_tls_address, but since the operand appears on the
612  stack, it is relatively hard to find, and the idea of calling
613  target_translate_tls_address with OFFSET==0 and then adding the
614  offset by hand seemed too hackish. */
615  if (is_tls)
616  {
617  struct frame_info *frame = get_selected_frame (NULL);
618  struct value *val;
619 
620  if (frame == NULL)
621  error (_("Symbol \"%s\" cannot be used because "
622  "there is no selected frame"),
623  SYMBOL_PRINT_NAME (sym));
624 
625  val = read_var_value (sym, NULL, frame);
626  if (VALUE_LVAL (val) != lval_memory)
627  error (_("Symbol \"%s\" cannot be used for compilation evaluation "
628  "as its address has not been found."),
629  SYMBOL_PRINT_NAME (sym));
630 
631  warning (_("Symbol \"%s\" is thread-local and currently can only "
632  "be referenced from the current thread in "
633  "compiled code."),
634  SYMBOL_PRINT_NAME (sym));
635 
636  fprintfi_filtered (indent, &stream, "%s = %s;\n",
637  result_name,
639  fprintfi_filtered (indent - 2, &stream, "}\n");
640  return;
641  }
642 
643  fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_stack[%d];\n",
644  stack_depth);
645 
646  if (need_tempvar)
647  fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_tmp;\n");
648  fprintfi_filtered (indent, &stream, "int __gdb_tos = -1;\n");
649 
650  if (initial != NULL)
651  pushf (indent, stream, "%s", core_addr_to_string (*initial));
652 
653  while (op_ptr < op_end)
654  {
655  enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
656  uint64_t uoffset, reg;
657  int64_t offset;
658 
659  print_spaces (indent - 2, &stream);
660  if (info[op_ptr - base].label)
661  {
662  print_label (stream, scope, op_ptr - base);
663  stream.puts (":;");
664  }
665  stream.printf ("/* %s */\n", get_DW_OP_name (op));
666 
667  /* This is handy for debugging the generated code:
668  fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
669  (int) info[op_ptr - base].depth - 1);
670  */
671 
672  ++op_ptr;
673 
674  switch (op)
675  {
676  case DW_OP_lit0:
677  case DW_OP_lit1:
678  case DW_OP_lit2:
679  case DW_OP_lit3:
680  case DW_OP_lit4:
681  case DW_OP_lit5:
682  case DW_OP_lit6:
683  case DW_OP_lit7:
684  case DW_OP_lit8:
685  case DW_OP_lit9:
686  case DW_OP_lit10:
687  case DW_OP_lit11:
688  case DW_OP_lit12:
689  case DW_OP_lit13:
690  case DW_OP_lit14:
691  case DW_OP_lit15:
692  case DW_OP_lit16:
693  case DW_OP_lit17:
694  case DW_OP_lit18:
695  case DW_OP_lit19:
696  case DW_OP_lit20:
697  case DW_OP_lit21:
698  case DW_OP_lit22:
699  case DW_OP_lit23:
700  case DW_OP_lit24:
701  case DW_OP_lit25:
702  case DW_OP_lit26:
703  case DW_OP_lit27:
704  case DW_OP_lit28:
705  case DW_OP_lit29:
706  case DW_OP_lit30:
707  case DW_OP_lit31:
708  push (indent, stream, op - DW_OP_lit0);
709  break;
710 
711  case DW_OP_addr:
712  uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
713  op_ptr += addr_size;
714  /* Some versions of GCC emit DW_OP_addr before
715  DW_OP_GNU_push_tls_address. In this case the value is an
716  index, not an address. We don't support things like
717  branching between the address and the TLS op. */
718  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
719  uoffset += dwarf2_per_cu_text_offset (per_cu);
720  push (indent, stream, uoffset);
721  break;
722 
723  case DW_OP_const1u:
724  push (indent, stream,
725  extract_unsigned_integer (op_ptr, 1, byte_order));
726  op_ptr += 1;
727  break;
728  case DW_OP_const1s:
729  push (indent, stream,
730  extract_signed_integer (op_ptr, 1, byte_order));
731  op_ptr += 1;
732  break;
733  case DW_OP_const2u:
734  push (indent, stream,
735  extract_unsigned_integer (op_ptr, 2, byte_order));
736  op_ptr += 2;
737  break;
738  case DW_OP_const2s:
739  push (indent, stream,
740  extract_signed_integer (op_ptr, 2, byte_order));
741  op_ptr += 2;
742  break;
743  case DW_OP_const4u:
744  push (indent, stream,
745  extract_unsigned_integer (op_ptr, 4, byte_order));
746  op_ptr += 4;
747  break;
748  case DW_OP_const4s:
749  push (indent, stream,
750  extract_signed_integer (op_ptr, 4, byte_order));
751  op_ptr += 4;
752  break;
753  case DW_OP_const8u:
754  push (indent, stream,
755  extract_unsigned_integer (op_ptr, 8, byte_order));
756  op_ptr += 8;
757  break;
758  case DW_OP_const8s:
759  push (indent, stream,
760  extract_signed_integer (op_ptr, 8, byte_order));
761  op_ptr += 8;
762  break;
763  case DW_OP_constu:
764  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
765  push (indent, stream, uoffset);
766  break;
767  case DW_OP_consts:
768  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
769  push (indent, stream, offset);
770  break;
771 
772  case DW_OP_reg0:
773  case DW_OP_reg1:
774  case DW_OP_reg2:
775  case DW_OP_reg3:
776  case DW_OP_reg4:
777  case DW_OP_reg5:
778  case DW_OP_reg6:
779  case DW_OP_reg7:
780  case DW_OP_reg8:
781  case DW_OP_reg9:
782  case DW_OP_reg10:
783  case DW_OP_reg11:
784  case DW_OP_reg12:
785  case DW_OP_reg13:
786  case DW_OP_reg14:
787  case DW_OP_reg15:
788  case DW_OP_reg16:
789  case DW_OP_reg17:
790  case DW_OP_reg18:
791  case DW_OP_reg19:
792  case DW_OP_reg20:
793  case DW_OP_reg21:
794  case DW_OP_reg22:
795  case DW_OP_reg23:
796  case DW_OP_reg24:
797  case DW_OP_reg25:
798  case DW_OP_reg26:
799  case DW_OP_reg27:
800  case DW_OP_reg28:
801  case DW_OP_reg29:
802  case DW_OP_reg30:
803  case DW_OP_reg31:
804  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
805  pushf_register_address (indent, stream, registers_used, arch,
807  (arch, op - DW_OP_reg0));
808  break;
809 
810  case DW_OP_regx:
811  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
812  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
813  pushf_register_address (indent, stream, registers_used, arch,
815  break;
816 
817  case DW_OP_breg0:
818  case DW_OP_breg1:
819  case DW_OP_breg2:
820  case DW_OP_breg3:
821  case DW_OP_breg4:
822  case DW_OP_breg5:
823  case DW_OP_breg6:
824  case DW_OP_breg7:
825  case DW_OP_breg8:
826  case DW_OP_breg9:
827  case DW_OP_breg10:
828  case DW_OP_breg11:
829  case DW_OP_breg12:
830  case DW_OP_breg13:
831  case DW_OP_breg14:
832  case DW_OP_breg15:
833  case DW_OP_breg16:
834  case DW_OP_breg17:
835  case DW_OP_breg18:
836  case DW_OP_breg19:
837  case DW_OP_breg20:
838  case DW_OP_breg21:
839  case DW_OP_breg22:
840  case DW_OP_breg23:
841  case DW_OP_breg24:
842  case DW_OP_breg25:
843  case DW_OP_breg26:
844  case DW_OP_breg27:
845  case DW_OP_breg28:
846  case DW_OP_breg29:
847  case DW_OP_breg30:
848  case DW_OP_breg31:
849  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
850  pushf_register (indent, stream, registers_used, arch,
852  op - DW_OP_breg0),
853  offset);
854  break;
855  case DW_OP_bregx:
856  {
857  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
858  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
859  pushf_register (indent, stream, registers_used, arch,
861  }
862  break;
863  case DW_OP_fbreg:
864  {
865  const gdb_byte *datastart;
866  size_t datalen;
867  const struct block *b;
868  struct symbol *framefunc;
869  char fb_name[50];
870 
871  b = block_for_pc (pc);
872 
873  if (!b)
874  error (_("No block found for address"));
875 
876  framefunc = block_linkage_function (b);
877 
878  if (!framefunc)
879  error (_("No function found for block"));
880 
881  func_get_frame_base_dwarf_block (framefunc, pc,
882  &datastart, &datalen);
883 
884  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
885 
886  /* Generate a unique-enough name, in case the frame base
887  is computed multiple times in this expression. */
888  xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
889  (long) (op_ptr - base));
890 
891  do_compile_dwarf_expr_to_c (indent, stream,
892  GCC_UINTPTR, fb_name,
893  sym, pc,
894  arch, registers_used, addr_size,
895  datastart, datastart + datalen,
896  NULL, per_cu);
897 
898  pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
899  }
900  break;
901 
902  case DW_OP_dup:
903  pushf (indent, stream, "__gdb_stack[__gdb_tos]");
904  break;
905 
906  case DW_OP_drop:
907  fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
908  break;
909 
910  case DW_OP_pick:
911  offset = *op_ptr++;
912  pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
913  plongest (offset));
914  break;
915 
916  case DW_OP_swap:
917  fprintfi_filtered (indent, &stream,
918  "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
919  fprintfi_filtered (indent, &stream,
920  "__gdb_stack[__gdb_tos - 1] = "
921  "__gdb_stack[__gdb_tos];\n");
922  fprintfi_filtered (indent, &stream, ("__gdb_stack[__gdb_tos] = "
923  "__gdb_tmp;\n"));
924  break;
925 
926  case DW_OP_over:
927  pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
928  break;
929 
930  case DW_OP_rot:
931  fprintfi_filtered (indent, &stream, ("__gdb_tmp = "
932  "__gdb_stack[__gdb_tos];\n"));
933  fprintfi_filtered (indent, &stream,
934  "__gdb_stack[__gdb_tos] = "
935  "__gdb_stack[__gdb_tos - 1];\n");
936  fprintfi_filtered (indent, &stream,
937  "__gdb_stack[__gdb_tos - 1] = "
938  "__gdb_stack[__gdb_tos -2];\n");
939  fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 2] = "
940  "__gdb_tmp;\n");
941  break;
942 
943  case DW_OP_deref:
944  case DW_OP_deref_size:
945  {
946  int size;
947  const char *mode;
948 
949  if (op == DW_OP_deref_size)
950  size = *op_ptr++;
951  else
952  size = addr_size;
953 
954  mode = c_get_mode_for_size (size);
955  if (mode == NULL)
956  error (_("Unsupported size %d in %s"),
957  size, get_DW_OP_name (op));
958 
959  /* Cast to a pointer of the desired type, then
960  dereference. */
961  fprintfi_filtered (indent, &stream,
962  "__gdb_stack[__gdb_tos] = "
963  "*((__gdb_int_%s *) "
964  "__gdb_stack[__gdb_tos]);\n",
965  mode);
966  }
967  break;
968 
969  case DW_OP_abs:
970  unary (indent, stream,
971  "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
972  "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
973  break;
974 
975  case DW_OP_neg:
976  unary (indent, stream, "-__gdb_stack[__gdb_tos]");
977  break;
978 
979  case DW_OP_not:
980  unary (indent, stream, "~__gdb_stack[__gdb_tos]");
981  break;
982 
983  case DW_OP_plus_uconst:
984  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
985  unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
986  hex_string (reg));
987  break;
988 
989  case DW_OP_div:
990  binary (indent, stream, ("((" GCC_INTPTR
991  ") __gdb_stack[__gdb_tos-1]) / (("
992  GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
993  break;
994 
995  case DW_OP_shra:
996  binary (indent, stream,
997  "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
998  "__gdb_stack[__gdb_tos]");
999  break;
1000 
1001 #define BINARY(OP) \
1002  binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1003  " __gdb_stack[__gdb_tos]"); \
1004  break
1005 
1006  case DW_OP_and:
1007  BINARY (&);
1008  case DW_OP_minus:
1009  BINARY (-);
1010  case DW_OP_mod:
1011  BINARY (%);
1012  case DW_OP_mul:
1013  BINARY (*);
1014  case DW_OP_or:
1015  BINARY (|);
1016  case DW_OP_plus:
1017  BINARY (+);
1018  case DW_OP_shl:
1019  BINARY (<<);
1020  case DW_OP_shr:
1021  BINARY (>>);
1022  case DW_OP_xor:
1023  BINARY (^);
1024 #undef BINARY
1025 
1026 #define COMPARE(OP) \
1027  binary (indent, stream, \
1028  "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1029  " ((" GCC_INTPTR \
1030  ") __gdb_stack[__gdb_tos]))"); \
1031  break
1032 
1033  case DW_OP_le:
1034  COMPARE (<=);
1035  case DW_OP_ge:
1036  COMPARE (>=);
1037  case DW_OP_eq:
1038  COMPARE (==);
1039  case DW_OP_lt:
1040  COMPARE (<);
1041  case DW_OP_gt:
1042  COMPARE (>);
1043  case DW_OP_ne:
1044  COMPARE (!=);
1045 #undef COMPARE
1046 
1047  case DW_OP_call_frame_cfa:
1048  {
1049  int regnum;
1050  CORE_ADDR text_offset;
1051  LONGEST off;
1052  const gdb_byte *cfa_start, *cfa_end;
1053 
1054  if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1055  &regnum, &off,
1056  &text_offset, &cfa_start, &cfa_end))
1057  {
1058  /* Register. */
1059  pushf_register (indent, stream, registers_used, arch, regnum,
1060  off);
1061  }
1062  else
1063  {
1064  /* Another expression. */
1065  char cfa_name[50];
1066 
1067  /* Generate a unique-enough name, in case the CFA is
1068  computed multiple times in this expression. */
1069  xsnprintf (cfa_name, sizeof (cfa_name),
1070  "__cfa_%ld", (long) (op_ptr - base));
1071 
1072  do_compile_dwarf_expr_to_c (indent, stream,
1073  GCC_UINTPTR, cfa_name,
1074  sym, pc, arch, registers_used,
1075  addr_size,
1076  cfa_start, cfa_end,
1077  &text_offset, per_cu);
1078  pushf (indent, stream, "%s", cfa_name);
1079  }
1080  }
1081 
1082  break;
1083 
1084  case DW_OP_skip:
1085  offset = extract_signed_integer (op_ptr, 2, byte_order);
1086  op_ptr += 2;
1087  fprintfi_filtered (indent, &stream, "goto ");
1088  print_label (stream, scope, op_ptr + offset - base);
1089  stream.puts (";\n");
1090  break;
1091 
1092  case DW_OP_bra:
1093  offset = extract_signed_integer (op_ptr, 2, byte_order);
1094  op_ptr += 2;
1095  fprintfi_filtered (indent, &stream,
1096  "if ((( " GCC_INTPTR
1097  ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1098  print_label (stream, scope, op_ptr + offset - base);
1099  stream.puts (";\n");
1100  break;
1101 
1102  case DW_OP_nop:
1103  break;
1104 
1105  default:
1106  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1107  }
1108  }
1109 
1110  fprintfi_filtered (indent, &stream, "%s = __gdb_stack[__gdb_tos];\n",
1111  result_name);
1112  fprintfi_filtered (indent - 2, &stream, "}\n");
1113 }
1114 
1115 /* See compile.h. */
1116 
1117 void
1118 compile_dwarf_expr_to_c (string_file &stream, const char *result_name,
1119  struct symbol *sym, CORE_ADDR pc,
1120  struct gdbarch *arch, unsigned char *registers_used,
1121  unsigned int addr_size,
1122  const gdb_byte *op_ptr, const gdb_byte *op_end,
1123  struct dwarf2_per_cu_data *per_cu)
1124 {
1125  do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1126  arch, registers_used, addr_size, op_ptr, op_end,
1127  NULL, per_cu);
1128 }
1129 
1130 /* See compile.h. */
1131 
1132 void
1134  const char *result_name,
1135  const struct dynamic_prop *prop,
1136  struct symbol *sym, CORE_ADDR pc,
1137  struct gdbarch *arch, unsigned char *registers_used,
1138  unsigned int addr_size,
1139  const gdb_byte *op_ptr, const gdb_byte *op_end,
1140  struct dwarf2_per_cu_data *per_cu)
1141 {
1142  do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1143  sym, pc, arch, registers_used,
1144  addr_size, op_ptr, op_end, NULL, per_cu);
1145 }
const char * string
Definition: signals.c:50
unsigned int is_tls
Definition: compile-loc2c.c:55
struct frame_info * get_selected_frame(const char *message)
Definition: frame.c:1638
#define SYMBOL_PRINT_NAME(symbol)
Definition: symtab.h:542
int dwarf2_fetch_cfa_info(struct gdbarch *gdbarch, CORE_ADDR pc, struct dwarf2_per_cu_data *data, int *regnum_out, LONGEST *offset_out, CORE_ADDR *text_offset_out, const gdb_byte **cfa_start_out, const gdb_byte **cfa_end_out)
Definition: dwarf2-frame.c:862
bfd_vma CORE_ADDR
Definition: common-types.h:41
#define COMPARE(OP)
void compile_dwarf_bounds_to_c(string_file &stream, const char *result_name, const struct dynamic_prop *prop, struct symbol *sym, CORE_ADDR pc, struct gdbarch *arch, unsigned char *registers_used, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, struct dwarf2_per_cu_data *per_cu)
void warning(const char *fmt,...)
Definition: errors.c:26
void print_spaces(int n, struct ui_file *file)
Definition: utils.c:810
struct symbol * block_linkage_function(const struct block *bl)
Definition: block.c:100
static void push(int indent, string_file &stream, ULONGEST l)
void dwarf_expr_require_composition(const gdb_byte *op_ptr, const gdb_byte *op_end, const char *op_name)
Definition: dwarf2expr.c:347
const gdb_byte * safe_read_uleb128(const gdb_byte *buf, const gdb_byte *buf_end, uint64_t *r)
Definition: dwarf2expr.c:311
#define _(String)
Definition: gdb_locale.h:35
static void pushf(int indent, string_file &stream, const char *format,...) ATTRIBUTE_PRINTF(3
static void compute_stack_depth_worker(int start, int *need_tempvar, std::vector< struct insn_info > *info, std::vector< int > *to_do, enum bfd_endian byte_order, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end)
Definition: compile-loc2c.c:73
#define VALUE_LVAL(val)
Definition: value.h:414
#define GCC_UINTPTR
const struct block * block_for_pc(CORE_ADDR pc)
Definition: block.c:282
unsigned int depth
Definition: compile-loc2c.c:41
static void binary(int indent, string_file &stream, const char *format,...) ATTRIBUTE_PRINTF(3
int dwarf_reg_to_regnum_or_error(struct gdbarch *arch, ULONGEST dwarf_reg)
Definition: dwarf2loc.c:2920
struct value::@186::@187 reg
struct value * read_var_value(struct symbol *var, const struct block *var_block, struct frame_info *frame)
Definition: findvar.c:807
static ULONGEST extract_unsigned_integer(const gdb_byte *addr, int len, enum bfd_endian byte_order)
Definition: defs.h:577
static void print_label(string_file &stream, unsigned int scope, int target)
#define COMPILE_I_SIMPLE_REGISTER_ARG_NAME
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1509
#define ATTRIBUTE_PRINTF
Definition: common-defs.h:70
static void do_compile_dwarf_expr_to_c(int indent, string_file &stream, const char *type_name, const char *result_name, struct symbol *sym, CORE_ADDR pc, struct gdbarch *arch, unsigned char *registers_used, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, CORE_ADDR *initial, struct dwarf2_per_cu_data *per_cu)
static void pushf_register(int indent, string_file &stream, unsigned char *registers_used, struct gdbarch *gdbarch, int regnum, uint64_t offset)
CORE_ADDR dwarf2_per_cu_text_offset(struct dwarf2_per_cu_data *cu)
Definition: dwarf2read.c:24898
int regnum
Definition: aarch64-tdep.c:77
const char * c_get_mode_for_size(int size)
virtual void puts(const char *str)
Definition: ui-file.h:63
#define GCC_INTPTR
const gdb_byte * safe_read_sleb128(const gdb_byte *buf, const gdb_byte *buf_end, int64_t *r)
Definition: dwarf2expr.c:323
Definition: regdef.h:22
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: block.h:60
Definition: value.c:169
void fprintfi_filtered(int spaces, struct ui_file *stream, const char *format,...)
Definition: utils.c:2031
void printf(const char *,...) ATTRIBUTE_PRINTF(2
Definition: ui-file.c:37
bfd_byte gdb_byte
Definition: common-types.h:38
std::string compile_register_name_mangled(struct gdbarch *gdbarch, int regnum)
Definition: compile.c:651
void func_get_frame_base_dwarf_block(struct symbol *framefunc, CORE_ADDR pc, const gdb_byte **start, size_t *length)
Definition: dwarf2loc.c:508
int xsnprintf(char *str, size_t size, const char *format,...)
Definition: common-utils.c:134
#define SET_CHECK_DEPTH(WHERE)
void compile_dwarf_expr_to_c(string_file &stream, const char *result_name, struct symbol *sym, CORE_ADDR pc, struct gdbarch *arch, unsigned char *registers_used, unsigned int addr_size, const gdb_byte *op_ptr, const gdb_byte *op_end, struct dwarf2_per_cu_data *per_cu)
int offset
Definition: agent.c:65
static LONGEST extract_signed_integer(const gdb_byte *addr, int len, enum bfd_endian byte_order)
Definition: defs.h:570
struct gdbarch * arch
Definition: symtab.h:1071
unsigned long long ULONGEST
Definition: common-types.h:53
unsigned int label
Definition: compile-loc2c.c:49
#define BINARY(OP)
static int compute_stack_depth(enum bfd_endian byte_order, unsigned int addr_size, int *need_tempvar, int *is_tls, const gdb_byte *op_ptr, const gdb_byte *op_end, int initial_depth, std::vector< struct insn_info > *info)
CORE_ADDR value_address(const struct value *value)
Definition: value.c:1529
unsigned int visited
Definition: compile-loc2c.c:45
static void unary(int indent, string_file &stream, const char *format,...) ATTRIBUTE_PRINTF(3
void error(const char *fmt,...)
Definition: errors.c:38
size_t size
Definition: go32-nat.c:242
static void pushf_register_address(int indent, string_file &stream, unsigned char *registers_used, struct gdbarch *gdbarch, int regnum)
long long LONGEST
Definition: common-types.h:52