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