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