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