xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/compile/compile-loc2c.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* Convert a DWARF location expression to C
2 
3    Copyright (C) 2014-2016 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.  This is
52      a hack until we can add a feature to glibc to let us properly
53      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 an array 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 			    struct insn_info *info,
75 			    VEC (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 	  info[ndx].is_tls = 1;
327 	  break;
328 
329 	case DW_OP_skip:
330 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
331 	  op_ptr += 2;
332 	  offset = op_ptr + offset - base;
333 	  /* If the destination has not been seen yet, add it to the
334 	     to-do list.  */
335 	  if (!info[offset].visited)
336 	    VEC_safe_push (int, *to_do, offset);
337 	  SET_CHECK_DEPTH (offset);
338 	  info[offset].label = 1;
339 	  /* We're done with this line of code.  */
340 	  return;
341 
342 	case DW_OP_bra:
343 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
344 	  op_ptr += 2;
345 	  offset = op_ptr + offset - base;
346 	  --stack_depth;
347 	  /* If the destination has not been seen yet, add it to the
348 	     to-do list.  */
349 	  if (!info[offset].visited)
350 	    VEC_safe_push (int, *to_do, offset);
351 	  SET_CHECK_DEPTH (offset);
352 	  info[offset].label = 1;
353 	  break;
354 
355 	case DW_OP_nop:
356 	  break;
357 
358 	default:
359 	  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
360 	}
361     }
362 
363   gdb_assert (op_ptr == op_end);
364 
365 #undef SET_CHECK_DEPTH
366 }
367 
368 /* Compute the maximum needed stack depth of a DWARF expression, and
369    some other information as well.
370 
371    BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
372    NEED_TEMPVAR is an out parameter which is set if this expression
373    needs a special temporary variable to be emitted (see the code
374    generator).
375    IS_TLS is an out parameter which is set if this expression refers
376    to a TLS variable.
377    OP_PTR and OP_END are the bounds of the DWARF expression.
378    INITIAL_DEPTH is the initial depth of the DWARF expression stack.
379    INFO is an array of insn_info objects, indexed by offset from the
380    start of the DWARF expression.
381 
382    This returns the maximum stack depth.  */
383 
384 static int
385 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
386 		     int *need_tempvar, int *is_tls,
387 		     const gdb_byte *op_ptr, const gdb_byte *op_end,
388 		     int initial_depth,
389 		     struct insn_info **info)
390 {
391   unsigned char *set;
392   struct cleanup *outer_cleanup, *cleanup;
393   VEC (int) *to_do = NULL;
394   int stack_depth, i;
395 
396   *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
397   outer_cleanup = make_cleanup (xfree, *info);
398 
399   cleanup = make_cleanup (VEC_cleanup (int), &to_do);
400 
401   VEC_safe_push (int, to_do, 0);
402   (*info)[0].depth = initial_depth;
403   (*info)[0].visited = 1;
404 
405   while (!VEC_empty (int, to_do))
406     {
407       int ndx = VEC_pop (int, to_do);
408 
409       compute_stack_depth_worker (ndx, need_tempvar, *info, &to_do,
410 				  byte_order, addr_size,
411 				  op_ptr, op_end);
412     }
413 
414   stack_depth = 0;
415   *is_tls = 0;
416   for (i = 0; i < op_end - op_ptr; ++i)
417     {
418       if ((*info)[i].depth > stack_depth)
419 	stack_depth = (*info)[i].depth;
420       if ((*info)[i].is_tls)
421 	*is_tls = 1;
422     }
423 
424   do_cleanups (cleanup);
425   discard_cleanups (outer_cleanup);
426   return stack_depth + 1;
427 }
428 
429 
430 
431 #define GCC_UINTPTR "__gdb_uintptr"
432 #define GCC_INTPTR "__gdb_intptr"
433 
434 /* Emit code to push a constant.  */
435 
436 static void
437 push (int indent, struct ui_file *stream, ULONGEST l)
438 {
439   fprintfi_filtered (indent, stream,
440 		     "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
441 		     hex_string (l));
442 }
443 
444 /* Emit code to push an arbitrary expression.  This works like
445    printf.  */
446 
447 static void pushf (int indent, struct ui_file *stream, const char *format, ...)
448   ATTRIBUTE_PRINTF (3, 4);
449 
450 static void
451 pushf (int indent, struct ui_file *stream, const char *format, ...)
452 {
453   va_list args;
454 
455   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
456   va_start (args, format);
457   vfprintf_filtered (stream, format, args);
458   va_end (args);
459   fprintf_filtered (stream, ";\n");
460 
461   fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
462 }
463 
464 /* Emit code for a unary expression -- one which operates in-place on
465    the top-of-stack.  This works like printf.  */
466 
467 static void unary (int indent, struct ui_file *stream, const char *format, ...)
468   ATTRIBUTE_PRINTF (3, 4);
469 
470 static void
471 unary (int indent, struct ui_file *stream, const char *format, ...)
472 {
473   va_list args;
474 
475   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
476   va_start (args, format);
477   vfprintf_filtered (stream, format, args);
478   va_end (args);
479   fprintf_filtered (stream, ";\n");
480 }
481 
482 /* Emit code for a unary expression -- one which uses the top two
483    stack items, popping the topmost one.  This works like printf.  */
484 static void binary (int indent, struct ui_file *stream, const char *format, ...)
485   ATTRIBUTE_PRINTF (3, 4);
486 
487 static void
488 binary (int indent, struct ui_file *stream, const char *format, ...)
489 {
490   va_list args;
491 
492   fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
493   va_start (args, format);
494   vfprintf_filtered (stream, format, args);
495   va_end (args);
496   fprintf_filtered (stream, ";\n");
497   fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
498 }
499 
500 /* Print the name of a label given its "SCOPE", an arbitrary integer
501    used for uniqueness, and its TARGET, the bytecode offset
502    corresponding to the label's point of definition.  */
503 
504 static void
505 print_label (struct ui_file *stream, unsigned int scope, int target)
506 {
507   fprintf_filtered (stream, "__label_%u_%s",
508 		    scope, pulongest (target));
509 }
510 
511 /* Emit code that pushes a register's address on the stack.
512    REGISTERS_USED is an out parameter which is updated to note which
513    register was needed by this expression.  */
514 
515 static void
516 pushf_register_address (int indent, struct ui_file *stream,
517 			unsigned char *registers_used,
518 			struct gdbarch *gdbarch, int regnum)
519 {
520   char *regname = compile_register_name_mangled (gdbarch, regnum);
521   struct cleanup *cleanups = make_cleanup (xfree, regname);
522 
523   registers_used[regnum] = 1;
524   pushf (indent, stream,
525 	 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
526 	 regname);
527 
528   do_cleanups (cleanups);
529 }
530 
531 /* Emit code that pushes a register's value on the stack.
532    REGISTERS_USED is an out parameter which is updated to note which
533    register was needed by this expression.  OFFSET is added to the
534    register's value before it is pushed.  */
535 
536 static void
537 pushf_register (int indent, struct ui_file *stream,
538 		unsigned char *registers_used,
539 		struct gdbarch *gdbarch, int regnum, uint64_t offset)
540 {
541   char *regname = compile_register_name_mangled (gdbarch, regnum);
542   struct cleanup *cleanups = make_cleanup (xfree, regname);
543 
544   registers_used[regnum] = 1;
545   if (offset == 0)
546     pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
547 	   regname);
548   else
549     pushf (indent, stream,
550 	   COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
551 	   regname, hex_string (offset));
552 
553   do_cleanups (cleanups);
554 }
555 
556 /* Compile a DWARF expression to C code.
557 
558    INDENT is the indentation level to use.
559    STREAM is the stream where the code should be written.
560 
561    TYPE_NAME names the type of the result of the DWARF expression.
562    For locations this is "void *" but for array bounds it will be an
563    integer type.
564 
565    RESULT_NAME is the name of a variable in the resulting C code.  The
566    result of the expression will be assigned to this variable.
567 
568    SYM is the symbol corresponding to this expression.
569    PC is the location at which the expression is being evaluated.
570    ARCH is the architecture to use.
571 
572    REGISTERS_USED is an out parameter which is updated to note which
573    registers were needed by this expression.
574 
575    ADDR_SIZE is the DWARF address size to use.
576 
577    OPT_PTR and OP_END are the bounds of the DWARF expression.
578 
579    If non-NULL, INITIAL points to an initial value to write to the
580    stack.  If NULL, no initial value is written.
581 
582    PER_CU is the per-CU object used for looking up various other
583    things.  */
584 
585 static void
586 do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
587 			    const char *type_name,
588 			    const char *result_name,
589 			    struct symbol *sym, CORE_ADDR pc,
590 			    struct gdbarch *arch,
591 			    unsigned char *registers_used,
592 			    unsigned int addr_size,
593 			    const gdb_byte *op_ptr, const gdb_byte *op_end,
594 			    CORE_ADDR *initial,
595 			    struct dwarf2_per_cu_data *per_cu)
596 {
597   /* We keep a counter so that labels and other objects we create have
598      unique names.  */
599   static unsigned int scope;
600 
601   enum bfd_endian byte_order = gdbarch_byte_order (arch);
602   const gdb_byte * const base = op_ptr;
603   int need_tempvar = 0;
604   int is_tls = 0;
605   struct cleanup *cleanup;
606   struct insn_info *info;
607   int stack_depth;
608 
609   ++scope;
610 
611   fprintfi_filtered (indent, stream, "__attribute__ ((unused)) %s %s;\n",
612 		     type_name, result_name);
613   fprintfi_filtered (indent, stream, "{\n");
614   indent += 2;
615 
616   stack_depth = compute_stack_depth (byte_order, addr_size,
617 				     &need_tempvar, &is_tls,
618 				     op_ptr, op_end, initial != NULL,
619 				     &info);
620   cleanup = make_cleanup (xfree, info);
621 
622   /* This is a hack until we can add a feature to glibc to let us
623      properly generate code for TLS.  You might think we could emit
624      the address in the ordinary course of translating
625      DW_OP_GNU_push_tls_address, but since the operand appears on the
626      stack, it is relatively hard to find, and the idea of calling
627      target_translate_tls_address with OFFSET==0 and then adding the
628      offset by hand seemed too hackish.  */
629   if (is_tls)
630     {
631       struct frame_info *frame = get_selected_frame (NULL);
632       struct value *val;
633 
634       if (frame == NULL)
635 	error (_("Symbol \"%s\" cannot be used because "
636 		 "there is no selected frame"),
637 	       SYMBOL_PRINT_NAME (sym));
638 
639       val = read_var_value (sym, NULL, frame);
640       if (VALUE_LVAL (val) != lval_memory)
641 	error (_("Symbol \"%s\" cannot be used for compilation evaluation "
642 		 "as its address has not been found."),
643 	       SYMBOL_PRINT_NAME (sym));
644 
645       warning (_("Symbol \"%s\" is thread-local and currently can only "
646 		 "be referenced from the current thread in "
647 		 "compiled code."),
648 	       SYMBOL_PRINT_NAME (sym));
649 
650       fprintfi_filtered (indent, stream, "%s = %s;\n",
651 			 result_name,
652 			 core_addr_to_string (value_address (val)));
653       fprintfi_filtered (indent - 2, stream, "}\n");
654       do_cleanups (cleanup);
655       return;
656     }
657 
658   fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
659 		     stack_depth);
660 
661   if (need_tempvar)
662     fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
663   fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
664 
665   if (initial != NULL)
666     pushf (indent, stream, "%s", core_addr_to_string (*initial));
667 
668   while (op_ptr < op_end)
669     {
670       enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
671       uint64_t uoffset, reg;
672       int64_t offset;
673 
674       print_spaces (indent - 2, stream);
675       if (info[op_ptr - base].label)
676 	{
677 	  print_label (stream, scope, op_ptr - base);
678 	  fprintf_filtered (stream, ":;");
679 	}
680       fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
681 
682       /* This is handy for debugging the generated code:
683       fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
684 			(int) info[op_ptr - base].depth - 1);
685       */
686 
687       ++op_ptr;
688 
689       switch (op)
690 	{
691 	case DW_OP_lit0:
692 	case DW_OP_lit1:
693 	case DW_OP_lit2:
694 	case DW_OP_lit3:
695 	case DW_OP_lit4:
696 	case DW_OP_lit5:
697 	case DW_OP_lit6:
698 	case DW_OP_lit7:
699 	case DW_OP_lit8:
700 	case DW_OP_lit9:
701 	case DW_OP_lit10:
702 	case DW_OP_lit11:
703 	case DW_OP_lit12:
704 	case DW_OP_lit13:
705 	case DW_OP_lit14:
706 	case DW_OP_lit15:
707 	case DW_OP_lit16:
708 	case DW_OP_lit17:
709 	case DW_OP_lit18:
710 	case DW_OP_lit19:
711 	case DW_OP_lit20:
712 	case DW_OP_lit21:
713 	case DW_OP_lit22:
714 	case DW_OP_lit23:
715 	case DW_OP_lit24:
716 	case DW_OP_lit25:
717 	case DW_OP_lit26:
718 	case DW_OP_lit27:
719 	case DW_OP_lit28:
720 	case DW_OP_lit29:
721 	case DW_OP_lit30:
722 	case DW_OP_lit31:
723 	  push (indent, stream, op - DW_OP_lit0);
724 	  break;
725 
726 	case DW_OP_addr:
727 	  op_ptr += addr_size;
728 	  /* Some versions of GCC emit DW_OP_addr before
729 	     DW_OP_GNU_push_tls_address.  In this case the value is an
730 	     index, not an address.  We don't support things like
731 	     branching between the address and the TLS op.  */
732 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
733 	    uoffset += dwarf2_per_cu_text_offset (per_cu);
734 	  push (indent, stream, uoffset);
735 	  break;
736 
737 	case DW_OP_const1u:
738 	  push (indent, stream,
739 		extract_unsigned_integer (op_ptr, 1, byte_order));
740 	  op_ptr += 1;
741 	  break;
742 	case DW_OP_const1s:
743 	  push (indent, stream,
744 		extract_signed_integer (op_ptr, 1, byte_order));
745 	  op_ptr += 1;
746 	  break;
747 	case DW_OP_const2u:
748 	  push (indent, stream,
749 		extract_unsigned_integer (op_ptr, 2, byte_order));
750 	  op_ptr += 2;
751 	  break;
752 	case DW_OP_const2s:
753 	  push (indent, stream,
754 		extract_signed_integer (op_ptr, 2, byte_order));
755 	  op_ptr += 2;
756 	  break;
757 	case DW_OP_const4u:
758 	  push (indent, stream,
759 		extract_unsigned_integer (op_ptr, 4, byte_order));
760 	  op_ptr += 4;
761 	  break;
762 	case DW_OP_const4s:
763 	  push (indent, stream,
764 		extract_signed_integer (op_ptr, 4, byte_order));
765 	  op_ptr += 4;
766 	  break;
767 	case DW_OP_const8u:
768 	  push (indent, stream,
769 		extract_unsigned_integer (op_ptr, 8, byte_order));
770 	  op_ptr += 8;
771 	  break;
772 	case DW_OP_const8s:
773 	  push (indent, stream,
774 		extract_signed_integer (op_ptr, 8, byte_order));
775 	  op_ptr += 8;
776 	  break;
777 	case DW_OP_constu:
778 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
779 	  push (indent, stream, uoffset);
780 	  break;
781 	case DW_OP_consts:
782 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
783 	  push (indent, stream, offset);
784 	  break;
785 
786 	case DW_OP_reg0:
787 	case DW_OP_reg1:
788 	case DW_OP_reg2:
789 	case DW_OP_reg3:
790 	case DW_OP_reg4:
791 	case DW_OP_reg5:
792 	case DW_OP_reg6:
793 	case DW_OP_reg7:
794 	case DW_OP_reg8:
795 	case DW_OP_reg9:
796 	case DW_OP_reg10:
797 	case DW_OP_reg11:
798 	case DW_OP_reg12:
799 	case DW_OP_reg13:
800 	case DW_OP_reg14:
801 	case DW_OP_reg15:
802 	case DW_OP_reg16:
803 	case DW_OP_reg17:
804 	case DW_OP_reg18:
805 	case DW_OP_reg19:
806 	case DW_OP_reg20:
807 	case DW_OP_reg21:
808 	case DW_OP_reg22:
809 	case DW_OP_reg23:
810 	case DW_OP_reg24:
811 	case DW_OP_reg25:
812 	case DW_OP_reg26:
813 	case DW_OP_reg27:
814 	case DW_OP_reg28:
815 	case DW_OP_reg29:
816 	case DW_OP_reg30:
817 	case DW_OP_reg31:
818 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
819 	  pushf_register_address (indent, stream, registers_used, arch,
820 				  dwarf_reg_to_regnum_or_error
821 				    (arch, op - DW_OP_reg0));
822 	  break;
823 
824 	case DW_OP_regx:
825 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
826 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
827 	  pushf_register_address (indent, stream, registers_used, arch,
828 				  dwarf_reg_to_regnum_or_error (arch, reg));
829 	  break;
830 
831 	case DW_OP_breg0:
832 	case DW_OP_breg1:
833 	case DW_OP_breg2:
834 	case DW_OP_breg3:
835 	case DW_OP_breg4:
836 	case DW_OP_breg5:
837 	case DW_OP_breg6:
838 	case DW_OP_breg7:
839 	case DW_OP_breg8:
840 	case DW_OP_breg9:
841 	case DW_OP_breg10:
842 	case DW_OP_breg11:
843 	case DW_OP_breg12:
844 	case DW_OP_breg13:
845 	case DW_OP_breg14:
846 	case DW_OP_breg15:
847 	case DW_OP_breg16:
848 	case DW_OP_breg17:
849 	case DW_OP_breg18:
850 	case DW_OP_breg19:
851 	case DW_OP_breg20:
852 	case DW_OP_breg21:
853 	case DW_OP_breg22:
854 	case DW_OP_breg23:
855 	case DW_OP_breg24:
856 	case DW_OP_breg25:
857 	case DW_OP_breg26:
858 	case DW_OP_breg27:
859 	case DW_OP_breg28:
860 	case DW_OP_breg29:
861 	case DW_OP_breg30:
862 	case DW_OP_breg31:
863 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
864 	  pushf_register (indent, stream, registers_used, arch,
865 			  dwarf_reg_to_regnum_or_error (arch,
866 							op - DW_OP_breg0),
867 			  offset);
868 	  break;
869 	case DW_OP_bregx:
870 	  {
871 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
872 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
873 	    pushf_register (indent, stream, registers_used, arch,
874 			    dwarf_reg_to_regnum_or_error (arch, reg), offset);
875 	  }
876 	  break;
877 	case DW_OP_fbreg:
878 	  {
879 	    const gdb_byte *datastart;
880 	    size_t datalen;
881 	    const struct block *b;
882 	    struct symbol *framefunc;
883 	    char fb_name[50];
884 
885 	    b = block_for_pc (pc);
886 
887 	    if (!b)
888 	      error (_("No block found for address"));
889 
890 	    framefunc = block_linkage_function (b);
891 
892 	    if (!framefunc)
893 	      error (_("No function found for block"));
894 
895 	    func_get_frame_base_dwarf_block (framefunc, pc,
896 					     &datastart, &datalen);
897 
898 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
899 
900 	    /* Generate a unique-enough name, in case the frame base
901 	       is computed multiple times in this expression.  */
902 	    xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
903 		       (long) (op_ptr - base));
904 
905 	    do_compile_dwarf_expr_to_c (indent, stream,
906 					GCC_UINTPTR, fb_name,
907 					sym, pc,
908 					arch, registers_used, addr_size,
909 					datastart, datastart + datalen,
910 					NULL, per_cu);
911 
912 	    pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
913 	  }
914 	  break;
915 
916 	case DW_OP_dup:
917 	  pushf (indent, stream, "__gdb_stack[__gdb_tos]");
918 	  break;
919 
920 	case DW_OP_drop:
921 	  fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
922 	  break;
923 
924 	case DW_OP_pick:
925 	  offset = *op_ptr++;
926 	  pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
927 		 plongest (offset));
928 	  break;
929 
930 	case DW_OP_swap:
931 	  fprintfi_filtered (indent, stream,
932 			     "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
933 	  fprintfi_filtered (indent, stream,
934 			     "__gdb_stack[__gdb_tos - 1] = "
935 			     "__gdb_stack[__gdb_tos];\n");
936 	  fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
937 					      "__gdb_tmp;\n"));
938 	  break;
939 
940 	case DW_OP_over:
941 	  pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
942 	  break;
943 
944 	case DW_OP_rot:
945 	  fprintfi_filtered (indent, stream, ("__gdb_tmp = "
946 					      "__gdb_stack[__gdb_tos];\n"));
947 	  fprintfi_filtered (indent, stream,
948 			     "__gdb_stack[__gdb_tos] = "
949 			     "__gdb_stack[__gdb_tos - 1];\n");
950 	  fprintfi_filtered (indent, stream,
951 			     "__gdb_stack[__gdb_tos - 1] = "
952 			     "__gdb_stack[__gdb_tos -2];\n");
953 	  fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
954 			     "__gdb_tmp;\n");
955 	  break;
956 
957 	case DW_OP_deref:
958 	case DW_OP_deref_size:
959 	  {
960 	    int size;
961 	    const char *mode;
962 
963 	    if (op == DW_OP_deref_size)
964 	      size = *op_ptr++;
965 	    else
966 	      size = addr_size;
967 
968 	    mode = c_get_mode_for_size (size);
969 	    if (mode == NULL)
970 	      error (_("Unsupported size %d in %s"),
971 		     size, get_DW_OP_name (op));
972 
973 	    /* Cast to a pointer of the desired type, then
974 	       dereference.  */
975 	    fprintfi_filtered (indent, stream,
976 			       "__gdb_stack[__gdb_tos] = "
977 			       "*((__gdb_int_%s *) "
978 			       "__gdb_stack[__gdb_tos]);\n",
979 			       mode);
980 	  }
981 	  break;
982 
983 	case DW_OP_abs:
984 	  unary (indent, stream,
985 		 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
986 		 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
987 	  break;
988 
989 	case DW_OP_neg:
990 	  unary (indent, stream, "-__gdb_stack[__gdb_tos]");
991 	  break;
992 
993 	case DW_OP_not:
994 	  unary (indent, stream, "~__gdb_stack[__gdb_tos]");
995 	  break;
996 
997 	case DW_OP_plus_uconst:
998 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
999 	  unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1000 		 hex_string (reg));
1001 	  break;
1002 
1003 	case DW_OP_div:
1004 	  binary (indent, stream, ("((" GCC_INTPTR
1005 				   ") __gdb_stack[__gdb_tos-1]) / (("
1006 				   GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1007 	  break;
1008 
1009 	case DW_OP_shra:
1010 	  binary (indent, stream,
1011 		  "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1012 		  "__gdb_stack[__gdb_tos]");
1013 	  break;
1014 
1015 #define BINARY(OP)							\
1016 	  binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1017 				   " __gdb_stack[__gdb_tos]");	\
1018 	  break
1019 
1020 	case DW_OP_and:
1021 	  BINARY (&);
1022 	case DW_OP_minus:
1023 	  BINARY (-);
1024 	case DW_OP_mod:
1025 	  BINARY (%);
1026 	case DW_OP_mul:
1027 	  BINARY (*);
1028 	case DW_OP_or:
1029 	  BINARY (|);
1030 	case DW_OP_plus:
1031 	  BINARY (+);
1032 	case DW_OP_shl:
1033 	  BINARY (<<);
1034 	case DW_OP_shr:
1035 	  BINARY (>>);
1036 	case DW_OP_xor:
1037 	  BINARY (^);
1038 #undef BINARY
1039 
1040 #define COMPARE(OP)							\
1041 	  binary (indent, stream,					\
1042 		  "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP	\
1043 		  " ((" GCC_INTPTR					\
1044 		  ") __gdb_stack[__gdb_tos]))");			\
1045 	  break
1046 
1047 	case DW_OP_le:
1048 	  COMPARE (<=);
1049 	case DW_OP_ge:
1050 	  COMPARE (>=);
1051 	case DW_OP_eq:
1052 	  COMPARE (==);
1053 	case DW_OP_lt:
1054 	  COMPARE (<);
1055 	case DW_OP_gt:
1056 	  COMPARE (>);
1057 	case DW_OP_ne:
1058 	  COMPARE (!=);
1059 #undef COMPARE
1060 
1061 	case DW_OP_call_frame_cfa:
1062 	  {
1063 	    int regnum;
1064 	    CORE_ADDR text_offset;
1065 	    LONGEST off;
1066 	    const gdb_byte *cfa_start, *cfa_end;
1067 
1068 	    if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1069 				       &regnum, &off,
1070 				       &text_offset, &cfa_start, &cfa_end))
1071 	      {
1072 		/* Register.  */
1073 		pushf_register (indent, stream, registers_used, arch, regnum,
1074 				off);
1075 	      }
1076 	    else
1077 	      {
1078 		/* Another expression.  */
1079 		char cfa_name[50];
1080 
1081 		/* Generate a unique-enough name, in case the CFA is
1082 		   computed multiple times in this expression.  */
1083 		xsnprintf (cfa_name, sizeof (cfa_name),
1084 			   "__cfa_%ld", (long) (op_ptr - base));
1085 
1086 		do_compile_dwarf_expr_to_c (indent, stream,
1087 					    GCC_UINTPTR, cfa_name,
1088 					    sym, pc, arch, registers_used,
1089 					    addr_size,
1090 					    cfa_start, cfa_end,
1091 					    &text_offset, per_cu);
1092 		pushf (indent, stream, "%s", cfa_name);
1093 	      }
1094 	  }
1095 
1096 	  break;
1097 
1098 	case DW_OP_skip:
1099 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
1100 	  op_ptr += 2;
1101 	  fprintfi_filtered (indent, stream, "goto ");
1102 	  print_label (stream, scope, op_ptr + offset - base);
1103 	  fprintf_filtered (stream, ";\n");
1104 	  break;
1105 
1106 	case DW_OP_bra:
1107 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
1108 	  op_ptr += 2;
1109 	  fprintfi_filtered (indent, stream,
1110 			     "if ((( " GCC_INTPTR
1111 			     ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1112 	  print_label (stream, scope, op_ptr + offset - base);
1113 	  fprintf_filtered (stream, ";\n");
1114 	  break;
1115 
1116 	case DW_OP_nop:
1117 	  break;
1118 
1119 	default:
1120 	  error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1121 	}
1122     }
1123 
1124   fprintfi_filtered (indent, stream, "%s = __gdb_stack[__gdb_tos];\n",
1125 		     result_name);
1126   fprintfi_filtered (indent - 2, stream, "}\n");
1127 
1128   do_cleanups (cleanup);
1129 }
1130 
1131 /* See compile.h.  */
1132 
1133 void
1134 compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
1135 			 struct symbol *sym, CORE_ADDR pc,
1136 			 struct gdbarch *arch, unsigned char *registers_used,
1137 			 unsigned int addr_size,
1138 			 const gdb_byte *op_ptr, const gdb_byte *op_end,
1139 			 struct dwarf2_per_cu_data *per_cu)
1140 {
1141   do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1142 			      arch, registers_used, addr_size, op_ptr, op_end,
1143 			      NULL, per_cu);
1144 }
1145 
1146 /* See compile.h.  */
1147 
1148 void
1149 compile_dwarf_bounds_to_c (struct ui_file *stream,
1150 			   const char *result_name,
1151 			   const struct dynamic_prop *prop,
1152 			   struct symbol *sym, CORE_ADDR pc,
1153 			   struct gdbarch *arch, unsigned char *registers_used,
1154 			   unsigned int addr_size,
1155 			   const gdb_byte *op_ptr, const gdb_byte *op_end,
1156 			   struct dwarf2_per_cu_data *per_cu)
1157 {
1158   do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1159 			      sym, pc, arch, registers_used,
1160 			      addr_size, op_ptr, op_end, NULL, per_cu);
1161 }
1162