* vec.h (VEC_cleanup): New macro.
[external/binutils.git] / gdb / dwarf2loc.c
1 /* DWARF 2 location expression support for GDB.
2
3    Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    Contributed by Daniel Jacobowitz, MontaVista Software, Inc.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "value.h"
26 #include "frame.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "inferior.h"
30 #include "ax.h"
31 #include "ax-gdb.h"
32 #include "regcache.h"
33 #include "objfiles.h"
34 #include "exceptions.h"
35 #include "block.h"
36
37 #include "dwarf2.h"
38 #include "dwarf2expr.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame.h"
41
42 #include "gdb_string.h"
43 #include "gdb_assert.h"
44
45 extern int dwarf2_always_disassemble;
46
47 static void
48 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
49                          const gdb_byte **start, size_t *length);
50
51 /* A helper function for dealing with location lists.  Given a
52    symbol baton (BATON) and a pc value (PC), find the appropriate
53    location expression, set *LOCEXPR_LENGTH, and return a pointer
54    to the beginning of the expression.  Returns NULL on failure.
55
56    For now, only return the first matching location expression; there
57    can be more than one in the list.  */
58
59 static const gdb_byte *
60 find_location_expression (struct dwarf2_loclist_baton *baton,
61                           size_t *locexpr_length, CORE_ADDR pc)
62 {
63   CORE_ADDR low, high;
64   const gdb_byte *loc_ptr, *buf_end;
65   int length;
66   struct objfile *objfile = dwarf2_per_cu_objfile (baton->per_cu);
67   struct gdbarch *gdbarch = get_objfile_arch (objfile);
68   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
69   unsigned int addr_size = dwarf2_per_cu_addr_size (baton->per_cu);
70   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
71   /* Adjust base_address for relocatable objects.  */
72   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
73                                     SECT_OFF_TEXT (objfile));
74   CORE_ADDR base_address = baton->base_address + base_offset;
75
76   loc_ptr = baton->data;
77   buf_end = baton->data + baton->size;
78
79   while (1)
80     {
81       if (buf_end - loc_ptr < 2 * addr_size)
82         error (_("find_location_expression: Corrupted DWARF expression."));
83
84       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
85       loc_ptr += addr_size;
86
87       /* A base-address-selection entry.  */
88       if (low == base_mask)
89         {
90           base_address = dwarf2_read_address (gdbarch,
91                                               loc_ptr, buf_end, addr_size);
92           loc_ptr += addr_size;
93           continue;
94         }
95
96       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
97       loc_ptr += addr_size;
98
99       /* An end-of-list entry.  */
100       if (low == 0 && high == 0)
101         return NULL;
102
103       /* Otherwise, a location expression entry.  */
104       low += base_address;
105       high += base_address;
106
107       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
108       loc_ptr += 2;
109
110       if (pc >= low && pc < high)
111         {
112           *locexpr_length = length;
113           return loc_ptr;
114         }
115
116       loc_ptr += length;
117     }
118 }
119
120 /* This is the baton used when performing dwarf2 expression
121    evaluation.  */
122 struct dwarf_expr_baton
123 {
124   struct frame_info *frame;
125   struct dwarf2_per_cu_data *per_cu;
126 };
127
128 /* Helper functions for dwarf2_evaluate_loc_desc.  */
129
130 /* Using the frame specified in BATON, return the value of register
131    REGNUM, treated as a pointer.  */
132 static CORE_ADDR
133 dwarf_expr_read_reg (void *baton, int dwarf_regnum)
134 {
135   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
136   struct gdbarch *gdbarch = get_frame_arch (debaton->frame);
137   CORE_ADDR result;
138   int regnum;
139
140   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
141   result = address_from_register (builtin_type (gdbarch)->builtin_data_ptr,
142                                   regnum, debaton->frame);
143   return result;
144 }
145
146 /* Read memory at ADDR (length LEN) into BUF.  */
147
148 static void
149 dwarf_expr_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
150 {
151   read_memory (addr, buf, len);
152 }
153
154 /* Using the frame specified in BATON, find the location expression
155    describing the frame base.  Return a pointer to it in START and
156    its length in LENGTH.  */
157 static void
158 dwarf_expr_frame_base (void *baton, const gdb_byte **start, size_t * length)
159 {
160   /* FIXME: cagney/2003-03-26: This code should be using
161      get_frame_base_address(), and then implement a dwarf2 specific
162      this_base method.  */
163   struct symbol *framefunc;
164   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
165
166   /* Use block_linkage_function, which returns a real (not inlined)
167      function, instead of get_frame_function, which may return an
168      inlined function.  */
169   framefunc = block_linkage_function (get_frame_block (debaton->frame, NULL));
170
171   /* If we found a frame-relative symbol then it was certainly within
172      some function associated with a frame. If we can't find the frame,
173      something has gone wrong.  */
174   gdb_assert (framefunc != NULL);
175
176   dwarf_expr_frame_base_1 (framefunc,
177                            get_frame_address_in_block (debaton->frame),
178                            start, length);
179 }
180
181 static void
182 dwarf_expr_frame_base_1 (struct symbol *framefunc, CORE_ADDR pc,
183                          const gdb_byte **start, size_t *length)
184 {
185   if (SYMBOL_LOCATION_BATON (framefunc) == NULL)
186     *start = NULL;
187   else if (SYMBOL_COMPUTED_OPS (framefunc) == &dwarf2_loclist_funcs)
188     {
189       struct dwarf2_loclist_baton *symbaton;
190
191       symbaton = SYMBOL_LOCATION_BATON (framefunc);
192       *start = find_location_expression (symbaton, length, pc);
193     }
194   else
195     {
196       struct dwarf2_locexpr_baton *symbaton;
197
198       symbaton = SYMBOL_LOCATION_BATON (framefunc);
199       if (symbaton != NULL)
200         {
201           *length = symbaton->size;
202           *start = symbaton->data;
203         }
204       else
205         *start = NULL;
206     }
207
208   if (*start == NULL)
209     error (_("Could not find the frame base for \"%s\"."),
210            SYMBOL_NATURAL_NAME (framefunc));
211 }
212
213 /* Helper function for dwarf2_evaluate_loc_desc.  Computes the CFA for
214    the frame in BATON.  */
215
216 static CORE_ADDR
217 dwarf_expr_frame_cfa (void *baton)
218 {
219   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
220
221   return dwarf2_frame_cfa (debaton->frame);
222 }
223
224 /* Using the objfile specified in BATON, find the address for the
225    current thread's thread-local storage with offset OFFSET.  */
226 static CORE_ADDR
227 dwarf_expr_tls_address (void *baton, CORE_ADDR offset)
228 {
229   struct dwarf_expr_baton *debaton = (struct dwarf_expr_baton *) baton;
230   struct objfile *objfile = dwarf2_per_cu_objfile (debaton->per_cu);
231
232   return target_translate_tls_address (objfile, offset);
233 }
234
235 /* Call DWARF subroutine from DW_AT_location of DIE at DIE_OFFSET in current CU
236    (as is PER_CU).  State of the CTX is not affected by the call and return.  */
237
238 static void
239 per_cu_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset,
240                    struct dwarf2_per_cu_data *per_cu)
241 {
242   struct dwarf2_locexpr_baton block;
243
244   block = dwarf2_fetch_die_location_block (die_offset, per_cu);
245
246   /* DW_OP_call_ref is currently not supported.  */
247   gdb_assert (block.per_cu == per_cu);
248
249   dwarf_expr_eval (ctx, block.data, block.size);
250 }
251
252 /* Helper interface of per_cu_dwarf_call for dwarf2_evaluate_loc_desc.  */
253
254 static void
255 dwarf_expr_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
256 {
257   struct dwarf_expr_baton *debaton = ctx->baton;
258
259   return per_cu_dwarf_call (ctx, die_offset, debaton->per_cu);
260 }
261
262 struct piece_closure
263 {
264   /* Reference count.  */
265   int refc;
266
267   /* The number of pieces used to describe this variable.  */
268   int n_pieces;
269
270   /* The target address size, used only for DWARF_VALUE_STACK.  */
271   int addr_size;
272
273   /* The pieces themselves.  */
274   struct dwarf_expr_piece *pieces;
275 };
276
277 /* Allocate a closure for a value formed from separately-described
278    PIECES.  */
279
280 static struct piece_closure *
281 allocate_piece_closure (int n_pieces, struct dwarf_expr_piece *pieces,
282                         int addr_size)
283 {
284   struct piece_closure *c = XZALLOC (struct piece_closure);
285
286   c->refc = 1;
287   c->n_pieces = n_pieces;
288   c->addr_size = addr_size;
289   c->pieces = XCALLOC (n_pieces, struct dwarf_expr_piece);
290
291   memcpy (c->pieces, pieces, n_pieces * sizeof (struct dwarf_expr_piece));
292
293   return c;
294 }
295
296 /* The lowest-level function to extract bits from a byte buffer.
297    SOURCE is the buffer.  It is updated if we read to the end of a
298    byte.
299    SOURCE_OFFSET_BITS is the offset of the first bit to read.  It is
300    updated to reflect the number of bits actually read.
301    NBITS is the number of bits we want to read.  It is updated to
302    reflect the number of bits actually read.  This function may read
303    fewer bits.
304    BITS_BIG_ENDIAN is taken directly from gdbarch.
305    This function returns the extracted bits.  */
306
307 static unsigned int
308 extract_bits_primitive (const gdb_byte **source,
309                         unsigned int *source_offset_bits,
310                         int *nbits, int bits_big_endian)
311 {
312   unsigned int avail, mask, datum;
313
314   gdb_assert (*source_offset_bits < 8);
315
316   avail = 8 - *source_offset_bits;
317   if (avail > *nbits)
318     avail = *nbits;
319
320   mask = (1 << avail) - 1;
321   datum = **source;
322   if (bits_big_endian)
323     datum >>= 8 - (*source_offset_bits + *nbits);
324   else
325     datum >>= *source_offset_bits;
326   datum &= mask;
327
328   *nbits -= avail;
329   *source_offset_bits += avail;
330   if (*source_offset_bits >= 8)
331     {
332       *source_offset_bits -= 8;
333       ++*source;
334     }
335
336   return datum;
337 }
338
339 /* Extract some bits from a source buffer and move forward in the
340    buffer.
341    
342    SOURCE is the source buffer.  It is updated as bytes are read.
343    SOURCE_OFFSET_BITS is the offset into SOURCE.  It is updated as
344    bits are read.
345    NBITS is the number of bits to read.
346    BITS_BIG_ENDIAN is taken directly from gdbarch.
347    
348    This function returns the bits that were read.  */
349
350 static unsigned int
351 extract_bits (const gdb_byte **source, unsigned int *source_offset_bits,
352               int nbits, int bits_big_endian)
353 {
354   unsigned int datum;
355
356   gdb_assert (nbits > 0 && nbits <= 8);
357
358   datum = extract_bits_primitive (source, source_offset_bits, &nbits,
359                                   bits_big_endian);
360   if (nbits > 0)
361     {
362       unsigned int more;
363
364       more = extract_bits_primitive (source, source_offset_bits, &nbits,
365                                      bits_big_endian);
366       if (bits_big_endian)
367         datum <<= nbits;
368       else
369         more <<= nbits;
370       datum |= more;
371     }
372
373   return datum;
374 }
375
376 /* Write some bits into a buffer and move forward in the buffer.
377    
378    DATUM is the bits to write.  The low-order bits of DATUM are used.
379    DEST is the destination buffer.  It is updated as bytes are
380    written.
381    DEST_OFFSET_BITS is the bit offset in DEST at which writing is
382    done.
383    NBITS is the number of valid bits in DATUM.
384    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
385
386 static void
387 insert_bits (unsigned int datum,
388              gdb_byte *dest, unsigned int dest_offset_bits,
389              int nbits, int bits_big_endian)
390 {
391   unsigned int mask;
392
393   gdb_assert (dest_offset_bits >= 0 && dest_offset_bits + nbits <= 8);
394
395   mask = (1 << nbits) - 1;
396   if (bits_big_endian)
397     {
398       datum <<= 8 - (dest_offset_bits + nbits);
399       mask <<= 8 - (dest_offset_bits + nbits);
400     }
401   else
402     {
403       datum <<= dest_offset_bits;
404       mask <<= dest_offset_bits;
405     }
406
407   gdb_assert ((datum & ~mask) == 0);
408
409   *dest = (*dest & ~mask) | datum;
410 }
411
412 /* Copy bits from a source to a destination.
413    
414    DEST is where the bits should be written.
415    DEST_OFFSET_BITS is the bit offset into DEST.
416    SOURCE is the source of bits.
417    SOURCE_OFFSET_BITS is the bit offset into SOURCE.
418    BIT_COUNT is the number of bits to copy.
419    BITS_BIG_ENDIAN is taken directly from gdbarch.  */
420
421 static void
422 copy_bitwise (gdb_byte *dest, unsigned int dest_offset_bits,
423               const gdb_byte *source, unsigned int source_offset_bits,
424               unsigned int bit_count,
425               int bits_big_endian)
426 {
427   unsigned int dest_avail;
428   int datum;
429
430   /* Reduce everything to byte-size pieces.  */
431   dest += dest_offset_bits / 8;
432   dest_offset_bits %= 8;
433   source += source_offset_bits / 8;
434   source_offset_bits %= 8;
435
436   dest_avail = 8 - dest_offset_bits % 8;
437
438   /* See if we can fill the first destination byte.  */
439   if (dest_avail < bit_count)
440     {
441       datum = extract_bits (&source, &source_offset_bits, dest_avail,
442                             bits_big_endian);
443       insert_bits (datum, dest, dest_offset_bits, dest_avail, bits_big_endian);
444       ++dest;
445       dest_offset_bits = 0;
446       bit_count -= dest_avail;
447     }
448
449   /* Now, either DEST_OFFSET_BITS is byte-aligned, or we have fewer
450      than 8 bits remaining.  */
451   gdb_assert (dest_offset_bits % 8 == 0 || bit_count < 8);
452   for (; bit_count >= 8; bit_count -= 8)
453     {
454       datum = extract_bits (&source, &source_offset_bits, 8, bits_big_endian);
455       *dest++ = (gdb_byte) datum;
456     }
457
458   /* Finally, we may have a few leftover bits.  */
459   gdb_assert (bit_count <= 8 - dest_offset_bits % 8);
460   if (bit_count > 0)
461     {
462       datum = extract_bits (&source, &source_offset_bits, bit_count,
463                             bits_big_endian);
464       insert_bits (datum, dest, dest_offset_bits, bit_count, bits_big_endian);
465     }
466 }
467
468 static void
469 read_pieced_value (struct value *v)
470 {
471   int i;
472   long offset = 0;
473   ULONGEST bits_to_skip;
474   gdb_byte *contents;
475   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
476   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (v));
477   size_t type_len;
478   size_t buffer_size = 0;
479   char *buffer = NULL;
480   struct cleanup *cleanup;
481   int bits_big_endian
482     = gdbarch_bits_big_endian (get_type_arch (value_type (v)));
483
484   if (value_type (v) != value_enclosing_type (v))
485     internal_error (__FILE__, __LINE__,
486                     _("Should not be able to create a lazy value with "
487                       "an enclosing type"));
488
489   cleanup = make_cleanup (free_current_contents, &buffer);
490
491   contents = value_contents_raw (v);
492   bits_to_skip = 8 * value_offset (v);
493   type_len = 8 * TYPE_LENGTH (value_type (v));
494
495   for (i = 0; i < c->n_pieces && offset < type_len; i++)
496     {
497       struct dwarf_expr_piece *p = &c->pieces[i];
498       size_t this_size, this_size_bits;
499       long dest_offset_bits, source_offset_bits, source_offset;
500       const gdb_byte *intermediate_buffer;
501
502       /* Compute size, source, and destination offsets for copying, in
503          bits.  */
504       this_size_bits = p->size;
505       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
506         {
507           bits_to_skip -= this_size_bits;
508           continue;
509         }
510       if (this_size_bits > type_len - offset)
511         this_size_bits = type_len - offset;
512       if (bits_to_skip > 0)
513         {
514           dest_offset_bits = 0;
515           source_offset_bits = bits_to_skip;
516           this_size_bits -= bits_to_skip;
517           bits_to_skip = 0;
518         }
519       else
520         {
521           dest_offset_bits = offset;
522           source_offset_bits = 0;
523         }
524
525       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
526       source_offset = source_offset_bits / 8;
527       if (buffer_size < this_size)
528         {
529           buffer_size = this_size;
530           buffer = xrealloc (buffer, buffer_size);
531         }
532       intermediate_buffer = buffer;
533
534       /* Copy from the source to DEST_BUFFER.  */
535       switch (p->location)
536         {
537         case DWARF_VALUE_REGISTER:
538           {
539             struct gdbarch *arch = get_frame_arch (frame);
540             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch,
541                                                            p->v.expr.value);
542             int reg_offset = source_offset;
543
544             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
545                 && this_size < register_size (arch, gdb_regnum))
546               {
547                 /* Big-endian, and we want less than full size.  */
548                 reg_offset = register_size (arch, gdb_regnum) - this_size;
549                 /* We want the lower-order THIS_SIZE_BITS of the bytes
550                    we extract from the register.  */
551                 source_offset_bits += 8 * this_size - this_size_bits;
552               }
553
554             if (gdb_regnum != -1)
555               {
556                 get_frame_register_bytes (frame, gdb_regnum, reg_offset, 
557                                           this_size, buffer);
558               }
559             else
560               {
561                 error (_("Unable to access DWARF register number %s"),
562                        paddress (arch, p->v.expr.value));
563               }
564           }
565           break;
566
567         case DWARF_VALUE_MEMORY:
568           if (p->v.expr.in_stack_memory)
569             read_stack (p->v.expr.value + source_offset, buffer, this_size);
570           else
571             read_memory (p->v.expr.value + source_offset, buffer, this_size);
572           break;
573
574         case DWARF_VALUE_STACK:
575           {
576             struct gdbarch *gdbarch = get_type_arch (value_type (v));
577             size_t n = this_size;
578
579             if (n > c->addr_size - source_offset)
580               n = (c->addr_size >= source_offset
581                    ? c->addr_size - source_offset
582                    : 0);
583             if (n == 0)
584               {
585                 /* Nothing.  */
586               }
587             else if (source_offset == 0)
588               store_unsigned_integer (buffer, n,
589                                       gdbarch_byte_order (gdbarch),
590                                       p->v.expr.value);
591             else
592               {
593                 gdb_byte bytes[sizeof (ULONGEST)];
594
595                 store_unsigned_integer (bytes, n + source_offset,
596                                         gdbarch_byte_order (gdbarch),
597                                         p->v.expr.value);
598                 memcpy (buffer, bytes + source_offset, n);
599               }
600           }
601           break;
602
603         case DWARF_VALUE_LITERAL:
604           {
605             size_t n = this_size;
606
607             if (n > p->v.literal.length - source_offset)
608               n = (p->v.literal.length >= source_offset
609                    ? p->v.literal.length - source_offset
610                    : 0);
611             if (n != 0)
612               intermediate_buffer = p->v.literal.data + source_offset;
613           }
614           break;
615
616         case DWARF_VALUE_OPTIMIZED_OUT:
617           /* We just leave the bits empty for now.  This is not ideal
618              but gdb currently does not have a nice way to represent
619              optimized-out pieces.  */
620           warning (_("bits %ld-%ld in computed object were optimized out; "
621                      "replacing with zeroes"),
622                    offset,
623                    offset + (long) this_size_bits);
624           break;
625
626         default:
627           internal_error (__FILE__, __LINE__, _("invalid location type"));
628         }
629
630       if (p->location != DWARF_VALUE_OPTIMIZED_OUT)
631         copy_bitwise (contents, dest_offset_bits,
632                       intermediate_buffer, source_offset_bits % 8,
633                       this_size_bits, bits_big_endian);
634
635       offset += this_size_bits;
636     }
637
638   do_cleanups (cleanup);
639 }
640
641 static void
642 write_pieced_value (struct value *to, struct value *from)
643 {
644   int i;
645   long offset = 0;
646   ULONGEST bits_to_skip;
647   const gdb_byte *contents;
648   struct piece_closure *c = (struct piece_closure *) value_computed_closure (to);
649   struct frame_info *frame = frame_find_by_id (VALUE_FRAME_ID (to));
650   size_t type_len;
651   size_t buffer_size = 0;
652   char *buffer = NULL;
653   struct cleanup *cleanup;
654   int bits_big_endian
655     = gdbarch_bits_big_endian (get_type_arch (value_type (to)));
656
657   if (frame == NULL)
658     {
659       set_value_optimized_out (to, 1);
660       return;
661     }
662
663   cleanup = make_cleanup (free_current_contents, &buffer);
664
665   contents = value_contents (from);
666   bits_to_skip = 8 * value_offset (to);
667   type_len = 8 * TYPE_LENGTH (value_type (to));
668   for (i = 0; i < c->n_pieces && offset < type_len; i++)
669     {
670       struct dwarf_expr_piece *p = &c->pieces[i];
671       size_t this_size_bits, this_size;
672       long dest_offset_bits, source_offset_bits, dest_offset, source_offset;
673       int need_bitwise;
674       const gdb_byte *source_buffer;
675
676       this_size_bits = p->size;
677       if (bits_to_skip > 0 && bits_to_skip >= this_size_bits)
678         {
679           bits_to_skip -= this_size_bits;
680           continue;
681         }
682       if (this_size_bits > type_len - offset)
683         this_size_bits = type_len - offset;
684       if (bits_to_skip > 0)
685         {
686           dest_offset_bits = bits_to_skip;
687           source_offset_bits = 0;
688           this_size_bits -= bits_to_skip;
689           bits_to_skip = 0;
690         }
691       else
692         {
693           dest_offset_bits = 0;
694           source_offset_bits = offset;
695         }
696
697       this_size = (this_size_bits + source_offset_bits % 8 + 7) / 8;
698       source_offset = source_offset_bits / 8;
699       dest_offset = dest_offset_bits / 8;
700       if (dest_offset_bits % 8 == 0 && source_offset_bits % 8 == 0)
701         {
702           source_buffer = contents + source_offset;
703           need_bitwise = 0;
704         }
705       else
706         {
707           if (buffer_size < this_size)
708             {
709               buffer_size = this_size;
710               buffer = xrealloc (buffer, buffer_size);
711             }
712           source_buffer = buffer;
713           need_bitwise = 1;
714         }
715
716       switch (p->location)
717         {
718         case DWARF_VALUE_REGISTER:
719           {
720             struct gdbarch *arch = get_frame_arch (frame);
721             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, p->v.expr.value);
722             int reg_offset = dest_offset;
723
724             if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG
725                 && this_size <= register_size (arch, gdb_regnum))
726               /* Big-endian, and we want less than full size.  */
727               reg_offset = register_size (arch, gdb_regnum) - this_size;
728
729             if (gdb_regnum != -1)
730               {
731                 if (need_bitwise)
732                   {
733                     get_frame_register_bytes (frame, gdb_regnum, reg_offset,
734                                               this_size, buffer);
735                     copy_bitwise (buffer, dest_offset_bits,
736                                   contents, source_offset_bits,
737                                   this_size_bits,
738                                   bits_big_endian);
739                   }
740
741                 put_frame_register_bytes (frame, gdb_regnum, reg_offset, 
742                                           this_size, source_buffer);
743               }
744             else
745               {
746                 error (_("Unable to write to DWARF register number %s"),
747                        paddress (arch, p->v.expr.value));
748               }
749           }
750           break;
751         case DWARF_VALUE_MEMORY:
752           if (need_bitwise)
753             {
754               /* Only the first and last bytes can possibly have any
755                  bits reused.  */
756               read_memory (p->v.expr.value + dest_offset, buffer, 1);
757               read_memory (p->v.expr.value + dest_offset + this_size - 1,
758                            buffer + this_size - 1, 1);
759               copy_bitwise (buffer, dest_offset_bits,
760                             contents, source_offset_bits,
761                             this_size_bits,
762                             bits_big_endian);
763             }
764
765           write_memory (p->v.expr.value + dest_offset,
766                         source_buffer, this_size);
767           break;
768         default:
769           set_value_optimized_out (to, 1);
770           goto done;
771         }
772       offset += this_size_bits;
773     }
774
775  done:
776   do_cleanups (cleanup);
777 }
778
779 static void *
780 copy_pieced_value_closure (struct value *v)
781 {
782   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
783   
784   ++c->refc;
785   return c;
786 }
787
788 static void
789 free_pieced_value_closure (struct value *v)
790 {
791   struct piece_closure *c = (struct piece_closure *) value_computed_closure (v);
792
793   --c->refc;
794   if (c->refc == 0)
795     {
796       xfree (c->pieces);
797       xfree (c);
798     }
799 }
800
801 /* Functions for accessing a variable described by DW_OP_piece.  */
802 static struct lval_funcs pieced_value_funcs = {
803   read_pieced_value,
804   write_pieced_value,
805   copy_pieced_value_closure,
806   free_pieced_value_closure
807 };
808
809 /* Evaluate a location description, starting at DATA and with length
810    SIZE, to find the current location of variable of TYPE in the context
811    of FRAME.  */
812
813 static struct value *
814 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
815                           const gdb_byte *data, unsigned short size,
816                           struct dwarf2_per_cu_data *per_cu)
817 {
818   struct value *retval;
819   struct dwarf_expr_baton baton;
820   struct dwarf_expr_context *ctx;
821   struct cleanup *old_chain;
822
823   if (size == 0)
824     {
825       retval = allocate_value (type);
826       VALUE_LVAL (retval) = not_lval;
827       set_value_optimized_out (retval, 1);
828       return retval;
829     }
830
831   baton.frame = frame;
832   baton.per_cu = per_cu;
833
834   ctx = new_dwarf_expr_context ();
835   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
836
837   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
838   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
839   ctx->baton = &baton;
840   ctx->read_reg = dwarf_expr_read_reg;
841   ctx->read_mem = dwarf_expr_read_mem;
842   ctx->get_frame_base = dwarf_expr_frame_base;
843   ctx->get_frame_cfa = dwarf_expr_frame_cfa;
844   ctx->get_tls_address = dwarf_expr_tls_address;
845   ctx->dwarf_call = dwarf_expr_dwarf_call;
846
847   dwarf_expr_eval (ctx, data, size);
848   if (ctx->num_pieces > 0)
849     {
850       struct piece_closure *c;
851       struct frame_id frame_id = get_frame_id (frame);
852
853       c = allocate_piece_closure (ctx->num_pieces, ctx->pieces,
854                                   ctx->addr_size);
855       retval = allocate_computed_value (type, &pieced_value_funcs, c);
856       VALUE_FRAME_ID (retval) = frame_id;
857     }
858   else
859     {
860       switch (ctx->location)
861         {
862         case DWARF_VALUE_REGISTER:
863           {
864             struct gdbarch *arch = get_frame_arch (frame);
865             CORE_ADDR dwarf_regnum = dwarf_expr_fetch (ctx, 0);
866             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
867
868             if (gdb_regnum != -1)
869               retval = value_from_register (type, gdb_regnum, frame);
870             else
871               error (_("Unable to access DWARF register number %s"),
872                      paddress (arch, dwarf_regnum));
873           }
874           break;
875
876         case DWARF_VALUE_MEMORY:
877           {
878             CORE_ADDR address = dwarf_expr_fetch (ctx, 0);
879             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
880
881             retval = allocate_value (type);
882             VALUE_LVAL (retval) = lval_memory;
883             set_value_lazy (retval, 1);
884             if (in_stack_memory)
885               set_value_stack (retval, 1);
886             set_value_address (retval, address);
887           }
888           break;
889
890         case DWARF_VALUE_STACK:
891           {
892             ULONGEST value = (ULONGEST) dwarf_expr_fetch (ctx, 0);
893             bfd_byte *contents;
894             size_t n = ctx->addr_size;
895
896             retval = allocate_value (type);
897             contents = value_contents_raw (retval);
898             if (n > TYPE_LENGTH (type))
899               n = TYPE_LENGTH (type);
900             store_unsigned_integer (contents, n,
901                                     gdbarch_byte_order (ctx->gdbarch),
902                                     value);
903           }
904           break;
905
906         case DWARF_VALUE_LITERAL:
907           {
908             bfd_byte *contents;
909             size_t n = ctx->len;
910
911             retval = allocate_value (type);
912             contents = value_contents_raw (retval);
913             if (n > TYPE_LENGTH (type))
914               n = TYPE_LENGTH (type);
915             memcpy (contents, ctx->data, n);
916           }
917           break;
918
919           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
920              it can only be encountered when making a piece.  */
921         case DWARF_VALUE_OPTIMIZED_OUT:
922         default:
923           internal_error (__FILE__, __LINE__, _("invalid location type"));
924         }
925     }
926
927   set_value_initialized (retval, ctx->initialized);
928
929   do_cleanups (old_chain);
930
931   return retval;
932 }
933 \f
934 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
935
936 struct needs_frame_baton
937 {
938   int needs_frame;
939   struct dwarf2_per_cu_data *per_cu;
940 };
941
942 /* Reads from registers do require a frame.  */
943 static CORE_ADDR
944 needs_frame_read_reg (void *baton, int regnum)
945 {
946   struct needs_frame_baton *nf_baton = baton;
947
948   nf_baton->needs_frame = 1;
949   return 1;
950 }
951
952 /* Reads from memory do not require a frame.  */
953 static void
954 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
955 {
956   memset (buf, 0, len);
957 }
958
959 /* Frame-relative accesses do require a frame.  */
960 static void
961 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
962 {
963   static gdb_byte lit0 = DW_OP_lit0;
964   struct needs_frame_baton *nf_baton = baton;
965
966   *start = &lit0;
967   *length = 1;
968
969   nf_baton->needs_frame = 1;
970 }
971
972 /* CFA accesses require a frame.  */
973
974 static CORE_ADDR
975 needs_frame_frame_cfa (void *baton)
976 {
977   struct needs_frame_baton *nf_baton = baton;
978
979   nf_baton->needs_frame = 1;
980   return 1;
981 }
982
983 /* Thread-local accesses do require a frame.  */
984 static CORE_ADDR
985 needs_frame_tls_address (void *baton, CORE_ADDR offset)
986 {
987   struct needs_frame_baton *nf_baton = baton;
988
989   nf_baton->needs_frame = 1;
990   return 1;
991 }
992
993 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
994
995 static void
996 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
997 {
998   struct needs_frame_baton *nf_baton = ctx->baton;
999
1000   return per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu);
1001 }
1002
1003 /* Return non-zero iff the location expression at DATA (length SIZE)
1004    requires a frame to evaluate.  */
1005
1006 static int
1007 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1008                              struct dwarf2_per_cu_data *per_cu)
1009 {
1010   struct needs_frame_baton baton;
1011   struct dwarf_expr_context *ctx;
1012   int in_reg;
1013   struct cleanup *old_chain;
1014
1015   baton.needs_frame = 0;
1016   baton.per_cu = per_cu;
1017
1018   ctx = new_dwarf_expr_context ();
1019   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1020
1021   ctx->gdbarch = get_objfile_arch (dwarf2_per_cu_objfile (per_cu));
1022   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1023   ctx->baton = &baton;
1024   ctx->read_reg = needs_frame_read_reg;
1025   ctx->read_mem = needs_frame_read_mem;
1026   ctx->get_frame_base = needs_frame_frame_base;
1027   ctx->get_frame_cfa = needs_frame_frame_cfa;
1028   ctx->get_tls_address = needs_frame_tls_address;
1029   ctx->dwarf_call = needs_frame_dwarf_call;
1030
1031   dwarf_expr_eval (ctx, data, size);
1032
1033   in_reg = ctx->location == DWARF_VALUE_REGISTER;
1034
1035   if (ctx->num_pieces > 0)
1036     {
1037       int i;
1038
1039       /* If the location has several pieces, and any of them are in
1040          registers, then we will need a frame to fetch them from.  */
1041       for (i = 0; i < ctx->num_pieces; i++)
1042         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1043           in_reg = 1;
1044     }
1045
1046   do_cleanups (old_chain);
1047
1048   return baton.needs_frame || in_reg;
1049 }
1050
1051 /* A helper function that throws an unimplemented error mentioning a
1052    given DWARF operator.  */
1053
1054 static void
1055 unimplemented (unsigned int op)
1056 {
1057   error (_("DWARF operator %s cannot be translated to an agent expression"),
1058          dwarf_stack_op_name (op, 1));
1059 }
1060
1061 /* A helper function to convert a DWARF register to an arch register.
1062    ARCH is the architecture.
1063    DWARF_REG is the register.
1064    This will throw an exception if the DWARF register cannot be
1065    translated to an architecture register.  */
1066
1067 static int
1068 translate_register (struct gdbarch *arch, int dwarf_reg)
1069 {
1070   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1071   if (reg == -1)
1072     error (_("Unable to access DWARF register number %d"), dwarf_reg);
1073   return reg;
1074 }
1075
1076 /* A helper function that emits an access to memory.  ARCH is the
1077    target architecture.  EXPR is the expression which we are building.
1078    NBITS is the number of bits we want to read.  This emits the
1079    opcodes needed to read the memory and then extract the desired
1080    bits.  */
1081
1082 static void
1083 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1084 {
1085   ULONGEST nbytes = (nbits + 7) / 8;
1086
1087   gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1088
1089   if (trace_kludge)
1090     ax_trace_quick (expr, nbytes);
1091
1092   if (nbits <= 8)
1093     ax_simple (expr, aop_ref8);
1094   else if (nbits <= 16)
1095     ax_simple (expr, aop_ref16);
1096   else if (nbits <= 32)
1097     ax_simple (expr, aop_ref32);
1098   else
1099     ax_simple (expr, aop_ref64);
1100
1101   /* If we read exactly the number of bytes we wanted, we're done.  */
1102   if (8 * nbytes == nbits)
1103     return;
1104
1105   if (gdbarch_bits_big_endian (arch))
1106     {
1107       /* On a bits-big-endian machine, we want the high-order
1108          NBITS.  */
1109       ax_const_l (expr, 8 * nbytes - nbits);
1110       ax_simple (expr, aop_rsh_unsigned);
1111     }
1112   else
1113     {
1114       /* On a bits-little-endian box, we want the low-order NBITS.  */
1115       ax_zero_ext (expr, nbits);
1116     }
1117 }
1118
1119 /* Compile a DWARF location expression to an agent expression.
1120    
1121    EXPR is the agent expression we are building.
1122    LOC is the agent value we modify.
1123    ARCH is the architecture.
1124    ADDR_SIZE is the size of addresses, in bytes.
1125    OP_PTR is the start of the location expression.
1126    OP_END is one past the last byte of the location expression.
1127    
1128    This will throw an exception for various kinds of errors -- for
1129    example, if the expression cannot be compiled, or if the expression
1130    is invalid.  */
1131
1132 static void
1133 compile_dwarf_to_ax (struct agent_expr *expr, struct axs_value *loc,
1134                      struct gdbarch *arch, unsigned int addr_size,
1135                      const gdb_byte *op_ptr, const gdb_byte *op_end,
1136                      struct dwarf2_per_cu_data *per_cu)
1137 {
1138   struct cleanup *cleanups;
1139   int i, *offsets;
1140   VEC(int) *dw_labels = NULL, *patches = NULL;
1141   const gdb_byte * const base = op_ptr;
1142   const gdb_byte *previous_piece = op_ptr;
1143   enum bfd_endian byte_order = gdbarch_byte_order (arch);
1144   ULONGEST bits_collected = 0;
1145   unsigned int addr_size_bits = 8 * addr_size;
1146   int bits_big_endian = gdbarch_bits_big_endian (arch);
1147
1148   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1149   cleanups = make_cleanup (xfree, offsets);
1150
1151   for (i = 0; i < op_end - op_ptr; ++i)
1152     offsets[i] = -1;
1153
1154   make_cleanup (VEC_cleanup (int), &dw_labels);
1155   make_cleanup (VEC_cleanup (int), &patches);
1156
1157   /* By default we are making an address.  */
1158   loc->kind = axs_lvalue_memory;
1159
1160   while (op_ptr < op_end)
1161     {
1162       enum dwarf_location_atom op = *op_ptr;
1163       CORE_ADDR result;
1164       ULONGEST uoffset, reg;
1165       LONGEST offset;
1166       int i;
1167
1168       offsets[op_ptr - base] = expr->len;
1169       ++op_ptr;
1170
1171       /* Our basic approach to code generation is to map DWARF
1172          operations directly to AX operations.  However, there are
1173          some differences.
1174
1175          First, DWARF works on address-sized units, but AX always uses
1176          LONGEST.  For most operations we simply ignore this
1177          difference; instead we generate sign extensions as needed
1178          before division and comparison operations.  It would be nice
1179          to omit the sign extensions, but there is no way to determine
1180          the size of the target's LONGEST.  (This code uses the size
1181          of the host LONGEST in some cases -- that is a bug but it is
1182          difficult to fix.)
1183
1184          Second, some DWARF operations cannot be translated to AX.
1185          For these we simply fail.  See
1186          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
1187       switch (op)
1188         {
1189         case DW_OP_lit0:
1190         case DW_OP_lit1:
1191         case DW_OP_lit2:
1192         case DW_OP_lit3:
1193         case DW_OP_lit4:
1194         case DW_OP_lit5:
1195         case DW_OP_lit6:
1196         case DW_OP_lit7:
1197         case DW_OP_lit8:
1198         case DW_OP_lit9:
1199         case DW_OP_lit10:
1200         case DW_OP_lit11:
1201         case DW_OP_lit12:
1202         case DW_OP_lit13:
1203         case DW_OP_lit14:
1204         case DW_OP_lit15:
1205         case DW_OP_lit16:
1206         case DW_OP_lit17:
1207         case DW_OP_lit18:
1208         case DW_OP_lit19:
1209         case DW_OP_lit20:
1210         case DW_OP_lit21:
1211         case DW_OP_lit22:
1212         case DW_OP_lit23:
1213         case DW_OP_lit24:
1214         case DW_OP_lit25:
1215         case DW_OP_lit26:
1216         case DW_OP_lit27:
1217         case DW_OP_lit28:
1218         case DW_OP_lit29:
1219         case DW_OP_lit30:
1220         case DW_OP_lit31:
1221           ax_const_l (expr, op - DW_OP_lit0);
1222           break;
1223
1224         case DW_OP_addr:
1225           result = dwarf2_read_address (arch, op_ptr, op_end, addr_size);
1226           ax_const_l (expr, result);
1227           op_ptr += addr_size;
1228           break;
1229
1230         case DW_OP_const1u:
1231           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1232           op_ptr += 1;
1233           break;
1234         case DW_OP_const1s:
1235           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1236           op_ptr += 1;
1237           break;
1238         case DW_OP_const2u:
1239           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1240           op_ptr += 2;
1241           break;
1242         case DW_OP_const2s:
1243           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1244           op_ptr += 2;
1245           break;
1246         case DW_OP_const4u:
1247           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1248           op_ptr += 4;
1249           break;
1250         case DW_OP_const4s:
1251           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1252           op_ptr += 4;
1253           break;
1254         case DW_OP_const8u:
1255           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1256           op_ptr += 8;
1257           break;
1258         case DW_OP_const8s:
1259           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1260           op_ptr += 8;
1261           break;
1262         case DW_OP_constu:
1263           op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1264           ax_const_l (expr, uoffset);
1265           break;
1266         case DW_OP_consts:
1267           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1268           ax_const_l (expr, offset);
1269           break;
1270
1271         case DW_OP_reg0:
1272         case DW_OP_reg1:
1273         case DW_OP_reg2:
1274         case DW_OP_reg3:
1275         case DW_OP_reg4:
1276         case DW_OP_reg5:
1277         case DW_OP_reg6:
1278         case DW_OP_reg7:
1279         case DW_OP_reg8:
1280         case DW_OP_reg9:
1281         case DW_OP_reg10:
1282         case DW_OP_reg11:
1283         case DW_OP_reg12:
1284         case DW_OP_reg13:
1285         case DW_OP_reg14:
1286         case DW_OP_reg15:
1287         case DW_OP_reg16:
1288         case DW_OP_reg17:
1289         case DW_OP_reg18:
1290         case DW_OP_reg19:
1291         case DW_OP_reg20:
1292         case DW_OP_reg21:
1293         case DW_OP_reg22:
1294         case DW_OP_reg23:
1295         case DW_OP_reg24:
1296         case DW_OP_reg25:
1297         case DW_OP_reg26:
1298         case DW_OP_reg27:
1299         case DW_OP_reg28:
1300         case DW_OP_reg29:
1301         case DW_OP_reg30:
1302         case DW_OP_reg31:
1303           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1304           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1305           loc->kind = axs_lvalue_register;
1306           break;
1307
1308         case DW_OP_regx:
1309           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1310           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1311           loc->u.reg = translate_register (arch, reg);
1312           loc->kind = axs_lvalue_register;
1313           break;
1314
1315         case DW_OP_implicit_value:
1316           {
1317             ULONGEST len;
1318
1319             op_ptr = read_uleb128 (op_ptr, op_end, &len);
1320             if (op_ptr + len > op_end)
1321               error (_("DW_OP_implicit_value: too few bytes available."));
1322             if (len > sizeof (ULONGEST))
1323               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1324                      (int) len);
1325
1326             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1327                                                         byte_order));
1328             op_ptr += len;
1329             dwarf_expr_require_composition (op_ptr, op_end,
1330                                             "DW_OP_implicit_value");
1331
1332             loc->kind = axs_rvalue;
1333           }
1334           break;
1335
1336         case DW_OP_stack_value:
1337           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1338           loc->kind = axs_rvalue;
1339           break;
1340
1341         case DW_OP_breg0:
1342         case DW_OP_breg1:
1343         case DW_OP_breg2:
1344         case DW_OP_breg3:
1345         case DW_OP_breg4:
1346         case DW_OP_breg5:
1347         case DW_OP_breg6:
1348         case DW_OP_breg7:
1349         case DW_OP_breg8:
1350         case DW_OP_breg9:
1351         case DW_OP_breg10:
1352         case DW_OP_breg11:
1353         case DW_OP_breg12:
1354         case DW_OP_breg13:
1355         case DW_OP_breg14:
1356         case DW_OP_breg15:
1357         case DW_OP_breg16:
1358         case DW_OP_breg17:
1359         case DW_OP_breg18:
1360         case DW_OP_breg19:
1361         case DW_OP_breg20:
1362         case DW_OP_breg21:
1363         case DW_OP_breg22:
1364         case DW_OP_breg23:
1365         case DW_OP_breg24:
1366         case DW_OP_breg25:
1367         case DW_OP_breg26:
1368         case DW_OP_breg27:
1369         case DW_OP_breg28:
1370         case DW_OP_breg29:
1371         case DW_OP_breg30:
1372         case DW_OP_breg31:
1373           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1374           i = translate_register (arch, op - DW_OP_breg0);
1375           ax_reg (expr, i);
1376           if (offset != 0)
1377             {
1378               ax_const_l (expr, offset);
1379               ax_simple (expr, aop_add);
1380             }
1381           break;
1382         case DW_OP_bregx:
1383           {
1384             op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1385             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1386             i = translate_register (arch, reg);
1387             ax_reg (expr, i);
1388             if (offset != 0)
1389               {
1390                 ax_const_l (expr, offset);
1391                 ax_simple (expr, aop_add);
1392               }
1393           }
1394           break;
1395         case DW_OP_fbreg:
1396           {
1397             const gdb_byte *datastart;
1398             size_t datalen;
1399             unsigned int before_stack_len;
1400             struct block *b;
1401             struct symbol *framefunc;
1402             LONGEST base_offset = 0;
1403
1404             b = block_for_pc (expr->scope);
1405
1406             if (!b)
1407               error (_("No block found for address"));
1408
1409             framefunc = block_linkage_function (b);
1410
1411             if (!framefunc)
1412               error (_("No function found for block"));
1413
1414             dwarf_expr_frame_base_1 (framefunc, expr->scope,
1415                                      &datastart, &datalen);
1416
1417             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1418             compile_dwarf_to_ax (expr, loc, arch, addr_size, datastart,
1419                                  datastart + datalen, per_cu);
1420
1421             if (offset != 0)
1422               {
1423                 ax_const_l (expr, offset);
1424                 ax_simple (expr, aop_add);
1425               }
1426
1427             loc->kind = axs_lvalue_memory;
1428           }
1429           break;
1430
1431         case DW_OP_dup:
1432           ax_simple (expr, aop_dup);
1433           break;
1434
1435         case DW_OP_drop:
1436           ax_simple (expr, aop_pop);
1437           break;
1438
1439         case DW_OP_pick:
1440           offset = *op_ptr++;
1441           unimplemented (op);
1442           break;
1443           
1444         case DW_OP_swap:
1445           ax_simple (expr, aop_swap);
1446           break;
1447
1448         case DW_OP_over:
1449           /* We can't directly support DW_OP_over, but GCC emits it as
1450              part of a sequence to implement signed modulus.  As a
1451              hack, we recognize this sequence.  Note that if GCC ever
1452              generates a branch to the middle of this sequence, then
1453              we will die somehow.  */
1454           if (op_end - op_ptr >= 4
1455               && op_ptr[0] == DW_OP_over
1456               && op_ptr[1] == DW_OP_div
1457               && op_ptr[2] == DW_OP_mul
1458               && op_ptr[3] == DW_OP_minus)
1459             {
1460               /* Sign extend the operands.  */
1461               ax_ext (expr, addr_size_bits);
1462               ax_simple (expr, aop_swap);
1463               ax_ext (expr, addr_size_bits);
1464               ax_simple (expr, aop_swap);
1465               ax_simple (expr, aop_rem_signed);
1466               op_ptr += 4;
1467             }
1468           else
1469             unimplemented (op);
1470           break;
1471
1472         case DW_OP_rot:
1473           unimplemented (op);
1474           break;
1475
1476         case DW_OP_deref:
1477         case DW_OP_deref_size:
1478           {
1479             int size;
1480
1481             if (op == DW_OP_deref_size)
1482               size = *op_ptr++;
1483             else
1484               size = addr_size;
1485
1486             switch (size)
1487               {
1488               case 8:
1489                 ax_simple (expr, aop_ref8);
1490                 break;
1491               case 16:
1492                 ax_simple (expr, aop_ref16);
1493                 break;
1494               case 32:
1495                 ax_simple (expr, aop_ref32);
1496                 break;
1497               case 64:
1498                 ax_simple (expr, aop_ref64);
1499                 break;
1500               default:
1501                 error (_("Unsupported size %d in %s"),
1502                        size, dwarf_stack_op_name (op, 1));
1503               }
1504           }
1505           break;
1506
1507         case DW_OP_abs:
1508           /* Sign extend the operand.  */
1509           ax_ext (expr, addr_size_bits);
1510           ax_simple (expr, aop_dup);
1511           ax_const_l (expr, 0);
1512           ax_simple (expr, aop_less_signed);
1513           ax_simple (expr, aop_log_not);
1514           i = ax_goto (expr, aop_if_goto);
1515           /* We have to emit 0 - X.  */
1516           ax_const_l (expr, 0);
1517           ax_simple (expr, aop_swap);
1518           ax_simple (expr, aop_sub);
1519           ax_label (expr, i, expr->len);
1520           break;
1521
1522         case DW_OP_neg:
1523           /* No need to sign extend here.  */
1524           ax_const_l (expr, 0);
1525           ax_simple (expr, aop_swap);
1526           ax_simple (expr, aop_sub);
1527           break;
1528
1529         case DW_OP_not:
1530           /* Sign extend the operand.  */
1531           ax_ext (expr, addr_size_bits);
1532           ax_simple (expr, aop_bit_not);
1533           break;
1534
1535         case DW_OP_plus_uconst:
1536           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1537           /* It would be really weird to emit `DW_OP_plus_uconst 0',
1538              but we micro-optimize anyhow.  */
1539           if (reg != 0)
1540             {
1541               ax_const_l (expr, reg);
1542               ax_simple (expr, aop_add);
1543             }
1544           break;
1545
1546         case DW_OP_and:
1547           ax_simple (expr, aop_bit_and);
1548           break;
1549
1550         case DW_OP_div:
1551           /* Sign extend the operands.  */
1552           ax_ext (expr, addr_size_bits);
1553           ax_simple (expr, aop_swap);
1554           ax_ext (expr, addr_size_bits);
1555           ax_simple (expr, aop_swap);
1556           ax_simple (expr, aop_div_signed);
1557           break;
1558
1559         case DW_OP_minus:
1560           ax_simple (expr, aop_sub);
1561           break;
1562
1563         case DW_OP_mod:
1564           ax_simple (expr, aop_rem_unsigned);
1565           break;
1566
1567         case DW_OP_mul:
1568           ax_simple (expr, aop_mul);
1569           break;
1570
1571         case DW_OP_or:
1572           ax_simple (expr, aop_bit_or);
1573           break;
1574
1575         case DW_OP_plus:
1576           ax_simple (expr, aop_add);
1577           break;
1578
1579         case DW_OP_shl:
1580           ax_simple (expr, aop_lsh);
1581           break;
1582
1583         case DW_OP_shr:
1584           ax_simple (expr, aop_rsh_unsigned);
1585           break;
1586
1587         case DW_OP_shra:
1588           ax_simple (expr, aop_rsh_signed);
1589           break;
1590
1591         case DW_OP_xor:
1592           ax_simple (expr, aop_bit_xor);
1593           break;
1594
1595         case DW_OP_le:
1596           /* Sign extend the operands.  */
1597           ax_ext (expr, addr_size_bits);
1598           ax_simple (expr, aop_swap);
1599           ax_ext (expr, addr_size_bits);
1600           /* Note no swap here: A <= B is !(B < A).  */
1601           ax_simple (expr, aop_less_signed);
1602           ax_simple (expr, aop_log_not);
1603           break;
1604
1605         case DW_OP_ge:
1606           /* Sign extend the operands.  */
1607           ax_ext (expr, addr_size_bits);
1608           ax_simple (expr, aop_swap);
1609           ax_ext (expr, addr_size_bits);
1610           ax_simple (expr, aop_swap);
1611           /* A >= B is !(A < B).  */
1612           ax_simple (expr, aop_less_signed);
1613           ax_simple (expr, aop_log_not);
1614           break;
1615
1616         case DW_OP_eq:
1617           /* Sign extend the operands.  */
1618           ax_ext (expr, addr_size_bits);
1619           ax_simple (expr, aop_swap);
1620           ax_ext (expr, addr_size_bits);
1621           /* No need for a second swap here.  */
1622           ax_simple (expr, aop_equal);
1623           break;
1624
1625         case DW_OP_lt:
1626           /* Sign extend the operands.  */
1627           ax_ext (expr, addr_size_bits);
1628           ax_simple (expr, aop_swap);
1629           ax_ext (expr, addr_size_bits);
1630           ax_simple (expr, aop_swap);
1631           ax_simple (expr, aop_less_signed);
1632           break;
1633
1634         case DW_OP_gt:
1635           /* Sign extend the operands.  */
1636           ax_ext (expr, addr_size_bits);
1637           ax_simple (expr, aop_swap);
1638           ax_ext (expr, addr_size_bits);
1639           /* Note no swap here: A > B is B < A.  */
1640           ax_simple (expr, aop_less_signed);
1641           break;
1642
1643         case DW_OP_ne:
1644           /* Sign extend the operands.  */
1645           ax_ext (expr, addr_size_bits);
1646           ax_simple (expr, aop_swap);
1647           ax_ext (expr, addr_size_bits);
1648           /* No need for a swap here.  */
1649           ax_simple (expr, aop_equal);
1650           ax_simple (expr, aop_log_not);
1651           break;
1652
1653         case DW_OP_call_frame_cfa:
1654           unimplemented (op);
1655           break;
1656
1657         case DW_OP_GNU_push_tls_address:
1658           unimplemented (op);
1659           break;
1660
1661         case DW_OP_skip:
1662           offset = extract_signed_integer (op_ptr, 2, byte_order);
1663           op_ptr += 2;
1664           i = ax_goto (expr, aop_goto);
1665           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1666           VEC_safe_push (int, patches, i);
1667           break;
1668
1669         case DW_OP_bra:
1670           offset = extract_signed_integer (op_ptr, 2, byte_order);
1671           op_ptr += 2;
1672           /* Zero extend the operand.  */
1673           ax_zero_ext (expr, addr_size_bits);
1674           i = ax_goto (expr, aop_if_goto);
1675           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
1676           VEC_safe_push (int, patches, i);
1677           break;
1678
1679         case DW_OP_nop:
1680           break;
1681
1682         case DW_OP_piece:
1683         case DW_OP_bit_piece:
1684           {
1685             ULONGEST size, offset;
1686
1687             if (op_ptr - 1 == previous_piece)
1688               error (_("Cannot translate empty pieces to agent expressions"));
1689             previous_piece = op_ptr - 1;
1690
1691             op_ptr = read_uleb128 (op_ptr, op_end, &size);
1692             if (op == DW_OP_piece)
1693               {
1694                 size *= 8;
1695                 offset = 0;
1696               }
1697             else
1698               op_ptr = read_uleb128 (op_ptr, op_end, &offset);
1699
1700             if (bits_collected + size > 8 * sizeof (LONGEST))
1701               error (_("Expression pieces exceed word size"));
1702
1703             /* Access the bits.  */
1704             switch (loc->kind)
1705               {
1706               case axs_lvalue_register:
1707                 ax_reg (expr, loc->u.reg);
1708                 break;
1709
1710               case axs_lvalue_memory:
1711                 /* Offset the pointer, if needed.  */
1712                 if (offset > 8)
1713                   {
1714                     ax_const_l (expr, offset / 8);
1715                     ax_simple (expr, aop_add);
1716                     offset %= 8;
1717                   }
1718                 access_memory (arch, expr, size);
1719                 break;
1720               }
1721
1722             /* For a bits-big-endian target, shift up what we already
1723                have.  For a bits-little-endian target, shift up the
1724                new data.  Note that there is a potential bug here if
1725                the DWARF expression leaves multiple values on the
1726                stack.  */
1727             if (bits_collected > 0)
1728               {
1729                 if (bits_big_endian)
1730                   {
1731                     ax_simple (expr, aop_swap);
1732                     ax_const_l (expr, size);
1733                     ax_simple (expr, aop_lsh);
1734                     /* We don't need a second swap here, because
1735                        aop_bit_or is symmetric.  */
1736                   }
1737                 else
1738                   {
1739                     ax_const_l (expr, size);
1740                     ax_simple (expr, aop_lsh);
1741                   }
1742                 ax_simple (expr, aop_bit_or);
1743               }
1744
1745             bits_collected += size;
1746             loc->kind = axs_rvalue;
1747           }
1748           break;
1749
1750         case DW_OP_GNU_uninit:
1751           unimplemented (op);
1752
1753         case DW_OP_call2:
1754         case DW_OP_call4:
1755           {
1756             struct dwarf2_locexpr_baton block;
1757             int size = (op == DW_OP_call2 ? 2 : 4);
1758
1759             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
1760             op_ptr += size;
1761
1762             block = dwarf2_fetch_die_location_block (uoffset, per_cu);
1763
1764             /* DW_OP_call_ref is currently not supported.  */
1765             gdb_assert (block.per_cu == per_cu);
1766
1767             compile_dwarf_to_ax (expr, loc, arch, addr_size,
1768                                  block.data, block.data + block.size,
1769                                  per_cu);
1770           }
1771           break;
1772
1773         case DW_OP_call_ref:
1774           unimplemented (op);
1775
1776         default:
1777           error (_("Unhandled dwarf expression opcode 0x%x"), op);
1778         }
1779     }
1780
1781   /* Patch all the branches we emitted.  */
1782   for (i = 0; i < VEC_length (int, patches); ++i)
1783     {
1784       int targ = offsets[VEC_index (int, dw_labels, i)];
1785       if (targ == -1)
1786         internal_error (__FILE__, __LINE__, _("invalid label"));
1787       ax_label (expr, VEC_index (int, patches, i), targ);
1788     }
1789
1790   do_cleanups (cleanups);
1791 }
1792
1793 \f
1794 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
1795    evaluator to calculate the location.  */
1796 static struct value *
1797 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
1798 {
1799   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1800   struct value *val;
1801
1802   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
1803                                   dlbaton->size, dlbaton->per_cu);
1804
1805   return val;
1806 }
1807
1808 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
1809 static int
1810 locexpr_read_needs_frame (struct symbol *symbol)
1811 {
1812   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
1813
1814   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
1815                                       dlbaton->per_cu);
1816 }
1817
1818 /* Return true if DATA points to the end of a piece.  END is one past
1819    the last byte in the expression.  */
1820
1821 static int
1822 piece_end_p (const gdb_byte *data, const gdb_byte *end)
1823 {
1824   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
1825 }
1826
1827 /* Nicely describe a single piece of a location, returning an updated
1828    position in the bytecode sequence.  This function cannot recognize
1829    all locations; if a location is not recognized, it simply returns
1830    DATA.  */
1831
1832 static const gdb_byte *
1833 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
1834                                  CORE_ADDR addr, struct objfile *objfile,
1835                                  const gdb_byte *data, const gdb_byte *end,
1836                                  unsigned int addr_size)
1837 {
1838   struct gdbarch *gdbarch = get_objfile_arch (objfile);
1839   int regno;
1840
1841   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
1842     {
1843       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_reg0);
1844       fprintf_filtered (stream, _("a variable in $%s"),
1845                         gdbarch_register_name (gdbarch, regno));
1846       data += 1;
1847     }
1848   else if (data[0] == DW_OP_regx)
1849     {
1850       ULONGEST reg;
1851
1852       data = read_uleb128 (data + 1, end, &reg);
1853       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
1854       fprintf_filtered (stream, _("a variable in $%s"),
1855                         gdbarch_register_name (gdbarch, regno));
1856     }
1857   else if (data[0] == DW_OP_fbreg)
1858     {
1859       struct block *b;
1860       struct symbol *framefunc;
1861       int frame_reg = 0;
1862       LONGEST frame_offset;
1863       const gdb_byte *base_data, *new_data;
1864       size_t base_size;
1865       LONGEST base_offset = 0;
1866
1867       new_data = read_sleb128 (data + 1, end, &frame_offset);
1868       if (!piece_end_p (new_data, end))
1869         return data;
1870       data = new_data;
1871
1872       b = block_for_pc (addr);
1873
1874       if (!b)
1875         error (_("No block found for address for symbol \"%s\"."),
1876                SYMBOL_PRINT_NAME (symbol));
1877
1878       framefunc = block_linkage_function (b);
1879
1880       if (!framefunc)
1881         error (_("No function found for block for symbol \"%s\"."),
1882                SYMBOL_PRINT_NAME (symbol));
1883
1884       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
1885
1886       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
1887         {
1888           const gdb_byte *buf_end;
1889           
1890           frame_reg = base_data[0] - DW_OP_breg0;
1891           buf_end = read_sleb128 (base_data + 1,
1892                                   base_data + base_size, &base_offset);
1893           if (buf_end != base_data + base_size)
1894             error (_("Unexpected opcode after DW_OP_breg%u for symbol \"%s\"."),
1895                    frame_reg, SYMBOL_PRINT_NAME (symbol));
1896         }
1897       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
1898         {
1899           /* The frame base is just the register, with no offset.  */
1900           frame_reg = base_data[0] - DW_OP_reg0;
1901           base_offset = 0;
1902         }
1903       else
1904         {
1905           /* We don't know what to do with the frame base expression,
1906              so we can't trace this variable; give up.  */
1907           error (_("Cannot describe location of symbol \"%s\"; "
1908                    "DWARF 2 encoding not handled, "
1909                    "first opcode in base data is 0x%x."),
1910                  SYMBOL_PRINT_NAME (symbol), base_data[0]);
1911         }
1912
1913       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, frame_reg);
1914
1915       fprintf_filtered (stream, _("a variable at frame base reg $%s offset %s+%s"),
1916                         gdbarch_register_name (gdbarch, regno),
1917                         plongest (base_offset), plongest (frame_offset));
1918     }
1919   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
1920            && piece_end_p (data, end))
1921     {
1922       LONGEST offset;
1923
1924       regno = gdbarch_dwarf2_reg_to_regnum (gdbarch, data[0] - DW_OP_breg0);
1925
1926       data = read_sleb128 (data + 1, end, &offset);
1927
1928       fprintf_filtered (stream,
1929                         _("a variable at offset %s from base reg $%s"),
1930                         plongest (offset),
1931                         gdbarch_register_name (gdbarch, regno));
1932     }
1933
1934   /* The location expression for a TLS variable looks like this (on a
1935      64-bit LE machine):
1936
1937      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
1938                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
1939      
1940      0x3 is the encoding for DW_OP_addr, which has an operand as long
1941      as the size of an address on the target machine (here is 8
1942      bytes).  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
1943      The operand represents the offset at which the variable is within
1944      the thread local storage.  */
1945
1946   else if (data + 1 + addr_size < end
1947            && data[0] == DW_OP_addr
1948            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
1949            && piece_end_p (data + 2 + addr_size, end))
1950     {
1951       CORE_ADDR offset = dwarf2_read_address (gdbarch,
1952                                               data + 1,
1953                                               end,
1954                                               addr_size);
1955
1956       fprintf_filtered (stream, 
1957                         _("a thread-local variable at offset %s "
1958                           "in the thread-local storage for `%s'"),
1959                         paddress (gdbarch, offset), objfile->name);
1960
1961       data += 1 + addr_size + 1;
1962     }
1963   else if (data[0] >= DW_OP_lit0
1964            && data[0] <= DW_OP_lit31
1965            && data + 1 < end
1966            && data[1] == DW_OP_stack_value)
1967     {
1968       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
1969       data += 2;
1970     }
1971
1972   return data;
1973 }
1974
1975 /* Disassemble an expression, stopping at the end of a piece or at the
1976    end of the expression.  Returns a pointer to the next unread byte
1977    in the input expression.  If ALL is nonzero, then this function
1978    will keep going until it reaches the end of the expression.  */
1979
1980 static const gdb_byte *
1981 disassemble_dwarf_expression (struct ui_file *stream,
1982                               struct gdbarch *arch, unsigned int addr_size,
1983                               int offset_size,
1984                               const gdb_byte *data, const gdb_byte *end,
1985                               int all)
1986 {
1987   const gdb_byte *start = data;
1988
1989   fprintf_filtered (stream, _("a complex DWARF expression:\n"));
1990
1991   while (data < end
1992          && (all
1993              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
1994     {
1995       enum dwarf_location_atom op = *data++;
1996       CORE_ADDR addr;
1997       ULONGEST ul;
1998       LONGEST l;
1999       const char *name;
2000
2001       name = dwarf_stack_op_name (op, 0);
2002
2003       if (!name)
2004         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2005                op, (long) (data - start));
2006       fprintf_filtered (stream, "  % 4ld: %s", (long) (data - start), name);
2007
2008       switch (op)
2009         {
2010         case DW_OP_addr:
2011           addr = dwarf2_read_address (arch, data, end, addr_size);
2012           data += addr_size;
2013           fprintf_filtered (stream, " %s", paddress (arch, addr));
2014           break;
2015
2016         case DW_OP_const1u:
2017           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2018           data += 1;
2019           fprintf_filtered (stream, " %s", pulongest (ul));
2020           break;
2021         case DW_OP_const1s:
2022           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2023           data += 1;
2024           fprintf_filtered (stream, " %s", plongest (l));
2025           break;
2026         case DW_OP_const2u:
2027           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2028           data += 2;
2029           fprintf_filtered (stream, " %s", pulongest (ul));
2030           break;
2031         case DW_OP_const2s:
2032           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2033           data += 2;
2034           fprintf_filtered (stream, " %s", plongest (l));
2035           break;
2036         case DW_OP_const4u:
2037           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2038           data += 4;
2039           fprintf_filtered (stream, " %s", pulongest (ul));
2040           break;
2041         case DW_OP_const4s:
2042           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2043           data += 4;
2044           fprintf_filtered (stream, " %s", plongest (l));
2045           break;
2046         case DW_OP_const8u:
2047           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2048           data += 8;
2049           fprintf_filtered (stream, " %s", pulongest (ul));
2050           break;
2051         case DW_OP_const8s:
2052           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2053           data += 8;
2054           fprintf_filtered (stream, " %s", plongest (l));
2055           break;
2056         case DW_OP_constu:
2057           data = read_uleb128 (data, end, &ul);
2058           fprintf_filtered (stream, " %s", pulongest (ul));
2059           break;
2060         case DW_OP_consts:
2061           data = read_sleb128 (data, end, &l);
2062           fprintf_filtered (stream, " %s", plongest (l));
2063           break;
2064
2065         case DW_OP_reg0:
2066         case DW_OP_reg1:
2067         case DW_OP_reg2:
2068         case DW_OP_reg3:
2069         case DW_OP_reg4:
2070         case DW_OP_reg5:
2071         case DW_OP_reg6:
2072         case DW_OP_reg7:
2073         case DW_OP_reg8:
2074         case DW_OP_reg9:
2075         case DW_OP_reg10:
2076         case DW_OP_reg11:
2077         case DW_OP_reg12:
2078         case DW_OP_reg13:
2079         case DW_OP_reg14:
2080         case DW_OP_reg15:
2081         case DW_OP_reg16:
2082         case DW_OP_reg17:
2083         case DW_OP_reg18:
2084         case DW_OP_reg19:
2085         case DW_OP_reg20:
2086         case DW_OP_reg21:
2087         case DW_OP_reg22:
2088         case DW_OP_reg23:
2089         case DW_OP_reg24:
2090         case DW_OP_reg25:
2091         case DW_OP_reg26:
2092         case DW_OP_reg27:
2093         case DW_OP_reg28:
2094         case DW_OP_reg29:
2095         case DW_OP_reg30:
2096         case DW_OP_reg31:
2097           fprintf_filtered (stream, " [$%s]",
2098                             gdbarch_register_name (arch, op - DW_OP_reg0));
2099           break;
2100
2101         case DW_OP_regx:
2102           data = read_uleb128 (data, end, &ul);
2103           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2104                             gdbarch_register_name (arch, (int) ul));
2105           break;
2106
2107         case DW_OP_implicit_value:
2108           data = read_uleb128 (data, end, &ul);
2109           data += ul;
2110           fprintf_filtered (stream, " %s", pulongest (ul));
2111           break;
2112
2113         case DW_OP_breg0:
2114         case DW_OP_breg1:
2115         case DW_OP_breg2:
2116         case DW_OP_breg3:
2117         case DW_OP_breg4:
2118         case DW_OP_breg5:
2119         case DW_OP_breg6:
2120         case DW_OP_breg7:
2121         case DW_OP_breg8:
2122         case DW_OP_breg9:
2123         case DW_OP_breg10:
2124         case DW_OP_breg11:
2125         case DW_OP_breg12:
2126         case DW_OP_breg13:
2127         case DW_OP_breg14:
2128         case DW_OP_breg15:
2129         case DW_OP_breg16:
2130         case DW_OP_breg17:
2131         case DW_OP_breg18:
2132         case DW_OP_breg19:
2133         case DW_OP_breg20:
2134         case DW_OP_breg21:
2135         case DW_OP_breg22:
2136         case DW_OP_breg23:
2137         case DW_OP_breg24:
2138         case DW_OP_breg25:
2139         case DW_OP_breg26:
2140         case DW_OP_breg27:
2141         case DW_OP_breg28:
2142         case DW_OP_breg29:
2143         case DW_OP_breg30:
2144         case DW_OP_breg31:
2145           data = read_sleb128 (data, end, &ul);
2146           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2147                             gdbarch_register_name (arch, op - DW_OP_breg0));
2148           break;
2149
2150         case DW_OP_bregx:
2151           {
2152             ULONGEST offset;
2153
2154             data = read_uleb128 (data, end, &ul);
2155             data = read_sleb128 (data, end, &offset);
2156             fprintf_filtered (stream, " register %s [$%s] offset %s",
2157                               pulongest (ul),
2158                               gdbarch_register_name (arch, (int) ul),
2159                               pulongest (offset));
2160           }
2161           break;
2162
2163         case DW_OP_fbreg:
2164           data = read_sleb128 (data, end, &ul);
2165           fprintf_filtered (stream, " %s", pulongest (ul));
2166           break;
2167
2168         case DW_OP_xderef_size:
2169         case DW_OP_deref_size:
2170         case DW_OP_pick:
2171           fprintf_filtered (stream, " %d", *data);
2172           ++data;
2173           break;
2174
2175         case DW_OP_plus_uconst:
2176           data = read_uleb128 (data, end, &ul);
2177           fprintf_filtered (stream, " %s", pulongest (ul));
2178           break;
2179
2180         case DW_OP_skip:
2181           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2182           data += 2;
2183           fprintf_filtered (stream, " to %ld",
2184                             (long) (data + l - start));
2185           break;
2186
2187         case DW_OP_bra:
2188           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2189           data += 2;
2190           fprintf_filtered (stream, " %ld",
2191                             (long) (data + l - start));
2192           break;
2193
2194         case DW_OP_call2:
2195           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2196           data += 2;
2197           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2198           break;
2199
2200         case DW_OP_call4:
2201           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2202           data += 4;
2203           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2204           break;
2205
2206         case DW_OP_call_ref:
2207           ul = extract_unsigned_integer (data, offset_size,
2208                                          gdbarch_byte_order (arch));
2209           data += offset_size;
2210           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2211           break;
2212
2213         case DW_OP_piece:
2214           data = read_uleb128 (data, end, &ul);
2215           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2216           break;
2217
2218         case DW_OP_bit_piece:
2219           {
2220             ULONGEST offset;
2221
2222             data = read_uleb128 (data, end, &ul);
2223             data = read_uleb128 (data, end, &offset);
2224             fprintf_filtered (stream, " size %s offset %s (bits)",
2225                               pulongest (ul), pulongest (offset));
2226           }
2227           break;
2228         }
2229
2230       fprintf_filtered (stream, "\n");
2231     }
2232
2233   return data;
2234 }
2235
2236 /* Describe a single location, which may in turn consist of multiple
2237    pieces.  */
2238
2239 static void
2240 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2241                              struct ui_file *stream,
2242                              const gdb_byte *data, int size,
2243                              struct objfile *objfile, unsigned int addr_size,
2244                              int offset_size)
2245 {
2246   const gdb_byte *end = data + size;
2247   int first_piece = 1, bad = 0;
2248
2249   while (data < end)
2250     {
2251       const gdb_byte *here = data;
2252       int disassemble = 1;
2253
2254       if (first_piece)
2255         first_piece = 0;
2256       else
2257         fprintf_filtered (stream, _(", and "));
2258
2259       if (!dwarf2_always_disassemble)
2260         {
2261           data = locexpr_describe_location_piece (symbol, stream, addr, objfile,
2262                                                   data, end, addr_size);
2263           /* If we printed anything, or if we have an empty piece,
2264              then don't disassemble.  */
2265           if (data != here
2266               || data[0] == DW_OP_piece
2267               || data[0] == DW_OP_bit_piece)
2268             disassemble = 0;
2269         }
2270       if (disassemble)
2271         data = disassemble_dwarf_expression (stream, get_objfile_arch (objfile),
2272                                              addr_size, offset_size, data, end,
2273                                              dwarf2_always_disassemble);
2274
2275       if (data < end)
2276         {
2277           int empty = data == here;
2278               
2279           if (disassemble)
2280             fprintf_filtered (stream, "   ");
2281           if (data[0] == DW_OP_piece)
2282             {
2283               ULONGEST bytes;
2284
2285               data = read_uleb128 (data + 1, end, &bytes);
2286
2287               if (empty)
2288                 fprintf_filtered (stream, _("an empty %s-byte piece"),
2289                                   pulongest (bytes));
2290               else
2291                 fprintf_filtered (stream, _(" [%s-byte piece]"),
2292                                   pulongest (bytes));
2293             }
2294           else if (data[0] == DW_OP_bit_piece)
2295             {
2296               ULONGEST bits, offset;
2297
2298               data = read_uleb128 (data + 1, end, &bits);
2299               data = read_uleb128 (data, end, &offset);
2300
2301               if (empty)
2302                 fprintf_filtered (stream,
2303                                   _("an empty %s-bit piece"),
2304                                   pulongest (bits));
2305               else
2306                 fprintf_filtered (stream,
2307                                   _(" [%s-bit piece, offset %s bits]"),
2308                                   pulongest (bits), pulongest (offset));
2309             }
2310           else
2311             {
2312               bad = 1;
2313               break;
2314             }
2315         }
2316     }
2317
2318   if (bad || data > end)
2319     error (_("Corrupted DWARF2 expression for \"%s\"."),
2320            SYMBOL_PRINT_NAME (symbol));
2321 }
2322
2323 /* Print a natural-language description of SYMBOL to STREAM.  This
2324    version is for a symbol with a single location.  */
2325
2326 static void
2327 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2328                            struct ui_file *stream)
2329 {
2330   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2331   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2332   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2333   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2334
2335   locexpr_describe_location_1 (symbol, addr, stream, dlbaton->data, dlbaton->size,
2336                                objfile, addr_size, offset_size);
2337 }
2338
2339 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2340    any necessary bytecode in AX.  */
2341
2342 static void
2343 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2344                             struct agent_expr *ax, struct axs_value *value)
2345 {
2346   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2347   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2348
2349   compile_dwarf_to_ax (ax, value, gdbarch, addr_size,
2350                        dlbaton->data, dlbaton->data + dlbaton->size,
2351                        dlbaton->per_cu);
2352 }
2353
2354 /* The set of location functions used with the DWARF-2 expression
2355    evaluator.  */
2356 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2357   locexpr_read_variable,
2358   locexpr_read_needs_frame,
2359   locexpr_describe_location,
2360   locexpr_tracepoint_var_ref
2361 };
2362
2363
2364 /* Wrapper functions for location lists.  These generally find
2365    the appropriate location expression and call something above.  */
2366
2367 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2368    evaluator to calculate the location.  */
2369 static struct value *
2370 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2371 {
2372   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2373   struct value *val;
2374   const gdb_byte *data;
2375   size_t size;
2376
2377   data = find_location_expression (dlbaton, &size,
2378                                    frame ? get_frame_address_in_block (frame)
2379                                    : 0);
2380   if (data == NULL)
2381     {
2382       val = allocate_value (SYMBOL_TYPE (symbol));
2383       VALUE_LVAL (val) = not_lval;
2384       set_value_optimized_out (val, 1);
2385     }
2386   else
2387     val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2388                                     dlbaton->per_cu);
2389
2390   return val;
2391 }
2392
2393 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
2394 static int
2395 loclist_read_needs_frame (struct symbol *symbol)
2396 {
2397   /* If there's a location list, then assume we need to have a frame
2398      to choose the appropriate location expression.  With tracking of
2399      global variables this is not necessarily true, but such tracking
2400      is disabled in GCC at the moment until we figure out how to
2401      represent it.  */
2402
2403   return 1;
2404 }
2405
2406 /* Print a natural-language description of SYMBOL to STREAM.  This
2407    version applies when there is a list of different locations, each
2408    with a specified address range.  */
2409
2410 static void
2411 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2412                            struct ui_file *stream)
2413 {
2414   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2415   CORE_ADDR low, high;
2416   const gdb_byte *loc_ptr, *buf_end;
2417   int length, first = 1;
2418   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2419   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2420   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2421   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2422   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2423   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2424   /* Adjust base_address for relocatable objects.  */
2425   CORE_ADDR base_offset = ANOFFSET (objfile->section_offsets,
2426                                     SECT_OFF_TEXT (objfile));
2427   CORE_ADDR base_address = dlbaton->base_address + base_offset;
2428
2429   loc_ptr = dlbaton->data;
2430   buf_end = dlbaton->data + dlbaton->size;
2431
2432   fprintf_filtered (stream, _("multi-location:\n"));
2433
2434   /* Iterate through locations until we run out.  */
2435   while (1)
2436     {
2437       if (buf_end - loc_ptr < 2 * addr_size)
2438         error (_("Corrupted DWARF expression for symbol \"%s\"."),
2439                SYMBOL_PRINT_NAME (symbol));
2440
2441       low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2442       loc_ptr += addr_size;
2443
2444       /* A base-address-selection entry.  */
2445       if (low == base_mask)
2446         {
2447           base_address = dwarf2_read_address (gdbarch,
2448                                               loc_ptr, buf_end, addr_size);
2449           fprintf_filtered (stream, _("  Base address %s"),
2450                             paddress (gdbarch, base_address));
2451           loc_ptr += addr_size;
2452           continue;
2453         }
2454
2455       high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2456       loc_ptr += addr_size;
2457
2458       /* An end-of-list entry.  */
2459       if (low == 0 && high == 0)
2460         break;
2461
2462       /* Otherwise, a location expression entry.  */
2463       low += base_address;
2464       high += base_address;
2465
2466       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2467       loc_ptr += 2;
2468
2469       /* (It would improve readability to print only the minimum
2470          necessary digits of the second number of the range.)  */
2471       fprintf_filtered (stream, _("  Range %s-%s: "),
2472                         paddress (gdbarch, low), paddress (gdbarch, high));
2473
2474       /* Now describe this particular location.  */
2475       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2476                                    objfile, addr_size, offset_size);
2477
2478       fprintf_filtered (stream, "\n");
2479
2480       loc_ptr += length;
2481     }
2482 }
2483
2484 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2485    any necessary bytecode in AX.  */
2486 static void
2487 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2488                             struct agent_expr *ax, struct axs_value *value)
2489 {
2490   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2491   const gdb_byte *data;
2492   size_t size;
2493   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2494
2495   data = find_location_expression (dlbaton, &size, ax->scope);
2496
2497   compile_dwarf_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2498                        dlbaton->per_cu);
2499 }
2500
2501 /* The set of location functions used with the DWARF-2 expression
2502    evaluator and location lists.  */
2503 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2504   loclist_read_variable,
2505   loclist_read_needs_frame,
2506   loclist_describe_location,
2507   loclist_tracepoint_var_ref
2508 };