xref: /dflybsd-src/contrib/gdb-7/gdb/dwarf2expr.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 2001-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    Contributed by Daniel Berlin (dan@dberlin.org)
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This file is part of GDB.
85796c8dcSSimon Schubert 
95796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
105796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
115796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
125796c8dcSSimon Schubert    (at your option) any later version.
135796c8dcSSimon Schubert 
145796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
155796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
165796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
175796c8dcSSimon Schubert    GNU General Public License for more details.
185796c8dcSSimon Schubert 
195796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
205796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
215796c8dcSSimon Schubert 
225796c8dcSSimon Schubert #include "defs.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "value.h"
265796c8dcSSimon Schubert #include "gdbcore.h"
275796c8dcSSimon Schubert #include "dwarf2.h"
285796c8dcSSimon Schubert #include "dwarf2expr.h"
295796c8dcSSimon Schubert #include "gdb_assert.h"
305796c8dcSSimon Schubert 
315796c8dcSSimon Schubert /* Local prototypes.  */
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert static void execute_stack_op (struct dwarf_expr_context *,
34cf7f2e2dSJohn Marino 			      const gdb_byte *, const gdb_byte *);
355796c8dcSSimon Schubert 
36a45ae5f8SJohn Marino /* Cookie for gdbarch data.  */
37a45ae5f8SJohn Marino 
38a45ae5f8SJohn Marino static struct gdbarch_data *dwarf_arch_cookie;
39a45ae5f8SJohn Marino 
40a45ae5f8SJohn Marino /* This holds gdbarch-specific types used by the DWARF expression
41a45ae5f8SJohn Marino    evaluator.  See comments in execute_stack_op.  */
42a45ae5f8SJohn Marino 
43a45ae5f8SJohn Marino struct dwarf_gdbarch_types
44a45ae5f8SJohn Marino {
45a45ae5f8SJohn Marino   struct type *dw_types[3];
46a45ae5f8SJohn Marino };
47a45ae5f8SJohn Marino 
48a45ae5f8SJohn Marino /* Allocate and fill in dwarf_gdbarch_types for an arch.  */
49a45ae5f8SJohn Marino 
50a45ae5f8SJohn Marino static void *
dwarf_gdbarch_types_init(struct gdbarch * gdbarch)51a45ae5f8SJohn Marino dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
52a45ae5f8SJohn Marino {
53a45ae5f8SJohn Marino   struct dwarf_gdbarch_types *types
54a45ae5f8SJohn Marino     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
55a45ae5f8SJohn Marino 
56a45ae5f8SJohn Marino   /* The types themselves are lazily initialized.  */
57a45ae5f8SJohn Marino 
58a45ae5f8SJohn Marino   return types;
59a45ae5f8SJohn Marino }
60a45ae5f8SJohn Marino 
61a45ae5f8SJohn Marino /* Return the type used for DWARF operations where the type is
62a45ae5f8SJohn Marino    unspecified in the DWARF spec.  Only certain sizes are
63a45ae5f8SJohn Marino    supported.  */
64a45ae5f8SJohn Marino 
65a45ae5f8SJohn Marino static struct type *
dwarf_expr_address_type(struct dwarf_expr_context * ctx)66a45ae5f8SJohn Marino dwarf_expr_address_type (struct dwarf_expr_context *ctx)
67a45ae5f8SJohn Marino {
68a45ae5f8SJohn Marino   struct dwarf_gdbarch_types *types = gdbarch_data (ctx->gdbarch,
69a45ae5f8SJohn Marino 						    dwarf_arch_cookie);
70a45ae5f8SJohn Marino   int ndx;
71a45ae5f8SJohn Marino 
72a45ae5f8SJohn Marino   if (ctx->addr_size == 2)
73a45ae5f8SJohn Marino     ndx = 0;
74a45ae5f8SJohn Marino   else if (ctx->addr_size == 4)
75a45ae5f8SJohn Marino     ndx = 1;
76a45ae5f8SJohn Marino   else if (ctx->addr_size == 8)
77a45ae5f8SJohn Marino     ndx = 2;
78a45ae5f8SJohn Marino   else
79a45ae5f8SJohn Marino     error (_("Unsupported address size in DWARF expressions: %d bits"),
80a45ae5f8SJohn Marino 	   8 * ctx->addr_size);
81a45ae5f8SJohn Marino 
82a45ae5f8SJohn Marino   if (types->dw_types[ndx] == NULL)
83a45ae5f8SJohn Marino     types->dw_types[ndx]
84a45ae5f8SJohn Marino       = arch_integer_type (ctx->gdbarch,
85a45ae5f8SJohn Marino 			   8 * ctx->addr_size,
86a45ae5f8SJohn Marino 			   0, "<signed DWARF address type>");
87a45ae5f8SJohn Marino 
88a45ae5f8SJohn Marino   return types->dw_types[ndx];
89a45ae5f8SJohn Marino }
90a45ae5f8SJohn Marino 
915796c8dcSSimon Schubert /* Create a new context for the expression evaluator.  */
925796c8dcSSimon Schubert 
935796c8dcSSimon Schubert struct dwarf_expr_context *
new_dwarf_expr_context(void)945796c8dcSSimon Schubert new_dwarf_expr_context (void)
955796c8dcSSimon Schubert {
965796c8dcSSimon Schubert   struct dwarf_expr_context *retval;
97cf7f2e2dSJohn Marino 
985796c8dcSSimon Schubert   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
995796c8dcSSimon Schubert   retval->stack_len = 0;
1005796c8dcSSimon Schubert   retval->stack_allocated = 10;
101cf7f2e2dSJohn Marino   retval->stack = xmalloc (retval->stack_allocated
102cf7f2e2dSJohn Marino 			   * sizeof (struct dwarf_stack_value));
1035796c8dcSSimon Schubert   retval->num_pieces = 0;
1045796c8dcSSimon Schubert   retval->pieces = 0;
1055796c8dcSSimon Schubert   retval->max_recursion_depth = 0x100;
1065796c8dcSSimon Schubert   return retval;
1075796c8dcSSimon Schubert }
1085796c8dcSSimon Schubert 
1095796c8dcSSimon Schubert /* Release the memory allocated to CTX.  */
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert void
free_dwarf_expr_context(struct dwarf_expr_context * ctx)1125796c8dcSSimon Schubert free_dwarf_expr_context (struct dwarf_expr_context *ctx)
1135796c8dcSSimon Schubert {
1145796c8dcSSimon Schubert   xfree (ctx->stack);
1155796c8dcSSimon Schubert   xfree (ctx->pieces);
1165796c8dcSSimon Schubert   xfree (ctx);
1175796c8dcSSimon Schubert }
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert /* Helper for make_cleanup_free_dwarf_expr_context.  */
1205796c8dcSSimon Schubert 
1215796c8dcSSimon Schubert static void
free_dwarf_expr_context_cleanup(void * arg)1225796c8dcSSimon Schubert free_dwarf_expr_context_cleanup (void *arg)
1235796c8dcSSimon Schubert {
1245796c8dcSSimon Schubert   free_dwarf_expr_context (arg);
1255796c8dcSSimon Schubert }
1265796c8dcSSimon Schubert 
1275796c8dcSSimon Schubert /* Return a cleanup that calls free_dwarf_expr_context.  */
1285796c8dcSSimon Schubert 
1295796c8dcSSimon Schubert struct cleanup *
make_cleanup_free_dwarf_expr_context(struct dwarf_expr_context * ctx)1305796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
1315796c8dcSSimon Schubert {
1325796c8dcSSimon Schubert   return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
1335796c8dcSSimon Schubert }
1345796c8dcSSimon Schubert 
1355796c8dcSSimon Schubert /* Expand the memory allocated to CTX's stack to contain at least
1365796c8dcSSimon Schubert    NEED more elements than are currently used.  */
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert static void
dwarf_expr_grow_stack(struct dwarf_expr_context * ctx,size_t need)1395796c8dcSSimon Schubert dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
1405796c8dcSSimon Schubert {
1415796c8dcSSimon Schubert   if (ctx->stack_len + need > ctx->stack_allocated)
1425796c8dcSSimon Schubert     {
1435796c8dcSSimon Schubert       size_t newlen = ctx->stack_len + need + 10;
144cf7f2e2dSJohn Marino 
1455796c8dcSSimon Schubert       ctx->stack = xrealloc (ctx->stack,
1465796c8dcSSimon Schubert 			     newlen * sizeof (struct dwarf_stack_value));
1475796c8dcSSimon Schubert       ctx->stack_allocated = newlen;
1485796c8dcSSimon Schubert     }
1495796c8dcSSimon Schubert }
1505796c8dcSSimon Schubert 
1515796c8dcSSimon Schubert /* Push VALUE onto CTX's stack.  */
1525796c8dcSSimon Schubert 
153a45ae5f8SJohn Marino static void
dwarf_expr_push(struct dwarf_expr_context * ctx,struct value * value,int in_stack_memory)154a45ae5f8SJohn Marino dwarf_expr_push (struct dwarf_expr_context *ctx, struct value *value,
1555796c8dcSSimon Schubert 		 int in_stack_memory)
1565796c8dcSSimon Schubert {
1575796c8dcSSimon Schubert   struct dwarf_stack_value *v;
1585796c8dcSSimon Schubert 
1595796c8dcSSimon Schubert   dwarf_expr_grow_stack (ctx, 1);
1605796c8dcSSimon Schubert   v = &ctx->stack[ctx->stack_len++];
1615796c8dcSSimon Schubert   v->value = value;
1625796c8dcSSimon Schubert   v->in_stack_memory = in_stack_memory;
1635796c8dcSSimon Schubert }
1645796c8dcSSimon Schubert 
165a45ae5f8SJohn Marino /* Push VALUE onto CTX's stack.  */
1665796c8dcSSimon Schubert 
1675796c8dcSSimon Schubert void
dwarf_expr_push_address(struct dwarf_expr_context * ctx,CORE_ADDR value,int in_stack_memory)168a45ae5f8SJohn Marino dwarf_expr_push_address (struct dwarf_expr_context *ctx, CORE_ADDR value,
169a45ae5f8SJohn Marino 			 int in_stack_memory)
170a45ae5f8SJohn Marino {
171a45ae5f8SJohn Marino   dwarf_expr_push (ctx,
172a45ae5f8SJohn Marino 		   value_from_ulongest (dwarf_expr_address_type (ctx), value),
173a45ae5f8SJohn Marino 		   in_stack_memory);
174a45ae5f8SJohn Marino }
175a45ae5f8SJohn Marino 
176a45ae5f8SJohn Marino /* Pop the top item off of CTX's stack.  */
177a45ae5f8SJohn Marino 
178a45ae5f8SJohn Marino static void
dwarf_expr_pop(struct dwarf_expr_context * ctx)1795796c8dcSSimon Schubert dwarf_expr_pop (struct dwarf_expr_context *ctx)
1805796c8dcSSimon Schubert {
1815796c8dcSSimon Schubert   if (ctx->stack_len <= 0)
1825796c8dcSSimon Schubert     error (_("dwarf expression stack underflow"));
1835796c8dcSSimon Schubert   ctx->stack_len--;
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert /* Retrieve the N'th item on CTX's stack.  */
1875796c8dcSSimon Schubert 
188a45ae5f8SJohn Marino struct value *
dwarf_expr_fetch(struct dwarf_expr_context * ctx,int n)1895796c8dcSSimon Schubert dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
1905796c8dcSSimon Schubert {
1915796c8dcSSimon Schubert   if (ctx->stack_len <= n)
192c50c785cSJohn Marino      error (_("Asked for position %d of stack, "
193c50c785cSJohn Marino 	      "stack only has %d elements on it."),
1945796c8dcSSimon Schubert 	    n, ctx->stack_len);
1955796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].value;
196a45ae5f8SJohn Marino }
1975796c8dcSSimon Schubert 
198a45ae5f8SJohn Marino /* Require that TYPE be an integral type; throw an exception if not.  */
199a45ae5f8SJohn Marino 
200a45ae5f8SJohn Marino static void
dwarf_require_integral(struct type * type)201a45ae5f8SJohn Marino dwarf_require_integral (struct type *type)
202a45ae5f8SJohn Marino {
203a45ae5f8SJohn Marino   if (TYPE_CODE (type) != TYPE_CODE_INT
204a45ae5f8SJohn Marino       && TYPE_CODE (type) != TYPE_CODE_CHAR
205a45ae5f8SJohn Marino       && TYPE_CODE (type) != TYPE_CODE_BOOL)
206a45ae5f8SJohn Marino     error (_("integral type expected in DWARF expression"));
207a45ae5f8SJohn Marino }
208a45ae5f8SJohn Marino 
209a45ae5f8SJohn Marino /* Return the unsigned form of TYPE.  TYPE is necessarily an integral
210a45ae5f8SJohn Marino    type.  */
211a45ae5f8SJohn Marino 
212a45ae5f8SJohn Marino static struct type *
get_unsigned_type(struct gdbarch * gdbarch,struct type * type)213a45ae5f8SJohn Marino get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
214a45ae5f8SJohn Marino {
215a45ae5f8SJohn Marino   switch (TYPE_LENGTH (type))
216a45ae5f8SJohn Marino     {
217a45ae5f8SJohn Marino     case 1:
218a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_uint8;
219a45ae5f8SJohn Marino     case 2:
220a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_uint16;
221a45ae5f8SJohn Marino     case 4:
222a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_uint32;
223a45ae5f8SJohn Marino     case 8:
224a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_uint64;
225a45ae5f8SJohn Marino     default:
226a45ae5f8SJohn Marino       error (_("no unsigned variant found for type, while evaluating "
227a45ae5f8SJohn Marino 	       "DWARF expression"));
228a45ae5f8SJohn Marino     }
229a45ae5f8SJohn Marino }
230a45ae5f8SJohn Marino 
231a45ae5f8SJohn Marino /* Return the signed form of TYPE.  TYPE is necessarily an integral
232a45ae5f8SJohn Marino    type.  */
233a45ae5f8SJohn Marino 
234a45ae5f8SJohn Marino static struct type *
get_signed_type(struct gdbarch * gdbarch,struct type * type)235a45ae5f8SJohn Marino get_signed_type (struct gdbarch *gdbarch, struct type *type)
236a45ae5f8SJohn Marino {
237a45ae5f8SJohn Marino   switch (TYPE_LENGTH (type))
238a45ae5f8SJohn Marino     {
239a45ae5f8SJohn Marino     case 1:
240a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_int8;
241a45ae5f8SJohn Marino     case 2:
242a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_int16;
243a45ae5f8SJohn Marino     case 4:
244a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_int32;
245a45ae5f8SJohn Marino     case 8:
246a45ae5f8SJohn Marino       return builtin_type (gdbarch)->builtin_int64;
247a45ae5f8SJohn Marino     default:
248a45ae5f8SJohn Marino       error (_("no signed variant found for type, while evaluating "
249a45ae5f8SJohn Marino 	       "DWARF expression"));
250a45ae5f8SJohn Marino     }
2515796c8dcSSimon Schubert }
2525796c8dcSSimon Schubert 
253cf7f2e2dSJohn Marino /* Retrieve the N'th item on CTX's stack, converted to an address.  */
254cf7f2e2dSJohn Marino 
255cf7f2e2dSJohn Marino CORE_ADDR
dwarf_expr_fetch_address(struct dwarf_expr_context * ctx,int n)256cf7f2e2dSJohn Marino dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
257cf7f2e2dSJohn Marino {
258a45ae5f8SJohn Marino   struct value *result_val = dwarf_expr_fetch (ctx, n);
259a45ae5f8SJohn Marino   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
260a45ae5f8SJohn Marino   ULONGEST result;
261a45ae5f8SJohn Marino 
262a45ae5f8SJohn Marino   dwarf_require_integral (value_type (result_val));
263a45ae5f8SJohn Marino   result = extract_unsigned_integer (value_contents (result_val),
264a45ae5f8SJohn Marino 				     TYPE_LENGTH (value_type (result_val)),
265a45ae5f8SJohn Marino 				     byte_order);
266cf7f2e2dSJohn Marino 
267cf7f2e2dSJohn Marino   /* For most architectures, calling extract_unsigned_integer() alone
268cf7f2e2dSJohn Marino      is sufficient for extracting an address.  However, some
269cf7f2e2dSJohn Marino      architectures (e.g. MIPS) use signed addresses and using
270cf7f2e2dSJohn Marino      extract_unsigned_integer() will not produce a correct
271cf7f2e2dSJohn Marino      result.  Make sure we invoke gdbarch_integer_to_address()
272cf7f2e2dSJohn Marino      for those architectures which require it.  */
273cf7f2e2dSJohn Marino   if (gdbarch_integer_to_address_p (ctx->gdbarch))
274cf7f2e2dSJohn Marino     {
275cf7f2e2dSJohn Marino       gdb_byte *buf = alloca (ctx->addr_size);
276a45ae5f8SJohn Marino       struct type *int_type = get_unsigned_type (ctx->gdbarch,
277a45ae5f8SJohn Marino 						 value_type (result_val));
278cf7f2e2dSJohn Marino 
279cf7f2e2dSJohn Marino       store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
280cf7f2e2dSJohn Marino       return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
281cf7f2e2dSJohn Marino     }
282cf7f2e2dSJohn Marino 
283cf7f2e2dSJohn Marino   return (CORE_ADDR) result;
284cf7f2e2dSJohn Marino }
285cf7f2e2dSJohn Marino 
2865796c8dcSSimon Schubert /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack.  */
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert int
dwarf_expr_fetch_in_stack_memory(struct dwarf_expr_context * ctx,int n)2895796c8dcSSimon Schubert dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert   if (ctx->stack_len <= n)
292c50c785cSJohn Marino      error (_("Asked for position %d of stack, "
293c50c785cSJohn Marino 	      "stack only has %d elements on it."),
2945796c8dcSSimon Schubert 	    n, ctx->stack_len);
2955796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
2965796c8dcSSimon Schubert }
2975796c8dcSSimon Schubert 
298cf7f2e2dSJohn Marino /* Return true if the expression stack is empty.  */
299cf7f2e2dSJohn Marino 
300cf7f2e2dSJohn Marino static int
dwarf_expr_stack_empty_p(struct dwarf_expr_context * ctx)301cf7f2e2dSJohn Marino dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
302cf7f2e2dSJohn Marino {
303cf7f2e2dSJohn Marino   return ctx->stack_len == 0;
304cf7f2e2dSJohn Marino }
305cf7f2e2dSJohn Marino 
3065796c8dcSSimon Schubert /* Add a new piece to CTX's piece list.  */
3075796c8dcSSimon Schubert static void
add_piece(struct dwarf_expr_context * ctx,ULONGEST size,ULONGEST offset)308cf7f2e2dSJohn Marino add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
3095796c8dcSSimon Schubert {
3105796c8dcSSimon Schubert   struct dwarf_expr_piece *p;
3115796c8dcSSimon Schubert 
3125796c8dcSSimon Schubert   ctx->num_pieces++;
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert   ctx->pieces = xrealloc (ctx->pieces,
3155796c8dcSSimon Schubert 			  (ctx->num_pieces
3165796c8dcSSimon Schubert 			   * sizeof (struct dwarf_expr_piece)));
3175796c8dcSSimon Schubert 
3185796c8dcSSimon Schubert   p = &ctx->pieces[ctx->num_pieces - 1];
3195796c8dcSSimon Schubert   p->location = ctx->location;
3205796c8dcSSimon Schubert   p->size = size;
321cf7f2e2dSJohn Marino   p->offset = offset;
322cf7f2e2dSJohn Marino 
3235796c8dcSSimon Schubert   if (p->location == DWARF_VALUE_LITERAL)
3245796c8dcSSimon Schubert     {
3255796c8dcSSimon Schubert       p->v.literal.data = ctx->data;
3265796c8dcSSimon Schubert       p->v.literal.length = ctx->len;
3275796c8dcSSimon Schubert     }
328cf7f2e2dSJohn Marino   else if (dwarf_expr_stack_empty_p (ctx))
329cf7f2e2dSJohn Marino     {
330cf7f2e2dSJohn Marino       p->location = DWARF_VALUE_OPTIMIZED_OUT;
331cf7f2e2dSJohn Marino       /* Also reset the context's location, for our callers.  This is
332cf7f2e2dSJohn Marino 	 a somewhat strange approach, but this lets us avoid setting
333cf7f2e2dSJohn Marino 	 the location to DWARF_VALUE_MEMORY in all the individual
334cf7f2e2dSJohn Marino 	 cases in the evaluator.  */
335cf7f2e2dSJohn Marino       ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
336cf7f2e2dSJohn Marino     }
337cf7f2e2dSJohn Marino   else if (p->location == DWARF_VALUE_MEMORY)
338cf7f2e2dSJohn Marino     {
339cf7f2e2dSJohn Marino       p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
340cf7f2e2dSJohn Marino       p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
341cf7f2e2dSJohn Marino     }
342c50c785cSJohn Marino   else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
343c50c785cSJohn Marino     {
344*ef5ccd6cSJohn Marino       p->v.ptr.die.sect_off = ctx->len;
345a45ae5f8SJohn Marino       p->v.ptr.offset = value_as_long (dwarf_expr_fetch (ctx, 0));
346c50c785cSJohn Marino     }
347a45ae5f8SJohn Marino   else if (p->location == DWARF_VALUE_REGISTER)
348a45ae5f8SJohn Marino     p->v.regno = value_as_long (dwarf_expr_fetch (ctx, 0));
3495796c8dcSSimon Schubert   else
3505796c8dcSSimon Schubert     {
351cf7f2e2dSJohn Marino       p->v.value = dwarf_expr_fetch (ctx, 0);
3525796c8dcSSimon Schubert     }
3535796c8dcSSimon Schubert }
3545796c8dcSSimon Schubert 
3555796c8dcSSimon Schubert /* Evaluate the expression at ADDR (LEN bytes long) using the context
3565796c8dcSSimon Schubert    CTX.  */
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert void
dwarf_expr_eval(struct dwarf_expr_context * ctx,const gdb_byte * addr,size_t len)359cf7f2e2dSJohn Marino dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
360cf7f2e2dSJohn Marino 		 size_t len)
3615796c8dcSSimon Schubert {
3625796c8dcSSimon Schubert   int old_recursion_depth = ctx->recursion_depth;
3635796c8dcSSimon Schubert 
3645796c8dcSSimon Schubert   execute_stack_op (ctx, addr, addr + len);
3655796c8dcSSimon Schubert 
3665796c8dcSSimon Schubert   /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here.  */
3675796c8dcSSimon Schubert 
3685796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth == old_recursion_depth);
3695796c8dcSSimon Schubert }
3705796c8dcSSimon Schubert 
371*ef5ccd6cSJohn Marino /* Helper to read a uleb128 value or throw an error.  */
3725796c8dcSSimon Schubert 
373cf7f2e2dSJohn Marino const gdb_byte *
safe_read_uleb128(const gdb_byte * buf,const gdb_byte * buf_end,uint64_t * r)374*ef5ccd6cSJohn Marino safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
375*ef5ccd6cSJohn Marino 		   uint64_t *r)
3765796c8dcSSimon Schubert {
377*ef5ccd6cSJohn Marino   buf = gdb_read_uleb128 (buf, buf_end, r);
378*ef5ccd6cSJohn Marino   if (buf == NULL)
379*ef5ccd6cSJohn Marino     error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
3805796c8dcSSimon Schubert   return buf;
3815796c8dcSSimon Schubert }
3825796c8dcSSimon Schubert 
383*ef5ccd6cSJohn Marino /* Helper to read a sleb128 value or throw an error.  */
3845796c8dcSSimon Schubert 
385cf7f2e2dSJohn Marino const gdb_byte *
safe_read_sleb128(const gdb_byte * buf,const gdb_byte * buf_end,int64_t * r)386*ef5ccd6cSJohn Marino safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
387*ef5ccd6cSJohn Marino 		   int64_t *r)
3885796c8dcSSimon Schubert {
389*ef5ccd6cSJohn Marino   buf = gdb_read_sleb128 (buf, buf_end, r);
390*ef5ccd6cSJohn Marino   if (buf == NULL)
391*ef5ccd6cSJohn Marino     error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
392*ef5ccd6cSJohn Marino   return buf;
3935796c8dcSSimon Schubert }
3945796c8dcSSimon Schubert 
395*ef5ccd6cSJohn Marino const gdb_byte *
safe_skip_leb128(const gdb_byte * buf,const gdb_byte * buf_end)396*ef5ccd6cSJohn Marino safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
397*ef5ccd6cSJohn Marino {
398*ef5ccd6cSJohn Marino   buf = gdb_skip_leb128 (buf, buf_end);
399*ef5ccd6cSJohn Marino   if (buf == NULL)
400*ef5ccd6cSJohn Marino     error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
4015796c8dcSSimon Schubert   return buf;
4025796c8dcSSimon Schubert }
4035796c8dcSSimon Schubert 
4045796c8dcSSimon Schubert 
4055796c8dcSSimon Schubert /* Check that the current operator is either at the end of an
4065796c8dcSSimon Schubert    expression, or that it is followed by a composition operator.  */
4075796c8dcSSimon Schubert 
408cf7f2e2dSJohn Marino void
dwarf_expr_require_composition(const gdb_byte * op_ptr,const gdb_byte * op_end,const char * op_name)409cf7f2e2dSJohn Marino dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
410cf7f2e2dSJohn Marino 				const char *op_name)
4115796c8dcSSimon Schubert {
4125796c8dcSSimon Schubert   /* It seems like DW_OP_GNU_uninit should be handled here.  However,
4135796c8dcSSimon Schubert      it doesn't seem to make sense for DW_OP_*_value, and it was not
4145796c8dcSSimon Schubert      checked at the other place that this function is called.  */
4155796c8dcSSimon Schubert   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
4165796c8dcSSimon Schubert     error (_("DWARF-2 expression error: `%s' operations must be "
417a45ae5f8SJohn Marino 	     "used either alone or in conjunction with DW_OP_piece "
4185796c8dcSSimon Schubert 	     "or DW_OP_bit_piece."),
4195796c8dcSSimon Schubert 	   op_name);
4205796c8dcSSimon Schubert }
4215796c8dcSSimon Schubert 
422a45ae5f8SJohn Marino /* Return true iff the types T1 and T2 are "the same".  This only does
423a45ae5f8SJohn Marino    checks that might reasonably be needed to compare DWARF base
424a45ae5f8SJohn Marino    types.  */
425a45ae5f8SJohn Marino 
426a45ae5f8SJohn Marino static int
base_types_equal_p(struct type * t1,struct type * t2)427a45ae5f8SJohn Marino base_types_equal_p (struct type *t1, struct type *t2)
428a45ae5f8SJohn Marino {
429a45ae5f8SJohn Marino   if (TYPE_CODE (t1) != TYPE_CODE (t2))
430a45ae5f8SJohn Marino     return 0;
431a45ae5f8SJohn Marino   if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
432a45ae5f8SJohn Marino     return 0;
433a45ae5f8SJohn Marino   return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
434a45ae5f8SJohn Marino }
435a45ae5f8SJohn Marino 
436a45ae5f8SJohn Marino /* A convenience function to call get_base_type on CTX and return the
437a45ae5f8SJohn Marino    result.  DIE is the DIE whose type we need.  SIZE is non-zero if
438a45ae5f8SJohn Marino    this function should verify that the resulting type has the correct
439a45ae5f8SJohn Marino    size.  */
440a45ae5f8SJohn Marino 
441a45ae5f8SJohn Marino static struct type *
dwarf_get_base_type(struct dwarf_expr_context * ctx,cu_offset die,int size)442*ef5ccd6cSJohn Marino dwarf_get_base_type (struct dwarf_expr_context *ctx, cu_offset die, int size)
443a45ae5f8SJohn Marino {
444a45ae5f8SJohn Marino   struct type *result;
445a45ae5f8SJohn Marino 
446a45ae5f8SJohn Marino   if (ctx->funcs->get_base_type)
447a45ae5f8SJohn Marino     {
448a45ae5f8SJohn Marino       result = ctx->funcs->get_base_type (ctx, die);
449a45ae5f8SJohn Marino       if (result == NULL)
450a45ae5f8SJohn Marino 	error (_("Could not find type for DW_OP_GNU_const_type"));
451a45ae5f8SJohn Marino       if (size != 0 && TYPE_LENGTH (result) != size)
452a45ae5f8SJohn Marino 	error (_("DW_OP_GNU_const_type has different sizes for type and data"));
453a45ae5f8SJohn Marino     }
454a45ae5f8SJohn Marino   else
455a45ae5f8SJohn Marino     /* Anything will do.  */
456a45ae5f8SJohn Marino     result = builtin_type (ctx->gdbarch)->builtin_int;
457a45ae5f8SJohn Marino 
458a45ae5f8SJohn Marino   return result;
459a45ae5f8SJohn Marino }
460a45ae5f8SJohn Marino 
461a45ae5f8SJohn Marino /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
462a45ae5f8SJohn Marino    DWARF register number.  Otherwise return -1.  */
463a45ae5f8SJohn Marino 
464a45ae5f8SJohn Marino int
dwarf_block_to_dwarf_reg(const gdb_byte * buf,const gdb_byte * buf_end)465a45ae5f8SJohn Marino dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
466a45ae5f8SJohn Marino {
467*ef5ccd6cSJohn Marino   uint64_t dwarf_reg;
468a45ae5f8SJohn Marino 
469a45ae5f8SJohn Marino   if (buf_end <= buf)
470a45ae5f8SJohn Marino     return -1;
471a45ae5f8SJohn Marino   if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
472a45ae5f8SJohn Marino     {
473a45ae5f8SJohn Marino       if (buf_end - buf != 1)
474a45ae5f8SJohn Marino 	return -1;
475a45ae5f8SJohn Marino       return *buf - DW_OP_reg0;
476a45ae5f8SJohn Marino     }
477a45ae5f8SJohn Marino 
478a45ae5f8SJohn Marino   if (*buf == DW_OP_GNU_regval_type)
479a45ae5f8SJohn Marino     {
480a45ae5f8SJohn Marino       buf++;
481*ef5ccd6cSJohn Marino       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
482*ef5ccd6cSJohn Marino       if (buf == NULL)
483*ef5ccd6cSJohn Marino 	return -1;
484*ef5ccd6cSJohn Marino       buf = gdb_skip_leb128 (buf, buf_end);
485*ef5ccd6cSJohn Marino       if (buf == NULL)
486*ef5ccd6cSJohn Marino 	return -1;
487a45ae5f8SJohn Marino     }
488a45ae5f8SJohn Marino   else if (*buf == DW_OP_regx)
489a45ae5f8SJohn Marino     {
490a45ae5f8SJohn Marino       buf++;
491*ef5ccd6cSJohn Marino       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
492*ef5ccd6cSJohn Marino       if (buf == NULL)
493*ef5ccd6cSJohn Marino 	return -1;
494a45ae5f8SJohn Marino     }
495a45ae5f8SJohn Marino   else
496a45ae5f8SJohn Marino     return -1;
497a45ae5f8SJohn Marino   if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
498a45ae5f8SJohn Marino     return -1;
499a45ae5f8SJohn Marino   return dwarf_reg;
500a45ae5f8SJohn Marino }
501a45ae5f8SJohn Marino 
502a45ae5f8SJohn Marino /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
503a45ae5f8SJohn Marino    DW_OP_deref* return the DWARF register number.  Otherwise return -1.
504a45ae5f8SJohn Marino    DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
505a45ae5f8SJohn Marino    size from DW_OP_deref_size.  */
506a45ae5f8SJohn Marino 
507a45ae5f8SJohn Marino int
dwarf_block_to_dwarf_reg_deref(const gdb_byte * buf,const gdb_byte * buf_end,CORE_ADDR * deref_size_return)508a45ae5f8SJohn Marino dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
509a45ae5f8SJohn Marino 				CORE_ADDR *deref_size_return)
510a45ae5f8SJohn Marino {
511*ef5ccd6cSJohn Marino   uint64_t dwarf_reg;
512*ef5ccd6cSJohn Marino   int64_t offset;
513a45ae5f8SJohn Marino 
514a45ae5f8SJohn Marino   if (buf_end <= buf)
515a45ae5f8SJohn Marino     return -1;
516*ef5ccd6cSJohn Marino 
517a45ae5f8SJohn Marino   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
518a45ae5f8SJohn Marino     {
519a45ae5f8SJohn Marino       dwarf_reg = *buf - DW_OP_breg0;
520a45ae5f8SJohn Marino       buf++;
521*ef5ccd6cSJohn Marino       if (buf >= buf_end)
522*ef5ccd6cSJohn Marino 	return -1;
523a45ae5f8SJohn Marino     }
524a45ae5f8SJohn Marino   else if (*buf == DW_OP_bregx)
525a45ae5f8SJohn Marino     {
526a45ae5f8SJohn Marino       buf++;
527*ef5ccd6cSJohn Marino       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
528*ef5ccd6cSJohn Marino       if (buf == NULL)
529*ef5ccd6cSJohn Marino 	return -1;
530a45ae5f8SJohn Marino       if ((int) dwarf_reg != dwarf_reg)
531a45ae5f8SJohn Marino        return -1;
532a45ae5f8SJohn Marino     }
533a45ae5f8SJohn Marino   else
534a45ae5f8SJohn Marino     return -1;
535a45ae5f8SJohn Marino 
536*ef5ccd6cSJohn Marino   buf = gdb_read_sleb128 (buf, buf_end, &offset);
537*ef5ccd6cSJohn Marino   if (buf == NULL)
538a45ae5f8SJohn Marino     return -1;
539*ef5ccd6cSJohn Marino   if (offset != 0)
540a45ae5f8SJohn Marino     return -1;
541a45ae5f8SJohn Marino 
542a45ae5f8SJohn Marino   if (*buf == DW_OP_deref)
543a45ae5f8SJohn Marino     {
544a45ae5f8SJohn Marino       buf++;
545a45ae5f8SJohn Marino       *deref_size_return = -1;
546a45ae5f8SJohn Marino     }
547a45ae5f8SJohn Marino   else if (*buf == DW_OP_deref_size)
548a45ae5f8SJohn Marino     {
549a45ae5f8SJohn Marino       buf++;
550a45ae5f8SJohn Marino       if (buf >= buf_end)
551a45ae5f8SJohn Marino        return -1;
552a45ae5f8SJohn Marino       *deref_size_return = *buf++;
553a45ae5f8SJohn Marino     }
554a45ae5f8SJohn Marino   else
555a45ae5f8SJohn Marino     return -1;
556a45ae5f8SJohn Marino 
557a45ae5f8SJohn Marino   if (buf != buf_end)
558a45ae5f8SJohn Marino     return -1;
559a45ae5f8SJohn Marino 
560a45ae5f8SJohn Marino   return dwarf_reg;
561a45ae5f8SJohn Marino }
562a45ae5f8SJohn Marino 
563a45ae5f8SJohn Marino /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
564a45ae5f8SJohn Marino    in FB_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.  */
565a45ae5f8SJohn Marino 
566a45ae5f8SJohn Marino int
dwarf_block_to_fb_offset(const gdb_byte * buf,const gdb_byte * buf_end,CORE_ADDR * fb_offset_return)567a45ae5f8SJohn Marino dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
568a45ae5f8SJohn Marino 			  CORE_ADDR *fb_offset_return)
569a45ae5f8SJohn Marino {
570*ef5ccd6cSJohn Marino   int64_t fb_offset;
571a45ae5f8SJohn Marino 
572a45ae5f8SJohn Marino   if (buf_end <= buf)
573a45ae5f8SJohn Marino     return 0;
574a45ae5f8SJohn Marino 
575a45ae5f8SJohn Marino   if (*buf != DW_OP_fbreg)
576a45ae5f8SJohn Marino     return 0;
577a45ae5f8SJohn Marino   buf++;
578a45ae5f8SJohn Marino 
579*ef5ccd6cSJohn Marino   buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
580*ef5ccd6cSJohn Marino   if (buf == NULL)
581*ef5ccd6cSJohn Marino     return 0;
582a45ae5f8SJohn Marino   *fb_offset_return = fb_offset;
583a45ae5f8SJohn Marino   if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
584a45ae5f8SJohn Marino     return 0;
585a45ae5f8SJohn Marino 
586a45ae5f8SJohn Marino   return 1;
587a45ae5f8SJohn Marino }
588a45ae5f8SJohn Marino 
589a45ae5f8SJohn Marino /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
590a45ae5f8SJohn Marino    in SP_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.
591a45ae5f8SJohn Marino    The matched SP register number depends on GDBARCH.  */
592a45ae5f8SJohn Marino 
593a45ae5f8SJohn Marino int
dwarf_block_to_sp_offset(struct gdbarch * gdbarch,const gdb_byte * buf,const gdb_byte * buf_end,CORE_ADDR * sp_offset_return)594a45ae5f8SJohn Marino dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
595a45ae5f8SJohn Marino 			  const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
596a45ae5f8SJohn Marino {
597*ef5ccd6cSJohn Marino   uint64_t dwarf_reg;
598*ef5ccd6cSJohn Marino   int64_t sp_offset;
599a45ae5f8SJohn Marino 
600a45ae5f8SJohn Marino   if (buf_end <= buf)
601a45ae5f8SJohn Marino     return 0;
602a45ae5f8SJohn Marino   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
603a45ae5f8SJohn Marino     {
604a45ae5f8SJohn Marino       dwarf_reg = *buf - DW_OP_breg0;
605a45ae5f8SJohn Marino       buf++;
606a45ae5f8SJohn Marino     }
607a45ae5f8SJohn Marino   else
608a45ae5f8SJohn Marino     {
609a45ae5f8SJohn Marino       if (*buf != DW_OP_bregx)
610a45ae5f8SJohn Marino        return 0;
611a45ae5f8SJohn Marino       buf++;
612*ef5ccd6cSJohn Marino       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
613*ef5ccd6cSJohn Marino       if (buf == NULL)
614*ef5ccd6cSJohn Marino 	return 0;
615a45ae5f8SJohn Marino     }
616a45ae5f8SJohn Marino 
617a45ae5f8SJohn Marino   if (gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_reg)
618a45ae5f8SJohn Marino       != gdbarch_sp_regnum (gdbarch))
619a45ae5f8SJohn Marino     return 0;
620a45ae5f8SJohn Marino 
621*ef5ccd6cSJohn Marino   buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
622*ef5ccd6cSJohn Marino   if (buf == NULL)
623*ef5ccd6cSJohn Marino     return 0;
624a45ae5f8SJohn Marino   *sp_offset_return = sp_offset;
625a45ae5f8SJohn Marino   if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
626a45ae5f8SJohn Marino     return 0;
627a45ae5f8SJohn Marino 
628a45ae5f8SJohn Marino   return 1;
629a45ae5f8SJohn Marino }
630a45ae5f8SJohn Marino 
6315796c8dcSSimon Schubert /* The engine for the expression evaluator.  Using the context in CTX,
6325796c8dcSSimon Schubert    evaluate the expression between OP_PTR and OP_END.  */
6335796c8dcSSimon Schubert 
6345796c8dcSSimon Schubert static void
execute_stack_op(struct dwarf_expr_context * ctx,const gdb_byte * op_ptr,const gdb_byte * op_end)6355796c8dcSSimon Schubert execute_stack_op (struct dwarf_expr_context *ctx,
636cf7f2e2dSJohn Marino 		  const gdb_byte *op_ptr, const gdb_byte *op_end)
6375796c8dcSSimon Schubert {
6385796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
639a45ae5f8SJohn Marino   /* Old-style "untyped" DWARF values need special treatment in a
640a45ae5f8SJohn Marino      couple of places, specifically DW_OP_mod and DW_OP_shr.  We need
641a45ae5f8SJohn Marino      a special type for these values so we can distinguish them from
642a45ae5f8SJohn Marino      values that have an explicit type, because explicitly-typed
643a45ae5f8SJohn Marino      values do not need special treatment.  This special type must be
644a45ae5f8SJohn Marino      different (in the `==' sense) from any base type coming from the
645a45ae5f8SJohn Marino      CU.  */
646a45ae5f8SJohn Marino   struct type *address_type = dwarf_expr_address_type (ctx);
647cf7f2e2dSJohn Marino 
6485796c8dcSSimon Schubert   ctx->location = DWARF_VALUE_MEMORY;
6495796c8dcSSimon Schubert   ctx->initialized = 1;  /* Default is initialized.  */
6505796c8dcSSimon Schubert 
6515796c8dcSSimon Schubert   if (ctx->recursion_depth > ctx->max_recursion_depth)
6525796c8dcSSimon Schubert     error (_("DWARF-2 expression error: Loop detected (%d)."),
6535796c8dcSSimon Schubert 	   ctx->recursion_depth);
6545796c8dcSSimon Schubert   ctx->recursion_depth++;
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert   while (op_ptr < op_end)
6575796c8dcSSimon Schubert     {
6585796c8dcSSimon Schubert       enum dwarf_location_atom op = *op_ptr++;
659cf7f2e2dSJohn Marino       ULONGEST result;
6605796c8dcSSimon Schubert       /* Assume the value is not in stack memory.
6615796c8dcSSimon Schubert 	 Code that knows otherwise sets this to 1.
6625796c8dcSSimon Schubert 	 Some arithmetic on stack addresses can probably be assumed to still
6635796c8dcSSimon Schubert 	 be a stack address, but we skip this complication for now.
6645796c8dcSSimon Schubert 	 This is just an optimization, so it's always ok to punt
6655796c8dcSSimon Schubert 	 and leave this as 0.  */
6665796c8dcSSimon Schubert       int in_stack_memory = 0;
667*ef5ccd6cSJohn Marino       uint64_t uoffset, reg;
668*ef5ccd6cSJohn Marino       int64_t offset;
669a45ae5f8SJohn Marino       struct value *result_val = NULL;
670a45ae5f8SJohn Marino 
671a45ae5f8SJohn Marino       /* The DWARF expression might have a bug causing an infinite
672a45ae5f8SJohn Marino 	 loop.  In that case, quitting is the only way out.  */
673a45ae5f8SJohn Marino       QUIT;
6745796c8dcSSimon Schubert 
6755796c8dcSSimon Schubert       switch (op)
6765796c8dcSSimon Schubert 	{
6775796c8dcSSimon Schubert 	case DW_OP_lit0:
6785796c8dcSSimon Schubert 	case DW_OP_lit1:
6795796c8dcSSimon Schubert 	case DW_OP_lit2:
6805796c8dcSSimon Schubert 	case DW_OP_lit3:
6815796c8dcSSimon Schubert 	case DW_OP_lit4:
6825796c8dcSSimon Schubert 	case DW_OP_lit5:
6835796c8dcSSimon Schubert 	case DW_OP_lit6:
6845796c8dcSSimon Schubert 	case DW_OP_lit7:
6855796c8dcSSimon Schubert 	case DW_OP_lit8:
6865796c8dcSSimon Schubert 	case DW_OP_lit9:
6875796c8dcSSimon Schubert 	case DW_OP_lit10:
6885796c8dcSSimon Schubert 	case DW_OP_lit11:
6895796c8dcSSimon Schubert 	case DW_OP_lit12:
6905796c8dcSSimon Schubert 	case DW_OP_lit13:
6915796c8dcSSimon Schubert 	case DW_OP_lit14:
6925796c8dcSSimon Schubert 	case DW_OP_lit15:
6935796c8dcSSimon Schubert 	case DW_OP_lit16:
6945796c8dcSSimon Schubert 	case DW_OP_lit17:
6955796c8dcSSimon Schubert 	case DW_OP_lit18:
6965796c8dcSSimon Schubert 	case DW_OP_lit19:
6975796c8dcSSimon Schubert 	case DW_OP_lit20:
6985796c8dcSSimon Schubert 	case DW_OP_lit21:
6995796c8dcSSimon Schubert 	case DW_OP_lit22:
7005796c8dcSSimon Schubert 	case DW_OP_lit23:
7015796c8dcSSimon Schubert 	case DW_OP_lit24:
7025796c8dcSSimon Schubert 	case DW_OP_lit25:
7035796c8dcSSimon Schubert 	case DW_OP_lit26:
7045796c8dcSSimon Schubert 	case DW_OP_lit27:
7055796c8dcSSimon Schubert 	case DW_OP_lit28:
7065796c8dcSSimon Schubert 	case DW_OP_lit29:
7075796c8dcSSimon Schubert 	case DW_OP_lit30:
7085796c8dcSSimon Schubert 	case DW_OP_lit31:
7095796c8dcSSimon Schubert 	  result = op - DW_OP_lit0;
710a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7115796c8dcSSimon Schubert 	  break;
7125796c8dcSSimon Schubert 
7135796c8dcSSimon Schubert 	case DW_OP_addr:
714cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr,
715cf7f2e2dSJohn Marino 					     ctx->addr_size, byte_order);
7165796c8dcSSimon Schubert 	  op_ptr += ctx->addr_size;
717cf7f2e2dSJohn Marino 	  /* Some versions of GCC emit DW_OP_addr before
718cf7f2e2dSJohn Marino 	     DW_OP_GNU_push_tls_address.  In this case the value is an
719cf7f2e2dSJohn Marino 	     index, not an address.  We don't support things like
720cf7f2e2dSJohn Marino 	     branching between the address and the TLS op.  */
721cf7f2e2dSJohn Marino 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
722cf7f2e2dSJohn Marino 	    result += ctx->offset;
723a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7245796c8dcSSimon Schubert 	  break;
7255796c8dcSSimon Schubert 
726*ef5ccd6cSJohn Marino 	case DW_OP_GNU_addr_index:
727*ef5ccd6cSJohn Marino 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
728*ef5ccd6cSJohn Marino 	  result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
729*ef5ccd6cSJohn Marino 	  result += ctx->offset;
730*ef5ccd6cSJohn Marino 	  result_val = value_from_ulongest (address_type, result);
731*ef5ccd6cSJohn Marino 	  break;
732*ef5ccd6cSJohn Marino 	case DW_OP_GNU_const_index:
733*ef5ccd6cSJohn Marino 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
734*ef5ccd6cSJohn Marino 	  result = (ctx->funcs->get_addr_index) (ctx->baton, uoffset);
735*ef5ccd6cSJohn Marino 	  result_val = value_from_ulongest (address_type, result);
736*ef5ccd6cSJohn Marino 	  break;
737*ef5ccd6cSJohn Marino 
7385796c8dcSSimon Schubert 	case DW_OP_const1u:
7395796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 1, byte_order);
740a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7415796c8dcSSimon Schubert 	  op_ptr += 1;
7425796c8dcSSimon Schubert 	  break;
7435796c8dcSSimon Schubert 	case DW_OP_const1s:
7445796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 1, byte_order);
745a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7465796c8dcSSimon Schubert 	  op_ptr += 1;
7475796c8dcSSimon Schubert 	  break;
7485796c8dcSSimon Schubert 	case DW_OP_const2u:
7495796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 2, byte_order);
750a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7515796c8dcSSimon Schubert 	  op_ptr += 2;
7525796c8dcSSimon Schubert 	  break;
7535796c8dcSSimon Schubert 	case DW_OP_const2s:
7545796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 2, byte_order);
755a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7565796c8dcSSimon Schubert 	  op_ptr += 2;
7575796c8dcSSimon Schubert 	  break;
7585796c8dcSSimon Schubert 	case DW_OP_const4u:
7595796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 4, byte_order);
760a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7615796c8dcSSimon Schubert 	  op_ptr += 4;
7625796c8dcSSimon Schubert 	  break;
7635796c8dcSSimon Schubert 	case DW_OP_const4s:
7645796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 4, byte_order);
765a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7665796c8dcSSimon Schubert 	  op_ptr += 4;
7675796c8dcSSimon Schubert 	  break;
7685796c8dcSSimon Schubert 	case DW_OP_const8u:
7695796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 8, byte_order);
770a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7715796c8dcSSimon Schubert 	  op_ptr += 8;
7725796c8dcSSimon Schubert 	  break;
7735796c8dcSSimon Schubert 	case DW_OP_const8s:
7745796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 8, byte_order);
775a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7765796c8dcSSimon Schubert 	  op_ptr += 8;
7775796c8dcSSimon Schubert 	  break;
7785796c8dcSSimon Schubert 	case DW_OP_constu:
779*ef5ccd6cSJohn Marino 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
7805796c8dcSSimon Schubert 	  result = uoffset;
781a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7825796c8dcSSimon Schubert 	  break;
7835796c8dcSSimon Schubert 	case DW_OP_consts:
784*ef5ccd6cSJohn Marino 	  op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
7855796c8dcSSimon Schubert 	  result = offset;
786a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
7875796c8dcSSimon Schubert 	  break;
7885796c8dcSSimon Schubert 
7895796c8dcSSimon Schubert 	/* The DW_OP_reg operations are required to occur alone in
7905796c8dcSSimon Schubert 	   location expressions.  */
7915796c8dcSSimon Schubert 	case DW_OP_reg0:
7925796c8dcSSimon Schubert 	case DW_OP_reg1:
7935796c8dcSSimon Schubert 	case DW_OP_reg2:
7945796c8dcSSimon Schubert 	case DW_OP_reg3:
7955796c8dcSSimon Schubert 	case DW_OP_reg4:
7965796c8dcSSimon Schubert 	case DW_OP_reg5:
7975796c8dcSSimon Schubert 	case DW_OP_reg6:
7985796c8dcSSimon Schubert 	case DW_OP_reg7:
7995796c8dcSSimon Schubert 	case DW_OP_reg8:
8005796c8dcSSimon Schubert 	case DW_OP_reg9:
8015796c8dcSSimon Schubert 	case DW_OP_reg10:
8025796c8dcSSimon Schubert 	case DW_OP_reg11:
8035796c8dcSSimon Schubert 	case DW_OP_reg12:
8045796c8dcSSimon Schubert 	case DW_OP_reg13:
8055796c8dcSSimon Schubert 	case DW_OP_reg14:
8065796c8dcSSimon Schubert 	case DW_OP_reg15:
8075796c8dcSSimon Schubert 	case DW_OP_reg16:
8085796c8dcSSimon Schubert 	case DW_OP_reg17:
8095796c8dcSSimon Schubert 	case DW_OP_reg18:
8105796c8dcSSimon Schubert 	case DW_OP_reg19:
8115796c8dcSSimon Schubert 	case DW_OP_reg20:
8125796c8dcSSimon Schubert 	case DW_OP_reg21:
8135796c8dcSSimon Schubert 	case DW_OP_reg22:
8145796c8dcSSimon Schubert 	case DW_OP_reg23:
8155796c8dcSSimon Schubert 	case DW_OP_reg24:
8165796c8dcSSimon Schubert 	case DW_OP_reg25:
8175796c8dcSSimon Schubert 	case DW_OP_reg26:
8185796c8dcSSimon Schubert 	case DW_OP_reg27:
8195796c8dcSSimon Schubert 	case DW_OP_reg28:
8205796c8dcSSimon Schubert 	case DW_OP_reg29:
8215796c8dcSSimon Schubert 	case DW_OP_reg30:
8225796c8dcSSimon Schubert 	case DW_OP_reg31:
8235796c8dcSSimon Schubert 	  if (op_ptr != op_end
8245796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_piece
825cf7f2e2dSJohn Marino 	      && *op_ptr != DW_OP_bit_piece
8265796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_GNU_uninit)
8275796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_reg operations must be "
828a45ae5f8SJohn Marino 		     "used either alone or in conjunction with DW_OP_piece "
829cf7f2e2dSJohn Marino 		     "or DW_OP_bit_piece."));
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert 	  result = op - DW_OP_reg0;
832a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
8335796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
8345796c8dcSSimon Schubert 	  break;
8355796c8dcSSimon Schubert 
8365796c8dcSSimon Schubert 	case DW_OP_regx:
837*ef5ccd6cSJohn Marino 	  op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
838cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
8395796c8dcSSimon Schubert 
8405796c8dcSSimon Schubert 	  result = reg;
841a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
8425796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
8435796c8dcSSimon Schubert 	  break;
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert 	case DW_OP_implicit_value:
8465796c8dcSSimon Schubert 	  {
847*ef5ccd6cSJohn Marino 	    uint64_t len;
848cf7f2e2dSJohn Marino 
849*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
8505796c8dcSSimon Schubert 	    if (op_ptr + len > op_end)
8515796c8dcSSimon Schubert 	      error (_("DW_OP_implicit_value: too few bytes available."));
8525796c8dcSSimon Schubert 	    ctx->len = len;
8535796c8dcSSimon Schubert 	    ctx->data = op_ptr;
8545796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_LITERAL;
8555796c8dcSSimon Schubert 	    op_ptr += len;
856cf7f2e2dSJohn Marino 	    dwarf_expr_require_composition (op_ptr, op_end,
857cf7f2e2dSJohn Marino 					    "DW_OP_implicit_value");
8585796c8dcSSimon Schubert 	  }
8595796c8dcSSimon Schubert 	  goto no_push;
8605796c8dcSSimon Schubert 
8615796c8dcSSimon Schubert 	case DW_OP_stack_value:
8625796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_STACK;
863cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
8645796c8dcSSimon Schubert 	  goto no_push;
8655796c8dcSSimon Schubert 
866c50c785cSJohn Marino 	case DW_OP_GNU_implicit_pointer:
867c50c785cSJohn Marino 	  {
868*ef5ccd6cSJohn Marino 	    int64_t len;
869c50c785cSJohn Marino 
870a45ae5f8SJohn Marino 	    if (ctx->ref_addr_size == -1)
871a45ae5f8SJohn Marino 	      error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
872a45ae5f8SJohn Marino 		       "is not allowed in frame context"));
873a45ae5f8SJohn Marino 
874*ef5ccd6cSJohn Marino 	    /* The referred-to DIE of sect_offset kind.  */
875a45ae5f8SJohn Marino 	    ctx->len = extract_unsigned_integer (op_ptr, ctx->ref_addr_size,
876c50c785cSJohn Marino 						 byte_order);
877a45ae5f8SJohn Marino 	    op_ptr += ctx->ref_addr_size;
878c50c785cSJohn Marino 
879c50c785cSJohn Marino 	    /* The byte offset into the data.  */
880*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
881c50c785cSJohn Marino 	    result = (ULONGEST) len;
882a45ae5f8SJohn Marino 	    result_val = value_from_ulongest (address_type, result);
883c50c785cSJohn Marino 
884c50c785cSJohn Marino 	    ctx->location = DWARF_VALUE_IMPLICIT_POINTER;
885c50c785cSJohn Marino 	    dwarf_expr_require_composition (op_ptr, op_end,
886c50c785cSJohn Marino 					    "DW_OP_GNU_implicit_pointer");
887c50c785cSJohn Marino 	  }
888c50c785cSJohn Marino 	  break;
889c50c785cSJohn Marino 
8905796c8dcSSimon Schubert 	case DW_OP_breg0:
8915796c8dcSSimon Schubert 	case DW_OP_breg1:
8925796c8dcSSimon Schubert 	case DW_OP_breg2:
8935796c8dcSSimon Schubert 	case DW_OP_breg3:
8945796c8dcSSimon Schubert 	case DW_OP_breg4:
8955796c8dcSSimon Schubert 	case DW_OP_breg5:
8965796c8dcSSimon Schubert 	case DW_OP_breg6:
8975796c8dcSSimon Schubert 	case DW_OP_breg7:
8985796c8dcSSimon Schubert 	case DW_OP_breg8:
8995796c8dcSSimon Schubert 	case DW_OP_breg9:
9005796c8dcSSimon Schubert 	case DW_OP_breg10:
9015796c8dcSSimon Schubert 	case DW_OP_breg11:
9025796c8dcSSimon Schubert 	case DW_OP_breg12:
9035796c8dcSSimon Schubert 	case DW_OP_breg13:
9045796c8dcSSimon Schubert 	case DW_OP_breg14:
9055796c8dcSSimon Schubert 	case DW_OP_breg15:
9065796c8dcSSimon Schubert 	case DW_OP_breg16:
9075796c8dcSSimon Schubert 	case DW_OP_breg17:
9085796c8dcSSimon Schubert 	case DW_OP_breg18:
9095796c8dcSSimon Schubert 	case DW_OP_breg19:
9105796c8dcSSimon Schubert 	case DW_OP_breg20:
9115796c8dcSSimon Schubert 	case DW_OP_breg21:
9125796c8dcSSimon Schubert 	case DW_OP_breg22:
9135796c8dcSSimon Schubert 	case DW_OP_breg23:
9145796c8dcSSimon Schubert 	case DW_OP_breg24:
9155796c8dcSSimon Schubert 	case DW_OP_breg25:
9165796c8dcSSimon Schubert 	case DW_OP_breg26:
9175796c8dcSSimon Schubert 	case DW_OP_breg27:
9185796c8dcSSimon Schubert 	case DW_OP_breg28:
9195796c8dcSSimon Schubert 	case DW_OP_breg29:
9205796c8dcSSimon Schubert 	case DW_OP_breg30:
9215796c8dcSSimon Schubert 	case DW_OP_breg31:
9225796c8dcSSimon Schubert 	  {
923*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
924a45ae5f8SJohn Marino 	    result = (ctx->funcs->read_reg) (ctx->baton, op - DW_OP_breg0);
9255796c8dcSSimon Schubert 	    result += offset;
926a45ae5f8SJohn Marino 	    result_val = value_from_ulongest (address_type, result);
9275796c8dcSSimon Schubert 	  }
9285796c8dcSSimon Schubert 	  break;
9295796c8dcSSimon Schubert 	case DW_OP_bregx:
9305796c8dcSSimon Schubert 	  {
931*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
932*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
933a45ae5f8SJohn Marino 	    result = (ctx->funcs->read_reg) (ctx->baton, reg);
9345796c8dcSSimon Schubert 	    result += offset;
935a45ae5f8SJohn Marino 	    result_val = value_from_ulongest (address_type, result);
9365796c8dcSSimon Schubert 	  }
9375796c8dcSSimon Schubert 	  break;
9385796c8dcSSimon Schubert 	case DW_OP_fbreg:
9395796c8dcSSimon Schubert 	  {
940cf7f2e2dSJohn Marino 	    const gdb_byte *datastart;
9415796c8dcSSimon Schubert 	    size_t datalen;
9425796c8dcSSimon Schubert 	    unsigned int before_stack_len;
9435796c8dcSSimon Schubert 
944*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
9455796c8dcSSimon Schubert 	    /* Rather than create a whole new context, we simply
9465796c8dcSSimon Schubert 	       record the stack length before execution, then reset it
9475796c8dcSSimon Schubert 	       afterwards, effectively erasing whatever the recursive
9485796c8dcSSimon Schubert 	       call put there.  */
9495796c8dcSSimon Schubert 	    before_stack_len = ctx->stack_len;
9505796c8dcSSimon Schubert 	    /* FIXME: cagney/2003-03-26: This code should be using
9515796c8dcSSimon Schubert                get_frame_base_address(), and then implement a dwarf2
9525796c8dcSSimon Schubert                specific this_base method.  */
953a45ae5f8SJohn Marino 	    (ctx->funcs->get_frame_base) (ctx->baton, &datastart, &datalen);
9545796c8dcSSimon Schubert 	    dwarf_expr_eval (ctx, datastart, datalen);
955cf7f2e2dSJohn Marino 	    if (ctx->location == DWARF_VALUE_MEMORY)
956cf7f2e2dSJohn Marino 	      result = dwarf_expr_fetch_address (ctx, 0);
957cf7f2e2dSJohn Marino 	    else if (ctx->location == DWARF_VALUE_REGISTER)
958a45ae5f8SJohn Marino 	      result = (ctx->funcs->read_reg) (ctx->baton,
959a45ae5f8SJohn Marino 				     value_as_long (dwarf_expr_fetch (ctx, 0)));
960cf7f2e2dSJohn Marino 	    else
961c50c785cSJohn Marino 	      error (_("Not implemented: computing frame "
962c50c785cSJohn Marino 		       "base using explicit value operator"));
9635796c8dcSSimon Schubert 	    result = result + offset;
964a45ae5f8SJohn Marino 	    result_val = value_from_ulongest (address_type, result);
9655796c8dcSSimon Schubert 	    in_stack_memory = 1;
9665796c8dcSSimon Schubert 	    ctx->stack_len = before_stack_len;
9675796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_MEMORY;
9685796c8dcSSimon Schubert 	  }
9695796c8dcSSimon Schubert 	  break;
9705796c8dcSSimon Schubert 
9715796c8dcSSimon Schubert 	case DW_OP_dup:
972a45ae5f8SJohn Marino 	  result_val = dwarf_expr_fetch (ctx, 0);
9735796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
9745796c8dcSSimon Schubert 	  break;
9755796c8dcSSimon Schubert 
9765796c8dcSSimon Schubert 	case DW_OP_drop:
9775796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
9785796c8dcSSimon Schubert 	  goto no_push;
9795796c8dcSSimon Schubert 
9805796c8dcSSimon Schubert 	case DW_OP_pick:
9815796c8dcSSimon Schubert 	  offset = *op_ptr++;
982a45ae5f8SJohn Marino 	  result_val = dwarf_expr_fetch (ctx, offset);
9835796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
9845796c8dcSSimon Schubert 	  break;
9855796c8dcSSimon Schubert 
9865796c8dcSSimon Schubert 	case DW_OP_swap:
9875796c8dcSSimon Schubert 	  {
9885796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2;
9895796c8dcSSimon Schubert 
9905796c8dcSSimon Schubert 	    if (ctx->stack_len < 2)
991c50c785cSJohn Marino 	       error (_("Not enough elements for "
992c50c785cSJohn Marino 			"DW_OP_swap.  Need 2, have %d."),
9935796c8dcSSimon Schubert 		      ctx->stack_len);
9945796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
9955796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
9965796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
9975796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t1;
9985796c8dcSSimon Schubert 	    goto no_push;
9995796c8dcSSimon Schubert 	  }
10005796c8dcSSimon Schubert 
10015796c8dcSSimon Schubert 	case DW_OP_over:
1002a45ae5f8SJohn Marino 	  result_val = dwarf_expr_fetch (ctx, 1);
10035796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
10045796c8dcSSimon Schubert 	  break;
10055796c8dcSSimon Schubert 
10065796c8dcSSimon Schubert 	case DW_OP_rot:
10075796c8dcSSimon Schubert 	  {
10085796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2, t3;
10095796c8dcSSimon Schubert 
10105796c8dcSSimon Schubert 	    if (ctx->stack_len < 3)
1011c50c785cSJohn Marino 	       error (_("Not enough elements for "
1012c50c785cSJohn Marino 			"DW_OP_rot.  Need 3, have %d."),
10135796c8dcSSimon Schubert 		      ctx->stack_len);
10145796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
10155796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
10165796c8dcSSimon Schubert 	    t3 = ctx->stack[ctx->stack_len - 3];
10175796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
10185796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t3;
10195796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 3] = t1;
10205796c8dcSSimon Schubert 	    goto no_push;
10215796c8dcSSimon Schubert 	  }
10225796c8dcSSimon Schubert 
10235796c8dcSSimon Schubert 	case DW_OP_deref:
10245796c8dcSSimon Schubert 	case DW_OP_deref_size:
1025a45ae5f8SJohn Marino 	case DW_OP_GNU_deref_type:
1026cf7f2e2dSJohn Marino 	  {
1027cf7f2e2dSJohn Marino 	    int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
1028cf7f2e2dSJohn Marino 	    gdb_byte *buf = alloca (addr_size);
1029cf7f2e2dSJohn Marino 	    CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
1030a45ae5f8SJohn Marino 	    struct type *type;
1031a45ae5f8SJohn Marino 
1032cf7f2e2dSJohn Marino 	    dwarf_expr_pop (ctx);
1033cf7f2e2dSJohn Marino 
1034a45ae5f8SJohn Marino 	    if (op == DW_OP_GNU_deref_type)
1035a45ae5f8SJohn Marino 	      {
1036*ef5ccd6cSJohn Marino 		cu_offset type_die;
1037a45ae5f8SJohn Marino 
1038*ef5ccd6cSJohn Marino 		op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1039*ef5ccd6cSJohn Marino 		type_die.cu_off = uoffset;
1040a45ae5f8SJohn Marino 		type = dwarf_get_base_type (ctx, type_die, 0);
1041a45ae5f8SJohn Marino 	      }
1042a45ae5f8SJohn Marino 	    else
1043a45ae5f8SJohn Marino 	      type = address_type;
1044a45ae5f8SJohn Marino 
1045a45ae5f8SJohn Marino 	    (ctx->funcs->read_mem) (ctx->baton, buf, addr, addr_size);
1046a45ae5f8SJohn Marino 
1047a45ae5f8SJohn Marino 	    /* If the size of the object read from memory is different
1048a45ae5f8SJohn Marino 	       from the type length, we need to zero-extend it.  */
1049a45ae5f8SJohn Marino 	    if (TYPE_LENGTH (type) != addr_size)
1050a45ae5f8SJohn Marino 	      {
1051a45ae5f8SJohn Marino 		ULONGEST result =
1052a45ae5f8SJohn Marino 		  extract_unsigned_integer (buf, addr_size, byte_order);
1053a45ae5f8SJohn Marino 
1054a45ae5f8SJohn Marino 		buf = alloca (TYPE_LENGTH (type));
1055a45ae5f8SJohn Marino 		store_unsigned_integer (buf, TYPE_LENGTH (type),
1056a45ae5f8SJohn Marino 					byte_order, result);
1057a45ae5f8SJohn Marino 	      }
1058a45ae5f8SJohn Marino 
1059a45ae5f8SJohn Marino 	    result_val = value_from_contents_and_address (type, buf, addr);
1060cf7f2e2dSJohn Marino 	    break;
1061cf7f2e2dSJohn Marino 	  }
1062cf7f2e2dSJohn Marino 
10635796c8dcSSimon Schubert 	case DW_OP_abs:
10645796c8dcSSimon Schubert 	case DW_OP_neg:
10655796c8dcSSimon Schubert 	case DW_OP_not:
10665796c8dcSSimon Schubert 	case DW_OP_plus_uconst:
1067a45ae5f8SJohn Marino 	  {
10685796c8dcSSimon Schubert 	    /* Unary operations.  */
1069a45ae5f8SJohn Marino 	    result_val = dwarf_expr_fetch (ctx, 0);
10705796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
10715796c8dcSSimon Schubert 
10725796c8dcSSimon Schubert 	    switch (op)
10735796c8dcSSimon Schubert 	      {
10745796c8dcSSimon Schubert 	      case DW_OP_abs:
1075a45ae5f8SJohn Marino 		if (value_less (result_val,
1076a45ae5f8SJohn Marino 				value_zero (value_type (result_val), not_lval)))
1077a45ae5f8SJohn Marino 		  result_val = value_neg (result_val);
10785796c8dcSSimon Schubert 		break;
10795796c8dcSSimon Schubert 	      case DW_OP_neg:
1080a45ae5f8SJohn Marino 		result_val = value_neg (result_val);
10815796c8dcSSimon Schubert 		break;
10825796c8dcSSimon Schubert 	      case DW_OP_not:
1083a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (result_val));
1084a45ae5f8SJohn Marino 		result_val = value_complement (result_val);
10855796c8dcSSimon Schubert 		break;
10865796c8dcSSimon Schubert 	      case DW_OP_plus_uconst:
1087a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (result_val));
1088a45ae5f8SJohn Marino 		result = value_as_long (result_val);
1089*ef5ccd6cSJohn Marino 		op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
10905796c8dcSSimon Schubert 		result += reg;
1091a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
10925796c8dcSSimon Schubert 		break;
10935796c8dcSSimon Schubert 	      }
1094a45ae5f8SJohn Marino 	  }
10955796c8dcSSimon Schubert 	  break;
10965796c8dcSSimon Schubert 
10975796c8dcSSimon Schubert 	case DW_OP_and:
10985796c8dcSSimon Schubert 	case DW_OP_div:
10995796c8dcSSimon Schubert 	case DW_OP_minus:
11005796c8dcSSimon Schubert 	case DW_OP_mod:
11015796c8dcSSimon Schubert 	case DW_OP_mul:
11025796c8dcSSimon Schubert 	case DW_OP_or:
11035796c8dcSSimon Schubert 	case DW_OP_plus:
11045796c8dcSSimon Schubert 	case DW_OP_shl:
11055796c8dcSSimon Schubert 	case DW_OP_shr:
11065796c8dcSSimon Schubert 	case DW_OP_shra:
11075796c8dcSSimon Schubert 	case DW_OP_xor:
11085796c8dcSSimon Schubert 	case DW_OP_le:
11095796c8dcSSimon Schubert 	case DW_OP_ge:
11105796c8dcSSimon Schubert 	case DW_OP_eq:
11115796c8dcSSimon Schubert 	case DW_OP_lt:
11125796c8dcSSimon Schubert 	case DW_OP_gt:
11135796c8dcSSimon Schubert 	case DW_OP_ne:
11145796c8dcSSimon Schubert 	  {
1115cf7f2e2dSJohn Marino 	    /* Binary operations.  */
1116a45ae5f8SJohn Marino 	    struct value *first, *second;
11175796c8dcSSimon Schubert 
11185796c8dcSSimon Schubert 	    second = dwarf_expr_fetch (ctx, 0);
11195796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert 	    first = dwarf_expr_fetch (ctx, 0);
11225796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
11235796c8dcSSimon Schubert 
1124a45ae5f8SJohn Marino 	    if (! base_types_equal_p (value_type (first), value_type (second)))
1125a45ae5f8SJohn Marino 	      error (_("Incompatible types on DWARF stack"));
1126a45ae5f8SJohn Marino 
11275796c8dcSSimon Schubert 	    switch (op)
11285796c8dcSSimon Schubert 	      {
11295796c8dcSSimon Schubert 	      case DW_OP_and:
1130a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1131a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1132a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_BITWISE_AND);
11335796c8dcSSimon Schubert 		break;
11345796c8dcSSimon Schubert 	      case DW_OP_div:
1135a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_DIV);
11365796c8dcSSimon Schubert                 break;
11375796c8dcSSimon Schubert 	      case DW_OP_minus:
1138a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_SUB);
11395796c8dcSSimon Schubert 		break;
11405796c8dcSSimon Schubert 	      case DW_OP_mod:
1141a45ae5f8SJohn Marino 		{
1142a45ae5f8SJohn Marino 		  int cast_back = 0;
1143a45ae5f8SJohn Marino 		  struct type *orig_type = value_type (first);
1144a45ae5f8SJohn Marino 
1145a45ae5f8SJohn Marino 		  /* We have to special-case "old-style" untyped values
1146a45ae5f8SJohn Marino 		     -- these must have mod computed using unsigned
1147a45ae5f8SJohn Marino 		     math.  */
1148a45ae5f8SJohn Marino 		  if (orig_type == address_type)
1149a45ae5f8SJohn Marino 		    {
1150a45ae5f8SJohn Marino 		      struct type *utype
1151a45ae5f8SJohn Marino 			= get_unsigned_type (ctx->gdbarch, orig_type);
1152a45ae5f8SJohn Marino 
1153a45ae5f8SJohn Marino 		      cast_back = 1;
1154a45ae5f8SJohn Marino 		      first = value_cast (utype, first);
1155a45ae5f8SJohn Marino 		      second = value_cast (utype, second);
1156a45ae5f8SJohn Marino 		    }
1157a45ae5f8SJohn Marino 		  /* Note that value_binop doesn't handle float or
1158a45ae5f8SJohn Marino 		     decimal float here.  This seems unimportant.  */
1159a45ae5f8SJohn Marino 		  result_val = value_binop (first, second, BINOP_MOD);
1160a45ae5f8SJohn Marino 		  if (cast_back)
1161a45ae5f8SJohn Marino 		    result_val = value_cast (orig_type, result_val);
1162a45ae5f8SJohn Marino 		}
11635796c8dcSSimon Schubert 		break;
11645796c8dcSSimon Schubert 	      case DW_OP_mul:
1165a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_MUL);
11665796c8dcSSimon Schubert 		break;
11675796c8dcSSimon Schubert 	      case DW_OP_or:
1168a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1169a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1170a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_BITWISE_IOR);
11715796c8dcSSimon Schubert 		break;
11725796c8dcSSimon Schubert 	      case DW_OP_plus:
1173a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_ADD);
11745796c8dcSSimon Schubert 		break;
11755796c8dcSSimon Schubert 	      case DW_OP_shl:
1176a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1177a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1178a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_LSH);
11795796c8dcSSimon Schubert 		break;
11805796c8dcSSimon Schubert 	      case DW_OP_shr:
1181a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1182a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1183a45ae5f8SJohn Marino 		if (!TYPE_UNSIGNED (value_type (first)))
1184a45ae5f8SJohn Marino 		  {
1185a45ae5f8SJohn Marino 		    struct type *utype
1186a45ae5f8SJohn Marino 		      = get_unsigned_type (ctx->gdbarch, value_type (first));
1187a45ae5f8SJohn Marino 
1188a45ae5f8SJohn Marino 		    first = value_cast (utype, first);
1189a45ae5f8SJohn Marino 		  }
1190a45ae5f8SJohn Marino 
1191a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_RSH);
1192a45ae5f8SJohn Marino 		/* Make sure we wind up with the same type we started
1193a45ae5f8SJohn Marino 		   with.  */
1194a45ae5f8SJohn Marino 		if (value_type (result_val) != value_type (second))
1195a45ae5f8SJohn Marino 		  result_val = value_cast (value_type (second), result_val);
11965796c8dcSSimon Schubert                 break;
11975796c8dcSSimon Schubert 	      case DW_OP_shra:
1198a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1199a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1200a45ae5f8SJohn Marino 		if (TYPE_UNSIGNED (value_type (first)))
1201a45ae5f8SJohn Marino 		  {
1202a45ae5f8SJohn Marino 		    struct type *stype
1203a45ae5f8SJohn Marino 		      = get_signed_type (ctx->gdbarch, value_type (first));
1204a45ae5f8SJohn Marino 
1205a45ae5f8SJohn Marino 		    first = value_cast (stype, first);
1206a45ae5f8SJohn Marino 		  }
1207a45ae5f8SJohn Marino 
1208a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_RSH);
1209a45ae5f8SJohn Marino 		/* Make sure we wind up with the same type we started
1210a45ae5f8SJohn Marino 		   with.  */
1211a45ae5f8SJohn Marino 		if (value_type (result_val) != value_type (second))
1212a45ae5f8SJohn Marino 		  result_val = value_cast (value_type (second), result_val);
12135796c8dcSSimon Schubert 		break;
12145796c8dcSSimon Schubert 	      case DW_OP_xor:
1215a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (first));
1216a45ae5f8SJohn Marino 		dwarf_require_integral (value_type (second));
1217a45ae5f8SJohn Marino 		result_val = value_binop (first, second, BINOP_BITWISE_XOR);
12185796c8dcSSimon Schubert 		break;
12195796c8dcSSimon Schubert 	      case DW_OP_le:
1220a45ae5f8SJohn Marino 		/* A <= B is !(B < A).  */
1221a45ae5f8SJohn Marino 		result = ! value_less (second, first);
1222a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12235796c8dcSSimon Schubert 		break;
12245796c8dcSSimon Schubert 	      case DW_OP_ge:
1225a45ae5f8SJohn Marino 		/* A >= B is !(A < B).  */
1226a45ae5f8SJohn Marino 		result = ! value_less (first, second);
1227a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12285796c8dcSSimon Schubert 		break;
12295796c8dcSSimon Schubert 	      case DW_OP_eq:
1230a45ae5f8SJohn Marino 		result = value_equal (first, second);
1231a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12325796c8dcSSimon Schubert 		break;
12335796c8dcSSimon Schubert 	      case DW_OP_lt:
1234a45ae5f8SJohn Marino 		result = value_less (first, second);
1235a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12365796c8dcSSimon Schubert 		break;
12375796c8dcSSimon Schubert 	      case DW_OP_gt:
1238a45ae5f8SJohn Marino 		/* A > B is B < A.  */
1239a45ae5f8SJohn Marino 		result = value_less (second, first);
1240a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12415796c8dcSSimon Schubert 		break;
12425796c8dcSSimon Schubert 	      case DW_OP_ne:
1243a45ae5f8SJohn Marino 		result = ! value_equal (first, second);
1244a45ae5f8SJohn Marino 		result_val = value_from_ulongest (address_type, result);
12455796c8dcSSimon Schubert 		break;
12465796c8dcSSimon Schubert 	      default:
12475796c8dcSSimon Schubert 		internal_error (__FILE__, __LINE__,
12485796c8dcSSimon Schubert 				_("Can't be reached."));
12495796c8dcSSimon Schubert 	      }
12505796c8dcSSimon Schubert 	  }
12515796c8dcSSimon Schubert 	  break;
12525796c8dcSSimon Schubert 
12535796c8dcSSimon Schubert 	case DW_OP_call_frame_cfa:
1254a45ae5f8SJohn Marino 	  result = (ctx->funcs->get_frame_cfa) (ctx->baton);
1255a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
12565796c8dcSSimon Schubert 	  in_stack_memory = 1;
12575796c8dcSSimon Schubert 	  break;
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert 	case DW_OP_GNU_push_tls_address:
12605796c8dcSSimon Schubert 	  /* Variable is at a constant offset in the thread-local
12615796c8dcSSimon Schubert 	  storage block into the objfile for the current thread and
12625796c8dcSSimon Schubert 	  the dynamic linker module containing this expression.  Here
12635796c8dcSSimon Schubert 	  we return returns the offset from that base.  The top of the
12645796c8dcSSimon Schubert 	  stack has the offset from the beginning of the thread
12655796c8dcSSimon Schubert 	  control block at which the variable is located.  Nothing
12665796c8dcSSimon Schubert 	  should follow this operator, so the top of stack would be
12675796c8dcSSimon Schubert 	  returned.  */
1268a45ae5f8SJohn Marino 	  result = value_as_long (dwarf_expr_fetch (ctx, 0));
12695796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
1270a45ae5f8SJohn Marino 	  result = (ctx->funcs->get_tls_address) (ctx->baton, result);
1271a45ae5f8SJohn Marino 	  result_val = value_from_ulongest (address_type, result);
12725796c8dcSSimon Schubert 	  break;
12735796c8dcSSimon Schubert 
12745796c8dcSSimon Schubert 	case DW_OP_skip:
12755796c8dcSSimon Schubert 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
12765796c8dcSSimon Schubert 	  op_ptr += 2;
12775796c8dcSSimon Schubert 	  op_ptr += offset;
12785796c8dcSSimon Schubert 	  goto no_push;
12795796c8dcSSimon Schubert 
12805796c8dcSSimon Schubert 	case DW_OP_bra:
1281a45ae5f8SJohn Marino 	  {
1282a45ae5f8SJohn Marino 	    struct value *val;
1283a45ae5f8SJohn Marino 
12845796c8dcSSimon Schubert 	    offset = extract_signed_integer (op_ptr, 2, byte_order);
12855796c8dcSSimon Schubert 	    op_ptr += 2;
1286a45ae5f8SJohn Marino 	    val = dwarf_expr_fetch (ctx, 0);
1287a45ae5f8SJohn Marino 	    dwarf_require_integral (value_type (val));
1288a45ae5f8SJohn Marino 	    if (value_as_long (val) != 0)
12895796c8dcSSimon Schubert 	      op_ptr += offset;
12905796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
1291a45ae5f8SJohn Marino 	  }
12925796c8dcSSimon Schubert 	  goto no_push;
12935796c8dcSSimon Schubert 
12945796c8dcSSimon Schubert 	case DW_OP_nop:
12955796c8dcSSimon Schubert 	  goto no_push;
12965796c8dcSSimon Schubert 
12975796c8dcSSimon Schubert         case DW_OP_piece:
12985796c8dcSSimon Schubert           {
1299*ef5ccd6cSJohn Marino             uint64_t size;
13005796c8dcSSimon Schubert 
13015796c8dcSSimon Schubert             /* Record the piece.  */
1302*ef5ccd6cSJohn Marino             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1303cf7f2e2dSJohn Marino 	    add_piece (ctx, 8 * size, 0);
13045796c8dcSSimon Schubert 
13055796c8dcSSimon Schubert             /* Pop off the address/regnum, and reset the location
13065796c8dcSSimon Schubert 	       type.  */
1307cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
1308cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
1309cf7f2e2dSJohn Marino 	      dwarf_expr_pop (ctx);
1310cf7f2e2dSJohn Marino             ctx->location = DWARF_VALUE_MEMORY;
1311cf7f2e2dSJohn Marino           }
1312cf7f2e2dSJohn Marino           goto no_push;
1313cf7f2e2dSJohn Marino 
1314cf7f2e2dSJohn Marino 	case DW_OP_bit_piece:
1315cf7f2e2dSJohn Marino 	  {
1316*ef5ccd6cSJohn Marino 	    uint64_t size, offset;
1317cf7f2e2dSJohn Marino 
1318cf7f2e2dSJohn Marino             /* Record the piece.  */
1319*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1320*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1321cf7f2e2dSJohn Marino 	    add_piece (ctx, size, offset);
1322cf7f2e2dSJohn Marino 
1323cf7f2e2dSJohn Marino             /* Pop off the address/regnum, and reset the location
1324cf7f2e2dSJohn Marino 	       type.  */
1325cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
1326cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
13275796c8dcSSimon Schubert 	      dwarf_expr_pop (ctx);
13285796c8dcSSimon Schubert             ctx->location = DWARF_VALUE_MEMORY;
13295796c8dcSSimon Schubert 	  }
13305796c8dcSSimon Schubert 	  goto no_push;
13315796c8dcSSimon Schubert 
13325796c8dcSSimon Schubert 	case DW_OP_GNU_uninit:
13335796c8dcSSimon Schubert 	  if (op_ptr != op_end)
13345796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
13355796c8dcSSimon Schubert 		   "be the very last op."));
13365796c8dcSSimon Schubert 
13375796c8dcSSimon Schubert 	  ctx->initialized = 0;
13385796c8dcSSimon Schubert 	  goto no_push;
13395796c8dcSSimon Schubert 
1340cf7f2e2dSJohn Marino 	case DW_OP_call2:
1341*ef5ccd6cSJohn Marino 	  {
1342*ef5ccd6cSJohn Marino 	    cu_offset offset;
1343*ef5ccd6cSJohn Marino 
1344*ef5ccd6cSJohn Marino 	    offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1345cf7f2e2dSJohn Marino 	    op_ptr += 2;
1346*ef5ccd6cSJohn Marino 	    ctx->funcs->dwarf_call (ctx, offset);
1347*ef5ccd6cSJohn Marino 	  }
1348cf7f2e2dSJohn Marino 	  goto no_push;
1349cf7f2e2dSJohn Marino 
1350cf7f2e2dSJohn Marino 	case DW_OP_call4:
1351*ef5ccd6cSJohn Marino 	  {
1352*ef5ccd6cSJohn Marino 	    cu_offset offset;
1353*ef5ccd6cSJohn Marino 
1354*ef5ccd6cSJohn Marino 	    offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1355cf7f2e2dSJohn Marino 	    op_ptr += 4;
1356*ef5ccd6cSJohn Marino 	    ctx->funcs->dwarf_call (ctx, offset);
1357*ef5ccd6cSJohn Marino 	  }
1358cf7f2e2dSJohn Marino 	  goto no_push;
1359cf7f2e2dSJohn Marino 
1360c50c785cSJohn Marino 	case DW_OP_GNU_entry_value:
1361a45ae5f8SJohn Marino 	  {
1362*ef5ccd6cSJohn Marino 	    uint64_t len;
1363a45ae5f8SJohn Marino 	    CORE_ADDR deref_size;
1364*ef5ccd6cSJohn Marino 	    union call_site_parameter_u kind_u;
1365a45ae5f8SJohn Marino 
1366*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1367a45ae5f8SJohn Marino 	    if (op_ptr + len > op_end)
1368a45ae5f8SJohn Marino 	      error (_("DW_OP_GNU_entry_value: too few bytes available."));
1369a45ae5f8SJohn Marino 
1370*ef5ccd6cSJohn Marino 	    kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1371*ef5ccd6cSJohn Marino 	    if (kind_u.dwarf_reg != -1)
1372a45ae5f8SJohn Marino 	      {
1373a45ae5f8SJohn Marino 		op_ptr += len;
1374*ef5ccd6cSJohn Marino 		ctx->funcs->push_dwarf_reg_entry_value (ctx,
1375*ef5ccd6cSJohn Marino 						  CALL_SITE_PARAMETER_DWARF_REG,
1376*ef5ccd6cSJohn Marino 							kind_u,
1377a45ae5f8SJohn Marino 							-1 /* deref_size */);
1378a45ae5f8SJohn Marino 		goto no_push;
1379a45ae5f8SJohn Marino 	      }
1380a45ae5f8SJohn Marino 
1381*ef5ccd6cSJohn Marino 	    kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1382*ef5ccd6cSJohn Marino 							       op_ptr + len,
1383a45ae5f8SJohn Marino 							       &deref_size);
1384*ef5ccd6cSJohn Marino 	    if (kind_u.dwarf_reg != -1)
1385a45ae5f8SJohn Marino 	      {
1386a45ae5f8SJohn Marino 		if (deref_size == -1)
1387a45ae5f8SJohn Marino 		  deref_size = ctx->addr_size;
1388a45ae5f8SJohn Marino 		op_ptr += len;
1389*ef5ccd6cSJohn Marino 		ctx->funcs->push_dwarf_reg_entry_value (ctx,
1390*ef5ccd6cSJohn Marino 						  CALL_SITE_PARAMETER_DWARF_REG,
1391*ef5ccd6cSJohn Marino 							kind_u, deref_size);
1392a45ae5f8SJohn Marino 		goto no_push;
1393a45ae5f8SJohn Marino 	      }
1394a45ae5f8SJohn Marino 
1395a45ae5f8SJohn Marino 	    error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1396a45ae5f8SJohn Marino 		     "supported only for single DW_OP_reg* "
1397a45ae5f8SJohn Marino 		     "or for DW_OP_breg*(0)+DW_OP_deref*"));
1398a45ae5f8SJohn Marino 	  }
1399a45ae5f8SJohn Marino 
1400*ef5ccd6cSJohn Marino 	case DW_OP_GNU_parameter_ref:
1401*ef5ccd6cSJohn Marino 	  {
1402*ef5ccd6cSJohn Marino 	    union call_site_parameter_u kind_u;
1403*ef5ccd6cSJohn Marino 
1404*ef5ccd6cSJohn Marino 	    kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1405*ef5ccd6cSJohn Marino 								   byte_order);
1406*ef5ccd6cSJohn Marino 	    op_ptr += 4;
1407*ef5ccd6cSJohn Marino 	    ctx->funcs->push_dwarf_reg_entry_value (ctx,
1408*ef5ccd6cSJohn Marino 					       CALL_SITE_PARAMETER_PARAM_OFFSET,
1409*ef5ccd6cSJohn Marino 						    kind_u,
1410*ef5ccd6cSJohn Marino 						    -1 /* deref_size */);
1411*ef5ccd6cSJohn Marino 	  }
1412*ef5ccd6cSJohn Marino 	  goto no_push;
1413*ef5ccd6cSJohn Marino 
1414a45ae5f8SJohn Marino 	case DW_OP_GNU_const_type:
1415a45ae5f8SJohn Marino 	  {
1416*ef5ccd6cSJohn Marino 	    cu_offset type_die;
1417a45ae5f8SJohn Marino 	    int n;
1418a45ae5f8SJohn Marino 	    const gdb_byte *data;
1419a45ae5f8SJohn Marino 	    struct type *type;
1420a45ae5f8SJohn Marino 
1421*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1422*ef5ccd6cSJohn Marino 	    type_die.cu_off = uoffset;
1423a45ae5f8SJohn Marino 	    n = *op_ptr++;
1424a45ae5f8SJohn Marino 	    data = op_ptr;
1425a45ae5f8SJohn Marino 	    op_ptr += n;
1426a45ae5f8SJohn Marino 
1427a45ae5f8SJohn Marino 	    type = dwarf_get_base_type (ctx, type_die, n);
1428a45ae5f8SJohn Marino 	    result_val = value_from_contents (type, data);
1429a45ae5f8SJohn Marino 	  }
1430a45ae5f8SJohn Marino 	  break;
1431a45ae5f8SJohn Marino 
1432a45ae5f8SJohn Marino 	case DW_OP_GNU_regval_type:
1433a45ae5f8SJohn Marino 	  {
1434*ef5ccd6cSJohn Marino 	    cu_offset type_die;
1435a45ae5f8SJohn Marino 	    struct type *type;
1436a45ae5f8SJohn Marino 
1437*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1438*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1439*ef5ccd6cSJohn Marino 	    type_die.cu_off = uoffset;
1440a45ae5f8SJohn Marino 
1441a45ae5f8SJohn Marino 	    type = dwarf_get_base_type (ctx, type_die, 0);
1442a45ae5f8SJohn Marino 	    result = (ctx->funcs->read_reg) (ctx->baton, reg);
1443a45ae5f8SJohn Marino 	    result_val = value_from_ulongest (address_type, result);
1444a45ae5f8SJohn Marino 	    result_val = value_from_contents (type,
1445a45ae5f8SJohn Marino 					      value_contents_all (result_val));
1446a45ae5f8SJohn Marino 	  }
1447a45ae5f8SJohn Marino 	  break;
1448a45ae5f8SJohn Marino 
1449a45ae5f8SJohn Marino 	case DW_OP_GNU_convert:
1450a45ae5f8SJohn Marino 	case DW_OP_GNU_reinterpret:
1451a45ae5f8SJohn Marino 	  {
1452*ef5ccd6cSJohn Marino 	    cu_offset type_die;
1453a45ae5f8SJohn Marino 	    struct type *type;
1454a45ae5f8SJohn Marino 
1455*ef5ccd6cSJohn Marino 	    op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1456*ef5ccd6cSJohn Marino 	    type_die.cu_off = uoffset;
1457a45ae5f8SJohn Marino 
1458*ef5ccd6cSJohn Marino 	    if (type_die.cu_off == 0)
1459a45ae5f8SJohn Marino 	      type = address_type;
1460a45ae5f8SJohn Marino 	    else
1461a45ae5f8SJohn Marino 	      type = dwarf_get_base_type (ctx, type_die, 0);
1462a45ae5f8SJohn Marino 
1463a45ae5f8SJohn Marino 	    result_val = dwarf_expr_fetch (ctx, 0);
1464a45ae5f8SJohn Marino 	    dwarf_expr_pop (ctx);
1465a45ae5f8SJohn Marino 
1466a45ae5f8SJohn Marino 	    if (op == DW_OP_GNU_convert)
1467a45ae5f8SJohn Marino 	      result_val = value_cast (type, result_val);
1468a45ae5f8SJohn Marino 	    else if (type == value_type (result_val))
1469a45ae5f8SJohn Marino 	      {
1470a45ae5f8SJohn Marino 		/* Nothing.  */
1471a45ae5f8SJohn Marino 	      }
1472a45ae5f8SJohn Marino 	    else if (TYPE_LENGTH (type)
1473a45ae5f8SJohn Marino 		     != TYPE_LENGTH (value_type (result_val)))
1474a45ae5f8SJohn Marino 	      error (_("DW_OP_GNU_reinterpret has wrong size"));
1475a45ae5f8SJohn Marino 	    else
1476a45ae5f8SJohn Marino 	      result_val
1477a45ae5f8SJohn Marino 		= value_from_contents (type,
1478a45ae5f8SJohn Marino 				       value_contents_all (result_val));
1479a45ae5f8SJohn Marino 	  }
1480a45ae5f8SJohn Marino 	  break;
1481c50c785cSJohn Marino 
14825796c8dcSSimon Schubert 	default:
14835796c8dcSSimon Schubert 	  error (_("Unhandled dwarf expression opcode 0x%x"), op);
14845796c8dcSSimon Schubert 	}
14855796c8dcSSimon Schubert 
14865796c8dcSSimon Schubert       /* Most things push a result value.  */
1487a45ae5f8SJohn Marino       gdb_assert (result_val != NULL);
1488a45ae5f8SJohn Marino       dwarf_expr_push (ctx, result_val, in_stack_memory);
1489c50c785cSJohn Marino     no_push:
1490c50c785cSJohn Marino       ;
14915796c8dcSSimon Schubert     }
14925796c8dcSSimon Schubert 
1493c50c785cSJohn Marino   /* To simplify our main caller, if the result is an implicit
1494c50c785cSJohn Marino      pointer, then make a pieced value.  This is ok because we can't
1495c50c785cSJohn Marino      have implicit pointers in contexts where pieces are invalid.  */
1496c50c785cSJohn Marino   if (ctx->location == DWARF_VALUE_IMPLICIT_POINTER)
1497c50c785cSJohn Marino     add_piece (ctx, 8 * ctx->addr_size, 0);
1498c50c785cSJohn Marino 
1499c50c785cSJohn Marino abort_expression:
15005796c8dcSSimon Schubert   ctx->recursion_depth--;
15015796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth >= 0);
1502a45ae5f8SJohn Marino }
1503a45ae5f8SJohn Marino 
1504a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.get_frame_base implementation.  */
1505a45ae5f8SJohn Marino 
1506a45ae5f8SJohn Marino void
ctx_no_get_frame_base(void * baton,const gdb_byte ** start,size_t * length)1507a45ae5f8SJohn Marino ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1508a45ae5f8SJohn Marino {
1509a45ae5f8SJohn Marino   error (_("%s is invalid in this context"), "DW_OP_fbreg");
1510a45ae5f8SJohn Marino }
1511a45ae5f8SJohn Marino 
1512a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation.  */
1513a45ae5f8SJohn Marino 
1514a45ae5f8SJohn Marino CORE_ADDR
ctx_no_get_frame_cfa(void * baton)1515a45ae5f8SJohn Marino ctx_no_get_frame_cfa (void *baton)
1516a45ae5f8SJohn Marino {
1517a45ae5f8SJohn Marino   error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1518a45ae5f8SJohn Marino }
1519a45ae5f8SJohn Marino 
1520a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.get_frame_pc implementation.  */
1521a45ae5f8SJohn Marino 
1522a45ae5f8SJohn Marino CORE_ADDR
ctx_no_get_frame_pc(void * baton)1523a45ae5f8SJohn Marino ctx_no_get_frame_pc (void *baton)
1524a45ae5f8SJohn Marino {
1525a45ae5f8SJohn Marino   error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1526a45ae5f8SJohn Marino }
1527a45ae5f8SJohn Marino 
1528a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.get_tls_address implementation.  */
1529a45ae5f8SJohn Marino 
1530a45ae5f8SJohn Marino CORE_ADDR
ctx_no_get_tls_address(void * baton,CORE_ADDR offset)1531a45ae5f8SJohn Marino ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1532a45ae5f8SJohn Marino {
1533a45ae5f8SJohn Marino   error (_("%s is invalid in this context"), "DW_OP_GNU_push_tls_address");
1534a45ae5f8SJohn Marino }
1535a45ae5f8SJohn Marino 
1536a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.dwarf_call implementation.  */
1537a45ae5f8SJohn Marino 
1538a45ae5f8SJohn Marino void
ctx_no_dwarf_call(struct dwarf_expr_context * ctx,cu_offset die_offset)1539*ef5ccd6cSJohn Marino ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1540a45ae5f8SJohn Marino {
1541a45ae5f8SJohn Marino   error (_("%s is invalid in this context"), "DW_OP_call*");
1542a45ae5f8SJohn Marino }
1543a45ae5f8SJohn Marino 
1544a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.get_base_type implementation.  */
1545a45ae5f8SJohn Marino 
1546a45ae5f8SJohn Marino struct type *
ctx_no_get_base_type(struct dwarf_expr_context * ctx,cu_offset die)1547*ef5ccd6cSJohn Marino ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1548a45ae5f8SJohn Marino {
1549a45ae5f8SJohn Marino   error (_("Support for typed DWARF is not supported in this context"));
1550a45ae5f8SJohn Marino }
1551a45ae5f8SJohn Marino 
1552a45ae5f8SJohn Marino /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1553a45ae5f8SJohn Marino    implementation.  */
1554a45ae5f8SJohn Marino 
1555a45ae5f8SJohn Marino void
ctx_no_push_dwarf_reg_entry_value(struct dwarf_expr_context * ctx,enum call_site_parameter_kind kind,union call_site_parameter_u kind_u,int deref_size)1556a45ae5f8SJohn Marino ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1557*ef5ccd6cSJohn Marino 				   enum call_site_parameter_kind kind,
1558*ef5ccd6cSJohn Marino 				   union call_site_parameter_u kind_u,
1559a45ae5f8SJohn Marino 				   int deref_size)
1560a45ae5f8SJohn Marino {
1561a45ae5f8SJohn Marino   internal_error (__FILE__, __LINE__,
1562a45ae5f8SJohn Marino 		  _("Support for DW_OP_GNU_entry_value is unimplemented"));
1563a45ae5f8SJohn Marino }
1564a45ae5f8SJohn Marino 
1565*ef5ccd6cSJohn Marino /* Stub dwarf_expr_context_funcs.get_addr_index implementation.  */
1566*ef5ccd6cSJohn Marino 
1567*ef5ccd6cSJohn Marino CORE_ADDR
ctx_no_get_addr_index(void * baton,unsigned int index)1568*ef5ccd6cSJohn Marino ctx_no_get_addr_index (void *baton, unsigned int index)
1569*ef5ccd6cSJohn Marino {
1570*ef5ccd6cSJohn Marino   error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1571*ef5ccd6cSJohn Marino }
1572*ef5ccd6cSJohn Marino 
1573*ef5ccd6cSJohn Marino /* Provide a prototype to silence -Wmissing-prototypes.  */
1574*ef5ccd6cSJohn Marino extern initialize_file_ftype _initialize_dwarf2expr;
1575*ef5ccd6cSJohn Marino 
1576a45ae5f8SJohn Marino void
_initialize_dwarf2expr(void)1577a45ae5f8SJohn Marino _initialize_dwarf2expr (void)
1578a45ae5f8SJohn Marino {
1579a45ae5f8SJohn Marino   dwarf_arch_cookie
1580a45ae5f8SJohn Marino     = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);
15815796c8dcSSimon Schubert }
1582