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