Convert DWARF expr functions to methods
[external/binutils.git] / gdb / dwarf2expr.c
1 /* DWARF 2 Expression Evaluator.
2
3    Copyright (C) 2001-2016 Free Software Foundation, Inc.
4
5    Contributed by Daniel Berlin (dan@dberlin.org)
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "value.h"
26 #include "gdbcore.h"
27 #include "dwarf2.h"
28 #include "dwarf2expr.h"
29 #include "dwarf2loc.h"
30
31 /* Cookie for gdbarch data.  */
32
33 static struct gdbarch_data *dwarf_arch_cookie;
34
35 /* This holds gdbarch-specific types used by the DWARF expression
36    evaluator.  See comments in execute_stack_op.  */
37
38 struct dwarf_gdbarch_types
39 {
40   struct type *dw_types[3];
41 };
42
43 /* Allocate and fill in dwarf_gdbarch_types for an arch.  */
44
45 static void *
46 dwarf_gdbarch_types_init (struct gdbarch *gdbarch)
47 {
48   struct dwarf_gdbarch_types *types
49     = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct dwarf_gdbarch_types);
50
51   /* The types themselves are lazily initialized.  */
52
53   return types;
54 }
55
56 /* Return the type used for DWARF operations where the type is
57    unspecified in the DWARF spec.  Only certain sizes are
58    supported.  */
59
60 struct type *
61 dwarf_expr_context::address_type () const
62 {
63   struct dwarf_gdbarch_types *types
64     = (struct dwarf_gdbarch_types *) gdbarch_data (this->gdbarch,
65                                                    dwarf_arch_cookie);
66   int ndx;
67
68   if (this->addr_size == 2)
69     ndx = 0;
70   else if (this->addr_size == 4)
71     ndx = 1;
72   else if (this->addr_size == 8)
73     ndx = 2;
74   else
75     error (_("Unsupported address size in DWARF expressions: %d bits"),
76            8 * this->addr_size);
77
78   if (types->dw_types[ndx] == NULL)
79     types->dw_types[ndx]
80       = arch_integer_type (this->gdbarch,
81                            8 * this->addr_size,
82                            0, "<signed DWARF address type>");
83
84   return types->dw_types[ndx];
85 }
86
87 /* Create a new context for the expression evaluator.  */
88
89 dwarf_expr_context::dwarf_expr_context ()
90 : stack (NULL),
91   stack_len (0),
92   stack_allocated (10),
93   gdbarch (NULL),
94   addr_size (0),
95   ref_addr_size (0),
96   offset (0),
97   recursion_depth (0),
98   max_recursion_depth (0x100),
99   location (DWARF_VALUE_MEMORY),
100   len (0),
101   data (NULL),
102   initialized (0),
103   num_pieces (0),
104   pieces (NULL)
105 {
106   this->stack = XNEWVEC (struct dwarf_stack_value, this->stack_allocated);
107 }
108
109 /* Clean up a dwarf_expr_context.  */
110
111 dwarf_expr_context::~dwarf_expr_context ()
112 {
113   xfree (this->stack);
114   xfree (this->pieces);
115 }
116
117 /* Expand the memory allocated stack to contain at least
118    NEED more elements than are currently used.  */
119
120 void
121 dwarf_expr_context::grow_stack (size_t need)
122 {
123   if (this->stack_len + need > this->stack_allocated)
124     {
125       size_t newlen = this->stack_len + need + 10;
126
127       this->stack = XRESIZEVEC (struct dwarf_stack_value, this->stack, newlen);
128       this->stack_allocated = newlen;
129     }
130 }
131
132 /* Push VALUE onto the stack.  */
133
134 void
135 dwarf_expr_context::push (struct value *value, int in_stack_memory)
136 {
137   struct dwarf_stack_value *v;
138
139   grow_stack (1);
140   v = &this->stack[this->stack_len++];
141   v->value = value;
142   v->in_stack_memory = in_stack_memory;
143 }
144
145 /* Push VALUE onto the stack.  */
146
147 void
148 dwarf_expr_context::push_address (CORE_ADDR value, int in_stack_memory)
149 {
150   push (value_from_ulongest (address_type (), value), in_stack_memory);
151 }
152
153 /* Pop the top item off of the stack.  */
154
155 void
156 dwarf_expr_context::pop ()
157 {
158   if (this->stack_len <= 0)
159     error (_("dwarf expression stack underflow"));
160   this->stack_len--;
161 }
162
163 /* Retrieve the N'th item on the stack.  */
164
165 struct value *
166 dwarf_expr_context::fetch (int n)
167 {
168   if (this->stack_len <= n)
169      error (_("Asked for position %d of stack, "
170               "stack only has %d elements on it."),
171             n, this->stack_len);
172   return this->stack[this->stack_len - (1 + n)].value;
173 }
174
175 /* Require that TYPE be an integral type; throw an exception if not.  */
176
177 static void
178 dwarf_require_integral (struct type *type)
179 {
180   if (TYPE_CODE (type) != TYPE_CODE_INT
181       && TYPE_CODE (type) != TYPE_CODE_CHAR
182       && TYPE_CODE (type) != TYPE_CODE_BOOL)
183     error (_("integral type expected in DWARF expression"));
184 }
185
186 /* Return the unsigned form of TYPE.  TYPE is necessarily an integral
187    type.  */
188
189 static struct type *
190 get_unsigned_type (struct gdbarch *gdbarch, struct type *type)
191 {
192   switch (TYPE_LENGTH (type))
193     {
194     case 1:
195       return builtin_type (gdbarch)->builtin_uint8;
196     case 2:
197       return builtin_type (gdbarch)->builtin_uint16;
198     case 4:
199       return builtin_type (gdbarch)->builtin_uint32;
200     case 8:
201       return builtin_type (gdbarch)->builtin_uint64;
202     default:
203       error (_("no unsigned variant found for type, while evaluating "
204                "DWARF expression"));
205     }
206 }
207
208 /* Return the signed form of TYPE.  TYPE is necessarily an integral
209    type.  */
210
211 static struct type *
212 get_signed_type (struct gdbarch *gdbarch, struct type *type)
213 {
214   switch (TYPE_LENGTH (type))
215     {
216     case 1:
217       return builtin_type (gdbarch)->builtin_int8;
218     case 2:
219       return builtin_type (gdbarch)->builtin_int16;
220     case 4:
221       return builtin_type (gdbarch)->builtin_int32;
222     case 8:
223       return builtin_type (gdbarch)->builtin_int64;
224     default:
225       error (_("no signed variant found for type, while evaluating "
226                "DWARF expression"));
227     }
228 }
229
230 /* Retrieve the N'th item on the stack, converted to an address.  */
231
232 CORE_ADDR
233 dwarf_expr_context::fetch_address (int n)
234 {
235   struct value *result_val = fetch (n);
236   enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
237   ULONGEST result;
238
239   dwarf_require_integral (value_type (result_val));
240   result = extract_unsigned_integer (value_contents (result_val),
241                                      TYPE_LENGTH (value_type (result_val)),
242                                      byte_order);
243
244   /* For most architectures, calling extract_unsigned_integer() alone
245      is sufficient for extracting an address.  However, some
246      architectures (e.g. MIPS) use signed addresses and using
247      extract_unsigned_integer() will not produce a correct
248      result.  Make sure we invoke gdbarch_integer_to_address()
249      for those architectures which require it.  */
250   if (gdbarch_integer_to_address_p (this->gdbarch))
251     {
252       gdb_byte *buf = (gdb_byte *) alloca (this->addr_size);
253       struct type *int_type = get_unsigned_type (this->gdbarch,
254                                                  value_type (result_val));
255
256       store_unsigned_integer (buf, this->addr_size, byte_order, result);
257       return gdbarch_integer_to_address (this->gdbarch, int_type, buf);
258     }
259
260   return (CORE_ADDR) result;
261 }
262
263 /* Retrieve the in_stack_memory flag of the N'th item on the stack.  */
264
265 int
266 dwarf_expr_context::fetch_in_stack_memory (int n)
267 {
268   if (this->stack_len <= n)
269      error (_("Asked for position %d of stack, "
270               "stack only has %d elements on it."),
271             n, this->stack_len);
272   return this->stack[this->stack_len - (1 + n)].in_stack_memory;
273 }
274
275 /* Return true if the expression stack is empty.  */
276
277 int
278 dwarf_expr_context::stack_empty_p () const
279 {
280   return this->stack_len == 0;
281 }
282
283 /* Add a new piece to the dwarf_expr_context's piece list.  */
284 void
285 dwarf_expr_context::add_piece (ULONGEST size, ULONGEST offset)
286 {
287   struct dwarf_expr_piece *p;
288
289   this->num_pieces++;
290
291   this->pieces
292     = XRESIZEVEC (struct dwarf_expr_piece, this->pieces, this->num_pieces);
293
294   p = &this->pieces[this->num_pieces - 1];
295   p->location = this->location;
296   p->size = size;
297   p->offset = offset;
298
299   if (p->location == DWARF_VALUE_LITERAL)
300     {
301       p->v.literal.data = this->data;
302       p->v.literal.length = this->len;
303     }
304   else if (stack_empty_p ())
305     {
306       p->location = DWARF_VALUE_OPTIMIZED_OUT;
307       /* Also reset the context's location, for our callers.  This is
308          a somewhat strange approach, but this lets us avoid setting
309          the location to DWARF_VALUE_MEMORY in all the individual
310          cases in the evaluator.  */
311       this->location = DWARF_VALUE_OPTIMIZED_OUT;
312     }
313   else if (p->location == DWARF_VALUE_MEMORY)
314     {
315       p->v.mem.addr = fetch_address (0);
316       p->v.mem.in_stack_memory = fetch_in_stack_memory (0);
317     }
318   else if (p->location == DWARF_VALUE_IMPLICIT_POINTER)
319     {
320       p->v.ptr.die.sect_off = this->len;
321       p->v.ptr.offset = value_as_long (fetch (0));
322     }
323   else if (p->location == DWARF_VALUE_REGISTER)
324     p->v.regno = value_as_long (fetch (0));
325   else
326     {
327       p->v.value = fetch (0);
328     }
329 }
330
331 /* Evaluate the expression at ADDR (LEN bytes long).  */
332
333 void
334 dwarf_expr_context::eval (const gdb_byte *addr, size_t len)
335 {
336   int old_recursion_depth = this->recursion_depth;
337
338   execute_stack_op (addr, addr + len);
339
340   /* RECURSION_DEPTH becomes invalid if an exception was thrown here.  */
341
342   gdb_assert (this->recursion_depth == old_recursion_depth);
343 }
344
345 /* Helper to read a uleb128 value or throw an error.  */
346
347 const gdb_byte *
348 safe_read_uleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
349                    uint64_t *r)
350 {
351   buf = gdb_read_uleb128 (buf, buf_end, r);
352   if (buf == NULL)
353     error (_("DWARF expression error: ran off end of buffer reading uleb128 value"));
354   return buf;
355 }
356
357 /* Helper to read a sleb128 value or throw an error.  */
358
359 const gdb_byte *
360 safe_read_sleb128 (const gdb_byte *buf, const gdb_byte *buf_end,
361                    int64_t *r)
362 {
363   buf = gdb_read_sleb128 (buf, buf_end, r);
364   if (buf == NULL)
365     error (_("DWARF expression error: ran off end of buffer reading sleb128 value"));
366   return buf;
367 }
368
369 const gdb_byte *
370 safe_skip_leb128 (const gdb_byte *buf, const gdb_byte *buf_end)
371 {
372   buf = gdb_skip_leb128 (buf, buf_end);
373   if (buf == NULL)
374     error (_("DWARF expression error: ran off end of buffer reading leb128 value"));
375   return buf;
376 }
377 \f
378
379 /* Check that the current operator is either at the end of an
380    expression, or that it is followed by a composition operator or by
381    DW_OP_GNU_uninit (which should terminate the expression).  */
382
383 void
384 dwarf_expr_require_composition (const gdb_byte *op_ptr, const gdb_byte *op_end,
385                                 const char *op_name)
386 {
387   if (op_ptr != op_end && *op_ptr != DW_OP_piece && *op_ptr != DW_OP_bit_piece
388       && *op_ptr != DW_OP_GNU_uninit)
389     error (_("DWARF-2 expression error: `%s' operations must be "
390              "used either alone or in conjunction with DW_OP_piece "
391              "or DW_OP_bit_piece."),
392            op_name);
393 }
394
395 /* Return true iff the types T1 and T2 are "the same".  This only does
396    checks that might reasonably be needed to compare DWARF base
397    types.  */
398
399 static int
400 base_types_equal_p (struct type *t1, struct type *t2)
401 {
402   if (TYPE_CODE (t1) != TYPE_CODE (t2))
403     return 0;
404   if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
405     return 0;
406   return TYPE_LENGTH (t1) == TYPE_LENGTH (t2);
407 }
408
409 /* A convenience function to call get_base_type and return the result.
410    DIE is the DIE whose type we need.  SIZE is non-zero if this
411    function should verify that the resulting type has the correct
412    size.  */
413
414 struct type *
415 dwarf_expr_context::get_base_type (cu_offset die, int size)
416 {
417   struct type *result;
418
419   if (this->funcs->get_base_type)
420     {
421       result = this->funcs->get_base_type (this, die);
422       if (result == NULL)
423         error (_("Could not find type for DW_OP_GNU_const_type"));
424       if (size != 0 && TYPE_LENGTH (result) != size)
425         error (_("DW_OP_GNU_const_type has different sizes for type and data"));
426     }
427   else
428     /* Anything will do.  */
429     result = builtin_type (this->gdbarch)->builtin_int;
430
431   return result;
432 }
433
434 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_reg* return the
435    DWARF register number.  Otherwise return -1.  */
436
437 int
438 dwarf_block_to_dwarf_reg (const gdb_byte *buf, const gdb_byte *buf_end)
439 {
440   uint64_t dwarf_reg;
441
442   if (buf_end <= buf)
443     return -1;
444   if (*buf >= DW_OP_reg0 && *buf <= DW_OP_reg31)
445     {
446       if (buf_end - buf != 1)
447         return -1;
448       return *buf - DW_OP_reg0;
449     }
450
451   if (*buf == DW_OP_GNU_regval_type)
452     {
453       buf++;
454       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
455       if (buf == NULL)
456         return -1;
457       buf = gdb_skip_leb128 (buf, buf_end);
458       if (buf == NULL)
459         return -1;
460     }
461   else if (*buf == DW_OP_regx)
462     {
463       buf++;
464       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
465       if (buf == NULL)
466         return -1;
467     }
468   else
469     return -1;
470   if (buf != buf_end || (int) dwarf_reg != dwarf_reg)
471     return -1;
472   return dwarf_reg;
473 }
474
475 /* If <BUF..BUF_END] contains DW_FORM_block* with just DW_OP_breg*(0) and
476    DW_OP_deref* return the DWARF register number.  Otherwise return -1.
477    DEREF_SIZE_RETURN contains -1 for DW_OP_deref; otherwise it contains the
478    size from DW_OP_deref_size.  */
479
480 int
481 dwarf_block_to_dwarf_reg_deref (const gdb_byte *buf, const gdb_byte *buf_end,
482                                 CORE_ADDR *deref_size_return)
483 {
484   uint64_t dwarf_reg;
485   int64_t offset;
486
487   if (buf_end <= buf)
488     return -1;
489
490   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
491     {
492       dwarf_reg = *buf - DW_OP_breg0;
493       buf++;
494       if (buf >= buf_end)
495         return -1;
496     }
497   else if (*buf == DW_OP_bregx)
498     {
499       buf++;
500       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
501       if (buf == NULL)
502         return -1;
503       if ((int) dwarf_reg != dwarf_reg)
504        return -1;
505     }
506   else
507     return -1;
508
509   buf = gdb_read_sleb128 (buf, buf_end, &offset);
510   if (buf == NULL)
511     return -1;
512   if (offset != 0)
513     return -1;
514
515   if (*buf == DW_OP_deref)
516     {
517       buf++;
518       *deref_size_return = -1;
519     }
520   else if (*buf == DW_OP_deref_size)
521     {
522       buf++;
523       if (buf >= buf_end)
524        return -1;
525       *deref_size_return = *buf++;
526     }
527   else
528     return -1;
529
530   if (buf != buf_end)
531     return -1;
532
533   return dwarf_reg;
534 }
535
536 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_fbreg(X) fill
537    in FB_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.  */
538
539 int
540 dwarf_block_to_fb_offset (const gdb_byte *buf, const gdb_byte *buf_end,
541                           CORE_ADDR *fb_offset_return)
542 {
543   int64_t fb_offset;
544
545   if (buf_end <= buf)
546     return 0;
547
548   if (*buf != DW_OP_fbreg)
549     return 0;
550   buf++;
551
552   buf = gdb_read_sleb128 (buf, buf_end, &fb_offset);
553   if (buf == NULL)
554     return 0;
555   *fb_offset_return = fb_offset;
556   if (buf != buf_end || fb_offset != (LONGEST) *fb_offset_return)
557     return 0;
558
559   return 1;
560 }
561
562 /* If <BUF..BUF_END] contains DW_FORM_block* with single DW_OP_bregSP(X) fill
563    in SP_OFFSET_RETURN with the X offset and return 1.  Otherwise return 0.
564    The matched SP register number depends on GDBARCH.  */
565
566 int
567 dwarf_block_to_sp_offset (struct gdbarch *gdbarch, const gdb_byte *buf,
568                           const gdb_byte *buf_end, CORE_ADDR *sp_offset_return)
569 {
570   uint64_t dwarf_reg;
571   int64_t sp_offset;
572
573   if (buf_end <= buf)
574     return 0;
575   if (*buf >= DW_OP_breg0 && *buf <= DW_OP_breg31)
576     {
577       dwarf_reg = *buf - DW_OP_breg0;
578       buf++;
579     }
580   else
581     {
582       if (*buf != DW_OP_bregx)
583        return 0;
584       buf++;
585       buf = gdb_read_uleb128 (buf, buf_end, &dwarf_reg);
586       if (buf == NULL)
587         return 0;
588     }
589
590   if (dwarf_reg_to_regnum (gdbarch, dwarf_reg)
591       != gdbarch_sp_regnum (gdbarch))
592     return 0;
593
594   buf = gdb_read_sleb128 (buf, buf_end, &sp_offset);
595   if (buf == NULL)
596     return 0;
597   *sp_offset_return = sp_offset;
598   if (buf != buf_end || sp_offset != (LONGEST) *sp_offset_return)
599     return 0;
600
601   return 1;
602 }
603
604 /* The engine for the expression evaluator.  Using the context in this
605    object, evaluate the expression between OP_PTR and OP_END.  */
606
607 void
608 dwarf_expr_context::execute_stack_op (const gdb_byte *op_ptr,
609                                       const gdb_byte *op_end)
610 {
611   enum bfd_endian byte_order = gdbarch_byte_order (this->gdbarch);
612   /* Old-style "untyped" DWARF values need special treatment in a
613      couple of places, specifically DW_OP_mod and DW_OP_shr.  We need
614      a special type for these values so we can distinguish them from
615      values that have an explicit type, because explicitly-typed
616      values do not need special treatment.  This special type must be
617      different (in the `==' sense) from any base type coming from the
618      CU.  */
619   struct type *address_type = this->address_type ();
620
621   this->location = DWARF_VALUE_MEMORY;
622   this->initialized = 1;  /* Default is initialized.  */
623
624   if (this->recursion_depth > this->max_recursion_depth)
625     error (_("DWARF-2 expression error: Loop detected (%d)."),
626            this->recursion_depth);
627   this->recursion_depth++;
628
629   while (op_ptr < op_end)
630     {
631       enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr++;
632       ULONGEST result;
633       /* Assume the value is not in stack memory.
634          Code that knows otherwise sets this to 1.
635          Some arithmetic on stack addresses can probably be assumed to still
636          be a stack address, but we skip this complication for now.
637          This is just an optimization, so it's always ok to punt
638          and leave this as 0.  */
639       int in_stack_memory = 0;
640       uint64_t uoffset, reg;
641       int64_t offset;
642       struct value *result_val = NULL;
643
644       /* The DWARF expression might have a bug causing an infinite
645          loop.  In that case, quitting is the only way out.  */
646       QUIT;
647
648       switch (op)
649         {
650         case DW_OP_lit0:
651         case DW_OP_lit1:
652         case DW_OP_lit2:
653         case DW_OP_lit3:
654         case DW_OP_lit4:
655         case DW_OP_lit5:
656         case DW_OP_lit6:
657         case DW_OP_lit7:
658         case DW_OP_lit8:
659         case DW_OP_lit9:
660         case DW_OP_lit10:
661         case DW_OP_lit11:
662         case DW_OP_lit12:
663         case DW_OP_lit13:
664         case DW_OP_lit14:
665         case DW_OP_lit15:
666         case DW_OP_lit16:
667         case DW_OP_lit17:
668         case DW_OP_lit18:
669         case DW_OP_lit19:
670         case DW_OP_lit20:
671         case DW_OP_lit21:
672         case DW_OP_lit22:
673         case DW_OP_lit23:
674         case DW_OP_lit24:
675         case DW_OP_lit25:
676         case DW_OP_lit26:
677         case DW_OP_lit27:
678         case DW_OP_lit28:
679         case DW_OP_lit29:
680         case DW_OP_lit30:
681         case DW_OP_lit31:
682           result = op - DW_OP_lit0;
683           result_val = value_from_ulongest (address_type, result);
684           break;
685
686         case DW_OP_addr:
687           result = extract_unsigned_integer (op_ptr,
688                                              this->addr_size, byte_order);
689           op_ptr += this->addr_size;
690           /* Some versions of GCC emit DW_OP_addr before
691              DW_OP_GNU_push_tls_address.  In this case the value is an
692              index, not an address.  We don't support things like
693              branching between the address and the TLS op.  */
694           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
695             result += this->offset;
696           result_val = value_from_ulongest (address_type, result);
697           break;
698
699         case DW_OP_GNU_addr_index:
700           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
701           result = (this->funcs->get_addr_index) (this->baton, uoffset);
702           result += this->offset;
703           result_val = value_from_ulongest (address_type, result);
704           break;
705         case DW_OP_GNU_const_index:
706           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
707           result = (this->funcs->get_addr_index) (this->baton, uoffset);
708           result_val = value_from_ulongest (address_type, result);
709           break;
710
711         case DW_OP_const1u:
712           result = extract_unsigned_integer (op_ptr, 1, byte_order);
713           result_val = value_from_ulongest (address_type, result);
714           op_ptr += 1;
715           break;
716         case DW_OP_const1s:
717           result = extract_signed_integer (op_ptr, 1, byte_order);
718           result_val = value_from_ulongest (address_type, result);
719           op_ptr += 1;
720           break;
721         case DW_OP_const2u:
722           result = extract_unsigned_integer (op_ptr, 2, byte_order);
723           result_val = value_from_ulongest (address_type, result);
724           op_ptr += 2;
725           break;
726         case DW_OP_const2s:
727           result = extract_signed_integer (op_ptr, 2, byte_order);
728           result_val = value_from_ulongest (address_type, result);
729           op_ptr += 2;
730           break;
731         case DW_OP_const4u:
732           result = extract_unsigned_integer (op_ptr, 4, byte_order);
733           result_val = value_from_ulongest (address_type, result);
734           op_ptr += 4;
735           break;
736         case DW_OP_const4s:
737           result = extract_signed_integer (op_ptr, 4, byte_order);
738           result_val = value_from_ulongest (address_type, result);
739           op_ptr += 4;
740           break;
741         case DW_OP_const8u:
742           result = extract_unsigned_integer (op_ptr, 8, byte_order);
743           result_val = value_from_ulongest (address_type, result);
744           op_ptr += 8;
745           break;
746         case DW_OP_const8s:
747           result = extract_signed_integer (op_ptr, 8, byte_order);
748           result_val = value_from_ulongest (address_type, result);
749           op_ptr += 8;
750           break;
751         case DW_OP_constu:
752           op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
753           result = uoffset;
754           result_val = value_from_ulongest (address_type, result);
755           break;
756         case DW_OP_consts:
757           op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
758           result = offset;
759           result_val = value_from_ulongest (address_type, result);
760           break;
761
762         /* The DW_OP_reg operations are required to occur alone in
763            location expressions.  */
764         case DW_OP_reg0:
765         case DW_OP_reg1:
766         case DW_OP_reg2:
767         case DW_OP_reg3:
768         case DW_OP_reg4:
769         case DW_OP_reg5:
770         case DW_OP_reg6:
771         case DW_OP_reg7:
772         case DW_OP_reg8:
773         case DW_OP_reg9:
774         case DW_OP_reg10:
775         case DW_OP_reg11:
776         case DW_OP_reg12:
777         case DW_OP_reg13:
778         case DW_OP_reg14:
779         case DW_OP_reg15:
780         case DW_OP_reg16:
781         case DW_OP_reg17:
782         case DW_OP_reg18:
783         case DW_OP_reg19:
784         case DW_OP_reg20:
785         case DW_OP_reg21:
786         case DW_OP_reg22:
787         case DW_OP_reg23:
788         case DW_OP_reg24:
789         case DW_OP_reg25:
790         case DW_OP_reg26:
791         case DW_OP_reg27:
792         case DW_OP_reg28:
793         case DW_OP_reg29:
794         case DW_OP_reg30:
795         case DW_OP_reg31:
796           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_reg");
797
798           result = op - DW_OP_reg0;
799           result_val = value_from_ulongest (address_type, result);
800           this->location = DWARF_VALUE_REGISTER;
801           break;
802
803         case DW_OP_regx:
804           op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
805           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
806
807           result = reg;
808           result_val = value_from_ulongest (address_type, result);
809           this->location = DWARF_VALUE_REGISTER;
810           break;
811
812         case DW_OP_implicit_value:
813           {
814             uint64_t len;
815
816             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
817             if (op_ptr + len > op_end)
818               error (_("DW_OP_implicit_value: too few bytes available."));
819             this->len = len;
820             this->data = op_ptr;
821             this->location = DWARF_VALUE_LITERAL;
822             op_ptr += len;
823             dwarf_expr_require_composition (op_ptr, op_end,
824                                             "DW_OP_implicit_value");
825           }
826           goto no_push;
827
828         case DW_OP_stack_value:
829           this->location = DWARF_VALUE_STACK;
830           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
831           goto no_push;
832
833         case DW_OP_GNU_implicit_pointer:
834           {
835             int64_t len;
836
837             if (this->ref_addr_size == -1)
838               error (_("DWARF-2 expression error: DW_OP_GNU_implicit_pointer "
839                        "is not allowed in frame context"));
840
841             /* The referred-to DIE of sect_offset kind.  */
842             this->len = extract_unsigned_integer (op_ptr, this->ref_addr_size,
843                                                  byte_order);
844             op_ptr += this->ref_addr_size;
845
846             /* The byte offset into the data.  */
847             op_ptr = safe_read_sleb128 (op_ptr, op_end, &len);
848             result = (ULONGEST) len;
849             result_val = value_from_ulongest (address_type, result);
850
851             this->location = DWARF_VALUE_IMPLICIT_POINTER;
852             dwarf_expr_require_composition (op_ptr, op_end,
853                                             "DW_OP_GNU_implicit_pointer");
854           }
855           break;
856
857         case DW_OP_breg0:
858         case DW_OP_breg1:
859         case DW_OP_breg2:
860         case DW_OP_breg3:
861         case DW_OP_breg4:
862         case DW_OP_breg5:
863         case DW_OP_breg6:
864         case DW_OP_breg7:
865         case DW_OP_breg8:
866         case DW_OP_breg9:
867         case DW_OP_breg10:
868         case DW_OP_breg11:
869         case DW_OP_breg12:
870         case DW_OP_breg13:
871         case DW_OP_breg14:
872         case DW_OP_breg15:
873         case DW_OP_breg16:
874         case DW_OP_breg17:
875         case DW_OP_breg18:
876         case DW_OP_breg19:
877         case DW_OP_breg20:
878         case DW_OP_breg21:
879         case DW_OP_breg22:
880         case DW_OP_breg23:
881         case DW_OP_breg24:
882         case DW_OP_breg25:
883         case DW_OP_breg26:
884         case DW_OP_breg27:
885         case DW_OP_breg28:
886         case DW_OP_breg29:
887         case DW_OP_breg30:
888         case DW_OP_breg31:
889           {
890             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
891             result = (this->funcs->read_addr_from_reg) (this->baton,
892                                                        op - DW_OP_breg0);
893             result += offset;
894             result_val = value_from_ulongest (address_type, result);
895           }
896           break;
897         case DW_OP_bregx:
898           {
899             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
900             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
901             result = (this->funcs->read_addr_from_reg) (this->baton, reg);
902             result += offset;
903             result_val = value_from_ulongest (address_type, result);
904           }
905           break;
906         case DW_OP_fbreg:
907           {
908             const gdb_byte *datastart;
909             size_t datalen;
910             unsigned int before_stack_len;
911
912             op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
913             /* Rather than create a whole new context, we simply
914                record the stack length before execution, then reset it
915                afterwards, effectively erasing whatever the recursive
916                call put there.  */
917             before_stack_len = this->stack_len;
918             /* FIXME: cagney/2003-03-26: This code should be using
919                get_frame_base_address(), and then implement a dwarf2
920                specific this_base method.  */
921             (this->funcs->get_frame_base) (this->baton, &datastart, &datalen);
922             eval (datastart, datalen);
923             if (this->location == DWARF_VALUE_MEMORY)
924               result = fetch_address (0);
925             else if (this->location == DWARF_VALUE_REGISTER)
926               result = (this->funcs->read_addr_from_reg)
927                           (this->baton,
928                            value_as_long (fetch (0)));
929             else
930               error (_("Not implemented: computing frame "
931                        "base using explicit value operator"));
932             result = result + offset;
933             result_val = value_from_ulongest (address_type, result);
934             in_stack_memory = 1;
935             this->stack_len = before_stack_len;
936             this->location = DWARF_VALUE_MEMORY;
937           }
938           break;
939
940         case DW_OP_dup:
941           result_val = fetch (0);
942           in_stack_memory = fetch_in_stack_memory (0);
943           break;
944
945         case DW_OP_drop:
946           pop ();
947           goto no_push;
948
949         case DW_OP_pick:
950           offset = *op_ptr++;
951           result_val = fetch (offset);
952           in_stack_memory = fetch_in_stack_memory (offset);
953           break;
954           
955         case DW_OP_swap:
956           {
957             struct dwarf_stack_value t1, t2;
958
959             if (this->stack_len < 2)
960                error (_("Not enough elements for "
961                         "DW_OP_swap.  Need 2, have %d."),
962                       this->stack_len);
963             t1 = this->stack[this->stack_len - 1];
964             t2 = this->stack[this->stack_len - 2];
965             this->stack[this->stack_len - 1] = t2;
966             this->stack[this->stack_len - 2] = t1;
967             goto no_push;
968           }
969
970         case DW_OP_over:
971           result_val = fetch (1);
972           in_stack_memory = fetch_in_stack_memory (1);
973           break;
974
975         case DW_OP_rot:
976           {
977             struct dwarf_stack_value t1, t2, t3;
978
979             if (this->stack_len < 3)
980                error (_("Not enough elements for "
981                         "DW_OP_rot.  Need 3, have %d."),
982                       this->stack_len);
983             t1 = this->stack[this->stack_len - 1];
984             t2 = this->stack[this->stack_len - 2];
985             t3 = this->stack[this->stack_len - 3];
986             this->stack[this->stack_len - 1] = t2;
987             this->stack[this->stack_len - 2] = t3;
988             this->stack[this->stack_len - 3] = t1;
989             goto no_push;
990           }
991
992         case DW_OP_deref:
993         case DW_OP_deref_size:
994         case DW_OP_GNU_deref_type:
995           {
996             int addr_size = (op == DW_OP_deref ? this->addr_size : *op_ptr++);
997             gdb_byte *buf = (gdb_byte *) alloca (addr_size);
998             CORE_ADDR addr = fetch_address (0);
999             struct type *type;
1000
1001             pop ();
1002
1003             if (op == DW_OP_GNU_deref_type)
1004               {
1005                 cu_offset type_die;
1006
1007                 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1008                 type_die.cu_off = uoffset;
1009                 type = get_base_type (type_die, 0);
1010               }
1011             else
1012               type = address_type;
1013
1014             (this->funcs->read_mem) (this->baton, buf, addr, addr_size);
1015
1016             /* If the size of the object read from memory is different
1017                from the type length, we need to zero-extend it.  */
1018             if (TYPE_LENGTH (type) != addr_size)
1019               {
1020                 ULONGEST result =
1021                   extract_unsigned_integer (buf, addr_size, byte_order);
1022
1023                 buf = (gdb_byte *) alloca (TYPE_LENGTH (type));
1024                 store_unsigned_integer (buf, TYPE_LENGTH (type),
1025                                         byte_order, result);
1026               }
1027
1028             result_val = value_from_contents_and_address (type, buf, addr);
1029             break;
1030           }
1031
1032         case DW_OP_abs:
1033         case DW_OP_neg:
1034         case DW_OP_not:
1035         case DW_OP_plus_uconst:
1036           {
1037             /* Unary operations.  */
1038             result_val = fetch (0);
1039             pop ();
1040
1041             switch (op)
1042               {
1043               case DW_OP_abs:
1044                 if (value_less (result_val,
1045                                 value_zero (value_type (result_val), not_lval)))
1046                   result_val = value_neg (result_val);
1047                 break;
1048               case DW_OP_neg:
1049                 result_val = value_neg (result_val);
1050                 break;
1051               case DW_OP_not:
1052                 dwarf_require_integral (value_type (result_val));
1053                 result_val = value_complement (result_val);
1054                 break;
1055               case DW_OP_plus_uconst:
1056                 dwarf_require_integral (value_type (result_val));
1057                 result = value_as_long (result_val);
1058                 op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1059                 result += reg;
1060                 result_val = value_from_ulongest (address_type, result);
1061                 break;
1062               }
1063           }
1064           break;
1065
1066         case DW_OP_and:
1067         case DW_OP_div:
1068         case DW_OP_minus:
1069         case DW_OP_mod:
1070         case DW_OP_mul:
1071         case DW_OP_or:
1072         case DW_OP_plus:
1073         case DW_OP_shl:
1074         case DW_OP_shr:
1075         case DW_OP_shra:
1076         case DW_OP_xor:
1077         case DW_OP_le:
1078         case DW_OP_ge:
1079         case DW_OP_eq:
1080         case DW_OP_lt:
1081         case DW_OP_gt:
1082         case DW_OP_ne:
1083           {
1084             /* Binary operations.  */
1085             struct value *first, *second;
1086
1087             second = fetch (0);
1088             pop ();
1089
1090             first = fetch (0);
1091             pop ();
1092
1093             if (! base_types_equal_p (value_type (first), value_type (second)))
1094               error (_("Incompatible types on DWARF stack"));
1095
1096             switch (op)
1097               {
1098               case DW_OP_and:
1099                 dwarf_require_integral (value_type (first));
1100                 dwarf_require_integral (value_type (second));
1101                 result_val = value_binop (first, second, BINOP_BITWISE_AND);
1102                 break;
1103               case DW_OP_div:
1104                 result_val = value_binop (first, second, BINOP_DIV);
1105                 break;
1106               case DW_OP_minus:
1107                 result_val = value_binop (first, second, BINOP_SUB);
1108                 break;
1109               case DW_OP_mod:
1110                 {
1111                   int cast_back = 0;
1112                   struct type *orig_type = value_type (first);
1113
1114                   /* We have to special-case "old-style" untyped values
1115                      -- these must have mod computed using unsigned
1116                      math.  */
1117                   if (orig_type == address_type)
1118                     {
1119                       struct type *utype
1120                         = get_unsigned_type (this->gdbarch, orig_type);
1121
1122                       cast_back = 1;
1123                       first = value_cast (utype, first);
1124                       second = value_cast (utype, second);
1125                     }
1126                   /* Note that value_binop doesn't handle float or
1127                      decimal float here.  This seems unimportant.  */
1128                   result_val = value_binop (first, second, BINOP_MOD);
1129                   if (cast_back)
1130                     result_val = value_cast (orig_type, result_val);
1131                 }
1132                 break;
1133               case DW_OP_mul:
1134                 result_val = value_binop (first, second, BINOP_MUL);
1135                 break;
1136               case DW_OP_or:
1137                 dwarf_require_integral (value_type (first));
1138                 dwarf_require_integral (value_type (second));
1139                 result_val = value_binop (first, second, BINOP_BITWISE_IOR);
1140                 break;
1141               case DW_OP_plus:
1142                 result_val = value_binop (first, second, BINOP_ADD);
1143                 break;
1144               case DW_OP_shl:
1145                 dwarf_require_integral (value_type (first));
1146                 dwarf_require_integral (value_type (second));
1147                 result_val = value_binop (first, second, BINOP_LSH);
1148                 break;
1149               case DW_OP_shr:
1150                 dwarf_require_integral (value_type (first));
1151                 dwarf_require_integral (value_type (second));
1152                 if (!TYPE_UNSIGNED (value_type (first)))
1153                   {
1154                     struct type *utype
1155                       = get_unsigned_type (this->gdbarch, value_type (first));
1156
1157                     first = value_cast (utype, first);
1158                   }
1159
1160                 result_val = value_binop (first, second, BINOP_RSH);
1161                 /* Make sure we wind up with the same type we started
1162                    with.  */
1163                 if (value_type (result_val) != value_type (second))
1164                   result_val = value_cast (value_type (second), result_val);
1165                 break;
1166               case DW_OP_shra:
1167                 dwarf_require_integral (value_type (first));
1168                 dwarf_require_integral (value_type (second));
1169                 if (TYPE_UNSIGNED (value_type (first)))
1170                   {
1171                     struct type *stype
1172                       = get_signed_type (this->gdbarch, value_type (first));
1173
1174                     first = value_cast (stype, first);
1175                   }
1176
1177                 result_val = value_binop (first, second, BINOP_RSH);
1178                 /* Make sure we wind up with the same type we started
1179                    with.  */
1180                 if (value_type (result_val) != value_type (second))
1181                   result_val = value_cast (value_type (second), result_val);
1182                 break;
1183               case DW_OP_xor:
1184                 dwarf_require_integral (value_type (first));
1185                 dwarf_require_integral (value_type (second));
1186                 result_val = value_binop (first, second, BINOP_BITWISE_XOR);
1187                 break;
1188               case DW_OP_le:
1189                 /* A <= B is !(B < A).  */
1190                 result = ! value_less (second, first);
1191                 result_val = value_from_ulongest (address_type, result);
1192                 break;
1193               case DW_OP_ge:
1194                 /* A >= B is !(A < B).  */
1195                 result = ! value_less (first, second);
1196                 result_val = value_from_ulongest (address_type, result);
1197                 break;
1198               case DW_OP_eq:
1199                 result = value_equal (first, second);
1200                 result_val = value_from_ulongest (address_type, result);
1201                 break;
1202               case DW_OP_lt:
1203                 result = value_less (first, second);
1204                 result_val = value_from_ulongest (address_type, result);
1205                 break;
1206               case DW_OP_gt:
1207                 /* A > B is B < A.  */
1208                 result = value_less (second, first);
1209                 result_val = value_from_ulongest (address_type, result);
1210                 break;
1211               case DW_OP_ne:
1212                 result = ! value_equal (first, second);
1213                 result_val = value_from_ulongest (address_type, result);
1214                 break;
1215               default:
1216                 internal_error (__FILE__, __LINE__,
1217                                 _("Can't be reached."));
1218               }
1219           }
1220           break;
1221
1222         case DW_OP_call_frame_cfa:
1223           result = (this->funcs->get_frame_cfa) (this->baton);
1224           result_val = value_from_ulongest (address_type, result);
1225           in_stack_memory = 1;
1226           break;
1227
1228         case DW_OP_GNU_push_tls_address:
1229         case DW_OP_form_tls_address:
1230           /* Variable is at a constant offset in the thread-local
1231           storage block into the objfile for the current thread and
1232           the dynamic linker module containing this expression.  Here
1233           we return returns the offset from that base.  The top of the
1234           stack has the offset from the beginning of the thread
1235           control block at which the variable is located.  Nothing
1236           should follow this operator, so the top of stack would be
1237           returned.  */
1238           result = value_as_long (fetch (0));
1239           pop ();
1240           result = (this->funcs->get_tls_address) (this->baton, result);
1241           result_val = value_from_ulongest (address_type, result);
1242           break;
1243
1244         case DW_OP_skip:
1245           offset = extract_signed_integer (op_ptr, 2, byte_order);
1246           op_ptr += 2;
1247           op_ptr += offset;
1248           goto no_push;
1249
1250         case DW_OP_bra:
1251           {
1252             struct value *val;
1253
1254             offset = extract_signed_integer (op_ptr, 2, byte_order);
1255             op_ptr += 2;
1256             val = fetch (0);
1257             dwarf_require_integral (value_type (val));
1258             if (value_as_long (val) != 0)
1259               op_ptr += offset;
1260             pop ();
1261           }
1262           goto no_push;
1263
1264         case DW_OP_nop:
1265           goto no_push;
1266
1267         case DW_OP_piece:
1268           {
1269             uint64_t size;
1270
1271             /* Record the piece.  */
1272             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1273             add_piece (8 * size, 0);
1274
1275             /* Pop off the address/regnum, and reset the location
1276                type.  */
1277             if (this->location != DWARF_VALUE_LITERAL
1278                 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1279               pop ();
1280             this->location = DWARF_VALUE_MEMORY;
1281           }
1282           goto no_push;
1283
1284         case DW_OP_bit_piece:
1285           {
1286             uint64_t size, offset;
1287
1288             /* Record the piece.  */
1289             op_ptr = safe_read_uleb128 (op_ptr, op_end, &size);
1290             op_ptr = safe_read_uleb128 (op_ptr, op_end, &offset);
1291             add_piece (size, offset);
1292
1293             /* Pop off the address/regnum, and reset the location
1294                type.  */
1295             if (this->location != DWARF_VALUE_LITERAL
1296                 && this->location != DWARF_VALUE_OPTIMIZED_OUT)
1297               pop ();
1298             this->location = DWARF_VALUE_MEMORY;
1299           }
1300           goto no_push;
1301
1302         case DW_OP_GNU_uninit:
1303           if (op_ptr != op_end)
1304             error (_("DWARF-2 expression error: DW_OP_GNU_uninit must always "
1305                    "be the very last op."));
1306
1307           this->initialized = 0;
1308           goto no_push;
1309
1310         case DW_OP_call2:
1311           {
1312             cu_offset offset;
1313
1314             offset.cu_off = extract_unsigned_integer (op_ptr, 2, byte_order);
1315             op_ptr += 2;
1316             this->funcs->dwarf_call (this, offset);
1317           }
1318           goto no_push;
1319
1320         case DW_OP_call4:
1321           {
1322             cu_offset offset;
1323
1324             offset.cu_off = extract_unsigned_integer (op_ptr, 4, byte_order);
1325             op_ptr += 4;
1326             this->funcs->dwarf_call (this, offset);
1327           }
1328           goto no_push;
1329         
1330         case DW_OP_GNU_entry_value:
1331           {
1332             uint64_t len;
1333             CORE_ADDR deref_size;
1334             union call_site_parameter_u kind_u;
1335
1336             op_ptr = safe_read_uleb128 (op_ptr, op_end, &len);
1337             if (op_ptr + len > op_end)
1338               error (_("DW_OP_GNU_entry_value: too few bytes available."));
1339
1340             kind_u.dwarf_reg = dwarf_block_to_dwarf_reg (op_ptr, op_ptr + len);
1341             if (kind_u.dwarf_reg != -1)
1342               {
1343                 op_ptr += len;
1344                 this->funcs->push_dwarf_reg_entry_value (this,
1345                                                   CALL_SITE_PARAMETER_DWARF_REG,
1346                                                         kind_u,
1347                                                         -1 /* deref_size */);
1348                 goto no_push;
1349               }
1350
1351             kind_u.dwarf_reg = dwarf_block_to_dwarf_reg_deref (op_ptr,
1352                                                                op_ptr + len,
1353                                                                &deref_size);
1354             if (kind_u.dwarf_reg != -1)
1355               {
1356                 if (deref_size == -1)
1357                   deref_size = this->addr_size;
1358                 op_ptr += len;
1359                 this->funcs->push_dwarf_reg_entry_value (this,
1360                                                   CALL_SITE_PARAMETER_DWARF_REG,
1361                                                         kind_u, deref_size);
1362                 goto no_push;
1363               }
1364
1365             error (_("DWARF-2 expression error: DW_OP_GNU_entry_value is "
1366                      "supported only for single DW_OP_reg* "
1367                      "or for DW_OP_breg*(0)+DW_OP_deref*"));
1368           }
1369
1370         case DW_OP_GNU_parameter_ref:
1371           {
1372             union call_site_parameter_u kind_u;
1373
1374             kind_u.param_offset.cu_off = extract_unsigned_integer (op_ptr, 4,
1375                                                                    byte_order);
1376             op_ptr += 4;
1377             this->funcs->push_dwarf_reg_entry_value (this,
1378                                                CALL_SITE_PARAMETER_PARAM_OFFSET,
1379                                                     kind_u,
1380                                                     -1 /* deref_size */);
1381           }
1382           goto no_push;
1383
1384         case DW_OP_GNU_const_type:
1385           {
1386             cu_offset type_die;
1387             int n;
1388             const gdb_byte *data;
1389             struct type *type;
1390
1391             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1392             type_die.cu_off = uoffset;
1393             n = *op_ptr++;
1394             data = op_ptr;
1395             op_ptr += n;
1396
1397             type = get_base_type (type_die, n);
1398             result_val = value_from_contents (type, data);
1399           }
1400           break;
1401
1402         case DW_OP_GNU_regval_type:
1403           {
1404             cu_offset type_die;
1405             struct type *type;
1406
1407             op_ptr = safe_read_uleb128 (op_ptr, op_end, &reg);
1408             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1409             type_die.cu_off = uoffset;
1410
1411             type = get_base_type (type_die, 0);
1412             result_val = this->funcs->get_reg_value (this->baton, type, reg);
1413           }
1414           break;
1415
1416         case DW_OP_GNU_convert:
1417         case DW_OP_GNU_reinterpret:
1418           {
1419             cu_offset type_die;
1420             struct type *type;
1421
1422             op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
1423             type_die.cu_off = uoffset;
1424
1425             if (type_die.cu_off == 0)
1426               type = address_type;
1427             else
1428               type = get_base_type (type_die, 0);
1429
1430             result_val = fetch (0);
1431             pop ();
1432
1433             if (op == DW_OP_GNU_convert)
1434               result_val = value_cast (type, result_val);
1435             else if (type == value_type (result_val))
1436               {
1437                 /* Nothing.  */
1438               }
1439             else if (TYPE_LENGTH (type)
1440                      != TYPE_LENGTH (value_type (result_val)))
1441               error (_("DW_OP_GNU_reinterpret has wrong size"));
1442             else
1443               result_val
1444                 = value_from_contents (type,
1445                                        value_contents_all (result_val));
1446           }
1447           break;
1448
1449         case DW_OP_push_object_address:
1450           /* Return the address of the object we are currently observing.  */
1451           result = (this->funcs->get_object_address) (this->baton);
1452           result_val = value_from_ulongest (address_type, result);
1453           break;
1454
1455         default:
1456           error (_("Unhandled dwarf expression opcode 0x%x"), op);
1457         }
1458
1459       /* Most things push a result value.  */
1460       gdb_assert (result_val != NULL);
1461       push (result_val, in_stack_memory);
1462     no_push:
1463       ;
1464     }
1465
1466   /* To simplify our main caller, if the result is an implicit
1467      pointer, then make a pieced value.  This is ok because we can't
1468      have implicit pointers in contexts where pieces are invalid.  */
1469   if (this->location == DWARF_VALUE_IMPLICIT_POINTER)
1470     add_piece (8 * this->addr_size, 0);
1471
1472 abort_expression:
1473   this->recursion_depth--;
1474   gdb_assert (this->recursion_depth >= 0);
1475 }
1476
1477 /* Stub dwarf_expr_context_funcs.get_frame_base implementation.  */
1478
1479 void
1480 ctx_no_get_frame_base (void *baton, const gdb_byte **start, size_t *length)
1481 {
1482   error (_("%s is invalid in this context"), "DW_OP_fbreg");
1483 }
1484
1485 /* Stub dwarf_expr_context_funcs.get_frame_cfa implementation.  */
1486
1487 CORE_ADDR
1488 ctx_no_get_frame_cfa (void *baton)
1489 {
1490   error (_("%s is invalid in this context"), "DW_OP_call_frame_cfa");
1491 }
1492
1493 /* Stub dwarf_expr_context_funcs.get_frame_pc implementation.  */
1494
1495 CORE_ADDR
1496 ctx_no_get_frame_pc (void *baton)
1497 {
1498   error (_("%s is invalid in this context"), "DW_OP_GNU_implicit_pointer");
1499 }
1500
1501 /* Stub dwarf_expr_context_funcs.get_tls_address implementation.  */
1502
1503 CORE_ADDR
1504 ctx_no_get_tls_address (void *baton, CORE_ADDR offset)
1505 {
1506   error (_("%s is invalid in this context"), "DW_OP_form_tls_address");
1507 }
1508
1509 /* Stub dwarf_expr_context_funcs.dwarf_call implementation.  */
1510
1511 void
1512 ctx_no_dwarf_call (struct dwarf_expr_context *ctx, cu_offset die_offset)
1513 {
1514   error (_("%s is invalid in this context"), "DW_OP_call*");
1515 }
1516
1517 /* Stub dwarf_expr_context_funcs.get_base_type implementation.  */
1518
1519 struct type *
1520 ctx_no_get_base_type (struct dwarf_expr_context *ctx, cu_offset die)
1521 {
1522   error (_("Support for typed DWARF is not supported in this context"));
1523 }
1524
1525 /* Stub dwarf_expr_context_funcs.push_dwarf_block_entry_value
1526    implementation.  */
1527
1528 void
1529 ctx_no_push_dwarf_reg_entry_value (struct dwarf_expr_context *ctx,
1530                                    enum call_site_parameter_kind kind,
1531                                    union call_site_parameter_u kind_u,
1532                                    int deref_size)
1533 {
1534   internal_error (__FILE__, __LINE__,
1535                   _("Support for DW_OP_GNU_entry_value is unimplemented"));
1536 }
1537
1538 /* Stub dwarf_expr_context_funcs.get_addr_index implementation.  */
1539
1540 CORE_ADDR
1541 ctx_no_get_addr_index (void *baton, unsigned int index)
1542 {
1543   error (_("%s is invalid in this context"), "DW_OP_GNU_addr_index");
1544 }
1545
1546 /* Provide a prototype to silence -Wmissing-prototypes.  */
1547 extern initialize_file_ftype _initialize_dwarf2expr;
1548
1549 void
1550 _initialize_dwarf2expr (void)
1551 {
1552   dwarf_arch_cookie
1553     = gdbarch_data_register_post_init (dwarf_gdbarch_types_init);
1554 }