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