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