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