Fix calculation of space remaining in buffer when printing the contents of a DST__K_R...
[binutils-gdb.git] / gdb / compile / compile-loc2c.c
blobfbcc3d6340b264bdfb3c641a05f293839f2b87f5
1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "dwarf2.h"
21 #include "objfiles.h"
22 #include "dwarf2/expr.h"
23 #include "dwarf2/loc.h"
24 #include "dwarf2/read.h"
25 #include "ui-file.h"
26 #include "utils.h"
27 #include "compile-internal.h"
28 #include "compile-c.h"
29 #include "compile.h"
30 #include "block.h"
31 #include "dwarf2/frame.h"
32 #include "gdbsupport/gdb_vecs.h"
33 #include "value.h"
34 #include "gdbarch.h"
38 /* Information about a given instruction. */
40 struct insn_info
42 /* Stack depth at entry. */
44 unsigned int depth;
46 /* Whether this instruction has been visited. */
48 unsigned int visited : 1;
50 /* Whether this instruction needs a label. */
52 unsigned int label : 1;
54 /* Whether this instruction is DW_OP_GNU_push_tls_address or
55 DW_OP_form_tls_address. This is a hack until we can add a
56 feature to glibc to let us properly generate code for TLS. */
58 unsigned int is_tls : 1;
61 /* A helper function for compute_stack_depth that does the work. This
62 examines the DWARF expression starting from START and computes
63 stack effects.
65 NEED_TEMPVAR is an out parameter which is set if this expression
66 needs a special temporary variable to be emitted (see the code
67 generator).
68 INFO is a vector of insn_info objects, indexed by offset from the
69 start of the DWARF expression.
70 TO_DO is a list of bytecodes which must be examined; it may be
71 added to by this function.
72 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
73 OP_PTR and OP_END are the bounds of the DWARF expression. */
75 static void
76 compute_stack_depth_worker (int start, int *need_tempvar,
77 std::vector<struct insn_info> *info,
78 std::vector<int> *to_do,
79 enum bfd_endian byte_order, unsigned int addr_size,
80 const gdb_byte *op_ptr, const gdb_byte *op_end)
82 const gdb_byte * const base = op_ptr;
83 int stack_depth;
85 op_ptr += start;
86 gdb_assert ((*info)[start].visited);
87 stack_depth = (*info)[start].depth;
89 while (op_ptr < op_end)
91 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
92 uint64_t reg;
93 int64_t offset;
94 int ndx = op_ptr - base;
96 #define SET_CHECK_DEPTH(WHERE) \
97 if ((*info)[WHERE].visited) \
98 { \
99 if ((*info)[WHERE].depth != stack_depth) \
100 error (_("inconsistent stack depths")); \
102 else \
104 /* Stack depth not set, so set it. */ \
105 (*info)[WHERE].visited = 1; \
106 (*info)[WHERE].depth = stack_depth; \
109 SET_CHECK_DEPTH (ndx);
111 ++op_ptr;
113 switch (op)
115 case DW_OP_lit0:
116 case DW_OP_lit1:
117 case DW_OP_lit2:
118 case DW_OP_lit3:
119 case DW_OP_lit4:
120 case DW_OP_lit5:
121 case DW_OP_lit6:
122 case DW_OP_lit7:
123 case DW_OP_lit8:
124 case DW_OP_lit9:
125 case DW_OP_lit10:
126 case DW_OP_lit11:
127 case DW_OP_lit12:
128 case DW_OP_lit13:
129 case DW_OP_lit14:
130 case DW_OP_lit15:
131 case DW_OP_lit16:
132 case DW_OP_lit17:
133 case DW_OP_lit18:
134 case DW_OP_lit19:
135 case DW_OP_lit20:
136 case DW_OP_lit21:
137 case DW_OP_lit22:
138 case DW_OP_lit23:
139 case DW_OP_lit24:
140 case DW_OP_lit25:
141 case DW_OP_lit26:
142 case DW_OP_lit27:
143 case DW_OP_lit28:
144 case DW_OP_lit29:
145 case DW_OP_lit30:
146 case DW_OP_lit31:
147 ++stack_depth;
148 break;
150 case DW_OP_addr:
151 op_ptr += addr_size;
152 ++stack_depth;
153 break;
155 case DW_OP_const1u:
156 case DW_OP_const1s:
157 op_ptr += 1;
158 ++stack_depth;
159 break;
160 case DW_OP_const2u:
161 case DW_OP_const2s:
162 op_ptr += 2;
163 ++stack_depth;
164 break;
165 case DW_OP_const4u:
166 case DW_OP_const4s:
167 op_ptr += 4;
168 ++stack_depth;
169 break;
170 case DW_OP_const8u:
171 case DW_OP_const8s:
172 op_ptr += 8;
173 ++stack_depth;
174 break;
175 case DW_OP_constu:
176 case DW_OP_consts:
177 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
178 ++stack_depth;
179 break;
181 case DW_OP_reg0:
182 case DW_OP_reg1:
183 case DW_OP_reg2:
184 case DW_OP_reg3:
185 case DW_OP_reg4:
186 case DW_OP_reg5:
187 case DW_OP_reg6:
188 case DW_OP_reg7:
189 case DW_OP_reg8:
190 case DW_OP_reg9:
191 case DW_OP_reg10:
192 case DW_OP_reg11:
193 case DW_OP_reg12:
194 case DW_OP_reg13:
195 case DW_OP_reg14:
196 case DW_OP_reg15:
197 case DW_OP_reg16:
198 case DW_OP_reg17:
199 case DW_OP_reg18:
200 case DW_OP_reg19:
201 case DW_OP_reg20:
202 case DW_OP_reg21:
203 case DW_OP_reg22:
204 case DW_OP_reg23:
205 case DW_OP_reg24:
206 case DW_OP_reg25:
207 case DW_OP_reg26:
208 case DW_OP_reg27:
209 case DW_OP_reg28:
210 case DW_OP_reg29:
211 case DW_OP_reg30:
212 case DW_OP_reg31:
213 ++stack_depth;
214 break;
216 case DW_OP_regx:
217 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
218 ++stack_depth;
219 break;
221 case DW_OP_breg0:
222 case DW_OP_breg1:
223 case DW_OP_breg2:
224 case DW_OP_breg3:
225 case DW_OP_breg4:
226 case DW_OP_breg5:
227 case DW_OP_breg6:
228 case DW_OP_breg7:
229 case DW_OP_breg8:
230 case DW_OP_breg9:
231 case DW_OP_breg10:
232 case DW_OP_breg11:
233 case DW_OP_breg12:
234 case DW_OP_breg13:
235 case DW_OP_breg14:
236 case DW_OP_breg15:
237 case DW_OP_breg16:
238 case DW_OP_breg17:
239 case DW_OP_breg18:
240 case DW_OP_breg19:
241 case DW_OP_breg20:
242 case DW_OP_breg21:
243 case DW_OP_breg22:
244 case DW_OP_breg23:
245 case DW_OP_breg24:
246 case DW_OP_breg25:
247 case DW_OP_breg26:
248 case DW_OP_breg27:
249 case DW_OP_breg28:
250 case DW_OP_breg29:
251 case DW_OP_breg30:
252 case DW_OP_breg31:
253 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
254 ++stack_depth;
255 break;
256 case DW_OP_bregx:
258 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
259 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
260 ++stack_depth;
262 break;
263 case DW_OP_fbreg:
264 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
265 ++stack_depth;
266 break;
268 case DW_OP_dup:
269 ++stack_depth;
270 break;
272 case DW_OP_drop:
273 --stack_depth;
274 break;
276 case DW_OP_pick:
277 ++op_ptr;
278 ++stack_depth;
279 break;
281 case DW_OP_rot:
282 case DW_OP_swap:
283 *need_tempvar = 1;
284 break;
286 case DW_OP_over:
287 ++stack_depth;
288 break;
290 case DW_OP_abs:
291 case DW_OP_neg:
292 case DW_OP_not:
293 case DW_OP_deref:
294 break;
296 case DW_OP_deref_size:
297 ++op_ptr;
298 break;
300 case DW_OP_plus_uconst:
301 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
302 break;
304 case DW_OP_div:
305 case DW_OP_shra:
306 case DW_OP_and:
307 case DW_OP_minus:
308 case DW_OP_mod:
309 case DW_OP_mul:
310 case DW_OP_or:
311 case DW_OP_plus:
312 case DW_OP_shl:
313 case DW_OP_shr:
314 case DW_OP_xor:
315 case DW_OP_le:
316 case DW_OP_ge:
317 case DW_OP_eq:
318 case DW_OP_lt:
319 case DW_OP_gt:
320 case DW_OP_ne:
321 --stack_depth;
322 break;
324 case DW_OP_call_frame_cfa:
325 ++stack_depth;
326 break;
328 case DW_OP_GNU_push_tls_address:
329 case DW_OP_form_tls_address:
330 (*info)[ndx].is_tls = 1;
331 break;
333 case DW_OP_skip:
334 offset = extract_signed_integer (op_ptr, 2, byte_order);
335 op_ptr += 2;
336 offset = op_ptr + offset - base;
337 /* If the destination has not been seen yet, add it to the
338 to-do list. */
339 if (!(*info)[offset].visited)
340 to_do->push_back (offset);
341 SET_CHECK_DEPTH (offset);
342 (*info)[offset].label = 1;
343 /* We're done with this line of code. */
344 return;
346 case DW_OP_bra:
347 offset = extract_signed_integer (op_ptr, 2, byte_order);
348 op_ptr += 2;
349 offset = op_ptr + offset - base;
350 --stack_depth;
351 /* If the destination has not been seen yet, add it to the
352 to-do list. */
353 if (!(*info)[offset].visited)
354 to_do->push_back (offset);
355 SET_CHECK_DEPTH (offset);
356 (*info)[offset].label = 1;
357 break;
359 case DW_OP_nop:
360 break;
362 default:
363 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
367 gdb_assert (op_ptr == op_end);
369 #undef SET_CHECK_DEPTH
372 /* Compute the maximum needed stack depth of a DWARF expression, and
373 some other information as well.
375 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
376 NEED_TEMPVAR is an out parameter which is set if this expression
377 needs a special temporary variable to be emitted (see the code
378 generator).
379 IS_TLS is an out parameter which is set if this expression refers
380 to a TLS variable.
381 OP_PTR and OP_END are the bounds of the DWARF expression.
382 INITIAL_DEPTH is the initial depth of the DWARF expression stack.
383 INFO is an array of insn_info objects, indexed by offset from the
384 start of the DWARF expression.
386 This returns the maximum stack depth. */
388 static int
389 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
390 int *need_tempvar, int *is_tls,
391 const gdb_byte *op_ptr, const gdb_byte *op_end,
392 int initial_depth,
393 std::vector<struct insn_info> *info)
395 std::vector<int> to_do;
396 int stack_depth, i;
398 info->resize (op_end - op_ptr);
400 to_do.push_back (0);
401 (*info)[0].depth = initial_depth;
402 (*info)[0].visited = 1;
404 while (!to_do.empty ())
406 int ndx = to_do.back ();
407 to_do.pop_back ();
409 compute_stack_depth_worker (ndx, need_tempvar, info, &to_do,
410 byte_order, addr_size,
411 op_ptr, op_end);
414 stack_depth = 0;
415 *is_tls = 0;
416 for (i = 0; i < op_end - op_ptr; ++i)
418 if ((*info)[i].depth > stack_depth)
419 stack_depth = (*info)[i].depth;
420 if ((*info)[i].is_tls)
421 *is_tls = 1;
424 return stack_depth + 1;
429 #define GCC_UINTPTR "__gdb_uintptr"
430 #define GCC_INTPTR "__gdb_intptr"
432 /* Emit code to push a constant. */
434 static void
435 push (int indent, string_file *stream, ULONGEST l)
437 gdb_printf (stream,
438 "%*s__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
439 indent, "", hex_string (l));
442 /* Emit code to push an arbitrary expression. This works like
443 printf. */
445 static void pushf (int indent, string_file *stream, const char *format, ...)
446 ATTRIBUTE_PRINTF (3, 4);
448 static void
449 pushf (int indent, string_file *stream, const char *format, ...)
451 va_list args;
453 gdb_printf (stream, "%*s__gdb_stack[__gdb_tos + 1] = ", indent, "");
454 va_start (args, format);
455 stream->vprintf (format, args);
456 va_end (args);
457 stream->puts (";\n");
459 gdb_printf (stream, "%*s++__gdb_tos;\n", indent, "");
462 /* Emit code for a unary expression -- one which operates in-place on
463 the top-of-stack. This works like printf. */
465 static void unary (int indent, string_file *stream, const char *format, ...)
466 ATTRIBUTE_PRINTF (3, 4);
468 static void
469 unary (int indent, string_file *stream, const char *format, ...)
471 va_list args;
473 gdb_printf (stream, "%*s__gdb_stack[__gdb_tos] = ", indent, "");
474 va_start (args, format);
475 stream->vprintf (format, args);
476 va_end (args);
477 stream->puts (";\n");
480 /* Emit code for a unary expression -- one which uses the top two
481 stack items, popping the topmost one. This works like printf. */
482 static void binary (int indent, string_file *stream, const char *format, ...)
483 ATTRIBUTE_PRINTF (3, 4);
485 static void
486 binary (int indent, string_file *stream, const char *format, ...)
488 va_list args;
490 gdb_printf (stream, "%*s__gdb_stack[__gdb_tos - 1] = ", indent, "");
491 va_start (args, format);
492 stream->vprintf (format, args);
493 va_end (args);
494 stream->puts (";\n");
495 gdb_printf (stream, "%*s--__gdb_tos;\n", indent, "");
498 /* Print the name of a label given its "SCOPE", an arbitrary integer
499 used for uniqueness, and its TARGET, the bytecode offset
500 corresponding to the label's point of definition. */
502 static void
503 print_label (string_file *stream, unsigned int scope, int target)
505 stream->printf ("__label_%u_%s", scope, pulongest (target));
508 /* Note that a register was used. */
510 static void
511 note_register (int regnum, std::vector<bool> &registers_used)
513 gdb_assert (regnum >= 0);
514 /* If the expression uses a cooked register, then we currently can't
515 compile it. We would need a gdbarch method to handle this
516 situation. */
517 if (regnum >= registers_used.size ())
518 error (_("Expression uses \"cooked\" register and cannot be compiled."));
519 registers_used[regnum] = true;
522 /* Emit code that pushes a register's address on the stack.
523 REGISTERS_USED is an out parameter which is updated to note which
524 register was needed by this expression. */
526 static void
527 pushf_register_address (int indent, string_file *stream,
528 std::vector<bool> &registers_used,
529 struct gdbarch *gdbarch, int regnum)
531 std::string regname = compile_register_name_mangled (gdbarch, regnum);
533 note_register (regnum, registers_used);
534 pushf (indent, stream,
535 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
536 regname.c_str ());
539 /* Emit code that pushes a register's value on the stack.
540 REGISTERS_USED is an out parameter which is updated to note which
541 register was needed by this expression. OFFSET is added to the
542 register's value before it is pushed. */
544 static void
545 pushf_register (int indent, string_file *stream,
546 std::vector<bool> &registers_used,
547 struct gdbarch *gdbarch, int regnum, uint64_t offset)
549 std::string regname = compile_register_name_mangled (gdbarch, regnum);
551 note_register (regnum, registers_used);
552 if (offset == 0)
553 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
554 regname.c_str ());
555 else
556 pushf (indent, stream,
557 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
558 regname.c_str (), hex_string (offset));
561 /* Compile a DWARF expression to C code.
563 INDENT is the indentation level to use.
564 STREAM is the stream where the code should be written.
566 TYPE_NAME names the type of the result of the DWARF expression.
567 For locations this is "void *" but for array bounds it will be an
568 integer type.
570 RESULT_NAME is the name of a variable in the resulting C code. The
571 result of the expression will be assigned to this variable.
573 SYM is the symbol corresponding to this expression.
574 PC is the location at which the expression is being evaluated.
575 ARCH is the architecture to use.
577 REGISTERS_USED is an out parameter which is updated to note which
578 registers were needed by this expression.
580 ADDR_SIZE is the DWARF address size to use.
582 OPT_PTR and OP_END are the bounds of the DWARF expression.
584 If non-NULL, INITIAL points to an initial value to write to the
585 stack. If NULL, no initial value is written.
587 PER_CU is the per-CU object used for looking up various other
588 things. */
590 static void
591 do_compile_dwarf_expr_to_c (int indent, string_file *stream,
592 const char *type_name,
593 const char *result_name,
594 struct symbol *sym, CORE_ADDR pc,
595 struct gdbarch *arch,
596 std::vector<bool> &registers_used,
597 unsigned int addr_size,
598 const gdb_byte *op_ptr, const gdb_byte *op_end,
599 CORE_ADDR *initial,
600 dwarf2_per_cu_data *per_cu,
601 dwarf2_per_objfile *per_objfile)
603 /* We keep a counter so that labels and other objects we create have
604 unique names. */
605 static unsigned int scope;
607 enum bfd_endian byte_order = gdbarch_byte_order (arch);
608 const gdb_byte * const base = op_ptr;
609 int need_tempvar = 0;
610 int is_tls = 0;
611 std::vector<struct insn_info> info;
612 int stack_depth;
614 ++scope;
616 gdb_printf (stream, "%*s__attribute__ ((unused)) %s %s;\n",
617 indent, "", type_name, result_name);
618 gdb_printf (stream, "%*s{\n", indent, "");
619 indent += 2;
621 stack_depth = compute_stack_depth (byte_order, addr_size,
622 &need_tempvar, &is_tls,
623 op_ptr, op_end, initial != NULL,
624 &info);
626 /* This is a hack until we can add a feature to glibc to let us
627 properly generate code for TLS. You might think we could emit
628 the address in the ordinary course of translating
629 DW_OP_GNU_push_tls_address, but since the operand appears on the
630 stack, it is relatively hard to find, and the idea of calling
631 target_translate_tls_address with OFFSET==0 and then adding the
632 offset by hand seemed too hackish. */
633 if (is_tls)
635 frame_info_ptr frame = get_selected_frame (NULL);
636 struct value *val;
638 if (frame == NULL)
639 error (_("Symbol \"%s\" cannot be used because "
640 "there is no selected frame"),
641 sym->print_name ());
643 val = read_var_value (sym, NULL, frame);
644 if (val->lval () != lval_memory)
645 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
646 "as its address has not been found."),
647 sym->print_name ());
649 warning (_("Symbol \"%s\" is thread-local and currently can only "
650 "be referenced from the current thread in "
651 "compiled code."),
652 sym->print_name ());
654 gdb_printf (stream, "%*s%s = %s;\n",
655 indent, "", result_name,
656 core_addr_to_string (val->address ()));
657 gdb_printf (stream, "%*s}\n", indent - 2, "");
658 return;
661 gdb_printf (stream, "%*s" GCC_UINTPTR " __gdb_stack[%d];\n",
662 indent, "", stack_depth);
664 if (need_tempvar)
665 gdb_printf (stream, "%*s" GCC_UINTPTR " __gdb_tmp;\n", indent, "");
666 gdb_printf (stream, "%*sint __gdb_tos = -1;\n", indent, "");
668 if (initial != NULL)
669 pushf (indent, stream, "%s", core_addr_to_string (*initial));
671 while (op_ptr < op_end)
673 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
674 uint64_t uoffset, reg;
675 int64_t offset;
677 stream->printf ("%*s", indent - 2, "");
678 if (info[op_ptr - base].label)
680 print_label (stream, scope, op_ptr - base);
681 stream->puts (":;");
683 stream->printf ("/* %s */\n", get_DW_OP_name (op));
685 /* This is handy for debugging the generated code:
686 gdb_printf (stream, "if (__gdb_tos != %d) abort ();\n",
687 (int) info[op_ptr - base].depth - 1);
690 ++op_ptr;
692 switch (op)
694 case DW_OP_lit0:
695 case DW_OP_lit1:
696 case DW_OP_lit2:
697 case DW_OP_lit3:
698 case DW_OP_lit4:
699 case DW_OP_lit5:
700 case DW_OP_lit6:
701 case DW_OP_lit7:
702 case DW_OP_lit8:
703 case DW_OP_lit9:
704 case DW_OP_lit10:
705 case DW_OP_lit11:
706 case DW_OP_lit12:
707 case DW_OP_lit13:
708 case DW_OP_lit14:
709 case DW_OP_lit15:
710 case DW_OP_lit16:
711 case DW_OP_lit17:
712 case DW_OP_lit18:
713 case DW_OP_lit19:
714 case DW_OP_lit20:
715 case DW_OP_lit21:
716 case DW_OP_lit22:
717 case DW_OP_lit23:
718 case DW_OP_lit24:
719 case DW_OP_lit25:
720 case DW_OP_lit26:
721 case DW_OP_lit27:
722 case DW_OP_lit28:
723 case DW_OP_lit29:
724 case DW_OP_lit30:
725 case DW_OP_lit31:
726 push (indent, stream, op - DW_OP_lit0);
727 break;
729 case DW_OP_addr:
730 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
731 op_ptr += addr_size;
732 /* Some versions of GCC emit DW_OP_addr before
733 DW_OP_GNU_push_tls_address. In this case the value is an
734 index, not an address. We don't support things like
735 branching between the address and the TLS op. */
736 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
737 uoffset += per_objfile->objfile->text_section_offset ();
738 push (indent, stream, uoffset);
739 break;
741 case DW_OP_const1u:
742 push (indent, stream,
743 extract_unsigned_integer (op_ptr, 1, byte_order));
744 op_ptr += 1;
745 break;
746 case DW_OP_const1s:
747 push (indent, stream,
748 extract_signed_integer (op_ptr, 1, byte_order));
749 op_ptr += 1;
750 break;
751 case DW_OP_const2u:
752 push (indent, stream,
753 extract_unsigned_integer (op_ptr, 2, byte_order));
754 op_ptr += 2;
755 break;
756 case DW_OP_const2s:
757 push (indent, stream,
758 extract_signed_integer (op_ptr, 2, byte_order));
759 op_ptr += 2;
760 break;
761 case DW_OP_const4u:
762 push (indent, stream,
763 extract_unsigned_integer (op_ptr, 4, byte_order));
764 op_ptr += 4;
765 break;
766 case DW_OP_const4s:
767 push (indent, stream,
768 extract_signed_integer (op_ptr, 4, byte_order));
769 op_ptr += 4;
770 break;
771 case DW_OP_const8u:
772 push (indent, stream,
773 extract_unsigned_integer (op_ptr, 8, byte_order));
774 op_ptr += 8;
775 break;
776 case DW_OP_const8s:
777 push (indent, stream,
778 extract_signed_integer (op_ptr, 8, byte_order));
779 op_ptr += 8;
780 break;
781 case DW_OP_constu:
782 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
783 push (indent, stream, uoffset);
784 break;
785 case DW_OP_consts:
786 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
787 push (indent, stream, offset);
788 break;
790 case DW_OP_reg0:
791 case DW_OP_reg1:
792 case DW_OP_reg2:
793 case DW_OP_reg3:
794 case DW_OP_reg4:
795 case DW_OP_reg5:
796 case DW_OP_reg6:
797 case DW_OP_reg7:
798 case DW_OP_reg8:
799 case DW_OP_reg9:
800 case DW_OP_reg10:
801 case DW_OP_reg11:
802 case DW_OP_reg12:
803 case DW_OP_reg13:
804 case DW_OP_reg14:
805 case DW_OP_reg15:
806 case DW_OP_reg16:
807 case DW_OP_reg17:
808 case DW_OP_reg18:
809 case DW_OP_reg19:
810 case DW_OP_reg20:
811 case DW_OP_reg21:
812 case DW_OP_reg22:
813 case DW_OP_reg23:
814 case DW_OP_reg24:
815 case DW_OP_reg25:
816 case DW_OP_reg26:
817 case DW_OP_reg27:
818 case DW_OP_reg28:
819 case DW_OP_reg29:
820 case DW_OP_reg30:
821 case DW_OP_reg31:
822 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
823 pushf_register_address (indent, stream, registers_used, arch,
824 dwarf_reg_to_regnum_or_error
825 (arch, op - DW_OP_reg0));
826 break;
828 case DW_OP_regx:
829 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
830 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
831 pushf_register_address (indent, stream, registers_used, arch,
832 dwarf_reg_to_regnum_or_error (arch, reg));
833 break;
835 case DW_OP_breg0:
836 case DW_OP_breg1:
837 case DW_OP_breg2:
838 case DW_OP_breg3:
839 case DW_OP_breg4:
840 case DW_OP_breg5:
841 case DW_OP_breg6:
842 case DW_OP_breg7:
843 case DW_OP_breg8:
844 case DW_OP_breg9:
845 case DW_OP_breg10:
846 case DW_OP_breg11:
847 case DW_OP_breg12:
848 case DW_OP_breg13:
849 case DW_OP_breg14:
850 case DW_OP_breg15:
851 case DW_OP_breg16:
852 case DW_OP_breg17:
853 case DW_OP_breg18:
854 case DW_OP_breg19:
855 case DW_OP_breg20:
856 case DW_OP_breg21:
857 case DW_OP_breg22:
858 case DW_OP_breg23:
859 case DW_OP_breg24:
860 case DW_OP_breg25:
861 case DW_OP_breg26:
862 case DW_OP_breg27:
863 case DW_OP_breg28:
864 case DW_OP_breg29:
865 case DW_OP_breg30:
866 case DW_OP_breg31:
867 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
868 pushf_register (indent, stream, registers_used, arch,
869 dwarf_reg_to_regnum_or_error (arch,
870 op - DW_OP_breg0),
871 offset);
872 break;
873 case DW_OP_bregx:
875 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
876 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
877 pushf_register (indent, stream, registers_used, arch,
878 dwarf_reg_to_regnum_or_error (arch, reg), offset);
880 break;
881 case DW_OP_fbreg:
883 const gdb_byte *datastart;
884 size_t datalen;
885 const struct block *b;
886 struct symbol *framefunc;
887 char fb_name[50];
889 b = block_for_pc (pc);
891 if (!b)
892 error (_("No block found for address"));
894 framefunc = b->linkage_function ();
896 if (!framefunc)
897 error (_("No function found for block"));
899 func_get_frame_base_dwarf_block (framefunc, pc,
900 &datastart, &datalen);
902 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
904 /* Generate a unique-enough name, in case the frame base
905 is computed multiple times in this expression. */
906 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
907 (long) (op_ptr - base));
909 do_compile_dwarf_expr_to_c (indent, stream,
910 GCC_UINTPTR, fb_name,
911 sym, pc,
912 arch, registers_used, addr_size,
913 datastart, datastart + datalen,
914 NULL, per_cu, per_objfile);
916 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
918 break;
920 case DW_OP_dup:
921 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
922 break;
924 case DW_OP_drop:
925 gdb_printf (stream, "%*s--__gdb_tos;\n", indent, "");
926 break;
928 case DW_OP_pick:
929 offset = *op_ptr++;
930 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
931 plongest (offset));
932 break;
934 case DW_OP_swap:
935 gdb_printf (stream,
936 "%*s__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n",
937 indent, "");
938 gdb_printf (stream,
939 "%*s__gdb_stack[__gdb_tos - 1] = "
940 "__gdb_stack[__gdb_tos];\n",
941 indent, "");
942 gdb_printf (stream, ("%*s__gdb_stack[__gdb_tos] = "
943 "__gdb_tmp;\n"),
944 indent, "");
945 break;
947 case DW_OP_over:
948 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
949 break;
951 case DW_OP_rot:
952 gdb_printf (stream, ("%*s__gdb_tmp = "
953 "__gdb_stack[__gdb_tos];\n"),
954 indent, "");
955 gdb_printf (stream,
956 "%*s__gdb_stack[__gdb_tos] = "
957 "__gdb_stack[__gdb_tos - 1];\n",
958 indent, "");
959 gdb_printf (stream,
960 "%*s__gdb_stack[__gdb_tos - 1] = "
961 "__gdb_stack[__gdb_tos -2];\n",
962 indent, "");
963 gdb_printf (stream, "%*s__gdb_stack[__gdb_tos - 2] = "
964 "__gdb_tmp;\n",
965 indent, "");
966 break;
968 case DW_OP_deref:
969 case DW_OP_deref_size:
971 int size;
972 const char *mode;
974 if (op == DW_OP_deref_size)
975 size = *op_ptr++;
976 else
977 size = addr_size;
979 mode = c_get_mode_for_size (size);
980 if (mode == NULL)
981 error (_("Unsupported size %d in %s"),
982 size, get_DW_OP_name (op));
984 /* Cast to a pointer of the desired type, then
985 dereference. */
986 gdb_printf (stream,
987 "%*s__gdb_stack[__gdb_tos] = "
988 "*((__gdb_int_%s *) "
989 "__gdb_stack[__gdb_tos]);\n",
990 indent, "", mode);
992 break;
994 case DW_OP_abs:
995 unary (indent, stream,
996 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
997 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
998 break;
1000 case DW_OP_neg:
1001 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
1002 break;
1004 case DW_OP_not:
1005 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
1006 break;
1008 case DW_OP_plus_uconst:
1009 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1010 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1011 hex_string (reg));
1012 break;
1014 case DW_OP_div:
1015 binary (indent, stream, ("((" GCC_INTPTR
1016 ") __gdb_stack[__gdb_tos-1]) / (("
1017 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1018 break;
1020 case DW_OP_shra:
1021 binary (indent, stream,
1022 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1023 "__gdb_stack[__gdb_tos]");
1024 break;
1026 #define BINARY(OP) \
1027 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1028 " __gdb_stack[__gdb_tos]"); \
1029 break
1031 case DW_OP_and:
1032 BINARY (&);
1033 case DW_OP_minus:
1034 BINARY (-);
1035 case DW_OP_mod:
1036 BINARY (%);
1037 case DW_OP_mul:
1038 BINARY (*);
1039 case DW_OP_or:
1040 BINARY (|);
1041 case DW_OP_plus:
1042 BINARY (+);
1043 case DW_OP_shl:
1044 BINARY (<<);
1045 case DW_OP_shr:
1046 BINARY (>>);
1047 case DW_OP_xor:
1048 BINARY (^);
1049 #undef BINARY
1051 #define COMPARE(OP) \
1052 binary (indent, stream, \
1053 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1054 " ((" GCC_INTPTR \
1055 ") __gdb_stack[__gdb_tos]))"); \
1056 break
1058 case DW_OP_le:
1059 COMPARE (<=);
1060 case DW_OP_ge:
1061 COMPARE (>=);
1062 case DW_OP_eq:
1063 COMPARE (==);
1064 case DW_OP_lt:
1065 COMPARE (<);
1066 case DW_OP_gt:
1067 COMPARE (>);
1068 case DW_OP_ne:
1069 COMPARE (!=);
1070 #undef COMPARE
1072 case DW_OP_call_frame_cfa:
1074 int regnum;
1075 CORE_ADDR text_offset;
1076 LONGEST off;
1077 const gdb_byte *cfa_start, *cfa_end;
1079 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1080 &regnum, &off,
1081 &text_offset, &cfa_start, &cfa_end))
1083 /* Register. */
1084 pushf_register (indent, stream, registers_used, arch, regnum,
1085 off);
1087 else
1089 /* Another expression. */
1090 char cfa_name[50];
1092 /* Generate a unique-enough name, in case the CFA is
1093 computed multiple times in this expression. */
1094 xsnprintf (cfa_name, sizeof (cfa_name),
1095 "__cfa_%ld", (long) (op_ptr - base));
1097 do_compile_dwarf_expr_to_c (indent, stream,
1098 GCC_UINTPTR, cfa_name,
1099 sym, pc, arch, registers_used,
1100 addr_size,
1101 cfa_start, cfa_end,
1102 &text_offset, per_cu, per_objfile);
1103 pushf (indent, stream, "%s", cfa_name);
1107 break;
1109 case DW_OP_skip:
1110 offset = extract_signed_integer (op_ptr, 2, byte_order);
1111 op_ptr += 2;
1112 gdb_printf (stream, "%*sgoto ", indent, "");
1113 print_label (stream, scope, op_ptr + offset - base);
1114 stream->puts (";\n");
1115 break;
1117 case DW_OP_bra:
1118 offset = extract_signed_integer (op_ptr, 2, byte_order);
1119 op_ptr += 2;
1120 gdb_printf (stream,
1121 "%*sif ((( " GCC_INTPTR
1122 ") __gdb_stack[__gdb_tos--]) != 0) goto ",
1123 indent, "");
1124 print_label (stream, scope, op_ptr + offset - base);
1125 stream->puts (";\n");
1126 break;
1128 case DW_OP_nop:
1129 break;
1131 default:
1132 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1136 gdb_printf (stream, "%*s%s = __gdb_stack[__gdb_tos];\n",
1137 indent, "", result_name);
1138 gdb_printf (stream, "%*s}\n", indent - 2, "");
1141 /* See compile.h. */
1143 void
1144 compile_dwarf_expr_to_c (string_file *stream, const char *result_name,
1145 struct symbol *sym, CORE_ADDR pc,
1146 struct gdbarch *arch,
1147 std::vector<bool> &registers_used,
1148 unsigned int addr_size,
1149 const gdb_byte *op_ptr, const gdb_byte *op_end,
1150 dwarf2_per_cu_data *per_cu,
1151 dwarf2_per_objfile *per_objfile)
1153 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1154 arch, registers_used, addr_size, op_ptr, op_end,
1155 NULL, per_cu, per_objfile);
1158 /* See compile.h. */
1160 void
1161 compile_dwarf_bounds_to_c (string_file *stream,
1162 const char *result_name,
1163 const struct dynamic_prop *prop,
1164 struct symbol *sym, CORE_ADDR pc,
1165 struct gdbarch *arch,
1166 std::vector<bool> &registers_used,
1167 unsigned int addr_size,
1168 const gdb_byte *op_ptr, const gdb_byte *op_end,
1169 dwarf2_per_cu_data *per_cu,
1170 dwarf2_per_objfile *per_objfile)
1172 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1173 sym, pc, arch, registers_used,
1174 addr_size, op_ptr, op_end, NULL, per_cu,
1175 per_objfile);