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