Make dwarf_expr_context::stack_empty_p return a bool
[deliverable/binutils-gdb.git] / gdb / dwarf2expr.c
CommitLineData
852483bc
MK
1/* DWARF 2 Expression Evaluator.
2
61baf725 3 Copyright (C) 2001-2017 Free Software Foundation, Inc.
852483bc 4
4c2df51b
DJ
5 Contributed by Daniel Berlin (dan@dberlin.org)
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
4c2df51b
DJ
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4c2df51b
DJ
21
22#include "defs.h"
23#include "symtab.h"
24#include "gdbtypes.h"
25#include "value.h"
26#include "gdbcore.h"
fa8f86ff 27#include "dwarf2.h"
4c2df51b 28#include "dwarf2expr.h"
0fde2c53 29#include "dwarf2loc.h"
9c541725 30#include "common/underlying.h"
4c2df51b 31
8a9b8146
TT
32/* Cookie for gdbarch data. */
33
34static struct gdbarch_data *dwarf_arch_cookie;
35
36/* This holds gdbarch-specific types used by the DWARF expression
37 evaluator. See comments in execute_stack_op. */
38
39struct dwarf_gdbarch_types
40{
41 struct type *dw_types[3];
42};
43
44/* Allocate and fill in dwarf_gdbarch_types for an arch. */
45
46static void *
47dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
48{
49 struct dwarf_gdbarch_types *types
50 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
51
52 /* The types themselves are lazily initialized. */
53
54 return types;
55}
56
57/* Return the type used for DWARF operations where the type is
58 unspecified in the DWARF spec. Only certain sizes are
59 supported. */
60
595d2e30
TT
61struct type *
62dwarf_expr_context::address_type () const
8a9b8146 63{
9a3c8263 64 struct dwarf_gdbarch_types *types
595d2e30 65 = (struct dwarf_gdbarch_types *) gdbarch_data (this->gdbarch,
9a3c8263 66 dwarf_arch_cookie);
8a9b8146
TT
67 int ndx;
68
595d2e30 69 if (this->addr_size == 2)
8a9b8146 70 ndx = 0;
595d2e30 71 else if (this->addr_size == 4)
8a9b8146 72 ndx = 1;
595d2e30 73 else if (this->addr_size == 8)
8a9b8146
TT
74 ndx = 2;
75 else
76 error (_("Unsupported address size in DWARF expressions: %d bits"),
595d2e30 77 8 * this->addr_size);
8a9b8146
TT
78
79 if (types->dw_types[ndx] == NULL)
80 types->dw_types[ndx]
595d2e30
TT
81 = arch_integer_type (this->gdbarch,
82 8 * this->addr_size,
8a9b8146
TT
83 0, "<signed DWARF address type>");
84
85 return types->dw_types[ndx];
86}
87
4c2df51b
DJ
88/* Create a new context for the expression evaluator. */
89
718b9626
TT
90dwarf_expr_context::dwarf_expr_context ()
91: stack (NULL),
92 stack_len (0),
93 stack_allocated (10),
94 gdbarch (NULL),
95 addr_size (0),
96 ref_addr_size (0),
97 offset (0),
98 recursion_depth (0),
99 max_recursion_depth (0x100),
100 location (DWARF_VALUE_MEMORY),
101 len (0),
102 data (NULL),
1e467161 103 initialized (0)
4c2df51b 104{
718b9626 105 this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
4c2df51b
DJ
106}
107
718b9626 108/* Clean up a dwarf_expr_context. */
4c2df51b 109
718b9626 110dwarf_expr_context::~dwarf_expr_context ()
4c2df51b 111{
718b9626 112 xfree (this->stack);
4a227398
TT
113}
114
595d2e30 115/* Expand the memory allocated stack to contain at least
4c2df51b
DJ
116 NEED more elements than are currently used. */
117
595d2e30
TT
118void
119dwarf_expr_context::grow_stack (size_t need)
4c2df51b 120{
595d2e30 121 if (this->stack_len + need > this->stack_allocated)
4c2df51b 122 {
595d2e30 123 size_t newlen = this->stack_len + need + 10;
9a619af0 124
595d2e30
TT
125 this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen);
126 this->stack_allocated = newlen;
4c2df51b
DJ
127 }
128}
129
595d2e30 130/* Push VALUE onto the stack. */
4c2df51b 131
595d2e30 132void
69009882 133dwarf_expr_context::push (struct value *value, bool in_stack_memory)
4c2df51b 134{
44353522
DE
135 struct dwarf_stack_value *v;
136
595d2e30
TT
137 grow_stack (1);
138 v = &this->stack[this->stack_len++];
44353522
DE
139 v->value = value;
140 v->in_stack_memory = in_stack_memory;
4c2df51b
DJ
141}
142
595d2e30 143/* Push VALUE onto the stack. */
4c2df51b
DJ
144
145void
69009882 146dwarf_expr_context::push_address (CORE_ADDR value, bool in_stack_memory)
8a9b8146 147{
595d2e30 148 push (value_from_ulongest (address_type (), value), in_stack_memory);
8a9b8146
TT
149}
150
595d2e30 151/* Pop the top item off of the stack. */
8a9b8146 152
595d2e30
TT
153void
154dwarf_expr_context::pop ()
4c2df51b 155{
595d2e30 156 if (this->stack_len <= 0)
8a3fe4f8 157 error (_("dwarf expression stack underflow"));
595d2e30 158 this->stack_len--;
4c2df51b
DJ
159}
160
595d2e30 161/* Retrieve the N'th item on the stack. */
4c2df51b 162
8a9b8146 163struct value *
595d2e30 164dwarf_expr_context::fetch (int n)
4c2df51b 165{
595d2e30 166 if (this->stack_len <= n)
3e43a32a
MS
167 error (_("Asked for position %d of stack, "
168 "stack only has %d elements on it."),
595d2e30
TT
169 n, this->stack_len);
170 return this->stack[this->stack_len - (1 + n)].value;
8a9b8146
TT
171}
172
173/* Require that TYPE be an integral type; throw an exception if not. */
44353522 174
8a9b8146
TT
175static void
176dwarf_require_integral (struct type *type)
177{
178 if (TYPE_CODE (type) != TYPE_CODE_INT
179 && TYPE_CODE (type) != TYPE_CODE_CHAR
180 && TYPE_CODE (type) != TYPE_CODE_BOOL)
181 error (_("integral type expected in DWARF expression"));
182}
183
184/* Return the unsigned form of TYPE. TYPE is necessarily an integral
185 type. */
186
187static struct type *
188get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
189{
190 switch (TYPE_LENGTH (type))
191 {
192 case 1:
193 return builtin_type (gdbarch)->builtin_uint8;
194 case 2:
195 return builtin_type (gdbarch)->builtin_uint16;
196 case 4:
197 return builtin_type (gdbarch)->builtin_uint32;
198 case 8:
199 return builtin_type (gdbarch)->builtin_uint64;
200 default:
201 error (_("no unsigned variant found for type, while evaluating "
202 "DWARF expression"));
203 }
44353522
DE
204}
205
8ddd9a20
TT
206/* Return the signed form of TYPE. TYPE is necessarily an integral
207 type. */
208
209static struct type *
210get_signed_type (struct gdbarch *gdbarch, struct type *type)
211{
212 switch (TYPE_LENGTH (type))
213 {
214 case 1:
215 return builtin_type (gdbarch)->builtin_int8;
216 case 2:
217 return builtin_type (gdbarch)->builtin_int16;
218 case 4:
219 return builtin_type (gdbarch)->builtin_int32;
220 case 8:
221 return builtin_type (gdbarch)->builtin_int64;
222 default:
223 error (_("no signed variant found for type, while evaluating "
224 "DWARF expression"));
225 }
226}
227
595d2e30 228/* Retrieve the N'th item on the stack, converted to an address. */
f2c7657e
UW
229
230CORE_ADDR
595d2e30 231dwarf_expr_context::fetch_address (int n)
f2c7657e 232{
595d2e30
TT
233 struct value *result_val = fetch (n);
234 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
8a9b8146
TT
235 ULONGEST result;
236
237 dwarf_require_integral (value_type (result_val));
238 result = extract_unsigned_integer (value_contents (result_val),
239 TYPE_LENGTH (value_type (result_val)),
240 byte_order);
f2c7657e
UW
241
242 /* For most architectures, calling extract_unsigned_integer() alone
243 is sufficient for extracting an address. However, some
244 architectures (e.g. MIPS) use signed addresses and using
245 extract_unsigned_integer() will not produce a correct
246 result. Make sure we invoke gdbarch_integer_to_address()
247 for those architectures which require it. */
595d2e30 248 if (gdbarch_integer_to_address_p (this->gdbarch))
f2c7657e 249 {
595d2e30
TT
250 gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
251 struct type *int_type = get_unsigned_type (this->gdbarch,
8a9b8146 252 value_type (result_val));
f2c7657e 253
595d2e30
TT
254 store_unsigned_integer (buf, this->addr_size, byte_order, result);
255 return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
f2c7657e
UW
256 }
257
258 return (CORE_ADDR) result;
259}
260
595d2e30 261/* Retrieve the in_stack_memory flag of the N'th item on the stack. */
44353522 262
69009882 263bool
595d2e30 264dwarf_expr_context::fetch_in_stack_memory (int n)
44353522 265{
595d2e30 266 if (this->stack_len <= n)
3e43a32a
MS
267 error (_("Asked for position %d of stack, "
268 "stack only has %d elements on it."),
595d2e30
TT
269 n, this->stack_len);
270 return this->stack[this->stack_len - (1 + n)].in_stack_memory;
4c2df51b
DJ
271}
272
cb826367
TT
273/* Return true if the expression stack is empty. */
274
eccd80d6 275bool
595d2e30 276dwarf_expr_context::stack_empty_p () const
cb826367 277{
595d2e30 278 return this->stack_len == 0;
cb826367
TT
279}
280
595d2e30
TT
281/* Add a new piece to the dwarf_expr_context's piece list. */
282void
283dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
87808bd6 284{
1e467161
SM
285 this->pieces.emplace_back ();
286 dwarf_expr_piece &p = this->pieces.back ();
87808bd6 287
1e467161
SM
288 p.location = this->location;
289 p.size = size;
290 p.offset = offset;
87808bd6 291
1e467161 292 if (p.location == DWARF_VALUE_LITERAL)
cec03d70 293 {
1e467161
SM
294 p.v.literal.data = this->data;
295 p.v.literal.length = this->len;
cec03d70 296 }
595d2e30 297 else if (stack_empty_p ())
cb826367 298 {
1e467161 299 p.location = DWARF_VALUE_OPTIMIZED_OUT;
cb826367
TT
300 /* Also reset the context's location, for our callers. This is
301 a somewhat strange approach, but this lets us avoid setting
302 the location to DWARF_VALUE_MEMORY in all the individual
303 cases in the evaluator. */
595d2e30 304 this->location = DWARF_VALUE_OPTIMIZED_OUT;
cb826367 305 }
1e467161 306 else if (p.location == DWARF_VALUE_MEMORY)
f2c7657e 307 {
1e467161
SM
308 p.v.mem.addr = fetch_address (0);
309 p.v.mem.in_stack_memory = fetch_in_stack_memory (0);
f2c7657e 310 }
1e467161 311 else if (p.location == DWARF_VALUE_IMPLICIT_POINTER)
8cf6f0b1 312 {
1e467161
SM
313 p.v.ptr.die_sect_off = (sect_offset) this->len;
314 p.v.ptr.offset = value_as_long (fetch (0));
8cf6f0b1 315 }
1e467161
SM
316 else if (p.location == DWARF_VALUE_REGISTER)
317 p.v.regno = value_as_long (fetch (0));
cec03d70 318 else
44353522 319 {
1e467161 320 p.v.value = fetch (0);
44353522 321 }
87808bd6
JB
322}
323
595d2e30 324/* Evaluate the expression at ADDR (LEN bytes long). */
4c2df51b
DJ
325
326void
595d2e30 327dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
4c2df51b 328{
595d2e30 329 int old_recursion_depth = this->recursion_depth;
1e3a102a 330
595d2e30 331 execute_stack_op (addr, addr + len);
1e3a102a 332
595d2e30 333 /* RECURSION_DEPTH becomes invalid if an exception was thrown here. */
1e3a102a 334
595d2e30 335 gdb_assert (this->recursion_depth == old_recursion_depth);
4c2df51b
DJ
336}
337
f664829e 338/* Helper to read a uleb128 value or throw an error. */
4c2df51b 339
0d45f56e 340const gdb_byte *
f664829e 341safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
9fccedf7 342 uint64_t *r)
4c2df51b 343{
f664829e
DE
344 buf = gdb_read_uleb128 (buf, buf_end, r);
345 if (buf == NULL)
346 error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
4c2df51b
DJ
347 return buf;
348}
349
f664829e 350/* Helper to read a sleb128 value or throw an error. */
4c2df51b 351
0d45f56e 352const gdb_byte *
f664829e 353safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
9fccedf7 354 int64_t *r)
4c2df51b 355{
f664829e
DE
356 buf = gdb_read_sleb128 (buf, buf_end, r);
357 if (buf == NULL)
358 error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
359 return buf;
360}
4c2df51b 361
f664829e
DE
362const gdb_byte *
363safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
364{
365 buf = gdb_skip_leb128 (buf, buf_end);
366 if (buf == NULL)
367 error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
4c2df51b
DJ
368 return buf;
369}
4c2df51b 370\f
cec03d70
TT
371
372/* Check that the current operator is either at the end of an
f206f69c
AA
373 expression, or that it is followed by a composition operator or by
374 DW_OP_GNU_uninit (which should terminate the expression). */
cec03d70 375
3cf03773
TT
376void
377dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
378 const char *op_name)
cec03d70 379{
f206f69c
AA
380 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
381 && *op_ptr != DW_OP_GNU_uninit)
cec03d70 382 error (_("DWARF-2 expression error: `%s' operations must be "
64b9b334 383 "used either alone or in conjunction with DW_OP_piece "
cec03d70
TT
384 "or DW_OP_bit_piece."),
385 op_name);
386}
387
8a9b8146
TT
388/* Return true iff the types T1 and T2 are "the same". This only does
389 checks that might reasonably be needed to compare DWARF base
390 types. */
391
392static int
393base_types_equal_p (struct type *t1, struct type *t2)
394{
395 if (TYPE_CODE (t1) != TYPE_CODE (t2))
396 return 0;
397 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
398 return 0;
399 return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
400}
401
8e3b41a9
JK
402/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
403 DWARF register number. Otherwise return -1. */
404
405int
406dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
407{
9fccedf7 408 uint64_t dwarf_reg;
8e3b41a9
JK
409
410 if (buf_end <= buf)
411 return -1;
412 if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
413 {
414 if (buf_end - buf != 1)
415 return -1;
416 return *buf - DW_OP_reg0;
417 }
418
216f72a1 419 if (*buf == DW_OP_regval_type || *buf == DW_OP_GNU_regval_type)
8e3b41a9
JK
420 {
421 buf++;
f664829e
DE
422 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
423 if (buf == NULL)
424 return -1;
425 buf = gdb_skip_leb128 (buf, buf_end);
426 if (buf == NULL)
427 return -1;
8e3b41a9
JK
428 }
429 else if (*buf == DW_OP_regx)
430 {
431 buf++;
f664829e
DE
432 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
433 if (buf == NULL)
434 return -1;
8e3b41a9
JK
435 }
436 else
437 return -1;
438 if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
439 return -1;
440 return dwarf_reg;
441}
442
a471c594
JK
443/* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
444 DW_OP_deref* return the DWARF register number. Otherwise return -1.
445 DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
446 size from DW_OP_deref_size. */
447
448int
449dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
450 CORE_ADDR *deref_size_return)
451{
9fccedf7
DE
452 uint64_t dwarf_reg;
453 int64_t offset;
a471c594
JK
454
455 if (buf_end <= buf)
456 return -1;
f664829e 457
a471c594
JK
458 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
459 {
460 dwarf_reg = *buf - DW_OP_breg0;
461 buf++;
f664829e
DE
462 if (buf >= buf_end)
463 return -1;
a471c594
JK
464 }
465 else if (*buf == DW_OP_bregx)
466 {
467 buf++;
f664829e
DE
468 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
469 if (buf == NULL)
470 return -1;
a471c594
JK
471 if ((int) dwarf_reg != dwarf_reg)
472 return -1;
473 }
474 else
475 return -1;
476
f664829e
DE
477 buf = gdb_read_sleb128 (buf, buf_end, &offset);
478 if (buf == NULL)
a471c594 479 return -1;
f664829e 480 if (offset != 0)
a471c594
JK
481 return -1;
482
483 if (*buf == DW_OP_deref)
484 {
485 buf++;
486 *deref_size_return = -1;
487 }
488 else if (*buf == DW_OP_deref_size)
489 {
490 buf++;
491 if (buf >= buf_end)
492 return -1;
493 *deref_size_return = *buf++;
494 }
495 else
496 return -1;
497
498 if (buf != buf_end)
499 return -1;
500
501 return dwarf_reg;
502}
503
e18b2753
JK
504/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
505 in FB_OFFSET_RETURN with the X offset and return 1. Otherwise return 0. */
506
507int
508dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
509 CORE_ADDR *fb_offset_return)
510{
9fccedf7 511 int64_t fb_offset;
e18b2753
JK
512
513 if (buf_end <= buf)
514 return 0;
515
516 if (*buf != DW_OP_fbreg)
517 return 0;
518 buf++;
519
f664829e
DE
520 buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
521 if (buf == NULL)
522 return 0;
e18b2753
JK
523 *fb_offset_return = fb_offset;
524 if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
525 return 0;
526
527 return 1;
528}
529
530/* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
531 in SP_OFFSET_RETURN with the X offset and return 1. Otherwise return 0.
532 The matched SP register number depends on GDBARCH. */
533
534int
535dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
536 const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
537{
9fccedf7
DE
538 uint64_t dwarf_reg;
539 int64_t sp_offset;
e18b2753
JK
540
541 if (buf_end <= buf)
542 return 0;
543 if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
544 {
545 dwarf_reg = *buf - DW_OP_breg0;
546 buf++;
547 }
548 else
549 {
550 if (*buf != DW_OP_bregx)
551 return 0;
552 buf++;
f664829e
DE
553 buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
554 if (buf == NULL)
555 return 0;
e18b2753
JK
556 }
557
0fde2c53 558 if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
e18b2753
JK
559 != gdbarch_sp_regnum (gdbarch))
560 return 0;
561
f664829e
DE
562 buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
563 if (buf == NULL)
564 return 0;
e18b2753
JK
565 *sp_offset_return = sp_offset;
566 if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
567 return 0;
568
569 return 1;
570}
571
595d2e30
TT
572/* The engine for the expression evaluator. Using the context in this
573 object, evaluate the expression between OP_PTR and OP_END. */
4c2df51b 574
595d2e30
TT
575void
576dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
577 const gdb_byte *op_end)
4c2df51b 578{
595d2e30 579 enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
8a9b8146
TT
580 /* Old-style "untyped" DWARF values need special treatment in a
581 couple of places, specifically DW_OP_mod and DW_OP_shr. We need
582 a special type for these values so we can distinguish them from
583 values that have an explicit type, because explicitly-typed
584 values do not need special treatment. This special type must be
585 different (in the `==' sense) from any base type coming from the
586 CU. */
595d2e30 587 struct type *address_type = this->address_type ();
9a619af0 588
595d2e30
TT
589 this->location = DWARF_VALUE_MEMORY;
590 this->initialized = 1; /* Default is initialized. */
18ec9831 591
595d2e30 592 if (this->recursion_depth > this->max_recursion_depth)
1e3a102a 593 error (_("DWARF-2 expression error: Loop detected (%d)."),
595d2e30
TT
594 this->recursion_depth);
595 this->recursion_depth++;
1e3a102a 596
4c2df51b
DJ
597 while (op_ptr < op_end)
598 {
aead7601 599 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
f2c7657e 600 ULONGEST result;
44353522 601 /* Assume the value is not in stack memory.
69009882 602 Code that knows otherwise sets this to true.
44353522
DE
603 Some arithmetic on stack addresses can probably be assumed to still
604 be a stack address, but we skip this complication for now.
605 This is just an optimization, so it's always ok to punt
69009882
SM
606 and leave this as false. */
607 bool in_stack_memory = false;
9fccedf7
DE
608 uint64_t uoffset, reg;
609 int64_t offset;
8a9b8146 610 struct value *result_val = NULL;
4c2df51b 611
e0e9434c
TT
612 /* The DWARF expression might have a bug causing an infinite
613 loop. In that case, quitting is the only way out. */
614 QUIT;
615
4c2df51b
DJ
616 switch (op)
617 {
618 case DW_OP_lit0:
619 case DW_OP_lit1:
620 case DW_OP_lit2:
621 case DW_OP_lit3:
622 case DW_OP_lit4:
623 case DW_OP_lit5:
624 case DW_OP_lit6:
625 case DW_OP_lit7:
626 case DW_OP_lit8:
627 case DW_OP_lit9:
628 case DW_OP_lit10:
629 case DW_OP_lit11:
630 case DW_OP_lit12:
631 case DW_OP_lit13:
632 case DW_OP_lit14:
633 case DW_OP_lit15:
634 case DW_OP_lit16:
635 case DW_OP_lit17:
636 case DW_OP_lit18:
637 case DW_OP_lit19:
638 case DW_OP_lit20:
639 case DW_OP_lit21:
640 case DW_OP_lit22:
641 case DW_OP_lit23:
642 case DW_OP_lit24:
643 case DW_OP_lit25:
644 case DW_OP_lit26:
645 case DW_OP_lit27:
646 case DW_OP_lit28:
647 case DW_OP_lit29:
648 case DW_OP_lit30:
649 case DW_OP_lit31:
650 result = op - DW_OP_lit0;
8a9b8146 651 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
652 break;
653
654 case DW_OP_addr:
f2c7657e 655 result = extract_unsigned_integer (op_ptr,
595d2e30
TT
656 this->addr_size, byte_order);
657 op_ptr += this->addr_size;
ac56253d
TT
658 /* Some versions of GCC emit DW_OP_addr before
659 DW_OP_GNU_push_tls_address. In this case the value is an
660 index, not an address. We don't support things like
661 branching between the address and the TLS op. */
662 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
595d2e30 663 result += this->offset;
8a9b8146 664 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
665 break;
666
3019eac3 667 case DW_OP_GNU_addr_index:
49f6c839 668 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
192ca6d8 669 result = this->get_addr_index (uoffset);
595d2e30 670 result += this->offset;
49f6c839
DE
671 result_val = value_from_ulongest (address_type, result);
672 break;
673 case DW_OP_GNU_const_index:
f664829e 674 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
192ca6d8 675 result = this->get_addr_index (uoffset);
3019eac3
DE
676 result_val = value_from_ulongest (address_type, result);
677 break;
678
4c2df51b 679 case DW_OP_const1u:
e17a4113 680 result = extract_unsigned_integer (op_ptr, 1, byte_order);
8a9b8146 681 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
682 op_ptr += 1;
683 break;
684 case DW_OP_const1s:
e17a4113 685 result = extract_signed_integer (op_ptr, 1, byte_order);
8a9b8146 686 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
687 op_ptr += 1;
688 break;
689 case DW_OP_const2u:
e17a4113 690 result = extract_unsigned_integer (op_ptr, 2, byte_order);
8a9b8146 691 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
692 op_ptr += 2;
693 break;
694 case DW_OP_const2s:
e17a4113 695 result = extract_signed_integer (op_ptr, 2, byte_order);
8a9b8146 696 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
697 op_ptr += 2;
698 break;
699 case DW_OP_const4u:
e17a4113 700 result = extract_unsigned_integer (op_ptr, 4, byte_order);
8a9b8146 701 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
702 op_ptr += 4;
703 break;
704 case DW_OP_const4s:
e17a4113 705 result = extract_signed_integer (op_ptr, 4, byte_order);
8a9b8146 706 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
707 op_ptr += 4;
708 break;
709 case DW_OP_const8u:
e17a4113 710 result = extract_unsigned_integer (op_ptr, 8, byte_order);
8a9b8146 711 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
712 op_ptr += 8;
713 break;
714 case DW_OP_const8s:
e17a4113 715 result = extract_signed_integer (op_ptr, 8, byte_order);
8a9b8146 716 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
717 op_ptr += 8;
718 break;
719 case DW_OP_constu:
f664829e 720 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
4c2df51b 721 result = uoffset;
8a9b8146 722 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
723 break;
724 case DW_OP_consts:
f664829e 725 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
4c2df51b 726 result = offset;
8a9b8146 727 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
728 break;
729
730 /* The DW_OP_reg operations are required to occur alone in
731 location expressions. */
732 case DW_OP_reg0:
733 case DW_OP_reg1:
734 case DW_OP_reg2:
735 case DW_OP_reg3:
736 case DW_OP_reg4:
737 case DW_OP_reg5:
738 case DW_OP_reg6:
739 case DW_OP_reg7:
740 case DW_OP_reg8:
741 case DW_OP_reg9:
742 case DW_OP_reg10:
743 case DW_OP_reg11:
744 case DW_OP_reg12:
745 case DW_OP_reg13:
746 case DW_OP_reg14:
747 case DW_OP_reg15:
748 case DW_OP_reg16:
749 case DW_OP_reg17:
750 case DW_OP_reg18:
751 case DW_OP_reg19:
752 case DW_OP_reg20:
753 case DW_OP_reg21:
754 case DW_OP_reg22:
755 case DW_OP_reg23:
756 case DW_OP_reg24:
757 case DW_OP_reg25:
758 case DW_OP_reg26:
759 case DW_OP_reg27:
760 case DW_OP_reg28:
761 case DW_OP_reg29:
762 case DW_OP_reg30:
763 case DW_OP_reg31:
f206f69c 764 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
4c2df51b 765
61fbb938 766 result = op - DW_OP_reg0;
8a9b8146 767 result_val = value_from_ulongest (address_type, result);
595d2e30 768 this->location = DWARF_VALUE_REGISTER;
4c2df51b
DJ
769 break;
770
771 case DW_OP_regx:
f664829e 772 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
3cf03773 773 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
4c2df51b 774
61fbb938 775 result = reg;
8a9b8146 776 result_val = value_from_ulongest (address_type, result);
595d2e30 777 this->location = DWARF_VALUE_REGISTER;
4c2df51b
DJ
778 break;
779
cec03d70
TT
780 case DW_OP_implicit_value:
781 {
9fccedf7 782 uint64_t len;
9a619af0 783
f664829e 784 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
cec03d70
TT
785 if (op_ptr + len > op_end)
786 error (_("DW_OP_implicit_value: too few bytes available."));
595d2e30
TT
787 this->len = len;
788 this->data = op_ptr;
789 this->location = DWARF_VALUE_LITERAL;
cec03d70 790 op_ptr += len;
3cf03773
TT
791 dwarf_expr_require_composition (op_ptr, op_end,
792 "DW_OP_implicit_value");
cec03d70
TT
793 }
794 goto no_push;
795
796 case DW_OP_stack_value:
595d2e30 797 this->location = DWARF_VALUE_STACK;
3cf03773 798 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
cec03d70
TT
799 goto no_push;
800
216f72a1 801 case DW_OP_implicit_pointer:
8cf6f0b1
TT
802 case DW_OP_GNU_implicit_pointer:
803 {
9fccedf7 804 int64_t len;
8cf6f0b1 805
595d2e30 806 if (this->ref_addr_size == -1)
216f72a1 807 error (_("DWARF-2 expression error: DW_OP_implicit_pointer "
181cebd4
JK
808 "is not allowed in frame context"));
809
8b9737bf 810 /* The referred-to DIE of sect_offset kind. */
595d2e30 811 this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
8cf6f0b1 812 byte_order);
595d2e30 813 op_ptr += this->ref_addr_size;
8cf6f0b1
TT
814
815 /* The byte offset into the data. */
f664829e 816 op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
8cf6f0b1 817 result = (ULONGEST) len;
8a9b8146 818 result_val = value_from_ulongest (address_type, result);
8cf6f0b1 819
595d2e30 820 this->location = DWARF_VALUE_IMPLICIT_POINTER;
8cf6f0b1 821 dwarf_expr_require_composition (op_ptr, op_end,
216f72a1 822 "DW_OP_implicit_pointer");
8cf6f0b1
TT
823 }
824 break;
825
4c2df51b
DJ
826 case DW_OP_breg0:
827 case DW_OP_breg1:
828 case DW_OP_breg2:
829 case DW_OP_breg3:
830 case DW_OP_breg4:
831 case DW_OP_breg5:
832 case DW_OP_breg6:
833 case DW_OP_breg7:
834 case DW_OP_breg8:
835 case DW_OP_breg9:
836 case DW_OP_breg10:
837 case DW_OP_breg11:
838 case DW_OP_breg12:
839 case DW_OP_breg13:
840 case DW_OP_breg14:
841 case DW_OP_breg15:
842 case DW_OP_breg16:
843 case DW_OP_breg17:
844 case DW_OP_breg18:
845 case DW_OP_breg19:
846 case DW_OP_breg20:
847 case DW_OP_breg21:
848 case DW_OP_breg22:
849 case DW_OP_breg23:
850 case DW_OP_breg24:
851 case DW_OP_breg25:
852 case DW_OP_breg26:
853 case DW_OP_breg27:
854 case DW_OP_breg28:
855 case DW_OP_breg29:
856 case DW_OP_breg30:
857 case DW_OP_breg31:
858 {
f664829e 859 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
192ca6d8 860 result = this->read_addr_from_reg (op - DW_OP_breg0);
4c2df51b 861 result += offset;
8a9b8146 862 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
863 }
864 break;
865 case DW_OP_bregx:
866 {
f664829e
DE
867 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
868 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
192ca6d8 869 result = this->read_addr_from_reg (reg);
4c2df51b 870 result += offset;
8a9b8146 871 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
872 }
873 break;
874 case DW_OP_fbreg:
875 {
0d45f56e 876 const gdb_byte *datastart;
4c2df51b
DJ
877 size_t datalen;
878 unsigned int before_stack_len;
879
f664829e 880 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
4c2df51b
DJ
881 /* Rather than create a whole new context, we simply
882 record the stack length before execution, then reset it
883 afterwards, effectively erasing whatever the recursive
884 call put there. */
595d2e30 885 before_stack_len = this->stack_len;
da62e633
AC
886 /* FIXME: cagney/2003-03-26: This code should be using
887 get_frame_base_address(), and then implement a dwarf2
888 specific this_base method. */
192ca6d8 889 this->get_frame_base (&datastart, &datalen);
595d2e30
TT
890 eval (datastart, datalen);
891 if (this->location == DWARF_VALUE_MEMORY)
892 result = fetch_address (0);
893 else if (this->location == DWARF_VALUE_REGISTER)
192ca6d8 894 result = this->read_addr_from_reg (value_as_long (fetch (0)));
f2c7657e 895 else
3e43a32a
MS
896 error (_("Not implemented: computing frame "
897 "base using explicit value operator"));
4c2df51b 898 result = result + offset;
8a9b8146 899 result_val = value_from_ulongest (address_type, result);
69009882 900 in_stack_memory = true;
595d2e30
TT
901 this->stack_len = before_stack_len;
902 this->location = DWARF_VALUE_MEMORY;
4c2df51b
DJ
903 }
904 break;
44353522 905
4c2df51b 906 case DW_OP_dup:
595d2e30
TT
907 result_val = fetch (0);
908 in_stack_memory = fetch_in_stack_memory (0);
4c2df51b
DJ
909 break;
910
911 case DW_OP_drop:
595d2e30 912 pop ();
4c2df51b
DJ
913 goto no_push;
914
915 case DW_OP_pick:
916 offset = *op_ptr++;
595d2e30
TT
917 result_val = fetch (offset);
918 in_stack_memory = fetch_in_stack_memory (offset);
4c2df51b 919 break;
9f3fe11c
TG
920
921 case DW_OP_swap:
922 {
44353522 923 struct dwarf_stack_value t1, t2;
9f3fe11c 924
595d2e30 925 if (this->stack_len < 2)
3e43a32a 926 error (_("Not enough elements for "
0963b4bd 927 "DW_OP_swap. Need 2, have %d."),
595d2e30
TT
928 this->stack_len);
929 t1 = this->stack[this->stack_len - 1];
930 t2 = this->stack[this->stack_len - 2];
931 this->stack[this->stack_len - 1] = t2;
932 this->stack[this->stack_len - 2] = t1;
9f3fe11c
TG
933 goto no_push;
934 }
4c2df51b
DJ
935
936 case DW_OP_over:
595d2e30
TT
937 result_val = fetch (1);
938 in_stack_memory = fetch_in_stack_memory (1);
4c2df51b
DJ
939 break;
940
941 case DW_OP_rot:
942 {
44353522 943 struct dwarf_stack_value t1, t2, t3;
4c2df51b 944
595d2e30 945 if (this->stack_len < 3)
0963b4bd
MS
946 error (_("Not enough elements for "
947 "DW_OP_rot. Need 3, have %d."),
595d2e30
TT
948 this->stack_len);
949 t1 = this->stack[this->stack_len - 1];
950 t2 = this->stack[this->stack_len - 2];
951 t3 = this->stack[this->stack_len - 3];
952 this->stack[this->stack_len - 1] = t2;
953 this->stack[this->stack_len - 2] = t3;
954 this->stack[this->stack_len - 3] = t1;
4c2df51b
DJ
955 goto no_push;
956 }
957
958 case DW_OP_deref:
959 case DW_OP_deref_size:
216f72a1 960 case DW_OP_deref_type:
8a9b8146 961 case DW_OP_GNU_deref_type:
f2c7657e 962 {
595d2e30 963 int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
224c3ddb 964 gdb_byte *buf = (gdb_byte *) alloca (addr_size);
595d2e30 965 CORE_ADDR addr = fetch_address (0);
8a9b8146
TT
966 struct type *type;
967
595d2e30 968 pop ();
f2c7657e 969
216f72a1 970 if (op == DW_OP_deref_type || op == DW_OP_GNU_deref_type)
8a9b8146 971 {
f664829e 972 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
9c541725
PA
973 cu_offset type_die_cu_off = (cu_offset) uoffset;
974 type = get_base_type (type_die_cu_off, 0);
8a9b8146
TT
975 }
976 else
977 type = address_type;
978
192ca6d8 979 this->read_mem (buf, addr, addr_size);
325663dc
JB
980
981 /* If the size of the object read from memory is different
982 from the type length, we need to zero-extend it. */
983 if (TYPE_LENGTH (type) != addr_size)
984 {
985 ULONGEST result =
986 extract_unsigned_integer (buf, addr_size, byte_order);
987
224c3ddb 988 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
325663dc
JB
989 store_unsigned_integer (buf, TYPE_LENGTH (type),
990 byte_order, result);
991 }
992
8a9b8146 993 result_val = value_from_contents_and_address (type, buf, addr);
f2c7657e
UW
994 break;
995 }
996
4c2df51b
DJ
997 case DW_OP_abs:
998 case DW_OP_neg:
999 case DW_OP_not:
1000 case DW_OP_plus_uconst:
8a9b8146
TT
1001 {
1002 /* Unary operations. */
595d2e30
TT
1003 result_val = fetch (0);
1004 pop ();
4c2df51b 1005
8a9b8146
TT
1006 switch (op)
1007 {
1008 case DW_OP_abs:
1009 if (value_less (result_val,
1010 value_zero (value_type (result_val), not_lval)))
1011 result_val = value_neg (result_val);
1012 break;
1013 case DW_OP_neg:
1014 result_val = value_neg (result_val);
1015 break;
1016 case DW_OP_not:
1017 dwarf_require_integral (value_type (result_val));
1018 result_val = value_complement (result_val);
1019 break;
1020 case DW_OP_plus_uconst:
1021 dwarf_require_integral (value_type (result_val));
1022 result = value_as_long (result_val);
f664829e 1023 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
8a9b8146
TT
1024 result += reg;
1025 result_val = value_from_ulongest (address_type, result);
1026 break;
1027 }
1028 }
4c2df51b
DJ
1029 break;
1030
1031 case DW_OP_and:
1032 case DW_OP_div:
1033 case DW_OP_minus:
1034 case DW_OP_mod:
1035 case DW_OP_mul:
1036 case DW_OP_or:
1037 case DW_OP_plus:
1038 case DW_OP_shl:
1039 case DW_OP_shr:
1040 case DW_OP_shra:
1041 case DW_OP_xor:
1042 case DW_OP_le:
1043 case DW_OP_ge:
1044 case DW_OP_eq:
1045 case DW_OP_lt:
1046 case DW_OP_gt:
1047 case DW_OP_ne:
1048 {
f2c7657e 1049 /* Binary operations. */
8a9b8146 1050 struct value *first, *second;
4c2df51b 1051
595d2e30
TT
1052 second = fetch (0);
1053 pop ();
4c2df51b 1054
595d2e30
TT
1055 first = fetch (0);
1056 pop ();
4c2df51b 1057
8a9b8146
TT
1058 if (! base_types_equal_p (value_type (first), value_type (second)))
1059 error (_("Incompatible types on DWARF stack"));
1060
4c2df51b
DJ
1061 switch (op)
1062 {
1063 case DW_OP_and:
8a9b8146
TT
1064 dwarf_require_integral (value_type (first));
1065 dwarf_require_integral (value_type (second));
1066 result_val = value_binop (first, second, BINOP_BITWISE_AND);
4c2df51b
DJ
1067 break;
1068 case DW_OP_div:
8a9b8146 1069 result_val = value_binop (first, second, BINOP_DIV);
99c87dab 1070 break;
4c2df51b 1071 case DW_OP_minus:
8a9b8146 1072 result_val = value_binop (first, second, BINOP_SUB);
4c2df51b
DJ
1073 break;
1074 case DW_OP_mod:
8a9b8146
TT
1075 {
1076 int cast_back = 0;
1077 struct type *orig_type = value_type (first);
1078
1079 /* We have to special-case "old-style" untyped values
1080 -- these must have mod computed using unsigned
1081 math. */
1082 if (orig_type == address_type)
1083 {
1084 struct type *utype
595d2e30 1085 = get_unsigned_type (this->gdbarch, orig_type);
8a9b8146
TT
1086
1087 cast_back = 1;
1088 first = value_cast (utype, first);
1089 second = value_cast (utype, second);
1090 }
1091 /* Note that value_binop doesn't handle float or
1092 decimal float here. This seems unimportant. */
1093 result_val = value_binop (first, second, BINOP_MOD);
1094 if (cast_back)
1095 result_val = value_cast (orig_type, result_val);
1096 }
4c2df51b
DJ
1097 break;
1098 case DW_OP_mul:
8a9b8146 1099 result_val = value_binop (first, second, BINOP_MUL);
4c2df51b
DJ
1100 break;
1101 case DW_OP_or:
8a9b8146
TT
1102 dwarf_require_integral (value_type (first));
1103 dwarf_require_integral (value_type (second));
1104 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
4c2df51b
DJ
1105 break;
1106 case DW_OP_plus:
8a9b8146 1107 result_val = value_binop (first, second, BINOP_ADD);
4c2df51b
DJ
1108 break;
1109 case DW_OP_shl:
8a9b8146
TT
1110 dwarf_require_integral (value_type (first));
1111 dwarf_require_integral (value_type (second));
1112 result_val = value_binop (first, second, BINOP_LSH);
4c2df51b
DJ
1113 break;
1114 case DW_OP_shr:
8a9b8146
TT
1115 dwarf_require_integral (value_type (first));
1116 dwarf_require_integral (value_type (second));
b087e0ed 1117 if (!TYPE_UNSIGNED (value_type (first)))
8a9b8146
TT
1118 {
1119 struct type *utype
595d2e30 1120 = get_unsigned_type (this->gdbarch, value_type (first));
8a9b8146
TT
1121
1122 first = value_cast (utype, first);
1123 }
1124
1125 result_val = value_binop (first, second, BINOP_RSH);
1126 /* Make sure we wind up with the same type we started
1127 with. */
1128 if (value_type (result_val) != value_type (second))
1129 result_val = value_cast (value_type (second), result_val);
99c87dab 1130 break;
4c2df51b 1131 case DW_OP_shra:
8a9b8146
TT
1132 dwarf_require_integral (value_type (first));
1133 dwarf_require_integral (value_type (second));
8ddd9a20
TT
1134 if (TYPE_UNSIGNED (value_type (first)))
1135 {
1136 struct type *stype
595d2e30 1137 = get_signed_type (this->gdbarch, value_type (first));
8ddd9a20
TT
1138
1139 first = value_cast (stype, first);
1140 }
1141
8a9b8146 1142 result_val = value_binop (first, second, BINOP_RSH);
8ddd9a20
TT
1143 /* Make sure we wind up with the same type we started
1144 with. */
1145 if (value_type (result_val) != value_type (second))
1146 result_val = value_cast (value_type (second), result_val);
4c2df51b
DJ
1147 break;
1148 case DW_OP_xor:
8a9b8146
TT
1149 dwarf_require_integral (value_type (first));
1150 dwarf_require_integral (value_type (second));
1151 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
4c2df51b
DJ
1152 break;
1153 case DW_OP_le:
8a9b8146
TT
1154 /* A <= B is !(B < A). */
1155 result = ! value_less (second, first);
1156 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1157 break;
1158 case DW_OP_ge:
8a9b8146
TT
1159 /* A >= B is !(A < B). */
1160 result = ! value_less (first, second);
1161 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1162 break;
1163 case DW_OP_eq:
8a9b8146
TT
1164 result = value_equal (first, second);
1165 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1166 break;
1167 case DW_OP_lt:
8a9b8146
TT
1168 result = value_less (first, second);
1169 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1170 break;
1171 case DW_OP_gt:
8a9b8146
TT
1172 /* A > B is B < A. */
1173 result = value_less (second, first);
1174 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1175 break;
1176 case DW_OP_ne:
8a9b8146
TT
1177 result = ! value_equal (first, second);
1178 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1179 break;
1180 default:
1181 internal_error (__FILE__, __LINE__,
e2e0b3e5 1182 _("Can't be reached."));
4c2df51b 1183 }
4c2df51b
DJ
1184 }
1185 break;
1186
e7802207 1187 case DW_OP_call_frame_cfa:
192ca6d8 1188 result = this->get_frame_cfa ();
8a9b8146 1189 result_val = value_from_ulongest (address_type, result);
69009882 1190 in_stack_memory = true;
e7802207
TT
1191 break;
1192
4c2df51b 1193 case DW_OP_GNU_push_tls_address:
4aa4e28b 1194 case DW_OP_form_tls_address:
c3228f12
EZ
1195 /* Variable is at a constant offset in the thread-local
1196 storage block into the objfile for the current thread and
0963b4bd 1197 the dynamic linker module containing this expression. Here
c3228f12
EZ
1198 we return returns the offset from that base. The top of the
1199 stack has the offset from the beginning of the thread
1200 control block at which the variable is located. Nothing
1201 should follow this operator, so the top of stack would be
1202 returned. */
595d2e30
TT
1203 result = value_as_long (fetch (0));
1204 pop ();
192ca6d8 1205 result = this->get_tls_address (result);
8a9b8146 1206 result_val = value_from_ulongest (address_type, result);
4c2df51b
DJ
1207 break;
1208
1209 case DW_OP_skip:
e17a4113 1210 offset = extract_signed_integer (op_ptr, 2, byte_order);
4c2df51b
DJ
1211 op_ptr += 2;
1212 op_ptr += offset;
1213 goto no_push;
1214
1215 case DW_OP_bra:
8a9b8146
TT
1216 {
1217 struct value *val;
1218
1219 offset = extract_signed_integer (op_ptr, 2, byte_order);
1220 op_ptr += 2;
595d2e30 1221 val = fetch (0);
8a9b8146
TT
1222 dwarf_require_integral (value_type (val));
1223 if (value_as_long (val) != 0)
1224 op_ptr += offset;
595d2e30 1225 pop ();
8a9b8146 1226 }
4c2df51b
DJ
1227 goto no_push;
1228
1229 case DW_OP_nop:
1230 goto no_push;
1231
87808bd6
JB
1232 case DW_OP_piece:
1233 {
9fccedf7 1234 uint64_t size;
87808bd6
JB
1235
1236 /* Record the piece. */
f664829e 1237 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
595d2e30 1238 add_piece (8 * size, 0);
87808bd6 1239
cec03d70
TT
1240 /* Pop off the address/regnum, and reset the location
1241 type. */
595d2e30
TT
1242 if (this->location != DWARF_VALUE_LITERAL
1243 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1244 pop ();
1245 this->location = DWARF_VALUE_MEMORY;
87808bd6
JB
1246 }
1247 goto no_push;
1248
d3b1e874
TT
1249 case DW_OP_bit_piece:
1250 {
9fccedf7 1251 uint64_t size, offset;
d3b1e874
TT
1252
1253 /* Record the piece. */
f664829e
DE
1254 op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1255 op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
595d2e30 1256 add_piece (size, offset);
d3b1e874
TT
1257
1258 /* Pop off the address/regnum, and reset the location
1259 type. */
595d2e30
TT
1260 if (this->location != DWARF_VALUE_LITERAL
1261 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1262 pop ();
1263 this->location = DWARF_VALUE_MEMORY;
d3b1e874
TT
1264 }
1265 goto no_push;
1266
42be36b3
CT
1267 case DW_OP_GNU_uninit:
1268 if (op_ptr != op_end)
9c482037 1269 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
42be36b3
CT
1270 "be the very last op."));
1271
595d2e30 1272 this->initialized = 0;
42be36b3
CT
1273 goto no_push;
1274
5c631832 1275 case DW_OP_call2:
b64f50a1 1276 {
9c541725
PA
1277 cu_offset cu_off
1278 = (cu_offset) extract_unsigned_integer (op_ptr, 2, byte_order);
b64f50a1 1279 op_ptr += 2;
9c541725 1280 this->dwarf_call (cu_off);
b64f50a1 1281 }
5c631832
JK
1282 goto no_push;
1283
1284 case DW_OP_call4:
b64f50a1 1285 {
9c541725
PA
1286 cu_offset cu_off
1287 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
b64f50a1 1288 op_ptr += 4;
9c541725 1289 this->dwarf_call (cu_off);
b64f50a1 1290 }
5c631832 1291 goto no_push;
dd90784c 1292
216f72a1 1293 case DW_OP_entry_value:
dd90784c 1294 case DW_OP_GNU_entry_value:
8e3b41a9 1295 {
9fccedf7 1296 uint64_t len;
8e3b41a9 1297 CORE_ADDR deref_size;
24c5c679 1298 union call_site_parameter_u kind_u;
8e3b41a9 1299
f664829e 1300 op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
8e3b41a9 1301 if (op_ptr + len > op_end)
216f72a1 1302 error (_("DW_OP_entry_value: too few bytes available."));
8e3b41a9 1303
24c5c679
JK
1304 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1305 if (kind_u.dwarf_reg != -1)
8e3b41a9
JK
1306 {
1307 op_ptr += len;
192ca6d8
TT
1308 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1309 kind_u,
1310 -1 /* deref_size */);
a471c594
JK
1311 goto no_push;
1312 }
1313
24c5c679
JK
1314 kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1315 op_ptr + len,
1316 &deref_size);
1317 if (kind_u.dwarf_reg != -1)
a471c594
JK
1318 {
1319 if (deref_size == -1)
595d2e30 1320 deref_size = this->addr_size;
a471c594 1321 op_ptr += len;
192ca6d8
TT
1322 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_DWARF_REG,
1323 kind_u, deref_size);
8e3b41a9
JK
1324 goto no_push;
1325 }
1326
216f72a1 1327 error (_("DWARF-2 expression error: DW_OP_entry_value is "
a471c594
JK
1328 "supported only for single DW_OP_reg* "
1329 "or for DW_OP_breg*(0)+DW_OP_deref*"));
8e3b41a9 1330 }
5c631832 1331
1788b2d3
JK
1332 case DW_OP_GNU_parameter_ref:
1333 {
1334 union call_site_parameter_u kind_u;
1335
9c541725
PA
1336 kind_u.param_cu_off
1337 = (cu_offset) extract_unsigned_integer (op_ptr, 4, byte_order);
1788b2d3 1338 op_ptr += 4;
192ca6d8
TT
1339 this->push_dwarf_reg_entry_value (CALL_SITE_PARAMETER_PARAM_OFFSET,
1340 kind_u,
1341 -1 /* deref_size */);
1788b2d3
JK
1342 }
1343 goto no_push;
1344
216f72a1 1345 case DW_OP_const_type:
8a9b8146
TT
1346 case DW_OP_GNU_const_type:
1347 {
8a9b8146
TT
1348 int n;
1349 const gdb_byte *data;
1350 struct type *type;
1351
f664829e 1352 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
9c541725
PA
1353 cu_offset type_die_cu_off = (cu_offset) uoffset;
1354
8a9b8146
TT
1355 n = *op_ptr++;
1356 data = op_ptr;
1357 op_ptr += n;
1358
9c541725 1359 type = get_base_type (type_die_cu_off, n);
8a9b8146
TT
1360 result_val = value_from_contents (type, data);
1361 }
1362 break;
1363
216f72a1 1364 case DW_OP_regval_type:
8a9b8146
TT
1365 case DW_OP_GNU_regval_type:
1366 {
8a9b8146
TT
1367 struct type *type;
1368
f664829e
DE
1369 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1370 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
9c541725 1371 cu_offset type_die_cu_off = (cu_offset) uoffset;
8a9b8146 1372
9c541725 1373 type = get_base_type (type_die_cu_off, 0);
192ca6d8 1374 result_val = this->get_reg_value (type, reg);
8a9b8146
TT
1375 }
1376 break;
1377
216f72a1 1378 case DW_OP_convert:
8a9b8146 1379 case DW_OP_GNU_convert:
216f72a1 1380 case DW_OP_reinterpret:
8a9b8146
TT
1381 case DW_OP_GNU_reinterpret:
1382 {
8a9b8146
TT
1383 struct type *type;
1384
f664829e 1385 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
9c541725 1386 cu_offset type_die_cu_off = (cu_offset) uoffset;
8a9b8146 1387
9c541725 1388 if (to_underlying (type_die_cu_off) == 0)
c38c4bc5
TT
1389 type = address_type;
1390 else
9c541725 1391 type = get_base_type (type_die_cu_off, 0);
8a9b8146 1392
595d2e30
TT
1393 result_val = fetch (0);
1394 pop ();
8a9b8146 1395
216f72a1 1396 if (op == DW_OP_convert || op == DW_OP_GNU_convert)
8a9b8146
TT
1397 result_val = value_cast (type, result_val);
1398 else if (type == value_type (result_val))
1399 {
1400 /* Nothing. */
1401 }
1402 else if (TYPE_LENGTH (type)
1403 != TYPE_LENGTH (value_type (result_val)))
216f72a1 1404 error (_("DW_OP_reinterpret has wrong size"));
8a9b8146
TT
1405 else
1406 result_val
1407 = value_from_contents (type,
1408 value_contents_all (result_val));
1409 }
1410 break;
1411
08412b07
JB
1412 case DW_OP_push_object_address:
1413 /* Return the address of the object we are currently observing. */
192ca6d8 1414 result = this->get_object_address ();
08412b07
JB
1415 result_val = value_from_ulongest (address_type, result);
1416 break;
1417
4c2df51b 1418 default:
8a3fe4f8 1419 error (_("Unhandled dwarf expression opcode 0x%x"), op);
4c2df51b
DJ
1420 }
1421
1422 /* Most things push a result value. */
8a9b8146 1423 gdb_assert (result_val != NULL);
595d2e30 1424 push (result_val, in_stack_memory);
82ae4854 1425 no_push:
b27cf2b3 1426 ;
4c2df51b 1427 }
1e3a102a 1428
8cf6f0b1
TT
1429 /* To simplify our main caller, if the result is an implicit
1430 pointer, then make a pieced value. This is ok because we can't
1431 have implicit pointers in contexts where pieces are invalid. */
595d2e30
TT
1432 if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1433 add_piece (8 * this->addr_size, 0);
8cf6f0b1 1434
dd90784c 1435abort_expression:
595d2e30
TT
1436 this->recursion_depth--;
1437 gdb_assert (this->recursion_depth >= 0);
8a9b8146
TT
1438}
1439
1440void
1441_initialize_dwarf2expr (void)
1442{
1443 dwarf_arch_cookie
1444 = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);
4c2df51b 1445}
This page took 1.251107 seconds and 4 git commands to generate.