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