xref: /dflybsd-src/contrib/gdb-7/gdb/dwarf2expr.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* DWARF 2 Expression Evaluator.
25796c8dcSSimon Schubert 
3*cf7f2e2dSJohn Marino    Copyright (C) 2001, 2002, 2003, 2005, 2007, 2008, 2009, 2010
45796c8dcSSimon Schubert    Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    Contributed by Daniel Berlin (dan@dberlin.org)
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This file is part of GDB.
95796c8dcSSimon Schubert 
105796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert    (at your option) any later version.
145796c8dcSSimon Schubert 
155796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
185796c8dcSSimon Schubert    GNU General Public License for more details.
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
225796c8dcSSimon Schubert 
235796c8dcSSimon Schubert #include "defs.h"
245796c8dcSSimon Schubert #include "symtab.h"
255796c8dcSSimon Schubert #include "gdbtypes.h"
265796c8dcSSimon Schubert #include "value.h"
275796c8dcSSimon Schubert #include "gdbcore.h"
285796c8dcSSimon Schubert #include "dwarf2.h"
295796c8dcSSimon Schubert #include "dwarf2expr.h"
305796c8dcSSimon Schubert #include "gdb_assert.h"
315796c8dcSSimon Schubert 
325796c8dcSSimon Schubert /* Local prototypes.  */
335796c8dcSSimon Schubert 
345796c8dcSSimon Schubert static void execute_stack_op (struct dwarf_expr_context *,
35*cf7f2e2dSJohn Marino 			      const gdb_byte *, const gdb_byte *);
365796c8dcSSimon Schubert 
375796c8dcSSimon Schubert /* Create a new context for the expression evaluator.  */
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert struct dwarf_expr_context *
405796c8dcSSimon Schubert new_dwarf_expr_context (void)
415796c8dcSSimon Schubert {
425796c8dcSSimon Schubert   struct dwarf_expr_context *retval;
43*cf7f2e2dSJohn Marino 
445796c8dcSSimon Schubert   retval = xcalloc (1, sizeof (struct dwarf_expr_context));
455796c8dcSSimon Schubert   retval->stack_len = 0;
465796c8dcSSimon Schubert   retval->stack_allocated = 10;
47*cf7f2e2dSJohn Marino   retval->stack = xmalloc (retval->stack_allocated
48*cf7f2e2dSJohn Marino 			   * sizeof (struct dwarf_stack_value));
495796c8dcSSimon Schubert   retval->num_pieces = 0;
505796c8dcSSimon Schubert   retval->pieces = 0;
515796c8dcSSimon Schubert   retval->max_recursion_depth = 0x100;
525796c8dcSSimon Schubert   return retval;
535796c8dcSSimon Schubert }
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert /* Release the memory allocated to CTX.  */
565796c8dcSSimon Schubert 
575796c8dcSSimon Schubert void
585796c8dcSSimon Schubert free_dwarf_expr_context (struct dwarf_expr_context *ctx)
595796c8dcSSimon Schubert {
605796c8dcSSimon Schubert   xfree (ctx->stack);
615796c8dcSSimon Schubert   xfree (ctx->pieces);
625796c8dcSSimon Schubert   xfree (ctx);
635796c8dcSSimon Schubert }
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert /* Helper for make_cleanup_free_dwarf_expr_context.  */
665796c8dcSSimon Schubert 
675796c8dcSSimon Schubert static void
685796c8dcSSimon Schubert free_dwarf_expr_context_cleanup (void *arg)
695796c8dcSSimon Schubert {
705796c8dcSSimon Schubert   free_dwarf_expr_context (arg);
715796c8dcSSimon Schubert }
725796c8dcSSimon Schubert 
735796c8dcSSimon Schubert /* Return a cleanup that calls free_dwarf_expr_context.  */
745796c8dcSSimon Schubert 
755796c8dcSSimon Schubert struct cleanup *
765796c8dcSSimon Schubert make_cleanup_free_dwarf_expr_context (struct dwarf_expr_context *ctx)
775796c8dcSSimon Schubert {
785796c8dcSSimon Schubert   return make_cleanup (free_dwarf_expr_context_cleanup, ctx);
795796c8dcSSimon Schubert }
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert /* Expand the memory allocated to CTX's stack to contain at least
825796c8dcSSimon Schubert    NEED more elements than are currently used.  */
835796c8dcSSimon Schubert 
845796c8dcSSimon Schubert static void
855796c8dcSSimon Schubert dwarf_expr_grow_stack (struct dwarf_expr_context *ctx, size_t need)
865796c8dcSSimon Schubert {
875796c8dcSSimon Schubert   if (ctx->stack_len + need > ctx->stack_allocated)
885796c8dcSSimon Schubert     {
895796c8dcSSimon Schubert       size_t newlen = ctx->stack_len + need + 10;
90*cf7f2e2dSJohn Marino 
915796c8dcSSimon Schubert       ctx->stack = xrealloc (ctx->stack,
925796c8dcSSimon Schubert 			     newlen * sizeof (struct dwarf_stack_value));
935796c8dcSSimon Schubert       ctx->stack_allocated = newlen;
945796c8dcSSimon Schubert     }
955796c8dcSSimon Schubert }
965796c8dcSSimon Schubert 
975796c8dcSSimon Schubert /* Push VALUE onto CTX's stack.  */
985796c8dcSSimon Schubert 
995796c8dcSSimon Schubert void
100*cf7f2e2dSJohn Marino dwarf_expr_push (struct dwarf_expr_context *ctx, ULONGEST value,
1015796c8dcSSimon Schubert 		 int in_stack_memory)
1025796c8dcSSimon Schubert {
1035796c8dcSSimon Schubert   struct dwarf_stack_value *v;
1045796c8dcSSimon Schubert 
105*cf7f2e2dSJohn Marino   /* We keep all stack elements within the range defined by the
106*cf7f2e2dSJohn Marino      DWARF address size.  */
107*cf7f2e2dSJohn Marino   if (ctx->addr_size < sizeof (ULONGEST))
108*cf7f2e2dSJohn Marino     value &= ((ULONGEST) 1 << (ctx->addr_size * HOST_CHAR_BIT)) - 1;
109*cf7f2e2dSJohn Marino 
1105796c8dcSSimon Schubert   dwarf_expr_grow_stack (ctx, 1);
1115796c8dcSSimon Schubert   v = &ctx->stack[ctx->stack_len++];
1125796c8dcSSimon Schubert   v->value = value;
1135796c8dcSSimon Schubert   v->in_stack_memory = in_stack_memory;
1145796c8dcSSimon Schubert }
1155796c8dcSSimon Schubert 
1165796c8dcSSimon Schubert /* Pop the top item off of CTX's stack.  */
1175796c8dcSSimon Schubert 
1185796c8dcSSimon Schubert void
1195796c8dcSSimon Schubert dwarf_expr_pop (struct dwarf_expr_context *ctx)
1205796c8dcSSimon Schubert {
1215796c8dcSSimon Schubert   if (ctx->stack_len <= 0)
1225796c8dcSSimon Schubert     error (_("dwarf expression stack underflow"));
1235796c8dcSSimon Schubert   ctx->stack_len--;
1245796c8dcSSimon Schubert }
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert /* Retrieve the N'th item on CTX's stack.  */
1275796c8dcSSimon Schubert 
128*cf7f2e2dSJohn Marino ULONGEST
1295796c8dcSSimon Schubert dwarf_expr_fetch (struct dwarf_expr_context *ctx, int n)
1305796c8dcSSimon Schubert {
1315796c8dcSSimon Schubert   if (ctx->stack_len <= n)
1325796c8dcSSimon Schubert      error (_("Asked for position %d of stack, stack only has %d elements on it."),
1335796c8dcSSimon Schubert 	    n, ctx->stack_len);
1345796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].value;
1355796c8dcSSimon Schubert 
1365796c8dcSSimon Schubert }
1375796c8dcSSimon Schubert 
138*cf7f2e2dSJohn Marino /* Retrieve the N'th item on CTX's stack, converted to an address.  */
139*cf7f2e2dSJohn Marino 
140*cf7f2e2dSJohn Marino CORE_ADDR
141*cf7f2e2dSJohn Marino dwarf_expr_fetch_address (struct dwarf_expr_context *ctx, int n)
142*cf7f2e2dSJohn Marino {
143*cf7f2e2dSJohn Marino   ULONGEST result = dwarf_expr_fetch (ctx, n);
144*cf7f2e2dSJohn Marino 
145*cf7f2e2dSJohn Marino   /* For most architectures, calling extract_unsigned_integer() alone
146*cf7f2e2dSJohn Marino      is sufficient for extracting an address.  However, some
147*cf7f2e2dSJohn Marino      architectures (e.g. MIPS) use signed addresses and using
148*cf7f2e2dSJohn Marino      extract_unsigned_integer() will not produce a correct
149*cf7f2e2dSJohn Marino      result.  Make sure we invoke gdbarch_integer_to_address()
150*cf7f2e2dSJohn Marino      for those architectures which require it.  */
151*cf7f2e2dSJohn Marino   if (gdbarch_integer_to_address_p (ctx->gdbarch))
152*cf7f2e2dSJohn Marino     {
153*cf7f2e2dSJohn Marino       enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
154*cf7f2e2dSJohn Marino       gdb_byte *buf = alloca (ctx->addr_size);
155*cf7f2e2dSJohn Marino       struct type *int_type;
156*cf7f2e2dSJohn Marino 
157*cf7f2e2dSJohn Marino       switch (ctx->addr_size)
158*cf7f2e2dSJohn Marino 	{
159*cf7f2e2dSJohn Marino 	case 2:
160*cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint16;
161*cf7f2e2dSJohn Marino 	  break;
162*cf7f2e2dSJohn Marino 	case 4:
163*cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint32;
164*cf7f2e2dSJohn Marino 	  break;
165*cf7f2e2dSJohn Marino 	case 8:
166*cf7f2e2dSJohn Marino 	  int_type = builtin_type (ctx->gdbarch)->builtin_uint64;
167*cf7f2e2dSJohn Marino 	  break;
168*cf7f2e2dSJohn Marino 	default:
169*cf7f2e2dSJohn Marino 	  internal_error (__FILE__, __LINE__,
170*cf7f2e2dSJohn Marino 			  _("Unsupported address size.\n"));
171*cf7f2e2dSJohn Marino 	}
172*cf7f2e2dSJohn Marino 
173*cf7f2e2dSJohn Marino       store_unsigned_integer (buf, ctx->addr_size, byte_order, result);
174*cf7f2e2dSJohn Marino       return gdbarch_integer_to_address (ctx->gdbarch, int_type, buf);
175*cf7f2e2dSJohn Marino     }
176*cf7f2e2dSJohn Marino 
177*cf7f2e2dSJohn Marino   return (CORE_ADDR) result;
178*cf7f2e2dSJohn Marino }
179*cf7f2e2dSJohn Marino 
1805796c8dcSSimon Schubert /* Retrieve the in_stack_memory flag of the N'th item on CTX's stack.  */
1815796c8dcSSimon Schubert 
1825796c8dcSSimon Schubert int
1835796c8dcSSimon Schubert dwarf_expr_fetch_in_stack_memory (struct dwarf_expr_context *ctx, int n)
1845796c8dcSSimon Schubert {
1855796c8dcSSimon Schubert   if (ctx->stack_len <= n)
1865796c8dcSSimon Schubert      error (_("Asked for position %d of stack, stack only has %d elements on it."),
1875796c8dcSSimon Schubert 	    n, ctx->stack_len);
1885796c8dcSSimon Schubert   return ctx->stack[ctx->stack_len - (1 + n)].in_stack_memory;
1895796c8dcSSimon Schubert 
1905796c8dcSSimon Schubert }
1915796c8dcSSimon Schubert 
192*cf7f2e2dSJohn Marino /* Return true if the expression stack is empty.  */
193*cf7f2e2dSJohn Marino 
194*cf7f2e2dSJohn Marino static int
195*cf7f2e2dSJohn Marino dwarf_expr_stack_empty_p (struct dwarf_expr_context *ctx)
196*cf7f2e2dSJohn Marino {
197*cf7f2e2dSJohn Marino   return ctx->stack_len == 0;
198*cf7f2e2dSJohn Marino }
199*cf7f2e2dSJohn Marino 
2005796c8dcSSimon Schubert /* Add a new piece to CTX's piece list.  */
2015796c8dcSSimon Schubert static void
202*cf7f2e2dSJohn Marino add_piece (struct dwarf_expr_context *ctx, ULONGEST size, ULONGEST offset)
2035796c8dcSSimon Schubert {
2045796c8dcSSimon Schubert   struct dwarf_expr_piece *p;
2055796c8dcSSimon Schubert 
2065796c8dcSSimon Schubert   ctx->num_pieces++;
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert   ctx->pieces = xrealloc (ctx->pieces,
2095796c8dcSSimon Schubert 			  (ctx->num_pieces
2105796c8dcSSimon Schubert 			   * sizeof (struct dwarf_expr_piece)));
2115796c8dcSSimon Schubert 
2125796c8dcSSimon Schubert   p = &ctx->pieces[ctx->num_pieces - 1];
2135796c8dcSSimon Schubert   p->location = ctx->location;
2145796c8dcSSimon Schubert   p->size = size;
215*cf7f2e2dSJohn Marino   p->offset = offset;
216*cf7f2e2dSJohn Marino 
2175796c8dcSSimon Schubert   if (p->location == DWARF_VALUE_LITERAL)
2185796c8dcSSimon Schubert     {
2195796c8dcSSimon Schubert       p->v.literal.data = ctx->data;
2205796c8dcSSimon Schubert       p->v.literal.length = ctx->len;
2215796c8dcSSimon Schubert     }
222*cf7f2e2dSJohn Marino   else if (dwarf_expr_stack_empty_p (ctx))
223*cf7f2e2dSJohn Marino     {
224*cf7f2e2dSJohn Marino       p->location = DWARF_VALUE_OPTIMIZED_OUT;
225*cf7f2e2dSJohn Marino       /* Also reset the context's location, for our callers.  This is
226*cf7f2e2dSJohn Marino 	 a somewhat strange approach, but this lets us avoid setting
227*cf7f2e2dSJohn Marino 	 the location to DWARF_VALUE_MEMORY in all the individual
228*cf7f2e2dSJohn Marino 	 cases in the evaluator.  */
229*cf7f2e2dSJohn Marino       ctx->location = DWARF_VALUE_OPTIMIZED_OUT;
230*cf7f2e2dSJohn Marino     }
231*cf7f2e2dSJohn Marino   else if (p->location == DWARF_VALUE_MEMORY)
232*cf7f2e2dSJohn Marino     {
233*cf7f2e2dSJohn Marino       p->v.mem.addr = dwarf_expr_fetch_address (ctx, 0);
234*cf7f2e2dSJohn Marino       p->v.mem.in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
235*cf7f2e2dSJohn Marino     }
2365796c8dcSSimon Schubert   else
2375796c8dcSSimon Schubert     {
238*cf7f2e2dSJohn Marino       p->v.value = dwarf_expr_fetch (ctx, 0);
2395796c8dcSSimon Schubert     }
2405796c8dcSSimon Schubert }
2415796c8dcSSimon Schubert 
2425796c8dcSSimon Schubert /* Evaluate the expression at ADDR (LEN bytes long) using the context
2435796c8dcSSimon Schubert    CTX.  */
2445796c8dcSSimon Schubert 
2455796c8dcSSimon Schubert void
246*cf7f2e2dSJohn Marino dwarf_expr_eval (struct dwarf_expr_context *ctx, const gdb_byte *addr,
247*cf7f2e2dSJohn Marino 		 size_t len)
2485796c8dcSSimon Schubert {
2495796c8dcSSimon Schubert   int old_recursion_depth = ctx->recursion_depth;
2505796c8dcSSimon Schubert 
2515796c8dcSSimon Schubert   execute_stack_op (ctx, addr, addr + len);
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert   /* CTX RECURSION_DEPTH becomes invalid if an exception was thrown here.  */
2545796c8dcSSimon Schubert 
2555796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth == old_recursion_depth);
2565796c8dcSSimon Schubert }
2575796c8dcSSimon Schubert 
2585796c8dcSSimon Schubert /* Decode the unsigned LEB128 constant at BUF into the variable pointed to
2595796c8dcSSimon Schubert    by R, and return the new value of BUF.  Verify that it doesn't extend
2605796c8dcSSimon Schubert    past BUF_END.  */
2615796c8dcSSimon Schubert 
262*cf7f2e2dSJohn Marino const gdb_byte *
263*cf7f2e2dSJohn Marino read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end, ULONGEST * r)
2645796c8dcSSimon Schubert {
2655796c8dcSSimon Schubert   unsigned shift = 0;
2665796c8dcSSimon Schubert   ULONGEST result = 0;
2675796c8dcSSimon Schubert   gdb_byte byte;
2685796c8dcSSimon Schubert 
2695796c8dcSSimon Schubert   while (1)
2705796c8dcSSimon Schubert     {
2715796c8dcSSimon Schubert       if (buf >= buf_end)
2725796c8dcSSimon Schubert 	error (_("read_uleb128: Corrupted DWARF expression."));
2735796c8dcSSimon Schubert 
2745796c8dcSSimon Schubert       byte = *buf++;
2755796c8dcSSimon Schubert       result |= (byte & 0x7f) << shift;
2765796c8dcSSimon Schubert       if ((byte & 0x80) == 0)
2775796c8dcSSimon Schubert 	break;
2785796c8dcSSimon Schubert       shift += 7;
2795796c8dcSSimon Schubert     }
2805796c8dcSSimon Schubert   *r = result;
2815796c8dcSSimon Schubert   return buf;
2825796c8dcSSimon Schubert }
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert /* Decode the signed LEB128 constant at BUF into the variable pointed to
2855796c8dcSSimon Schubert    by R, and return the new value of BUF.  Verify that it doesn't extend
2865796c8dcSSimon Schubert    past BUF_END.  */
2875796c8dcSSimon Schubert 
288*cf7f2e2dSJohn Marino const gdb_byte *
289*cf7f2e2dSJohn Marino read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end, LONGEST * r)
2905796c8dcSSimon Schubert {
2915796c8dcSSimon Schubert   unsigned shift = 0;
2925796c8dcSSimon Schubert   LONGEST result = 0;
2935796c8dcSSimon Schubert   gdb_byte byte;
2945796c8dcSSimon Schubert 
2955796c8dcSSimon Schubert   while (1)
2965796c8dcSSimon Schubert     {
2975796c8dcSSimon Schubert       if (buf >= buf_end)
2985796c8dcSSimon Schubert 	error (_("read_sleb128: Corrupted DWARF expression."));
2995796c8dcSSimon Schubert 
3005796c8dcSSimon Schubert       byte = *buf++;
3015796c8dcSSimon Schubert       result |= (byte & 0x7f) << shift;
3025796c8dcSSimon Schubert       shift += 7;
3035796c8dcSSimon Schubert       if ((byte & 0x80) == 0)
3045796c8dcSSimon Schubert 	break;
3055796c8dcSSimon Schubert     }
3065796c8dcSSimon Schubert   if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0)
3075796c8dcSSimon Schubert     result |= -(1 << shift);
3085796c8dcSSimon Schubert 
3095796c8dcSSimon Schubert   *r = result;
3105796c8dcSSimon Schubert   return buf;
3115796c8dcSSimon Schubert }
3125796c8dcSSimon Schubert 
3135796c8dcSSimon Schubert 
3145796c8dcSSimon Schubert /* Check that the current operator is either at the end of an
3155796c8dcSSimon Schubert    expression, or that it is followed by a composition operator.  */
3165796c8dcSSimon Schubert 
317*cf7f2e2dSJohn Marino void
318*cf7f2e2dSJohn Marino dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
319*cf7f2e2dSJohn Marino 				const char *op_name)
3205796c8dcSSimon Schubert {
3215796c8dcSSimon Schubert   /* It seems like DW_OP_GNU_uninit should be handled here.  However,
3225796c8dcSSimon Schubert      it doesn't seem to make sense for DW_OP_*_value, and it was not
3235796c8dcSSimon Schubert      checked at the other place that this function is called.  */
3245796c8dcSSimon Schubert   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece)
3255796c8dcSSimon Schubert     error (_("DWARF-2 expression error: `%s' operations must be "
3265796c8dcSSimon Schubert 	     "used either alone or in conjuction with DW_OP_piece "
3275796c8dcSSimon Schubert 	     "or DW_OP_bit_piece."),
3285796c8dcSSimon Schubert 	   op_name);
3295796c8dcSSimon Schubert }
3305796c8dcSSimon Schubert 
3315796c8dcSSimon Schubert /* The engine for the expression evaluator.  Using the context in CTX,
3325796c8dcSSimon Schubert    evaluate the expression between OP_PTR and OP_END.  */
3335796c8dcSSimon Schubert 
3345796c8dcSSimon Schubert static void
3355796c8dcSSimon Schubert execute_stack_op (struct dwarf_expr_context *ctx,
336*cf7f2e2dSJohn Marino 		  const gdb_byte *op_ptr, const gdb_byte *op_end)
3375796c8dcSSimon Schubert {
338*cf7f2e2dSJohn Marino #define sign_ext(x) ((LONGEST) (((x) ^ sign_bit) - sign_bit))
339*cf7f2e2dSJohn Marino   ULONGEST sign_bit = (ctx->addr_size >= sizeof (ULONGEST) ? 0
340*cf7f2e2dSJohn Marino 		       : ((ULONGEST) 1) << (ctx->addr_size * 8 - 1));
3415796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (ctx->gdbarch);
342*cf7f2e2dSJohn Marino 
3435796c8dcSSimon Schubert   ctx->location = DWARF_VALUE_MEMORY;
3445796c8dcSSimon Schubert   ctx->initialized = 1;  /* Default is initialized.  */
3455796c8dcSSimon Schubert 
3465796c8dcSSimon Schubert   if (ctx->recursion_depth > ctx->max_recursion_depth)
3475796c8dcSSimon Schubert     error (_("DWARF-2 expression error: Loop detected (%d)."),
3485796c8dcSSimon Schubert 	   ctx->recursion_depth);
3495796c8dcSSimon Schubert   ctx->recursion_depth++;
3505796c8dcSSimon Schubert 
3515796c8dcSSimon Schubert   while (op_ptr < op_end)
3525796c8dcSSimon Schubert     {
3535796c8dcSSimon Schubert       enum dwarf_location_atom op = *op_ptr++;
354*cf7f2e2dSJohn Marino       ULONGEST result;
3555796c8dcSSimon Schubert       /* Assume the value is not in stack memory.
3565796c8dcSSimon Schubert 	 Code that knows otherwise sets this to 1.
3575796c8dcSSimon Schubert 	 Some arithmetic on stack addresses can probably be assumed to still
3585796c8dcSSimon Schubert 	 be a stack address, but we skip this complication for now.
3595796c8dcSSimon Schubert 	 This is just an optimization, so it's always ok to punt
3605796c8dcSSimon Schubert 	 and leave this as 0.  */
3615796c8dcSSimon Schubert       int in_stack_memory = 0;
3625796c8dcSSimon Schubert       ULONGEST uoffset, reg;
3635796c8dcSSimon Schubert       LONGEST offset;
3645796c8dcSSimon Schubert 
3655796c8dcSSimon Schubert       switch (op)
3665796c8dcSSimon Schubert 	{
3675796c8dcSSimon Schubert 	case DW_OP_lit0:
3685796c8dcSSimon Schubert 	case DW_OP_lit1:
3695796c8dcSSimon Schubert 	case DW_OP_lit2:
3705796c8dcSSimon Schubert 	case DW_OP_lit3:
3715796c8dcSSimon Schubert 	case DW_OP_lit4:
3725796c8dcSSimon Schubert 	case DW_OP_lit5:
3735796c8dcSSimon Schubert 	case DW_OP_lit6:
3745796c8dcSSimon Schubert 	case DW_OP_lit7:
3755796c8dcSSimon Schubert 	case DW_OP_lit8:
3765796c8dcSSimon Schubert 	case DW_OP_lit9:
3775796c8dcSSimon Schubert 	case DW_OP_lit10:
3785796c8dcSSimon Schubert 	case DW_OP_lit11:
3795796c8dcSSimon Schubert 	case DW_OP_lit12:
3805796c8dcSSimon Schubert 	case DW_OP_lit13:
3815796c8dcSSimon Schubert 	case DW_OP_lit14:
3825796c8dcSSimon Schubert 	case DW_OP_lit15:
3835796c8dcSSimon Schubert 	case DW_OP_lit16:
3845796c8dcSSimon Schubert 	case DW_OP_lit17:
3855796c8dcSSimon Schubert 	case DW_OP_lit18:
3865796c8dcSSimon Schubert 	case DW_OP_lit19:
3875796c8dcSSimon Schubert 	case DW_OP_lit20:
3885796c8dcSSimon Schubert 	case DW_OP_lit21:
3895796c8dcSSimon Schubert 	case DW_OP_lit22:
3905796c8dcSSimon Schubert 	case DW_OP_lit23:
3915796c8dcSSimon Schubert 	case DW_OP_lit24:
3925796c8dcSSimon Schubert 	case DW_OP_lit25:
3935796c8dcSSimon Schubert 	case DW_OP_lit26:
3945796c8dcSSimon Schubert 	case DW_OP_lit27:
3955796c8dcSSimon Schubert 	case DW_OP_lit28:
3965796c8dcSSimon Schubert 	case DW_OP_lit29:
3975796c8dcSSimon Schubert 	case DW_OP_lit30:
3985796c8dcSSimon Schubert 	case DW_OP_lit31:
3995796c8dcSSimon Schubert 	  result = op - DW_OP_lit0;
4005796c8dcSSimon Schubert 	  break;
4015796c8dcSSimon Schubert 
4025796c8dcSSimon Schubert 	case DW_OP_addr:
403*cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr,
404*cf7f2e2dSJohn Marino 					     ctx->addr_size, byte_order);
4055796c8dcSSimon Schubert 	  op_ptr += ctx->addr_size;
406*cf7f2e2dSJohn Marino 	  /* Some versions of GCC emit DW_OP_addr before
407*cf7f2e2dSJohn Marino 	     DW_OP_GNU_push_tls_address.  In this case the value is an
408*cf7f2e2dSJohn Marino 	     index, not an address.  We don't support things like
409*cf7f2e2dSJohn Marino 	     branching between the address and the TLS op.  */
410*cf7f2e2dSJohn Marino 	  if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
411*cf7f2e2dSJohn Marino 	    result += ctx->offset;
4125796c8dcSSimon Schubert 	  break;
4135796c8dcSSimon Schubert 
4145796c8dcSSimon Schubert 	case DW_OP_const1u:
4155796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 1, byte_order);
4165796c8dcSSimon Schubert 	  op_ptr += 1;
4175796c8dcSSimon Schubert 	  break;
4185796c8dcSSimon Schubert 	case DW_OP_const1s:
4195796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 1, byte_order);
4205796c8dcSSimon Schubert 	  op_ptr += 1;
4215796c8dcSSimon Schubert 	  break;
4225796c8dcSSimon Schubert 	case DW_OP_const2u:
4235796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 2, byte_order);
4245796c8dcSSimon Schubert 	  op_ptr += 2;
4255796c8dcSSimon Schubert 	  break;
4265796c8dcSSimon Schubert 	case DW_OP_const2s:
4275796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 2, byte_order);
4285796c8dcSSimon Schubert 	  op_ptr += 2;
4295796c8dcSSimon Schubert 	  break;
4305796c8dcSSimon Schubert 	case DW_OP_const4u:
4315796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 4, byte_order);
4325796c8dcSSimon Schubert 	  op_ptr += 4;
4335796c8dcSSimon Schubert 	  break;
4345796c8dcSSimon Schubert 	case DW_OP_const4s:
4355796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 4, byte_order);
4365796c8dcSSimon Schubert 	  op_ptr += 4;
4375796c8dcSSimon Schubert 	  break;
4385796c8dcSSimon Schubert 	case DW_OP_const8u:
4395796c8dcSSimon Schubert 	  result = extract_unsigned_integer (op_ptr, 8, byte_order);
4405796c8dcSSimon Schubert 	  op_ptr += 8;
4415796c8dcSSimon Schubert 	  break;
4425796c8dcSSimon Schubert 	case DW_OP_const8s:
4435796c8dcSSimon Schubert 	  result = extract_signed_integer (op_ptr, 8, byte_order);
4445796c8dcSSimon Schubert 	  op_ptr += 8;
4455796c8dcSSimon Schubert 	  break;
4465796c8dcSSimon Schubert 	case DW_OP_constu:
4475796c8dcSSimon Schubert 	  op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
4485796c8dcSSimon Schubert 	  result = uoffset;
4495796c8dcSSimon Schubert 	  break;
4505796c8dcSSimon Schubert 	case DW_OP_consts:
4515796c8dcSSimon Schubert 	  op_ptr = read_sleb128 (op_ptr, op_end, &offset);
4525796c8dcSSimon Schubert 	  result = offset;
4535796c8dcSSimon Schubert 	  break;
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert 	/* The DW_OP_reg operations are required to occur alone in
4565796c8dcSSimon Schubert 	   location expressions.  */
4575796c8dcSSimon Schubert 	case DW_OP_reg0:
4585796c8dcSSimon Schubert 	case DW_OP_reg1:
4595796c8dcSSimon Schubert 	case DW_OP_reg2:
4605796c8dcSSimon Schubert 	case DW_OP_reg3:
4615796c8dcSSimon Schubert 	case DW_OP_reg4:
4625796c8dcSSimon Schubert 	case DW_OP_reg5:
4635796c8dcSSimon Schubert 	case DW_OP_reg6:
4645796c8dcSSimon Schubert 	case DW_OP_reg7:
4655796c8dcSSimon Schubert 	case DW_OP_reg8:
4665796c8dcSSimon Schubert 	case DW_OP_reg9:
4675796c8dcSSimon Schubert 	case DW_OP_reg10:
4685796c8dcSSimon Schubert 	case DW_OP_reg11:
4695796c8dcSSimon Schubert 	case DW_OP_reg12:
4705796c8dcSSimon Schubert 	case DW_OP_reg13:
4715796c8dcSSimon Schubert 	case DW_OP_reg14:
4725796c8dcSSimon Schubert 	case DW_OP_reg15:
4735796c8dcSSimon Schubert 	case DW_OP_reg16:
4745796c8dcSSimon Schubert 	case DW_OP_reg17:
4755796c8dcSSimon Schubert 	case DW_OP_reg18:
4765796c8dcSSimon Schubert 	case DW_OP_reg19:
4775796c8dcSSimon Schubert 	case DW_OP_reg20:
4785796c8dcSSimon Schubert 	case DW_OP_reg21:
4795796c8dcSSimon Schubert 	case DW_OP_reg22:
4805796c8dcSSimon Schubert 	case DW_OP_reg23:
4815796c8dcSSimon Schubert 	case DW_OP_reg24:
4825796c8dcSSimon Schubert 	case DW_OP_reg25:
4835796c8dcSSimon Schubert 	case DW_OP_reg26:
4845796c8dcSSimon Schubert 	case DW_OP_reg27:
4855796c8dcSSimon Schubert 	case DW_OP_reg28:
4865796c8dcSSimon Schubert 	case DW_OP_reg29:
4875796c8dcSSimon Schubert 	case DW_OP_reg30:
4885796c8dcSSimon Schubert 	case DW_OP_reg31:
4895796c8dcSSimon Schubert 	  if (op_ptr != op_end
4905796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_piece
491*cf7f2e2dSJohn Marino 	      && *op_ptr != DW_OP_bit_piece
4925796c8dcSSimon Schubert 	      && *op_ptr != DW_OP_GNU_uninit)
4935796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_reg operations must be "
494*cf7f2e2dSJohn Marino 		     "used either alone or in conjuction with DW_OP_piece "
495*cf7f2e2dSJohn Marino 		     "or DW_OP_bit_piece."));
4965796c8dcSSimon Schubert 
4975796c8dcSSimon Schubert 	  result = op - DW_OP_reg0;
4985796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
4995796c8dcSSimon Schubert 	  break;
5005796c8dcSSimon Schubert 
5015796c8dcSSimon Schubert 	case DW_OP_regx:
5025796c8dcSSimon Schubert 	  op_ptr = read_uleb128 (op_ptr, op_end, &reg);
503*cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
5045796c8dcSSimon Schubert 
5055796c8dcSSimon Schubert 	  result = reg;
5065796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_REGISTER;
5075796c8dcSSimon Schubert 	  break;
5085796c8dcSSimon Schubert 
5095796c8dcSSimon Schubert 	case DW_OP_implicit_value:
5105796c8dcSSimon Schubert 	  {
5115796c8dcSSimon Schubert 	    ULONGEST len;
512*cf7f2e2dSJohn Marino 
5135796c8dcSSimon Schubert 	    op_ptr = read_uleb128 (op_ptr, op_end, &len);
5145796c8dcSSimon Schubert 	    if (op_ptr + len > op_end)
5155796c8dcSSimon Schubert 	      error (_("DW_OP_implicit_value: too few bytes available."));
5165796c8dcSSimon Schubert 	    ctx->len = len;
5175796c8dcSSimon Schubert 	    ctx->data = op_ptr;
5185796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_LITERAL;
5195796c8dcSSimon Schubert 	    op_ptr += len;
520*cf7f2e2dSJohn Marino 	    dwarf_expr_require_composition (op_ptr, op_end,
521*cf7f2e2dSJohn Marino 					    "DW_OP_implicit_value");
5225796c8dcSSimon Schubert 	  }
5235796c8dcSSimon Schubert 	  goto no_push;
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert 	case DW_OP_stack_value:
5265796c8dcSSimon Schubert 	  ctx->location = DWARF_VALUE_STACK;
527*cf7f2e2dSJohn Marino 	  dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
5285796c8dcSSimon Schubert 	  goto no_push;
5295796c8dcSSimon Schubert 
5305796c8dcSSimon Schubert 	case DW_OP_breg0:
5315796c8dcSSimon Schubert 	case DW_OP_breg1:
5325796c8dcSSimon Schubert 	case DW_OP_breg2:
5335796c8dcSSimon Schubert 	case DW_OP_breg3:
5345796c8dcSSimon Schubert 	case DW_OP_breg4:
5355796c8dcSSimon Schubert 	case DW_OP_breg5:
5365796c8dcSSimon Schubert 	case DW_OP_breg6:
5375796c8dcSSimon Schubert 	case DW_OP_breg7:
5385796c8dcSSimon Schubert 	case DW_OP_breg8:
5395796c8dcSSimon Schubert 	case DW_OP_breg9:
5405796c8dcSSimon Schubert 	case DW_OP_breg10:
5415796c8dcSSimon Schubert 	case DW_OP_breg11:
5425796c8dcSSimon Schubert 	case DW_OP_breg12:
5435796c8dcSSimon Schubert 	case DW_OP_breg13:
5445796c8dcSSimon Schubert 	case DW_OP_breg14:
5455796c8dcSSimon Schubert 	case DW_OP_breg15:
5465796c8dcSSimon Schubert 	case DW_OP_breg16:
5475796c8dcSSimon Schubert 	case DW_OP_breg17:
5485796c8dcSSimon Schubert 	case DW_OP_breg18:
5495796c8dcSSimon Schubert 	case DW_OP_breg19:
5505796c8dcSSimon Schubert 	case DW_OP_breg20:
5515796c8dcSSimon Schubert 	case DW_OP_breg21:
5525796c8dcSSimon Schubert 	case DW_OP_breg22:
5535796c8dcSSimon Schubert 	case DW_OP_breg23:
5545796c8dcSSimon Schubert 	case DW_OP_breg24:
5555796c8dcSSimon Schubert 	case DW_OP_breg25:
5565796c8dcSSimon Schubert 	case DW_OP_breg26:
5575796c8dcSSimon Schubert 	case DW_OP_breg27:
5585796c8dcSSimon Schubert 	case DW_OP_breg28:
5595796c8dcSSimon Schubert 	case DW_OP_breg29:
5605796c8dcSSimon Schubert 	case DW_OP_breg30:
5615796c8dcSSimon Schubert 	case DW_OP_breg31:
5625796c8dcSSimon Schubert 	  {
5635796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
5645796c8dcSSimon Schubert 	    result = (ctx->read_reg) (ctx->baton, op - DW_OP_breg0);
5655796c8dcSSimon Schubert 	    result += offset;
5665796c8dcSSimon Schubert 	  }
5675796c8dcSSimon Schubert 	  break;
5685796c8dcSSimon Schubert 	case DW_OP_bregx:
5695796c8dcSSimon Schubert 	  {
5705796c8dcSSimon Schubert 	    op_ptr = read_uleb128 (op_ptr, op_end, &reg);
5715796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
5725796c8dcSSimon Schubert 	    result = (ctx->read_reg) (ctx->baton, reg);
5735796c8dcSSimon Schubert 	    result += offset;
5745796c8dcSSimon Schubert 	  }
5755796c8dcSSimon Schubert 	  break;
5765796c8dcSSimon Schubert 	case DW_OP_fbreg:
5775796c8dcSSimon Schubert 	  {
578*cf7f2e2dSJohn Marino 	    const gdb_byte *datastart;
5795796c8dcSSimon Schubert 	    size_t datalen;
5805796c8dcSSimon Schubert 	    unsigned int before_stack_len;
5815796c8dcSSimon Schubert 
5825796c8dcSSimon Schubert 	    op_ptr = read_sleb128 (op_ptr, op_end, &offset);
5835796c8dcSSimon Schubert 	    /* Rather than create a whole new context, we simply
5845796c8dcSSimon Schubert 	       record the stack length before execution, then reset it
5855796c8dcSSimon Schubert 	       afterwards, effectively erasing whatever the recursive
5865796c8dcSSimon Schubert 	       call put there.  */
5875796c8dcSSimon Schubert 	    before_stack_len = ctx->stack_len;
5885796c8dcSSimon Schubert 	    /* FIXME: cagney/2003-03-26: This code should be using
5895796c8dcSSimon Schubert                get_frame_base_address(), and then implement a dwarf2
5905796c8dcSSimon Schubert                specific this_base method.  */
5915796c8dcSSimon Schubert 	    (ctx->get_frame_base) (ctx->baton, &datastart, &datalen);
5925796c8dcSSimon Schubert 	    dwarf_expr_eval (ctx, datastart, datalen);
593*cf7f2e2dSJohn Marino 	    if (ctx->location == DWARF_VALUE_MEMORY)
594*cf7f2e2dSJohn Marino 	      result = dwarf_expr_fetch_address (ctx, 0);
595*cf7f2e2dSJohn Marino 	    else if (ctx->location == DWARF_VALUE_REGISTER)
596*cf7f2e2dSJohn Marino 	      result = (ctx->read_reg) (ctx->baton, dwarf_expr_fetch (ctx, 0));
597*cf7f2e2dSJohn Marino 	    else
5985796c8dcSSimon Schubert 	      error (_("Not implemented: computing frame base using explicit value operator"));
5995796c8dcSSimon Schubert 	    result = result + offset;
6005796c8dcSSimon Schubert 	    in_stack_memory = 1;
6015796c8dcSSimon Schubert 	    ctx->stack_len = before_stack_len;
6025796c8dcSSimon Schubert 	    ctx->location = DWARF_VALUE_MEMORY;
6035796c8dcSSimon Schubert 	  }
6045796c8dcSSimon Schubert 	  break;
6055796c8dcSSimon Schubert 
6065796c8dcSSimon Schubert 	case DW_OP_dup:
6075796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
6085796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
6095796c8dcSSimon Schubert 	  break;
6105796c8dcSSimon Schubert 
6115796c8dcSSimon Schubert 	case DW_OP_drop:
6125796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
6135796c8dcSSimon Schubert 	  goto no_push;
6145796c8dcSSimon Schubert 
6155796c8dcSSimon Schubert 	case DW_OP_pick:
6165796c8dcSSimon Schubert 	  offset = *op_ptr++;
6175796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, offset);
6185796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, offset);
6195796c8dcSSimon Schubert 	  break;
6205796c8dcSSimon Schubert 
6215796c8dcSSimon Schubert 	case DW_OP_swap:
6225796c8dcSSimon Schubert 	  {
6235796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2;
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert 	    if (ctx->stack_len < 2)
6265796c8dcSSimon Schubert 	       error (_("Not enough elements for DW_OP_swap. Need 2, have %d."),
6275796c8dcSSimon Schubert 		      ctx->stack_len);
6285796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
6295796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
6305796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
6315796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t1;
6325796c8dcSSimon Schubert 	    goto no_push;
6335796c8dcSSimon Schubert 	  }
6345796c8dcSSimon Schubert 
6355796c8dcSSimon Schubert 	case DW_OP_over:
6365796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 1);
6375796c8dcSSimon Schubert 	  in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 1);
6385796c8dcSSimon Schubert 	  break;
6395796c8dcSSimon Schubert 
6405796c8dcSSimon Schubert 	case DW_OP_rot:
6415796c8dcSSimon Schubert 	  {
6425796c8dcSSimon Schubert 	    struct dwarf_stack_value t1, t2, t3;
6435796c8dcSSimon Schubert 
6445796c8dcSSimon Schubert 	    if (ctx->stack_len < 3)
6455796c8dcSSimon Schubert 	       error (_("Not enough elements for DW_OP_rot. Need 3, have %d."),
6465796c8dcSSimon Schubert 		      ctx->stack_len);
6475796c8dcSSimon Schubert 	    t1 = ctx->stack[ctx->stack_len - 1];
6485796c8dcSSimon Schubert 	    t2 = ctx->stack[ctx->stack_len - 2];
6495796c8dcSSimon Schubert 	    t3 = ctx->stack[ctx->stack_len - 3];
6505796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 1] = t2;
6515796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 2] = t3;
6525796c8dcSSimon Schubert 	    ctx->stack[ctx->stack_len - 3] = t1;
6535796c8dcSSimon Schubert 	    goto no_push;
6545796c8dcSSimon Schubert 	  }
6555796c8dcSSimon Schubert 
6565796c8dcSSimon Schubert 	case DW_OP_deref:
6575796c8dcSSimon Schubert 	case DW_OP_deref_size:
658*cf7f2e2dSJohn Marino 	  {
659*cf7f2e2dSJohn Marino 	    int addr_size = (op == DW_OP_deref ? ctx->addr_size : *op_ptr++);
660*cf7f2e2dSJohn Marino 	    gdb_byte *buf = alloca (addr_size);
661*cf7f2e2dSJohn Marino 	    CORE_ADDR addr = dwarf_expr_fetch_address (ctx, 0);
662*cf7f2e2dSJohn Marino 	    dwarf_expr_pop (ctx);
663*cf7f2e2dSJohn Marino 
664*cf7f2e2dSJohn Marino 	    (ctx->read_mem) (ctx->baton, buf, addr, addr_size);
665*cf7f2e2dSJohn Marino 	    result = extract_unsigned_integer (buf, addr_size, byte_order);
666*cf7f2e2dSJohn Marino 	    break;
667*cf7f2e2dSJohn Marino 	  }
668*cf7f2e2dSJohn Marino 
6695796c8dcSSimon Schubert 	case DW_OP_abs:
6705796c8dcSSimon Schubert 	case DW_OP_neg:
6715796c8dcSSimon Schubert 	case DW_OP_not:
6725796c8dcSSimon Schubert 	case DW_OP_plus_uconst:
6735796c8dcSSimon Schubert 	  /* Unary operations.  */
6745796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
6755796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
6765796c8dcSSimon Schubert 
6775796c8dcSSimon Schubert 	  switch (op)
6785796c8dcSSimon Schubert 	    {
6795796c8dcSSimon Schubert 	    case DW_OP_abs:
680*cf7f2e2dSJohn Marino 	      if (sign_ext (result) < 0)
6815796c8dcSSimon Schubert 		result = -result;
6825796c8dcSSimon Schubert 	      break;
6835796c8dcSSimon Schubert 	    case DW_OP_neg:
6845796c8dcSSimon Schubert 	      result = -result;
6855796c8dcSSimon Schubert 	      break;
6865796c8dcSSimon Schubert 	    case DW_OP_not:
6875796c8dcSSimon Schubert 	      result = ~result;
6885796c8dcSSimon Schubert 	      break;
6895796c8dcSSimon Schubert 	    case DW_OP_plus_uconst:
6905796c8dcSSimon Schubert 	      op_ptr = read_uleb128 (op_ptr, op_end, &reg);
6915796c8dcSSimon Schubert 	      result += reg;
6925796c8dcSSimon Schubert 	      break;
6935796c8dcSSimon Schubert 	    }
6945796c8dcSSimon Schubert 	  break;
6955796c8dcSSimon Schubert 
6965796c8dcSSimon Schubert 	case DW_OP_and:
6975796c8dcSSimon Schubert 	case DW_OP_div:
6985796c8dcSSimon Schubert 	case DW_OP_minus:
6995796c8dcSSimon Schubert 	case DW_OP_mod:
7005796c8dcSSimon Schubert 	case DW_OP_mul:
7015796c8dcSSimon Schubert 	case DW_OP_or:
7025796c8dcSSimon Schubert 	case DW_OP_plus:
7035796c8dcSSimon Schubert 	case DW_OP_shl:
7045796c8dcSSimon Schubert 	case DW_OP_shr:
7055796c8dcSSimon Schubert 	case DW_OP_shra:
7065796c8dcSSimon Schubert 	case DW_OP_xor:
7075796c8dcSSimon Schubert 	case DW_OP_le:
7085796c8dcSSimon Schubert 	case DW_OP_ge:
7095796c8dcSSimon Schubert 	case DW_OP_eq:
7105796c8dcSSimon Schubert 	case DW_OP_lt:
7115796c8dcSSimon Schubert 	case DW_OP_gt:
7125796c8dcSSimon Schubert 	case DW_OP_ne:
7135796c8dcSSimon Schubert 	  {
714*cf7f2e2dSJohn Marino 	    /* Binary operations.  */
715*cf7f2e2dSJohn Marino 	    ULONGEST first, second;
7165796c8dcSSimon Schubert 
7175796c8dcSSimon Schubert 	    second = dwarf_expr_fetch (ctx, 0);
7185796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
7195796c8dcSSimon Schubert 
7205796c8dcSSimon Schubert 	    first = dwarf_expr_fetch (ctx, 0);
7215796c8dcSSimon Schubert 	    dwarf_expr_pop (ctx);
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert 	    switch (op)
7245796c8dcSSimon Schubert 	      {
7255796c8dcSSimon Schubert 	      case DW_OP_and:
726*cf7f2e2dSJohn Marino 		result = first & second;
7275796c8dcSSimon Schubert 		break;
7285796c8dcSSimon Schubert 	      case DW_OP_div:
729*cf7f2e2dSJohn Marino 		if (!second)
730*cf7f2e2dSJohn Marino 		  error (_("Division by zero"));
731*cf7f2e2dSJohn Marino 		result = sign_ext (first) / sign_ext (second);
7325796c8dcSSimon Schubert                 break;
7335796c8dcSSimon Schubert 	      case DW_OP_minus:
734*cf7f2e2dSJohn Marino 		result = first - second;
7355796c8dcSSimon Schubert 		break;
7365796c8dcSSimon Schubert 	      case DW_OP_mod:
737*cf7f2e2dSJohn Marino 		if (!second)
738*cf7f2e2dSJohn Marino 		  error (_("Division by zero"));
739*cf7f2e2dSJohn Marino 		result = first % second;
7405796c8dcSSimon Schubert 		break;
7415796c8dcSSimon Schubert 	      case DW_OP_mul:
742*cf7f2e2dSJohn Marino 		result = first * second;
7435796c8dcSSimon Schubert 		break;
7445796c8dcSSimon Schubert 	      case DW_OP_or:
745*cf7f2e2dSJohn Marino 		result = first | second;
7465796c8dcSSimon Schubert 		break;
7475796c8dcSSimon Schubert 	      case DW_OP_plus:
748*cf7f2e2dSJohn Marino 		result = first + second;
7495796c8dcSSimon Schubert 		break;
7505796c8dcSSimon Schubert 	      case DW_OP_shl:
751*cf7f2e2dSJohn Marino 		result = first << second;
7525796c8dcSSimon Schubert 		break;
7535796c8dcSSimon Schubert 	      case DW_OP_shr:
754*cf7f2e2dSJohn Marino 		result = first >> second;
7555796c8dcSSimon Schubert                 break;
7565796c8dcSSimon Schubert 	      case DW_OP_shra:
757*cf7f2e2dSJohn Marino 		result = sign_ext (first) >> second;
7585796c8dcSSimon Schubert 		break;
7595796c8dcSSimon Schubert 	      case DW_OP_xor:
760*cf7f2e2dSJohn Marino 		result = first ^ second;
7615796c8dcSSimon Schubert 		break;
7625796c8dcSSimon Schubert 	      case DW_OP_le:
763*cf7f2e2dSJohn Marino 		result = sign_ext (first) <= sign_ext (second);
7645796c8dcSSimon Schubert 		break;
7655796c8dcSSimon Schubert 	      case DW_OP_ge:
766*cf7f2e2dSJohn Marino 		result = sign_ext (first) >= sign_ext (second);
7675796c8dcSSimon Schubert 		break;
7685796c8dcSSimon Schubert 	      case DW_OP_eq:
769*cf7f2e2dSJohn Marino 		result = sign_ext (first) == sign_ext (second);
7705796c8dcSSimon Schubert 		break;
7715796c8dcSSimon Schubert 	      case DW_OP_lt:
772*cf7f2e2dSJohn Marino 		result = sign_ext (first) < sign_ext (second);
7735796c8dcSSimon Schubert 		break;
7745796c8dcSSimon Schubert 	      case DW_OP_gt:
775*cf7f2e2dSJohn Marino 		result = sign_ext (first) > sign_ext (second);
7765796c8dcSSimon Schubert 		break;
7775796c8dcSSimon Schubert 	      case DW_OP_ne:
778*cf7f2e2dSJohn Marino 		result = sign_ext (first) != sign_ext (second);
7795796c8dcSSimon Schubert 		break;
7805796c8dcSSimon Schubert 	      default:
7815796c8dcSSimon Schubert 		internal_error (__FILE__, __LINE__,
7825796c8dcSSimon Schubert 				_("Can't be reached."));
7835796c8dcSSimon Schubert 	      }
7845796c8dcSSimon Schubert 	  }
7855796c8dcSSimon Schubert 	  break;
7865796c8dcSSimon Schubert 
7875796c8dcSSimon Schubert 	case DW_OP_call_frame_cfa:
7885796c8dcSSimon Schubert 	  result = (ctx->get_frame_cfa) (ctx->baton);
7895796c8dcSSimon Schubert 	  in_stack_memory = 1;
7905796c8dcSSimon Schubert 	  break;
7915796c8dcSSimon Schubert 
7925796c8dcSSimon Schubert 	case DW_OP_GNU_push_tls_address:
7935796c8dcSSimon Schubert 	  /* Variable is at a constant offset in the thread-local
7945796c8dcSSimon Schubert 	  storage block into the objfile for the current thread and
7955796c8dcSSimon Schubert 	  the dynamic linker module containing this expression. Here
7965796c8dcSSimon Schubert 	  we return returns the offset from that base.  The top of the
7975796c8dcSSimon Schubert 	  stack has the offset from the beginning of the thread
7985796c8dcSSimon Schubert 	  control block at which the variable is located.  Nothing
7995796c8dcSSimon Schubert 	  should follow this operator, so the top of stack would be
8005796c8dcSSimon Schubert 	  returned.  */
8015796c8dcSSimon Schubert 	  result = dwarf_expr_fetch (ctx, 0);
8025796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
8035796c8dcSSimon Schubert 	  result = (ctx->get_tls_address) (ctx->baton, result);
8045796c8dcSSimon Schubert 	  break;
8055796c8dcSSimon Schubert 
8065796c8dcSSimon Schubert 	case DW_OP_skip:
8075796c8dcSSimon Schubert 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
8085796c8dcSSimon Schubert 	  op_ptr += 2;
8095796c8dcSSimon Schubert 	  op_ptr += offset;
8105796c8dcSSimon Schubert 	  goto no_push;
8115796c8dcSSimon Schubert 
8125796c8dcSSimon Schubert 	case DW_OP_bra:
8135796c8dcSSimon Schubert 	  offset = extract_signed_integer (op_ptr, 2, byte_order);
8145796c8dcSSimon Schubert 	  op_ptr += 2;
8155796c8dcSSimon Schubert 	  if (dwarf_expr_fetch (ctx, 0) != 0)
8165796c8dcSSimon Schubert 	    op_ptr += offset;
8175796c8dcSSimon Schubert 	  dwarf_expr_pop (ctx);
8185796c8dcSSimon Schubert 	  goto no_push;
8195796c8dcSSimon Schubert 
8205796c8dcSSimon Schubert 	case DW_OP_nop:
8215796c8dcSSimon Schubert 	  goto no_push;
8225796c8dcSSimon Schubert 
8235796c8dcSSimon Schubert         case DW_OP_piece:
8245796c8dcSSimon Schubert           {
8255796c8dcSSimon Schubert             ULONGEST size;
8265796c8dcSSimon Schubert 
8275796c8dcSSimon Schubert             /* Record the piece.  */
8285796c8dcSSimon Schubert             op_ptr = read_uleb128 (op_ptr, op_end, &size);
829*cf7f2e2dSJohn Marino 	    add_piece (ctx, 8 * size, 0);
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert             /* Pop off the address/regnum, and reset the location
8325796c8dcSSimon Schubert 	       type.  */
833*cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
834*cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
835*cf7f2e2dSJohn Marino 	      dwarf_expr_pop (ctx);
836*cf7f2e2dSJohn Marino             ctx->location = DWARF_VALUE_MEMORY;
837*cf7f2e2dSJohn Marino           }
838*cf7f2e2dSJohn Marino           goto no_push;
839*cf7f2e2dSJohn Marino 
840*cf7f2e2dSJohn Marino 	case DW_OP_bit_piece:
841*cf7f2e2dSJohn Marino 	  {
842*cf7f2e2dSJohn Marino 	    ULONGEST size, offset;
843*cf7f2e2dSJohn Marino 
844*cf7f2e2dSJohn Marino             /* Record the piece.  */
845*cf7f2e2dSJohn Marino 	    op_ptr = read_uleb128 (op_ptr, op_end, &size);
846*cf7f2e2dSJohn Marino 	    op_ptr = read_uleb128 (op_ptr, op_end, &offset);
847*cf7f2e2dSJohn Marino 	    add_piece (ctx, size, offset);
848*cf7f2e2dSJohn Marino 
849*cf7f2e2dSJohn Marino             /* Pop off the address/regnum, and reset the location
850*cf7f2e2dSJohn Marino 	       type.  */
851*cf7f2e2dSJohn Marino 	    if (ctx->location != DWARF_VALUE_LITERAL
852*cf7f2e2dSJohn Marino 		&& ctx->location != DWARF_VALUE_OPTIMIZED_OUT)
8535796c8dcSSimon Schubert 	      dwarf_expr_pop (ctx);
8545796c8dcSSimon Schubert             ctx->location = DWARF_VALUE_MEMORY;
8555796c8dcSSimon Schubert 	  }
8565796c8dcSSimon Schubert 	  goto no_push;
8575796c8dcSSimon Schubert 
8585796c8dcSSimon Schubert 	case DW_OP_GNU_uninit:
8595796c8dcSSimon Schubert 	  if (op_ptr != op_end)
8605796c8dcSSimon Schubert 	    error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
8615796c8dcSSimon Schubert 		   "be the very last op."));
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert 	  ctx->initialized = 0;
8645796c8dcSSimon Schubert 	  goto no_push;
8655796c8dcSSimon Schubert 
866*cf7f2e2dSJohn Marino 	case DW_OP_call2:
867*cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr, 2, byte_order);
868*cf7f2e2dSJohn Marino 	  op_ptr += 2;
869*cf7f2e2dSJohn Marino 	  ctx->dwarf_call (ctx, result);
870*cf7f2e2dSJohn Marino 	  goto no_push;
871*cf7f2e2dSJohn Marino 
872*cf7f2e2dSJohn Marino 	case DW_OP_call4:
873*cf7f2e2dSJohn Marino 	  result = extract_unsigned_integer (op_ptr, 4, byte_order);
874*cf7f2e2dSJohn Marino 	  op_ptr += 4;
875*cf7f2e2dSJohn Marino 	  ctx->dwarf_call (ctx, result);
876*cf7f2e2dSJohn Marino 	  goto no_push;
877*cf7f2e2dSJohn Marino 
8785796c8dcSSimon Schubert 	default:
8795796c8dcSSimon Schubert 	  error (_("Unhandled dwarf expression opcode 0x%x"), op);
8805796c8dcSSimon Schubert 	}
8815796c8dcSSimon Schubert 
8825796c8dcSSimon Schubert       /* Most things push a result value.  */
8835796c8dcSSimon Schubert       dwarf_expr_push (ctx, result, in_stack_memory);
8845796c8dcSSimon Schubert     no_push:;
8855796c8dcSSimon Schubert     }
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert   ctx->recursion_depth--;
8885796c8dcSSimon Schubert   gdb_assert (ctx->recursion_depth >= 0);
889*cf7f2e2dSJohn Marino #undef sign_ext
8905796c8dcSSimon Schubert }
891