gdb/
[external/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->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   struct value *result;
968   LONGEST byte_offset;
969
970   type = value_type (value);
971   if (TYPE_CODE (type) != TYPE_CODE_PTR)
972     return NULL;
973
974   bit_length = 8 * TYPE_LENGTH (type);
975   bit_offset = 8 * value_offset (value);
976   if (value_bitsize (value))
977     bit_offset += value_bitpos (value);
978
979   for (i = 0; i < c->n_pieces && bit_length > 0; i++)
980     {
981       struct dwarf_expr_piece *p = &c->pieces[i];
982       size_t this_size_bits = p->size;
983
984       if (bit_offset > 0)
985         {
986           if (bit_offset >= this_size_bits)
987             {
988               bit_offset -= this_size_bits;
989               continue;
990             }
991
992           bit_length -= this_size_bits - bit_offset;
993           bit_offset = 0;
994         }
995       else
996         bit_length -= this_size_bits;
997
998       if (p->location != DWARF_VALUE_IMPLICIT_POINTER)
999         return NULL;
1000
1001       if (bit_length != 0)
1002         error (_("Invalid use of DW_OP_GNU_implicit_pointer"));
1003
1004       piece = p;
1005       break;
1006     }
1007
1008   frame = get_selected_frame (_("No frame selected."));
1009   byte_offset = value_as_address (value);
1010
1011   gdb_assert (piece);
1012   baton = dwarf2_fetch_die_location_block (piece->v.ptr.die, c->per_cu,
1013                                            get_frame_address_in_block_wrapper,
1014                                            frame);
1015
1016   result = dwarf2_evaluate_loc_desc_full (TYPE_TARGET_TYPE (type), frame,
1017                                           baton.data, baton.size, baton.per_cu,
1018                                           byte_offset);
1019
1020   return result;
1021 }
1022
1023 static void *
1024 copy_pieced_value_closure (const struct value *v)
1025 {
1026   struct piece_closure *c
1027     = (struct piece_closure *) value_computed_closure (v);
1028   
1029   ++c->refc;
1030   return c;
1031 }
1032
1033 static void
1034 free_pieced_value_closure (struct value *v)
1035 {
1036   struct piece_closure *c
1037     = (struct piece_closure *) value_computed_closure (v);
1038
1039   --c->refc;
1040   if (c->refc == 0)
1041     {
1042       int i;
1043
1044       for (i = 0; i < c->n_pieces; ++i)
1045         if (c->pieces[i].location == DWARF_VALUE_STACK)
1046           value_free (c->pieces[i].v.value);
1047
1048       xfree (c->pieces);
1049       xfree (c);
1050     }
1051 }
1052
1053 /* Functions for accessing a variable described by DW_OP_piece.  */
1054 static const struct lval_funcs pieced_value_funcs = {
1055   read_pieced_value,
1056   write_pieced_value,
1057   check_pieced_value_validity,
1058   check_pieced_value_invalid,
1059   indirect_pieced_value,
1060   check_pieced_synthetic_pointer,
1061   copy_pieced_value_closure,
1062   free_pieced_value_closure
1063 };
1064
1065 /* Helper function which throws an error if a synthetic pointer is
1066    invalid.  */
1067
1068 static void
1069 invalid_synthetic_pointer (void)
1070 {
1071   error (_("access outside bounds of object "
1072            "referenced via synthetic pointer"));
1073 }
1074
1075 /* Evaluate a location description, starting at DATA and with length
1076    SIZE, to find the current location of variable of TYPE in the
1077    context of FRAME.  BYTE_OFFSET is applied after the contents are
1078    computed.  */
1079
1080 static struct value *
1081 dwarf2_evaluate_loc_desc_full (struct type *type, struct frame_info *frame,
1082                                const gdb_byte *data, unsigned short size,
1083                                struct dwarf2_per_cu_data *per_cu,
1084                                LONGEST byte_offset)
1085 {
1086   struct value *retval;
1087   struct dwarf_expr_baton baton;
1088   struct dwarf_expr_context *ctx;
1089   struct cleanup *old_chain, *value_chain;
1090   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1091   volatile struct gdb_exception ex;
1092
1093   if (byte_offset < 0)
1094     invalid_synthetic_pointer ();
1095
1096   if (size == 0)
1097     return allocate_optimized_out_value (type);
1098
1099   baton.frame = frame;
1100   baton.per_cu = per_cu;
1101
1102   ctx = new_dwarf_expr_context ();
1103   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1104   value_chain = make_cleanup_value_free_to_mark (value_mark ());
1105
1106   ctx->gdbarch = get_objfile_arch (objfile);
1107   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1108   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1109   ctx->baton = &baton;
1110   ctx->read_reg = dwarf_expr_read_reg;
1111   ctx->read_mem = dwarf_expr_read_mem;
1112   ctx->get_frame_base = dwarf_expr_frame_base;
1113   ctx->get_frame_cfa = dwarf_expr_frame_cfa;
1114   ctx->get_frame_pc = dwarf_expr_frame_pc;
1115   ctx->get_tls_address = dwarf_expr_tls_address;
1116   ctx->dwarf_call = dwarf_expr_dwarf_call;
1117   ctx->get_base_type = dwarf_expr_get_base_type;
1118
1119   TRY_CATCH (ex, RETURN_MASK_ERROR)
1120     {
1121       dwarf_expr_eval (ctx, data, size);
1122     }
1123   if (ex.reason < 0)
1124     {
1125       if (ex.error == NOT_AVAILABLE_ERROR)
1126         {
1127           do_cleanups (old_chain);
1128           retval = allocate_value (type);
1129           mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (type));
1130           return retval;
1131         }
1132       else
1133         throw_exception (ex);
1134     }
1135
1136   if (ctx->num_pieces > 0)
1137     {
1138       struct piece_closure *c;
1139       struct frame_id frame_id = get_frame_id (frame);
1140       ULONGEST bit_size = 0;
1141       int i;
1142
1143       for (i = 0; i < ctx->num_pieces; ++i)
1144         bit_size += ctx->pieces[i].size;
1145       if (8 * (byte_offset + TYPE_LENGTH (type)) > bit_size)
1146         invalid_synthetic_pointer ();
1147
1148       c = allocate_piece_closure (per_cu, ctx->num_pieces, ctx->pieces,
1149                                   ctx->addr_size);
1150       /* We must clean up the value chain after creating the piece
1151          closure but before allocating the result.  */
1152       do_cleanups (value_chain);
1153       retval = allocate_computed_value (type, &pieced_value_funcs, c);
1154       VALUE_FRAME_ID (retval) = frame_id;
1155       set_value_offset (retval, byte_offset);
1156     }
1157   else
1158     {
1159       switch (ctx->location)
1160         {
1161         case DWARF_VALUE_REGISTER:
1162           {
1163             struct gdbarch *arch = get_frame_arch (frame);
1164             ULONGEST dwarf_regnum = value_as_long (dwarf_expr_fetch (ctx, 0));
1165             int gdb_regnum = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_regnum);
1166
1167             if (byte_offset != 0)
1168               error (_("cannot use offset on synthetic pointer to register"));
1169             do_cleanups (value_chain);
1170             if (gdb_regnum != -1)
1171               retval = value_from_register (type, gdb_regnum, frame);
1172             else
1173               error (_("Unable to access DWARF register number %s"),
1174                      paddress (arch, dwarf_regnum));
1175           }
1176           break;
1177
1178         case DWARF_VALUE_MEMORY:
1179           {
1180             CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
1181             int in_stack_memory = dwarf_expr_fetch_in_stack_memory (ctx, 0);
1182
1183             do_cleanups (value_chain);
1184             retval = allocate_value_lazy (type);
1185             VALUE_LVAL (retval) = lval_memory;
1186             if (in_stack_memory)
1187               set_value_stack (retval, 1);
1188             set_value_address (retval, address + byte_offset);
1189           }
1190           break;
1191
1192         case DWARF_VALUE_STACK:
1193           {
1194             struct value *value = dwarf_expr_fetch (ctx, 0);
1195             gdb_byte *contents;
1196             const gdb_byte *val_bytes;
1197             size_t n = TYPE_LENGTH (value_type (value));
1198
1199             if (byte_offset + TYPE_LENGTH (type) > n)
1200               invalid_synthetic_pointer ();
1201
1202             val_bytes = value_contents_all (value);
1203             val_bytes += byte_offset;
1204             n -= byte_offset;
1205
1206             /* Preserve VALUE because we are going to free values back
1207                to the mark, but we still need the value contents
1208                below.  */
1209             value_incref (value);
1210             do_cleanups (value_chain);
1211             make_cleanup_value_free (value);
1212
1213             retval = allocate_value (type);
1214             contents = value_contents_raw (retval);
1215             if (n > TYPE_LENGTH (type))
1216               n = TYPE_LENGTH (type);
1217             memcpy (contents, val_bytes, n);
1218           }
1219           break;
1220
1221         case DWARF_VALUE_LITERAL:
1222           {
1223             bfd_byte *contents;
1224             const bfd_byte *ldata;
1225             size_t n = ctx->len;
1226
1227             if (byte_offset + TYPE_LENGTH (type) > n)
1228               invalid_synthetic_pointer ();
1229
1230             do_cleanups (value_chain);
1231             retval = allocate_value (type);
1232             contents = value_contents_raw (retval);
1233
1234             ldata = ctx->data + byte_offset;
1235             n -= byte_offset;
1236
1237             if (n > TYPE_LENGTH (type))
1238               n = TYPE_LENGTH (type);
1239             memcpy (contents, ldata, n);
1240           }
1241           break;
1242
1243         case DWARF_VALUE_OPTIMIZED_OUT:
1244           do_cleanups (value_chain);
1245           retval = allocate_optimized_out_value (type);
1246           break;
1247
1248           /* DWARF_VALUE_IMPLICIT_POINTER was converted to a pieced
1249              operation by execute_stack_op.  */
1250         case DWARF_VALUE_IMPLICIT_POINTER:
1251           /* DWARF_VALUE_OPTIMIZED_OUT can't occur in this context --
1252              it can only be encountered when making a piece.  */
1253         default:
1254           internal_error (__FILE__, __LINE__, _("invalid location type"));
1255         }
1256     }
1257
1258   set_value_initialized (retval, ctx->initialized);
1259
1260   do_cleanups (old_chain);
1261
1262   return retval;
1263 }
1264
1265 /* The exported interface to dwarf2_evaluate_loc_desc_full; it always
1266    passes 0 as the byte_offset.  */
1267
1268 struct value *
1269 dwarf2_evaluate_loc_desc (struct type *type, struct frame_info *frame,
1270                           const gdb_byte *data, unsigned short size,
1271                           struct dwarf2_per_cu_data *per_cu)
1272 {
1273   return dwarf2_evaluate_loc_desc_full (type, frame, data, size, per_cu, 0);
1274 }
1275
1276 \f
1277 /* Helper functions and baton for dwarf2_loc_desc_needs_frame.  */
1278
1279 struct needs_frame_baton
1280 {
1281   int needs_frame;
1282   struct dwarf2_per_cu_data *per_cu;
1283 };
1284
1285 /* Reads from registers do require a frame.  */
1286 static CORE_ADDR
1287 needs_frame_read_reg (void *baton, int regnum)
1288 {
1289   struct needs_frame_baton *nf_baton = baton;
1290
1291   nf_baton->needs_frame = 1;
1292   return 1;
1293 }
1294
1295 /* Reads from memory do not require a frame.  */
1296 static void
1297 needs_frame_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
1298 {
1299   memset (buf, 0, len);
1300 }
1301
1302 /* Frame-relative accesses do require a frame.  */
1303 static void
1304 needs_frame_frame_base (void *baton, const gdb_byte **start, size_t * length)
1305 {
1306   static gdb_byte lit0 = DW_OP_lit0;
1307   struct needs_frame_baton *nf_baton = baton;
1308
1309   *start = &lit0;
1310   *length = 1;
1311
1312   nf_baton->needs_frame = 1;
1313 }
1314
1315 /* CFA accesses require a frame.  */
1316
1317 static CORE_ADDR
1318 needs_frame_frame_cfa (void *baton)
1319 {
1320   struct needs_frame_baton *nf_baton = baton;
1321
1322   nf_baton->needs_frame = 1;
1323   return 1;
1324 }
1325
1326 /* Thread-local accesses do require a frame.  */
1327 static CORE_ADDR
1328 needs_frame_tls_address (void *baton, CORE_ADDR offset)
1329 {
1330   struct needs_frame_baton *nf_baton = baton;
1331
1332   nf_baton->needs_frame = 1;
1333   return 1;
1334 }
1335
1336 /* Helper interface of per_cu_dwarf_call for dwarf2_loc_desc_needs_frame.  */
1337
1338 static void
1339 needs_frame_dwarf_call (struct dwarf_expr_context *ctx, size_t die_offset)
1340 {
1341   struct needs_frame_baton *nf_baton = ctx->baton;
1342
1343   per_cu_dwarf_call (ctx, die_offset, nf_baton->per_cu,
1344                      ctx->get_frame_pc, ctx->baton);
1345 }
1346
1347 /* Return non-zero iff the location expression at DATA (length SIZE)
1348    requires a frame to evaluate.  */
1349
1350 static int
1351 dwarf2_loc_desc_needs_frame (const gdb_byte *data, unsigned short size,
1352                              struct dwarf2_per_cu_data *per_cu)
1353 {
1354   struct needs_frame_baton baton;
1355   struct dwarf_expr_context *ctx;
1356   int in_reg;
1357   struct cleanup *old_chain;
1358   struct objfile *objfile = dwarf2_per_cu_objfile (per_cu);
1359
1360   baton.needs_frame = 0;
1361   baton.per_cu = per_cu;
1362
1363   ctx = new_dwarf_expr_context ();
1364   old_chain = make_cleanup_free_dwarf_expr_context (ctx);
1365   make_cleanup_value_free_to_mark (value_mark ());
1366
1367   ctx->gdbarch = get_objfile_arch (objfile);
1368   ctx->addr_size = dwarf2_per_cu_addr_size (per_cu);
1369   ctx->offset = dwarf2_per_cu_text_offset (per_cu);
1370   ctx->baton = &baton;
1371   ctx->read_reg = needs_frame_read_reg;
1372   ctx->read_mem = needs_frame_read_mem;
1373   ctx->get_frame_base = needs_frame_frame_base;
1374   ctx->get_frame_cfa = needs_frame_frame_cfa;
1375   ctx->get_frame_pc = needs_frame_frame_cfa;
1376   ctx->get_tls_address = needs_frame_tls_address;
1377   ctx->dwarf_call = needs_frame_dwarf_call;
1378
1379   dwarf_expr_eval (ctx, data, size);
1380
1381   in_reg = ctx->location == DWARF_VALUE_REGISTER;
1382
1383   if (ctx->num_pieces > 0)
1384     {
1385       int i;
1386
1387       /* If the location has several pieces, and any of them are in
1388          registers, then we will need a frame to fetch them from.  */
1389       for (i = 0; i < ctx->num_pieces; i++)
1390         if (ctx->pieces[i].location == DWARF_VALUE_REGISTER)
1391           in_reg = 1;
1392     }
1393
1394   do_cleanups (old_chain);
1395
1396   return baton.needs_frame || in_reg;
1397 }
1398
1399 /* A helper function that throws an unimplemented error mentioning a
1400    given DWARF operator.  */
1401
1402 static void
1403 unimplemented (unsigned int op)
1404 {
1405   const char *name = dwarf_stack_op_name (op);
1406
1407   if (name)
1408     error (_("DWARF operator %s cannot be translated to an agent expression"),
1409            name);
1410   else
1411     error (_("Unknown DWARF operator 0x%02x cannot be translated "
1412              "to an agent expression"),
1413            op);
1414 }
1415
1416 /* A helper function to convert a DWARF register to an arch register.
1417    ARCH is the architecture.
1418    DWARF_REG is the register.
1419    This will throw an exception if the DWARF register cannot be
1420    translated to an architecture register.  */
1421
1422 static int
1423 translate_register (struct gdbarch *arch, int dwarf_reg)
1424 {
1425   int reg = gdbarch_dwarf2_reg_to_regnum (arch, dwarf_reg);
1426   if (reg == -1)
1427     error (_("Unable to access DWARF register number %d"), dwarf_reg);
1428   return reg;
1429 }
1430
1431 /* A helper function that emits an access to memory.  ARCH is the
1432    target architecture.  EXPR is the expression which we are building.
1433    NBITS is the number of bits we want to read.  This emits the
1434    opcodes needed to read the memory and then extract the desired
1435    bits.  */
1436
1437 static void
1438 access_memory (struct gdbarch *arch, struct agent_expr *expr, ULONGEST nbits)
1439 {
1440   ULONGEST nbytes = (nbits + 7) / 8;
1441
1442   gdb_assert (nbits > 0 && nbits <= sizeof (LONGEST));
1443
1444   if (trace_kludge)
1445     ax_trace_quick (expr, nbytes);
1446
1447   if (nbits <= 8)
1448     ax_simple (expr, aop_ref8);
1449   else if (nbits <= 16)
1450     ax_simple (expr, aop_ref16);
1451   else if (nbits <= 32)
1452     ax_simple (expr, aop_ref32);
1453   else
1454     ax_simple (expr, aop_ref64);
1455
1456   /* If we read exactly the number of bytes we wanted, we're done.  */
1457   if (8 * nbytes == nbits)
1458     return;
1459
1460   if (gdbarch_bits_big_endian (arch))
1461     {
1462       /* On a bits-big-endian machine, we want the high-order
1463          NBITS.  */
1464       ax_const_l (expr, 8 * nbytes - nbits);
1465       ax_simple (expr, aop_rsh_unsigned);
1466     }
1467   else
1468     {
1469       /* On a bits-little-endian box, we want the low-order NBITS.  */
1470       ax_zero_ext (expr, nbits);
1471     }
1472 }
1473
1474 /* A helper function to return the frame's PC.  */
1475
1476 static CORE_ADDR
1477 get_ax_pc (void *baton)
1478 {
1479   struct agent_expr *expr = baton;
1480
1481   return expr->scope;
1482 }
1483
1484 /* Compile a DWARF location expression to an agent expression.
1485    
1486    EXPR is the agent expression we are building.
1487    LOC is the agent value we modify.
1488    ARCH is the architecture.
1489    ADDR_SIZE is the size of addresses, in bytes.
1490    OP_PTR is the start of the location expression.
1491    OP_END is one past the last byte of the location expression.
1492    
1493    This will throw an exception for various kinds of errors -- for
1494    example, if the expression cannot be compiled, or if the expression
1495    is invalid.  */
1496
1497 void
1498 dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
1499                            struct gdbarch *arch, unsigned int addr_size,
1500                            const gdb_byte *op_ptr, const gdb_byte *op_end,
1501                            struct dwarf2_per_cu_data *per_cu)
1502 {
1503   struct cleanup *cleanups;
1504   int i, *offsets;
1505   VEC(int) *dw_labels = NULL, *patches = NULL;
1506   const gdb_byte * const base = op_ptr;
1507   const gdb_byte *previous_piece = op_ptr;
1508   enum bfd_endian byte_order = gdbarch_byte_order (arch);
1509   ULONGEST bits_collected = 0;
1510   unsigned int addr_size_bits = 8 * addr_size;
1511   int bits_big_endian = gdbarch_bits_big_endian (arch);
1512
1513   offsets = xmalloc ((op_end - op_ptr) * sizeof (int));
1514   cleanups = make_cleanup (xfree, offsets);
1515
1516   for (i = 0; i < op_end - op_ptr; ++i)
1517     offsets[i] = -1;
1518
1519   make_cleanup (VEC_cleanup (int), &dw_labels);
1520   make_cleanup (VEC_cleanup (int), &patches);
1521
1522   /* By default we are making an address.  */
1523   loc->kind = axs_lvalue_memory;
1524
1525   while (op_ptr < op_end)
1526     {
1527       enum dwarf_location_atom op = *op_ptr;
1528       ULONGEST uoffset, reg;
1529       LONGEST offset;
1530       int i;
1531
1532       offsets[op_ptr - base] = expr->len;
1533       ++op_ptr;
1534
1535       /* Our basic approach to code generation is to map DWARF
1536          operations directly to AX operations.  However, there are
1537          some differences.
1538
1539          First, DWARF works on address-sized units, but AX always uses
1540          LONGEST.  For most operations we simply ignore this
1541          difference; instead we generate sign extensions as needed
1542          before division and comparison operations.  It would be nice
1543          to omit the sign extensions, but there is no way to determine
1544          the size of the target's LONGEST.  (This code uses the size
1545          of the host LONGEST in some cases -- that is a bug but it is
1546          difficult to fix.)
1547
1548          Second, some DWARF operations cannot be translated to AX.
1549          For these we simply fail.  See
1550          http://sourceware.org/bugzilla/show_bug.cgi?id=11662.  */
1551       switch (op)
1552         {
1553         case DW_OP_lit0:
1554         case DW_OP_lit1:
1555         case DW_OP_lit2:
1556         case DW_OP_lit3:
1557         case DW_OP_lit4:
1558         case DW_OP_lit5:
1559         case DW_OP_lit6:
1560         case DW_OP_lit7:
1561         case DW_OP_lit8:
1562         case DW_OP_lit9:
1563         case DW_OP_lit10:
1564         case DW_OP_lit11:
1565         case DW_OP_lit12:
1566         case DW_OP_lit13:
1567         case DW_OP_lit14:
1568         case DW_OP_lit15:
1569         case DW_OP_lit16:
1570         case DW_OP_lit17:
1571         case DW_OP_lit18:
1572         case DW_OP_lit19:
1573         case DW_OP_lit20:
1574         case DW_OP_lit21:
1575         case DW_OP_lit22:
1576         case DW_OP_lit23:
1577         case DW_OP_lit24:
1578         case DW_OP_lit25:
1579         case DW_OP_lit26:
1580         case DW_OP_lit27:
1581         case DW_OP_lit28:
1582         case DW_OP_lit29:
1583         case DW_OP_lit30:
1584         case DW_OP_lit31:
1585           ax_const_l (expr, op - DW_OP_lit0);
1586           break;
1587
1588         case DW_OP_addr:
1589           uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
1590           op_ptr += addr_size;
1591           /* Some versions of GCC emit DW_OP_addr before
1592              DW_OP_GNU_push_tls_address.  In this case the value is an
1593              index, not an address.  We don't support things like
1594              branching between the address and the TLS op.  */
1595           if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
1596             uoffset += dwarf2_per_cu_text_offset (per_cu);
1597           ax_const_l (expr, uoffset);
1598           break;
1599
1600         case DW_OP_const1u:
1601           ax_const_l (expr, extract_unsigned_integer (op_ptr, 1, byte_order));
1602           op_ptr += 1;
1603           break;
1604         case DW_OP_const1s:
1605           ax_const_l (expr, extract_signed_integer (op_ptr, 1, byte_order));
1606           op_ptr += 1;
1607           break;
1608         case DW_OP_const2u:
1609           ax_const_l (expr, extract_unsigned_integer (op_ptr, 2, byte_order));
1610           op_ptr += 2;
1611           break;
1612         case DW_OP_const2s:
1613           ax_const_l (expr, extract_signed_integer (op_ptr, 2, byte_order));
1614           op_ptr += 2;
1615           break;
1616         case DW_OP_const4u:
1617           ax_const_l (expr, extract_unsigned_integer (op_ptr, 4, byte_order));
1618           op_ptr += 4;
1619           break;
1620         case DW_OP_const4s:
1621           ax_const_l (expr, extract_signed_integer (op_ptr, 4, byte_order));
1622           op_ptr += 4;
1623           break;
1624         case DW_OP_const8u:
1625           ax_const_l (expr, extract_unsigned_integer (op_ptr, 8, byte_order));
1626           op_ptr += 8;
1627           break;
1628         case DW_OP_const8s:
1629           ax_const_l (expr, extract_signed_integer (op_ptr, 8, byte_order));
1630           op_ptr += 8;
1631           break;
1632         case DW_OP_constu:
1633           op_ptr = read_uleb128 (op_ptr, op_end, &uoffset);
1634           ax_const_l (expr, uoffset);
1635           break;
1636         case DW_OP_consts:
1637           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1638           ax_const_l (expr, offset);
1639           break;
1640
1641         case DW_OP_reg0:
1642         case DW_OP_reg1:
1643         case DW_OP_reg2:
1644         case DW_OP_reg3:
1645         case DW_OP_reg4:
1646         case DW_OP_reg5:
1647         case DW_OP_reg6:
1648         case DW_OP_reg7:
1649         case DW_OP_reg8:
1650         case DW_OP_reg9:
1651         case DW_OP_reg10:
1652         case DW_OP_reg11:
1653         case DW_OP_reg12:
1654         case DW_OP_reg13:
1655         case DW_OP_reg14:
1656         case DW_OP_reg15:
1657         case DW_OP_reg16:
1658         case DW_OP_reg17:
1659         case DW_OP_reg18:
1660         case DW_OP_reg19:
1661         case DW_OP_reg20:
1662         case DW_OP_reg21:
1663         case DW_OP_reg22:
1664         case DW_OP_reg23:
1665         case DW_OP_reg24:
1666         case DW_OP_reg25:
1667         case DW_OP_reg26:
1668         case DW_OP_reg27:
1669         case DW_OP_reg28:
1670         case DW_OP_reg29:
1671         case DW_OP_reg30:
1672         case DW_OP_reg31:
1673           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1674           loc->u.reg = translate_register (arch, op - DW_OP_reg0);
1675           loc->kind = axs_lvalue_register;
1676           break;
1677
1678         case DW_OP_regx:
1679           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1680           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
1681           loc->u.reg = translate_register (arch, reg);
1682           loc->kind = axs_lvalue_register;
1683           break;
1684
1685         case DW_OP_implicit_value:
1686           {
1687             ULONGEST len;
1688
1689             op_ptr = read_uleb128 (op_ptr, op_end, &len);
1690             if (op_ptr + len > op_end)
1691               error (_("DW_OP_implicit_value: too few bytes available."));
1692             if (len > sizeof (ULONGEST))
1693               error (_("Cannot translate DW_OP_implicit_value of %d bytes"),
1694                      (int) len);
1695
1696             ax_const_l (expr, extract_unsigned_integer (op_ptr, len,
1697                                                         byte_order));
1698             op_ptr += len;
1699             dwarf_expr_require_composition (op_ptr, op_end,
1700                                             "DW_OP_implicit_value");
1701
1702             loc->kind = axs_rvalue;
1703           }
1704           break;
1705
1706         case DW_OP_stack_value:
1707           dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_stack_value");
1708           loc->kind = axs_rvalue;
1709           break;
1710
1711         case DW_OP_breg0:
1712         case DW_OP_breg1:
1713         case DW_OP_breg2:
1714         case DW_OP_breg3:
1715         case DW_OP_breg4:
1716         case DW_OP_breg5:
1717         case DW_OP_breg6:
1718         case DW_OP_breg7:
1719         case DW_OP_breg8:
1720         case DW_OP_breg9:
1721         case DW_OP_breg10:
1722         case DW_OP_breg11:
1723         case DW_OP_breg12:
1724         case DW_OP_breg13:
1725         case DW_OP_breg14:
1726         case DW_OP_breg15:
1727         case DW_OP_breg16:
1728         case DW_OP_breg17:
1729         case DW_OP_breg18:
1730         case DW_OP_breg19:
1731         case DW_OP_breg20:
1732         case DW_OP_breg21:
1733         case DW_OP_breg22:
1734         case DW_OP_breg23:
1735         case DW_OP_breg24:
1736         case DW_OP_breg25:
1737         case DW_OP_breg26:
1738         case DW_OP_breg27:
1739         case DW_OP_breg28:
1740         case DW_OP_breg29:
1741         case DW_OP_breg30:
1742         case DW_OP_breg31:
1743           op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1744           i = translate_register (arch, op - DW_OP_breg0);
1745           ax_reg (expr, i);
1746           if (offset != 0)
1747             {
1748               ax_const_l (expr, offset);
1749               ax_simple (expr, aop_add);
1750             }
1751           break;
1752         case DW_OP_bregx:
1753           {
1754             op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1755             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1756             i = translate_register (arch, reg);
1757             ax_reg (expr, i);
1758             if (offset != 0)
1759               {
1760                 ax_const_l (expr, offset);
1761                 ax_simple (expr, aop_add);
1762               }
1763           }
1764           break;
1765         case DW_OP_fbreg:
1766           {
1767             const gdb_byte *datastart;
1768             size_t datalen;
1769             unsigned int before_stack_len;
1770             struct block *b;
1771             struct symbol *framefunc;
1772             LONGEST base_offset = 0;
1773
1774             b = block_for_pc (expr->scope);
1775
1776             if (!b)
1777               error (_("No block found for address"));
1778
1779             framefunc = block_linkage_function (b);
1780
1781             if (!framefunc)
1782               error (_("No function found for block"));
1783
1784             dwarf_expr_frame_base_1 (framefunc, expr->scope,
1785                                      &datastart, &datalen);
1786
1787             op_ptr = read_sleb128 (op_ptr, op_end, &offset);
1788             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size, datastart,
1789                                        datastart + datalen, per_cu);
1790
1791             if (offset != 0)
1792               {
1793                 ax_const_l (expr, offset);
1794                 ax_simple (expr, aop_add);
1795               }
1796
1797             loc->kind = axs_lvalue_memory;
1798           }
1799           break;
1800
1801         case DW_OP_dup:
1802           ax_simple (expr, aop_dup);
1803           break;
1804
1805         case DW_OP_drop:
1806           ax_simple (expr, aop_pop);
1807           break;
1808
1809         case DW_OP_pick:
1810           offset = *op_ptr++;
1811           ax_pick (expr, offset);
1812           break;
1813           
1814         case DW_OP_swap:
1815           ax_simple (expr, aop_swap);
1816           break;
1817
1818         case DW_OP_over:
1819           ax_pick (expr, 1);
1820           break;
1821
1822         case DW_OP_rot:
1823           ax_simple (expr, aop_rot);
1824           break;
1825
1826         case DW_OP_deref:
1827         case DW_OP_deref_size:
1828           {
1829             int size;
1830
1831             if (op == DW_OP_deref_size)
1832               size = *op_ptr++;
1833             else
1834               size = addr_size;
1835
1836             switch (size)
1837               {
1838               case 8:
1839                 ax_simple (expr, aop_ref8);
1840                 break;
1841               case 16:
1842                 ax_simple (expr, aop_ref16);
1843                 break;
1844               case 32:
1845                 ax_simple (expr, aop_ref32);
1846                 break;
1847               case 64:
1848                 ax_simple (expr, aop_ref64);
1849                 break;
1850               default:
1851                 /* Note that dwarf_stack_op_name will never return
1852                    NULL here.  */
1853                 error (_("Unsupported size %d in %s"),
1854                        size, dwarf_stack_op_name (op));
1855               }
1856           }
1857           break;
1858
1859         case DW_OP_abs:
1860           /* Sign extend the operand.  */
1861           ax_ext (expr, addr_size_bits);
1862           ax_simple (expr, aop_dup);
1863           ax_const_l (expr, 0);
1864           ax_simple (expr, aop_less_signed);
1865           ax_simple (expr, aop_log_not);
1866           i = ax_goto (expr, aop_if_goto);
1867           /* We have to emit 0 - X.  */
1868           ax_const_l (expr, 0);
1869           ax_simple (expr, aop_swap);
1870           ax_simple (expr, aop_sub);
1871           ax_label (expr, i, expr->len);
1872           break;
1873
1874         case DW_OP_neg:
1875           /* No need to sign extend here.  */
1876           ax_const_l (expr, 0);
1877           ax_simple (expr, aop_swap);
1878           ax_simple (expr, aop_sub);
1879           break;
1880
1881         case DW_OP_not:
1882           /* Sign extend the operand.  */
1883           ax_ext (expr, addr_size_bits);
1884           ax_simple (expr, aop_bit_not);
1885           break;
1886
1887         case DW_OP_plus_uconst:
1888           op_ptr = read_uleb128 (op_ptr, op_end, &reg);
1889           /* It would be really weird to emit `DW_OP_plus_uconst 0',
1890              but we micro-optimize anyhow.  */
1891           if (reg != 0)
1892             {
1893               ax_const_l (expr, reg);
1894               ax_simple (expr, aop_add);
1895             }
1896           break;
1897
1898         case DW_OP_and:
1899           ax_simple (expr, aop_bit_and);
1900           break;
1901
1902         case DW_OP_div:
1903           /* Sign extend the operands.  */
1904           ax_ext (expr, addr_size_bits);
1905           ax_simple (expr, aop_swap);
1906           ax_ext (expr, addr_size_bits);
1907           ax_simple (expr, aop_swap);
1908           ax_simple (expr, aop_div_signed);
1909           break;
1910
1911         case DW_OP_minus:
1912           ax_simple (expr, aop_sub);
1913           break;
1914
1915         case DW_OP_mod:
1916           ax_simple (expr, aop_rem_unsigned);
1917           break;
1918
1919         case DW_OP_mul:
1920           ax_simple (expr, aop_mul);
1921           break;
1922
1923         case DW_OP_or:
1924           ax_simple (expr, aop_bit_or);
1925           break;
1926
1927         case DW_OP_plus:
1928           ax_simple (expr, aop_add);
1929           break;
1930
1931         case DW_OP_shl:
1932           ax_simple (expr, aop_lsh);
1933           break;
1934
1935         case DW_OP_shr:
1936           ax_simple (expr, aop_rsh_unsigned);
1937           break;
1938
1939         case DW_OP_shra:
1940           ax_simple (expr, aop_rsh_signed);
1941           break;
1942
1943         case DW_OP_xor:
1944           ax_simple (expr, aop_bit_xor);
1945           break;
1946
1947         case DW_OP_le:
1948           /* Sign extend the operands.  */
1949           ax_ext (expr, addr_size_bits);
1950           ax_simple (expr, aop_swap);
1951           ax_ext (expr, addr_size_bits);
1952           /* Note no swap here: A <= B is !(B < A).  */
1953           ax_simple (expr, aop_less_signed);
1954           ax_simple (expr, aop_log_not);
1955           break;
1956
1957         case DW_OP_ge:
1958           /* Sign extend the operands.  */
1959           ax_ext (expr, addr_size_bits);
1960           ax_simple (expr, aop_swap);
1961           ax_ext (expr, addr_size_bits);
1962           ax_simple (expr, aop_swap);
1963           /* A >= B is !(A < B).  */
1964           ax_simple (expr, aop_less_signed);
1965           ax_simple (expr, aop_log_not);
1966           break;
1967
1968         case DW_OP_eq:
1969           /* Sign extend the operands.  */
1970           ax_ext (expr, addr_size_bits);
1971           ax_simple (expr, aop_swap);
1972           ax_ext (expr, addr_size_bits);
1973           /* No need for a second swap here.  */
1974           ax_simple (expr, aop_equal);
1975           break;
1976
1977         case DW_OP_lt:
1978           /* Sign extend the operands.  */
1979           ax_ext (expr, addr_size_bits);
1980           ax_simple (expr, aop_swap);
1981           ax_ext (expr, addr_size_bits);
1982           ax_simple (expr, aop_swap);
1983           ax_simple (expr, aop_less_signed);
1984           break;
1985
1986         case DW_OP_gt:
1987           /* Sign extend the operands.  */
1988           ax_ext (expr, addr_size_bits);
1989           ax_simple (expr, aop_swap);
1990           ax_ext (expr, addr_size_bits);
1991           /* Note no swap here: A > B is B < A.  */
1992           ax_simple (expr, aop_less_signed);
1993           break;
1994
1995         case DW_OP_ne:
1996           /* Sign extend the operands.  */
1997           ax_ext (expr, addr_size_bits);
1998           ax_simple (expr, aop_swap);
1999           ax_ext (expr, addr_size_bits);
2000           /* No need for a swap here.  */
2001           ax_simple (expr, aop_equal);
2002           ax_simple (expr, aop_log_not);
2003           break;
2004
2005         case DW_OP_call_frame_cfa:
2006           dwarf2_compile_cfa_to_ax (expr, loc, arch, expr->scope, per_cu);
2007           loc->kind = axs_lvalue_memory;
2008           break;
2009
2010         case DW_OP_GNU_push_tls_address:
2011           unimplemented (op);
2012           break;
2013
2014         case DW_OP_skip:
2015           offset = extract_signed_integer (op_ptr, 2, byte_order);
2016           op_ptr += 2;
2017           i = ax_goto (expr, aop_goto);
2018           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2019           VEC_safe_push (int, patches, i);
2020           break;
2021
2022         case DW_OP_bra:
2023           offset = extract_signed_integer (op_ptr, 2, byte_order);
2024           op_ptr += 2;
2025           /* Zero extend the operand.  */
2026           ax_zero_ext (expr, addr_size_bits);
2027           i = ax_goto (expr, aop_if_goto);
2028           VEC_safe_push (int, dw_labels, op_ptr + offset - base);
2029           VEC_safe_push (int, patches, i);
2030           break;
2031
2032         case DW_OP_nop:
2033           break;
2034
2035         case DW_OP_piece:
2036         case DW_OP_bit_piece:
2037           {
2038             ULONGEST size, offset;
2039
2040             if (op_ptr - 1 == previous_piece)
2041               error (_("Cannot translate empty pieces to agent expressions"));
2042             previous_piece = op_ptr - 1;
2043
2044             op_ptr = read_uleb128 (op_ptr, op_end, &size);
2045             if (op == DW_OP_piece)
2046               {
2047                 size *= 8;
2048                 offset = 0;
2049               }
2050             else
2051               op_ptr = read_uleb128 (op_ptr, op_end, &offset);
2052
2053             if (bits_collected + size > 8 * sizeof (LONGEST))
2054               error (_("Expression pieces exceed word size"));
2055
2056             /* Access the bits.  */
2057             switch (loc->kind)
2058               {
2059               case axs_lvalue_register:
2060                 ax_reg (expr, loc->u.reg);
2061                 break;
2062
2063               case axs_lvalue_memory:
2064                 /* Offset the pointer, if needed.  */
2065                 if (offset > 8)
2066                   {
2067                     ax_const_l (expr, offset / 8);
2068                     ax_simple (expr, aop_add);
2069                     offset %= 8;
2070                   }
2071                 access_memory (arch, expr, size);
2072                 break;
2073               }
2074
2075             /* For a bits-big-endian target, shift up what we already
2076                have.  For a bits-little-endian target, shift up the
2077                new data.  Note that there is a potential bug here if
2078                the DWARF expression leaves multiple values on the
2079                stack.  */
2080             if (bits_collected > 0)
2081               {
2082                 if (bits_big_endian)
2083                   {
2084                     ax_simple (expr, aop_swap);
2085                     ax_const_l (expr, size);
2086                     ax_simple (expr, aop_lsh);
2087                     /* We don't need a second swap here, because
2088                        aop_bit_or is symmetric.  */
2089                   }
2090                 else
2091                   {
2092                     ax_const_l (expr, size);
2093                     ax_simple (expr, aop_lsh);
2094                   }
2095                 ax_simple (expr, aop_bit_or);
2096               }
2097
2098             bits_collected += size;
2099             loc->kind = axs_rvalue;
2100           }
2101           break;
2102
2103         case DW_OP_GNU_uninit:
2104           unimplemented (op);
2105
2106         case DW_OP_call2:
2107         case DW_OP_call4:
2108           {
2109             struct dwarf2_locexpr_baton block;
2110             int size = (op == DW_OP_call2 ? 2 : 4);
2111
2112             uoffset = extract_unsigned_integer (op_ptr, size, byte_order);
2113             op_ptr += size;
2114
2115             block = dwarf2_fetch_die_location_block (uoffset, per_cu,
2116                                                      get_ax_pc, expr);
2117
2118             /* DW_OP_call_ref is currently not supported.  */
2119             gdb_assert (block.per_cu == per_cu);
2120
2121             dwarf2_compile_expr_to_ax (expr, loc, arch, addr_size,
2122                                        block.data, block.data + block.size,
2123                                        per_cu);
2124           }
2125           break;
2126
2127         case DW_OP_call_ref:
2128           unimplemented (op);
2129
2130         default:
2131           unimplemented (op);
2132         }
2133     }
2134
2135   /* Patch all the branches we emitted.  */
2136   for (i = 0; i < VEC_length (int, patches); ++i)
2137     {
2138       int targ = offsets[VEC_index (int, dw_labels, i)];
2139       if (targ == -1)
2140         internal_error (__FILE__, __LINE__, _("invalid label"));
2141       ax_label (expr, VEC_index (int, patches, i), targ);
2142     }
2143
2144   do_cleanups (cleanups);
2145 }
2146
2147 \f
2148 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2149    evaluator to calculate the location.  */
2150 static struct value *
2151 locexpr_read_variable (struct symbol *symbol, struct frame_info *frame)
2152 {
2153   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2154   struct value *val;
2155
2156   val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, dlbaton->data,
2157                                   dlbaton->size, dlbaton->per_cu);
2158
2159   return val;
2160 }
2161
2162 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
2163 static int
2164 locexpr_read_needs_frame (struct symbol *symbol)
2165 {
2166   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2167
2168   return dwarf2_loc_desc_needs_frame (dlbaton->data, dlbaton->size,
2169                                       dlbaton->per_cu);
2170 }
2171
2172 /* Return true if DATA points to the end of a piece.  END is one past
2173    the last byte in the expression.  */
2174
2175 static int
2176 piece_end_p (const gdb_byte *data, const gdb_byte *end)
2177 {
2178   return data == end || data[0] == DW_OP_piece || data[0] == DW_OP_bit_piece;
2179 }
2180
2181 /* Helper for locexpr_describe_location_piece that finds the name of a
2182    DWARF register.  */
2183
2184 static const char *
2185 locexpr_regname (struct gdbarch *gdbarch, int dwarf_regnum)
2186 {
2187   int regnum;
2188
2189   regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, dwarf_regnum);
2190   return gdbarch_register_name (gdbarch, regnum);
2191 }
2192
2193 /* Nicely describe a single piece of a location, returning an updated
2194    position in the bytecode sequence.  This function cannot recognize
2195    all locations; if a location is not recognized, it simply returns
2196    DATA.  */
2197
2198 static const gdb_byte *
2199 locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
2200                                  CORE_ADDR addr, struct objfile *objfile,
2201                                  const gdb_byte *data, const gdb_byte *end,
2202                                  unsigned int addr_size)
2203 {
2204   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2205
2206   if (data[0] >= DW_OP_reg0 && data[0] <= DW_OP_reg31)
2207     {
2208       fprintf_filtered (stream, _("a variable in $%s"),
2209                         locexpr_regname (gdbarch, data[0] - DW_OP_reg0));
2210       data += 1;
2211     }
2212   else if (data[0] == DW_OP_regx)
2213     {
2214       ULONGEST reg;
2215
2216       data = read_uleb128 (data + 1, end, &reg);
2217       fprintf_filtered (stream, _("a variable in $%s"),
2218                         locexpr_regname (gdbarch, reg));
2219     }
2220   else if (data[0] == DW_OP_fbreg)
2221     {
2222       struct block *b;
2223       struct symbol *framefunc;
2224       int frame_reg = 0;
2225       LONGEST frame_offset;
2226       const gdb_byte *base_data, *new_data, *save_data = data;
2227       size_t base_size;
2228       LONGEST base_offset = 0;
2229
2230       new_data = read_sleb128 (data + 1, end, &frame_offset);
2231       if (!piece_end_p (new_data, end))
2232         return data;
2233       data = new_data;
2234
2235       b = block_for_pc (addr);
2236
2237       if (!b)
2238         error (_("No block found for address for symbol \"%s\"."),
2239                SYMBOL_PRINT_NAME (symbol));
2240
2241       framefunc = block_linkage_function (b);
2242
2243       if (!framefunc)
2244         error (_("No function found for block for symbol \"%s\"."),
2245                SYMBOL_PRINT_NAME (symbol));
2246
2247       dwarf_expr_frame_base_1 (framefunc, addr, &base_data, &base_size);
2248
2249       if (base_data[0] >= DW_OP_breg0 && base_data[0] <= DW_OP_breg31)
2250         {
2251           const gdb_byte *buf_end;
2252           
2253           frame_reg = base_data[0] - DW_OP_breg0;
2254           buf_end = read_sleb128 (base_data + 1,
2255                                   base_data + base_size, &base_offset);
2256           if (buf_end != base_data + base_size)
2257             error (_("Unexpected opcode after "
2258                      "DW_OP_breg%u for symbol \"%s\"."),
2259                    frame_reg, SYMBOL_PRINT_NAME (symbol));
2260         }
2261       else if (base_data[0] >= DW_OP_reg0 && base_data[0] <= DW_OP_reg31)
2262         {
2263           /* The frame base is just the register, with no offset.  */
2264           frame_reg = base_data[0] - DW_OP_reg0;
2265           base_offset = 0;
2266         }
2267       else
2268         {
2269           /* We don't know what to do with the frame base expression,
2270              so we can't trace this variable; give up.  */
2271           return save_data;
2272         }
2273
2274       fprintf_filtered (stream,
2275                         _("a variable at frame base reg $%s offset %s+%s"),
2276                         locexpr_regname (gdbarch, frame_reg),
2277                         plongest (base_offset), plongest (frame_offset));
2278     }
2279   else if (data[0] >= DW_OP_breg0 && data[0] <= DW_OP_breg31
2280            && piece_end_p (data, end))
2281     {
2282       LONGEST offset;
2283
2284       data = read_sleb128 (data + 1, end, &offset);
2285
2286       fprintf_filtered (stream,
2287                         _("a variable at offset %s from base reg $%s"),
2288                         plongest (offset),
2289                         locexpr_regname (gdbarch, data[0] - DW_OP_breg0));
2290     }
2291
2292   /* The location expression for a TLS variable looks like this (on a
2293      64-bit LE machine):
2294
2295      DW_AT_location    : 10 byte block: 3 4 0 0 0 0 0 0 0 e0
2296                         (DW_OP_addr: 4; DW_OP_GNU_push_tls_address)
2297
2298      0x3 is the encoding for DW_OP_addr, which has an operand as long
2299      as the size of an address on the target machine (here is 8
2300      bytes).  Note that more recent version of GCC emit DW_OP_const4u
2301      or DW_OP_const8u, depending on address size, rather than
2302      DW_OP_addr.  0xe0 is the encoding for DW_OP_GNU_push_tls_address.
2303      The operand represents the offset at which the variable is within
2304      the thread local storage.  */
2305
2306   else if (data + 1 + addr_size < end
2307            && (data[0] == DW_OP_addr
2308                || (addr_size == 4 && data[0] == DW_OP_const4u)
2309                || (addr_size == 8 && data[0] == DW_OP_const8u))
2310            && data[1 + addr_size] == DW_OP_GNU_push_tls_address
2311            && piece_end_p (data + 2 + addr_size, end))
2312     {
2313       ULONGEST offset;
2314       offset = extract_unsigned_integer (data + 1, addr_size,
2315                                          gdbarch_byte_order (gdbarch));
2316
2317       fprintf_filtered (stream, 
2318                         _("a thread-local variable at offset 0x%s "
2319                           "in the thread-local storage for `%s'"),
2320                         phex_nz (offset, addr_size), objfile->name);
2321
2322       data += 1 + addr_size + 1;
2323     }
2324   else if (data[0] >= DW_OP_lit0
2325            && data[0] <= DW_OP_lit31
2326            && data + 1 < end
2327            && data[1] == DW_OP_stack_value)
2328     {
2329       fprintf_filtered (stream, _("the constant %d"), data[0] - DW_OP_lit0);
2330       data += 2;
2331     }
2332
2333   return data;
2334 }
2335
2336 /* Disassemble an expression, stopping at the end of a piece or at the
2337    end of the expression.  Returns a pointer to the next unread byte
2338    in the input expression.  If ALL is nonzero, then this function
2339    will keep going until it reaches the end of the expression.  */
2340
2341 static const gdb_byte *
2342 disassemble_dwarf_expression (struct ui_file *stream,
2343                               struct gdbarch *arch, unsigned int addr_size,
2344                               int offset_size,
2345                               const gdb_byte *data, const gdb_byte *end,
2346                               int all,
2347                               struct dwarf2_per_cu_data *per_cu)
2348 {
2349   const gdb_byte *start = data;
2350
2351   fprintf_filtered (stream, _("a complex DWARF expression:\n"));
2352
2353   while (data < end
2354          && (all
2355              || (data[0] != DW_OP_piece && data[0] != DW_OP_bit_piece)))
2356     {
2357       enum dwarf_location_atom op = *data++;
2358       ULONGEST ul;
2359       LONGEST l;
2360       const char *name;
2361
2362       name = dwarf_stack_op_name (op);
2363
2364       if (!name)
2365         error (_("Unrecognized DWARF opcode 0x%02x at %ld"),
2366                op, (long) (data - 1 - start));
2367       fprintf_filtered (stream, "  % 4ld: %s", (long) (data - 1 - start), name);
2368
2369       switch (op)
2370         {
2371         case DW_OP_addr:
2372           ul = extract_unsigned_integer (data, addr_size,
2373                                          gdbarch_byte_order (arch));
2374           data += addr_size;
2375           fprintf_filtered (stream, " 0x%s", phex_nz (ul, addr_size));
2376           break;
2377
2378         case DW_OP_const1u:
2379           ul = extract_unsigned_integer (data, 1, gdbarch_byte_order (arch));
2380           data += 1;
2381           fprintf_filtered (stream, " %s", pulongest (ul));
2382           break;
2383         case DW_OP_const1s:
2384           l = extract_signed_integer (data, 1, gdbarch_byte_order (arch));
2385           data += 1;
2386           fprintf_filtered (stream, " %s", plongest (l));
2387           break;
2388         case DW_OP_const2u:
2389           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2390           data += 2;
2391           fprintf_filtered (stream, " %s", pulongest (ul));
2392           break;
2393         case DW_OP_const2s:
2394           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2395           data += 2;
2396           fprintf_filtered (stream, " %s", plongest (l));
2397           break;
2398         case DW_OP_const4u:
2399           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2400           data += 4;
2401           fprintf_filtered (stream, " %s", pulongest (ul));
2402           break;
2403         case DW_OP_const4s:
2404           l = extract_signed_integer (data, 4, gdbarch_byte_order (arch));
2405           data += 4;
2406           fprintf_filtered (stream, " %s", plongest (l));
2407           break;
2408         case DW_OP_const8u:
2409           ul = extract_unsigned_integer (data, 8, gdbarch_byte_order (arch));
2410           data += 8;
2411           fprintf_filtered (stream, " %s", pulongest (ul));
2412           break;
2413         case DW_OP_const8s:
2414           l = extract_signed_integer (data, 8, gdbarch_byte_order (arch));
2415           data += 8;
2416           fprintf_filtered (stream, " %s", plongest (l));
2417           break;
2418         case DW_OP_constu:
2419           data = read_uleb128 (data, end, &ul);
2420           fprintf_filtered (stream, " %s", pulongest (ul));
2421           break;
2422         case DW_OP_consts:
2423           data = read_sleb128 (data, end, &l);
2424           fprintf_filtered (stream, " %s", plongest (l));
2425           break;
2426
2427         case DW_OP_reg0:
2428         case DW_OP_reg1:
2429         case DW_OP_reg2:
2430         case DW_OP_reg3:
2431         case DW_OP_reg4:
2432         case DW_OP_reg5:
2433         case DW_OP_reg6:
2434         case DW_OP_reg7:
2435         case DW_OP_reg8:
2436         case DW_OP_reg9:
2437         case DW_OP_reg10:
2438         case DW_OP_reg11:
2439         case DW_OP_reg12:
2440         case DW_OP_reg13:
2441         case DW_OP_reg14:
2442         case DW_OP_reg15:
2443         case DW_OP_reg16:
2444         case DW_OP_reg17:
2445         case DW_OP_reg18:
2446         case DW_OP_reg19:
2447         case DW_OP_reg20:
2448         case DW_OP_reg21:
2449         case DW_OP_reg22:
2450         case DW_OP_reg23:
2451         case DW_OP_reg24:
2452         case DW_OP_reg25:
2453         case DW_OP_reg26:
2454         case DW_OP_reg27:
2455         case DW_OP_reg28:
2456         case DW_OP_reg29:
2457         case DW_OP_reg30:
2458         case DW_OP_reg31:
2459           fprintf_filtered (stream, " [$%s]",
2460                             locexpr_regname (arch, op - DW_OP_reg0));
2461           break;
2462
2463         case DW_OP_regx:
2464           data = read_uleb128 (data, end, &ul);
2465           fprintf_filtered (stream, " %s [$%s]", pulongest (ul),
2466                             locexpr_regname (arch, (int) ul));
2467           break;
2468
2469         case DW_OP_implicit_value:
2470           data = read_uleb128 (data, end, &ul);
2471           data += ul;
2472           fprintf_filtered (stream, " %s", pulongest (ul));
2473           break;
2474
2475         case DW_OP_breg0:
2476         case DW_OP_breg1:
2477         case DW_OP_breg2:
2478         case DW_OP_breg3:
2479         case DW_OP_breg4:
2480         case DW_OP_breg5:
2481         case DW_OP_breg6:
2482         case DW_OP_breg7:
2483         case DW_OP_breg8:
2484         case DW_OP_breg9:
2485         case DW_OP_breg10:
2486         case DW_OP_breg11:
2487         case DW_OP_breg12:
2488         case DW_OP_breg13:
2489         case DW_OP_breg14:
2490         case DW_OP_breg15:
2491         case DW_OP_breg16:
2492         case DW_OP_breg17:
2493         case DW_OP_breg18:
2494         case DW_OP_breg19:
2495         case DW_OP_breg20:
2496         case DW_OP_breg21:
2497         case DW_OP_breg22:
2498         case DW_OP_breg23:
2499         case DW_OP_breg24:
2500         case DW_OP_breg25:
2501         case DW_OP_breg26:
2502         case DW_OP_breg27:
2503         case DW_OP_breg28:
2504         case DW_OP_breg29:
2505         case DW_OP_breg30:
2506         case DW_OP_breg31:
2507           data = read_sleb128 (data, end, &l);
2508           fprintf_filtered (stream, " %s [$%s]", plongest (l),
2509                             locexpr_regname (arch, op - DW_OP_breg0));
2510           break;
2511
2512         case DW_OP_bregx:
2513           data = read_uleb128 (data, end, &ul);
2514           data = read_sleb128 (data, end, &l);
2515           fprintf_filtered (stream, " register %s [$%s] offset %s",
2516                             pulongest (ul),
2517                             locexpr_regname (arch, (int) ul),
2518                             plongest (l));
2519           break;
2520
2521         case DW_OP_fbreg:
2522           data = read_sleb128 (data, end, &l);
2523           fprintf_filtered (stream, " %s", plongest (l));
2524           break;
2525
2526         case DW_OP_xderef_size:
2527         case DW_OP_deref_size:
2528         case DW_OP_pick:
2529           fprintf_filtered (stream, " %d", *data);
2530           ++data;
2531           break;
2532
2533         case DW_OP_plus_uconst:
2534           data = read_uleb128 (data, end, &ul);
2535           fprintf_filtered (stream, " %s", pulongest (ul));
2536           break;
2537
2538         case DW_OP_skip:
2539           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2540           data += 2;
2541           fprintf_filtered (stream, " to %ld",
2542                             (long) (data + l - start));
2543           break;
2544
2545         case DW_OP_bra:
2546           l = extract_signed_integer (data, 2, gdbarch_byte_order (arch));
2547           data += 2;
2548           fprintf_filtered (stream, " %ld",
2549                             (long) (data + l - start));
2550           break;
2551
2552         case DW_OP_call2:
2553           ul = extract_unsigned_integer (data, 2, gdbarch_byte_order (arch));
2554           data += 2;
2555           fprintf_filtered (stream, " offset %s", phex_nz (ul, 2));
2556           break;
2557
2558         case DW_OP_call4:
2559           ul = extract_unsigned_integer (data, 4, gdbarch_byte_order (arch));
2560           data += 4;
2561           fprintf_filtered (stream, " offset %s", phex_nz (ul, 4));
2562           break;
2563
2564         case DW_OP_call_ref:
2565           ul = extract_unsigned_integer (data, offset_size,
2566                                          gdbarch_byte_order (arch));
2567           data += offset_size;
2568           fprintf_filtered (stream, " offset %s", phex_nz (ul, offset_size));
2569           break;
2570
2571         case DW_OP_piece:
2572           data = read_uleb128 (data, end, &ul);
2573           fprintf_filtered (stream, " %s (bytes)", pulongest (ul));
2574           break;
2575
2576         case DW_OP_bit_piece:
2577           {
2578             ULONGEST offset;
2579
2580             data = read_uleb128 (data, end, &ul);
2581             data = read_uleb128 (data, end, &offset);
2582             fprintf_filtered (stream, " size %s offset %s (bits)",
2583                               pulongest (ul), pulongest (offset));
2584           }
2585           break;
2586
2587         case DW_OP_GNU_implicit_pointer:
2588           {
2589             ul = extract_unsigned_integer (data, offset_size,
2590                                            gdbarch_byte_order (arch));
2591             data += offset_size;
2592
2593             data = read_sleb128 (data, end, &l);
2594
2595             fprintf_filtered (stream, " DIE %s offset %s",
2596                               phex_nz (ul, offset_size),
2597                               plongest (l));
2598           }
2599           break;
2600
2601         case DW_OP_GNU_deref_type:
2602           {
2603             int addr_size = *data++;
2604             ULONGEST offset;
2605             struct type *type;
2606
2607             data = read_uleb128 (data, end, &offset);
2608             type = dwarf2_get_die_type (offset, per_cu);
2609             fprintf_filtered (stream, "<");
2610             type_print (type, "", stream, -1);
2611             fprintf_filtered (stream, " [0x%s]> %d", phex_nz (offset, 0),
2612                               addr_size);
2613           }
2614           break;
2615
2616         case DW_OP_GNU_const_type:
2617           {
2618             ULONGEST type_die;
2619             struct type *type;
2620
2621             data = read_uleb128 (data, end, &type_die);
2622             type = dwarf2_get_die_type (type_die, per_cu);
2623             fprintf_filtered (stream, "<");
2624             type_print (type, "", stream, -1);
2625             fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
2626           }
2627           break;
2628
2629         case DW_OP_GNU_regval_type:
2630           {
2631             ULONGEST type_die, reg;
2632             struct type *type;
2633
2634             data = read_uleb128 (data, end, &reg);
2635             data = read_uleb128 (data, end, &type_die);
2636
2637             type = dwarf2_get_die_type (type_die, per_cu);
2638             fprintf_filtered (stream, "<");
2639             type_print (type, "", stream, -1);
2640             fprintf_filtered (stream, " [0x%s]> [$%s]", phex_nz (type_die, 0),
2641                               locexpr_regname (arch, reg));
2642           }
2643           break;
2644
2645         case DW_OP_GNU_convert:
2646         case DW_OP_GNU_reinterpret:
2647           {
2648             ULONGEST type_die;
2649
2650             data = read_uleb128 (data, end, &type_die);
2651
2652             if (type_die == 0)
2653               fprintf_filtered (stream, "<0>");
2654             else
2655               {
2656                 struct type *type;
2657
2658                 type = dwarf2_get_die_type (type_die, per_cu);
2659                 fprintf_filtered (stream, "<");
2660                 type_print (type, "", stream, -1);
2661                 fprintf_filtered (stream, " [0x%s]>", phex_nz (type_die, 0));
2662               }
2663           }
2664           break;
2665         }
2666
2667       fprintf_filtered (stream, "\n");
2668     }
2669
2670   return data;
2671 }
2672
2673 /* Describe a single location, which may in turn consist of multiple
2674    pieces.  */
2675
2676 static void
2677 locexpr_describe_location_1 (struct symbol *symbol, CORE_ADDR addr,
2678                              struct ui_file *stream,
2679                              const gdb_byte *data, int size,
2680                              struct objfile *objfile, unsigned int addr_size,
2681                              int offset_size, struct dwarf2_per_cu_data *per_cu)
2682 {
2683   const gdb_byte *end = data + size;
2684   int first_piece = 1, bad = 0;
2685
2686   while (data < end)
2687     {
2688       const gdb_byte *here = data;
2689       int disassemble = 1;
2690
2691       if (first_piece)
2692         first_piece = 0;
2693       else
2694         fprintf_filtered (stream, _(", and "));
2695
2696       if (!dwarf2_always_disassemble)
2697         {
2698           data = locexpr_describe_location_piece (symbol, stream,
2699                                                   addr, objfile,
2700                                                   data, end, addr_size);
2701           /* If we printed anything, or if we have an empty piece,
2702              then don't disassemble.  */
2703           if (data != here
2704               || data[0] == DW_OP_piece
2705               || data[0] == DW_OP_bit_piece)
2706             disassemble = 0;
2707         }
2708       if (disassemble)
2709         data = disassemble_dwarf_expression (stream,
2710                                              get_objfile_arch (objfile),
2711                                              addr_size, offset_size, data, end,
2712                                              dwarf2_always_disassemble,
2713                                              per_cu);
2714
2715       if (data < end)
2716         {
2717           int empty = data == here;
2718               
2719           if (disassemble)
2720             fprintf_filtered (stream, "   ");
2721           if (data[0] == DW_OP_piece)
2722             {
2723               ULONGEST bytes;
2724
2725               data = read_uleb128 (data + 1, end, &bytes);
2726
2727               if (empty)
2728                 fprintf_filtered (stream, _("an empty %s-byte piece"),
2729                                   pulongest (bytes));
2730               else
2731                 fprintf_filtered (stream, _(" [%s-byte piece]"),
2732                                   pulongest (bytes));
2733             }
2734           else if (data[0] == DW_OP_bit_piece)
2735             {
2736               ULONGEST bits, offset;
2737
2738               data = read_uleb128 (data + 1, end, &bits);
2739               data = read_uleb128 (data, end, &offset);
2740
2741               if (empty)
2742                 fprintf_filtered (stream,
2743                                   _("an empty %s-bit piece"),
2744                                   pulongest (bits));
2745               else
2746                 fprintf_filtered (stream,
2747                                   _(" [%s-bit piece, offset %s bits]"),
2748                                   pulongest (bits), pulongest (offset));
2749             }
2750           else
2751             {
2752               bad = 1;
2753               break;
2754             }
2755         }
2756     }
2757
2758   if (bad || data > end)
2759     error (_("Corrupted DWARF2 expression for \"%s\"."),
2760            SYMBOL_PRINT_NAME (symbol));
2761 }
2762
2763 /* Print a natural-language description of SYMBOL to STREAM.  This
2764    version is for a symbol with a single location.  */
2765
2766 static void
2767 locexpr_describe_location (struct symbol *symbol, CORE_ADDR addr,
2768                            struct ui_file *stream)
2769 {
2770   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2771   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2772   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2773   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2774
2775   locexpr_describe_location_1 (symbol, addr, stream,
2776                                dlbaton->data, dlbaton->size,
2777                                objfile, addr_size, offset_size,
2778                                dlbaton->per_cu);
2779 }
2780
2781 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2782    any necessary bytecode in AX.  */
2783
2784 static void
2785 locexpr_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2786                             struct agent_expr *ax, struct axs_value *value)
2787 {
2788   struct dwarf2_locexpr_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2789   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2790
2791   if (dlbaton->data == NULL || dlbaton->size == 0)
2792     value->optimized_out = 1;
2793   else
2794     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size,
2795                                dlbaton->data, dlbaton->data + dlbaton->size,
2796                                dlbaton->per_cu);
2797 }
2798
2799 /* The set of location functions used with the DWARF-2 expression
2800    evaluator.  */
2801 const struct symbol_computed_ops dwarf2_locexpr_funcs = {
2802   locexpr_read_variable,
2803   locexpr_read_needs_frame,
2804   locexpr_describe_location,
2805   locexpr_tracepoint_var_ref
2806 };
2807
2808
2809 /* Wrapper functions for location lists.  These generally find
2810    the appropriate location expression and call something above.  */
2811
2812 /* Return the value of SYMBOL in FRAME using the DWARF-2 expression
2813    evaluator to calculate the location.  */
2814 static struct value *
2815 loclist_read_variable (struct symbol *symbol, struct frame_info *frame)
2816 {
2817   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2818   struct value *val;
2819   const gdb_byte *data;
2820   size_t size;
2821   CORE_ADDR pc = frame ? get_frame_address_in_block (frame) : 0;
2822
2823   data = dwarf2_find_location_expression (dlbaton, &size, pc);
2824   if (data == NULL)
2825     val = allocate_optimized_out_value (SYMBOL_TYPE (symbol));
2826   else
2827     val = dwarf2_evaluate_loc_desc (SYMBOL_TYPE (symbol), frame, data, size,
2828                                     dlbaton->per_cu);
2829
2830   return val;
2831 }
2832
2833 /* Return non-zero iff we need a frame to evaluate SYMBOL.  */
2834 static int
2835 loclist_read_needs_frame (struct symbol *symbol)
2836 {
2837   /* If there's a location list, then assume we need to have a frame
2838      to choose the appropriate location expression.  With tracking of
2839      global variables this is not necessarily true, but such tracking
2840      is disabled in GCC at the moment until we figure out how to
2841      represent it.  */
2842
2843   return 1;
2844 }
2845
2846 /* Print a natural-language description of SYMBOL to STREAM.  This
2847    version applies when there is a list of different locations, each
2848    with a specified address range.  */
2849
2850 static void
2851 loclist_describe_location (struct symbol *symbol, CORE_ADDR addr,
2852                            struct ui_file *stream)
2853 {
2854   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2855   CORE_ADDR low, high;
2856   const gdb_byte *loc_ptr, *buf_end;
2857   int length, first = 1;
2858   struct objfile *objfile = dwarf2_per_cu_objfile (dlbaton->per_cu);
2859   struct gdbarch *gdbarch = get_objfile_arch (objfile);
2860   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2861   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2862   int offset_size = dwarf2_per_cu_offset_size (dlbaton->per_cu);
2863   int signed_addr_p = bfd_get_sign_extend_vma (objfile->obfd);
2864   CORE_ADDR base_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
2865   /* Adjust base_address for relocatable objects.  */
2866   CORE_ADDR base_offset = dwarf2_per_cu_text_offset (dlbaton->per_cu);
2867   CORE_ADDR base_address = dlbaton->base_address + base_offset;
2868
2869   loc_ptr = dlbaton->data;
2870   buf_end = dlbaton->data + dlbaton->size;
2871
2872   fprintf_filtered (stream, _("multi-location:\n"));
2873
2874   /* Iterate through locations until we run out.  */
2875   while (1)
2876     {
2877       if (buf_end - loc_ptr < 2 * addr_size)
2878         error (_("Corrupted DWARF expression for symbol \"%s\"."),
2879                SYMBOL_PRINT_NAME (symbol));
2880
2881       if (signed_addr_p)
2882         low = extract_signed_integer (loc_ptr, addr_size, byte_order);
2883       else
2884         low = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2885       loc_ptr += addr_size;
2886
2887       if (signed_addr_p)
2888         high = extract_signed_integer (loc_ptr, addr_size, byte_order);
2889       else
2890         high = extract_unsigned_integer (loc_ptr, addr_size, byte_order);
2891       loc_ptr += addr_size;
2892
2893       /* A base-address-selection entry.  */
2894       if ((low & base_mask) == base_mask)
2895         {
2896           base_address = high + base_offset;
2897           fprintf_filtered (stream, _("  Base address %s"),
2898                             paddress (gdbarch, base_address));
2899           continue;
2900         }
2901
2902       /* An end-of-list entry.  */
2903       if (low == 0 && high == 0)
2904         break;
2905
2906       /* Otherwise, a location expression entry.  */
2907       low += base_address;
2908       high += base_address;
2909
2910       length = extract_unsigned_integer (loc_ptr, 2, byte_order);
2911       loc_ptr += 2;
2912
2913       /* (It would improve readability to print only the minimum
2914          necessary digits of the second number of the range.)  */
2915       fprintf_filtered (stream, _("  Range %s-%s: "),
2916                         paddress (gdbarch, low), paddress (gdbarch, high));
2917
2918       /* Now describe this particular location.  */
2919       locexpr_describe_location_1 (symbol, low, stream, loc_ptr, length,
2920                                    objfile, addr_size, offset_size,
2921                                    dlbaton->per_cu);
2922
2923       fprintf_filtered (stream, "\n");
2924
2925       loc_ptr += length;
2926     }
2927 }
2928
2929 /* Describe the location of SYMBOL as an agent value in VALUE, generating
2930    any necessary bytecode in AX.  */
2931 static void
2932 loclist_tracepoint_var_ref (struct symbol *symbol, struct gdbarch *gdbarch,
2933                             struct agent_expr *ax, struct axs_value *value)
2934 {
2935   struct dwarf2_loclist_baton *dlbaton = SYMBOL_LOCATION_BATON (symbol);
2936   const gdb_byte *data;
2937   size_t size;
2938   unsigned int addr_size = dwarf2_per_cu_addr_size (dlbaton->per_cu);
2939
2940   data = dwarf2_find_location_expression (dlbaton, &size, ax->scope);
2941   if (data == NULL || size == 0)
2942     value->optimized_out = 1;
2943   else
2944     dwarf2_compile_expr_to_ax (ax, value, gdbarch, addr_size, data, data + size,
2945                                dlbaton->per_cu);
2946 }
2947
2948 /* The set of location functions used with the DWARF-2 expression
2949    evaluator and location lists.  */
2950 const struct symbol_computed_ops dwarf2_loclist_funcs = {
2951   loclist_read_variable,
2952   loclist_read_needs_frame,
2953   loclist_describe_location,
2954   loclist_tracepoint_var_ref
2955 };