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