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