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