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