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