]> Git Repo - binutils.git/blob - gdb/valops.c
* remote.texi: Update list of stubs in the GDB distribution.
[binutils.git] / gdb / valops.c
1 /* Perform non-arithmetic operations on values, for GDB.
2    Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "demangle.h"
30 #include "language.h"
31
32 #include <errno.h>
33 #include "gdb_string.h"
34
35 /* Default to coercing float to double in function calls only when there is
36    no prototype.  Otherwise on targets where the debug information is incorrect
37    for either the prototype or non-prototype case, we can force it by defining
38    COERCE_FLOAT_TO_DOUBLE in the target configuration file. */
39
40 #ifndef COERCE_FLOAT_TO_DOUBLE
41 #define COERCE_FLOAT_TO_DOUBLE (param_type == NULL)
42 #endif
43
44 /* Local functions.  */
45
46 static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[]));
47
48 static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **));
49
50 static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr));
51
52 static value_ptr search_struct_field PARAMS ((char *, value_ptr, int,
53                                               struct type *, int));
54
55 static value_ptr search_struct_method PARAMS ((char *, value_ptr *,
56                                                value_ptr *,
57                                                int, int *, struct type *));
58
59 static int check_field_in PARAMS ((struct type *, const char *));
60
61 static CORE_ADDR allocate_space_in_inferior PARAMS ((int));
62
63 static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr));
64
65 #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL)
66
67 /* Flag for whether we want to abandon failed expression evals by default.  */
68
69 #if 0
70 static int auto_abandon = 0;
71 #endif
72
73 \f
74 /* Find the address of function name NAME in the inferior.  */
75
76 value_ptr
77 find_function_in_inferior (name)
78      char *name;
79 {
80   register struct symbol *sym;
81   sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL);
82   if (sym != NULL)
83     {
84       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
85         {
86           error ("\"%s\" exists in this program but is not a function.",
87                  name);
88         }
89       return value_of_variable (sym, NULL);
90     }
91   else
92     {
93       struct minimal_symbol *msymbol = lookup_minimal_symbol(name, NULL, NULL);
94       if (msymbol != NULL)
95         {
96           struct type *type;
97           LONGEST maddr;
98           type = lookup_pointer_type (builtin_type_char);
99           type = lookup_function_type (type);
100           type = lookup_pointer_type (type);
101           maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
102           return value_from_longest (type, maddr);
103         }
104       else
105         {
106           error ("evaluation of this expression requires the program to have a function \"%s\".", name);
107         }
108     }
109 }
110
111 /* Allocate NBYTES of space in the inferior using the inferior's malloc
112    and return a value that is a pointer to the allocated space. */
113
114 value_ptr
115 value_allocate_space_in_inferior (len)
116      int len;
117 {
118   value_ptr blocklen;
119   register value_ptr val = find_function_in_inferior ("malloc");
120
121   blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
122   val = call_function_by_hand (val, 1, &blocklen);
123   if (value_logical_not (val))
124     {
125       error ("No memory available to program.");
126     }
127   return val;
128 }
129
130 static CORE_ADDR
131 allocate_space_in_inferior (len)
132      int len;
133 {
134   return value_as_long (value_allocate_space_in_inferior (len));
135 }
136
137 /* Cast value ARG2 to type TYPE and return as a value.
138    More general than a C cast: accepts any two types of the same length,
139    and if ARG2 is an lvalue it can be cast into anything at all.  */
140 /* In C++, casts may change pointer or object representations.  */
141
142 value_ptr
143 value_cast (type, arg2)
144      struct type *type;
145      register value_ptr arg2;
146 {
147   register enum type_code code1;
148   register enum type_code code2;
149   register int scalar;
150   struct type *type2;
151
152   if (VALUE_TYPE (arg2) == type)
153     return arg2;
154
155   CHECK_TYPEDEF (type);
156   code1 = TYPE_CODE (type);
157   COERCE_REF(arg2);
158   type2 = check_typedef (VALUE_TYPE (arg2));
159
160   /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT,
161      is treated like a cast to (TYPE [N])OBJECT,
162      where N is sizeof(OBJECT)/sizeof(TYPE). */
163   if (code1 == TYPE_CODE_ARRAY)
164     {
165       struct type *element_type = TYPE_TARGET_TYPE (type);
166       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
167       if (element_length > 0
168           && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED)
169         {
170           struct type *range_type = TYPE_INDEX_TYPE (type);
171           int val_length = TYPE_LENGTH (type2);
172           LONGEST low_bound, high_bound, new_length;
173           if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
174             low_bound = 0, high_bound = 0;
175           new_length = val_length / element_length;
176           if (val_length % element_length != 0)
177        warning("array element type size does not divide object size in cast");
178           /* FIXME-type-allocation: need a way to free this type when we are
179              done with it.  */
180           range_type = create_range_type ((struct type *) NULL,
181                                           TYPE_TARGET_TYPE (range_type),
182                                           low_bound,
183                                           new_length + low_bound - 1);
184           VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL,
185                                                  element_type, range_type);
186           return arg2;
187         }
188     }
189
190   if (current_language->c_style_arrays
191       && TYPE_CODE (type2) == TYPE_CODE_ARRAY)
192     arg2 = value_coerce_array (arg2);
193
194   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
195     arg2 = value_coerce_function (arg2);
196
197   type2 = check_typedef (VALUE_TYPE (arg2));
198   COERCE_VARYING_ARRAY (arg2, type2);
199   code2 = TYPE_CODE (type2);
200
201   if (code1 == TYPE_CODE_COMPLEX)
202     return cast_into_complex (type, arg2);
203   if (code1 == TYPE_CODE_BOOL || code1 == TYPE_CODE_CHAR)
204     code1 = TYPE_CODE_INT; 
205   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
206     code2 = TYPE_CODE_INT;
207
208   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
209             || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE);
210
211   if (   code1 == TYPE_CODE_STRUCT
212       && code2 == TYPE_CODE_STRUCT
213       && TYPE_NAME (type) != 0)
214     {
215       /* Look in the type of the source to see if it contains the
216          type of the target as a superclass.  If so, we'll need to
217          offset the object in addition to changing its type.  */
218       value_ptr v = search_struct_field (type_name_no_tag (type),
219                                          arg2, 0, type2, 1);
220       if (v)
221         {
222           VALUE_TYPE (v) = type;
223           return v;
224         }
225     }
226   if (code1 == TYPE_CODE_FLT && scalar)
227     return value_from_double (type, value_as_double (arg2));
228   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
229             || code1 == TYPE_CODE_RANGE)
230            && (scalar || code2 == TYPE_CODE_PTR))
231     return value_from_longest (type, value_as_long (arg2));
232   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
233     {
234       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
235         {
236           /* Look in the type of the source to see if it contains the
237              type of the target as a superclass.  If so, we'll need to
238              offset the pointer rather than just change its type.  */
239           struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type));
240           struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
241           if (   TYPE_CODE (t1) == TYPE_CODE_STRUCT
242               && TYPE_CODE (t2) == TYPE_CODE_STRUCT
243               && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
244             {
245               value_ptr v = search_struct_field (type_name_no_tag (t1),
246                                                  value_ind (arg2), 0, t2, 1);
247               if (v)
248                 {
249                   v = value_addr (v);
250                   VALUE_TYPE (v) = type;
251                   return v;
252                 }
253             }
254           /* No superclass found, just fall through to change ptr type.  */
255         }
256       VALUE_TYPE (arg2) = type;
257       return arg2;
258     }
259   else if (chill_varying_type (type))
260     {
261       struct type *range1, *range2, *eltype1, *eltype2;
262       value_ptr val;
263       int count1, count2;
264       LONGEST low_bound, high_bound;
265       char *valaddr, *valaddr_data;
266       if (code2 == TYPE_CODE_BITSTRING)
267         error ("not implemented: converting bitstring to varying type");
268       if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING)
269           || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))),
270               eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)),
271               (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2)
272                /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ )))
273         error ("Invalid conversion to varying type");
274       range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0);
275       range2 = TYPE_FIELD_TYPE (type2, 0);
276       if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0)
277         count1 = -1;
278       else
279         count1 = high_bound - low_bound + 1;
280       if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0)
281         count1 = -1, count2 = 0;  /* To force error before */
282       else
283         count2 = high_bound - low_bound + 1;
284       if (count2 > count1)
285         error ("target varying type is too small");
286       val = allocate_value (type);
287       valaddr = VALUE_CONTENTS_RAW (val);
288       valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
289       /* Set val's __var_length field to count2. */
290       store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)),
291                             count2);
292       /* Set the __var_data field to count2 elements copied from arg2. */
293       memcpy (valaddr_data, VALUE_CONTENTS (arg2),
294               count2 * TYPE_LENGTH (eltype2));
295       /* Zero the rest of the __var_data field of val. */
296       memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0',
297               (count1 - count2) * TYPE_LENGTH (eltype2));
298       return val;
299     }
300   else if (VALUE_LVAL (arg2) == lval_memory)
301     {
302       return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
303     }
304   else if (code1 == TYPE_CODE_VOID)
305     {
306       return value_zero (builtin_type_void, not_lval);
307     }
308   else
309     {
310       error ("Invalid cast.");
311       return 0;
312     }
313 }
314
315 /* Create a value of type TYPE that is zero, and return it.  */
316
317 value_ptr
318 value_zero (type, lv)
319      struct type *type;
320      enum lval_type lv;
321 {
322   register value_ptr val = allocate_value (type);
323
324   memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type)));
325   VALUE_LVAL (val) = lv;
326
327   return val;
328 }
329
330 /* Return a value with type TYPE located at ADDR.  
331
332    Call value_at only if the data needs to be fetched immediately;
333    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
334    value_at_lazy instead.  value_at_lazy simply records the address of
335    the data and sets the lazy-evaluation-required flag.  The lazy flag 
336    is tested in the VALUE_CONTENTS macro, which is used if and when 
337    the contents are actually required.  */
338
339 value_ptr
340 value_at (type, addr)
341      struct type *type;
342      CORE_ADDR addr;
343 {
344   register value_ptr val;
345
346   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
347     error ("Attempt to dereference a generic pointer.");
348
349   val = allocate_value (type);
350
351   read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
352
353   VALUE_LVAL (val) = lval_memory;
354   VALUE_ADDRESS (val) = addr;
355
356   return val;
357 }
358
359 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
360
361 value_ptr
362 value_at_lazy (type, addr)
363      struct type *type;
364      CORE_ADDR addr;
365 {
366   register value_ptr val;
367
368   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
369     error ("Attempt to dereference a generic pointer.");
370
371   val = allocate_value (type);
372
373   VALUE_LVAL (val) = lval_memory;
374   VALUE_ADDRESS (val) = addr;
375   VALUE_LAZY (val) = 1;
376
377   return val;
378 }
379
380 /* Called only from the VALUE_CONTENTS macro, if the current data for
381    a variable needs to be loaded into VALUE_CONTENTS(VAL).  Fetches the
382    data from the user's process, and clears the lazy flag to indicate
383    that the data in the buffer is valid.
384
385    If the value is zero-length, we avoid calling read_memory, which would
386    abort.  We mark the value as fetched anyway -- all 0 bytes of it.
387
388    This function returns a value because it is used in the VALUE_CONTENTS
389    macro as part of an expression, where a void would not work.  The
390    value is ignored.  */
391
392 int
393 value_fetch_lazy (val)
394      register value_ptr val;
395 {
396   CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
397   int length = TYPE_LENGTH (VALUE_TYPE (val));
398
399   if (length)
400     read_memory (addr, VALUE_CONTENTS_RAW (val), length);
401   VALUE_LAZY (val) = 0;
402   return 0;
403 }
404
405
406 /* Store the contents of FROMVAL into the location of TOVAL.
407    Return a new value with the location of TOVAL and contents of FROMVAL.  */
408
409 value_ptr
410 value_assign (toval, fromval)
411      register value_ptr toval, fromval;
412 {
413   register struct type *type;
414   register value_ptr val;
415   char raw_buffer[MAX_REGISTER_RAW_SIZE];
416   int use_buffer = 0;
417
418   if (!toval->modifiable)
419     error ("Left operand of assignment is not a modifiable lvalue.");
420
421   COERCE_REF (toval);
422
423   type = VALUE_TYPE (toval);
424   if (VALUE_LVAL (toval) != lval_internalvar)
425     fromval = value_cast (type, fromval);
426   else
427     COERCE_ARRAY (fromval);
428   CHECK_TYPEDEF (type);
429
430   /* If TOVAL is a special machine register requiring conversion
431      of program values to a special raw format,
432      convert FROMVAL's contents now, with result in `raw_buffer',
433      and set USE_BUFFER to the number of bytes to write.  */
434
435 #ifdef REGISTER_CONVERTIBLE
436   if (VALUE_REGNO (toval) >= 0
437       && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
438     {
439       int regno = VALUE_REGNO (toval);
440       if (REGISTER_CONVERTIBLE (regno))
441         {
442           struct type *fromtype = check_typedef (VALUE_TYPE (fromval));
443           REGISTER_CONVERT_TO_RAW (fromtype, regno,
444                                    VALUE_CONTENTS (fromval), raw_buffer);
445           use_buffer = REGISTER_RAW_SIZE (regno);
446         }
447     }
448 #endif
449
450   switch (VALUE_LVAL (toval))
451     {
452     case lval_internalvar:
453       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
454       return value_copy (VALUE_INTERNALVAR (toval)->value);
455
456     case lval_internalvar_component:
457       set_internalvar_component (VALUE_INTERNALVAR (toval),
458                                  VALUE_OFFSET (toval),
459                                  VALUE_BITPOS (toval),
460                                  VALUE_BITSIZE (toval),
461                                  fromval);
462       break;
463
464     case lval_memory:
465       if (VALUE_BITSIZE (toval))
466         {
467           char buffer[sizeof (LONGEST)];
468           /* We assume that the argument to read_memory is in units of
469              host chars.  FIXME:  Is that correct?  */
470           int len = (VALUE_BITPOS (toval)
471                      + VALUE_BITSIZE (toval)
472                      + HOST_CHAR_BIT - 1)
473                     / HOST_CHAR_BIT;
474
475           if (len > (int) sizeof (LONGEST))
476             error ("Can't handle bitfields which don't fit in a %d bit word.",
477                    sizeof (LONGEST) * HOST_CHAR_BIT);
478
479           read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
480                        buffer, len);
481           modify_field (buffer, value_as_long (fromval),
482                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
483           write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
484                         buffer, len);
485         }
486       else if (use_buffer)
487         write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
488                       raw_buffer, use_buffer);
489       else
490         write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
491                       VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
492       break;
493
494     case lval_register:
495       if (VALUE_BITSIZE (toval))
496         {
497           char buffer[sizeof (LONGEST)];
498           int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval));
499
500           if (len > (int) sizeof (LONGEST))
501             error ("Can't handle bitfields in registers larger than %d bits.",
502                    sizeof (LONGEST) * HOST_CHAR_BIT);
503
504           if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval)
505               > len * HOST_CHAR_BIT)
506             /* Getting this right would involve being very careful about
507                byte order.  */
508             error ("\
509 Can't handle bitfield which doesn't fit in a single register.");
510
511           read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
512                                buffer, len);
513           modify_field (buffer, value_as_long (fromval),
514                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
515           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
516                                 buffer, len);
517         }
518       else if (use_buffer)
519         write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
520                               raw_buffer, use_buffer);
521       else
522         {
523           /* Do any conversion necessary when storing this type to more
524              than one register.  */
525 #ifdef REGISTER_CONVERT_FROM_TYPE
526           memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
527           REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer);
528           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
529                                 raw_buffer, TYPE_LENGTH (type));
530 #else
531           write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
532                                 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
533 #endif
534         }
535       /* Assigning to the stack pointer, frame pointer, and other
536          (architecture and calling convention specific) registers may
537          cause the frame cache to be out of date.  We just do this
538          on all assignments to registers for simplicity; I doubt the slowdown
539          matters.  */
540       reinit_frame_cache ();
541       break;
542
543     case lval_reg_frame_relative:
544       {
545         /* value is stored in a series of registers in the frame
546            specified by the structure.  Copy that value out, modify
547            it, and copy it back in.  */
548         int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
549         int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
550         int byte_offset = VALUE_OFFSET (toval) % reg_size;
551         int reg_offset = VALUE_OFFSET (toval) / reg_size;
552         int amount_copied;
553
554         /* Make the buffer large enough in all cases.  */
555         char *buffer = (char *) alloca (amount_to_copy
556                                         + sizeof (LONGEST)
557                                         + MAX_REGISTER_RAW_SIZE);
558
559         int regno;
560         struct frame_info *frame;
561
562         /* Figure out which frame this is in currently.  */
563         for (frame = get_current_frame ();
564              frame && FRAME_FP (frame) != VALUE_FRAME (toval);
565              frame = get_prev_frame (frame))
566           ;
567
568         if (!frame)
569           error ("Value being assigned to is no longer active.");
570
571         amount_to_copy += (reg_size - amount_to_copy % reg_size);
572
573         /* Copy it out.  */
574         for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
575               amount_copied = 0);
576              amount_copied < amount_to_copy;
577              amount_copied += reg_size, regno++)
578           {
579             get_saved_register (buffer + amount_copied,
580                                 (int *)NULL, (CORE_ADDR *)NULL,
581                                 frame, regno, (enum lval_type *)NULL);
582           }
583
584         /* Modify what needs to be modified.  */
585         if (VALUE_BITSIZE (toval))
586           modify_field (buffer + byte_offset,
587                         value_as_long (fromval),
588                         VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
589         else if (use_buffer)
590           memcpy (buffer + byte_offset, raw_buffer, use_buffer);
591         else
592           memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
593                   TYPE_LENGTH (type));
594
595         /* Copy it back.  */
596         for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
597               amount_copied = 0);
598              amount_copied < amount_to_copy;
599              amount_copied += reg_size, regno++)
600           {
601             enum lval_type lval;
602             CORE_ADDR addr;
603             int optim;
604
605             /* Just find out where to put it.  */
606             get_saved_register ((char *)NULL,
607                                 &optim, &addr, frame, regno, &lval);
608             
609             if (optim)
610               error ("Attempt to assign to a value that was optimized out.");
611             if (lval == lval_memory)
612               write_memory (addr, buffer + amount_copied, reg_size);
613             else if (lval == lval_register)
614               write_register_bytes (addr, buffer + amount_copied, reg_size);
615             else
616               error ("Attempt to assign to an unmodifiable value.");
617           }
618       }
619       break;
620         
621
622     default:
623       error ("Left operand of assignment is not an lvalue.");
624     }
625
626   /* If the field does not entirely fill a LONGEST, then zero the sign bits.
627      If the field is signed, and is negative, then sign extend. */
628   if ((VALUE_BITSIZE (toval) > 0)
629       && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST)))
630     {
631       LONGEST fieldval = value_as_long (fromval);
632       LONGEST valmask = (((unsigned LONGEST) 1) << VALUE_BITSIZE (toval)) - 1;
633
634       fieldval &= valmask;
635       if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1))))
636         fieldval |= ~valmask;
637
638       fromval = value_from_longest (type, fieldval);
639     }
640
641   val = value_copy (toval);
642   memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
643           TYPE_LENGTH (type));
644   VALUE_TYPE (val) = type;
645   
646   return val;
647 }
648
649 /* Extend a value VAL to COUNT repetitions of its type.  */
650
651 value_ptr
652 value_repeat (arg1, count)
653      value_ptr arg1;
654      int count;
655 {
656   register value_ptr val;
657
658   if (VALUE_LVAL (arg1) != lval_memory)
659     error ("Only values in memory can be extended with '@'.");
660   if (count < 1)
661     error ("Invalid number %d of repetitions.", count);
662
663   val = allocate_repeat_value (VALUE_TYPE (arg1), count);
664
665   read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
666                VALUE_CONTENTS_RAW (val),
667                TYPE_LENGTH (VALUE_TYPE (val)));
668   VALUE_LVAL (val) = lval_memory;
669   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
670
671   return val;
672 }
673
674 value_ptr
675 value_of_variable (var, b)
676      struct symbol *var;
677      struct block *b;
678 {
679   value_ptr val;
680   struct frame_info *frame;
681
682   if (b == NULL)
683     /* Use selected frame.  */
684     frame = NULL;
685   else
686     {
687       frame = block_innermost_frame (b);
688       if (frame == NULL && symbol_read_needs_frame (var))
689         {
690           if (BLOCK_FUNCTION (b) != NULL
691               && SYMBOL_NAME (BLOCK_FUNCTION (b)) != NULL)
692             error ("No frame is currently executing in block %s.",
693                    SYMBOL_NAME (BLOCK_FUNCTION (b)));
694           else
695             error ("No frame is currently executing in specified block");
696         }
697     }
698   val = read_var_value (var, frame);
699   if (val == 0)
700     error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
701   return val;
702 }
703
704 /* Given a value which is an array, return a value which is a pointer to its
705    first element, regardless of whether or not the array has a nonzero lower
706    bound.
707
708    FIXME:  A previous comment here indicated that this routine should be
709    substracting the array's lower bound.  It's not clear to me that this
710    is correct.  Given an array subscripting operation, it would certainly
711    work to do the adjustment here, essentially computing:
712
713    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
714
715    However I believe a more appropriate and logical place to account for
716    the lower bound is to do so in value_subscript, essentially computing:
717
718    (&array[0] + ((index - lowerbound) * sizeof array[0]))
719
720    As further evidence consider what would happen with operations other
721    than array subscripting, where the caller would get back a value that
722    had an address somewhere before the actual first element of the array,
723    and the information about the lower bound would be lost because of
724    the coercion to pointer type.
725    */
726
727 value_ptr
728 value_coerce_array (arg1)
729      value_ptr arg1;
730 {
731   register struct type *type = check_typedef (VALUE_TYPE (arg1));
732
733   if (VALUE_LVAL (arg1) != lval_memory)
734     error ("Attempt to take address of value not located in memory.");
735
736   return value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
737                        (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
738 }
739
740 /* Given a value which is a function, return a value which is a pointer
741    to it.  */
742
743 value_ptr
744 value_coerce_function (arg1)
745      value_ptr arg1;
746 {
747
748   if (VALUE_LVAL (arg1) != lval_memory)
749     error ("Attempt to take address of value not located in memory.");
750
751   return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
752                 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
753 }  
754
755 /* Return a pointer value for the object for which ARG1 is the contents.  */
756
757 value_ptr
758 value_addr (arg1)
759      value_ptr arg1;
760 {
761   struct type *type = check_typedef (VALUE_TYPE (arg1));
762   if (TYPE_CODE (type) == TYPE_CODE_REF)
763     {
764       /* Copy the value, but change the type from (T&) to (T*).
765          We keep the same location information, which is efficient,
766          and allows &(&X) to get the location containing the reference. */
767       value_ptr arg2 = value_copy (arg1);
768       VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
769       return arg2;
770     }
771   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
772     return value_coerce_function (arg1);
773
774   if (VALUE_LVAL (arg1) != lval_memory)
775     error ("Attempt to take address of value not located in memory.");
776
777   return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
778                 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
779 }
780
781 /* Given a value of a pointer type, apply the C unary * operator to it.  */
782
783 value_ptr
784 value_ind (arg1)
785      value_ptr arg1;
786 {
787   struct type *type1;
788   COERCE_ARRAY (arg1);
789   type1 = check_typedef (VALUE_TYPE (arg1));
790
791   if (TYPE_CODE (type1) == TYPE_CODE_MEMBER)
792     error ("not implemented: member types in value_ind");
793
794   /* Allow * on an integer so we can cast it to whatever we want.
795      This returns an int, which seems like the most C-like thing
796      to do.  "long long" variables are rare enough that
797      BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
798   if (TYPE_CODE (type1) == TYPE_CODE_INT)
799     return value_at (builtin_type_int,
800                      (CORE_ADDR) value_as_long (arg1));
801   else if (TYPE_CODE (type1) == TYPE_CODE_PTR)
802     return value_at_lazy (TYPE_TARGET_TYPE (type1), value_as_pointer (arg1));
803   error ("Attempt to take contents of a non-pointer value.");
804   return 0;  /* For lint -- never reached */
805 }
806 \f
807 /* Pushing small parts of stack frames.  */
808
809 /* Push one word (the size of object that a register holds).  */
810
811 CORE_ADDR
812 push_word (sp, word)
813      CORE_ADDR sp;
814      unsigned LONGEST word;
815 {
816   register int len = REGISTER_SIZE;
817   char buffer[MAX_REGISTER_RAW_SIZE];
818
819   store_unsigned_integer (buffer, len, word);
820 #if 1 INNER_THAN 2
821   sp -= len;
822   write_memory (sp, buffer, len);
823 #else /* stack grows upward */
824   write_memory (sp, buffer, len);
825   sp += len;
826 #endif /* stack grows upward */
827
828   return sp;
829 }
830
831 /* Push LEN bytes with data at BUFFER.  */
832
833 CORE_ADDR
834 push_bytes (sp, buffer, len)
835      CORE_ADDR sp;
836      char *buffer;
837      int len;
838 {
839 #if 1 INNER_THAN 2
840   sp -= len;
841   write_memory (sp, buffer, len);
842 #else /* stack grows upward */
843   write_memory (sp, buffer, len);
844   sp += len;
845 #endif /* stack grows upward */
846
847   return sp;
848 }
849
850 /* Push onto the stack the specified value VALUE.  */
851
852 static CORE_ADDR
853 value_push (sp, arg)
854      register CORE_ADDR sp;
855      value_ptr arg;
856 {
857   register int len = TYPE_LENGTH (VALUE_TYPE (arg));
858
859 #if 1 INNER_THAN 2
860   sp -= len;
861   write_memory (sp, VALUE_CONTENTS (arg), len);
862 #else /* stack grows upward */
863   write_memory (sp, VALUE_CONTENTS (arg), len);
864   sp += len;
865 #endif /* stack grows upward */
866
867   return sp;
868 }
869
870 /* Perform the standard coercions that are specified
871    for arguments to be passed to C functions.
872
873    If PARAM_TYPE is non-NULL, it is the expected parameter type. */
874
875 static value_ptr
876 value_arg_coerce (arg, param_type)
877      value_ptr arg;
878      struct type *param_type;
879 {
880   register struct type *arg_type = check_typedef (VALUE_TYPE (arg));
881   register struct type *type
882     = param_type ? check_typedef (param_type) : arg_type;
883
884   switch (TYPE_CODE (type))
885     {
886     case TYPE_CODE_REF:
887       if (TYPE_CODE (arg_type) != TYPE_CODE_REF)
888         {
889           arg = value_addr (arg);
890           VALUE_TYPE (arg) = param_type;
891           return arg;
892         }
893       break;
894     case TYPE_CODE_INT:
895     case TYPE_CODE_CHAR:
896     case TYPE_CODE_BOOL:
897     case TYPE_CODE_ENUM:
898       if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
899         type = builtin_type_int;
900       break;
901    case TYPE_CODE_FLT:
902      /* coerce float to double, unless the function prototype specifies float */
903      if (COERCE_FLOAT_TO_DOUBLE)
904        {
905          if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
906            type = builtin_type_double;
907          else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double))
908            type = builtin_type_long_double;
909        }
910      break;
911     case TYPE_CODE_FUNC:
912       type = lookup_pointer_type (type);
913       break;
914     case TYPE_CODE_ARRAY:
915       if (current_language->c_style_arrays)
916         type = lookup_pointer_type (TYPE_TARGET_TYPE (type));
917       break;
918     case TYPE_CODE_UNDEF:
919     case TYPE_CODE_PTR:
920     case TYPE_CODE_STRUCT:
921     case TYPE_CODE_UNION:
922     case TYPE_CODE_VOID:
923     case TYPE_CODE_SET:
924     case TYPE_CODE_RANGE:
925     case TYPE_CODE_STRING:
926     case TYPE_CODE_BITSTRING:
927     case TYPE_CODE_ERROR:
928     case TYPE_CODE_MEMBER:
929     case TYPE_CODE_METHOD:
930     case TYPE_CODE_COMPLEX:
931     default:
932       break;
933     }
934
935   return value_cast (type, arg);
936 }
937
938 /* Determine a function's address and its return type from its value. 
939    Calls error() if the function is not valid for calling.  */
940
941 static CORE_ADDR
942 find_function_addr (function, retval_type)
943      value_ptr function;
944      struct type **retval_type;
945 {
946   register struct type *ftype = check_typedef (VALUE_TYPE (function));
947   register enum type_code code = TYPE_CODE (ftype);
948   struct type *value_type;
949   CORE_ADDR funaddr;
950
951   /* If it's a member function, just look at the function
952      part of it.  */
953
954   /* Determine address to call.  */
955   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
956     {
957       funaddr = VALUE_ADDRESS (function);
958       value_type = TYPE_TARGET_TYPE (ftype);
959     }
960   else if (code == TYPE_CODE_PTR)
961     {
962       funaddr = value_as_pointer (function);
963       ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
964       if (TYPE_CODE (ftype) == TYPE_CODE_FUNC
965           || TYPE_CODE (ftype) == TYPE_CODE_METHOD)
966         {
967 #ifdef CONVERT_FROM_FUNC_PTR_ADDR
968           /* FIXME: This is a workaround for the unusual function
969              pointer representation on the RS/6000, see comment
970              in config/rs6000/tm-rs6000.h  */
971           funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr);
972 #endif
973           value_type = TYPE_TARGET_TYPE (ftype);
974         }
975       else
976         value_type = builtin_type_int;
977     }
978   else if (code == TYPE_CODE_INT)
979     {
980       /* Handle the case of functions lacking debugging info.
981          Their values are characters since their addresses are char */
982       if (TYPE_LENGTH (ftype) == 1)
983         funaddr = value_as_pointer (value_addr (function));
984       else
985         /* Handle integer used as address of a function.  */
986         funaddr = (CORE_ADDR) value_as_long (function);
987
988       value_type = builtin_type_int;
989     }
990   else
991     error ("Invalid data type for function to be called.");
992
993   *retval_type = value_type;
994   return funaddr;
995 }
996
997 #if defined (CALL_DUMMY)
998 /* All this stuff with a dummy frame may seem unnecessarily complicated
999    (why not just save registers in GDB?).  The purpose of pushing a dummy
1000    frame which looks just like a real frame is so that if you call a
1001    function and then hit a breakpoint (get a signal, etc), "backtrace"
1002    will look right.  Whether the backtrace needs to actually show the
1003    stack at the time the inferior function was called is debatable, but
1004    it certainly needs to not display garbage.  So if you are contemplating
1005    making dummy frames be different from normal frames, consider that.  */
1006
1007 /* Perform a function call in the inferior.
1008    ARGS is a vector of values of arguments (NARGS of them).
1009    FUNCTION is a value, the function to be called.
1010    Returns a value representing what the function returned.
1011    May fail to return, if a breakpoint or signal is hit
1012    during the execution of the function.
1013
1014    ARGS is modified to contain coerced values. */
1015
1016 value_ptr
1017 call_function_by_hand (function, nargs, args)
1018      value_ptr function;
1019      int nargs;
1020      value_ptr *args;
1021 {
1022   register CORE_ADDR sp;
1023   register int i;
1024   CORE_ADDR start_sp;
1025   /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word
1026      is in host byte order.  Before calling FIX_CALL_DUMMY, we byteswap it
1027      and remove any extra bytes which might exist because unsigned LONGEST is
1028      bigger than REGISTER_SIZE.  */
1029   static unsigned LONGEST dummy[] = CALL_DUMMY;
1030   char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (unsigned LONGEST)];
1031   CORE_ADDR old_sp;
1032   struct type *value_type;
1033   unsigned char struct_return;
1034   CORE_ADDR struct_addr;
1035   struct inferior_status inf_status;
1036   struct cleanup *old_chain;
1037   CORE_ADDR funaddr;
1038   int using_gcc;
1039   CORE_ADDR real_pc;
1040   struct type *ftype = check_typedef (SYMBOL_TYPE (function));
1041
1042   if (!target_has_execution)
1043     noprocess();
1044
1045   save_inferior_status (&inf_status, 1);
1046   old_chain = make_cleanup (restore_inferior_status, &inf_status);
1047
1048   /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
1049      (and POP_FRAME for restoring them).  (At least on most machines)
1050      they are saved on the stack in the inferior.  */
1051   PUSH_DUMMY_FRAME;
1052
1053   old_sp = sp = read_sp ();
1054
1055 #if 1 INNER_THAN 2              /* Stack grows down */
1056   sp -= sizeof dummy1;
1057   start_sp = sp;
1058 #else                           /* Stack grows up */
1059   start_sp = sp;
1060   sp += sizeof dummy1;
1061 #endif
1062
1063   funaddr = find_function_addr (function, &value_type);
1064   CHECK_TYPEDEF (value_type);
1065
1066   {
1067     struct block *b = block_for_pc (funaddr);
1068     /* If compiled without -g, assume GCC.  */
1069     using_gcc = b == NULL ? 0 : BLOCK_GCC_COMPILED (b);
1070   }
1071
1072   /* Are we returning a value using a structure return or a normal
1073      value return? */
1074
1075   struct_return = using_struct_return (function, funaddr, value_type,
1076                                        using_gcc);
1077
1078   /* Create a call sequence customized for this function
1079      and the number of arguments for it.  */
1080   for (i = 0; i < (int) (sizeof (dummy) / sizeof (dummy[0])); i++)
1081     store_unsigned_integer (&dummy1[i * REGISTER_SIZE],
1082                             REGISTER_SIZE,
1083                             (unsigned LONGEST)dummy[i]);
1084
1085 #ifdef GDB_TARGET_IS_HPPA
1086   real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1087                             value_type, using_gcc);
1088 #else
1089   FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
1090                   value_type, using_gcc);
1091   real_pc = start_sp;
1092 #endif
1093
1094 #if CALL_DUMMY_LOCATION == ON_STACK
1095   write_memory (start_sp, (char *)dummy1, sizeof dummy1);
1096 #endif /* On stack.  */
1097
1098 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
1099   /* Convex Unix prohibits executing in the stack segment. */
1100   /* Hope there is empty room at the top of the text segment. */
1101   {
1102     extern CORE_ADDR text_end;
1103     static checked = 0;
1104     if (!checked)
1105       for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp)
1106         if (read_memory_integer (start_sp, 1) != 0)
1107           error ("text segment full -- no place to put call");
1108     checked = 1;
1109     sp = old_sp;
1110     real_pc = text_end - sizeof dummy1;
1111     write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1112   }
1113 #endif /* Before text_end.  */
1114
1115 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
1116   {
1117     extern CORE_ADDR text_end;
1118     int errcode;
1119     sp = old_sp;
1120     real_pc = text_end;
1121     errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1);
1122     if (errcode != 0)
1123       error ("Cannot write text segment -- call_function failed");
1124   }
1125 #endif /* After text_end.  */
1126
1127 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
1128   real_pc = funaddr;
1129 #endif /* At entry point.  */
1130
1131 #ifdef lint
1132   sp = old_sp;          /* It really is used, for some ifdef's... */
1133 #endif
1134
1135   if (nargs < TYPE_NFIELDS (ftype))
1136     error ("too few arguments in function call");
1137
1138   for (i = nargs - 1; i >= 0; i--)
1139     {
1140       struct type *param_type;
1141       if (TYPE_NFIELDS (ftype) > i)
1142         param_type = TYPE_FIELD_TYPE (ftype, i);
1143       else
1144         param_type = 0;
1145       args[i] = value_arg_coerce (args[i], param_type);
1146     }
1147
1148 #if defined (REG_STRUCT_HAS_ADDR)
1149   {
1150     /* This is a machine like the sparc, where we may need to pass a pointer
1151        to the structure, not the structure itself.  */
1152     for (i = nargs - 1; i >= 0; i--)
1153       {
1154         struct type *arg_type = check_typedef (VALUE_TYPE (args[i]));
1155         if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT
1156              || TYPE_CODE (arg_type) == TYPE_CODE_UNION
1157              || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY
1158              || TYPE_CODE (arg_type) == TYPE_CODE_STRING
1159              || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING
1160              || TYPE_CODE (arg_type) == TYPE_CODE_SET
1161              || (TYPE_CODE (arg_type) == TYPE_CODE_FLT
1162                  && TYPE_LENGTH (arg_type) > 8)
1163              )
1164           && REG_STRUCT_HAS_ADDR (using_gcc, arg_type))
1165           {
1166             CORE_ADDR addr;
1167             int len = TYPE_LENGTH (arg_type);
1168 #ifdef STACK_ALIGN
1169             int aligned_len = STACK_ALIGN (len);
1170 #else
1171             int aligned_len = len;
1172 #endif
1173 #if !(1 INNER_THAN 2)
1174             /* The stack grows up, so the address of the thing we push
1175                is the stack pointer before we push it.  */
1176             addr = sp;
1177 #else
1178             sp -= aligned_len;
1179 #endif
1180             /* Push the structure.  */
1181             write_memory (sp, VALUE_CONTENTS (args[i]), len);
1182 #if 1 INNER_THAN 2
1183             /* The stack grows down, so the address of the thing we push
1184                is the stack pointer after we push it.  */
1185             addr = sp;
1186 #else
1187             sp += aligned_len;
1188 #endif
1189             /* The value we're going to pass is the address of the thing
1190                we just pushed.  */
1191             args[i] = value_from_longest (lookup_pointer_type (value_type),
1192                                           (LONGEST) addr);
1193           }
1194       }
1195   }
1196 #endif /* REG_STRUCT_HAS_ADDR.  */
1197
1198   /* Reserve space for the return structure to be written on the
1199      stack, if necessary */
1200
1201   if (struct_return)
1202     {
1203       int len = TYPE_LENGTH (value_type);
1204 #ifdef STACK_ALIGN
1205       len = STACK_ALIGN (len);
1206 #endif
1207 #if 1 INNER_THAN 2
1208       sp -= len;
1209       struct_addr = sp;
1210 #else
1211       struct_addr = sp;
1212       sp += len;
1213 #endif
1214     }
1215
1216 #ifdef STACK_ALIGN
1217   /* If stack grows down, we must leave a hole at the top. */
1218   {
1219     int len = 0;
1220
1221     for (i = nargs - 1; i >= 0; i--)
1222       len += TYPE_LENGTH (VALUE_TYPE (args[i]));
1223 #ifdef CALL_DUMMY_STACK_ADJUST
1224     len += CALL_DUMMY_STACK_ADJUST;
1225 #endif
1226 #if 1 INNER_THAN 2
1227     sp -= STACK_ALIGN (len) - len;
1228 #else
1229     sp += STACK_ALIGN (len) - len;
1230 #endif
1231   }
1232 #endif /* STACK_ALIGN */
1233
1234 #ifdef PUSH_ARGUMENTS
1235   PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
1236 #else /* !PUSH_ARGUMENTS */
1237   for (i = nargs - 1; i >= 0; i--)
1238     sp = value_push (sp, args[i]);
1239 #endif /* !PUSH_ARGUMENTS */
1240
1241 #ifdef CALL_DUMMY_STACK_ADJUST
1242 #if 1 INNER_THAN 2
1243   sp -= CALL_DUMMY_STACK_ADJUST;
1244 #else
1245   sp += CALL_DUMMY_STACK_ADJUST;
1246 #endif
1247 #endif /* CALL_DUMMY_STACK_ADJUST */
1248
1249   /* Store the address at which the structure is supposed to be
1250      written.  Note that this (and the code which reserved the space
1251      above) assumes that gcc was used to compile this function.  Since
1252      it doesn't cost us anything but space and if the function is pcc
1253      it will ignore this value, we will make that assumption.
1254
1255      Also note that on some machines (like the sparc) pcc uses a 
1256      convention like gcc's.  */
1257
1258   if (struct_return)
1259     STORE_STRUCT_RETURN (struct_addr, sp);
1260
1261   /* Write the stack pointer.  This is here because the statements above
1262      might fool with it.  On SPARC, this write also stores the register
1263      window into the right place in the new stack frame, which otherwise
1264      wouldn't happen.  (See store_inferior_registers in sparc-nat.c.)  */
1265   write_sp (sp);
1266
1267   {
1268     char retbuf[REGISTER_BYTES];
1269     char *name;
1270     struct symbol *symbol;
1271
1272     name = NULL;
1273     symbol = find_pc_function (funaddr);
1274     if (symbol)
1275       {
1276         name = SYMBOL_SOURCE_NAME (symbol);
1277       }
1278     else
1279       {
1280         /* Try the minimal symbols.  */
1281         struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr);
1282
1283         if (msymbol)
1284           {
1285             name = SYMBOL_SOURCE_NAME (msymbol);
1286           }
1287       }
1288     if (name == NULL)
1289       {
1290         char format[80];
1291         sprintf (format, "at %s", local_hex_format ());
1292         name = alloca (80);
1293         /* FIXME-32x64: assumes funaddr fits in a long.  */
1294         sprintf (name, format, (unsigned long) funaddr);
1295       }
1296
1297     /* Execute the stack dummy routine, calling FUNCTION.
1298        When it is done, discard the empty frame
1299        after storing the contents of all regs into retbuf.  */
1300     if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf))
1301       {
1302         /* We stopped somewhere besides the call dummy.  */
1303
1304         /* If we did the cleanups, we would print a spurious error message
1305            (Unable to restore previously selected frame), would write the
1306            registers from the inf_status (which is wrong), and would do other
1307            wrong things (like set stop_bpstat to the wrong thing).  */
1308         discard_cleanups (old_chain);
1309         /* Prevent memory leak.  */
1310         bpstat_clear (&inf_status.stop_bpstat);
1311
1312         /* The following error message used to say "The expression
1313            which contained the function call has been discarded."  It
1314            is a hard concept to explain in a few words.  Ideally, GDB
1315            would be able to resume evaluation of the expression when
1316            the function finally is done executing.  Perhaps someday
1317            this will be implemented (it would not be easy).  */
1318
1319         /* FIXME: Insert a bunch of wrap_here; name can be very long if it's
1320            a C++ name with arguments and stuff.  */
1321         error ("\
1322 The program being debugged stopped while in a function called from GDB.\n\
1323 When the function (%s) is done executing, GDB will silently\n\
1324 stop (instead of continuing to evaluate the expression containing\n\
1325 the function call).", name);
1326       }
1327
1328     do_cleanups (old_chain);
1329
1330     /* Figure out the value returned by the function.  */
1331     return value_being_returned (value_type, retbuf, struct_return);
1332   }
1333 }
1334 #else /* no CALL_DUMMY.  */
1335 value_ptr
1336 call_function_by_hand (function, nargs, args)
1337      value_ptr function;
1338      int nargs;
1339      value_ptr *args;
1340 {
1341   error ("Cannot invoke functions on this machine.");
1342 }
1343 #endif /* no CALL_DUMMY.  */
1344
1345 \f
1346 /* Create a value for an array by allocating space in the inferior, copying
1347    the data into that space, and then setting up an array value.
1348
1349    The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
1350    populated from the values passed in ELEMVEC.
1351
1352    The element type of the array is inherited from the type of the
1353    first element, and all elements must have the same size (though we
1354    don't currently enforce any restriction on their types). */
1355
1356 value_ptr
1357 value_array (lowbound, highbound, elemvec)
1358      int lowbound;
1359      int highbound;
1360      value_ptr *elemvec;
1361 {
1362   int nelem;
1363   int idx;
1364   unsigned int typelength;
1365   value_ptr val;
1366   struct type *rangetype;
1367   struct type *arraytype;
1368   CORE_ADDR addr;
1369
1370   /* Validate that the bounds are reasonable and that each of the elements
1371      have the same size. */
1372
1373   nelem = highbound - lowbound + 1;
1374   if (nelem <= 0)
1375     {
1376       error ("bad array bounds (%d, %d)", lowbound, highbound);
1377     }
1378   typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
1379   for (idx = 1; idx < nelem; idx++)
1380     {
1381       if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
1382         {
1383           error ("array elements must all be the same size");
1384         }
1385     }
1386
1387   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
1388                                  lowbound, highbound);
1389   arraytype = create_array_type ((struct type *) NULL, 
1390                                  VALUE_TYPE (elemvec[0]), rangetype);
1391
1392   if (!current_language->c_style_arrays)
1393     {
1394       val = allocate_value (arraytype);
1395       for (idx = 0; idx < nelem; idx++)
1396         {
1397           memcpy (VALUE_CONTENTS_RAW (val) + (idx * typelength),
1398                   VALUE_CONTENTS (elemvec[idx]),
1399                   typelength);
1400         }
1401       return val;
1402     }
1403
1404   /* Allocate space to store the array in the inferior, and then initialize
1405      it by copying in each element.  FIXME:  Is it worth it to create a
1406      local buffer in which to collect each value and then write all the
1407      bytes in one operation? */
1408
1409   addr = allocate_space_in_inferior (nelem * typelength);
1410   for (idx = 0; idx < nelem; idx++)
1411     {
1412       write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
1413                     typelength);
1414     }
1415
1416   /* Create the array type and set up an array value to be evaluated lazily. */
1417
1418   val = value_at_lazy (arraytype, addr);
1419   return (val);
1420 }
1421
1422 /* Create a value for a string constant by allocating space in the inferior,
1423    copying the data into that space, and returning the address with type
1424    TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
1425    of characters.
1426    Note that string types are like array of char types with a lower bound of
1427    zero and an upper bound of LEN - 1.  Also note that the string may contain
1428    embedded null bytes. */
1429
1430 value_ptr
1431 value_string (ptr, len)
1432      char *ptr;
1433      int len;
1434 {
1435   value_ptr val;
1436   int lowbound = current_language->string_lower_bound;
1437   struct type *rangetype = create_range_type ((struct type *) NULL,
1438                                               builtin_type_int,
1439                                               lowbound, len + lowbound - 1);
1440   struct type *stringtype
1441     = create_string_type ((struct type *) NULL, rangetype);
1442   CORE_ADDR addr;
1443
1444   if (current_language->c_style_arrays == 0)
1445     {
1446       val = allocate_value (stringtype);
1447       memcpy (VALUE_CONTENTS_RAW (val), ptr, len);
1448       return val;
1449     }
1450
1451
1452   /* Allocate space to store the string in the inferior, and then
1453      copy LEN bytes from PTR in gdb to that address in the inferior. */
1454
1455   addr = allocate_space_in_inferior (len);
1456   write_memory (addr, ptr, len);
1457
1458   val = value_at_lazy (stringtype, addr);
1459   return (val);
1460 }
1461
1462 value_ptr
1463 value_bitstring (ptr, len)
1464      char *ptr;
1465      int len;
1466 {
1467   value_ptr val;
1468   struct type *domain_type = create_range_type (NULL, builtin_type_int,
1469                                                 0, len - 1);
1470   struct type *type = create_set_type ((struct type*) NULL, domain_type);
1471   TYPE_CODE (type) = TYPE_CODE_BITSTRING;
1472   val = allocate_value (type);
1473   memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type));
1474   return val;
1475 }
1476 \f
1477 /* See if we can pass arguments in T2 to a function which takes arguments
1478    of types T1.  Both t1 and t2 are NULL-terminated vectors.  If some
1479    arguments need coercion of some sort, then the coerced values are written
1480    into T2.  Return value is 0 if the arguments could be matched, or the
1481    position at which they differ if not.
1482
1483    STATICP is nonzero if the T1 argument list came from a
1484    static member function.
1485
1486    For non-static member functions, we ignore the first argument,
1487    which is the type of the instance variable.  This is because we want
1488    to handle calls with objects from derived classes.  This is not
1489    entirely correct: we should actually check to make sure that a
1490    requested operation is type secure, shouldn't we?  FIXME.  */
1491
1492 static int
1493 typecmp (staticp, t1, t2)
1494      int staticp;
1495      struct type *t1[];
1496      value_ptr t2[];
1497 {
1498   int i;
1499
1500   if (t2 == 0)
1501     return 1;
1502   if (staticp && t1 == 0)
1503     return t2[1] != 0;
1504   if (t1 == 0)
1505     return 1;
1506   if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
1507   if (t1[!staticp] == 0) return 0;
1508   for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
1509     {
1510     struct type *tt1, *tt2;
1511       if (! t2[i])
1512         return i+1;
1513       tt1 = check_typedef (t1[i]);
1514       tt2 = check_typedef (VALUE_TYPE(t2[i]));
1515       if (TYPE_CODE (tt1) == TYPE_CODE_REF
1516           /* We should be doing hairy argument matching, as below.  */
1517           && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2)))
1518         {
1519           if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
1520             t2[i] = value_coerce_array (t2[i]);
1521           else
1522             t2[i] = value_addr (t2[i]);
1523           continue;
1524         }
1525
1526       while (TYPE_CODE (tt1) == TYPE_CODE_PTR
1527           && (   TYPE_CODE (tt2) == TYPE_CODE_ARRAY
1528               || TYPE_CODE (tt2) == TYPE_CODE_PTR))
1529         {
1530            tt1 = check_typedef (TYPE_TARGET_TYPE(tt1)); 
1531            tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
1532         }
1533       if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue;
1534       /* Array to pointer is a `trivial conversion' according to the ARM.  */
1535
1536       /* We should be doing much hairier argument matching (see section 13.2
1537          of the ARM), but as a quick kludge, just check for the same type
1538          code.  */
1539       if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
1540         return i+1;
1541     }
1542   if (!t1[i]) return 0;
1543   return t2[i] ? i+1 : 0;
1544 }
1545
1546 /* Helper function used by value_struct_elt to recurse through baseclasses.
1547    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1548    and search in it assuming it has (class) type TYPE.
1549    If found, return value, else return NULL.
1550
1551    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
1552    look for a baseclass named NAME.  */
1553
1554 static value_ptr
1555 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
1556      char *name;
1557      register value_ptr arg1;
1558      int offset;
1559      register struct type *type;
1560      int looking_for_baseclass;
1561 {
1562   int i;
1563
1564   CHECK_TYPEDEF (type);
1565
1566   if (! looking_for_baseclass)
1567     for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1568       {
1569         char *t_field_name = TYPE_FIELD_NAME (type, i);
1570
1571         if (t_field_name && STREQ (t_field_name, name))
1572           {
1573             value_ptr v;
1574             if (TYPE_FIELD_STATIC (type, i))
1575               {
1576                 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1577                 struct symbol *sym =
1578                     lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1579                 if (sym == NULL)
1580                     error ("Internal error: could not find physical static variable named %s",
1581                            phys_name);
1582                 v = value_at (TYPE_FIELD_TYPE (type, i),
1583                               (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1584               }
1585             else
1586               v = value_primitive_field (arg1, offset, i, type);
1587             if (v == 0)
1588               error("there is no field named %s", name);
1589             return v;
1590           }
1591
1592         if (t_field_name
1593             && (t_field_name[0] == '\0'
1594                 || (TYPE_CODE (type) == TYPE_CODE_UNION
1595                     && STREQ (t_field_name, "else"))))
1596           {
1597             struct type *field_type = TYPE_FIELD_TYPE (type, i);
1598             if (TYPE_CODE (field_type) == TYPE_CODE_UNION
1599                 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
1600               {
1601                 /* Look for a match through the fields of an anonymous union,
1602                    or anonymous struct.  C++ provides anonymous unions.
1603
1604                    In the GNU Chill implementation of variant record types,
1605                    each <alternative field> has an (anonymous) union type,
1606                    each member of the union represents a <variant alternative>.
1607                    Each <variant alternative> is represented as a struct,
1608                    with a member for each <variant field>.  */
1609                    
1610                 value_ptr v;
1611                 int new_offset = offset;
1612
1613                 /* This is pretty gross.  In G++, the offset in an anonymous
1614                    union is relative to the beginning of the enclosing struct.
1615                    In the GNU Chill implementation of variant records,
1616                    the bitpos is zero in an anonymous union field, so we
1617                    have to add the offset of the union here. */
1618                 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
1619                     || (TYPE_NFIELDS (field_type) > 0
1620                         && TYPE_FIELD_BITPOS (field_type, 0) == 0))
1621                   new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
1622
1623                 v = search_struct_field (name, arg1, new_offset, field_type,
1624                                          looking_for_baseclass);
1625                 if (v)
1626                   return v;
1627               }
1628           }
1629       }
1630
1631   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1632     {
1633       value_ptr v;
1634       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
1635       /* If we are looking for baseclasses, this is what we get when we
1636          hit them.  But it could happen that the base part's member name
1637          is not yet filled in.  */
1638       int found_baseclass = (looking_for_baseclass
1639                              && TYPE_BASECLASS_NAME (type, i) != NULL
1640                              && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
1641
1642       if (BASETYPE_VIA_VIRTUAL (type, i))
1643         {
1644           int boffset = VALUE_OFFSET (arg1) + offset;
1645           boffset = baseclass_offset (type, i,
1646                                       VALUE_CONTENTS (arg1) + boffset,
1647                                       VALUE_ADDRESS (arg1) + boffset);
1648           if (boffset == -1)
1649             error ("virtual baseclass botch");
1650           if (found_baseclass)
1651             {
1652               value_ptr v2 = allocate_value (basetype);
1653               VALUE_LVAL (v2) = VALUE_LVAL (arg1);
1654               VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1);
1655               VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + offset + boffset;
1656               if (VALUE_LAZY (arg1))
1657                 VALUE_LAZY (v2) = 1;
1658               else
1659                 memcpy (VALUE_CONTENTS_RAW (v2),
1660                         VALUE_CONTENTS_RAW (arg1) + offset + boffset,
1661                         TYPE_LENGTH (basetype));
1662               return v2;
1663             }
1664           v = search_struct_field (name, arg1, offset + boffset,
1665                                    TYPE_BASECLASS (type, i),
1666                                    looking_for_baseclass);
1667         }
1668       else if (found_baseclass)
1669         v = value_primitive_field (arg1, offset, i, type);
1670       else
1671         v = search_struct_field (name, arg1,
1672                                  offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1673                                  basetype, looking_for_baseclass);
1674       if (v) return v;
1675     }
1676   return NULL;
1677 }
1678
1679 /* Helper function used by value_struct_elt to recurse through baseclasses.
1680    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1681    and search in it assuming it has (class) type TYPE.
1682    If found, return value, else if name matched and args not return (value)-1,
1683    else return NULL. */
1684
1685 static value_ptr
1686 search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
1687      char *name;
1688      register value_ptr *arg1p, *args;
1689      int offset, *static_memfuncp;
1690      register struct type *type;
1691 {
1692   int i;
1693   value_ptr v;
1694   int name_matched = 0;
1695   char dem_opname[64];
1696
1697   CHECK_TYPEDEF (type);
1698   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1699     {
1700       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1701       if (strncmp(t_field_name, "__", 2)==0 ||
1702         strncmp(t_field_name, "op", 2)==0 ||
1703         strncmp(t_field_name, "type", 4)==0 )
1704         {
1705           if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
1706             t_field_name = dem_opname;
1707           else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
1708             t_field_name = dem_opname; 
1709         }
1710       if (t_field_name && STREQ (t_field_name, name))
1711         {
1712           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1713           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1714           name_matched = 1; 
1715
1716           if (j > 0 && args == 0)
1717             error ("cannot resolve overloaded method `%s'", name);
1718           while (j >= 0)
1719             {
1720               if (TYPE_FN_FIELD_STUB (f, j))
1721                 check_stub_method (type, i, j);
1722               if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1723                             TYPE_FN_FIELD_ARGS (f, j), args))
1724                 {
1725                   if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1726                     return value_virtual_fn_field (arg1p, f, j, type, offset);
1727                   if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1728                     *static_memfuncp = 1;
1729                   v = value_fn_field (arg1p, f, j, type, offset);
1730                   if (v != NULL) return v;
1731                 }
1732               j--;
1733             }
1734         }
1735     }
1736
1737   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1738     {
1739       int base_offset;
1740
1741       if (BASETYPE_VIA_VIRTUAL (type, i))
1742         {
1743           base_offset = VALUE_OFFSET (*arg1p) + offset;
1744           base_offset =
1745             baseclass_offset (type, i,
1746                               VALUE_CONTENTS (*arg1p) + base_offset,
1747                               VALUE_ADDRESS (*arg1p) + base_offset);
1748           if (base_offset == -1)
1749             error ("virtual baseclass botch");
1750         }
1751       else
1752         {
1753           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1754         }
1755       v = search_struct_method (name, arg1p, args, base_offset + offset,
1756                                 static_memfuncp, TYPE_BASECLASS (type, i));
1757       if (v == (value_ptr) -1)
1758         {
1759           name_matched = 1;
1760         }
1761       else if (v)
1762         {
1763 /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
1764 /*        *arg1p = arg1_tmp;*/
1765           return v;
1766         }
1767     }
1768   if (name_matched) return (value_ptr) -1;
1769   else return NULL;
1770 }
1771
1772 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1773    extract the component named NAME from the ultimate target structure/union
1774    and return it as a value with its appropriate type.
1775    ERR is used in the error message if *ARGP's type is wrong.
1776
1777    C++: ARGS is a list of argument types to aid in the selection of
1778    an appropriate method. Also, handle derived types.
1779
1780    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1781    where the truthvalue of whether the function that was resolved was
1782    a static member function or not is stored.
1783
1784    ERR is an error message to be printed in case the field is not found.  */
1785
1786 value_ptr
1787 value_struct_elt (argp, args, name, static_memfuncp, err)
1788      register value_ptr *argp, *args;
1789      char *name;
1790      int *static_memfuncp;
1791      char *err;
1792 {
1793   register struct type *t;
1794   value_ptr v;
1795
1796   COERCE_ARRAY (*argp);
1797
1798   t = check_typedef (VALUE_TYPE (*argp));
1799
1800   /* Follow pointers until we get to a non-pointer.  */
1801
1802   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1803     {
1804       *argp = value_ind (*argp);
1805       /* Don't coerce fn pointer to fn and then back again!  */
1806       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1807         COERCE_ARRAY (*argp);
1808       t = check_typedef (VALUE_TYPE (*argp));
1809     }
1810
1811   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1812     error ("not implemented: member type in value_struct_elt");
1813
1814   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
1815       && TYPE_CODE (t) != TYPE_CODE_UNION)
1816     error ("Attempt to extract a component of a value that is not a %s.", err);
1817
1818   /* Assume it's not, unless we see that it is.  */
1819   if (static_memfuncp)
1820     *static_memfuncp =0;
1821
1822   if (!args)
1823     {
1824       /* if there are no arguments ...do this...  */
1825
1826       /* Try as a field first, because if we succeed, there
1827          is less work to be done.  */
1828       v = search_struct_field (name, *argp, 0, t, 0);
1829       if (v)
1830         return v;
1831
1832       /* C++: If it was not found as a data field, then try to
1833          return it as a pointer to a method.  */
1834
1835       if (destructor_name_p (name, t))
1836         error ("Cannot get value of destructor");
1837
1838       v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1839
1840       if (v == (value_ptr) -1)
1841         error ("Cannot take address of a method");
1842       else if (v == 0)
1843         {
1844           if (TYPE_NFN_FIELDS (t))
1845             error ("There is no member or method named %s.", name);
1846           else
1847             error ("There is no member named %s.", name);
1848         }
1849       return v;
1850     }
1851
1852   if (destructor_name_p (name, t))
1853     {
1854       if (!args[1])
1855         {
1856           /* destructors are a special case.  */
1857           v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
1858                               TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0);
1859           if (!v) error("could not find destructor function named %s.", name);
1860           else return v;
1861         }
1862       else
1863         {
1864           error ("destructor should not have any argument");
1865         }
1866     }
1867   else
1868     v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
1869
1870   if (v == (value_ptr) -1)
1871     {
1872         error("Argument list of %s mismatch with component in the structure.", name);
1873     }
1874   else if (v == 0)
1875     {
1876       /* See if user tried to invoke data as function.  If so,
1877          hand it back.  If it's not callable (i.e., a pointer to function),
1878          gdb should give an error.  */
1879       v = search_struct_field (name, *argp, 0, t, 0);
1880     }
1881
1882   if (!v)
1883     error ("Structure has no component named %s.", name);
1884   return v;
1885 }
1886
1887 /* C++: return 1 is NAME is a legitimate name for the destructor
1888    of type TYPE.  If TYPE does not have a destructor, or
1889    if NAME is inappropriate for TYPE, an error is signaled.  */
1890 int
1891 destructor_name_p (name, type)
1892      const char *name;
1893      const struct type *type;
1894 {
1895   /* destructors are a special case.  */
1896
1897   if (name[0] == '~')
1898     {
1899       char *dname = type_name_no_tag (type);
1900       char *cp = strchr (dname, '<');
1901       unsigned int len;
1902
1903       /* Do not compare the template part for template classes.  */
1904       if (cp == NULL)
1905         len = strlen (dname);
1906       else
1907         len = cp - dname;
1908       if (strlen (name + 1) != len || !STREQN (dname, name + 1, len))
1909         error ("name of destructor must equal name of class");
1910       else
1911         return 1;
1912     }
1913   return 0;
1914 }
1915
1916 /* Helper function for check_field: Given TYPE, a structure/union,
1917    return 1 if the component named NAME from the ultimate
1918    target structure/union is defined, otherwise, return 0. */
1919
1920 static int
1921 check_field_in (type, name)
1922      register struct type *type;
1923      const char *name;
1924 {
1925   register int i;
1926
1927   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1928     {
1929       char *t_field_name = TYPE_FIELD_NAME (type, i);
1930       if (t_field_name && STREQ (t_field_name, name))
1931         return 1;
1932     }
1933
1934   /* C++: If it was not found as a data field, then try to
1935      return it as a pointer to a method.  */
1936
1937   /* Destructors are a special case.  */
1938   if (destructor_name_p (name, type))
1939     return 1;
1940
1941   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1942     {
1943       if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
1944         return 1;
1945     }
1946
1947   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1948     if (check_field_in (TYPE_BASECLASS (type, i), name))
1949       return 1;
1950       
1951   return 0;
1952 }
1953
1954
1955 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1956    return 1 if the component named NAME from the ultimate
1957    target structure/union is defined, otherwise, return 0.  */
1958
1959 int
1960 check_field (arg1, name)
1961      register value_ptr arg1;
1962      const char *name;
1963 {
1964   register struct type *t;
1965
1966   COERCE_ARRAY (arg1);
1967
1968   t = VALUE_TYPE (arg1);
1969
1970   /* Follow pointers until we get to a non-pointer.  */
1971
1972   for (;;)
1973     {
1974       CHECK_TYPEDEF (t);
1975       if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF)
1976         break;
1977       t = TYPE_TARGET_TYPE (t);
1978     }
1979
1980   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1981     error ("not implemented: member type in check_field");
1982
1983   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
1984       && TYPE_CODE (t) != TYPE_CODE_UNION)
1985     error ("Internal error: `this' is not an aggregate");
1986
1987   return check_field_in (t, name);
1988 }
1989
1990 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1991    return the address of this member as a "pointer to member"
1992    type.  If INTYPE is non-null, then it will be the type
1993    of the member we are looking for.  This will help us resolve
1994    "pointers to member functions".  This function is used
1995    to resolve user expressions of the form "DOMAIN::NAME".  */
1996
1997 value_ptr
1998 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1999      struct type *domain, *curtype, *intype;
2000      int offset;
2001      char *name;
2002 {
2003   register struct type *t = curtype;
2004   register int i;
2005   value_ptr v;
2006
2007   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
2008       && TYPE_CODE (t) != TYPE_CODE_UNION)
2009     error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
2010
2011   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
2012     {
2013       char *t_field_name = TYPE_FIELD_NAME (t, i);
2014       
2015       if (t_field_name && STREQ (t_field_name, name))
2016         {
2017           if (TYPE_FIELD_STATIC (t, i))
2018             {
2019               char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
2020               struct symbol *sym =
2021                 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
2022               if (sym == NULL)
2023                 error ("Internal error: could not find physical static variable named %s",
2024                        phys_name);
2025               return value_at (SYMBOL_TYPE (sym),
2026                                (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
2027             }
2028           if (TYPE_FIELD_PACKED (t, i))
2029             error ("pointers to bitfield members not allowed");
2030           
2031           return value_from_longest
2032             (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
2033                                                         domain)),
2034              offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
2035         }
2036     }
2037
2038   /* C++: If it was not found as a data field, then try to
2039      return it as a pointer to a method.  */
2040
2041   /* Destructors are a special case.  */
2042   if (destructor_name_p (name, t))
2043     {
2044       error ("member pointers to destructors not implemented yet");
2045     }
2046
2047   /* Perform all necessary dereferencing.  */
2048   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
2049     intype = TYPE_TARGET_TYPE (intype);
2050
2051   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
2052     {
2053       char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
2054       char dem_opname[64];
2055
2056       if (strncmp(t_field_name, "__", 2)==0 ||
2057         strncmp(t_field_name, "op", 2)==0 ||
2058         strncmp(t_field_name, "type", 4)==0 )
2059         {
2060           if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI))
2061             t_field_name = dem_opname;
2062           else if (cplus_demangle_opname(t_field_name, dem_opname, 0))
2063             t_field_name = dem_opname; 
2064         }
2065       if (t_field_name && STREQ (t_field_name, name))
2066         {
2067           int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
2068           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
2069           
2070           if (intype == 0 && j > 1)
2071             error ("non-unique member `%s' requires type instantiation", name);
2072           if (intype)
2073             {
2074               while (j--)
2075                 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
2076                   break;
2077               if (j < 0)
2078                 error ("no member function matches that type instantiation");
2079             }
2080           else
2081             j = 0;
2082           
2083           if (TYPE_FN_FIELD_STUB (f, j))
2084             check_stub_method (t, i, j);
2085           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
2086             {
2087               return value_from_longest
2088                 (lookup_reference_type
2089                  (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2090                                       domain)),
2091                  (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j)));
2092             }
2093           else
2094             {
2095               struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
2096                                                 0, VAR_NAMESPACE, 0, NULL);
2097               if (s == NULL)
2098                 {
2099                   v = 0;
2100                 }
2101               else
2102                 {
2103                   v = read_var_value (s, 0);
2104 #if 0
2105                   VALUE_TYPE (v) = lookup_reference_type
2106                     (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
2107                                          domain));
2108 #endif
2109                 }
2110               return v;
2111             }
2112         }
2113     }
2114   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
2115     {
2116       value_ptr v;
2117       int base_offset;
2118
2119       if (BASETYPE_VIA_VIRTUAL (t, i))
2120         base_offset = 0;
2121       else
2122         base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
2123       v = value_struct_elt_for_reference (domain,
2124                                           offset + base_offset,
2125                                           TYPE_BASECLASS (t, i),
2126                                           name,
2127                                           intype);
2128       if (v)
2129         return v;
2130     }
2131   return 0;
2132 }
2133
2134 /* C++: return the value of the class instance variable, if one exists.
2135    Flag COMPLAIN signals an error if the request is made in an
2136    inappropriate context.  */
2137
2138 value_ptr
2139 value_of_this (complain)
2140      int complain;
2141 {
2142   struct symbol *func, *sym;
2143   struct block *b;
2144   int i;
2145   static const char funny_this[] = "this";
2146   value_ptr this;
2147
2148   if (selected_frame == 0)
2149     if (complain)
2150       error ("no frame selected");
2151     else return 0;
2152
2153   func = get_frame_function (selected_frame);
2154   if (!func)
2155     {
2156       if (complain)
2157         error ("no `this' in nameless context");
2158       else return 0;
2159     }
2160
2161   b = SYMBOL_BLOCK_VALUE (func);
2162   i = BLOCK_NSYMS (b);
2163   if (i <= 0)
2164     if (complain)
2165       error ("no args, no `this'");
2166     else return 0;
2167
2168   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
2169      symbol instead of the LOC_ARG one (if both exist).  */
2170   sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
2171   if (sym == NULL)
2172     {
2173       if (complain)
2174         error ("current stack frame not in method");
2175       else
2176         return NULL;
2177     }
2178
2179   this = read_var_value (sym, selected_frame);
2180   if (this == 0 && complain)
2181     error ("`this' argument at unknown address");
2182   return this;
2183 }
2184
2185 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements
2186    long, starting at LOWBOUND.  The result has the same lower bound as
2187    the original ARRAY.  */
2188
2189 value_ptr
2190 value_slice (array, lowbound, length)
2191      value_ptr array;
2192      int lowbound, length;
2193 {
2194   struct type *slice_range_type, *slice_type, *range_type;
2195   LONGEST lowerbound, upperbound, offset;
2196   value_ptr slice;
2197   struct type *array_type;
2198   array_type = check_typedef (VALUE_TYPE (array));
2199   COERCE_VARYING_ARRAY (array, array_type);
2200   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
2201       && TYPE_CODE (array_type) != TYPE_CODE_STRING
2202       && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING)
2203     error ("cannot take slice of non-array");
2204   range_type = TYPE_INDEX_TYPE (array_type);
2205   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
2206     error ("slice from bad array or bitstring");
2207   if (lowbound < lowerbound || length < 0
2208       || lowbound + length - 1 > upperbound
2209       /* Chill allows zero-length strings but not arrays. */
2210       || (current_language->la_language == language_chill
2211           && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY))
2212     error ("slice out of range");
2213   /* FIXME-type-allocation: need a way to free this type when we are
2214      done with it.  */
2215   slice_range_type = create_range_type ((struct type*) NULL,
2216                                         TYPE_TARGET_TYPE (range_type),
2217                                         lowerbound, lowerbound + length - 1);
2218   if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING)
2219     {
2220       int i;
2221       slice_type = create_set_type ((struct type*) NULL, slice_range_type);
2222       TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING;
2223       slice = value_zero (slice_type, not_lval);
2224       for (i = 0; i < length; i++)
2225         {
2226           int element = value_bit_index (array_type,
2227                                          VALUE_CONTENTS (array),
2228                                          lowbound + i);
2229           if (element < 0)
2230             error ("internal error accessing bitstring");
2231           else if (element > 0)
2232             {
2233               int j = i % TARGET_CHAR_BIT;
2234               if (BITS_BIG_ENDIAN)
2235                 j = TARGET_CHAR_BIT - 1 - j;
2236               VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j);
2237             }
2238         }
2239       /* We should set the address, bitssize, and bitspos, so the clice
2240          can be used on the LHS, but that may require extensions to
2241          value_assign.  For now, just leave as a non_lval.  FIXME.  */
2242     }
2243   else
2244     {
2245       struct type *element_type = TYPE_TARGET_TYPE (array_type);
2246       offset
2247         = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
2248       slice_type = create_array_type ((struct type*) NULL, element_type,
2249                                       slice_range_type);
2250       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
2251       slice = allocate_value (slice_type);
2252       if (VALUE_LAZY (array))
2253         VALUE_LAZY (slice) = 1;
2254       else
2255         memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset,
2256                 TYPE_LENGTH (slice_type));
2257       if (VALUE_LVAL (array) == lval_internalvar)
2258         VALUE_LVAL (slice) = lval_internalvar_component;
2259       else
2260         VALUE_LVAL (slice) = VALUE_LVAL (array);
2261       VALUE_ADDRESS (slice) = VALUE_ADDRESS (array);
2262       VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset;
2263     }
2264   return slice;
2265 }
2266
2267 /* Assuming chill_varying_type (VARRAY) is true, return an equivalent
2268    value as a fixed-length array. */
2269
2270 value_ptr
2271 varying_to_slice (varray)
2272      value_ptr varray;
2273 {
2274   struct type *vtype = check_typedef (VALUE_TYPE (varray));
2275   LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0),
2276                                 VALUE_CONTENTS (varray)
2277                                 + TYPE_FIELD_BITPOS (vtype, 0) / 8);
2278   return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length);
2279 }
2280
2281 /* Create a value for a FORTRAN complex number.  Currently most of 
2282    the time values are coerced to COMPLEX*16 (i.e. a complex number 
2283    composed of 2 doubles.  This really should be a smarter routine 
2284    that figures out precision inteligently as opposed to assuming 
2285    doubles. FIXME: fmb */ 
2286
2287 value_ptr
2288 value_literal_complex (arg1, arg2, type)
2289      value_ptr arg1;
2290      value_ptr arg2;
2291      struct type *type;
2292 {
2293   register value_ptr val;
2294   struct type *real_type = TYPE_TARGET_TYPE (type);
2295
2296   val = allocate_value (type);
2297   arg1 = value_cast (real_type, arg1);
2298   arg2 = value_cast (real_type, arg2);
2299
2300   memcpy (VALUE_CONTENTS_RAW (val),
2301           VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type));
2302   memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type),
2303           VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type));
2304   return val;
2305 }
2306
2307 /* Cast a value into the appropriate complex data type. */
2308
2309 static value_ptr
2310 cast_into_complex (type, val)
2311      struct type *type;
2312      register value_ptr val;
2313 {
2314   struct type *real_type = TYPE_TARGET_TYPE (type);
2315   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX)
2316     {
2317       struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val));
2318       value_ptr re_val = allocate_value (val_real_type);
2319       value_ptr im_val = allocate_value (val_real_type);
2320
2321       memcpy (VALUE_CONTENTS_RAW (re_val),
2322               VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type));
2323       memcpy (VALUE_CONTENTS_RAW (im_val),
2324               VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type),
2325                TYPE_LENGTH (val_real_type));
2326
2327       return value_literal_complex (re_val, im_val, type);
2328     }
2329   else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT
2330            || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT)
2331     return value_literal_complex (val, value_zero (real_type, not_lval), type);
2332   else
2333     error ("cannot cast non-number to complex");
2334 }
2335
2336 void
2337 _initialize_valops ()
2338 {
2339 #if 0
2340   add_show_from_set
2341     (add_set_cmd ("abandon", class_support, var_boolean, (char *)&auto_abandon,
2342                   "Set automatic abandonment of expressions upon failure.",
2343                   &setlist),
2344      &showlist);
2345 #endif
2346 }
This page took 0.157448 seconds and 4 git commands to generate.