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