xref: /dflybsd-src/contrib/gdb-7/gdb/value.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
15796c8dcSSimon Schubert /* Low level packing and unpacking of values for GDB, the GNU Debugger.
25796c8dcSSimon Schubert 
3*ef5ccd6cSJohn Marino    Copyright (C) 1986-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert 
55796c8dcSSimon Schubert    This file is part of GDB.
65796c8dcSSimon Schubert 
75796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
85796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
95796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
105796c8dcSSimon Schubert    (at your option) any later version.
115796c8dcSSimon Schubert 
125796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
135796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
145796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
155796c8dcSSimon Schubert    GNU General Public License for more details.
165796c8dcSSimon Schubert 
175796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
185796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
195796c8dcSSimon Schubert 
205796c8dcSSimon Schubert #include "defs.h"
215796c8dcSSimon Schubert #include "arch-utils.h"
225796c8dcSSimon Schubert #include "gdb_string.h"
235796c8dcSSimon Schubert #include "symtab.h"
245796c8dcSSimon Schubert #include "gdbtypes.h"
255796c8dcSSimon Schubert #include "value.h"
265796c8dcSSimon Schubert #include "gdbcore.h"
275796c8dcSSimon Schubert #include "command.h"
285796c8dcSSimon Schubert #include "gdbcmd.h"
295796c8dcSSimon Schubert #include "target.h"
305796c8dcSSimon Schubert #include "language.h"
315796c8dcSSimon Schubert #include "demangle.h"
325796c8dcSSimon Schubert #include "doublest.h"
335796c8dcSSimon Schubert #include "gdb_assert.h"
345796c8dcSSimon Schubert #include "regcache.h"
355796c8dcSSimon Schubert #include "block.h"
365796c8dcSSimon Schubert #include "dfp.h"
375796c8dcSSimon Schubert #include "objfiles.h"
385796c8dcSSimon Schubert #include "valprint.h"
395796c8dcSSimon Schubert #include "cli/cli-decode.h"
40c50c785cSJohn Marino #include "exceptions.h"
415796c8dcSSimon Schubert #include "python/python.h"
42c50c785cSJohn Marino #include <ctype.h>
43c50c785cSJohn Marino #include "tracepoint.h"
44*ef5ccd6cSJohn Marino #include "cp-abi.h"
455796c8dcSSimon Schubert 
465796c8dcSSimon Schubert /* Prototypes for exported functions.  */
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert void _initialize_values (void);
495796c8dcSSimon Schubert 
505796c8dcSSimon Schubert /* Definition of a user function.  */
515796c8dcSSimon Schubert struct internal_function
525796c8dcSSimon Schubert {
535796c8dcSSimon Schubert   /* The name of the function.  It is a bit odd to have this in the
545796c8dcSSimon Schubert      function itself -- the user might use a differently-named
555796c8dcSSimon Schubert      convenience variable to hold the function.  */
565796c8dcSSimon Schubert   char *name;
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert   /* The handler.  */
595796c8dcSSimon Schubert   internal_function_fn handler;
605796c8dcSSimon Schubert 
615796c8dcSSimon Schubert   /* User data for the handler.  */
625796c8dcSSimon Schubert   void *cookie;
635796c8dcSSimon Schubert };
645796c8dcSSimon Schubert 
65c50c785cSJohn Marino /* Defines an [OFFSET, OFFSET + LENGTH) range.  */
66c50c785cSJohn Marino 
67c50c785cSJohn Marino struct range
68c50c785cSJohn Marino {
69c50c785cSJohn Marino   /* Lowest offset in the range.  */
70c50c785cSJohn Marino   int offset;
71c50c785cSJohn Marino 
72c50c785cSJohn Marino   /* Length of the range.  */
73c50c785cSJohn Marino   int length;
74c50c785cSJohn Marino };
75c50c785cSJohn Marino 
76c50c785cSJohn Marino typedef struct range range_s;
77c50c785cSJohn Marino 
78c50c785cSJohn Marino DEF_VEC_O(range_s);
79c50c785cSJohn Marino 
80c50c785cSJohn Marino /* Returns true if the ranges defined by [offset1, offset1+len1) and
81c50c785cSJohn Marino    [offset2, offset2+len2) overlap.  */
82c50c785cSJohn Marino 
83c50c785cSJohn Marino static int
ranges_overlap(int offset1,int len1,int offset2,int len2)84c50c785cSJohn Marino ranges_overlap (int offset1, int len1,
85c50c785cSJohn Marino 		int offset2, int len2)
86c50c785cSJohn Marino {
87c50c785cSJohn Marino   ULONGEST h, l;
88c50c785cSJohn Marino 
89c50c785cSJohn Marino   l = max (offset1, offset2);
90c50c785cSJohn Marino   h = min (offset1 + len1, offset2 + len2);
91c50c785cSJohn Marino   return (l < h);
92c50c785cSJohn Marino }
93c50c785cSJohn Marino 
94c50c785cSJohn Marino /* Returns true if the first argument is strictly less than the
95c50c785cSJohn Marino    second, useful for VEC_lower_bound.  We keep ranges sorted by
96c50c785cSJohn Marino    offset and coalesce overlapping and contiguous ranges, so this just
97c50c785cSJohn Marino    compares the starting offset.  */
98c50c785cSJohn Marino 
99c50c785cSJohn Marino static int
range_lessthan(const range_s * r1,const range_s * r2)100c50c785cSJohn Marino range_lessthan (const range_s *r1, const range_s *r2)
101c50c785cSJohn Marino {
102c50c785cSJohn Marino   return r1->offset < r2->offset;
103c50c785cSJohn Marino }
104c50c785cSJohn Marino 
105c50c785cSJohn Marino /* Returns true if RANGES contains any range that overlaps [OFFSET,
106c50c785cSJohn Marino    OFFSET+LENGTH).  */
107c50c785cSJohn Marino 
108c50c785cSJohn Marino static int
ranges_contain(VEC (range_s)* ranges,int offset,int length)109c50c785cSJohn Marino ranges_contain (VEC(range_s) *ranges, int offset, int length)
110c50c785cSJohn Marino {
111c50c785cSJohn Marino   range_s what;
112c50c785cSJohn Marino   int i;
113c50c785cSJohn Marino 
114c50c785cSJohn Marino   what.offset = offset;
115c50c785cSJohn Marino   what.length = length;
116c50c785cSJohn Marino 
117c50c785cSJohn Marino   /* We keep ranges sorted by offset and coalesce overlapping and
118c50c785cSJohn Marino      contiguous ranges, so to check if a range list contains a given
119c50c785cSJohn Marino      range, we can do a binary search for the position the given range
120c50c785cSJohn Marino      would be inserted if we only considered the starting OFFSET of
121c50c785cSJohn Marino      ranges.  We call that position I.  Since we also have LENGTH to
122c50c785cSJohn Marino      care for (this is a range afterall), we need to check if the
123c50c785cSJohn Marino      _previous_ range overlaps the I range.  E.g.,
124c50c785cSJohn Marino 
125c50c785cSJohn Marino          R
126c50c785cSJohn Marino          |---|
127c50c785cSJohn Marino        |---|    |---|  |------| ... |--|
128c50c785cSJohn Marino        0        1      2            N
129c50c785cSJohn Marino 
130c50c785cSJohn Marino        I=1
131c50c785cSJohn Marino 
132c50c785cSJohn Marino      In the case above, the binary search would return `I=1', meaning,
133c50c785cSJohn Marino      this OFFSET should be inserted at position 1, and the current
134c50c785cSJohn Marino      position 1 should be pushed further (and before 2).  But, `0'
135c50c785cSJohn Marino      overlaps with R.
136c50c785cSJohn Marino 
137c50c785cSJohn Marino      Then we need to check if the I range overlaps the I range itself.
138c50c785cSJohn Marino      E.g.,
139c50c785cSJohn Marino 
140c50c785cSJohn Marino               R
141c50c785cSJohn Marino               |---|
142c50c785cSJohn Marino        |---|    |---|  |-------| ... |--|
143c50c785cSJohn Marino        0        1      2             N
144c50c785cSJohn Marino 
145c50c785cSJohn Marino        I=1
146c50c785cSJohn Marino   */
147c50c785cSJohn Marino 
148c50c785cSJohn Marino   i = VEC_lower_bound (range_s, ranges, &what, range_lessthan);
149c50c785cSJohn Marino 
150c50c785cSJohn Marino   if (i > 0)
151c50c785cSJohn Marino     {
152c50c785cSJohn Marino       struct range *bef = VEC_index (range_s, ranges, i - 1);
153c50c785cSJohn Marino 
154c50c785cSJohn Marino       if (ranges_overlap (bef->offset, bef->length, offset, length))
155c50c785cSJohn Marino 	return 1;
156c50c785cSJohn Marino     }
157c50c785cSJohn Marino 
158c50c785cSJohn Marino   if (i < VEC_length (range_s, ranges))
159c50c785cSJohn Marino     {
160c50c785cSJohn Marino       struct range *r = VEC_index (range_s, ranges, i);
161c50c785cSJohn Marino 
162c50c785cSJohn Marino       if (ranges_overlap (r->offset, r->length, offset, length))
163c50c785cSJohn Marino 	return 1;
164c50c785cSJohn Marino     }
165c50c785cSJohn Marino 
166c50c785cSJohn Marino   return 0;
167c50c785cSJohn Marino }
168c50c785cSJohn Marino 
1695796c8dcSSimon Schubert static struct cmd_list_element *functionlist;
1705796c8dcSSimon Schubert 
171*ef5ccd6cSJohn Marino /* Note that the fields in this structure are arranged to save a bit
172*ef5ccd6cSJohn Marino    of memory.  */
173*ef5ccd6cSJohn Marino 
1745796c8dcSSimon Schubert struct value
1755796c8dcSSimon Schubert {
1765796c8dcSSimon Schubert   /* Type of value; either not an lval, or one of the various
1775796c8dcSSimon Schubert      different possible kinds of lval.  */
1785796c8dcSSimon Schubert   enum lval_type lval;
1795796c8dcSSimon Schubert 
1805796c8dcSSimon Schubert   /* Is it modifiable?  Only relevant if lval != not_lval.  */
181*ef5ccd6cSJohn Marino   unsigned int modifiable : 1;
182*ef5ccd6cSJohn Marino 
183*ef5ccd6cSJohn Marino   /* If zero, contents of this value are in the contents field.  If
184*ef5ccd6cSJohn Marino      nonzero, contents are in inferior.  If the lval field is lval_memory,
185*ef5ccd6cSJohn Marino      the contents are in inferior memory at location.address plus offset.
186*ef5ccd6cSJohn Marino      The lval field may also be lval_register.
187*ef5ccd6cSJohn Marino 
188*ef5ccd6cSJohn Marino      WARNING: This field is used by the code which handles watchpoints
189*ef5ccd6cSJohn Marino      (see breakpoint.c) to decide whether a particular value can be
190*ef5ccd6cSJohn Marino      watched by hardware watchpoints.  If the lazy flag is set for
191*ef5ccd6cSJohn Marino      some member of a value chain, it is assumed that this member of
192*ef5ccd6cSJohn Marino      the chain doesn't need to be watched as part of watching the
193*ef5ccd6cSJohn Marino      value itself.  This is how GDB avoids watching the entire struct
194*ef5ccd6cSJohn Marino      or array when the user wants to watch a single struct member or
195*ef5ccd6cSJohn Marino      array element.  If you ever change the way lazy flag is set and
196*ef5ccd6cSJohn Marino      reset, be sure to consider this use as well!  */
197*ef5ccd6cSJohn Marino   unsigned int lazy : 1;
198*ef5ccd6cSJohn Marino 
199*ef5ccd6cSJohn Marino   /* If nonzero, this is the value of a variable which does not
200*ef5ccd6cSJohn Marino      actually exist in the program.  */
201*ef5ccd6cSJohn Marino   unsigned int optimized_out : 1;
202*ef5ccd6cSJohn Marino 
203*ef5ccd6cSJohn Marino   /* If value is a variable, is it initialized or not.  */
204*ef5ccd6cSJohn Marino   unsigned int initialized : 1;
205*ef5ccd6cSJohn Marino 
206*ef5ccd6cSJohn Marino   /* If value is from the stack.  If this is set, read_stack will be
207*ef5ccd6cSJohn Marino      used instead of read_memory to enable extra caching.  */
208*ef5ccd6cSJohn Marino   unsigned int stack : 1;
209*ef5ccd6cSJohn Marino 
210*ef5ccd6cSJohn Marino   /* If the value has been released.  */
211*ef5ccd6cSJohn Marino   unsigned int released : 1;
2125796c8dcSSimon Schubert 
2135796c8dcSSimon Schubert   /* Location of value (if lval).  */
2145796c8dcSSimon Schubert   union
2155796c8dcSSimon Schubert   {
2165796c8dcSSimon Schubert     /* If lval == lval_memory, this is the address in the inferior.
2175796c8dcSSimon Schubert        If lval == lval_register, this is the byte offset into the
2185796c8dcSSimon Schubert        registers structure.  */
2195796c8dcSSimon Schubert     CORE_ADDR address;
2205796c8dcSSimon Schubert 
2215796c8dcSSimon Schubert     /* Pointer to internal variable.  */
2225796c8dcSSimon Schubert     struct internalvar *internalvar;
2235796c8dcSSimon Schubert 
2245796c8dcSSimon Schubert     /* If lval == lval_computed, this is a set of function pointers
2255796c8dcSSimon Schubert        to use to access and describe the value, and a closure pointer
2265796c8dcSSimon Schubert        for them to use.  */
2275796c8dcSSimon Schubert     struct
2285796c8dcSSimon Schubert     {
229a45ae5f8SJohn Marino       /* Functions to call.  */
230a45ae5f8SJohn Marino       const struct lval_funcs *funcs;
231a45ae5f8SJohn Marino 
232a45ae5f8SJohn Marino       /* Closure for those functions to use.  */
233a45ae5f8SJohn Marino       void *closure;
2345796c8dcSSimon Schubert     } computed;
2355796c8dcSSimon Schubert   } location;
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert   /* Describes offset of a value within lval of a structure in bytes.
2385796c8dcSSimon Schubert      If lval == lval_memory, this is an offset to the address.  If
2395796c8dcSSimon Schubert      lval == lval_register, this is a further offset from
2405796c8dcSSimon Schubert      location.address within the registers structure.  Note also the
2415796c8dcSSimon Schubert      member embedded_offset below.  */
2425796c8dcSSimon Schubert   int offset;
2435796c8dcSSimon Schubert 
2445796c8dcSSimon Schubert   /* Only used for bitfields; number of bits contained in them.  */
2455796c8dcSSimon Schubert   int bitsize;
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert   /* Only used for bitfields; position of start of field.  For
2485796c8dcSSimon Schubert      gdbarch_bits_big_endian=0 targets, it is the position of the LSB.  For
2495796c8dcSSimon Schubert      gdbarch_bits_big_endian=1 targets, it is the position of the MSB.  */
2505796c8dcSSimon Schubert   int bitpos;
2515796c8dcSSimon Schubert 
252*ef5ccd6cSJohn Marino   /* The number of references to this value.  When a value is created,
253*ef5ccd6cSJohn Marino      the value chain holds a reference, so REFERENCE_COUNT is 1.  If
254*ef5ccd6cSJohn Marino      release_value is called, this value is removed from the chain but
255*ef5ccd6cSJohn Marino      the caller of release_value now has a reference to this value.
256*ef5ccd6cSJohn Marino      The caller must arrange for a call to value_free later.  */
257*ef5ccd6cSJohn Marino   int reference_count;
258*ef5ccd6cSJohn Marino 
2595796c8dcSSimon Schubert   /* Only used for bitfields; the containing value.  This allows a
2605796c8dcSSimon Schubert      single read from the target when displaying multiple
2615796c8dcSSimon Schubert      bitfields.  */
2625796c8dcSSimon Schubert   struct value *parent;
2635796c8dcSSimon Schubert 
2645796c8dcSSimon Schubert   /* Frame register value is relative to.  This will be described in
2655796c8dcSSimon Schubert      the lval enum above as "lval_register".  */
2665796c8dcSSimon Schubert   struct frame_id frame_id;
2675796c8dcSSimon Schubert 
2685796c8dcSSimon Schubert   /* Type of the value.  */
2695796c8dcSSimon Schubert   struct type *type;
2705796c8dcSSimon Schubert 
2715796c8dcSSimon Schubert   /* If a value represents a C++ object, then the `type' field gives
2725796c8dcSSimon Schubert      the object's compile-time type.  If the object actually belongs
2735796c8dcSSimon Schubert      to some class derived from `type', perhaps with other base
2745796c8dcSSimon Schubert      classes and additional members, then `type' is just a subobject
2755796c8dcSSimon Schubert      of the real thing, and the full object is probably larger than
2765796c8dcSSimon Schubert      `type' would suggest.
2775796c8dcSSimon Schubert 
2785796c8dcSSimon Schubert      If `type' is a dynamic class (i.e. one with a vtable), then GDB
2795796c8dcSSimon Schubert      can actually determine the object's run-time type by looking at
2805796c8dcSSimon Schubert      the run-time type information in the vtable.  When this
2815796c8dcSSimon Schubert      information is available, we may elect to read in the entire
2825796c8dcSSimon Schubert      object, for several reasons:
2835796c8dcSSimon Schubert 
2845796c8dcSSimon Schubert      - When printing the value, the user would probably rather see the
2855796c8dcSSimon Schubert      full object, not just the limited portion apparent from the
2865796c8dcSSimon Schubert      compile-time type.
2875796c8dcSSimon Schubert 
2885796c8dcSSimon Schubert      - If `type' has virtual base classes, then even printing `type'
2895796c8dcSSimon Schubert      alone may require reaching outside the `type' portion of the
2905796c8dcSSimon Schubert      object to wherever the virtual base class has been stored.
2915796c8dcSSimon Schubert 
2925796c8dcSSimon Schubert      When we store the entire object, `enclosing_type' is the run-time
2935796c8dcSSimon Schubert      type -- the complete object -- and `embedded_offset' is the
2945796c8dcSSimon Schubert      offset of `type' within that larger type, in bytes.  The
2955796c8dcSSimon Schubert      value_contents() macro takes `embedded_offset' into account, so
2965796c8dcSSimon Schubert      most GDB code continues to see the `type' portion of the value,
2975796c8dcSSimon Schubert      just as the inferior would.
2985796c8dcSSimon Schubert 
2995796c8dcSSimon Schubert      If `type' is a pointer to an object, then `enclosing_type' is a
3005796c8dcSSimon Schubert      pointer to the object's run-time type, and `pointed_to_offset' is
3015796c8dcSSimon Schubert      the offset in bytes from the full object to the pointed-to object
3025796c8dcSSimon Schubert      -- that is, the value `embedded_offset' would have if we followed
3035796c8dcSSimon Schubert      the pointer and fetched the complete object.  (I don't really see
3045796c8dcSSimon Schubert      the point.  Why not just determine the run-time type when you
3055796c8dcSSimon Schubert      indirect, and avoid the special case?  The contents don't matter
3065796c8dcSSimon Schubert      until you indirect anyway.)
3075796c8dcSSimon Schubert 
3085796c8dcSSimon Schubert      If we're not doing anything fancy, `enclosing_type' is equal to
3095796c8dcSSimon Schubert      `type', and `embedded_offset' is zero, so everything works
3105796c8dcSSimon Schubert      normally.  */
3115796c8dcSSimon Schubert   struct type *enclosing_type;
3125796c8dcSSimon Schubert   int embedded_offset;
3135796c8dcSSimon Schubert   int pointed_to_offset;
3145796c8dcSSimon Schubert 
3155796c8dcSSimon Schubert   /* Values are stored in a chain, so that they can be deleted easily
3165796c8dcSSimon Schubert      over calls to the inferior.  Values assigned to internal
3175796c8dcSSimon Schubert      variables, put into the value history or exposed to Python are
3185796c8dcSSimon Schubert      taken off this list.  */
3195796c8dcSSimon Schubert   struct value *next;
3205796c8dcSSimon Schubert 
3215796c8dcSSimon Schubert   /* Register number if the value is from a register.  */
3225796c8dcSSimon Schubert   short regnum;
3235796c8dcSSimon Schubert 
3245796c8dcSSimon Schubert   /* Actual contents of the value.  Target byte-order.  NULL or not
3255796c8dcSSimon Schubert      valid if lazy is nonzero.  */
3265796c8dcSSimon Schubert   gdb_byte *contents;
3275796c8dcSSimon Schubert 
328c50c785cSJohn Marino   /* Unavailable ranges in CONTENTS.  We mark unavailable ranges,
329c50c785cSJohn Marino      rather than available, since the common and default case is for a
330c50c785cSJohn Marino      value to be available.  This is filled in at value read time.  */
331c50c785cSJohn Marino   VEC(range_s) *unavailable;
3325796c8dcSSimon Schubert };
3335796c8dcSSimon Schubert 
334c50c785cSJohn Marino int
value_bytes_available(const struct value * value,int offset,int length)335c50c785cSJohn Marino value_bytes_available (const struct value *value, int offset, int length)
336c50c785cSJohn Marino {
337c50c785cSJohn Marino   gdb_assert (!value->lazy);
338c50c785cSJohn Marino 
339c50c785cSJohn Marino   return !ranges_contain (value->unavailable, offset, length);
340c50c785cSJohn Marino }
341c50c785cSJohn Marino 
342c50c785cSJohn Marino int
value_entirely_available(struct value * value)343c50c785cSJohn Marino value_entirely_available (struct value *value)
344c50c785cSJohn Marino {
345c50c785cSJohn Marino   /* We can only tell whether the whole value is available when we try
346c50c785cSJohn Marino      to read it.  */
347c50c785cSJohn Marino   if (value->lazy)
348c50c785cSJohn Marino     value_fetch_lazy (value);
349c50c785cSJohn Marino 
350c50c785cSJohn Marino   if (VEC_empty (range_s, value->unavailable))
351c50c785cSJohn Marino     return 1;
352c50c785cSJohn Marino   return 0;
353c50c785cSJohn Marino }
354c50c785cSJohn Marino 
355c50c785cSJohn Marino void
mark_value_bytes_unavailable(struct value * value,int offset,int length)356c50c785cSJohn Marino mark_value_bytes_unavailable (struct value *value, int offset, int length)
357c50c785cSJohn Marino {
358c50c785cSJohn Marino   range_s newr;
359c50c785cSJohn Marino   int i;
360c50c785cSJohn Marino 
361c50c785cSJohn Marino   /* Insert the range sorted.  If there's overlap or the new range
362c50c785cSJohn Marino      would be contiguous with an existing range, merge.  */
363c50c785cSJohn Marino 
364c50c785cSJohn Marino   newr.offset = offset;
365c50c785cSJohn Marino   newr.length = length;
366c50c785cSJohn Marino 
367c50c785cSJohn Marino   /* Do a binary search for the position the given range would be
368c50c785cSJohn Marino      inserted if we only considered the starting OFFSET of ranges.
369c50c785cSJohn Marino      Call that position I.  Since we also have LENGTH to care for
370c50c785cSJohn Marino      (this is a range afterall), we need to check if the _previous_
371c50c785cSJohn Marino      range overlaps the I range.  E.g., calling R the new range:
372c50c785cSJohn Marino 
373c50c785cSJohn Marino        #1 - overlaps with previous
374c50c785cSJohn Marino 
375c50c785cSJohn Marino 	   R
376c50c785cSJohn Marino 	   |-...-|
377c50c785cSJohn Marino 	 |---|     |---|  |------| ... |--|
378c50c785cSJohn Marino 	 0         1      2            N
379c50c785cSJohn Marino 
380c50c785cSJohn Marino 	 I=1
381c50c785cSJohn Marino 
382c50c785cSJohn Marino      In the case #1 above, the binary search would return `I=1',
383c50c785cSJohn Marino      meaning, this OFFSET should be inserted at position 1, and the
384c50c785cSJohn Marino      current position 1 should be pushed further (and become 2).  But,
385c50c785cSJohn Marino      note that `0' overlaps with R, so we want to merge them.
386c50c785cSJohn Marino 
387c50c785cSJohn Marino      A similar consideration needs to be taken if the new range would
388c50c785cSJohn Marino      be contiguous with the previous range:
389c50c785cSJohn Marino 
390c50c785cSJohn Marino        #2 - contiguous with previous
391c50c785cSJohn Marino 
392c50c785cSJohn Marino 	    R
393c50c785cSJohn Marino 	    |-...-|
394c50c785cSJohn Marino 	 |--|       |---|  |------| ... |--|
395c50c785cSJohn Marino 	 0          1      2            N
396c50c785cSJohn Marino 
397c50c785cSJohn Marino 	 I=1
398c50c785cSJohn Marino 
399c50c785cSJohn Marino      If there's no overlap with the previous range, as in:
400c50c785cSJohn Marino 
401c50c785cSJohn Marino        #3 - not overlapping and not contiguous
402c50c785cSJohn Marino 
403c50c785cSJohn Marino 	       R
404c50c785cSJohn Marino 	       |-...-|
405c50c785cSJohn Marino 	  |--|         |---|  |------| ... |--|
406c50c785cSJohn Marino 	  0            1      2            N
407c50c785cSJohn Marino 
408c50c785cSJohn Marino 	 I=1
409c50c785cSJohn Marino 
410c50c785cSJohn Marino      or if I is 0:
411c50c785cSJohn Marino 
412c50c785cSJohn Marino        #4 - R is the range with lowest offset
413c50c785cSJohn Marino 
414c50c785cSJohn Marino 	  R
415c50c785cSJohn Marino 	 |-...-|
416c50c785cSJohn Marino 	         |--|       |---|  |------| ... |--|
417c50c785cSJohn Marino 	         0          1      2            N
418c50c785cSJohn Marino 
419c50c785cSJohn Marino 	 I=0
420c50c785cSJohn Marino 
421c50c785cSJohn Marino      ... we just push the new range to I.
422c50c785cSJohn Marino 
423c50c785cSJohn Marino      All the 4 cases above need to consider that the new range may
424c50c785cSJohn Marino      also overlap several of the ranges that follow, or that R may be
425c50c785cSJohn Marino      contiguous with the following range, and merge.  E.g.,
426c50c785cSJohn Marino 
427c50c785cSJohn Marino        #5 - overlapping following ranges
428c50c785cSJohn Marino 
429c50c785cSJohn Marino 	  R
430c50c785cSJohn Marino 	 |------------------------|
431c50c785cSJohn Marino 	         |--|       |---|  |------| ... |--|
432c50c785cSJohn Marino 	         0          1      2            N
433c50c785cSJohn Marino 
434c50c785cSJohn Marino 	 I=0
435c50c785cSJohn Marino 
436c50c785cSJohn Marino        or:
437c50c785cSJohn Marino 
438c50c785cSJohn Marino 	    R
439c50c785cSJohn Marino 	    |-------|
440c50c785cSJohn Marino 	 |--|       |---|  |------| ... |--|
441c50c785cSJohn Marino 	 0          1      2            N
442c50c785cSJohn Marino 
443c50c785cSJohn Marino 	 I=1
444c50c785cSJohn Marino 
445c50c785cSJohn Marino   */
446c50c785cSJohn Marino 
447c50c785cSJohn Marino   i = VEC_lower_bound (range_s, value->unavailable, &newr, range_lessthan);
448c50c785cSJohn Marino   if (i > 0)
449c50c785cSJohn Marino     {
450c50c785cSJohn Marino       struct range *bef = VEC_index (range_s, value->unavailable, i - 1);
451c50c785cSJohn Marino 
452c50c785cSJohn Marino       if (ranges_overlap (bef->offset, bef->length, offset, length))
453c50c785cSJohn Marino 	{
454c50c785cSJohn Marino 	  /* #1 */
455c50c785cSJohn Marino 	  ULONGEST l = min (bef->offset, offset);
456c50c785cSJohn Marino 	  ULONGEST h = max (bef->offset + bef->length, offset + length);
457c50c785cSJohn Marino 
458c50c785cSJohn Marino 	  bef->offset = l;
459c50c785cSJohn Marino 	  bef->length = h - l;
460c50c785cSJohn Marino 	  i--;
461c50c785cSJohn Marino 	}
462c50c785cSJohn Marino       else if (offset == bef->offset + bef->length)
463c50c785cSJohn Marino 	{
464c50c785cSJohn Marino 	  /* #2 */
465c50c785cSJohn Marino 	  bef->length += length;
466c50c785cSJohn Marino 	  i--;
467c50c785cSJohn Marino 	}
468c50c785cSJohn Marino       else
469c50c785cSJohn Marino 	{
470c50c785cSJohn Marino 	  /* #3 */
471c50c785cSJohn Marino 	  VEC_safe_insert (range_s, value->unavailable, i, &newr);
472c50c785cSJohn Marino 	}
473c50c785cSJohn Marino     }
474c50c785cSJohn Marino   else
475c50c785cSJohn Marino     {
476c50c785cSJohn Marino       /* #4 */
477c50c785cSJohn Marino       VEC_safe_insert (range_s, value->unavailable, i, &newr);
478c50c785cSJohn Marino     }
479c50c785cSJohn Marino 
480c50c785cSJohn Marino   /* Check whether the ranges following the one we've just added or
481c50c785cSJohn Marino      touched can be folded in (#5 above).  */
482c50c785cSJohn Marino   if (i + 1 < VEC_length (range_s, value->unavailable))
483c50c785cSJohn Marino     {
484c50c785cSJohn Marino       struct range *t;
485c50c785cSJohn Marino       struct range *r;
486c50c785cSJohn Marino       int removed = 0;
487c50c785cSJohn Marino       int next = i + 1;
488c50c785cSJohn Marino 
489c50c785cSJohn Marino       /* Get the range we just touched.  */
490c50c785cSJohn Marino       t = VEC_index (range_s, value->unavailable, i);
491c50c785cSJohn Marino       removed = 0;
492c50c785cSJohn Marino 
493c50c785cSJohn Marino       i = next;
494c50c785cSJohn Marino       for (; VEC_iterate (range_s, value->unavailable, i, r); i++)
495c50c785cSJohn Marino 	if (r->offset <= t->offset + t->length)
496c50c785cSJohn Marino 	  {
497c50c785cSJohn Marino 	    ULONGEST l, h;
498c50c785cSJohn Marino 
499c50c785cSJohn Marino 	    l = min (t->offset, r->offset);
500c50c785cSJohn Marino 	    h = max (t->offset + t->length, r->offset + r->length);
501c50c785cSJohn Marino 
502c50c785cSJohn Marino 	    t->offset = l;
503c50c785cSJohn Marino 	    t->length = h - l;
504c50c785cSJohn Marino 
505c50c785cSJohn Marino 	    removed++;
506c50c785cSJohn Marino 	  }
507c50c785cSJohn Marino 	else
508c50c785cSJohn Marino 	  {
509c50c785cSJohn Marino 	    /* If we couldn't merge this one, we won't be able to
510c50c785cSJohn Marino 	       merge following ones either, since the ranges are
511c50c785cSJohn Marino 	       always sorted by OFFSET.  */
512c50c785cSJohn Marino 	    break;
513c50c785cSJohn Marino 	  }
514c50c785cSJohn Marino 
515c50c785cSJohn Marino       if (removed != 0)
516c50c785cSJohn Marino 	VEC_block_remove (range_s, value->unavailable, next, removed);
517c50c785cSJohn Marino     }
518c50c785cSJohn Marino }
519c50c785cSJohn Marino 
520c50c785cSJohn Marino /* Find the first range in RANGES that overlaps the range defined by
521c50c785cSJohn Marino    OFFSET and LENGTH, starting at element POS in the RANGES vector,
522c50c785cSJohn Marino    Returns the index into RANGES where such overlapping range was
523c50c785cSJohn Marino    found, or -1 if none was found.  */
524c50c785cSJohn Marino 
525c50c785cSJohn Marino static int
find_first_range_overlap(VEC (range_s)* ranges,int pos,int offset,int length)526c50c785cSJohn Marino find_first_range_overlap (VEC(range_s) *ranges, int pos,
527c50c785cSJohn Marino 			  int offset, int length)
528c50c785cSJohn Marino {
529c50c785cSJohn Marino   range_s *r;
530c50c785cSJohn Marino   int i;
531c50c785cSJohn Marino 
532c50c785cSJohn Marino   for (i = pos; VEC_iterate (range_s, ranges, i, r); i++)
533c50c785cSJohn Marino     if (ranges_overlap (r->offset, r->length, offset, length))
534c50c785cSJohn Marino       return i;
535c50c785cSJohn Marino 
536c50c785cSJohn Marino   return -1;
537c50c785cSJohn Marino }
538c50c785cSJohn Marino 
539c50c785cSJohn Marino int
value_available_contents_eq(const struct value * val1,int offset1,const struct value * val2,int offset2,int length)540c50c785cSJohn Marino value_available_contents_eq (const struct value *val1, int offset1,
541c50c785cSJohn Marino 			     const struct value *val2, int offset2,
542c50c785cSJohn Marino 			     int length)
543c50c785cSJohn Marino {
544c50c785cSJohn Marino   int idx1 = 0, idx2 = 0;
545c50c785cSJohn Marino 
546*ef5ccd6cSJohn Marino   /* See function description in value.h.  */
547c50c785cSJohn Marino   gdb_assert (!val1->lazy && !val2->lazy);
548c50c785cSJohn Marino 
549c50c785cSJohn Marino   while (length > 0)
550c50c785cSJohn Marino     {
551c50c785cSJohn Marino       range_s *r1, *r2;
552c50c785cSJohn Marino       ULONGEST l1, h1;
553c50c785cSJohn Marino       ULONGEST l2, h2;
554c50c785cSJohn Marino 
555c50c785cSJohn Marino       idx1 = find_first_range_overlap (val1->unavailable, idx1,
556c50c785cSJohn Marino 				       offset1, length);
557c50c785cSJohn Marino       idx2 = find_first_range_overlap (val2->unavailable, idx2,
558c50c785cSJohn Marino 				       offset2, length);
559c50c785cSJohn Marino 
560c50c785cSJohn Marino       /* The usual case is for both values to be completely available.  */
561c50c785cSJohn Marino       if (idx1 == -1 && idx2 == -1)
562c50c785cSJohn Marino 	return (memcmp (val1->contents + offset1,
563c50c785cSJohn Marino 			val2->contents + offset2,
564c50c785cSJohn Marino 			length) == 0);
565c50c785cSJohn Marino       /* The contents only match equal if the available set matches as
566c50c785cSJohn Marino 	 well.  */
567c50c785cSJohn Marino       else if (idx1 == -1 || idx2 == -1)
568c50c785cSJohn Marino 	return 0;
569c50c785cSJohn Marino 
570c50c785cSJohn Marino       gdb_assert (idx1 != -1 && idx2 != -1);
571c50c785cSJohn Marino 
572c50c785cSJohn Marino       r1 = VEC_index (range_s, val1->unavailable, idx1);
573c50c785cSJohn Marino       r2 = VEC_index (range_s, val2->unavailable, idx2);
574c50c785cSJohn Marino 
575c50c785cSJohn Marino       /* Get the unavailable windows intersected by the incoming
576c50c785cSJohn Marino 	 ranges.  The first and last ranges that overlap the argument
577c50c785cSJohn Marino 	 range may be wider than said incoming arguments ranges.  */
578c50c785cSJohn Marino       l1 = max (offset1, r1->offset);
579c50c785cSJohn Marino       h1 = min (offset1 + length, r1->offset + r1->length);
580c50c785cSJohn Marino 
581c50c785cSJohn Marino       l2 = max (offset2, r2->offset);
582c50c785cSJohn Marino       h2 = min (offset2 + length, r2->offset + r2->length);
583c50c785cSJohn Marino 
584c50c785cSJohn Marino       /* Make them relative to the respective start offsets, so we can
585c50c785cSJohn Marino 	 compare them for equality.  */
586c50c785cSJohn Marino       l1 -= offset1;
587c50c785cSJohn Marino       h1 -= offset1;
588c50c785cSJohn Marino 
589c50c785cSJohn Marino       l2 -= offset2;
590c50c785cSJohn Marino       h2 -= offset2;
591c50c785cSJohn Marino 
592c50c785cSJohn Marino       /* Different availability, no match.  */
593c50c785cSJohn Marino       if (l1 != l2 || h1 != h2)
594c50c785cSJohn Marino 	return 0;
595c50c785cSJohn Marino 
596c50c785cSJohn Marino       /* Compare the _available_ contents.  */
597c50c785cSJohn Marino       if (memcmp (val1->contents + offset1,
598c50c785cSJohn Marino 		  val2->contents + offset2,
599c50c785cSJohn Marino 		  l1) != 0)
600c50c785cSJohn Marino 	return 0;
601c50c785cSJohn Marino 
602c50c785cSJohn Marino       length -= h1;
603c50c785cSJohn Marino       offset1 += h1;
604c50c785cSJohn Marino       offset2 += h1;
605c50c785cSJohn Marino     }
606c50c785cSJohn Marino 
607c50c785cSJohn Marino   return 1;
608c50c785cSJohn Marino }
609c50c785cSJohn Marino 
6105796c8dcSSimon Schubert /* Prototypes for local functions.  */
6115796c8dcSSimon Schubert 
6125796c8dcSSimon Schubert static void show_values (char *, int);
6135796c8dcSSimon Schubert 
6145796c8dcSSimon Schubert static void show_convenience (char *, int);
6155796c8dcSSimon Schubert 
6165796c8dcSSimon Schubert 
6175796c8dcSSimon Schubert /* The value-history records all the values printed
6185796c8dcSSimon Schubert    by print commands during this session.  Each chunk
6195796c8dcSSimon Schubert    records 60 consecutive values.  The first chunk on
6205796c8dcSSimon Schubert    the chain records the most recent values.
6215796c8dcSSimon Schubert    The total number of values is in value_history_count.  */
6225796c8dcSSimon Schubert 
6235796c8dcSSimon Schubert #define VALUE_HISTORY_CHUNK 60
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert struct value_history_chunk
6265796c8dcSSimon Schubert   {
6275796c8dcSSimon Schubert     struct value_history_chunk *next;
6285796c8dcSSimon Schubert     struct value *values[VALUE_HISTORY_CHUNK];
6295796c8dcSSimon Schubert   };
6305796c8dcSSimon Schubert 
6315796c8dcSSimon Schubert /* Chain of chunks now in use.  */
6325796c8dcSSimon Schubert 
6335796c8dcSSimon Schubert static struct value_history_chunk *value_history_chain;
6345796c8dcSSimon Schubert 
635c50c785cSJohn Marino static int value_history_count;	/* Abs number of last entry stored.  */
6365796c8dcSSimon Schubert 
6375796c8dcSSimon Schubert 
6385796c8dcSSimon Schubert /* List of all value objects currently allocated
6395796c8dcSSimon Schubert    (except for those released by calls to release_value)
6405796c8dcSSimon Schubert    This is so they can be freed after each command.  */
6415796c8dcSSimon Schubert 
6425796c8dcSSimon Schubert static struct value *all_values;
6435796c8dcSSimon Schubert 
6445796c8dcSSimon Schubert /* Allocate a lazy value for type TYPE.  Its actual content is
6455796c8dcSSimon Schubert    "lazily" allocated too: the content field of the return value is
6465796c8dcSSimon Schubert    NULL; it will be allocated when it is fetched from the target.  */
6475796c8dcSSimon Schubert 
6485796c8dcSSimon Schubert struct value *
allocate_value_lazy(struct type * type)6495796c8dcSSimon Schubert allocate_value_lazy (struct type *type)
6505796c8dcSSimon Schubert {
6515796c8dcSSimon Schubert   struct value *val;
652cf7f2e2dSJohn Marino 
653cf7f2e2dSJohn Marino   /* Call check_typedef on our type to make sure that, if TYPE
654cf7f2e2dSJohn Marino      is a TYPE_CODE_TYPEDEF, its length is set to the length
655cf7f2e2dSJohn Marino      of the target type instead of zero.  However, we do not
656cf7f2e2dSJohn Marino      replace the typedef type by the target type, because we want
657cf7f2e2dSJohn Marino      to keep the typedef in order to be able to set the VAL's type
658cf7f2e2dSJohn Marino      description correctly.  */
659cf7f2e2dSJohn Marino   check_typedef (type);
6605796c8dcSSimon Schubert 
6615796c8dcSSimon Schubert   val = (struct value *) xzalloc (sizeof (struct value));
6625796c8dcSSimon Schubert   val->contents = NULL;
6635796c8dcSSimon Schubert   val->next = all_values;
6645796c8dcSSimon Schubert   all_values = val;
6655796c8dcSSimon Schubert   val->type = type;
6665796c8dcSSimon Schubert   val->enclosing_type = type;
6675796c8dcSSimon Schubert   VALUE_LVAL (val) = not_lval;
6685796c8dcSSimon Schubert   val->location.address = 0;
6695796c8dcSSimon Schubert   VALUE_FRAME_ID (val) = null_frame_id;
6705796c8dcSSimon Schubert   val->offset = 0;
6715796c8dcSSimon Schubert   val->bitpos = 0;
6725796c8dcSSimon Schubert   val->bitsize = 0;
6735796c8dcSSimon Schubert   VALUE_REGNUM (val) = -1;
6745796c8dcSSimon Schubert   val->lazy = 1;
6755796c8dcSSimon Schubert   val->optimized_out = 0;
6765796c8dcSSimon Schubert   val->embedded_offset = 0;
6775796c8dcSSimon Schubert   val->pointed_to_offset = 0;
6785796c8dcSSimon Schubert   val->modifiable = 1;
6795796c8dcSSimon Schubert   val->initialized = 1;  /* Default to initialized.  */
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert   /* Values start out on the all_values chain.  */
6825796c8dcSSimon Schubert   val->reference_count = 1;
6835796c8dcSSimon Schubert 
6845796c8dcSSimon Schubert   return val;
6855796c8dcSSimon Schubert }
6865796c8dcSSimon Schubert 
6875796c8dcSSimon Schubert /* Allocate the contents of VAL if it has not been allocated yet.  */
6885796c8dcSSimon Schubert 
6895796c8dcSSimon Schubert void
allocate_value_contents(struct value * val)6905796c8dcSSimon Schubert allocate_value_contents (struct value *val)
6915796c8dcSSimon Schubert {
6925796c8dcSSimon Schubert   if (!val->contents)
6935796c8dcSSimon Schubert     val->contents = (gdb_byte *) xzalloc (TYPE_LENGTH (val->enclosing_type));
6945796c8dcSSimon Schubert }
6955796c8dcSSimon Schubert 
6965796c8dcSSimon Schubert /* Allocate a  value  and its contents for type TYPE.  */
6975796c8dcSSimon Schubert 
6985796c8dcSSimon Schubert struct value *
allocate_value(struct type * type)6995796c8dcSSimon Schubert allocate_value (struct type *type)
7005796c8dcSSimon Schubert {
7015796c8dcSSimon Schubert   struct value *val = allocate_value_lazy (type);
702cf7f2e2dSJohn Marino 
7035796c8dcSSimon Schubert   allocate_value_contents (val);
7045796c8dcSSimon Schubert   val->lazy = 0;
7055796c8dcSSimon Schubert   return val;
7065796c8dcSSimon Schubert }
7075796c8dcSSimon Schubert 
7085796c8dcSSimon Schubert /* Allocate a  value  that has the correct length
7095796c8dcSSimon Schubert    for COUNT repetitions of type TYPE.  */
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert struct value *
allocate_repeat_value(struct type * type,int count)7125796c8dcSSimon Schubert allocate_repeat_value (struct type *type, int count)
7135796c8dcSSimon Schubert {
7145796c8dcSSimon Schubert   int low_bound = current_language->string_lower_bound;		/* ??? */
7155796c8dcSSimon Schubert   /* FIXME-type-allocation: need a way to free this type when we are
7165796c8dcSSimon Schubert      done with it.  */
7175796c8dcSSimon Schubert   struct type *array_type
7185796c8dcSSimon Schubert     = lookup_array_range_type (type, low_bound, count + low_bound - 1);
719cf7f2e2dSJohn Marino 
7205796c8dcSSimon Schubert   return allocate_value (array_type);
7215796c8dcSSimon Schubert }
7225796c8dcSSimon Schubert 
7235796c8dcSSimon Schubert struct value *
allocate_computed_value(struct type * type,const struct lval_funcs * funcs,void * closure)7245796c8dcSSimon Schubert allocate_computed_value (struct type *type,
725a45ae5f8SJohn Marino                          const struct lval_funcs *funcs,
7265796c8dcSSimon Schubert                          void *closure)
7275796c8dcSSimon Schubert {
728c50c785cSJohn Marino   struct value *v = allocate_value_lazy (type);
729cf7f2e2dSJohn Marino 
7305796c8dcSSimon Schubert   VALUE_LVAL (v) = lval_computed;
7315796c8dcSSimon Schubert   v->location.computed.funcs = funcs;
7325796c8dcSSimon Schubert   v->location.computed.closure = closure;
7335796c8dcSSimon Schubert 
7345796c8dcSSimon Schubert   return v;
7355796c8dcSSimon Schubert }
7365796c8dcSSimon Schubert 
737a45ae5f8SJohn Marino /* Allocate NOT_LVAL value for type TYPE being OPTIMIZED_OUT.  */
738a45ae5f8SJohn Marino 
739a45ae5f8SJohn Marino struct value *
allocate_optimized_out_value(struct type * type)740a45ae5f8SJohn Marino allocate_optimized_out_value (struct type *type)
741a45ae5f8SJohn Marino {
742a45ae5f8SJohn Marino   struct value *retval = allocate_value_lazy (type);
743a45ae5f8SJohn Marino 
744a45ae5f8SJohn Marino   set_value_optimized_out (retval, 1);
745a45ae5f8SJohn Marino 
746a45ae5f8SJohn Marino   return retval;
747a45ae5f8SJohn Marino }
748a45ae5f8SJohn Marino 
7495796c8dcSSimon Schubert /* Accessor methods.  */
7505796c8dcSSimon Schubert 
7515796c8dcSSimon Schubert struct value *
value_next(struct value * value)7525796c8dcSSimon Schubert value_next (struct value *value)
7535796c8dcSSimon Schubert {
7545796c8dcSSimon Schubert   return value->next;
7555796c8dcSSimon Schubert }
7565796c8dcSSimon Schubert 
7575796c8dcSSimon Schubert struct type *
value_type(const struct value * value)758cf7f2e2dSJohn Marino value_type (const struct value *value)
7595796c8dcSSimon Schubert {
7605796c8dcSSimon Schubert   return value->type;
7615796c8dcSSimon Schubert }
7625796c8dcSSimon Schubert void
deprecated_set_value_type(struct value * value,struct type * type)7635796c8dcSSimon Schubert deprecated_set_value_type (struct value *value, struct type *type)
7645796c8dcSSimon Schubert {
7655796c8dcSSimon Schubert   value->type = type;
7665796c8dcSSimon Schubert }
7675796c8dcSSimon Schubert 
7685796c8dcSSimon Schubert int
value_offset(const struct value * value)769cf7f2e2dSJohn Marino value_offset (const struct value *value)
7705796c8dcSSimon Schubert {
7715796c8dcSSimon Schubert   return value->offset;
7725796c8dcSSimon Schubert }
7735796c8dcSSimon Schubert void
set_value_offset(struct value * value,int offset)7745796c8dcSSimon Schubert set_value_offset (struct value *value, int offset)
7755796c8dcSSimon Schubert {
7765796c8dcSSimon Schubert   value->offset = offset;
7775796c8dcSSimon Schubert }
7785796c8dcSSimon Schubert 
7795796c8dcSSimon Schubert int
value_bitpos(const struct value * value)780cf7f2e2dSJohn Marino value_bitpos (const struct value *value)
7815796c8dcSSimon Schubert {
7825796c8dcSSimon Schubert   return value->bitpos;
7835796c8dcSSimon Schubert }
7845796c8dcSSimon Schubert void
set_value_bitpos(struct value * value,int bit)7855796c8dcSSimon Schubert set_value_bitpos (struct value *value, int bit)
7865796c8dcSSimon Schubert {
7875796c8dcSSimon Schubert   value->bitpos = bit;
7885796c8dcSSimon Schubert }
7895796c8dcSSimon Schubert 
7905796c8dcSSimon Schubert int
value_bitsize(const struct value * value)791cf7f2e2dSJohn Marino value_bitsize (const struct value *value)
7925796c8dcSSimon Schubert {
7935796c8dcSSimon Schubert   return value->bitsize;
7945796c8dcSSimon Schubert }
7955796c8dcSSimon Schubert void
set_value_bitsize(struct value * value,int bit)7965796c8dcSSimon Schubert set_value_bitsize (struct value *value, int bit)
7975796c8dcSSimon Schubert {
7985796c8dcSSimon Schubert   value->bitsize = bit;
7995796c8dcSSimon Schubert }
8005796c8dcSSimon Schubert 
8015796c8dcSSimon Schubert struct value *
value_parent(struct value * value)8025796c8dcSSimon Schubert value_parent (struct value *value)
8035796c8dcSSimon Schubert {
8045796c8dcSSimon Schubert   return value->parent;
8055796c8dcSSimon Schubert }
8065796c8dcSSimon Schubert 
807*ef5ccd6cSJohn Marino /* See value.h.  */
808*ef5ccd6cSJohn Marino 
809*ef5ccd6cSJohn Marino void
set_value_parent(struct value * value,struct value * parent)810*ef5ccd6cSJohn Marino set_value_parent (struct value *value, struct value *parent)
811*ef5ccd6cSJohn Marino {
812*ef5ccd6cSJohn Marino   value->parent = parent;
813*ef5ccd6cSJohn Marino }
814*ef5ccd6cSJohn Marino 
8155796c8dcSSimon Schubert gdb_byte *
value_contents_raw(struct value * value)8165796c8dcSSimon Schubert value_contents_raw (struct value *value)
8175796c8dcSSimon Schubert {
8185796c8dcSSimon Schubert   allocate_value_contents (value);
8195796c8dcSSimon Schubert   return value->contents + value->embedded_offset;
8205796c8dcSSimon Schubert }
8215796c8dcSSimon Schubert 
8225796c8dcSSimon Schubert gdb_byte *
value_contents_all_raw(struct value * value)8235796c8dcSSimon Schubert value_contents_all_raw (struct value *value)
8245796c8dcSSimon Schubert {
8255796c8dcSSimon Schubert   allocate_value_contents (value);
8265796c8dcSSimon Schubert   return value->contents;
8275796c8dcSSimon Schubert }
8285796c8dcSSimon Schubert 
8295796c8dcSSimon Schubert struct type *
value_enclosing_type(struct value * value)8305796c8dcSSimon Schubert value_enclosing_type (struct value *value)
8315796c8dcSSimon Schubert {
8325796c8dcSSimon Schubert   return value->enclosing_type;
8335796c8dcSSimon Schubert }
8345796c8dcSSimon Schubert 
835*ef5ccd6cSJohn Marino /* Look at value.h for description.  */
836*ef5ccd6cSJohn Marino 
837*ef5ccd6cSJohn Marino struct type *
value_actual_type(struct value * value,int resolve_simple_types,int * real_type_found)838*ef5ccd6cSJohn Marino value_actual_type (struct value *value, int resolve_simple_types,
839*ef5ccd6cSJohn Marino 		   int *real_type_found)
840*ef5ccd6cSJohn Marino {
841*ef5ccd6cSJohn Marino   struct value_print_options opts;
842*ef5ccd6cSJohn Marino   struct type *result;
843*ef5ccd6cSJohn Marino 
844*ef5ccd6cSJohn Marino   get_user_print_options (&opts);
845*ef5ccd6cSJohn Marino 
846*ef5ccd6cSJohn Marino   if (real_type_found)
847*ef5ccd6cSJohn Marino     *real_type_found = 0;
848*ef5ccd6cSJohn Marino   result = value_type (value);
849*ef5ccd6cSJohn Marino   if (opts.objectprint)
850*ef5ccd6cSJohn Marino     {
851*ef5ccd6cSJohn Marino       /* If result's target type is TYPE_CODE_STRUCT, proceed to
852*ef5ccd6cSJohn Marino 	 fetch its rtti type.  */
853*ef5ccd6cSJohn Marino       if ((TYPE_CODE (result) == TYPE_CODE_PTR
854*ef5ccd6cSJohn Marino 	  || TYPE_CODE (result) == TYPE_CODE_REF)
855*ef5ccd6cSJohn Marino 	  && TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (result)))
856*ef5ccd6cSJohn Marino 	     == TYPE_CODE_STRUCT)
857*ef5ccd6cSJohn Marino         {
858*ef5ccd6cSJohn Marino           struct type *real_type;
859*ef5ccd6cSJohn Marino 
860*ef5ccd6cSJohn Marino           real_type = value_rtti_indirect_type (value, NULL, NULL, NULL);
861*ef5ccd6cSJohn Marino           if (real_type)
862*ef5ccd6cSJohn Marino             {
863*ef5ccd6cSJohn Marino               if (real_type_found)
864*ef5ccd6cSJohn Marino                 *real_type_found = 1;
865*ef5ccd6cSJohn Marino               result = real_type;
866*ef5ccd6cSJohn Marino             }
867*ef5ccd6cSJohn Marino         }
868*ef5ccd6cSJohn Marino       else if (resolve_simple_types)
869*ef5ccd6cSJohn Marino         {
870*ef5ccd6cSJohn Marino           if (real_type_found)
871*ef5ccd6cSJohn Marino             *real_type_found = 1;
872*ef5ccd6cSJohn Marino           result = value_enclosing_type (value);
873*ef5ccd6cSJohn Marino         }
874*ef5ccd6cSJohn Marino     }
875*ef5ccd6cSJohn Marino 
876*ef5ccd6cSJohn Marino   return result;
877*ef5ccd6cSJohn Marino }
878*ef5ccd6cSJohn Marino 
879cf7f2e2dSJohn Marino static void
require_not_optimized_out(const struct value * value)880c50c785cSJohn Marino require_not_optimized_out (const struct value *value)
881cf7f2e2dSJohn Marino {
882cf7f2e2dSJohn Marino   if (value->optimized_out)
883cf7f2e2dSJohn Marino     error (_("value has been optimized out"));
884cf7f2e2dSJohn Marino }
885cf7f2e2dSJohn Marino 
886c50c785cSJohn Marino static void
require_available(const struct value * value)887c50c785cSJohn Marino require_available (const struct value *value)
888c50c785cSJohn Marino {
889c50c785cSJohn Marino   if (!VEC_empty (range_s, value->unavailable))
890c50c785cSJohn Marino     throw_error (NOT_AVAILABLE_ERROR, _("value is not available"));
891c50c785cSJohn Marino }
892c50c785cSJohn Marino 
8935796c8dcSSimon Schubert const gdb_byte *
value_contents_for_printing(struct value * value)894cf7f2e2dSJohn Marino value_contents_for_printing (struct value *value)
8955796c8dcSSimon Schubert {
8965796c8dcSSimon Schubert   if (value->lazy)
8975796c8dcSSimon Schubert     value_fetch_lazy (value);
8985796c8dcSSimon Schubert   return value->contents;
8995796c8dcSSimon Schubert }
9005796c8dcSSimon Schubert 
901cf7f2e2dSJohn Marino const gdb_byte *
value_contents_for_printing_const(const struct value * value)902c50c785cSJohn Marino value_contents_for_printing_const (const struct value *value)
903c50c785cSJohn Marino {
904c50c785cSJohn Marino   gdb_assert (!value->lazy);
905c50c785cSJohn Marino   return value->contents;
906c50c785cSJohn Marino }
907c50c785cSJohn Marino 
908c50c785cSJohn Marino const gdb_byte *
value_contents_all(struct value * value)909cf7f2e2dSJohn Marino value_contents_all (struct value *value)
910cf7f2e2dSJohn Marino {
911cf7f2e2dSJohn Marino   const gdb_byte *result = value_contents_for_printing (value);
912cf7f2e2dSJohn Marino   require_not_optimized_out (value);
913c50c785cSJohn Marino   require_available (value);
914cf7f2e2dSJohn Marino   return result;
915cf7f2e2dSJohn Marino }
916cf7f2e2dSJohn Marino 
917c50c785cSJohn Marino /* Copy LENGTH bytes of SRC value's (all) contents
918c50c785cSJohn Marino    (value_contents_all) starting at SRC_OFFSET, into DST value's (all)
919c50c785cSJohn Marino    contents, starting at DST_OFFSET.  If unavailable contents are
920c50c785cSJohn Marino    being copied from SRC, the corresponding DST contents are marked
921c50c785cSJohn Marino    unavailable accordingly.  Neither DST nor SRC may be lazy
922c50c785cSJohn Marino    values.
923c50c785cSJohn Marino 
924c50c785cSJohn Marino    It is assumed the contents of DST in the [DST_OFFSET,
925c50c785cSJohn Marino    DST_OFFSET+LENGTH) range are wholly available.  */
926c50c785cSJohn Marino 
927c50c785cSJohn Marino void
value_contents_copy_raw(struct value * dst,int dst_offset,struct value * src,int src_offset,int length)928c50c785cSJohn Marino value_contents_copy_raw (struct value *dst, int dst_offset,
929c50c785cSJohn Marino 			 struct value *src, int src_offset, int length)
930c50c785cSJohn Marino {
931c50c785cSJohn Marino   range_s *r;
932c50c785cSJohn Marino   int i;
933c50c785cSJohn Marino 
934c50c785cSJohn Marino   /* A lazy DST would make that this copy operation useless, since as
935c50c785cSJohn Marino      soon as DST's contents were un-lazied (by a later value_contents
936c50c785cSJohn Marino      call, say), the contents would be overwritten.  A lazy SRC would
937c50c785cSJohn Marino      mean we'd be copying garbage.  */
938c50c785cSJohn Marino   gdb_assert (!dst->lazy && !src->lazy);
939c50c785cSJohn Marino 
940c50c785cSJohn Marino   /* The overwritten DST range gets unavailability ORed in, not
941c50c785cSJohn Marino      replaced.  Make sure to remember to implement replacing if it
942c50c785cSJohn Marino      turns out actually necessary.  */
943c50c785cSJohn Marino   gdb_assert (value_bytes_available (dst, dst_offset, length));
944c50c785cSJohn Marino 
945c50c785cSJohn Marino   /* Copy the data.  */
946c50c785cSJohn Marino   memcpy (value_contents_all_raw (dst) + dst_offset,
947c50c785cSJohn Marino 	  value_contents_all_raw (src) + src_offset,
948c50c785cSJohn Marino 	  length);
949c50c785cSJohn Marino 
950c50c785cSJohn Marino   /* Copy the meta-data, adjusted.  */
951c50c785cSJohn Marino   for (i = 0; VEC_iterate (range_s, src->unavailable, i, r); i++)
952c50c785cSJohn Marino     {
953c50c785cSJohn Marino       ULONGEST h, l;
954c50c785cSJohn Marino 
955c50c785cSJohn Marino       l = max (r->offset, src_offset);
956c50c785cSJohn Marino       h = min (r->offset + r->length, src_offset + length);
957c50c785cSJohn Marino 
958c50c785cSJohn Marino       if (l < h)
959c50c785cSJohn Marino 	mark_value_bytes_unavailable (dst,
960c50c785cSJohn Marino 				      dst_offset + (l - src_offset),
961c50c785cSJohn Marino 				      h - l);
962c50c785cSJohn Marino     }
963c50c785cSJohn Marino }
964c50c785cSJohn Marino 
965c50c785cSJohn Marino /* Copy LENGTH bytes of SRC value's (all) contents
966c50c785cSJohn Marino    (value_contents_all) starting at SRC_OFFSET byte, into DST value's
967c50c785cSJohn Marino    (all) contents, starting at DST_OFFSET.  If unavailable contents
968c50c785cSJohn Marino    are being copied from SRC, the corresponding DST contents are
969c50c785cSJohn Marino    marked unavailable accordingly.  DST must not be lazy.  If SRC is
970c50c785cSJohn Marino    lazy, it will be fetched now.  If SRC is not valid (is optimized
971c50c785cSJohn Marino    out), an error is thrown.
972c50c785cSJohn Marino 
973c50c785cSJohn Marino    It is assumed the contents of DST in the [DST_OFFSET,
974c50c785cSJohn Marino    DST_OFFSET+LENGTH) range are wholly available.  */
975c50c785cSJohn Marino 
976c50c785cSJohn Marino void
value_contents_copy(struct value * dst,int dst_offset,struct value * src,int src_offset,int length)977c50c785cSJohn Marino value_contents_copy (struct value *dst, int dst_offset,
978c50c785cSJohn Marino 		     struct value *src, int src_offset, int length)
979c50c785cSJohn Marino {
980c50c785cSJohn Marino   require_not_optimized_out (src);
981c50c785cSJohn Marino 
982c50c785cSJohn Marino   if (src->lazy)
983c50c785cSJohn Marino     value_fetch_lazy (src);
984c50c785cSJohn Marino 
985c50c785cSJohn Marino   value_contents_copy_raw (dst, dst_offset, src, src_offset, length);
986c50c785cSJohn Marino }
987c50c785cSJohn Marino 
9885796c8dcSSimon Schubert int
value_lazy(struct value * value)9895796c8dcSSimon Schubert value_lazy (struct value *value)
9905796c8dcSSimon Schubert {
9915796c8dcSSimon Schubert   return value->lazy;
9925796c8dcSSimon Schubert }
9935796c8dcSSimon Schubert 
9945796c8dcSSimon Schubert void
set_value_lazy(struct value * value,int val)9955796c8dcSSimon Schubert set_value_lazy (struct value *value, int val)
9965796c8dcSSimon Schubert {
9975796c8dcSSimon Schubert   value->lazy = val;
9985796c8dcSSimon Schubert }
9995796c8dcSSimon Schubert 
10005796c8dcSSimon Schubert int
value_stack(struct value * value)10015796c8dcSSimon Schubert value_stack (struct value *value)
10025796c8dcSSimon Schubert {
10035796c8dcSSimon Schubert   return value->stack;
10045796c8dcSSimon Schubert }
10055796c8dcSSimon Schubert 
10065796c8dcSSimon Schubert void
set_value_stack(struct value * value,int val)10075796c8dcSSimon Schubert set_value_stack (struct value *value, int val)
10085796c8dcSSimon Schubert {
10095796c8dcSSimon Schubert   value->stack = val;
10105796c8dcSSimon Schubert }
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert const gdb_byte *
value_contents(struct value * value)10135796c8dcSSimon Schubert value_contents (struct value *value)
10145796c8dcSSimon Schubert {
1015cf7f2e2dSJohn Marino   const gdb_byte *result = value_contents_writeable (value);
1016cf7f2e2dSJohn Marino   require_not_optimized_out (value);
1017c50c785cSJohn Marino   require_available (value);
1018cf7f2e2dSJohn Marino   return result;
10195796c8dcSSimon Schubert }
10205796c8dcSSimon Schubert 
10215796c8dcSSimon Schubert gdb_byte *
value_contents_writeable(struct value * value)10225796c8dcSSimon Schubert value_contents_writeable (struct value *value)
10235796c8dcSSimon Schubert {
10245796c8dcSSimon Schubert   if (value->lazy)
10255796c8dcSSimon Schubert     value_fetch_lazy (value);
10265796c8dcSSimon Schubert   return value_contents_raw (value);
10275796c8dcSSimon Schubert }
10285796c8dcSSimon Schubert 
10295796c8dcSSimon Schubert /* Return non-zero if VAL1 and VAL2 have the same contents.  Note that
10305796c8dcSSimon Schubert    this function is different from value_equal; in C the operator ==
10315796c8dcSSimon Schubert    can return 0 even if the two values being compared are equal.  */
10325796c8dcSSimon Schubert 
10335796c8dcSSimon Schubert int
value_contents_equal(struct value * val1,struct value * val2)10345796c8dcSSimon Schubert value_contents_equal (struct value *val1, struct value *val2)
10355796c8dcSSimon Schubert {
10365796c8dcSSimon Schubert   struct type *type1;
10375796c8dcSSimon Schubert   struct type *type2;
10385796c8dcSSimon Schubert 
10395796c8dcSSimon Schubert   type1 = check_typedef (value_type (val1));
10405796c8dcSSimon Schubert   type2 = check_typedef (value_type (val2));
1041*ef5ccd6cSJohn Marino   if (TYPE_LENGTH (type1) != TYPE_LENGTH (type2))
10425796c8dcSSimon Schubert     return 0;
10435796c8dcSSimon Schubert 
1044*ef5ccd6cSJohn Marino   return (memcmp (value_contents (val1), value_contents (val2),
1045*ef5ccd6cSJohn Marino 		  TYPE_LENGTH (type1)) == 0);
10465796c8dcSSimon Schubert }
10475796c8dcSSimon Schubert 
10485796c8dcSSimon Schubert int
value_optimized_out(struct value * value)10495796c8dcSSimon Schubert value_optimized_out (struct value *value)
10505796c8dcSSimon Schubert {
10515796c8dcSSimon Schubert   return value->optimized_out;
10525796c8dcSSimon Schubert }
10535796c8dcSSimon Schubert 
10545796c8dcSSimon Schubert void
set_value_optimized_out(struct value * value,int val)10555796c8dcSSimon Schubert set_value_optimized_out (struct value *value, int val)
10565796c8dcSSimon Schubert {
10575796c8dcSSimon Schubert   value->optimized_out = val;
10585796c8dcSSimon Schubert }
10595796c8dcSSimon Schubert 
10605796c8dcSSimon Schubert int
value_entirely_optimized_out(const struct value * value)1061cf7f2e2dSJohn Marino value_entirely_optimized_out (const struct value *value)
1062cf7f2e2dSJohn Marino {
1063cf7f2e2dSJohn Marino   if (!value->optimized_out)
1064cf7f2e2dSJohn Marino     return 0;
1065cf7f2e2dSJohn Marino   if (value->lval != lval_computed
1066c50c785cSJohn Marino       || !value->location.computed.funcs->check_any_valid)
1067cf7f2e2dSJohn Marino     return 1;
1068cf7f2e2dSJohn Marino   return !value->location.computed.funcs->check_any_valid (value);
1069cf7f2e2dSJohn Marino }
1070cf7f2e2dSJohn Marino 
1071cf7f2e2dSJohn Marino int
value_bits_valid(const struct value * value,int offset,int length)1072cf7f2e2dSJohn Marino value_bits_valid (const struct value *value, int offset, int length)
1073cf7f2e2dSJohn Marino {
1074c50c785cSJohn Marino   if (!value->optimized_out)
1075cf7f2e2dSJohn Marino     return 1;
1076cf7f2e2dSJohn Marino   if (value->lval != lval_computed
1077cf7f2e2dSJohn Marino       || !value->location.computed.funcs->check_validity)
1078cf7f2e2dSJohn Marino     return 0;
1079cf7f2e2dSJohn Marino   return value->location.computed.funcs->check_validity (value, offset,
1080cf7f2e2dSJohn Marino 							 length);
1081cf7f2e2dSJohn Marino }
1082cf7f2e2dSJohn Marino 
1083cf7f2e2dSJohn Marino int
value_bits_synthetic_pointer(const struct value * value,int offset,int length)1084c50c785cSJohn Marino value_bits_synthetic_pointer (const struct value *value,
1085c50c785cSJohn Marino 			      int offset, int length)
1086c50c785cSJohn Marino {
1087c50c785cSJohn Marino   if (value->lval != lval_computed
1088c50c785cSJohn Marino       || !value->location.computed.funcs->check_synthetic_pointer)
1089c50c785cSJohn Marino     return 0;
1090c50c785cSJohn Marino   return value->location.computed.funcs->check_synthetic_pointer (value,
1091c50c785cSJohn Marino 								  offset,
1092c50c785cSJohn Marino 								  length);
1093c50c785cSJohn Marino }
1094c50c785cSJohn Marino 
1095c50c785cSJohn Marino int
value_embedded_offset(struct value * value)10965796c8dcSSimon Schubert value_embedded_offset (struct value *value)
10975796c8dcSSimon Schubert {
10985796c8dcSSimon Schubert   return value->embedded_offset;
10995796c8dcSSimon Schubert }
11005796c8dcSSimon Schubert 
11015796c8dcSSimon Schubert void
set_value_embedded_offset(struct value * value,int val)11025796c8dcSSimon Schubert set_value_embedded_offset (struct value *value, int val)
11035796c8dcSSimon Schubert {
11045796c8dcSSimon Schubert   value->embedded_offset = val;
11055796c8dcSSimon Schubert }
11065796c8dcSSimon Schubert 
11075796c8dcSSimon Schubert int
value_pointed_to_offset(struct value * value)11085796c8dcSSimon Schubert value_pointed_to_offset (struct value *value)
11095796c8dcSSimon Schubert {
11105796c8dcSSimon Schubert   return value->pointed_to_offset;
11115796c8dcSSimon Schubert }
11125796c8dcSSimon Schubert 
11135796c8dcSSimon Schubert void
set_value_pointed_to_offset(struct value * value,int val)11145796c8dcSSimon Schubert set_value_pointed_to_offset (struct value *value, int val)
11155796c8dcSSimon Schubert {
11165796c8dcSSimon Schubert   value->pointed_to_offset = val;
11175796c8dcSSimon Schubert }
11185796c8dcSSimon Schubert 
1119a45ae5f8SJohn Marino const struct lval_funcs *
value_computed_funcs(const struct value * v)1120a45ae5f8SJohn Marino value_computed_funcs (const struct value *v)
11215796c8dcSSimon Schubert {
1122a45ae5f8SJohn Marino   gdb_assert (value_lval_const (v) == lval_computed);
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert   return v->location.computed.funcs;
11255796c8dcSSimon Schubert }
11265796c8dcSSimon Schubert 
11275796c8dcSSimon Schubert void *
value_computed_closure(const struct value * v)1128cf7f2e2dSJohn Marino value_computed_closure (const struct value *v)
11295796c8dcSSimon Schubert {
1130cf7f2e2dSJohn Marino   gdb_assert (v->lval == lval_computed);
11315796c8dcSSimon Schubert 
11325796c8dcSSimon Schubert   return v->location.computed.closure;
11335796c8dcSSimon Schubert }
11345796c8dcSSimon Schubert 
11355796c8dcSSimon Schubert enum lval_type *
deprecated_value_lval_hack(struct value * value)11365796c8dcSSimon Schubert deprecated_value_lval_hack (struct value *value)
11375796c8dcSSimon Schubert {
11385796c8dcSSimon Schubert   return &value->lval;
11395796c8dcSSimon Schubert }
11405796c8dcSSimon Schubert 
1141a45ae5f8SJohn Marino enum lval_type
value_lval_const(const struct value * value)1142a45ae5f8SJohn Marino value_lval_const (const struct value *value)
1143a45ae5f8SJohn Marino {
1144a45ae5f8SJohn Marino   return value->lval;
1145a45ae5f8SJohn Marino }
1146a45ae5f8SJohn Marino 
11475796c8dcSSimon Schubert CORE_ADDR
value_address(const struct value * value)1148c50c785cSJohn Marino value_address (const struct value *value)
11495796c8dcSSimon Schubert {
11505796c8dcSSimon Schubert   if (value->lval == lval_internalvar
11515796c8dcSSimon Schubert       || value->lval == lval_internalvar_component)
11525796c8dcSSimon Schubert     return 0;
1153*ef5ccd6cSJohn Marino   if (value->parent != NULL)
1154*ef5ccd6cSJohn Marino     return value_address (value->parent) + value->offset;
1155*ef5ccd6cSJohn Marino   else
11565796c8dcSSimon Schubert     return value->location.address + value->offset;
11575796c8dcSSimon Schubert }
11585796c8dcSSimon Schubert 
11595796c8dcSSimon Schubert CORE_ADDR
value_raw_address(struct value * value)11605796c8dcSSimon Schubert value_raw_address (struct value *value)
11615796c8dcSSimon Schubert {
11625796c8dcSSimon Schubert   if (value->lval == lval_internalvar
11635796c8dcSSimon Schubert       || value->lval == lval_internalvar_component)
11645796c8dcSSimon Schubert     return 0;
11655796c8dcSSimon Schubert   return value->location.address;
11665796c8dcSSimon Schubert }
11675796c8dcSSimon Schubert 
11685796c8dcSSimon Schubert void
set_value_address(struct value * value,CORE_ADDR addr)11695796c8dcSSimon Schubert set_value_address (struct value *value, CORE_ADDR addr)
11705796c8dcSSimon Schubert {
11715796c8dcSSimon Schubert   gdb_assert (value->lval != lval_internalvar
11725796c8dcSSimon Schubert 	      && value->lval != lval_internalvar_component);
11735796c8dcSSimon Schubert   value->location.address = addr;
11745796c8dcSSimon Schubert }
11755796c8dcSSimon Schubert 
11765796c8dcSSimon Schubert struct internalvar **
deprecated_value_internalvar_hack(struct value * value)11775796c8dcSSimon Schubert deprecated_value_internalvar_hack (struct value *value)
11785796c8dcSSimon Schubert {
11795796c8dcSSimon Schubert   return &value->location.internalvar;
11805796c8dcSSimon Schubert }
11815796c8dcSSimon Schubert 
11825796c8dcSSimon Schubert struct frame_id *
deprecated_value_frame_id_hack(struct value * value)11835796c8dcSSimon Schubert deprecated_value_frame_id_hack (struct value *value)
11845796c8dcSSimon Schubert {
11855796c8dcSSimon Schubert   return &value->frame_id;
11865796c8dcSSimon Schubert }
11875796c8dcSSimon Schubert 
11885796c8dcSSimon Schubert short *
deprecated_value_regnum_hack(struct value * value)11895796c8dcSSimon Schubert deprecated_value_regnum_hack (struct value *value)
11905796c8dcSSimon Schubert {
11915796c8dcSSimon Schubert   return &value->regnum;
11925796c8dcSSimon Schubert }
11935796c8dcSSimon Schubert 
11945796c8dcSSimon Schubert int
deprecated_value_modifiable(struct value * value)11955796c8dcSSimon Schubert deprecated_value_modifiable (struct value *value)
11965796c8dcSSimon Schubert {
11975796c8dcSSimon Schubert   return value->modifiable;
11985796c8dcSSimon Schubert }
11995796c8dcSSimon Schubert 
12005796c8dcSSimon Schubert /* Return a mark in the value chain.  All values allocated after the
12015796c8dcSSimon Schubert    mark is obtained (except for those released) are subject to being freed
12025796c8dcSSimon Schubert    if a subsequent value_free_to_mark is passed the mark.  */
12035796c8dcSSimon Schubert struct value *
value_mark(void)12045796c8dcSSimon Schubert value_mark (void)
12055796c8dcSSimon Schubert {
12065796c8dcSSimon Schubert   return all_values;
12075796c8dcSSimon Schubert }
12085796c8dcSSimon Schubert 
12095796c8dcSSimon Schubert /* Take a reference to VAL.  VAL will not be deallocated until all
12105796c8dcSSimon Schubert    references are released.  */
12115796c8dcSSimon Schubert 
12125796c8dcSSimon Schubert void
value_incref(struct value * val)12135796c8dcSSimon Schubert value_incref (struct value *val)
12145796c8dcSSimon Schubert {
12155796c8dcSSimon Schubert   val->reference_count++;
12165796c8dcSSimon Schubert }
12175796c8dcSSimon Schubert 
12185796c8dcSSimon Schubert /* Release a reference to VAL, which was acquired with value_incref.
12195796c8dcSSimon Schubert    This function is also called to deallocate values from the value
12205796c8dcSSimon Schubert    chain.  */
12215796c8dcSSimon Schubert 
12225796c8dcSSimon Schubert void
value_free(struct value * val)12235796c8dcSSimon Schubert value_free (struct value *val)
12245796c8dcSSimon Schubert {
12255796c8dcSSimon Schubert   if (val)
12265796c8dcSSimon Schubert     {
12275796c8dcSSimon Schubert       gdb_assert (val->reference_count > 0);
12285796c8dcSSimon Schubert       val->reference_count--;
12295796c8dcSSimon Schubert       if (val->reference_count > 0)
12305796c8dcSSimon Schubert 	return;
12315796c8dcSSimon Schubert 
12325796c8dcSSimon Schubert       /* If there's an associated parent value, drop our reference to
12335796c8dcSSimon Schubert 	 it.  */
12345796c8dcSSimon Schubert       if (val->parent != NULL)
12355796c8dcSSimon Schubert 	value_free (val->parent);
12365796c8dcSSimon Schubert 
12375796c8dcSSimon Schubert       if (VALUE_LVAL (val) == lval_computed)
12385796c8dcSSimon Schubert 	{
1239a45ae5f8SJohn Marino 	  const struct lval_funcs *funcs = val->location.computed.funcs;
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert 	  if (funcs->free_closure)
12425796c8dcSSimon Schubert 	    funcs->free_closure (val);
12435796c8dcSSimon Schubert 	}
12445796c8dcSSimon Schubert 
12455796c8dcSSimon Schubert       xfree (val->contents);
1246c50c785cSJohn Marino       VEC_free (range_s, val->unavailable);
12475796c8dcSSimon Schubert     }
12485796c8dcSSimon Schubert   xfree (val);
12495796c8dcSSimon Schubert }
12505796c8dcSSimon Schubert 
12515796c8dcSSimon Schubert /* Free all values allocated since MARK was obtained by value_mark
12525796c8dcSSimon Schubert    (except for those released).  */
12535796c8dcSSimon Schubert void
value_free_to_mark(struct value * mark)12545796c8dcSSimon Schubert value_free_to_mark (struct value *mark)
12555796c8dcSSimon Schubert {
12565796c8dcSSimon Schubert   struct value *val;
12575796c8dcSSimon Schubert   struct value *next;
12585796c8dcSSimon Schubert 
12595796c8dcSSimon Schubert   for (val = all_values; val && val != mark; val = next)
12605796c8dcSSimon Schubert     {
12615796c8dcSSimon Schubert       next = val->next;
1262*ef5ccd6cSJohn Marino       val->released = 1;
12635796c8dcSSimon Schubert       value_free (val);
12645796c8dcSSimon Schubert     }
12655796c8dcSSimon Schubert   all_values = val;
12665796c8dcSSimon Schubert }
12675796c8dcSSimon Schubert 
12685796c8dcSSimon Schubert /* Free all the values that have been allocated (except for those released).
12695796c8dcSSimon Schubert    Call after each command, successful or not.
12705796c8dcSSimon Schubert    In practice this is called before each command, which is sufficient.  */
12715796c8dcSSimon Schubert 
12725796c8dcSSimon Schubert void
free_all_values(void)12735796c8dcSSimon Schubert free_all_values (void)
12745796c8dcSSimon Schubert {
12755796c8dcSSimon Schubert   struct value *val;
12765796c8dcSSimon Schubert   struct value *next;
12775796c8dcSSimon Schubert 
12785796c8dcSSimon Schubert   for (val = all_values; val; val = next)
12795796c8dcSSimon Schubert     {
12805796c8dcSSimon Schubert       next = val->next;
1281*ef5ccd6cSJohn Marino       val->released = 1;
12825796c8dcSSimon Schubert       value_free (val);
12835796c8dcSSimon Schubert     }
12845796c8dcSSimon Schubert 
12855796c8dcSSimon Schubert   all_values = 0;
12865796c8dcSSimon Schubert }
12875796c8dcSSimon Schubert 
1288cf7f2e2dSJohn Marino /* Frees all the elements in a chain of values.  */
1289cf7f2e2dSJohn Marino 
1290cf7f2e2dSJohn Marino void
free_value_chain(struct value * v)1291cf7f2e2dSJohn Marino free_value_chain (struct value *v)
1292cf7f2e2dSJohn Marino {
1293cf7f2e2dSJohn Marino   struct value *next;
1294cf7f2e2dSJohn Marino 
1295cf7f2e2dSJohn Marino   for (; v; v = next)
1296cf7f2e2dSJohn Marino     {
1297cf7f2e2dSJohn Marino       next = value_next (v);
1298cf7f2e2dSJohn Marino       value_free (v);
1299cf7f2e2dSJohn Marino     }
1300cf7f2e2dSJohn Marino }
1301cf7f2e2dSJohn Marino 
13025796c8dcSSimon Schubert /* Remove VAL from the chain all_values
13035796c8dcSSimon Schubert    so it will not be freed automatically.  */
13045796c8dcSSimon Schubert 
13055796c8dcSSimon Schubert void
release_value(struct value * val)13065796c8dcSSimon Schubert release_value (struct value *val)
13075796c8dcSSimon Schubert {
13085796c8dcSSimon Schubert   struct value *v;
13095796c8dcSSimon Schubert 
13105796c8dcSSimon Schubert   if (all_values == val)
13115796c8dcSSimon Schubert     {
13125796c8dcSSimon Schubert       all_values = val->next;
1313c50c785cSJohn Marino       val->next = NULL;
1314*ef5ccd6cSJohn Marino       val->released = 1;
13155796c8dcSSimon Schubert       return;
13165796c8dcSSimon Schubert     }
13175796c8dcSSimon Schubert 
13185796c8dcSSimon Schubert   for (v = all_values; v; v = v->next)
13195796c8dcSSimon Schubert     {
13205796c8dcSSimon Schubert       if (v->next == val)
13215796c8dcSSimon Schubert 	{
13225796c8dcSSimon Schubert 	  v->next = val->next;
1323c50c785cSJohn Marino 	  val->next = NULL;
1324*ef5ccd6cSJohn Marino 	  val->released = 1;
13255796c8dcSSimon Schubert 	  break;
13265796c8dcSSimon Schubert 	}
13275796c8dcSSimon Schubert     }
13285796c8dcSSimon Schubert }
13295796c8dcSSimon Schubert 
1330*ef5ccd6cSJohn Marino /* If the value is not already released, release it.
1331*ef5ccd6cSJohn Marino    If the value is already released, increment its reference count.
1332*ef5ccd6cSJohn Marino    That is, this function ensures that the value is released from the
1333*ef5ccd6cSJohn Marino    value chain and that the caller owns a reference to it.  */
1334*ef5ccd6cSJohn Marino 
1335*ef5ccd6cSJohn Marino void
release_value_or_incref(struct value * val)1336*ef5ccd6cSJohn Marino release_value_or_incref (struct value *val)
1337*ef5ccd6cSJohn Marino {
1338*ef5ccd6cSJohn Marino   if (val->released)
1339*ef5ccd6cSJohn Marino     value_incref (val);
1340*ef5ccd6cSJohn Marino   else
1341*ef5ccd6cSJohn Marino     release_value (val);
1342*ef5ccd6cSJohn Marino }
1343*ef5ccd6cSJohn Marino 
13445796c8dcSSimon Schubert /* Release all values up to mark  */
13455796c8dcSSimon Schubert struct value *
value_release_to_mark(struct value * mark)13465796c8dcSSimon Schubert value_release_to_mark (struct value *mark)
13475796c8dcSSimon Schubert {
13485796c8dcSSimon Schubert   struct value *val;
13495796c8dcSSimon Schubert   struct value *next;
13505796c8dcSSimon Schubert 
13515796c8dcSSimon Schubert   for (val = next = all_values; next; next = next->next)
1352*ef5ccd6cSJohn Marino     {
13535796c8dcSSimon Schubert       if (next->next == mark)
13545796c8dcSSimon Schubert 	{
13555796c8dcSSimon Schubert 	  all_values = next->next;
13565796c8dcSSimon Schubert 	  next->next = NULL;
13575796c8dcSSimon Schubert 	  return val;
13585796c8dcSSimon Schubert 	}
1359*ef5ccd6cSJohn Marino       next->released = 1;
1360*ef5ccd6cSJohn Marino     }
13615796c8dcSSimon Schubert   all_values = 0;
13625796c8dcSSimon Schubert   return val;
13635796c8dcSSimon Schubert }
13645796c8dcSSimon Schubert 
13655796c8dcSSimon Schubert /* Return a copy of the value ARG.
13665796c8dcSSimon Schubert    It contains the same contents, for same memory address,
13675796c8dcSSimon Schubert    but it's a different block of storage.  */
13685796c8dcSSimon Schubert 
13695796c8dcSSimon Schubert struct value *
value_copy(struct value * arg)13705796c8dcSSimon Schubert value_copy (struct value *arg)
13715796c8dcSSimon Schubert {
13725796c8dcSSimon Schubert   struct type *encl_type = value_enclosing_type (arg);
13735796c8dcSSimon Schubert   struct value *val;
13745796c8dcSSimon Schubert 
13755796c8dcSSimon Schubert   if (value_lazy (arg))
13765796c8dcSSimon Schubert     val = allocate_value_lazy (encl_type);
13775796c8dcSSimon Schubert   else
13785796c8dcSSimon Schubert     val = allocate_value (encl_type);
13795796c8dcSSimon Schubert   val->type = arg->type;
13805796c8dcSSimon Schubert   VALUE_LVAL (val) = VALUE_LVAL (arg);
13815796c8dcSSimon Schubert   val->location = arg->location;
13825796c8dcSSimon Schubert   val->offset = arg->offset;
13835796c8dcSSimon Schubert   val->bitpos = arg->bitpos;
13845796c8dcSSimon Schubert   val->bitsize = arg->bitsize;
13855796c8dcSSimon Schubert   VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
13865796c8dcSSimon Schubert   VALUE_REGNUM (val) = VALUE_REGNUM (arg);
13875796c8dcSSimon Schubert   val->lazy = arg->lazy;
13885796c8dcSSimon Schubert   val->optimized_out = arg->optimized_out;
13895796c8dcSSimon Schubert   val->embedded_offset = value_embedded_offset (arg);
13905796c8dcSSimon Schubert   val->pointed_to_offset = arg->pointed_to_offset;
13915796c8dcSSimon Schubert   val->modifiable = arg->modifiable;
13925796c8dcSSimon Schubert   if (!value_lazy (val))
13935796c8dcSSimon Schubert     {
13945796c8dcSSimon Schubert       memcpy (value_contents_all_raw (val), value_contents_all_raw (arg),
13955796c8dcSSimon Schubert 	      TYPE_LENGTH (value_enclosing_type (arg)));
13965796c8dcSSimon Schubert 
13975796c8dcSSimon Schubert     }
1398c50c785cSJohn Marino   val->unavailable = VEC_copy (range_s, arg->unavailable);
13995796c8dcSSimon Schubert   val->parent = arg->parent;
14005796c8dcSSimon Schubert   if (val->parent)
14015796c8dcSSimon Schubert     value_incref (val->parent);
14025796c8dcSSimon Schubert   if (VALUE_LVAL (val) == lval_computed)
14035796c8dcSSimon Schubert     {
1404a45ae5f8SJohn Marino       const struct lval_funcs *funcs = val->location.computed.funcs;
14055796c8dcSSimon Schubert 
14065796c8dcSSimon Schubert       if (funcs->copy_closure)
14075796c8dcSSimon Schubert         val->location.computed.closure = funcs->copy_closure (val);
14085796c8dcSSimon Schubert     }
14095796c8dcSSimon Schubert   return val;
14105796c8dcSSimon Schubert }
14115796c8dcSSimon Schubert 
1412c50c785cSJohn Marino /* Return a version of ARG that is non-lvalue.  */
1413c50c785cSJohn Marino 
1414c50c785cSJohn Marino struct value *
value_non_lval(struct value * arg)1415c50c785cSJohn Marino value_non_lval (struct value *arg)
1416c50c785cSJohn Marino {
1417c50c785cSJohn Marino   if (VALUE_LVAL (arg) != not_lval)
1418c50c785cSJohn Marino     {
1419c50c785cSJohn Marino       struct type *enc_type = value_enclosing_type (arg);
1420c50c785cSJohn Marino       struct value *val = allocate_value (enc_type);
1421c50c785cSJohn Marino 
1422c50c785cSJohn Marino       memcpy (value_contents_all_raw (val), value_contents_all (arg),
1423c50c785cSJohn Marino 	      TYPE_LENGTH (enc_type));
1424c50c785cSJohn Marino       val->type = arg->type;
1425c50c785cSJohn Marino       set_value_embedded_offset (val, value_embedded_offset (arg));
1426c50c785cSJohn Marino       set_value_pointed_to_offset (val, value_pointed_to_offset (arg));
1427c50c785cSJohn Marino       return val;
1428c50c785cSJohn Marino     }
1429c50c785cSJohn Marino    return arg;
1430c50c785cSJohn Marino }
1431c50c785cSJohn Marino 
14325796c8dcSSimon Schubert void
set_value_component_location(struct value * component,const struct value * whole)1433cf7f2e2dSJohn Marino set_value_component_location (struct value *component,
1434cf7f2e2dSJohn Marino 			      const struct value *whole)
14355796c8dcSSimon Schubert {
1436cf7f2e2dSJohn Marino   if (whole->lval == lval_internalvar)
14375796c8dcSSimon Schubert     VALUE_LVAL (component) = lval_internalvar_component;
14385796c8dcSSimon Schubert   else
1439cf7f2e2dSJohn Marino     VALUE_LVAL (component) = whole->lval;
14405796c8dcSSimon Schubert 
14415796c8dcSSimon Schubert   component->location = whole->location;
1442cf7f2e2dSJohn Marino   if (whole->lval == lval_computed)
14435796c8dcSSimon Schubert     {
1444a45ae5f8SJohn Marino       const struct lval_funcs *funcs = whole->location.computed.funcs;
14455796c8dcSSimon Schubert 
14465796c8dcSSimon Schubert       if (funcs->copy_closure)
14475796c8dcSSimon Schubert         component->location.computed.closure = funcs->copy_closure (whole);
14485796c8dcSSimon Schubert     }
14495796c8dcSSimon Schubert }
14505796c8dcSSimon Schubert 
14515796c8dcSSimon Schubert 
14525796c8dcSSimon Schubert /* Access to the value history.  */
14535796c8dcSSimon Schubert 
14545796c8dcSSimon Schubert /* Record a new value in the value history.
14555796c8dcSSimon Schubert    Returns the absolute history index of the entry.
14565796c8dcSSimon Schubert    Result of -1 indicates the value was not saved; otherwise it is the
14575796c8dcSSimon Schubert    value history index of this new item.  */
14585796c8dcSSimon Schubert 
14595796c8dcSSimon Schubert int
record_latest_value(struct value * val)14605796c8dcSSimon Schubert record_latest_value (struct value *val)
14615796c8dcSSimon Schubert {
14625796c8dcSSimon Schubert   int i;
14635796c8dcSSimon Schubert 
14645796c8dcSSimon Schubert   /* We don't want this value to have anything to do with the inferior anymore.
14655796c8dcSSimon Schubert      In particular, "set $1 = 50" should not affect the variable from which
14665796c8dcSSimon Schubert      the value was taken, and fast watchpoints should be able to assume that
14675796c8dcSSimon Schubert      a value on the value history never changes.  */
14685796c8dcSSimon Schubert   if (value_lazy (val))
14695796c8dcSSimon Schubert     value_fetch_lazy (val);
14705796c8dcSSimon Schubert   /* We preserve VALUE_LVAL so that the user can find out where it was fetched
14715796c8dcSSimon Schubert      from.  This is a bit dubious, because then *&$1 does not just return $1
14725796c8dcSSimon Schubert      but the current contents of that location.  c'est la vie...  */
14735796c8dcSSimon Schubert   val->modifiable = 0;
14745796c8dcSSimon Schubert   release_value (val);
14755796c8dcSSimon Schubert 
14765796c8dcSSimon Schubert   /* Here we treat value_history_count as origin-zero
14775796c8dcSSimon Schubert      and applying to the value being stored now.  */
14785796c8dcSSimon Schubert 
14795796c8dcSSimon Schubert   i = value_history_count % VALUE_HISTORY_CHUNK;
14805796c8dcSSimon Schubert   if (i == 0)
14815796c8dcSSimon Schubert     {
14825796c8dcSSimon Schubert       struct value_history_chunk *new
14835796c8dcSSimon Schubert 	= (struct value_history_chunk *)
1484cf7f2e2dSJohn Marino 
14855796c8dcSSimon Schubert       xmalloc (sizeof (struct value_history_chunk));
14865796c8dcSSimon Schubert       memset (new->values, 0, sizeof new->values);
14875796c8dcSSimon Schubert       new->next = value_history_chain;
14885796c8dcSSimon Schubert       value_history_chain = new;
14895796c8dcSSimon Schubert     }
14905796c8dcSSimon Schubert 
14915796c8dcSSimon Schubert   value_history_chain->values[i] = val;
14925796c8dcSSimon Schubert 
14935796c8dcSSimon Schubert   /* Now we regard value_history_count as origin-one
14945796c8dcSSimon Schubert      and applying to the value just stored.  */
14955796c8dcSSimon Schubert 
14965796c8dcSSimon Schubert   return ++value_history_count;
14975796c8dcSSimon Schubert }
14985796c8dcSSimon Schubert 
14995796c8dcSSimon Schubert /* Return a copy of the value in the history with sequence number NUM.  */
15005796c8dcSSimon Schubert 
15015796c8dcSSimon Schubert struct value *
access_value_history(int num)15025796c8dcSSimon Schubert access_value_history (int num)
15035796c8dcSSimon Schubert {
15045796c8dcSSimon Schubert   struct value_history_chunk *chunk;
15055796c8dcSSimon Schubert   int i;
15065796c8dcSSimon Schubert   int absnum = num;
15075796c8dcSSimon Schubert 
15085796c8dcSSimon Schubert   if (absnum <= 0)
15095796c8dcSSimon Schubert     absnum += value_history_count;
15105796c8dcSSimon Schubert 
15115796c8dcSSimon Schubert   if (absnum <= 0)
15125796c8dcSSimon Schubert     {
15135796c8dcSSimon Schubert       if (num == 0)
15145796c8dcSSimon Schubert 	error (_("The history is empty."));
15155796c8dcSSimon Schubert       else if (num == 1)
15165796c8dcSSimon Schubert 	error (_("There is only one value in the history."));
15175796c8dcSSimon Schubert       else
15185796c8dcSSimon Schubert 	error (_("History does not go back to $$%d."), -num);
15195796c8dcSSimon Schubert     }
15205796c8dcSSimon Schubert   if (absnum > value_history_count)
15215796c8dcSSimon Schubert     error (_("History has not yet reached $%d."), absnum);
15225796c8dcSSimon Schubert 
15235796c8dcSSimon Schubert   absnum--;
15245796c8dcSSimon Schubert 
15255796c8dcSSimon Schubert   /* Now absnum is always absolute and origin zero.  */
15265796c8dcSSimon Schubert 
15275796c8dcSSimon Schubert   chunk = value_history_chain;
1528c50c785cSJohn Marino   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK
1529c50c785cSJohn Marino 	 - absnum / VALUE_HISTORY_CHUNK;
15305796c8dcSSimon Schubert        i > 0; i--)
15315796c8dcSSimon Schubert     chunk = chunk->next;
15325796c8dcSSimon Schubert 
15335796c8dcSSimon Schubert   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
15345796c8dcSSimon Schubert }
15355796c8dcSSimon Schubert 
15365796c8dcSSimon Schubert static void
show_values(char * num_exp,int from_tty)15375796c8dcSSimon Schubert show_values (char *num_exp, int from_tty)
15385796c8dcSSimon Schubert {
15395796c8dcSSimon Schubert   int i;
15405796c8dcSSimon Schubert   struct value *val;
15415796c8dcSSimon Schubert   static int num = 1;
15425796c8dcSSimon Schubert 
15435796c8dcSSimon Schubert   if (num_exp)
15445796c8dcSSimon Schubert     {
15455796c8dcSSimon Schubert       /* "show values +" should print from the stored position.
15465796c8dcSSimon Schubert          "show values <exp>" should print around value number <exp>.  */
15475796c8dcSSimon Schubert       if (num_exp[0] != '+' || num_exp[1] != '\0')
15485796c8dcSSimon Schubert 	num = parse_and_eval_long (num_exp) - 5;
15495796c8dcSSimon Schubert     }
15505796c8dcSSimon Schubert   else
15515796c8dcSSimon Schubert     {
15525796c8dcSSimon Schubert       /* "show values" means print the last 10 values.  */
15535796c8dcSSimon Schubert       num = value_history_count - 9;
15545796c8dcSSimon Schubert     }
15555796c8dcSSimon Schubert 
15565796c8dcSSimon Schubert   if (num <= 0)
15575796c8dcSSimon Schubert     num = 1;
15585796c8dcSSimon Schubert 
15595796c8dcSSimon Schubert   for (i = num; i < num + 10 && i <= value_history_count; i++)
15605796c8dcSSimon Schubert     {
15615796c8dcSSimon Schubert       struct value_print_options opts;
1562cf7f2e2dSJohn Marino 
15635796c8dcSSimon Schubert       val = access_value_history (i);
15645796c8dcSSimon Schubert       printf_filtered (("$%d = "), i);
15655796c8dcSSimon Schubert       get_user_print_options (&opts);
15665796c8dcSSimon Schubert       value_print (val, gdb_stdout, &opts);
15675796c8dcSSimon Schubert       printf_filtered (("\n"));
15685796c8dcSSimon Schubert     }
15695796c8dcSSimon Schubert 
15705796c8dcSSimon Schubert   /* The next "show values +" should start after what we just printed.  */
15715796c8dcSSimon Schubert   num += 10;
15725796c8dcSSimon Schubert 
15735796c8dcSSimon Schubert   /* Hitting just return after this command should do the same thing as
15745796c8dcSSimon Schubert      "show values +".  If num_exp is null, this is unnecessary, since
15755796c8dcSSimon Schubert      "show values +" is not useful after "show values".  */
15765796c8dcSSimon Schubert   if (from_tty && num_exp)
15775796c8dcSSimon Schubert     {
15785796c8dcSSimon Schubert       num_exp[0] = '+';
15795796c8dcSSimon Schubert       num_exp[1] = '\0';
15805796c8dcSSimon Schubert     }
15815796c8dcSSimon Schubert }
15825796c8dcSSimon Schubert 
15835796c8dcSSimon Schubert /* Internal variables.  These are variables within the debugger
15845796c8dcSSimon Schubert    that hold values assigned by debugger commands.
15855796c8dcSSimon Schubert    The user refers to them with a '$' prefix
15865796c8dcSSimon Schubert    that does not appear in the variable names stored internally.  */
15875796c8dcSSimon Schubert 
15885796c8dcSSimon Schubert struct internalvar
15895796c8dcSSimon Schubert {
15905796c8dcSSimon Schubert   struct internalvar *next;
15915796c8dcSSimon Schubert   char *name;
15925796c8dcSSimon Schubert 
15935796c8dcSSimon Schubert   /* We support various different kinds of content of an internal variable.
15945796c8dcSSimon Schubert      enum internalvar_kind specifies the kind, and union internalvar_data
15955796c8dcSSimon Schubert      provides the data associated with this particular kind.  */
15965796c8dcSSimon Schubert 
15975796c8dcSSimon Schubert   enum internalvar_kind
15985796c8dcSSimon Schubert     {
15995796c8dcSSimon Schubert       /* The internal variable is empty.  */
16005796c8dcSSimon Schubert       INTERNALVAR_VOID,
16015796c8dcSSimon Schubert 
16025796c8dcSSimon Schubert       /* The value of the internal variable is provided directly as
16035796c8dcSSimon Schubert 	 a GDB value object.  */
16045796c8dcSSimon Schubert       INTERNALVAR_VALUE,
16055796c8dcSSimon Schubert 
16065796c8dcSSimon Schubert       /* A fresh value is computed via a call-back routine on every
16075796c8dcSSimon Schubert 	 access to the internal variable.  */
16085796c8dcSSimon Schubert       INTERNALVAR_MAKE_VALUE,
16095796c8dcSSimon Schubert 
16105796c8dcSSimon Schubert       /* The internal variable holds a GDB internal convenience function.  */
16115796c8dcSSimon Schubert       INTERNALVAR_FUNCTION,
16125796c8dcSSimon Schubert 
16135796c8dcSSimon Schubert       /* The variable holds an integer value.  */
16145796c8dcSSimon Schubert       INTERNALVAR_INTEGER,
16155796c8dcSSimon Schubert 
16165796c8dcSSimon Schubert       /* The variable holds a GDB-provided string.  */
16175796c8dcSSimon Schubert       INTERNALVAR_STRING,
16185796c8dcSSimon Schubert 
16195796c8dcSSimon Schubert     } kind;
16205796c8dcSSimon Schubert 
16215796c8dcSSimon Schubert   union internalvar_data
16225796c8dcSSimon Schubert     {
16235796c8dcSSimon Schubert       /* A value object used with INTERNALVAR_VALUE.  */
16245796c8dcSSimon Schubert       struct value *value;
16255796c8dcSSimon Schubert 
16265796c8dcSSimon Schubert       /* The call-back routine used with INTERNALVAR_MAKE_VALUE.  */
1627*ef5ccd6cSJohn Marino       struct
1628*ef5ccd6cSJohn Marino         {
1629*ef5ccd6cSJohn Marino 	  /* The functions to call.  */
1630*ef5ccd6cSJohn Marino 	  const struct internalvar_funcs *functions;
1631*ef5ccd6cSJohn Marino 
1632*ef5ccd6cSJohn Marino 	  /* The function's user-data.  */
1633*ef5ccd6cSJohn Marino 	  void *data;
1634*ef5ccd6cSJohn Marino         } make_value;
16355796c8dcSSimon Schubert 
16365796c8dcSSimon Schubert       /* The internal function used with INTERNALVAR_FUNCTION.  */
16375796c8dcSSimon Schubert       struct
16385796c8dcSSimon Schubert 	{
16395796c8dcSSimon Schubert 	  struct internal_function *function;
16405796c8dcSSimon Schubert 	  /* True if this is the canonical name for the function.  */
16415796c8dcSSimon Schubert 	  int canonical;
16425796c8dcSSimon Schubert 	} fn;
16435796c8dcSSimon Schubert 
16445796c8dcSSimon Schubert       /* An integer value used with INTERNALVAR_INTEGER.  */
16455796c8dcSSimon Schubert       struct
16465796c8dcSSimon Schubert         {
16475796c8dcSSimon Schubert 	  /* If type is non-NULL, it will be used as the type to generate
16485796c8dcSSimon Schubert 	     a value for this internal variable.  If type is NULL, a default
16495796c8dcSSimon Schubert 	     integer type for the architecture is used.  */
16505796c8dcSSimon Schubert 	  struct type *type;
16515796c8dcSSimon Schubert 	  LONGEST val;
16525796c8dcSSimon Schubert         } integer;
16535796c8dcSSimon Schubert 
16545796c8dcSSimon Schubert       /* A string value used with INTERNALVAR_STRING.  */
16555796c8dcSSimon Schubert       char *string;
16565796c8dcSSimon Schubert     } u;
16575796c8dcSSimon Schubert };
16585796c8dcSSimon Schubert 
16595796c8dcSSimon Schubert static struct internalvar *internalvars;
16605796c8dcSSimon Schubert 
1661c50c785cSJohn Marino /* If the variable does not already exist create it and give it the
1662c50c785cSJohn Marino    value given.  If no value is given then the default is zero.  */
16635796c8dcSSimon Schubert static void
init_if_undefined_command(char * args,int from_tty)16645796c8dcSSimon Schubert init_if_undefined_command (char* args, int from_tty)
16655796c8dcSSimon Schubert {
16665796c8dcSSimon Schubert   struct internalvar* intvar;
16675796c8dcSSimon Schubert 
16685796c8dcSSimon Schubert   /* Parse the expression - this is taken from set_command().  */
16695796c8dcSSimon Schubert   struct expression *expr = parse_expression (args);
16705796c8dcSSimon Schubert   register struct cleanup *old_chain =
16715796c8dcSSimon Schubert     make_cleanup (free_current_contents, &expr);
16725796c8dcSSimon Schubert 
16735796c8dcSSimon Schubert   /* Validate the expression.
16745796c8dcSSimon Schubert      Was the expression an assignment?
16755796c8dcSSimon Schubert      Or even an expression at all?  */
16765796c8dcSSimon Schubert   if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN)
16775796c8dcSSimon Schubert     error (_("Init-if-undefined requires an assignment expression."));
16785796c8dcSSimon Schubert 
16795796c8dcSSimon Schubert   /* Extract the variable from the parsed expression.
16805796c8dcSSimon Schubert      In the case of an assign the lvalue will be in elts[1] and elts[2].  */
16815796c8dcSSimon Schubert   if (expr->elts[1].opcode != OP_INTERNALVAR)
1682c50c785cSJohn Marino     error (_("The first parameter to init-if-undefined "
1683c50c785cSJohn Marino 	     "should be a GDB variable."));
16845796c8dcSSimon Schubert   intvar = expr->elts[2].internalvar;
16855796c8dcSSimon Schubert 
16865796c8dcSSimon Schubert   /* Only evaluate the expression if the lvalue is void.
16875796c8dcSSimon Schubert      This may still fail if the expresssion is invalid.  */
16885796c8dcSSimon Schubert   if (intvar->kind == INTERNALVAR_VOID)
16895796c8dcSSimon Schubert     evaluate_expression (expr);
16905796c8dcSSimon Schubert 
16915796c8dcSSimon Schubert   do_cleanups (old_chain);
16925796c8dcSSimon Schubert }
16935796c8dcSSimon Schubert 
16945796c8dcSSimon Schubert 
16955796c8dcSSimon Schubert /* Look up an internal variable with name NAME.  NAME should not
16965796c8dcSSimon Schubert    normally include a dollar sign.
16975796c8dcSSimon Schubert 
16985796c8dcSSimon Schubert    If the specified internal variable does not exist,
16995796c8dcSSimon Schubert    the return value is NULL.  */
17005796c8dcSSimon Schubert 
17015796c8dcSSimon Schubert struct internalvar *
lookup_only_internalvar(const char * name)17025796c8dcSSimon Schubert lookup_only_internalvar (const char *name)
17035796c8dcSSimon Schubert {
17045796c8dcSSimon Schubert   struct internalvar *var;
17055796c8dcSSimon Schubert 
17065796c8dcSSimon Schubert   for (var = internalvars; var; var = var->next)
17075796c8dcSSimon Schubert     if (strcmp (var->name, name) == 0)
17085796c8dcSSimon Schubert       return var;
17095796c8dcSSimon Schubert 
17105796c8dcSSimon Schubert   return NULL;
17115796c8dcSSimon Schubert }
17125796c8dcSSimon Schubert 
1713*ef5ccd6cSJohn Marino /* Complete NAME by comparing it to the names of internal variables.
1714*ef5ccd6cSJohn Marino    Returns a vector of newly allocated strings, or NULL if no matches
1715*ef5ccd6cSJohn Marino    were found.  */
1716*ef5ccd6cSJohn Marino 
VEC(char_ptr)1717*ef5ccd6cSJohn Marino VEC (char_ptr) *
1718*ef5ccd6cSJohn Marino complete_internalvar (const char *name)
1719*ef5ccd6cSJohn Marino {
1720*ef5ccd6cSJohn Marino   VEC (char_ptr) *result = NULL;
1721*ef5ccd6cSJohn Marino   struct internalvar *var;
1722*ef5ccd6cSJohn Marino   int len;
1723*ef5ccd6cSJohn Marino 
1724*ef5ccd6cSJohn Marino   len = strlen (name);
1725*ef5ccd6cSJohn Marino 
1726*ef5ccd6cSJohn Marino   for (var = internalvars; var; var = var->next)
1727*ef5ccd6cSJohn Marino     if (strncmp (var->name, name, len) == 0)
1728*ef5ccd6cSJohn Marino       {
1729*ef5ccd6cSJohn Marino 	char *r = xstrdup (var->name);
1730*ef5ccd6cSJohn Marino 
1731*ef5ccd6cSJohn Marino 	VEC_safe_push (char_ptr, result, r);
1732*ef5ccd6cSJohn Marino       }
1733*ef5ccd6cSJohn Marino 
1734*ef5ccd6cSJohn Marino   return result;
1735*ef5ccd6cSJohn Marino }
17365796c8dcSSimon Schubert 
17375796c8dcSSimon Schubert /* Create an internal variable with name NAME and with a void value.
17385796c8dcSSimon Schubert    NAME should not normally include a dollar sign.  */
17395796c8dcSSimon Schubert 
17405796c8dcSSimon Schubert struct internalvar *
create_internalvar(const char * name)17415796c8dcSSimon Schubert create_internalvar (const char *name)
17425796c8dcSSimon Schubert {
17435796c8dcSSimon Schubert   struct internalvar *var;
1744cf7f2e2dSJohn Marino 
17455796c8dcSSimon Schubert   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
17465796c8dcSSimon Schubert   var->name = concat (name, (char *)NULL);
17475796c8dcSSimon Schubert   var->kind = INTERNALVAR_VOID;
17485796c8dcSSimon Schubert   var->next = internalvars;
17495796c8dcSSimon Schubert   internalvars = var;
17505796c8dcSSimon Schubert   return var;
17515796c8dcSSimon Schubert }
17525796c8dcSSimon Schubert 
17535796c8dcSSimon Schubert /* Create an internal variable with name NAME and register FUN as the
17545796c8dcSSimon Schubert    function that value_of_internalvar uses to create a value whenever
17555796c8dcSSimon Schubert    this variable is referenced.  NAME should not normally include a
1756*ef5ccd6cSJohn Marino    dollar sign.  DATA is passed uninterpreted to FUN when it is
1757*ef5ccd6cSJohn Marino    called.  CLEANUP, if not NULL, is called when the internal variable
1758*ef5ccd6cSJohn Marino    is destroyed.  It is passed DATA as its only argument.  */
17595796c8dcSSimon Schubert 
17605796c8dcSSimon Schubert struct internalvar *
create_internalvar_type_lazy(const char * name,const struct internalvar_funcs * funcs,void * data)1761*ef5ccd6cSJohn Marino create_internalvar_type_lazy (const char *name,
1762*ef5ccd6cSJohn Marino 			      const struct internalvar_funcs *funcs,
1763*ef5ccd6cSJohn Marino 			      void *data)
17645796c8dcSSimon Schubert {
17655796c8dcSSimon Schubert   struct internalvar *var = create_internalvar (name);
1766cf7f2e2dSJohn Marino 
17675796c8dcSSimon Schubert   var->kind = INTERNALVAR_MAKE_VALUE;
1768*ef5ccd6cSJohn Marino   var->u.make_value.functions = funcs;
1769*ef5ccd6cSJohn Marino   var->u.make_value.data = data;
17705796c8dcSSimon Schubert   return var;
17715796c8dcSSimon Schubert }
17725796c8dcSSimon Schubert 
1773*ef5ccd6cSJohn Marino /* See documentation in value.h.  */
1774*ef5ccd6cSJohn Marino 
1775*ef5ccd6cSJohn Marino int
compile_internalvar_to_ax(struct internalvar * var,struct agent_expr * expr,struct axs_value * value)1776*ef5ccd6cSJohn Marino compile_internalvar_to_ax (struct internalvar *var,
1777*ef5ccd6cSJohn Marino 			   struct agent_expr *expr,
1778*ef5ccd6cSJohn Marino 			   struct axs_value *value)
1779*ef5ccd6cSJohn Marino {
1780*ef5ccd6cSJohn Marino   if (var->kind != INTERNALVAR_MAKE_VALUE
1781*ef5ccd6cSJohn Marino       || var->u.make_value.functions->compile_to_ax == NULL)
1782*ef5ccd6cSJohn Marino     return 0;
1783*ef5ccd6cSJohn Marino 
1784*ef5ccd6cSJohn Marino   var->u.make_value.functions->compile_to_ax (var, expr, value,
1785*ef5ccd6cSJohn Marino 					      var->u.make_value.data);
1786*ef5ccd6cSJohn Marino   return 1;
1787*ef5ccd6cSJohn Marino }
1788*ef5ccd6cSJohn Marino 
17895796c8dcSSimon Schubert /* Look up an internal variable with name NAME.  NAME should not
17905796c8dcSSimon Schubert    normally include a dollar sign.
17915796c8dcSSimon Schubert 
17925796c8dcSSimon Schubert    If the specified internal variable does not exist,
17935796c8dcSSimon Schubert    one is created, with a void value.  */
17945796c8dcSSimon Schubert 
17955796c8dcSSimon Schubert struct internalvar *
lookup_internalvar(const char * name)17965796c8dcSSimon Schubert lookup_internalvar (const char *name)
17975796c8dcSSimon Schubert {
17985796c8dcSSimon Schubert   struct internalvar *var;
17995796c8dcSSimon Schubert 
18005796c8dcSSimon Schubert   var = lookup_only_internalvar (name);
18015796c8dcSSimon Schubert   if (var)
18025796c8dcSSimon Schubert     return var;
18035796c8dcSSimon Schubert 
18045796c8dcSSimon Schubert   return create_internalvar (name);
18055796c8dcSSimon Schubert }
18065796c8dcSSimon Schubert 
18075796c8dcSSimon Schubert /* Return current value of internal variable VAR.  For variables that
18085796c8dcSSimon Schubert    are not inherently typed, use a value type appropriate for GDBARCH.  */
18095796c8dcSSimon Schubert 
18105796c8dcSSimon Schubert struct value *
value_of_internalvar(struct gdbarch * gdbarch,struct internalvar * var)18115796c8dcSSimon Schubert value_of_internalvar (struct gdbarch *gdbarch, struct internalvar *var)
18125796c8dcSSimon Schubert {
18135796c8dcSSimon Schubert   struct value *val;
1814c50c785cSJohn Marino   struct trace_state_variable *tsv;
1815c50c785cSJohn Marino 
1816c50c785cSJohn Marino   /* If there is a trace state variable of the same name, assume that
1817c50c785cSJohn Marino      is what we really want to see.  */
1818c50c785cSJohn Marino   tsv = find_trace_state_variable (var->name);
1819c50c785cSJohn Marino   if (tsv)
1820c50c785cSJohn Marino     {
1821c50c785cSJohn Marino       tsv->value_known = target_get_trace_state_variable_value (tsv->number,
1822c50c785cSJohn Marino 								&(tsv->value));
1823c50c785cSJohn Marino       if (tsv->value_known)
1824c50c785cSJohn Marino 	val = value_from_longest (builtin_type (gdbarch)->builtin_int64,
1825c50c785cSJohn Marino 				  tsv->value);
1826c50c785cSJohn Marino       else
1827c50c785cSJohn Marino 	val = allocate_value (builtin_type (gdbarch)->builtin_void);
1828c50c785cSJohn Marino       return val;
1829c50c785cSJohn Marino     }
18305796c8dcSSimon Schubert 
18315796c8dcSSimon Schubert   switch (var->kind)
18325796c8dcSSimon Schubert     {
18335796c8dcSSimon Schubert     case INTERNALVAR_VOID:
18345796c8dcSSimon Schubert       val = allocate_value (builtin_type (gdbarch)->builtin_void);
18355796c8dcSSimon Schubert       break;
18365796c8dcSSimon Schubert 
18375796c8dcSSimon Schubert     case INTERNALVAR_FUNCTION:
18385796c8dcSSimon Schubert       val = allocate_value (builtin_type (gdbarch)->internal_fn);
18395796c8dcSSimon Schubert       break;
18405796c8dcSSimon Schubert 
18415796c8dcSSimon Schubert     case INTERNALVAR_INTEGER:
18425796c8dcSSimon Schubert       if (!var->u.integer.type)
18435796c8dcSSimon Schubert 	val = value_from_longest (builtin_type (gdbarch)->builtin_int,
18445796c8dcSSimon Schubert 				  var->u.integer.val);
18455796c8dcSSimon Schubert       else
18465796c8dcSSimon Schubert 	val = value_from_longest (var->u.integer.type, var->u.integer.val);
18475796c8dcSSimon Schubert       break;
18485796c8dcSSimon Schubert 
18495796c8dcSSimon Schubert     case INTERNALVAR_STRING:
18505796c8dcSSimon Schubert       val = value_cstring (var->u.string, strlen (var->u.string),
18515796c8dcSSimon Schubert 			   builtin_type (gdbarch)->builtin_char);
18525796c8dcSSimon Schubert       break;
18535796c8dcSSimon Schubert 
18545796c8dcSSimon Schubert     case INTERNALVAR_VALUE:
18555796c8dcSSimon Schubert       val = value_copy (var->u.value);
18565796c8dcSSimon Schubert       if (value_lazy (val))
18575796c8dcSSimon Schubert 	value_fetch_lazy (val);
18585796c8dcSSimon Schubert       break;
18595796c8dcSSimon Schubert 
18605796c8dcSSimon Schubert     case INTERNALVAR_MAKE_VALUE:
1861*ef5ccd6cSJohn Marino       val = (*var->u.make_value.functions->make_value) (gdbarch, var,
1862*ef5ccd6cSJohn Marino 							var->u.make_value.data);
18635796c8dcSSimon Schubert       break;
18645796c8dcSSimon Schubert 
18655796c8dcSSimon Schubert     default:
1866c50c785cSJohn Marino       internal_error (__FILE__, __LINE__, _("bad kind"));
18675796c8dcSSimon Schubert     }
18685796c8dcSSimon Schubert 
18695796c8dcSSimon Schubert   /* Change the VALUE_LVAL to lval_internalvar so that future operations
18705796c8dcSSimon Schubert      on this value go back to affect the original internal variable.
18715796c8dcSSimon Schubert 
18725796c8dcSSimon Schubert      Do not do this for INTERNALVAR_MAKE_VALUE variables, as those have
18735796c8dcSSimon Schubert      no underlying modifyable state in the internal variable.
18745796c8dcSSimon Schubert 
18755796c8dcSSimon Schubert      Likewise, if the variable's value is a computed lvalue, we want
18765796c8dcSSimon Schubert      references to it to produce another computed lvalue, where
18775796c8dcSSimon Schubert      references and assignments actually operate through the
18785796c8dcSSimon Schubert      computed value's functions.
18795796c8dcSSimon Schubert 
18805796c8dcSSimon Schubert      This means that internal variables with computed values
18815796c8dcSSimon Schubert      behave a little differently from other internal variables:
18825796c8dcSSimon Schubert      assignments to them don't just replace the previous value
18835796c8dcSSimon Schubert      altogether.  At the moment, this seems like the behavior we
18845796c8dcSSimon Schubert      want.  */
18855796c8dcSSimon Schubert 
18865796c8dcSSimon Schubert   if (var->kind != INTERNALVAR_MAKE_VALUE
18875796c8dcSSimon Schubert       && val->lval != lval_computed)
18885796c8dcSSimon Schubert     {
18895796c8dcSSimon Schubert       VALUE_LVAL (val) = lval_internalvar;
18905796c8dcSSimon Schubert       VALUE_INTERNALVAR (val) = var;
18915796c8dcSSimon Schubert     }
18925796c8dcSSimon Schubert 
18935796c8dcSSimon Schubert   return val;
18945796c8dcSSimon Schubert }
18955796c8dcSSimon Schubert 
18965796c8dcSSimon Schubert int
get_internalvar_integer(struct internalvar * var,LONGEST * result)18975796c8dcSSimon Schubert get_internalvar_integer (struct internalvar *var, LONGEST *result)
18985796c8dcSSimon Schubert {
1899c50c785cSJohn Marino   if (var->kind == INTERNALVAR_INTEGER)
19005796c8dcSSimon Schubert     {
19015796c8dcSSimon Schubert       *result = var->u.integer.val;
19025796c8dcSSimon Schubert       return 1;
19035796c8dcSSimon Schubert     }
1904c50c785cSJohn Marino 
1905c50c785cSJohn Marino   if (var->kind == INTERNALVAR_VALUE)
1906c50c785cSJohn Marino     {
1907c50c785cSJohn Marino       struct type *type = check_typedef (value_type (var->u.value));
1908c50c785cSJohn Marino 
1909c50c785cSJohn Marino       if (TYPE_CODE (type) == TYPE_CODE_INT)
1910c50c785cSJohn Marino 	{
1911c50c785cSJohn Marino 	  *result = value_as_long (var->u.value);
1912c50c785cSJohn Marino 	  return 1;
1913c50c785cSJohn Marino 	}
1914c50c785cSJohn Marino     }
1915c50c785cSJohn Marino 
1916c50c785cSJohn Marino   return 0;
19175796c8dcSSimon Schubert }
19185796c8dcSSimon Schubert 
19195796c8dcSSimon Schubert static int
get_internalvar_function(struct internalvar * var,struct internal_function ** result)19205796c8dcSSimon Schubert get_internalvar_function (struct internalvar *var,
19215796c8dcSSimon Schubert 			  struct internal_function **result)
19225796c8dcSSimon Schubert {
19235796c8dcSSimon Schubert   switch (var->kind)
19245796c8dcSSimon Schubert     {
19255796c8dcSSimon Schubert     case INTERNALVAR_FUNCTION:
19265796c8dcSSimon Schubert       *result = var->u.fn.function;
19275796c8dcSSimon Schubert       return 1;
19285796c8dcSSimon Schubert 
19295796c8dcSSimon Schubert     default:
19305796c8dcSSimon Schubert       return 0;
19315796c8dcSSimon Schubert     }
19325796c8dcSSimon Schubert }
19335796c8dcSSimon Schubert 
19345796c8dcSSimon Schubert void
set_internalvar_component(struct internalvar * var,int offset,int bitpos,int bitsize,struct value * newval)19355796c8dcSSimon Schubert set_internalvar_component (struct internalvar *var, int offset, int bitpos,
19365796c8dcSSimon Schubert 			   int bitsize, struct value *newval)
19375796c8dcSSimon Schubert {
19385796c8dcSSimon Schubert   gdb_byte *addr;
19395796c8dcSSimon Schubert 
19405796c8dcSSimon Schubert   switch (var->kind)
19415796c8dcSSimon Schubert     {
19425796c8dcSSimon Schubert     case INTERNALVAR_VALUE:
19435796c8dcSSimon Schubert       addr = value_contents_writeable (var->u.value);
19445796c8dcSSimon Schubert 
19455796c8dcSSimon Schubert       if (bitsize)
19465796c8dcSSimon Schubert 	modify_field (value_type (var->u.value), addr + offset,
19475796c8dcSSimon Schubert 		      value_as_long (newval), bitpos, bitsize);
19485796c8dcSSimon Schubert       else
19495796c8dcSSimon Schubert 	memcpy (addr + offset, value_contents (newval),
19505796c8dcSSimon Schubert 		TYPE_LENGTH (value_type (newval)));
19515796c8dcSSimon Schubert       break;
19525796c8dcSSimon Schubert 
19535796c8dcSSimon Schubert     default:
19545796c8dcSSimon Schubert       /* We can never get a component of any other kind.  */
1955c50c785cSJohn Marino       internal_error (__FILE__, __LINE__, _("set_internalvar_component"));
19565796c8dcSSimon Schubert     }
19575796c8dcSSimon Schubert }
19585796c8dcSSimon Schubert 
19595796c8dcSSimon Schubert void
set_internalvar(struct internalvar * var,struct value * val)19605796c8dcSSimon Schubert set_internalvar (struct internalvar *var, struct value *val)
19615796c8dcSSimon Schubert {
19625796c8dcSSimon Schubert   enum internalvar_kind new_kind;
19635796c8dcSSimon Schubert   union internalvar_data new_data = { 0 };
19645796c8dcSSimon Schubert 
19655796c8dcSSimon Schubert   if (var->kind == INTERNALVAR_FUNCTION && var->u.fn.canonical)
19665796c8dcSSimon Schubert     error (_("Cannot overwrite convenience function %s"), var->name);
19675796c8dcSSimon Schubert 
19685796c8dcSSimon Schubert   /* Prepare new contents.  */
19695796c8dcSSimon Schubert   switch (TYPE_CODE (check_typedef (value_type (val))))
19705796c8dcSSimon Schubert     {
19715796c8dcSSimon Schubert     case TYPE_CODE_VOID:
19725796c8dcSSimon Schubert       new_kind = INTERNALVAR_VOID;
19735796c8dcSSimon Schubert       break;
19745796c8dcSSimon Schubert 
19755796c8dcSSimon Schubert     case TYPE_CODE_INTERNAL_FUNCTION:
19765796c8dcSSimon Schubert       gdb_assert (VALUE_LVAL (val) == lval_internalvar);
19775796c8dcSSimon Schubert       new_kind = INTERNALVAR_FUNCTION;
19785796c8dcSSimon Schubert       get_internalvar_function (VALUE_INTERNALVAR (val),
19795796c8dcSSimon Schubert 				&new_data.fn.function);
19805796c8dcSSimon Schubert       /* Copies created here are never canonical.  */
19815796c8dcSSimon Schubert       break;
19825796c8dcSSimon Schubert 
19835796c8dcSSimon Schubert     default:
19845796c8dcSSimon Schubert       new_kind = INTERNALVAR_VALUE;
19855796c8dcSSimon Schubert       new_data.value = value_copy (val);
19865796c8dcSSimon Schubert       new_data.value->modifiable = 1;
19875796c8dcSSimon Schubert 
19885796c8dcSSimon Schubert       /* Force the value to be fetched from the target now, to avoid problems
19895796c8dcSSimon Schubert 	 later when this internalvar is referenced and the target is gone or
19905796c8dcSSimon Schubert 	 has changed.  */
19915796c8dcSSimon Schubert       if (value_lazy (new_data.value))
19925796c8dcSSimon Schubert        value_fetch_lazy (new_data.value);
19935796c8dcSSimon Schubert 
19945796c8dcSSimon Schubert       /* Release the value from the value chain to prevent it from being
19955796c8dcSSimon Schubert 	 deleted by free_all_values.  From here on this function should not
19965796c8dcSSimon Schubert 	 call error () until new_data is installed into the var->u to avoid
19975796c8dcSSimon Schubert 	 leaking memory.  */
19985796c8dcSSimon Schubert       release_value (new_data.value);
19995796c8dcSSimon Schubert       break;
20005796c8dcSSimon Schubert     }
20015796c8dcSSimon Schubert 
20025796c8dcSSimon Schubert   /* Clean up old contents.  */
20035796c8dcSSimon Schubert   clear_internalvar (var);
20045796c8dcSSimon Schubert 
20055796c8dcSSimon Schubert   /* Switch over.  */
20065796c8dcSSimon Schubert   var->kind = new_kind;
20075796c8dcSSimon Schubert   var->u = new_data;
20085796c8dcSSimon Schubert   /* End code which must not call error().  */
20095796c8dcSSimon Schubert }
20105796c8dcSSimon Schubert 
20115796c8dcSSimon Schubert void
set_internalvar_integer(struct internalvar * var,LONGEST l)20125796c8dcSSimon Schubert set_internalvar_integer (struct internalvar *var, LONGEST l)
20135796c8dcSSimon Schubert {
20145796c8dcSSimon Schubert   /* Clean up old contents.  */
20155796c8dcSSimon Schubert   clear_internalvar (var);
20165796c8dcSSimon Schubert 
20175796c8dcSSimon Schubert   var->kind = INTERNALVAR_INTEGER;
20185796c8dcSSimon Schubert   var->u.integer.type = NULL;
20195796c8dcSSimon Schubert   var->u.integer.val = l;
20205796c8dcSSimon Schubert }
20215796c8dcSSimon Schubert 
20225796c8dcSSimon Schubert void
set_internalvar_string(struct internalvar * var,const char * string)20235796c8dcSSimon Schubert set_internalvar_string (struct internalvar *var, const char *string)
20245796c8dcSSimon Schubert {
20255796c8dcSSimon Schubert   /* Clean up old contents.  */
20265796c8dcSSimon Schubert   clear_internalvar (var);
20275796c8dcSSimon Schubert 
20285796c8dcSSimon Schubert   var->kind = INTERNALVAR_STRING;
20295796c8dcSSimon Schubert   var->u.string = xstrdup (string);
20305796c8dcSSimon Schubert }
20315796c8dcSSimon Schubert 
20325796c8dcSSimon Schubert static void
set_internalvar_function(struct internalvar * var,struct internal_function * f)20335796c8dcSSimon Schubert set_internalvar_function (struct internalvar *var, struct internal_function *f)
20345796c8dcSSimon Schubert {
20355796c8dcSSimon Schubert   /* Clean up old contents.  */
20365796c8dcSSimon Schubert   clear_internalvar (var);
20375796c8dcSSimon Schubert 
20385796c8dcSSimon Schubert   var->kind = INTERNALVAR_FUNCTION;
20395796c8dcSSimon Schubert   var->u.fn.function = f;
20405796c8dcSSimon Schubert   var->u.fn.canonical = 1;
20415796c8dcSSimon Schubert   /* Variables installed here are always the canonical version.  */
20425796c8dcSSimon Schubert }
20435796c8dcSSimon Schubert 
20445796c8dcSSimon Schubert void
clear_internalvar(struct internalvar * var)20455796c8dcSSimon Schubert clear_internalvar (struct internalvar *var)
20465796c8dcSSimon Schubert {
20475796c8dcSSimon Schubert   /* Clean up old contents.  */
20485796c8dcSSimon Schubert   switch (var->kind)
20495796c8dcSSimon Schubert     {
20505796c8dcSSimon Schubert     case INTERNALVAR_VALUE:
20515796c8dcSSimon Schubert       value_free (var->u.value);
20525796c8dcSSimon Schubert       break;
20535796c8dcSSimon Schubert 
20545796c8dcSSimon Schubert     case INTERNALVAR_STRING:
20555796c8dcSSimon Schubert       xfree (var->u.string);
20565796c8dcSSimon Schubert       break;
20575796c8dcSSimon Schubert 
2058*ef5ccd6cSJohn Marino     case INTERNALVAR_MAKE_VALUE:
2059*ef5ccd6cSJohn Marino       if (var->u.make_value.functions->destroy != NULL)
2060*ef5ccd6cSJohn Marino 	var->u.make_value.functions->destroy (var->u.make_value.data);
2061*ef5ccd6cSJohn Marino       break;
2062*ef5ccd6cSJohn Marino 
20635796c8dcSSimon Schubert     default:
20645796c8dcSSimon Schubert       break;
20655796c8dcSSimon Schubert     }
20665796c8dcSSimon Schubert 
20675796c8dcSSimon Schubert   /* Reset to void kind.  */
20685796c8dcSSimon Schubert   var->kind = INTERNALVAR_VOID;
20695796c8dcSSimon Schubert }
20705796c8dcSSimon Schubert 
20715796c8dcSSimon Schubert char *
internalvar_name(struct internalvar * var)20725796c8dcSSimon Schubert internalvar_name (struct internalvar *var)
20735796c8dcSSimon Schubert {
20745796c8dcSSimon Schubert   return var->name;
20755796c8dcSSimon Schubert }
20765796c8dcSSimon Schubert 
20775796c8dcSSimon Schubert static struct internal_function *
create_internal_function(const char * name,internal_function_fn handler,void * cookie)20785796c8dcSSimon Schubert create_internal_function (const char *name,
20795796c8dcSSimon Schubert 			  internal_function_fn handler, void *cookie)
20805796c8dcSSimon Schubert {
20815796c8dcSSimon Schubert   struct internal_function *ifn = XNEW (struct internal_function);
2082cf7f2e2dSJohn Marino 
20835796c8dcSSimon Schubert   ifn->name = xstrdup (name);
20845796c8dcSSimon Schubert   ifn->handler = handler;
20855796c8dcSSimon Schubert   ifn->cookie = cookie;
20865796c8dcSSimon Schubert   return ifn;
20875796c8dcSSimon Schubert }
20885796c8dcSSimon Schubert 
20895796c8dcSSimon Schubert char *
value_internal_function_name(struct value * val)20905796c8dcSSimon Schubert value_internal_function_name (struct value *val)
20915796c8dcSSimon Schubert {
20925796c8dcSSimon Schubert   struct internal_function *ifn;
20935796c8dcSSimon Schubert   int result;
20945796c8dcSSimon Schubert 
20955796c8dcSSimon Schubert   gdb_assert (VALUE_LVAL (val) == lval_internalvar);
20965796c8dcSSimon Schubert   result = get_internalvar_function (VALUE_INTERNALVAR (val), &ifn);
20975796c8dcSSimon Schubert   gdb_assert (result);
20985796c8dcSSimon Schubert 
20995796c8dcSSimon Schubert   return ifn->name;
21005796c8dcSSimon Schubert }
21015796c8dcSSimon Schubert 
21025796c8dcSSimon Schubert struct value *
call_internal_function(struct gdbarch * gdbarch,const struct language_defn * language,struct value * func,int argc,struct value ** argv)21035796c8dcSSimon Schubert call_internal_function (struct gdbarch *gdbarch,
21045796c8dcSSimon Schubert 			const struct language_defn *language,
21055796c8dcSSimon Schubert 			struct value *func, int argc, struct value **argv)
21065796c8dcSSimon Schubert {
21075796c8dcSSimon Schubert   struct internal_function *ifn;
21085796c8dcSSimon Schubert   int result;
21095796c8dcSSimon Schubert 
21105796c8dcSSimon Schubert   gdb_assert (VALUE_LVAL (func) == lval_internalvar);
21115796c8dcSSimon Schubert   result = get_internalvar_function (VALUE_INTERNALVAR (func), &ifn);
21125796c8dcSSimon Schubert   gdb_assert (result);
21135796c8dcSSimon Schubert 
21145796c8dcSSimon Schubert   return (*ifn->handler) (gdbarch, language, ifn->cookie, argc, argv);
21155796c8dcSSimon Schubert }
21165796c8dcSSimon Schubert 
21175796c8dcSSimon Schubert /* The 'function' command.  This does nothing -- it is just a
21185796c8dcSSimon Schubert    placeholder to let "help function NAME" work.  This is also used as
21195796c8dcSSimon Schubert    the implementation of the sub-command that is created when
21205796c8dcSSimon Schubert    registering an internal function.  */
21215796c8dcSSimon Schubert static void
function_command(char * command,int from_tty)21225796c8dcSSimon Schubert function_command (char *command, int from_tty)
21235796c8dcSSimon Schubert {
21245796c8dcSSimon Schubert   /* Do nothing.  */
21255796c8dcSSimon Schubert }
21265796c8dcSSimon Schubert 
21275796c8dcSSimon Schubert /* Clean up if an internal function's command is destroyed.  */
21285796c8dcSSimon Schubert static void
function_destroyer(struct cmd_list_element * self,void * ignore)21295796c8dcSSimon Schubert function_destroyer (struct cmd_list_element *self, void *ignore)
21305796c8dcSSimon Schubert {
21315796c8dcSSimon Schubert   xfree (self->name);
21325796c8dcSSimon Schubert   xfree (self->doc);
21335796c8dcSSimon Schubert }
21345796c8dcSSimon Schubert 
21355796c8dcSSimon Schubert /* Add a new internal function.  NAME is the name of the function; DOC
21365796c8dcSSimon Schubert    is a documentation string describing the function.  HANDLER is
21375796c8dcSSimon Schubert    called when the function is invoked.  COOKIE is an arbitrary
21385796c8dcSSimon Schubert    pointer which is passed to HANDLER and is intended for "user
21395796c8dcSSimon Schubert    data".  */
21405796c8dcSSimon Schubert void
add_internal_function(const char * name,const char * doc,internal_function_fn handler,void * cookie)21415796c8dcSSimon Schubert add_internal_function (const char *name, const char *doc,
21425796c8dcSSimon Schubert 		       internal_function_fn handler, void *cookie)
21435796c8dcSSimon Schubert {
21445796c8dcSSimon Schubert   struct cmd_list_element *cmd;
21455796c8dcSSimon Schubert   struct internal_function *ifn;
21465796c8dcSSimon Schubert   struct internalvar *var = lookup_internalvar (name);
21475796c8dcSSimon Schubert 
21485796c8dcSSimon Schubert   ifn = create_internal_function (name, handler, cookie);
21495796c8dcSSimon Schubert   set_internalvar_function (var, ifn);
21505796c8dcSSimon Schubert 
21515796c8dcSSimon Schubert   cmd = add_cmd (xstrdup (name), no_class, function_command, (char *) doc,
21525796c8dcSSimon Schubert 		 &functionlist);
21535796c8dcSSimon Schubert   cmd->destroyer = function_destroyer;
21545796c8dcSSimon Schubert }
21555796c8dcSSimon Schubert 
21565796c8dcSSimon Schubert /* Update VALUE before discarding OBJFILE.  COPIED_TYPES is used to
21575796c8dcSSimon Schubert    prevent cycles / duplicates.  */
21585796c8dcSSimon Schubert 
21595796c8dcSSimon Schubert void
preserve_one_value(struct value * value,struct objfile * objfile,htab_t copied_types)21605796c8dcSSimon Schubert preserve_one_value (struct value *value, struct objfile *objfile,
21615796c8dcSSimon Schubert 		    htab_t copied_types)
21625796c8dcSSimon Schubert {
21635796c8dcSSimon Schubert   if (TYPE_OBJFILE (value->type) == objfile)
21645796c8dcSSimon Schubert     value->type = copy_type_recursive (objfile, value->type, copied_types);
21655796c8dcSSimon Schubert 
21665796c8dcSSimon Schubert   if (TYPE_OBJFILE (value->enclosing_type) == objfile)
21675796c8dcSSimon Schubert     value->enclosing_type = copy_type_recursive (objfile,
21685796c8dcSSimon Schubert 						 value->enclosing_type,
21695796c8dcSSimon Schubert 						 copied_types);
21705796c8dcSSimon Schubert }
21715796c8dcSSimon Schubert 
21725796c8dcSSimon Schubert /* Likewise for internal variable VAR.  */
21735796c8dcSSimon Schubert 
21745796c8dcSSimon Schubert static void
preserve_one_internalvar(struct internalvar * var,struct objfile * objfile,htab_t copied_types)21755796c8dcSSimon Schubert preserve_one_internalvar (struct internalvar *var, struct objfile *objfile,
21765796c8dcSSimon Schubert 			  htab_t copied_types)
21775796c8dcSSimon Schubert {
21785796c8dcSSimon Schubert   switch (var->kind)
21795796c8dcSSimon Schubert     {
21805796c8dcSSimon Schubert     case INTERNALVAR_INTEGER:
21815796c8dcSSimon Schubert       if (var->u.integer.type && TYPE_OBJFILE (var->u.integer.type) == objfile)
21825796c8dcSSimon Schubert 	var->u.integer.type
21835796c8dcSSimon Schubert 	  = copy_type_recursive (objfile, var->u.integer.type, copied_types);
21845796c8dcSSimon Schubert       break;
21855796c8dcSSimon Schubert 
21865796c8dcSSimon Schubert     case INTERNALVAR_VALUE:
21875796c8dcSSimon Schubert       preserve_one_value (var->u.value, objfile, copied_types);
21885796c8dcSSimon Schubert       break;
21895796c8dcSSimon Schubert     }
21905796c8dcSSimon Schubert }
21915796c8dcSSimon Schubert 
21925796c8dcSSimon Schubert /* Update the internal variables and value history when OBJFILE is
21935796c8dcSSimon Schubert    discarded; we must copy the types out of the objfile.  New global types
21945796c8dcSSimon Schubert    will be created for every convenience variable which currently points to
21955796c8dcSSimon Schubert    this objfile's types, and the convenience variables will be adjusted to
21965796c8dcSSimon Schubert    use the new global types.  */
21975796c8dcSSimon Schubert 
21985796c8dcSSimon Schubert void
preserve_values(struct objfile * objfile)21995796c8dcSSimon Schubert preserve_values (struct objfile *objfile)
22005796c8dcSSimon Schubert {
22015796c8dcSSimon Schubert   htab_t copied_types;
22025796c8dcSSimon Schubert   struct value_history_chunk *cur;
22035796c8dcSSimon Schubert   struct internalvar *var;
22045796c8dcSSimon Schubert   int i;
22055796c8dcSSimon Schubert 
22065796c8dcSSimon Schubert   /* Create the hash table.  We allocate on the objfile's obstack, since
22075796c8dcSSimon Schubert      it is soon to be deleted.  */
22085796c8dcSSimon Schubert   copied_types = create_copied_types_hash (objfile);
22095796c8dcSSimon Schubert 
22105796c8dcSSimon Schubert   for (cur = value_history_chain; cur; cur = cur->next)
22115796c8dcSSimon Schubert     for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
22125796c8dcSSimon Schubert       if (cur->values[i])
22135796c8dcSSimon Schubert 	preserve_one_value (cur->values[i], objfile, copied_types);
22145796c8dcSSimon Schubert 
22155796c8dcSSimon Schubert   for (var = internalvars; var; var = var->next)
22165796c8dcSSimon Schubert     preserve_one_internalvar (var, objfile, copied_types);
22175796c8dcSSimon Schubert 
22185796c8dcSSimon Schubert   preserve_python_values (objfile, copied_types);
22195796c8dcSSimon Schubert 
22205796c8dcSSimon Schubert   htab_delete (copied_types);
22215796c8dcSSimon Schubert }
22225796c8dcSSimon Schubert 
22235796c8dcSSimon Schubert static void
show_convenience(char * ignore,int from_tty)22245796c8dcSSimon Schubert show_convenience (char *ignore, int from_tty)
22255796c8dcSSimon Schubert {
22265796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_current_arch ();
22275796c8dcSSimon Schubert   struct internalvar *var;
22285796c8dcSSimon Schubert   int varseen = 0;
22295796c8dcSSimon Schubert   struct value_print_options opts;
22305796c8dcSSimon Schubert 
22315796c8dcSSimon Schubert   get_user_print_options (&opts);
22325796c8dcSSimon Schubert   for (var = internalvars; var; var = var->next)
22335796c8dcSSimon Schubert     {
2234a45ae5f8SJohn Marino       volatile struct gdb_exception ex;
2235a45ae5f8SJohn Marino 
22365796c8dcSSimon Schubert       if (!varseen)
22375796c8dcSSimon Schubert 	{
22385796c8dcSSimon Schubert 	  varseen = 1;
22395796c8dcSSimon Schubert 	}
22405796c8dcSSimon Schubert       printf_filtered (("$%s = "), var->name);
2241a45ae5f8SJohn Marino 
2242a45ae5f8SJohn Marino       TRY_CATCH (ex, RETURN_MASK_ERROR)
2243a45ae5f8SJohn Marino 	{
2244a45ae5f8SJohn Marino 	  struct value *val;
2245a45ae5f8SJohn Marino 
2246a45ae5f8SJohn Marino 	  val = value_of_internalvar (gdbarch, var);
2247a45ae5f8SJohn Marino 	  value_print (val, gdb_stdout, &opts);
2248a45ae5f8SJohn Marino 	}
2249a45ae5f8SJohn Marino       if (ex.reason < 0)
2250a45ae5f8SJohn Marino 	fprintf_filtered (gdb_stdout, _("<error: %s>"), ex.message);
22515796c8dcSSimon Schubert       printf_filtered (("\n"));
22525796c8dcSSimon Schubert     }
22535796c8dcSSimon Schubert   if (!varseen)
2254*ef5ccd6cSJohn Marino     {
2255*ef5ccd6cSJohn Marino       /* This text does not mention convenience functions on purpose.
2256*ef5ccd6cSJohn Marino 	 The user can't create them except via Python, and if Python support
2257*ef5ccd6cSJohn Marino 	 is installed this message will never be printed ($_streq will
2258*ef5ccd6cSJohn Marino 	 exist).  */
2259c50c785cSJohn Marino       printf_unfiltered (_("No debugger convenience variables now defined.\n"
2260c50c785cSJohn Marino 			   "Convenience variables have "
2261c50c785cSJohn Marino 			   "names starting with \"$\";\n"
2262c50c785cSJohn Marino 			   "use \"set\" as in \"set "
2263c50c785cSJohn Marino 			   "$foo = 5\" to define them.\n"));
22645796c8dcSSimon Schubert     }
2265*ef5ccd6cSJohn Marino }
22665796c8dcSSimon Schubert 
22675796c8dcSSimon Schubert /* Extract a value as a C number (either long or double).
22685796c8dcSSimon Schubert    Knows how to convert fixed values to double, or
22695796c8dcSSimon Schubert    floating values to long.
22705796c8dcSSimon Schubert    Does not deallocate the value.  */
22715796c8dcSSimon Schubert 
22725796c8dcSSimon Schubert LONGEST
value_as_long(struct value * val)22735796c8dcSSimon Schubert value_as_long (struct value *val)
22745796c8dcSSimon Schubert {
22755796c8dcSSimon Schubert   /* This coerces arrays and functions, which is necessary (e.g.
22765796c8dcSSimon Schubert      in disassemble_command).  It also dereferences references, which
22775796c8dcSSimon Schubert      I suspect is the most logical thing to do.  */
22785796c8dcSSimon Schubert   val = coerce_array (val);
22795796c8dcSSimon Schubert   return unpack_long (value_type (val), value_contents (val));
22805796c8dcSSimon Schubert }
22815796c8dcSSimon Schubert 
22825796c8dcSSimon Schubert DOUBLEST
value_as_double(struct value * val)22835796c8dcSSimon Schubert value_as_double (struct value *val)
22845796c8dcSSimon Schubert {
22855796c8dcSSimon Schubert   DOUBLEST foo;
22865796c8dcSSimon Schubert   int inv;
22875796c8dcSSimon Schubert 
22885796c8dcSSimon Schubert   foo = unpack_double (value_type (val), value_contents (val), &inv);
22895796c8dcSSimon Schubert   if (inv)
22905796c8dcSSimon Schubert     error (_("Invalid floating value found in program."));
22915796c8dcSSimon Schubert   return foo;
22925796c8dcSSimon Schubert }
22935796c8dcSSimon Schubert 
22945796c8dcSSimon Schubert /* Extract a value as a C pointer.  Does not deallocate the value.
22955796c8dcSSimon Schubert    Note that val's type may not actually be a pointer; value_as_long
22965796c8dcSSimon Schubert    handles all the cases.  */
22975796c8dcSSimon Schubert CORE_ADDR
value_as_address(struct value * val)22985796c8dcSSimon Schubert value_as_address (struct value *val)
22995796c8dcSSimon Schubert {
23005796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_type_arch (value_type (val));
23015796c8dcSSimon Schubert 
23025796c8dcSSimon Schubert   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
23035796c8dcSSimon Schubert      whether we want this to be true eventually.  */
23045796c8dcSSimon Schubert #if 0
23055796c8dcSSimon Schubert   /* gdbarch_addr_bits_remove is wrong if we are being called for a
23065796c8dcSSimon Schubert      non-address (e.g. argument to "signal", "info break", etc.), or
23075796c8dcSSimon Schubert      for pointers to char, in which the low bits *are* significant.  */
23085796c8dcSSimon Schubert   return gdbarch_addr_bits_remove (gdbarch, value_as_long (val));
23095796c8dcSSimon Schubert #else
23105796c8dcSSimon Schubert 
23115796c8dcSSimon Schubert   /* There are several targets (IA-64, PowerPC, and others) which
23125796c8dcSSimon Schubert      don't represent pointers to functions as simply the address of
23135796c8dcSSimon Schubert      the function's entry point.  For example, on the IA-64, a
23145796c8dcSSimon Schubert      function pointer points to a two-word descriptor, generated by
23155796c8dcSSimon Schubert      the linker, which contains the function's entry point, and the
23165796c8dcSSimon Schubert      value the IA-64 "global pointer" register should have --- to
23175796c8dcSSimon Schubert      support position-independent code.  The linker generates
23185796c8dcSSimon Schubert      descriptors only for those functions whose addresses are taken.
23195796c8dcSSimon Schubert 
23205796c8dcSSimon Schubert      On such targets, it's difficult for GDB to convert an arbitrary
23215796c8dcSSimon Schubert      function address into a function pointer; it has to either find
23225796c8dcSSimon Schubert      an existing descriptor for that function, or call malloc and
23235796c8dcSSimon Schubert      build its own.  On some targets, it is impossible for GDB to
23245796c8dcSSimon Schubert      build a descriptor at all: the descriptor must contain a jump
23255796c8dcSSimon Schubert      instruction; data memory cannot be executed; and code memory
23265796c8dcSSimon Schubert      cannot be modified.
23275796c8dcSSimon Schubert 
23285796c8dcSSimon Schubert      Upon entry to this function, if VAL is a value of type `function'
23295796c8dcSSimon Schubert      (that is, TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FUNC), then
23305796c8dcSSimon Schubert      value_address (val) is the address of the function.  This is what
23315796c8dcSSimon Schubert      you'll get if you evaluate an expression like `main'.  The call
23325796c8dcSSimon Schubert      to COERCE_ARRAY below actually does all the usual unary
23335796c8dcSSimon Schubert      conversions, which includes converting values of type `function'
23345796c8dcSSimon Schubert      to `pointer to function'.  This is the challenging conversion
23355796c8dcSSimon Schubert      discussed above.  Then, `unpack_long' will convert that pointer
23365796c8dcSSimon Schubert      back into an address.
23375796c8dcSSimon Schubert 
23385796c8dcSSimon Schubert      So, suppose the user types `disassemble foo' on an architecture
23395796c8dcSSimon Schubert      with a strange function pointer representation, on which GDB
23405796c8dcSSimon Schubert      cannot build its own descriptors, and suppose further that `foo'
23415796c8dcSSimon Schubert      has no linker-built descriptor.  The address->pointer conversion
23425796c8dcSSimon Schubert      will signal an error and prevent the command from running, even
23435796c8dcSSimon Schubert      though the next step would have been to convert the pointer
23445796c8dcSSimon Schubert      directly back into the same address.
23455796c8dcSSimon Schubert 
23465796c8dcSSimon Schubert      The following shortcut avoids this whole mess.  If VAL is a
23475796c8dcSSimon Schubert      function, just return its address directly.  */
23485796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) == TYPE_CODE_FUNC
23495796c8dcSSimon Schubert       || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
23505796c8dcSSimon Schubert     return value_address (val);
23515796c8dcSSimon Schubert 
23525796c8dcSSimon Schubert   val = coerce_array (val);
23535796c8dcSSimon Schubert 
23545796c8dcSSimon Schubert   /* Some architectures (e.g. Harvard), map instruction and data
23555796c8dcSSimon Schubert      addresses onto a single large unified address space.  For
23565796c8dcSSimon Schubert      instance: An architecture may consider a large integer in the
23575796c8dcSSimon Schubert      range 0x10000000 .. 0x1000ffff to already represent a data
23585796c8dcSSimon Schubert      addresses (hence not need a pointer to address conversion) while
23595796c8dcSSimon Schubert      a small integer would still need to be converted integer to
23605796c8dcSSimon Schubert      pointer to address.  Just assume such architectures handle all
23615796c8dcSSimon Schubert      integer conversions in a single function.  */
23625796c8dcSSimon Schubert 
23635796c8dcSSimon Schubert   /* JimB writes:
23645796c8dcSSimon Schubert 
23655796c8dcSSimon Schubert      I think INTEGER_TO_ADDRESS is a good idea as proposed --- but we
23665796c8dcSSimon Schubert      must admonish GDB hackers to make sure its behavior matches the
23675796c8dcSSimon Schubert      compiler's, whenever possible.
23685796c8dcSSimon Schubert 
23695796c8dcSSimon Schubert      In general, I think GDB should evaluate expressions the same way
23705796c8dcSSimon Schubert      the compiler does.  When the user copies an expression out of
23715796c8dcSSimon Schubert      their source code and hands it to a `print' command, they should
23725796c8dcSSimon Schubert      get the same value the compiler would have computed.  Any
23735796c8dcSSimon Schubert      deviation from this rule can cause major confusion and annoyance,
23745796c8dcSSimon Schubert      and needs to be justified carefully.  In other words, GDB doesn't
23755796c8dcSSimon Schubert      really have the freedom to do these conversions in clever and
23765796c8dcSSimon Schubert      useful ways.
23775796c8dcSSimon Schubert 
23785796c8dcSSimon Schubert      AndrewC pointed out that users aren't complaining about how GDB
23795796c8dcSSimon Schubert      casts integers to pointers; they are complaining that they can't
23805796c8dcSSimon Schubert      take an address from a disassembly listing and give it to `x/i'.
23815796c8dcSSimon Schubert      This is certainly important.
23825796c8dcSSimon Schubert 
23835796c8dcSSimon Schubert      Adding an architecture method like integer_to_address() certainly
23845796c8dcSSimon Schubert      makes it possible for GDB to "get it right" in all circumstances
23855796c8dcSSimon Schubert      --- the target has complete control over how things get done, so
23865796c8dcSSimon Schubert      people can Do The Right Thing for their target without breaking
23875796c8dcSSimon Schubert      anyone else.  The standard doesn't specify how integers get
23885796c8dcSSimon Schubert      converted to pointers; usually, the ABI doesn't either, but
23895796c8dcSSimon Schubert      ABI-specific code is a more reasonable place to handle it.  */
23905796c8dcSSimon Schubert 
23915796c8dcSSimon Schubert   if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
23925796c8dcSSimon Schubert       && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
23935796c8dcSSimon Schubert       && gdbarch_integer_to_address_p (gdbarch))
23945796c8dcSSimon Schubert     return gdbarch_integer_to_address (gdbarch, value_type (val),
23955796c8dcSSimon Schubert 				       value_contents (val));
23965796c8dcSSimon Schubert 
23975796c8dcSSimon Schubert   return unpack_long (value_type (val), value_contents (val));
23985796c8dcSSimon Schubert #endif
23995796c8dcSSimon Schubert }
24005796c8dcSSimon Schubert 
24015796c8dcSSimon Schubert /* Unpack raw data (copied from debugee, target byte order) at VALADDR
24025796c8dcSSimon Schubert    as a long, or as a double, assuming the raw data is described
24035796c8dcSSimon Schubert    by type TYPE.  Knows how to convert different sizes of values
24045796c8dcSSimon Schubert    and can convert between fixed and floating point.  We don't assume
24055796c8dcSSimon Schubert    any alignment for the raw data.  Return value is in host byte order.
24065796c8dcSSimon Schubert 
24075796c8dcSSimon Schubert    If you want functions and arrays to be coerced to pointers, and
24085796c8dcSSimon Schubert    references to be dereferenced, call value_as_long() instead.
24095796c8dcSSimon Schubert 
24105796c8dcSSimon Schubert    C++: It is assumed that the front-end has taken care of
24115796c8dcSSimon Schubert    all matters concerning pointers to members.  A pointer
24125796c8dcSSimon Schubert    to member which reaches here is considered to be equivalent
24135796c8dcSSimon Schubert    to an INT (or some size).  After all, it is only an offset.  */
24145796c8dcSSimon Schubert 
24155796c8dcSSimon Schubert LONGEST
unpack_long(struct type * type,const gdb_byte * valaddr)24165796c8dcSSimon Schubert unpack_long (struct type *type, const gdb_byte *valaddr)
24175796c8dcSSimon Schubert {
24185796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
24195796c8dcSSimon Schubert   enum type_code code = TYPE_CODE (type);
24205796c8dcSSimon Schubert   int len = TYPE_LENGTH (type);
24215796c8dcSSimon Schubert   int nosign = TYPE_UNSIGNED (type);
24225796c8dcSSimon Schubert 
24235796c8dcSSimon Schubert   switch (code)
24245796c8dcSSimon Schubert     {
24255796c8dcSSimon Schubert     case TYPE_CODE_TYPEDEF:
24265796c8dcSSimon Schubert       return unpack_long (check_typedef (type), valaddr);
24275796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
24285796c8dcSSimon Schubert     case TYPE_CODE_FLAGS:
24295796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
24305796c8dcSSimon Schubert     case TYPE_CODE_INT:
24315796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
24325796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
24335796c8dcSSimon Schubert     case TYPE_CODE_MEMBERPTR:
24345796c8dcSSimon Schubert       if (nosign)
24355796c8dcSSimon Schubert 	return extract_unsigned_integer (valaddr, len, byte_order);
24365796c8dcSSimon Schubert       else
24375796c8dcSSimon Schubert 	return extract_signed_integer (valaddr, len, byte_order);
24385796c8dcSSimon Schubert 
24395796c8dcSSimon Schubert     case TYPE_CODE_FLT:
24405796c8dcSSimon Schubert       return extract_typed_floating (valaddr, type);
24415796c8dcSSimon Schubert 
24425796c8dcSSimon Schubert     case TYPE_CODE_DECFLOAT:
24435796c8dcSSimon Schubert       /* libdecnumber has a function to convert from decimal to integer, but
24445796c8dcSSimon Schubert 	 it doesn't work when the decimal number has a fractional part.  */
24455796c8dcSSimon Schubert       return decimal_to_doublest (valaddr, len, byte_order);
24465796c8dcSSimon Schubert 
24475796c8dcSSimon Schubert     case TYPE_CODE_PTR:
24485796c8dcSSimon Schubert     case TYPE_CODE_REF:
24495796c8dcSSimon Schubert       /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
24505796c8dcSSimon Schubert          whether we want this to be true eventually.  */
24515796c8dcSSimon Schubert       return extract_typed_address (valaddr, type);
24525796c8dcSSimon Schubert 
24535796c8dcSSimon Schubert     default:
24545796c8dcSSimon Schubert       error (_("Value can't be converted to integer."));
24555796c8dcSSimon Schubert     }
24565796c8dcSSimon Schubert   return 0;			/* Placate lint.  */
24575796c8dcSSimon Schubert }
24585796c8dcSSimon Schubert 
24595796c8dcSSimon Schubert /* Return a double value from the specified type and address.
24605796c8dcSSimon Schubert    INVP points to an int which is set to 0 for valid value,
24615796c8dcSSimon Schubert    1 for invalid value (bad float format).  In either case,
24625796c8dcSSimon Schubert    the returned double is OK to use.  Argument is in target
24635796c8dcSSimon Schubert    format, result is in host format.  */
24645796c8dcSSimon Schubert 
24655796c8dcSSimon Schubert DOUBLEST
unpack_double(struct type * type,const gdb_byte * valaddr,int * invp)24665796c8dcSSimon Schubert unpack_double (struct type *type, const gdb_byte *valaddr, int *invp)
24675796c8dcSSimon Schubert {
24685796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
24695796c8dcSSimon Schubert   enum type_code code;
24705796c8dcSSimon Schubert   int len;
24715796c8dcSSimon Schubert   int nosign;
24725796c8dcSSimon Schubert 
24735796c8dcSSimon Schubert   *invp = 0;			/* Assume valid.  */
24745796c8dcSSimon Schubert   CHECK_TYPEDEF (type);
24755796c8dcSSimon Schubert   code = TYPE_CODE (type);
24765796c8dcSSimon Schubert   len = TYPE_LENGTH (type);
24775796c8dcSSimon Schubert   nosign = TYPE_UNSIGNED (type);
24785796c8dcSSimon Schubert   if (code == TYPE_CODE_FLT)
24795796c8dcSSimon Schubert     {
24805796c8dcSSimon Schubert       /* NOTE: cagney/2002-02-19: There was a test here to see if the
24815796c8dcSSimon Schubert 	 floating-point value was valid (using the macro
24825796c8dcSSimon Schubert 	 INVALID_FLOAT).  That test/macro have been removed.
24835796c8dcSSimon Schubert 
24845796c8dcSSimon Schubert 	 It turns out that only the VAX defined this macro and then
24855796c8dcSSimon Schubert 	 only in a non-portable way.  Fixing the portability problem
24865796c8dcSSimon Schubert 	 wouldn't help since the VAX floating-point code is also badly
24875796c8dcSSimon Schubert 	 bit-rotten.  The target needs to add definitions for the
24885796c8dcSSimon Schubert 	 methods gdbarch_float_format and gdbarch_double_format - these
24895796c8dcSSimon Schubert 	 exactly describe the target floating-point format.  The
24905796c8dcSSimon Schubert 	 problem here is that the corresponding floatformat_vax_f and
24915796c8dcSSimon Schubert 	 floatformat_vax_d values these methods should be set to are
24925796c8dcSSimon Schubert 	 also not defined either.  Oops!
24935796c8dcSSimon Schubert 
24945796c8dcSSimon Schubert          Hopefully someone will add both the missing floatformat
24955796c8dcSSimon Schubert          definitions and the new cases for floatformat_is_valid ().  */
24965796c8dcSSimon Schubert 
24975796c8dcSSimon Schubert       if (!floatformat_is_valid (floatformat_from_type (type), valaddr))
24985796c8dcSSimon Schubert 	{
24995796c8dcSSimon Schubert 	  *invp = 1;
25005796c8dcSSimon Schubert 	  return 0.0;
25015796c8dcSSimon Schubert 	}
25025796c8dcSSimon Schubert 
25035796c8dcSSimon Schubert       return extract_typed_floating (valaddr, type);
25045796c8dcSSimon Schubert     }
25055796c8dcSSimon Schubert   else if (code == TYPE_CODE_DECFLOAT)
25065796c8dcSSimon Schubert     return decimal_to_doublest (valaddr, len, byte_order);
25075796c8dcSSimon Schubert   else if (nosign)
25085796c8dcSSimon Schubert     {
25095796c8dcSSimon Schubert       /* Unsigned -- be sure we compensate for signed LONGEST.  */
25105796c8dcSSimon Schubert       return (ULONGEST) unpack_long (type, valaddr);
25115796c8dcSSimon Schubert     }
25125796c8dcSSimon Schubert   else
25135796c8dcSSimon Schubert     {
25145796c8dcSSimon Schubert       /* Signed -- we are OK with unpack_long.  */
25155796c8dcSSimon Schubert       return unpack_long (type, valaddr);
25165796c8dcSSimon Schubert     }
25175796c8dcSSimon Schubert }
25185796c8dcSSimon Schubert 
25195796c8dcSSimon Schubert /* Unpack raw data (copied from debugee, target byte order) at VALADDR
25205796c8dcSSimon Schubert    as a CORE_ADDR, assuming the raw data is described by type TYPE.
25215796c8dcSSimon Schubert    We don't assume any alignment for the raw data.  Return value is in
25225796c8dcSSimon Schubert    host byte order.
25235796c8dcSSimon Schubert 
25245796c8dcSSimon Schubert    If you want functions and arrays to be coerced to pointers, and
25255796c8dcSSimon Schubert    references to be dereferenced, call value_as_address() instead.
25265796c8dcSSimon Schubert 
25275796c8dcSSimon Schubert    C++: It is assumed that the front-end has taken care of
25285796c8dcSSimon Schubert    all matters concerning pointers to members.  A pointer
25295796c8dcSSimon Schubert    to member which reaches here is considered to be equivalent
25305796c8dcSSimon Schubert    to an INT (or some size).  After all, it is only an offset.  */
25315796c8dcSSimon Schubert 
25325796c8dcSSimon Schubert CORE_ADDR
unpack_pointer(struct type * type,const gdb_byte * valaddr)25335796c8dcSSimon Schubert unpack_pointer (struct type *type, const gdb_byte *valaddr)
25345796c8dcSSimon Schubert {
25355796c8dcSSimon Schubert   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
25365796c8dcSSimon Schubert      whether we want this to be true eventually.  */
25375796c8dcSSimon Schubert   return unpack_long (type, valaddr);
25385796c8dcSSimon Schubert }
25395796c8dcSSimon Schubert 
25405796c8dcSSimon Schubert 
2541cf7f2e2dSJohn Marino /* Get the value of the FIELDNO'th field (which must be static) of
25425796c8dcSSimon Schubert    TYPE.  Return NULL if the field doesn't exist or has been
25435796c8dcSSimon Schubert    optimized out.  */
25445796c8dcSSimon Schubert 
25455796c8dcSSimon Schubert struct value *
value_static_field(struct type * type,int fieldno)25465796c8dcSSimon Schubert value_static_field (struct type *type, int fieldno)
25475796c8dcSSimon Schubert {
25485796c8dcSSimon Schubert   struct value *retval;
25495796c8dcSSimon Schubert 
2550cf7f2e2dSJohn Marino   switch (TYPE_FIELD_LOC_KIND (type, fieldno))
25515796c8dcSSimon Schubert     {
2552cf7f2e2dSJohn Marino     case FIELD_LOC_KIND_PHYSADDR:
2553cf7f2e2dSJohn Marino       retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
25545796c8dcSSimon Schubert 			      TYPE_FIELD_STATIC_PHYSADDR (type, fieldno));
2555cf7f2e2dSJohn Marino       break;
2556cf7f2e2dSJohn Marino     case FIELD_LOC_KIND_PHYSNAME:
25575796c8dcSSimon Schubert     {
2558a45ae5f8SJohn Marino       const char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, fieldno);
2559cf7f2e2dSJohn Marino       /* TYPE_FIELD_NAME (type, fieldno); */
25605796c8dcSSimon Schubert       struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0);
2561cf7f2e2dSJohn Marino 
25625796c8dcSSimon Schubert       if (sym == NULL)
25635796c8dcSSimon Schubert 	{
2564cf7f2e2dSJohn Marino 	  /* With some compilers, e.g. HP aCC, static data members are
2565c50c785cSJohn Marino 	     reported as non-debuggable symbols.  */
2566cf7f2e2dSJohn Marino 	  struct minimal_symbol *msym = lookup_minimal_symbol (phys_name,
2567cf7f2e2dSJohn Marino 							       NULL, NULL);
2568cf7f2e2dSJohn Marino 
25695796c8dcSSimon Schubert 	  if (!msym)
25705796c8dcSSimon Schubert 	    return NULL;
25715796c8dcSSimon Schubert 	  else
25725796c8dcSSimon Schubert 	    {
2573cf7f2e2dSJohn Marino 	      retval = value_at_lazy (TYPE_FIELD_TYPE (type, fieldno),
25745796c8dcSSimon Schubert 				      SYMBOL_VALUE_ADDRESS (msym));
25755796c8dcSSimon Schubert 	    }
25765796c8dcSSimon Schubert 	}
25775796c8dcSSimon Schubert       else
2578c50c785cSJohn Marino 	retval = value_of_variable (sym, NULL);
2579cf7f2e2dSJohn Marino       break;
25805796c8dcSSimon Schubert     }
2581cf7f2e2dSJohn Marino     default:
2582c50c785cSJohn Marino       gdb_assert_not_reached ("unexpected field location kind");
2583cf7f2e2dSJohn Marino     }
2584cf7f2e2dSJohn Marino 
25855796c8dcSSimon Schubert   return retval;
25865796c8dcSSimon Schubert }
25875796c8dcSSimon Schubert 
25885796c8dcSSimon Schubert /* Change the enclosing type of a value object VAL to NEW_ENCL_TYPE.
25895796c8dcSSimon Schubert    You have to be careful here, since the size of the data area for the value
25905796c8dcSSimon Schubert    is set by the length of the enclosing type.  So if NEW_ENCL_TYPE is bigger
2591c50c785cSJohn Marino    than the old enclosing type, you have to allocate more space for the
2592c50c785cSJohn Marino    data.  */
25935796c8dcSSimon Schubert 
2594c50c785cSJohn Marino void
set_value_enclosing_type(struct value * val,struct type * new_encl_type)2595c50c785cSJohn Marino set_value_enclosing_type (struct value *val, struct type *new_encl_type)
25965796c8dcSSimon Schubert {
25975796c8dcSSimon Schubert   if (TYPE_LENGTH (new_encl_type) > TYPE_LENGTH (value_enclosing_type (val)))
25985796c8dcSSimon Schubert     val->contents =
25995796c8dcSSimon Schubert       (gdb_byte *) xrealloc (val->contents, TYPE_LENGTH (new_encl_type));
26005796c8dcSSimon Schubert 
26015796c8dcSSimon Schubert   val->enclosing_type = new_encl_type;
26025796c8dcSSimon Schubert }
26035796c8dcSSimon Schubert 
26045796c8dcSSimon Schubert /* Given a value ARG1 (offset by OFFSET bytes)
26055796c8dcSSimon Schubert    of a struct or union type ARG_TYPE,
26065796c8dcSSimon Schubert    extract and return the value of one of its (non-static) fields.
26075796c8dcSSimon Schubert    FIELDNO says which field.  */
26085796c8dcSSimon Schubert 
26095796c8dcSSimon Schubert struct value *
value_primitive_field(struct value * arg1,int offset,int fieldno,struct type * arg_type)26105796c8dcSSimon Schubert value_primitive_field (struct value *arg1, int offset,
26115796c8dcSSimon Schubert 		       int fieldno, struct type *arg_type)
26125796c8dcSSimon Schubert {
26135796c8dcSSimon Schubert   struct value *v;
26145796c8dcSSimon Schubert   struct type *type;
26155796c8dcSSimon Schubert 
26165796c8dcSSimon Schubert   CHECK_TYPEDEF (arg_type);
26175796c8dcSSimon Schubert   type = TYPE_FIELD_TYPE (arg_type, fieldno);
26185796c8dcSSimon Schubert 
2619cf7f2e2dSJohn Marino   /* Call check_typedef on our type to make sure that, if TYPE
2620cf7f2e2dSJohn Marino      is a TYPE_CODE_TYPEDEF, its length is set to the length
2621cf7f2e2dSJohn Marino      of the target type instead of zero.  However, we do not
2622cf7f2e2dSJohn Marino      replace the typedef type by the target type, because we want
2623cf7f2e2dSJohn Marino      to keep the typedef in order to be able to print the type
2624cf7f2e2dSJohn Marino      description correctly.  */
2625cf7f2e2dSJohn Marino   check_typedef (type);
2626cf7f2e2dSJohn Marino 
2627a45ae5f8SJohn Marino   if (value_optimized_out (arg1))
2628a45ae5f8SJohn Marino     v = allocate_optimized_out_value (type);
2629a45ae5f8SJohn Marino   else if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
26305796c8dcSSimon Schubert     {
2631a45ae5f8SJohn Marino       /* Handle packed fields.
2632a45ae5f8SJohn Marino 
2633a45ae5f8SJohn Marino 	 Create a new value for the bitfield, with bitpos and bitsize
26345796c8dcSSimon Schubert 	 set.  If possible, arrange offset and bitpos so that we can
26355796c8dcSSimon Schubert 	 do a single aligned read of the size of the containing type.
26365796c8dcSSimon Schubert 	 Otherwise, adjust offset to the byte containing the first
26375796c8dcSSimon Schubert 	 bit.  Assume that the address, offset, and embedded offset
26385796c8dcSSimon Schubert 	 are sufficiently aligned.  */
2639a45ae5f8SJohn Marino 
26405796c8dcSSimon Schubert       int bitpos = TYPE_FIELD_BITPOS (arg_type, fieldno);
26415796c8dcSSimon Schubert       int container_bitsize = TYPE_LENGTH (type) * 8;
26425796c8dcSSimon Schubert 
26435796c8dcSSimon Schubert       v = allocate_value_lazy (type);
26445796c8dcSSimon Schubert       v->bitsize = TYPE_FIELD_BITSIZE (arg_type, fieldno);
26455796c8dcSSimon Schubert       if ((bitpos % container_bitsize) + v->bitsize <= container_bitsize
26465796c8dcSSimon Schubert 	  && TYPE_LENGTH (type) <= (int) sizeof (LONGEST))
26475796c8dcSSimon Schubert 	v->bitpos = bitpos % container_bitsize;
26485796c8dcSSimon Schubert       else
26495796c8dcSSimon Schubert 	v->bitpos = bitpos % 8;
2650c50c785cSJohn Marino       v->offset = (value_embedded_offset (arg1)
2651c50c785cSJohn Marino 		   + offset
2652c50c785cSJohn Marino 		   + (bitpos - v->bitpos) / 8);
26535796c8dcSSimon Schubert       v->parent = arg1;
26545796c8dcSSimon Schubert       value_incref (v->parent);
26555796c8dcSSimon Schubert       if (!value_lazy (arg1))
26565796c8dcSSimon Schubert 	value_fetch_lazy (v);
26575796c8dcSSimon Schubert     }
26585796c8dcSSimon Schubert   else if (fieldno < TYPE_N_BASECLASSES (arg_type))
26595796c8dcSSimon Schubert     {
26605796c8dcSSimon Schubert       /* This field is actually a base subobject, so preserve the
26615796c8dcSSimon Schubert 	 entire object's contents for later references to virtual
26625796c8dcSSimon Schubert 	 bases, etc.  */
2663*ef5ccd6cSJohn Marino       int boffset;
26645796c8dcSSimon Schubert 
26655796c8dcSSimon Schubert       /* Lazy register values with offsets are not supported.  */
26665796c8dcSSimon Schubert       if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
26675796c8dcSSimon Schubert 	value_fetch_lazy (arg1);
26685796c8dcSSimon Schubert 
2669*ef5ccd6cSJohn Marino       /* We special case virtual inheritance here because this
2670*ef5ccd6cSJohn Marino 	 requires access to the contents, which we would rather avoid
2671*ef5ccd6cSJohn Marino 	 for references to ordinary fields of unavailable values.  */
2672*ef5ccd6cSJohn Marino       if (BASETYPE_VIA_VIRTUAL (arg_type, fieldno))
2673*ef5ccd6cSJohn Marino 	boffset = baseclass_offset (arg_type, fieldno,
2674*ef5ccd6cSJohn Marino 				    value_contents (arg1),
2675*ef5ccd6cSJohn Marino 				    value_embedded_offset (arg1),
2676*ef5ccd6cSJohn Marino 				    value_address (arg1),
2677*ef5ccd6cSJohn Marino 				    arg1);
2678*ef5ccd6cSJohn Marino       else
2679*ef5ccd6cSJohn Marino 	boffset = TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
2680*ef5ccd6cSJohn Marino 
26815796c8dcSSimon Schubert       if (value_lazy (arg1))
26825796c8dcSSimon Schubert 	v = allocate_value_lazy (value_enclosing_type (arg1));
26835796c8dcSSimon Schubert       else
26845796c8dcSSimon Schubert 	{
26855796c8dcSSimon Schubert 	  v = allocate_value (value_enclosing_type (arg1));
2686c50c785cSJohn Marino 	  value_contents_copy_raw (v, 0, arg1, 0,
26875796c8dcSSimon Schubert 				   TYPE_LENGTH (value_enclosing_type (arg1)));
26885796c8dcSSimon Schubert 	}
26895796c8dcSSimon Schubert       v->type = type;
26905796c8dcSSimon Schubert       v->offset = value_offset (arg1);
2691*ef5ccd6cSJohn Marino       v->embedded_offset = offset + value_embedded_offset (arg1) + boffset;
26925796c8dcSSimon Schubert     }
26935796c8dcSSimon Schubert   else
26945796c8dcSSimon Schubert     {
26955796c8dcSSimon Schubert       /* Plain old data member */
26965796c8dcSSimon Schubert       offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
26975796c8dcSSimon Schubert 
26985796c8dcSSimon Schubert       /* Lazy register values with offsets are not supported.  */
26995796c8dcSSimon Schubert       if (VALUE_LVAL (arg1) == lval_register && value_lazy (arg1))
27005796c8dcSSimon Schubert 	value_fetch_lazy (arg1);
27015796c8dcSSimon Schubert 
27025796c8dcSSimon Schubert       if (value_lazy (arg1))
27035796c8dcSSimon Schubert 	v = allocate_value_lazy (type);
27045796c8dcSSimon Schubert       else
27055796c8dcSSimon Schubert 	{
27065796c8dcSSimon Schubert 	  v = allocate_value (type);
2707c50c785cSJohn Marino 	  value_contents_copy_raw (v, value_embedded_offset (v),
2708c50c785cSJohn Marino 				   arg1, value_embedded_offset (arg1) + offset,
27095796c8dcSSimon Schubert 				   TYPE_LENGTH (type));
27105796c8dcSSimon Schubert 	}
27115796c8dcSSimon Schubert       v->offset = (value_offset (arg1) + offset
27125796c8dcSSimon Schubert 		   + value_embedded_offset (arg1));
27135796c8dcSSimon Schubert     }
27145796c8dcSSimon Schubert   set_value_component_location (v, arg1);
27155796c8dcSSimon Schubert   VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
27165796c8dcSSimon Schubert   VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
27175796c8dcSSimon Schubert   return v;
27185796c8dcSSimon Schubert }
27195796c8dcSSimon Schubert 
27205796c8dcSSimon Schubert /* Given a value ARG1 of a struct or union type,
27215796c8dcSSimon Schubert    extract and return the value of one of its (non-static) fields.
27225796c8dcSSimon Schubert    FIELDNO says which field.  */
27235796c8dcSSimon Schubert 
27245796c8dcSSimon Schubert struct value *
value_field(struct value * arg1,int fieldno)27255796c8dcSSimon Schubert value_field (struct value *arg1, int fieldno)
27265796c8dcSSimon Schubert {
27275796c8dcSSimon Schubert   return value_primitive_field (arg1, 0, fieldno, value_type (arg1));
27285796c8dcSSimon Schubert }
27295796c8dcSSimon Schubert 
27305796c8dcSSimon Schubert /* Return a non-virtual function as a value.
27315796c8dcSSimon Schubert    F is the list of member functions which contains the desired method.
27325796c8dcSSimon Schubert    J is an index into F which provides the desired method.
27335796c8dcSSimon Schubert 
27345796c8dcSSimon Schubert    We only use the symbol for its address, so be happy with either a
2735c50c785cSJohn Marino    full symbol or a minimal symbol.  */
27365796c8dcSSimon Schubert 
27375796c8dcSSimon Schubert struct value *
value_fn_field(struct value ** arg1p,struct fn_field * f,int j,struct type * type,int offset)2738c50c785cSJohn Marino value_fn_field (struct value **arg1p, struct fn_field *f,
2739c50c785cSJohn Marino 		int j, struct type *type,
27405796c8dcSSimon Schubert 		int offset)
27415796c8dcSSimon Schubert {
27425796c8dcSSimon Schubert   struct value *v;
27435796c8dcSSimon Schubert   struct type *ftype = TYPE_FN_FIELD_TYPE (f, j);
2744a45ae5f8SJohn Marino   const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
27455796c8dcSSimon Schubert   struct symbol *sym;
27465796c8dcSSimon Schubert   struct minimal_symbol *msym;
27475796c8dcSSimon Schubert 
27485796c8dcSSimon Schubert   sym = lookup_symbol (physname, 0, VAR_DOMAIN, 0);
27495796c8dcSSimon Schubert   if (sym != NULL)
27505796c8dcSSimon Schubert     {
27515796c8dcSSimon Schubert       msym = NULL;
27525796c8dcSSimon Schubert     }
27535796c8dcSSimon Schubert   else
27545796c8dcSSimon Schubert     {
27555796c8dcSSimon Schubert       gdb_assert (sym == NULL);
27565796c8dcSSimon Schubert       msym = lookup_minimal_symbol (physname, NULL, NULL);
27575796c8dcSSimon Schubert       if (msym == NULL)
27585796c8dcSSimon Schubert 	return NULL;
27595796c8dcSSimon Schubert     }
27605796c8dcSSimon Schubert 
27615796c8dcSSimon Schubert   v = allocate_value (ftype);
27625796c8dcSSimon Schubert   if (sym)
27635796c8dcSSimon Schubert     {
27645796c8dcSSimon Schubert       set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
27655796c8dcSSimon Schubert     }
27665796c8dcSSimon Schubert   else
27675796c8dcSSimon Schubert     {
27685796c8dcSSimon Schubert       /* The minimal symbol might point to a function descriptor;
27695796c8dcSSimon Schubert 	 resolve it to the actual code address instead.  */
27705796c8dcSSimon Schubert       struct objfile *objfile = msymbol_objfile (msym);
27715796c8dcSSimon Schubert       struct gdbarch *gdbarch = get_objfile_arch (objfile);
27725796c8dcSSimon Schubert 
27735796c8dcSSimon Schubert       set_value_address (v,
27745796c8dcSSimon Schubert 	gdbarch_convert_from_func_ptr_addr
27755796c8dcSSimon Schubert 	   (gdbarch, SYMBOL_VALUE_ADDRESS (msym), &current_target));
27765796c8dcSSimon Schubert     }
27775796c8dcSSimon Schubert 
27785796c8dcSSimon Schubert   if (arg1p)
27795796c8dcSSimon Schubert     {
27805796c8dcSSimon Schubert       if (type != value_type (*arg1p))
27815796c8dcSSimon Schubert 	*arg1p = value_ind (value_cast (lookup_pointer_type (type),
27825796c8dcSSimon Schubert 					value_addr (*arg1p)));
27835796c8dcSSimon Schubert 
27845796c8dcSSimon Schubert       /* Move the `this' pointer according to the offset.
2785c50c785cSJohn Marino          VALUE_OFFSET (*arg1p) += offset; */
27865796c8dcSSimon Schubert     }
27875796c8dcSSimon Schubert 
27885796c8dcSSimon Schubert   return v;
27895796c8dcSSimon Schubert }
27905796c8dcSSimon Schubert 
27915796c8dcSSimon Schubert 
27925796c8dcSSimon Schubert 
2793c50c785cSJohn Marino /* Helper function for both unpack_value_bits_as_long and
2794c50c785cSJohn Marino    unpack_bits_as_long.  See those functions for more details on the
2795c50c785cSJohn Marino    interface; the only difference is that this function accepts either
2796c50c785cSJohn Marino    a NULL or a non-NULL ORIGINAL_VALUE.  */
27975796c8dcSSimon Schubert 
2798c50c785cSJohn Marino static int
unpack_value_bits_as_long_1(struct type * field_type,const gdb_byte * valaddr,int embedded_offset,int bitpos,int bitsize,const struct value * original_value,LONGEST * result)2799c50c785cSJohn Marino unpack_value_bits_as_long_1 (struct type *field_type, const gdb_byte *valaddr,
2800c50c785cSJohn Marino 			     int embedded_offset, int bitpos, int bitsize,
2801c50c785cSJohn Marino 			     const struct value *original_value,
2802c50c785cSJohn Marino 			     LONGEST *result)
28035796c8dcSSimon Schubert {
28045796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (field_type));
28055796c8dcSSimon Schubert   ULONGEST val;
28065796c8dcSSimon Schubert   ULONGEST valmask;
28075796c8dcSSimon Schubert   int lsbcount;
28085796c8dcSSimon Schubert   int bytes_read;
2809c50c785cSJohn Marino   int read_offset;
28105796c8dcSSimon Schubert 
28115796c8dcSSimon Schubert   /* Read the minimum number of bytes required; there may not be
28125796c8dcSSimon Schubert      enough bytes to read an entire ULONGEST.  */
28135796c8dcSSimon Schubert   CHECK_TYPEDEF (field_type);
28145796c8dcSSimon Schubert   if (bitsize)
28155796c8dcSSimon Schubert     bytes_read = ((bitpos % 8) + bitsize + 7) / 8;
28165796c8dcSSimon Schubert   else
28175796c8dcSSimon Schubert     bytes_read = TYPE_LENGTH (field_type);
28185796c8dcSSimon Schubert 
2819c50c785cSJohn Marino   read_offset = bitpos / 8;
2820c50c785cSJohn Marino 
2821c50c785cSJohn Marino   if (original_value != NULL
2822c50c785cSJohn Marino       && !value_bytes_available (original_value, embedded_offset + read_offset,
2823c50c785cSJohn Marino 				 bytes_read))
2824c50c785cSJohn Marino     return 0;
2825c50c785cSJohn Marino 
2826c50c785cSJohn Marino   val = extract_unsigned_integer (valaddr + embedded_offset + read_offset,
28275796c8dcSSimon Schubert 				  bytes_read, byte_order);
28285796c8dcSSimon Schubert 
28295796c8dcSSimon Schubert   /* Extract bits.  See comment above.  */
28305796c8dcSSimon Schubert 
28315796c8dcSSimon Schubert   if (gdbarch_bits_big_endian (get_type_arch (field_type)))
28325796c8dcSSimon Schubert     lsbcount = (bytes_read * 8 - bitpos % 8 - bitsize);
28335796c8dcSSimon Schubert   else
28345796c8dcSSimon Schubert     lsbcount = (bitpos % 8);
28355796c8dcSSimon Schubert   val >>= lsbcount;
28365796c8dcSSimon Schubert 
28375796c8dcSSimon Schubert   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
28385796c8dcSSimon Schubert      If the field is signed, and is negative, then sign extend.  */
28395796c8dcSSimon Schubert 
28405796c8dcSSimon Schubert   if ((bitsize > 0) && (bitsize < 8 * (int) sizeof (val)))
28415796c8dcSSimon Schubert     {
28425796c8dcSSimon Schubert       valmask = (((ULONGEST) 1) << bitsize) - 1;
28435796c8dcSSimon Schubert       val &= valmask;
28445796c8dcSSimon Schubert       if (!TYPE_UNSIGNED (field_type))
28455796c8dcSSimon Schubert 	{
28465796c8dcSSimon Schubert 	  if (val & (valmask ^ (valmask >> 1)))
28475796c8dcSSimon Schubert 	    {
28485796c8dcSSimon Schubert 	      val |= ~valmask;
28495796c8dcSSimon Schubert 	    }
28505796c8dcSSimon Schubert 	}
28515796c8dcSSimon Schubert     }
2852c50c785cSJohn Marino 
2853c50c785cSJohn Marino   *result = val;
2854c50c785cSJohn Marino   return 1;
28555796c8dcSSimon Schubert }
28565796c8dcSSimon Schubert 
2857c50c785cSJohn Marino /* Unpack a bitfield of the specified FIELD_TYPE, from the object at
2858c50c785cSJohn Marino    VALADDR + EMBEDDED_OFFSET, and store the result in *RESULT.
2859c50c785cSJohn Marino    VALADDR points to the contents of ORIGINAL_VALUE, which must not be
2860c50c785cSJohn Marino    NULL.  The bitfield starts at BITPOS bits and contains BITSIZE
2861c50c785cSJohn Marino    bits.
28625796c8dcSSimon Schubert 
2863c50c785cSJohn Marino    Returns false if the value contents are unavailable, otherwise
2864c50c785cSJohn Marino    returns true, indicating a valid value has been stored in *RESULT.
2865c50c785cSJohn Marino 
2866c50c785cSJohn Marino    Extracting bits depends on endianness of the machine.  Compute the
2867c50c785cSJohn Marino    number of least significant bits to discard.  For big endian machines,
2868c50c785cSJohn Marino    we compute the total number of bits in the anonymous object, subtract
2869c50c785cSJohn Marino    off the bit count from the MSB of the object to the MSB of the
2870c50c785cSJohn Marino    bitfield, then the size of the bitfield, which leaves the LSB discard
2871c50c785cSJohn Marino    count.  For little endian machines, the discard count is simply the
2872c50c785cSJohn Marino    number of bits from the LSB of the anonymous object to the LSB of the
2873c50c785cSJohn Marino    bitfield.
2874c50c785cSJohn Marino 
2875c50c785cSJohn Marino    If the field is signed, we also do sign extension.  */
2876c50c785cSJohn Marino 
2877c50c785cSJohn Marino int
unpack_value_bits_as_long(struct type * field_type,const gdb_byte * valaddr,int embedded_offset,int bitpos,int bitsize,const struct value * original_value,LONGEST * result)2878c50c785cSJohn Marino unpack_value_bits_as_long (struct type *field_type, const gdb_byte *valaddr,
2879c50c785cSJohn Marino 			   int embedded_offset, int bitpos, int bitsize,
2880c50c785cSJohn Marino 			   const struct value *original_value,
2881c50c785cSJohn Marino 			   LONGEST *result)
2882c50c785cSJohn Marino {
2883c50c785cSJohn Marino   gdb_assert (original_value != NULL);
2884c50c785cSJohn Marino 
2885c50c785cSJohn Marino   return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2886c50c785cSJohn Marino 				      bitpos, bitsize, original_value, result);
2887c50c785cSJohn Marino 
2888c50c785cSJohn Marino }
2889c50c785cSJohn Marino 
2890c50c785cSJohn Marino /* Unpack a field FIELDNO of the specified TYPE, from the object at
2891c50c785cSJohn Marino    VALADDR + EMBEDDED_OFFSET.  VALADDR points to the contents of
2892c50c785cSJohn Marino    ORIGINAL_VALUE.  See unpack_value_bits_as_long for more
2893c50c785cSJohn Marino    details.  */
2894c50c785cSJohn Marino 
2895c50c785cSJohn Marino static int
unpack_value_field_as_long_1(struct type * type,const gdb_byte * valaddr,int embedded_offset,int fieldno,const struct value * val,LONGEST * result)2896c50c785cSJohn Marino unpack_value_field_as_long_1 (struct type *type, const gdb_byte *valaddr,
2897c50c785cSJohn Marino 			      int embedded_offset, int fieldno,
2898c50c785cSJohn Marino 			      const struct value *val, LONGEST *result)
28995796c8dcSSimon Schubert {
29005796c8dcSSimon Schubert   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
29015796c8dcSSimon Schubert   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
29025796c8dcSSimon Schubert   struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
29035796c8dcSSimon Schubert 
2904c50c785cSJohn Marino   return unpack_value_bits_as_long_1 (field_type, valaddr, embedded_offset,
2905c50c785cSJohn Marino 				      bitpos, bitsize, val,
2906c50c785cSJohn Marino 				      result);
2907c50c785cSJohn Marino }
2908c50c785cSJohn Marino 
2909c50c785cSJohn Marino /* Unpack a field FIELDNO of the specified TYPE, from the object at
2910c50c785cSJohn Marino    VALADDR + EMBEDDED_OFFSET.  VALADDR points to the contents of
2911c50c785cSJohn Marino    ORIGINAL_VALUE, which must not be NULL.  See
2912c50c785cSJohn Marino    unpack_value_bits_as_long for more details.  */
2913c50c785cSJohn Marino 
2914c50c785cSJohn Marino int
unpack_value_field_as_long(struct type * type,const gdb_byte * valaddr,int embedded_offset,int fieldno,const struct value * val,LONGEST * result)2915c50c785cSJohn Marino unpack_value_field_as_long (struct type *type, const gdb_byte *valaddr,
2916c50c785cSJohn Marino 			    int embedded_offset, int fieldno,
2917c50c785cSJohn Marino 			    const struct value *val, LONGEST *result)
2918c50c785cSJohn Marino {
2919c50c785cSJohn Marino   gdb_assert (val != NULL);
2920c50c785cSJohn Marino 
2921c50c785cSJohn Marino   return unpack_value_field_as_long_1 (type, valaddr, embedded_offset,
2922c50c785cSJohn Marino 				       fieldno, val, result);
2923c50c785cSJohn Marino }
2924c50c785cSJohn Marino 
2925c50c785cSJohn Marino /* Unpack a field FIELDNO of the specified TYPE, from the anonymous
2926c50c785cSJohn Marino    object at VALADDR.  See unpack_value_bits_as_long for more details.
2927c50c785cSJohn Marino    This function differs from unpack_value_field_as_long in that it
2928c50c785cSJohn Marino    operates without a struct value object.  */
2929c50c785cSJohn Marino 
2930c50c785cSJohn Marino LONGEST
unpack_field_as_long(struct type * type,const gdb_byte * valaddr,int fieldno)2931c50c785cSJohn Marino unpack_field_as_long (struct type *type, const gdb_byte *valaddr, int fieldno)
2932c50c785cSJohn Marino {
2933c50c785cSJohn Marino   LONGEST result;
2934c50c785cSJohn Marino 
2935c50c785cSJohn Marino   unpack_value_field_as_long_1 (type, valaddr, 0, fieldno, NULL, &result);
2936c50c785cSJohn Marino   return result;
2937c50c785cSJohn Marino }
2938c50c785cSJohn Marino 
2939c50c785cSJohn Marino /* Return a new value with type TYPE, which is FIELDNO field of the
2940c50c785cSJohn Marino    object at VALADDR + EMBEDDEDOFFSET.  VALADDR points to the contents
2941c50c785cSJohn Marino    of VAL.  If the VAL's contents required to extract the bitfield
2942c50c785cSJohn Marino    from are unavailable, the new value is correspondingly marked as
2943c50c785cSJohn Marino    unavailable.  */
2944c50c785cSJohn Marino 
2945c50c785cSJohn Marino struct value *
value_field_bitfield(struct type * type,int fieldno,const gdb_byte * valaddr,int embedded_offset,const struct value * val)2946c50c785cSJohn Marino value_field_bitfield (struct type *type, int fieldno,
2947c50c785cSJohn Marino 		      const gdb_byte *valaddr,
2948c50c785cSJohn Marino 		      int embedded_offset, const struct value *val)
2949c50c785cSJohn Marino {
2950c50c785cSJohn Marino   LONGEST l;
2951c50c785cSJohn Marino 
2952c50c785cSJohn Marino   if (!unpack_value_field_as_long (type, valaddr, embedded_offset, fieldno,
2953c50c785cSJohn Marino 				   val, &l))
2954c50c785cSJohn Marino     {
2955c50c785cSJohn Marino       struct type *field_type = TYPE_FIELD_TYPE (type, fieldno);
2956c50c785cSJohn Marino       struct value *retval = allocate_value (field_type);
2957c50c785cSJohn Marino       mark_value_bytes_unavailable (retval, 0, TYPE_LENGTH (field_type));
2958c50c785cSJohn Marino       return retval;
2959c50c785cSJohn Marino     }
2960c50c785cSJohn Marino   else
2961c50c785cSJohn Marino     {
2962c50c785cSJohn Marino       return value_from_longest (TYPE_FIELD_TYPE (type, fieldno), l);
2963c50c785cSJohn Marino     }
29645796c8dcSSimon Schubert }
29655796c8dcSSimon Schubert 
29665796c8dcSSimon Schubert /* Modify the value of a bitfield.  ADDR points to a block of memory in
29675796c8dcSSimon Schubert    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
29685796c8dcSSimon Schubert    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
29695796c8dcSSimon Schubert    indicate which bits (in target bit order) comprise the bitfield.
2970c50c785cSJohn Marino    Requires 0 < BITSIZE <= lbits, 0 <= BITPOS % 8 + BITSIZE <= lbits, and
29715796c8dcSSimon Schubert    0 <= BITPOS, where lbits is the size of a LONGEST in bits.  */
29725796c8dcSSimon Schubert 
29735796c8dcSSimon Schubert void
modify_field(struct type * type,gdb_byte * addr,LONGEST fieldval,int bitpos,int bitsize)29745796c8dcSSimon Schubert modify_field (struct type *type, gdb_byte *addr,
29755796c8dcSSimon Schubert 	      LONGEST fieldval, int bitpos, int bitsize)
29765796c8dcSSimon Schubert {
29775796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
29785796c8dcSSimon Schubert   ULONGEST oword;
29795796c8dcSSimon Schubert   ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
2980c50c785cSJohn Marino   int bytesize;
2981c50c785cSJohn Marino 
2982c50c785cSJohn Marino   /* Normalize BITPOS.  */
2983c50c785cSJohn Marino   addr += bitpos / 8;
2984c50c785cSJohn Marino   bitpos %= 8;
29855796c8dcSSimon Schubert 
29865796c8dcSSimon Schubert   /* If a negative fieldval fits in the field in question, chop
29875796c8dcSSimon Schubert      off the sign extension bits.  */
29885796c8dcSSimon Schubert   if ((~fieldval & ~(mask >> 1)) == 0)
29895796c8dcSSimon Schubert     fieldval &= mask;
29905796c8dcSSimon Schubert 
29915796c8dcSSimon Schubert   /* Warn if value is too big to fit in the field in question.  */
29925796c8dcSSimon Schubert   if (0 != (fieldval & ~mask))
29935796c8dcSSimon Schubert     {
29945796c8dcSSimon Schubert       /* FIXME: would like to include fieldval in the message, but
29955796c8dcSSimon Schubert          we don't have a sprintf_longest.  */
29965796c8dcSSimon Schubert       warning (_("Value does not fit in %d bits."), bitsize);
29975796c8dcSSimon Schubert 
29985796c8dcSSimon Schubert       /* Truncate it, otherwise adjoining fields may be corrupted.  */
29995796c8dcSSimon Schubert       fieldval &= mask;
30005796c8dcSSimon Schubert     }
30015796c8dcSSimon Schubert 
3002c50c785cSJohn Marino   /* Ensure no bytes outside of the modified ones get accessed as it may cause
3003c50c785cSJohn Marino      false valgrind reports.  */
3004c50c785cSJohn Marino 
3005c50c785cSJohn Marino   bytesize = (bitpos + bitsize + 7) / 8;
3006c50c785cSJohn Marino   oword = extract_unsigned_integer (addr, bytesize, byte_order);
30075796c8dcSSimon Schubert 
30085796c8dcSSimon Schubert   /* Shifting for bit field depends on endianness of the target machine.  */
30095796c8dcSSimon Schubert   if (gdbarch_bits_big_endian (get_type_arch (type)))
3010c50c785cSJohn Marino     bitpos = bytesize * 8 - bitpos - bitsize;
30115796c8dcSSimon Schubert 
30125796c8dcSSimon Schubert   oword &= ~(mask << bitpos);
30135796c8dcSSimon Schubert   oword |= fieldval << bitpos;
30145796c8dcSSimon Schubert 
3015c50c785cSJohn Marino   store_unsigned_integer (addr, bytesize, byte_order, oword);
30165796c8dcSSimon Schubert }
30175796c8dcSSimon Schubert 
30185796c8dcSSimon Schubert /* Pack NUM into BUF using a target format of TYPE.  */
30195796c8dcSSimon Schubert 
30205796c8dcSSimon Schubert void
pack_long(gdb_byte * buf,struct type * type,LONGEST num)30215796c8dcSSimon Schubert pack_long (gdb_byte *buf, struct type *type, LONGEST num)
30225796c8dcSSimon Schubert {
30235796c8dcSSimon Schubert   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
30245796c8dcSSimon Schubert   int len;
30255796c8dcSSimon Schubert 
30265796c8dcSSimon Schubert   type = check_typedef (type);
30275796c8dcSSimon Schubert   len = TYPE_LENGTH (type);
30285796c8dcSSimon Schubert 
30295796c8dcSSimon Schubert   switch (TYPE_CODE (type))
30305796c8dcSSimon Schubert     {
30315796c8dcSSimon Schubert     case TYPE_CODE_INT:
30325796c8dcSSimon Schubert     case TYPE_CODE_CHAR:
30335796c8dcSSimon Schubert     case TYPE_CODE_ENUM:
30345796c8dcSSimon Schubert     case TYPE_CODE_FLAGS:
30355796c8dcSSimon Schubert     case TYPE_CODE_BOOL:
30365796c8dcSSimon Schubert     case TYPE_CODE_RANGE:
30375796c8dcSSimon Schubert     case TYPE_CODE_MEMBERPTR:
30385796c8dcSSimon Schubert       store_signed_integer (buf, len, byte_order, num);
30395796c8dcSSimon Schubert       break;
30405796c8dcSSimon Schubert 
30415796c8dcSSimon Schubert     case TYPE_CODE_REF:
30425796c8dcSSimon Schubert     case TYPE_CODE_PTR:
30435796c8dcSSimon Schubert       store_typed_address (buf, type, (CORE_ADDR) num);
30445796c8dcSSimon Schubert       break;
30455796c8dcSSimon Schubert 
30465796c8dcSSimon Schubert     default:
30475796c8dcSSimon Schubert       error (_("Unexpected type (%d) encountered for integer constant."),
30485796c8dcSSimon Schubert 	     TYPE_CODE (type));
30495796c8dcSSimon Schubert     }
30505796c8dcSSimon Schubert }
30515796c8dcSSimon Schubert 
30525796c8dcSSimon Schubert 
3053cf7f2e2dSJohn Marino /* Pack NUM into BUF using a target format of TYPE.  */
3054cf7f2e2dSJohn Marino 
3055*ef5ccd6cSJohn Marino static void
pack_unsigned_long(gdb_byte * buf,struct type * type,ULONGEST num)3056cf7f2e2dSJohn Marino pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
3057cf7f2e2dSJohn Marino {
3058cf7f2e2dSJohn Marino   int len;
3059cf7f2e2dSJohn Marino   enum bfd_endian byte_order;
3060cf7f2e2dSJohn Marino 
3061cf7f2e2dSJohn Marino   type = check_typedef (type);
3062cf7f2e2dSJohn Marino   len = TYPE_LENGTH (type);
3063cf7f2e2dSJohn Marino   byte_order = gdbarch_byte_order (get_type_arch (type));
3064cf7f2e2dSJohn Marino 
3065cf7f2e2dSJohn Marino   switch (TYPE_CODE (type))
3066cf7f2e2dSJohn Marino     {
3067cf7f2e2dSJohn Marino     case TYPE_CODE_INT:
3068cf7f2e2dSJohn Marino     case TYPE_CODE_CHAR:
3069cf7f2e2dSJohn Marino     case TYPE_CODE_ENUM:
3070cf7f2e2dSJohn Marino     case TYPE_CODE_FLAGS:
3071cf7f2e2dSJohn Marino     case TYPE_CODE_BOOL:
3072cf7f2e2dSJohn Marino     case TYPE_CODE_RANGE:
3073cf7f2e2dSJohn Marino     case TYPE_CODE_MEMBERPTR:
3074cf7f2e2dSJohn Marino       store_unsigned_integer (buf, len, byte_order, num);
3075cf7f2e2dSJohn Marino       break;
3076cf7f2e2dSJohn Marino 
3077cf7f2e2dSJohn Marino     case TYPE_CODE_REF:
3078cf7f2e2dSJohn Marino     case TYPE_CODE_PTR:
3079cf7f2e2dSJohn Marino       store_typed_address (buf, type, (CORE_ADDR) num);
3080cf7f2e2dSJohn Marino       break;
3081cf7f2e2dSJohn Marino 
3082cf7f2e2dSJohn Marino     default:
3083c50c785cSJohn Marino       error (_("Unexpected type (%d) encountered "
3084c50c785cSJohn Marino 	       "for unsigned integer constant."),
3085cf7f2e2dSJohn Marino 	     TYPE_CODE (type));
3086cf7f2e2dSJohn Marino     }
3087cf7f2e2dSJohn Marino }
3088cf7f2e2dSJohn Marino 
3089cf7f2e2dSJohn Marino 
30905796c8dcSSimon Schubert /* Convert C numbers into newly allocated values.  */
30915796c8dcSSimon Schubert 
30925796c8dcSSimon Schubert struct value *
value_from_longest(struct type * type,LONGEST num)30935796c8dcSSimon Schubert value_from_longest (struct type *type, LONGEST num)
30945796c8dcSSimon Schubert {
30955796c8dcSSimon Schubert   struct value *val = allocate_value (type);
30965796c8dcSSimon Schubert 
30975796c8dcSSimon Schubert   pack_long (value_contents_raw (val), type, num);
3098cf7f2e2dSJohn Marino   return val;
3099cf7f2e2dSJohn Marino }
3100cf7f2e2dSJohn Marino 
3101cf7f2e2dSJohn Marino 
3102cf7f2e2dSJohn Marino /* Convert C unsigned numbers into newly allocated values.  */
3103cf7f2e2dSJohn Marino 
3104cf7f2e2dSJohn Marino struct value *
value_from_ulongest(struct type * type,ULONGEST num)3105cf7f2e2dSJohn Marino value_from_ulongest (struct type *type, ULONGEST num)
3106cf7f2e2dSJohn Marino {
3107cf7f2e2dSJohn Marino   struct value *val = allocate_value (type);
3108cf7f2e2dSJohn Marino 
3109cf7f2e2dSJohn Marino   pack_unsigned_long (value_contents_raw (val), type, num);
31105796c8dcSSimon Schubert 
31115796c8dcSSimon Schubert   return val;
31125796c8dcSSimon Schubert }
31135796c8dcSSimon Schubert 
31145796c8dcSSimon Schubert 
31155796c8dcSSimon Schubert /* Create a value representing a pointer of type TYPE to the address
31165796c8dcSSimon Schubert    ADDR.  */
31175796c8dcSSimon Schubert struct value *
value_from_pointer(struct type * type,CORE_ADDR addr)31185796c8dcSSimon Schubert value_from_pointer (struct type *type, CORE_ADDR addr)
31195796c8dcSSimon Schubert {
31205796c8dcSSimon Schubert   struct value *val = allocate_value (type);
3121cf7f2e2dSJohn Marino 
31225796c8dcSSimon Schubert   store_typed_address (value_contents_raw (val), check_typedef (type), addr);
31235796c8dcSSimon Schubert   return val;
31245796c8dcSSimon Schubert }
31255796c8dcSSimon Schubert 
31265796c8dcSSimon Schubert 
31275796c8dcSSimon Schubert /* Create a value of type TYPE whose contents come from VALADDR, if it
31285796c8dcSSimon Schubert    is non-null, and whose memory address (in the inferior) is
31295796c8dcSSimon Schubert    ADDRESS.  */
31305796c8dcSSimon Schubert 
31315796c8dcSSimon Schubert struct value *
value_from_contents_and_address(struct type * type,const gdb_byte * valaddr,CORE_ADDR address)31325796c8dcSSimon Schubert value_from_contents_and_address (struct type *type,
31335796c8dcSSimon Schubert 				 const gdb_byte *valaddr,
31345796c8dcSSimon Schubert 				 CORE_ADDR address)
31355796c8dcSSimon Schubert {
3136c50c785cSJohn Marino   struct value *v;
3137cf7f2e2dSJohn Marino 
31385796c8dcSSimon Schubert   if (valaddr == NULL)
3139c50c785cSJohn Marino     v = allocate_value_lazy (type);
31405796c8dcSSimon Schubert   else
3141c50c785cSJohn Marino     {
3142c50c785cSJohn Marino       v = allocate_value (type);
31435796c8dcSSimon Schubert       memcpy (value_contents_raw (v), valaddr, TYPE_LENGTH (type));
3144c50c785cSJohn Marino     }
31455796c8dcSSimon Schubert   set_value_address (v, address);
31465796c8dcSSimon Schubert   VALUE_LVAL (v) = lval_memory;
31475796c8dcSSimon Schubert   return v;
31485796c8dcSSimon Schubert }
31495796c8dcSSimon Schubert 
3150a45ae5f8SJohn Marino /* Create a value of type TYPE holding the contents CONTENTS.
3151a45ae5f8SJohn Marino    The new value is `not_lval'.  */
3152a45ae5f8SJohn Marino 
3153a45ae5f8SJohn Marino struct value *
value_from_contents(struct type * type,const gdb_byte * contents)3154a45ae5f8SJohn Marino value_from_contents (struct type *type, const gdb_byte *contents)
3155a45ae5f8SJohn Marino {
3156a45ae5f8SJohn Marino   struct value *result;
3157a45ae5f8SJohn Marino 
3158a45ae5f8SJohn Marino   result = allocate_value (type);
3159a45ae5f8SJohn Marino   memcpy (value_contents_raw (result), contents, TYPE_LENGTH (type));
3160a45ae5f8SJohn Marino   return result;
3161a45ae5f8SJohn Marino }
3162a45ae5f8SJohn Marino 
31635796c8dcSSimon Schubert struct value *
value_from_double(struct type * type,DOUBLEST num)31645796c8dcSSimon Schubert value_from_double (struct type *type, DOUBLEST num)
31655796c8dcSSimon Schubert {
31665796c8dcSSimon Schubert   struct value *val = allocate_value (type);
31675796c8dcSSimon Schubert   struct type *base_type = check_typedef (type);
31685796c8dcSSimon Schubert   enum type_code code = TYPE_CODE (base_type);
31695796c8dcSSimon Schubert 
31705796c8dcSSimon Schubert   if (code == TYPE_CODE_FLT)
31715796c8dcSSimon Schubert     {
31725796c8dcSSimon Schubert       store_typed_floating (value_contents_raw (val), base_type, num);
31735796c8dcSSimon Schubert     }
31745796c8dcSSimon Schubert   else
31755796c8dcSSimon Schubert     error (_("Unexpected type encountered for floating constant."));
31765796c8dcSSimon Schubert 
31775796c8dcSSimon Schubert   return val;
31785796c8dcSSimon Schubert }
31795796c8dcSSimon Schubert 
31805796c8dcSSimon Schubert struct value *
value_from_decfloat(struct type * type,const gdb_byte * dec)31815796c8dcSSimon Schubert value_from_decfloat (struct type *type, const gdb_byte *dec)
31825796c8dcSSimon Schubert {
31835796c8dcSSimon Schubert   struct value *val = allocate_value (type);
31845796c8dcSSimon Schubert 
31855796c8dcSSimon Schubert   memcpy (value_contents_raw (val), dec, TYPE_LENGTH (type));
31865796c8dcSSimon Schubert   return val;
31875796c8dcSSimon Schubert }
31885796c8dcSSimon Schubert 
3189c50c785cSJohn Marino /* Extract a value from the history file.  Input will be of the form
3190c50c785cSJohn Marino    $digits or $$digits.  See block comment above 'write_dollar_variable'
3191c50c785cSJohn Marino    for details.  */
3192c50c785cSJohn Marino 
3193c50c785cSJohn Marino struct value *
value_from_history_ref(char * h,char ** endp)3194c50c785cSJohn Marino value_from_history_ref (char *h, char **endp)
3195c50c785cSJohn Marino {
3196c50c785cSJohn Marino   int index, len;
3197c50c785cSJohn Marino 
3198c50c785cSJohn Marino   if (h[0] == '$')
3199c50c785cSJohn Marino     len = 1;
3200c50c785cSJohn Marino   else
3201c50c785cSJohn Marino     return NULL;
3202c50c785cSJohn Marino 
3203c50c785cSJohn Marino   if (h[1] == '$')
3204c50c785cSJohn Marino     len = 2;
3205c50c785cSJohn Marino 
3206c50c785cSJohn Marino   /* Find length of numeral string.  */
3207c50c785cSJohn Marino   for (; isdigit (h[len]); len++)
3208c50c785cSJohn Marino     ;
3209c50c785cSJohn Marino 
3210c50c785cSJohn Marino   /* Make sure numeral string is not part of an identifier.  */
3211c50c785cSJohn Marino   if (h[len] == '_' || isalpha (h[len]))
3212c50c785cSJohn Marino     return NULL;
3213c50c785cSJohn Marino 
3214c50c785cSJohn Marino   /* Now collect the index value.  */
3215c50c785cSJohn Marino   if (h[1] == '$')
3216c50c785cSJohn Marino     {
3217c50c785cSJohn Marino       if (len == 2)
3218c50c785cSJohn Marino 	{
3219c50c785cSJohn Marino 	  /* For some bizarre reason, "$$" is equivalent to "$$1",
3220c50c785cSJohn Marino 	     rather than to "$$0" as it ought to be!  */
3221c50c785cSJohn Marino 	  index = -1;
3222c50c785cSJohn Marino 	  *endp += len;
3223c50c785cSJohn Marino 	}
3224c50c785cSJohn Marino       else
3225c50c785cSJohn Marino 	index = -strtol (&h[2], endp, 10);
3226c50c785cSJohn Marino     }
3227c50c785cSJohn Marino   else
3228c50c785cSJohn Marino     {
3229c50c785cSJohn Marino       if (len == 1)
3230c50c785cSJohn Marino 	{
3231c50c785cSJohn Marino 	  /* "$" is equivalent to "$0".  */
3232c50c785cSJohn Marino 	  index = 0;
3233c50c785cSJohn Marino 	  *endp += len;
3234c50c785cSJohn Marino 	}
3235c50c785cSJohn Marino       else
3236c50c785cSJohn Marino 	index = strtol (&h[1], endp, 10);
3237c50c785cSJohn Marino     }
3238c50c785cSJohn Marino 
3239c50c785cSJohn Marino   return access_value_history (index);
3240c50c785cSJohn Marino }
3241c50c785cSJohn Marino 
32425796c8dcSSimon Schubert struct value *
coerce_ref_if_computed(const struct value * arg)3243a45ae5f8SJohn Marino coerce_ref_if_computed (const struct value *arg)
3244a45ae5f8SJohn Marino {
3245a45ae5f8SJohn Marino   const struct lval_funcs *funcs;
3246a45ae5f8SJohn Marino 
3247a45ae5f8SJohn Marino   if (TYPE_CODE (check_typedef (value_type (arg))) != TYPE_CODE_REF)
3248a45ae5f8SJohn Marino     return NULL;
3249a45ae5f8SJohn Marino 
3250a45ae5f8SJohn Marino   if (value_lval_const (arg) != lval_computed)
3251a45ae5f8SJohn Marino     return NULL;
3252a45ae5f8SJohn Marino 
3253a45ae5f8SJohn Marino   funcs = value_computed_funcs (arg);
3254a45ae5f8SJohn Marino   if (funcs->coerce_ref == NULL)
3255a45ae5f8SJohn Marino     return NULL;
3256a45ae5f8SJohn Marino 
3257a45ae5f8SJohn Marino   return funcs->coerce_ref (arg);
3258a45ae5f8SJohn Marino }
3259a45ae5f8SJohn Marino 
3260*ef5ccd6cSJohn Marino /* Look at value.h for description.  */
3261*ef5ccd6cSJohn Marino 
3262*ef5ccd6cSJohn Marino struct value *
readjust_indirect_value_type(struct value * value,struct type * enc_type,struct type * original_type,struct value * original_value)3263*ef5ccd6cSJohn Marino readjust_indirect_value_type (struct value *value, struct type *enc_type,
3264*ef5ccd6cSJohn Marino 			      struct type *original_type,
3265*ef5ccd6cSJohn Marino 			      struct value *original_value)
3266*ef5ccd6cSJohn Marino {
3267*ef5ccd6cSJohn Marino   /* Re-adjust type.  */
3268*ef5ccd6cSJohn Marino   deprecated_set_value_type (value, TYPE_TARGET_TYPE (original_type));
3269*ef5ccd6cSJohn Marino 
3270*ef5ccd6cSJohn Marino   /* Add embedding info.  */
3271*ef5ccd6cSJohn Marino   set_value_enclosing_type (value, enc_type);
3272*ef5ccd6cSJohn Marino   set_value_embedded_offset (value, value_pointed_to_offset (original_value));
3273*ef5ccd6cSJohn Marino 
3274*ef5ccd6cSJohn Marino   /* We may be pointing to an object of some derived type.  */
3275*ef5ccd6cSJohn Marino   return value_full_object (value, NULL, 0, 0, 0);
3276*ef5ccd6cSJohn Marino }
3277*ef5ccd6cSJohn Marino 
3278a45ae5f8SJohn Marino struct value *
coerce_ref(struct value * arg)32795796c8dcSSimon Schubert coerce_ref (struct value *arg)
32805796c8dcSSimon Schubert {
32815796c8dcSSimon Schubert   struct type *value_type_arg_tmp = check_typedef (value_type (arg));
3282a45ae5f8SJohn Marino   struct value *retval;
3283*ef5ccd6cSJohn Marino   struct type *enc_type;
3284cf7f2e2dSJohn Marino 
3285a45ae5f8SJohn Marino   retval = coerce_ref_if_computed (arg);
3286a45ae5f8SJohn Marino   if (retval)
3287a45ae5f8SJohn Marino     return retval;
3288a45ae5f8SJohn Marino 
3289a45ae5f8SJohn Marino   if (TYPE_CODE (value_type_arg_tmp) != TYPE_CODE_REF)
3290a45ae5f8SJohn Marino     return arg;
3291a45ae5f8SJohn Marino 
3292*ef5ccd6cSJohn Marino   enc_type = check_typedef (value_enclosing_type (arg));
3293*ef5ccd6cSJohn Marino   enc_type = TYPE_TARGET_TYPE (enc_type);
3294*ef5ccd6cSJohn Marino 
3295*ef5ccd6cSJohn Marino   retval = value_at_lazy (enc_type,
32965796c8dcSSimon Schubert                           unpack_pointer (value_type (arg),
32975796c8dcSSimon Schubert                                           value_contents (arg)));
3298*ef5ccd6cSJohn Marino   return readjust_indirect_value_type (retval, enc_type,
3299*ef5ccd6cSJohn Marino                                        value_type_arg_tmp, arg);
33005796c8dcSSimon Schubert }
33015796c8dcSSimon Schubert 
33025796c8dcSSimon Schubert struct value *
coerce_array(struct value * arg)33035796c8dcSSimon Schubert coerce_array (struct value *arg)
33045796c8dcSSimon Schubert {
33055796c8dcSSimon Schubert   struct type *type;
33065796c8dcSSimon Schubert 
33075796c8dcSSimon Schubert   arg = coerce_ref (arg);
33085796c8dcSSimon Schubert   type = check_typedef (value_type (arg));
33095796c8dcSSimon Schubert 
33105796c8dcSSimon Schubert   switch (TYPE_CODE (type))
33115796c8dcSSimon Schubert     {
33125796c8dcSSimon Schubert     case TYPE_CODE_ARRAY:
3313c50c785cSJohn Marino       if (!TYPE_VECTOR (type) && current_language->c_style_arrays)
33145796c8dcSSimon Schubert 	arg = value_coerce_array (arg);
33155796c8dcSSimon Schubert       break;
33165796c8dcSSimon Schubert     case TYPE_CODE_FUNC:
33175796c8dcSSimon Schubert       arg = value_coerce_function (arg);
33185796c8dcSSimon Schubert       break;
33195796c8dcSSimon Schubert     }
33205796c8dcSSimon Schubert   return arg;
33215796c8dcSSimon Schubert }
33225796c8dcSSimon Schubert 
33235796c8dcSSimon Schubert 
3324*ef5ccd6cSJohn Marino /* Return the return value convention that will be used for the
3325*ef5ccd6cSJohn Marino    specified type.  */
33265796c8dcSSimon Schubert 
3327*ef5ccd6cSJohn Marino enum return_value_convention
struct_return_convention(struct gdbarch * gdbarch,struct value * function,struct type * value_type)3328*ef5ccd6cSJohn Marino struct_return_convention (struct gdbarch *gdbarch,
3329*ef5ccd6cSJohn Marino 			  struct value *function, struct type *value_type)
33305796c8dcSSimon Schubert {
33315796c8dcSSimon Schubert   enum type_code code = TYPE_CODE (value_type);
33325796c8dcSSimon Schubert 
33335796c8dcSSimon Schubert   if (code == TYPE_CODE_ERROR)
33345796c8dcSSimon Schubert     error (_("Function return type unknown."));
33355796c8dcSSimon Schubert 
3336*ef5ccd6cSJohn Marino   /* Probe the architecture for the return-value convention.  */
3337*ef5ccd6cSJohn Marino   return gdbarch_return_value (gdbarch, function, value_type,
3338*ef5ccd6cSJohn Marino 			       NULL, NULL, NULL);
3339*ef5ccd6cSJohn Marino }
3340*ef5ccd6cSJohn Marino 
3341*ef5ccd6cSJohn Marino /* Return true if the function returning the specified type is using
3342*ef5ccd6cSJohn Marino    the convention of returning structures in memory (passing in the
3343*ef5ccd6cSJohn Marino    address as a hidden first parameter).  */
3344*ef5ccd6cSJohn Marino 
3345*ef5ccd6cSJohn Marino int
using_struct_return(struct gdbarch * gdbarch,struct value * function,struct type * value_type)3346*ef5ccd6cSJohn Marino using_struct_return (struct gdbarch *gdbarch,
3347*ef5ccd6cSJohn Marino 		     struct value *function, struct type *value_type)
3348*ef5ccd6cSJohn Marino {
3349*ef5ccd6cSJohn Marino   if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
33505796c8dcSSimon Schubert     /* A void return value is never in memory.  See also corresponding
33515796c8dcSSimon Schubert        code in "print_return_value".  */
33525796c8dcSSimon Schubert     return 0;
33535796c8dcSSimon Schubert 
3354*ef5ccd6cSJohn Marino   return (struct_return_convention (gdbarch, function, value_type)
33555796c8dcSSimon Schubert 	  != RETURN_VALUE_REGISTER_CONVENTION);
33565796c8dcSSimon Schubert }
33575796c8dcSSimon Schubert 
33585796c8dcSSimon Schubert /* Set the initialized field in a value struct.  */
33595796c8dcSSimon Schubert 
33605796c8dcSSimon Schubert void
set_value_initialized(struct value * val,int status)33615796c8dcSSimon Schubert set_value_initialized (struct value *val, int status)
33625796c8dcSSimon Schubert {
33635796c8dcSSimon Schubert   val->initialized = status;
33645796c8dcSSimon Schubert }
33655796c8dcSSimon Schubert 
33665796c8dcSSimon Schubert /* Return the initialized field in a value struct.  */
33675796c8dcSSimon Schubert 
33685796c8dcSSimon Schubert int
value_initialized(struct value * val)33695796c8dcSSimon Schubert value_initialized (struct value *val)
33705796c8dcSSimon Schubert {
33715796c8dcSSimon Schubert   return val->initialized;
33725796c8dcSSimon Schubert }
33735796c8dcSSimon Schubert 
33745796c8dcSSimon Schubert void
_initialize_values(void)33755796c8dcSSimon Schubert _initialize_values (void)
33765796c8dcSSimon Schubert {
33775796c8dcSSimon Schubert   add_cmd ("convenience", no_class, show_convenience, _("\
3378*ef5ccd6cSJohn Marino Debugger convenience (\"$foo\") variables and functions.\n\
3379*ef5ccd6cSJohn Marino Convenience variables are created when you assign them values;\n\
3380*ef5ccd6cSJohn Marino thus, \"set $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\
33815796c8dcSSimon Schubert \n\
33825796c8dcSSimon Schubert A few convenience variables are given values automatically:\n\
33835796c8dcSSimon Schubert \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
3384*ef5ccd6cSJohn Marino \"$__\" holds the contents of the last address examined with \"x\"."
3385*ef5ccd6cSJohn Marino #ifdef HAVE_PYTHON
3386*ef5ccd6cSJohn Marino "\n\n\
3387*ef5ccd6cSJohn Marino Convenience functions are defined via the Python API."
3388*ef5ccd6cSJohn Marino #endif
3389*ef5ccd6cSJohn Marino 	   ), &showlist);
3390*ef5ccd6cSJohn Marino   add_alias_cmd ("conv", "convenience", no_class, 1, &showlist);
33915796c8dcSSimon Schubert 
3392c50c785cSJohn Marino   add_cmd ("values", no_set_class, show_values, _("\
3393c50c785cSJohn Marino Elements of value history around item number IDX (or last ten)."),
33945796c8dcSSimon Schubert 	   &showlist);
33955796c8dcSSimon Schubert 
33965796c8dcSSimon Schubert   add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\
33975796c8dcSSimon Schubert Initialize a convenience variable if necessary.\n\
33985796c8dcSSimon Schubert init-if-undefined VARIABLE = EXPRESSION\n\
33995796c8dcSSimon Schubert Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\
34005796c8dcSSimon Schubert exist or does not contain a value.  The EXPRESSION is not evaluated if the\n\
34015796c8dcSSimon Schubert VARIABLE is already initialized."));
34025796c8dcSSimon Schubert 
34035796c8dcSSimon Schubert   add_prefix_cmd ("function", no_class, function_command, _("\
34045796c8dcSSimon Schubert Placeholder command for showing help on convenience functions."),
34055796c8dcSSimon Schubert 		  &functionlist, "function ", 0, &cmdlist);
34065796c8dcSSimon Schubert }
3407