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