2011-01-05 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2expr.c
CommitLineData
852483bc
MK
1/* DWARF 2 Expression Evaluator.
2
7b6bb8da 3 Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
9b254dd1 4 Free Software Foundation, 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
DJ
36
37/* Create a new context for the expression evaluator. */
38
39struct dwarf_expr_context *
e4adbba9 40new_dwarf_expr_context (void)
4c2df51b
DJ
41{
42 struct dwarf_expr_context *retval;
9a619af0 43
4c2df51b 44 retval = xcalloc (1, sizeof (struct dwarf_expr_context));
18ec9831
KB
45 retval->stack_len = 0;
46 retval->stack_allocated = 10;
b966cb8a
TT
47 retval->stack = xmalloc (retval->stack_allocated
48 * sizeof (struct dwarf_stack_value));
87808bd6
JB
49 retval->num_pieces = 0;
50 retval->pieces = 0;
1e3a102a 51 retval->max_recursion_depth = 0x100;
4c2df51b
DJ
52 return retval;
53}
54
55/* Release the memory allocated to CTX. */
56
57void
58free_dwarf_expr_context (struct dwarf_expr_context *ctx)
59{
60 xfree (ctx->stack);
87808bd6 61 xfree (ctx->pieces);
4c2df51b
DJ
62 xfree (ctx);
63}
64
4a227398
TT
65/* Helper for make_cleanup_free_dwarf_expr_context. */
66
67static void
68free_dwarf_expr_context_cleanup (void *arg)
69{
70 free_dwarf_expr_context (arg);
71}
72
73/* Return a cleanup that calls free_dwarf_expr_context. */
74
75struct cleanup *
76make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
77{
78 return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
79}
80
4c2df51b
DJ
81/* Expand the memory allocated to CTX's stack to contain at least
82 NEED more elements than are currently used. */
83
84static void
85dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
86{
87 if (ctx->stack_len + need > ctx->stack_allocated)
88 {
18ec9831 89 size_t newlen = ctx->stack_len + need + 10;
9a619af0 90
4c2df51b 91 ctx->stack = xrealloc (ctx->stack,
44353522 92 newlen * sizeof (struct dwarf_stack_value));
18ec9831 93 ctx->stack_allocated = newlen;
4c2df51b
DJ
94 }
95}
96
97/* Push VALUE onto CTX's stack. */
98
99void
f2c7657e 100dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value,
44353522 101 int in_stack_memory)
4c2df51b 102{
44353522
DE
103 struct dwarf_stack_value *v;
104
f2c7657e
UW
105 /* We keep all stack elements within the range defined by the
106 DWARF address size. */
107 if (ctx->addr_size < sizeof (ULONGEST))
108 value &= ((ULONGEST) 1 << (ctx->addr_size * HOST_CHAR_BIT)) - 1;
109
4c2df51b 110 dwarf_expr_grow_stack (ctx, 1);
44353522
DE
111 v = &ctx->stack[ctx->stack_len++];
112 v->value = value;
113 v->in_stack_memory = in_stack_memory;
4c2df51b
DJ
114}
115
116/* Pop the top item off of CTX's stack. */
117
118void
119dwarf_expr_pop (struct dwarf_expr_context *ctx)
120{
121 if (ctx->stack_len <= 0)
8a3fe4f8 122 error (_("dwarf expression stack underflow"));
4c2df51b
DJ
123 ctx->stack_len--;
124}
125
126/* Retrieve the N'th item on CTX's stack. */
127
f2c7657e 128ULONGEST
4c2df51b
DJ
129dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
130{
ef0fdf07 131 if (ctx->stack_len <= n)
3e43a32a
MS
132 error (_("Asked for position %d of stack, "
133 "stack only has %d elements on it."),
4c2df51b 134 n, ctx->stack_len);
44353522
DE
135 return ctx->stack[ctx->stack_len - (1 + n)].value;
136
137}
138
f2c7657e
UW
139/* Retrieve the N'th item on CTX's stack, converted to an address. */
140
141CORE_ADDR
142dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
143{
144 ULONGEST result = dwarf_expr_fetch (ctx, n);
145
146 /* For most architectures, calling extract_unsigned_integer() alone
147 is sufficient for extracting an address. However, some
148 architectures (e.g. MIPS) use signed addresses and using
149 extract_unsigned_integer() will not produce a correct
150 result. Make sure we invoke gdbarch_integer_to_address()
151 for those architectures which require it. */
152 if (gdbarch_integer_to_address_p (ctx->gdbarch))
153 {
154 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
155 gdb_byte *buf = alloca (ctx->addr_size);
156 struct type *int_type;
157
158 switch (ctx->addr_size)
159 {
160 case 2:
161 int_type = builtin_type (ctx->gdbarch)->builtin_uint16;
162 break;
163 case 4:
164 int_type = builtin_type (ctx->gdbarch)->builtin_uint32;
165 break;
166 case 8:
167 int_type = builtin_type (ctx->gdbarch)->builtin_uint64;
168 break;
169 default:
170 internal_error (__FILE__, __LINE__,
171 _("Unsupported address size.\n"));
172 }
173
174 store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
175 return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
176 }
177
178 return (CORE_ADDR) result;
179}
180
44353522
DE
181/* Retrieve the in_stack_memory flag of the N'th item on CTX's stack. */
182
183int
184dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
185{
186 if (ctx->stack_len <= n)
3e43a32a
MS
187 error (_("Asked for position %d of stack, "
188 "stack only has %d elements on it."),
44353522
DE
189 n, ctx->stack_len);
190 return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
4c2df51b
DJ
191
192}
193
cb826367
TT
194/* Return true if the expression stack is empty. */
195
196static int
197dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
198{
199 return ctx->stack_len == 0;
200}
201
87808bd6
JB
202/* Add a new piece to CTX's piece list. */
203static void
d3b1e874 204add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
87808bd6
JB
205{
206 struct dwarf_expr_piece *p;
207
208 ctx->num_pieces++;
209
d3b1e874
TT
210 ctx->pieces = xrealloc (ctx->pieces,
211 (ctx->num_pieces
212 * sizeof (struct dwarf_expr_piece)));
87808bd6
JB
213
214 p = &ctx->pieces[ctx->num_pieces - 1];
cec03d70 215 p->location = ctx->location;
87808bd6 216 p->size = size;
d3b1e874
TT
217 p->offset = offset;
218
cec03d70
TT
219 if (p->location == DWARF_VALUE_LITERAL)
220 {
221 p->v.literal.data = ctx->data;
222 p->v.literal.length = ctx->len;
223 }
cb826367
TT
224 else if (dwarf_expr_stack_empty_p (ctx))
225 {
226 p->location = DWARF_VALUE_OPTIMIZED_OUT;
227 /* Also reset the context's location, for our callers. This is
228 a somewhat strange approach, but this lets us avoid setting
229 the location to DWARF_VALUE_MEMORY in all the individual
230 cases in the evaluator. */
231 ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
232 }
f2c7657e
UW
233 else if (p->location == DWARF_VALUE_MEMORY)
234 {
235 p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
236 p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
237 }
8cf6f0b1
TT
238 else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
239 {
240 p->v.ptr.die = ctx->len;
241 p->v.ptr.offset = (LONGEST) dwarf_expr_fetch (ctx, 0);
242 }
cec03d70 243 else
44353522 244 {
f2c7657e 245 p->v.value = dwarf_expr_fetch (ctx, 0);
44353522 246 }
87808bd6
JB
247}
248
4c2df51b
DJ
249/* Evaluate the expression at ADDR (LEN bytes long) using the context
250 CTX. */
251
252void
0d45f56e
TT
253dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
254 size_t len)
4c2df51b 255{
1e3a102a
JK
256 int old_recursion_depth = ctx->recursion_depth;
257
4c2df51b 258 execute_stack_op (ctx, addr, addr + len);
1e3a102a
JK
259
260 /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here. */
261
262 gdb_assert (ctx->recursion_depth == old_recursion_depth);
4c2df51b
DJ
263}
264
265/* Decode the unsigned LEB128 constant at BUF into the variable pointed to
266 by R, and return the new value of BUF. Verify that it doesn't extend
267 past BUF_END. */
268
0d45f56e
TT
269const gdb_byte *
270read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
4c2df51b
DJ
271{
272 unsigned shift = 0;
273 ULONGEST result = 0;
852483bc 274 gdb_byte byte;
4c2df51b
DJ
275
276 while (1)
277 {
278 if (buf >= buf_end)
8a3fe4f8 279 error (_("read_uleb128: Corrupted DWARF expression."));
4c2df51b
DJ
280
281 byte = *buf++;
282 result |= (byte & 0x7f) << shift;
283 if ((byte & 0x80) == 0)
284 break;
285 shift += 7;
286 }
287 *r = result;
288 return buf;
289}
290
291/* Decode the signed LEB128 constant at BUF into the variable pointed to
292 by R, and return the new value of BUF. Verify that it doesn't extend
293 past BUF_END. */
294
0d45f56e
TT
295const gdb_byte *
296read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
4c2df51b
DJ
297{
298 unsigned shift = 0;
299 LONGEST result = 0;
852483bc 300 gdb_byte byte;
4c2df51b
DJ
301
302 while (1)
303 {
304 if (buf >= buf_end)
8a3fe4f8 305 error (_("read_sleb128: Corrupted DWARF expression."));
4c2df51b
DJ
306
307 byte = *buf++;
308 result |= (byte & 0x7f) << shift;
309 shift += 7;
310 if ((byte & 0x80) == 0)
311 break;
312 }
313 if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
314 result |= -(1 << shift);
315
316 *r = result;
317 return buf;
318}
4c2df51b 319\f
cec03d70
TT
320
321/* Check that the current operator is either at the end of an
322 expression, or that it is followed by a composition operator. */
323
3cf03773
TT
324void
325dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
326 const char *op_name)
cec03d70
TT
327{
328 /* It seems like DW_OP_GNU_uninit should be handled here. However,
329 it doesn't seem to make sense for DW_OP_*_value, and it was not
330 checked at the other place that this function is called. */
331 if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
332 error (_("DWARF-2 expression error: `%s' operations must be "
333 "used either alone or in conjuction with DW_OP_piece "
334 "or DW_OP_bit_piece."),
335 op_name);
336}
337
4c2df51b
DJ
338/* The engine for the expression evaluator. Using the context in CTX,
339 evaluate the expression between OP_PTR and OP_END. */
340
341static void
852483bc 342execute_stack_op (struct dwarf_expr_context *ctx,
0d45f56e 343 const gdb_byte *op_ptr, const gdb_byte *op_end)
4c2df51b 344{
43dabe42 345#define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit))
f2c7657e
UW
346 ULONGEST sign_bit = (ctx->addr_size >= sizeof (ULONGEST) ? 0
347 : ((ULONGEST) 1) << (ctx->addr_size * 8 - 1));
e17a4113 348 enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
9a619af0 349
cec03d70 350 ctx->location = DWARF_VALUE_MEMORY;
42be36b3 351 ctx->initialized = 1; /* Default is initialized. */
18ec9831 352
1e3a102a
JK
353 if (ctx->recursion_depth > ctx->max_recursion_depth)
354 error (_("DWARF-2 expression error: Loop detected (%d)."),
355 ctx->recursion_depth);
356 ctx->recursion_depth++;
357
4c2df51b
DJ
358 while (op_ptr < op_end)
359 {
360 enum dwarf_location_atom op = *op_ptr++;
f2c7657e 361 ULONGEST result;
44353522
DE
362 /* Assume the value is not in stack memory.
363 Code that knows otherwise sets this to 1.
364 Some arithmetic on stack addresses can probably be assumed to still
365 be a stack address, but we skip this complication for now.
366 This is just an optimization, so it's always ok to punt
367 and leave this as 0. */
368 int in_stack_memory = 0;
4c2df51b
DJ
369 ULONGEST uoffset, reg;
370 LONGEST offset;
4c2df51b 371
4c2df51b
DJ
372 switch (op)
373 {
374 case DW_OP_lit0:
375 case DW_OP_lit1:
376 case DW_OP_lit2:
377 case DW_OP_lit3:
378 case DW_OP_lit4:
379 case DW_OP_lit5:
380 case DW_OP_lit6:
381 case DW_OP_lit7:
382 case DW_OP_lit8:
383 case DW_OP_lit9:
384 case DW_OP_lit10:
385 case DW_OP_lit11:
386 case DW_OP_lit12:
387 case DW_OP_lit13:
388 case DW_OP_lit14:
389 case DW_OP_lit15:
390 case DW_OP_lit16:
391 case DW_OP_lit17:
392 case DW_OP_lit18:
393 case DW_OP_lit19:
394 case DW_OP_lit20:
395 case DW_OP_lit21:
396 case DW_OP_lit22:
397 case DW_OP_lit23:
398 case DW_OP_lit24:
399 case DW_OP_lit25:
400 case DW_OP_lit26:
401 case DW_OP_lit27:
402 case DW_OP_lit28:
403 case DW_OP_lit29:
404 case DW_OP_lit30:
405 case DW_OP_lit31:
406 result = op - DW_OP_lit0;
407 break;
408
409 case DW_OP_addr:
f2c7657e
UW
410 result = extract_unsigned_integer (op_ptr,
411 ctx->addr_size, byte_order);
ae0d2f24 412 op_ptr += ctx->addr_size;
ac56253d
TT
413 /* Some versions of GCC emit DW_OP_addr before
414 DW_OP_GNU_push_tls_address. In this case the value is an
415 index, not an address. We don't support things like
416 branching between the address and the TLS op. */
417 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
418 result += ctx->offset;
4c2df51b
DJ
419 break;
420
421 case DW_OP_const1u:
e17a4113 422 result = extract_unsigned_integer (op_ptr, 1, byte_order);
4c2df51b
DJ
423 op_ptr += 1;
424 break;
425 case DW_OP_const1s:
e17a4113 426 result = extract_signed_integer (op_ptr, 1, byte_order);
4c2df51b
DJ
427 op_ptr += 1;
428 break;
429 case DW_OP_const2u:
e17a4113 430 result = extract_unsigned_integer (op_ptr, 2, byte_order);
4c2df51b
DJ
431 op_ptr += 2;
432 break;
433 case DW_OP_const2s:
e17a4113 434 result = extract_signed_integer (op_ptr, 2, byte_order);
4c2df51b
DJ
435 op_ptr += 2;
436 break;
437 case DW_OP_const4u:
e17a4113 438 result = extract_unsigned_integer (op_ptr, 4, byte_order);
4c2df51b
DJ
439 op_ptr += 4;
440 break;
441 case DW_OP_const4s:
e17a4113 442 result = extract_signed_integer (op_ptr, 4, byte_order);
4c2df51b
DJ
443 op_ptr += 4;
444 break;
445 case DW_OP_const8u:
e17a4113 446 result = extract_unsigned_integer (op_ptr, 8, byte_order);
4c2df51b
DJ
447 op_ptr += 8;
448 break;
449 case DW_OP_const8s:
e17a4113 450 result = extract_signed_integer (op_ptr, 8, byte_order);
4c2df51b
DJ
451 op_ptr += 8;
452 break;
453 case DW_OP_constu:
454 op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
455 result = uoffset;
456 break;
457 case DW_OP_consts:
458 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
459 result = offset;
460 break;
461
462 /* The DW_OP_reg operations are required to occur alone in
463 location expressions. */
464 case DW_OP_reg0:
465 case DW_OP_reg1:
466 case DW_OP_reg2:
467 case DW_OP_reg3:
468 case DW_OP_reg4:
469 case DW_OP_reg5:
470 case DW_OP_reg6:
471 case DW_OP_reg7:
472 case DW_OP_reg8:
473 case DW_OP_reg9:
474 case DW_OP_reg10:
475 case DW_OP_reg11:
476 case DW_OP_reg12:
477 case DW_OP_reg13:
478 case DW_OP_reg14:
479 case DW_OP_reg15:
480 case DW_OP_reg16:
481 case DW_OP_reg17:
482 case DW_OP_reg18:
483 case DW_OP_reg19:
484 case DW_OP_reg20:
485 case DW_OP_reg21:
486 case DW_OP_reg22:
487 case DW_OP_reg23:
488 case DW_OP_reg24:
489 case DW_OP_reg25:
490 case DW_OP_reg26:
491 case DW_OP_reg27:
492 case DW_OP_reg28:
493 case DW_OP_reg29:
494 case DW_OP_reg30:
495 case DW_OP_reg31:
42be36b3
CT
496 if (op_ptr != op_end
497 && *op_ptr != DW_OP_piece
d3b1e874 498 && *op_ptr != DW_OP_bit_piece
42be36b3 499 && *op_ptr != DW_OP_GNU_uninit)
8a3fe4f8 500 error (_("DWARF-2 expression error: DW_OP_reg operations must be "
d3b1e874
TT
501 "used either alone or in conjuction with DW_OP_piece "
502 "or DW_OP_bit_piece."));
4c2df51b 503
61fbb938 504 result = op - DW_OP_reg0;
cec03d70 505 ctx->location = DWARF_VALUE_REGISTER;
4c2df51b
DJ
506 break;
507
508 case DW_OP_regx:
509 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
3cf03773 510 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
4c2df51b 511
61fbb938 512 result = reg;
cec03d70 513 ctx->location = DWARF_VALUE_REGISTER;
4c2df51b
DJ
514 break;
515
cec03d70
TT
516 case DW_OP_implicit_value:
517 {
518 ULONGEST len;
9a619af0 519
cec03d70
TT
520 op_ptr = read_uleb128 (op_ptr, op_end, &len);
521 if (op_ptr + len > op_end)
522 error (_("DW_OP_implicit_value: too few bytes available."));
523 ctx->len = len;
524 ctx->data = op_ptr;
525 ctx->location = DWARF_VALUE_LITERAL;
526 op_ptr += len;
3cf03773
TT
527 dwarf_expr_require_composition (op_ptr, op_end,
528 "DW_OP_implicit_value");
cec03d70
TT
529 }
530 goto no_push;
531
532 case DW_OP_stack_value:
533 ctx->location = DWARF_VALUE_STACK;
3cf03773 534 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
cec03d70
TT
535 goto no_push;
536
8cf6f0b1
TT
537 case DW_OP_GNU_implicit_pointer:
538 {
539 ULONGEST die;
540 LONGEST len;
541
542 /* The referred-to DIE. */
543 ctx->len = extract_unsigned_integer (op_ptr, ctx->addr_size,
544 byte_order);
545 op_ptr += ctx->addr_size;
546
547 /* The byte offset into the data. */
548 op_ptr = read_sleb128 (op_ptr, op_end, &len);
549 result = (ULONGEST) len;
550
551 ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
552 dwarf_expr_require_composition (op_ptr, op_end,
553 "DW_OP_GNU_implicit_pointer");
554 }
555 break;
556
4c2df51b
DJ
557 case DW_OP_breg0:
558 case DW_OP_breg1:
559 case DW_OP_breg2:
560 case DW_OP_breg3:
561 case DW_OP_breg4:
562 case DW_OP_breg5:
563 case DW_OP_breg6:
564 case DW_OP_breg7:
565 case DW_OP_breg8:
566 case DW_OP_breg9:
567 case DW_OP_breg10:
568 case DW_OP_breg11:
569 case DW_OP_breg12:
570 case DW_OP_breg13:
571 case DW_OP_breg14:
572 case DW_OP_breg15:
573 case DW_OP_breg16:
574 case DW_OP_breg17:
575 case DW_OP_breg18:
576 case DW_OP_breg19:
577 case DW_OP_breg20:
578 case DW_OP_breg21:
579 case DW_OP_breg22:
580 case DW_OP_breg23:
581 case DW_OP_breg24:
582 case DW_OP_breg25:
583 case DW_OP_breg26:
584 case DW_OP_breg27:
585 case DW_OP_breg28:
586 case DW_OP_breg29:
587 case DW_OP_breg30:
588 case DW_OP_breg31:
589 {
590 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
61fbb938 591 result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
4c2df51b
DJ
592 result += offset;
593 }
594 break;
595 case DW_OP_bregx:
596 {
597 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
598 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
61fbb938 599 result = (ctx->read_reg) (ctx->baton, reg);
4c2df51b
DJ
600 result += offset;
601 }
602 break;
603 case DW_OP_fbreg:
604 {
0d45f56e 605 const gdb_byte *datastart;
4c2df51b
DJ
606 size_t datalen;
607 unsigned int before_stack_len;
608
609 op_ptr = read_sleb128 (op_ptr, op_end, &offset);
610 /* Rather than create a whole new context, we simply
611 record the stack length before execution, then reset it
612 afterwards, effectively erasing whatever the recursive
613 call put there. */
614 before_stack_len = ctx->stack_len;
da62e633
AC
615 /* FIXME: cagney/2003-03-26: This code should be using
616 get_frame_base_address(), and then implement a dwarf2
617 specific this_base method. */
4c2df51b
DJ
618 (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
619 dwarf_expr_eval (ctx, datastart, datalen);
f2c7657e
UW
620 if (ctx->location == DWARF_VALUE_MEMORY)
621 result = dwarf_expr_fetch_address (ctx, 0);
622 else if (ctx->location == DWARF_VALUE_REGISTER)
623 result = (ctx->read_reg) (ctx->baton, dwarf_expr_fetch (ctx, 0));
624 else
3e43a32a
MS
625 error (_("Not implemented: computing frame "
626 "base using explicit value operator"));
4c2df51b 627 result = result + offset;
44353522 628 in_stack_memory = 1;
4c2df51b 629 ctx->stack_len = before_stack_len;
cec03d70 630 ctx->location = DWARF_VALUE_MEMORY;
4c2df51b
DJ
631 }
632 break;
44353522 633
4c2df51b
DJ
634 case DW_OP_dup:
635 result = dwarf_expr_fetch (ctx, 0);
44353522 636 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
4c2df51b
DJ
637 break;
638
639 case DW_OP_drop:
640 dwarf_expr_pop (ctx);
641 goto no_push;
642
643 case DW_OP_pick:
644 offset = *op_ptr++;
645 result = dwarf_expr_fetch (ctx, offset);
44353522 646 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
4c2df51b 647 break;
9f3fe11c
TG
648
649 case DW_OP_swap:
650 {
44353522 651 struct dwarf_stack_value t1, t2;
9f3fe11c
TG
652
653 if (ctx->stack_len < 2)
3e43a32a
MS
654 error (_("Not enough elements for "
655 "DW_OP_swap. Need 2, have %d."),
9f3fe11c
TG
656 ctx->stack_len);
657 t1 = ctx->stack[ctx->stack_len - 1];
658 t2 = ctx->stack[ctx->stack_len - 2];
659 ctx->stack[ctx->stack_len - 1] = t2;
660 ctx->stack[ctx->stack_len - 2] = t1;
661 goto no_push;
662 }
4c2df51b
DJ
663
664 case DW_OP_over:
665 result = dwarf_expr_fetch (ctx, 1);
44353522 666 in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
4c2df51b
DJ
667 break;
668
669 case DW_OP_rot:
670 {
44353522 671 struct dwarf_stack_value t1, t2, t3;
4c2df51b
DJ
672
673 if (ctx->stack_len < 3)
8a3fe4f8 674 error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
4c2df51b
DJ
675 ctx->stack_len);
676 t1 = ctx->stack[ctx->stack_len - 1];
677 t2 = ctx->stack[ctx->stack_len - 2];
678 t3 = ctx->stack[ctx->stack_len - 3];
679 ctx->stack[ctx->stack_len - 1] = t2;
680 ctx->stack[ctx->stack_len - 2] = t3;
681 ctx->stack[ctx->stack_len - 3] = t1;
682 goto no_push;
683 }
684
685 case DW_OP_deref:
686 case DW_OP_deref_size:
f2c7657e
UW
687 {
688 int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
689 gdb_byte *buf = alloca (addr_size);
690 CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
691 dwarf_expr_pop (ctx);
692
693 (ctx->read_mem) (ctx->baton, buf, addr, addr_size);
694 result = extract_unsigned_integer (buf, addr_size, byte_order);
695 break;
696 }
697
4c2df51b
DJ
698 case DW_OP_abs:
699 case DW_OP_neg:
700 case DW_OP_not:
701 case DW_OP_plus_uconst:
702 /* Unary operations. */
703 result = dwarf_expr_fetch (ctx, 0);
704 dwarf_expr_pop (ctx);
705
706 switch (op)
707 {
4c2df51b 708 case DW_OP_abs:
f2c7657e 709 if (sign_ext (result) < 0)
4c2df51b
DJ
710 result = -result;
711 break;
712 case DW_OP_neg:
713 result = -result;
714 break;
715 case DW_OP_not:
716 result = ~result;
717 break;
718 case DW_OP_plus_uconst:
719 op_ptr = read_uleb128 (op_ptr, op_end, &reg);
720 result += reg;
721 break;
722 }
723 break;
724
725 case DW_OP_and:
726 case DW_OP_div:
727 case DW_OP_minus:
728 case DW_OP_mod:
729 case DW_OP_mul:
730 case DW_OP_or:
731 case DW_OP_plus:
732 case DW_OP_shl:
733 case DW_OP_shr:
734 case DW_OP_shra:
735 case DW_OP_xor:
736 case DW_OP_le:
737 case DW_OP_ge:
738 case DW_OP_eq:
739 case DW_OP_lt:
740 case DW_OP_gt:
741 case DW_OP_ne:
742 {
f2c7657e
UW
743 /* Binary operations. */
744 ULONGEST first, second;
4c2df51b
DJ
745
746 second = dwarf_expr_fetch (ctx, 0);
747 dwarf_expr_pop (ctx);
748
b263358a 749 first = dwarf_expr_fetch (ctx, 0);
4c2df51b
DJ
750 dwarf_expr_pop (ctx);
751
4c2df51b
DJ
752 switch (op)
753 {
754 case DW_OP_and:
f2c7657e 755 result = first & second;
4c2df51b
DJ
756 break;
757 case DW_OP_div:
f2c7657e
UW
758 if (!second)
759 error (_("Division by zero"));
760 result = sign_ext (first) / sign_ext (second);
99c87dab 761 break;
4c2df51b 762 case DW_OP_minus:
f2c7657e 763 result = first - second;
4c2df51b
DJ
764 break;
765 case DW_OP_mod:
f2c7657e
UW
766 if (!second)
767 error (_("Division by zero"));
768 result = first % second;
4c2df51b
DJ
769 break;
770 case DW_OP_mul:
f2c7657e 771 result = first * second;
4c2df51b
DJ
772 break;
773 case DW_OP_or:
f2c7657e 774 result = first | second;
4c2df51b
DJ
775 break;
776 case DW_OP_plus:
f2c7657e 777 result = first + second;
4c2df51b
DJ
778 break;
779 case DW_OP_shl:
f2c7657e 780 result = first << second;
4c2df51b
DJ
781 break;
782 case DW_OP_shr:
f2c7657e 783 result = first >> second;
99c87dab 784 break;
4c2df51b 785 case DW_OP_shra:
f2c7657e 786 result = sign_ext (first) >> second;
4c2df51b
DJ
787 break;
788 case DW_OP_xor:
f2c7657e 789 result = first ^ second;
4c2df51b
DJ
790 break;
791 case DW_OP_le:
f2c7657e 792 result = sign_ext (first) <= sign_ext (second);
4c2df51b
DJ
793 break;
794 case DW_OP_ge:
f2c7657e 795 result = sign_ext (first) >= sign_ext (second);
4c2df51b
DJ
796 break;
797 case DW_OP_eq:
f2c7657e 798 result = sign_ext (first) == sign_ext (second);
4c2df51b
DJ
799 break;
800 case DW_OP_lt:
f2c7657e 801 result = sign_ext (first) < sign_ext (second);
4c2df51b
DJ
802 break;
803 case DW_OP_gt:
f2c7657e 804 result = sign_ext (first) > sign_ext (second);
4c2df51b
DJ
805 break;
806 case DW_OP_ne:
f2c7657e 807 result = sign_ext (first) != sign_ext (second);
4c2df51b
DJ
808 break;
809 default:
810 internal_error (__FILE__, __LINE__,
e2e0b3e5 811 _("Can't be reached."));
4c2df51b 812 }
4c2df51b
DJ
813 }
814 break;
815
e7802207
TT
816 case DW_OP_call_frame_cfa:
817 result = (ctx->get_frame_cfa) (ctx->baton);
44353522 818 in_stack_memory = 1;
e7802207
TT
819 break;
820
4c2df51b 821 case DW_OP_GNU_push_tls_address:
c3228f12
EZ
822 /* Variable is at a constant offset in the thread-local
823 storage block into the objfile for the current thread and
824 the dynamic linker module containing this expression. Here
825 we return returns the offset from that base. The top of the
826 stack has the offset from the beginning of the thread
827 control block at which the variable is located. Nothing
828 should follow this operator, so the top of stack would be
829 returned. */
4c2df51b
DJ
830 result = dwarf_expr_fetch (ctx, 0);
831 dwarf_expr_pop (ctx);
832 result = (ctx->get_tls_address) (ctx->baton, result);
833 break;
834
835 case DW_OP_skip:
e17a4113 836 offset = extract_signed_integer (op_ptr, 2, byte_order);
4c2df51b
DJ
837 op_ptr += 2;
838 op_ptr += offset;
839 goto no_push;
840
841 case DW_OP_bra:
e17a4113 842 offset = extract_signed_integer (op_ptr, 2, byte_order);
4c2df51b
DJ
843 op_ptr += 2;
844 if (dwarf_expr_fetch (ctx, 0) != 0)
845 op_ptr += offset;
846 dwarf_expr_pop (ctx);
847 goto no_push;
848
849 case DW_OP_nop:
850 goto no_push;
851
87808bd6
JB
852 case DW_OP_piece:
853 {
854 ULONGEST size;
87808bd6
JB
855
856 /* Record the piece. */
857 op_ptr = read_uleb128 (op_ptr, op_end, &size);
d3b1e874 858 add_piece (ctx, 8 * size, 0);
87808bd6 859
cec03d70
TT
860 /* Pop off the address/regnum, and reset the location
861 type. */
cb826367
TT
862 if (ctx->location != DWARF_VALUE_LITERAL
863 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
cec03d70
TT
864 dwarf_expr_pop (ctx);
865 ctx->location = DWARF_VALUE_MEMORY;
87808bd6
JB
866 }
867 goto no_push;
868
d3b1e874
TT
869 case DW_OP_bit_piece:
870 {
871 ULONGEST size, offset;
872
873 /* Record the piece. */
874 op_ptr = read_uleb128 (op_ptr, op_end, &size);
875 op_ptr = read_uleb128 (op_ptr, op_end, &offset);
876 add_piece (ctx, size, offset);
877
878 /* Pop off the address/regnum, and reset the location
879 type. */
880 if (ctx->location != DWARF_VALUE_LITERAL
881 && ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
882 dwarf_expr_pop (ctx);
883 ctx->location = DWARF_VALUE_MEMORY;
884 }
885 goto no_push;
886
42be36b3
CT
887 case DW_OP_GNU_uninit:
888 if (op_ptr != op_end)
9c482037 889 error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
42be36b3
CT
890 "be the very last op."));
891
892 ctx->initialized = 0;
893 goto no_push;
894
5c631832
JK
895 case DW_OP_call2:
896 result = extract_unsigned_integer (op_ptr, 2, byte_order);
897 op_ptr += 2;
898 ctx->dwarf_call (ctx, result);
899 goto no_push;
900
901 case DW_OP_call4:
902 result = extract_unsigned_integer (op_ptr, 4, byte_order);
903 op_ptr += 4;
904 ctx->dwarf_call (ctx, result);
905 goto no_push;
906
4c2df51b 907 default:
8a3fe4f8 908 error (_("Unhandled dwarf expression opcode 0x%x"), op);
4c2df51b
DJ
909 }
910
911 /* Most things push a result value. */
44353522 912 dwarf_expr_push (ctx, result, in_stack_memory);
4c2df51b
DJ
913 no_push:;
914 }
1e3a102a 915
8cf6f0b1
TT
916 /* To simplify our main caller, if the result is an implicit
917 pointer, then make a pieced value. This is ok because we can't
918 have implicit pointers in contexts where pieces are invalid. */
919 if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
920 add_piece (ctx, 8 * ctx->addr_size, 0);
921
1e3a102a
JK
922 ctx->recursion_depth--;
923 gdb_assert (ctx->recursion_depth >= 0);
43dabe42 924#undef sign_ext
4c2df51b 925}
This page took 0.652619 seconds and 4 git commands to generate.