1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31 /* Local functions. */
34 find_function_addr PARAMS ((value, struct type **));
37 value_push PARAMS ((CORE_ADDR, value));
40 value_arg_push PARAMS ((CORE_ADDR, value));
43 search_struct_field PARAMS ((char *, value, int, struct type *, int));
46 search_struct_method PARAMS ((char *, value, value *, int, int *,
50 check_field_in PARAMS ((struct type *, const char *));
53 /* Cast value ARG2 to type TYPE and return as a value.
54 More general than a C cast: accepts any two types of the same length,
55 and if ARG2 is an lvalue it can be cast into anything at all. */
56 /* In C++, casts may change pointer representations. */
59 value_cast (type, arg2)
63 register enum type_code code1;
64 register enum type_code code2;
67 /* Coerce arrays but not enums. Enums will work as-is
68 and coercing them would cause an infinite recursion. */
69 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
72 code1 = TYPE_CODE (type);
73 code2 = TYPE_CODE (VALUE_TYPE (arg2));
74 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
75 || code2 == TYPE_CODE_ENUM);
77 if (code1 == TYPE_CODE_FLT && scalar)
78 return value_from_double (type, value_as_double (arg2));
79 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
80 && (scalar || code2 == TYPE_CODE_PTR))
81 return value_from_longest (type, value_as_long (arg2));
82 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
84 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
86 /* Look in the type of the source to see if it contains the
87 type of the target as a superclass. If so, we'll need to
88 offset the pointer rather than just change its type. */
89 struct type *t1 = TYPE_TARGET_TYPE (type);
90 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
91 if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT
92 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
93 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
95 value v = search_struct_field (type_name_no_tag (t1),
96 value_ind (arg2), 0, t2, 1);
100 VALUE_TYPE (v) = type;
104 /* No superclass found, just fall through to change ptr type. */
106 VALUE_TYPE (arg2) = type;
109 else if (VALUE_LVAL (arg2) == lval_memory)
111 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
113 else if (code1 == TYPE_CODE_VOID)
115 return value_zero (builtin_type_void, not_lval);
119 error ("Invalid cast.");
124 /* Create a value of type TYPE that is zero, and return it. */
127 value_zero (type, lv)
131 register value val = allocate_value (type);
133 (void) memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
134 VALUE_LVAL (val) = lv;
139 /* Return a value with type TYPE located at ADDR.
141 Call value_at only if the data needs to be fetched immediately;
142 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
143 value_at_lazy instead. value_at_lazy simply records the address of
144 the data and sets the lazy-evaluation-required flag. The lazy flag
145 is tested in the VALUE_CONTENTS macro, which is used if and when
146 the contents are actually required. */
149 value_at (type, addr)
153 register value val = allocate_value (type);
155 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
157 VALUE_LVAL (val) = lval_memory;
158 VALUE_ADDRESS (val) = addr;
163 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
166 value_at_lazy (type, addr)
170 register value val = allocate_value (type);
172 VALUE_LVAL (val) = lval_memory;
173 VALUE_ADDRESS (val) = addr;
174 VALUE_LAZY (val) = 1;
179 /* Called only from the VALUE_CONTENTS macro, if the current data for
180 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
181 data from the user's process, and clears the lazy flag to indicate
182 that the data in the buffer is valid.
184 If the value is zero-length, we avoid calling read_memory, which would
185 abort. We mark the value as fetched anyway -- all 0 bytes of it.
187 This function returns a value because it is used in the VALUE_CONTENTS
188 macro as part of an expression, where a void would not work. The
192 value_fetch_lazy (val)
195 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
197 if (TYPE_LENGTH (VALUE_TYPE (val)))
198 read_memory (addr, VALUE_CONTENTS_RAW (val),
199 TYPE_LENGTH (VALUE_TYPE (val)));
200 VALUE_LAZY (val) = 0;
205 /* Store the contents of FROMVAL into the location of TOVAL.
206 Return a new value with the location of TOVAL and contents of FROMVAL. */
209 value_assign (toval, fromval)
210 register value toval, fromval;
212 register struct type *type = VALUE_TYPE (toval);
214 char raw_buffer[MAX_REGISTER_RAW_SIZE];
215 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
218 COERCE_ARRAY (fromval);
221 if (VALUE_LVAL (toval) != lval_internalvar)
222 fromval = value_cast (type, fromval);
224 /* If TOVAL is a special machine register requiring conversion
225 of program values to a special raw format,
226 convert FROMVAL's contents now, with result in `raw_buffer',
227 and set USE_BUFFER to the number of bytes to write. */
229 if (VALUE_REGNO (toval) >= 0
230 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
232 int regno = VALUE_REGNO (toval);
233 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
234 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
235 memcpy (virtual_buffer, VALUE_CONTENTS (fromval),
236 REGISTER_VIRTUAL_SIZE (regno));
237 target_convert_from_virtual (regno, virtual_buffer, raw_buffer);
238 use_buffer = REGISTER_RAW_SIZE (regno);
241 switch (VALUE_LVAL (toval))
243 case lval_internalvar:
244 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
247 case lval_internalvar_component:
248 set_internalvar_component (VALUE_INTERNALVAR (toval),
249 VALUE_OFFSET (toval),
250 VALUE_BITPOS (toval),
251 VALUE_BITSIZE (toval),
256 if (VALUE_BITSIZE (toval))
258 int v; /* FIXME, this won't work for large bitfields */
259 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
260 (char *) &v, sizeof v);
261 modify_field ((char *) &v, (int) value_as_long (fromval),
262 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
263 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
264 (char *)&v, sizeof v);
267 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
268 raw_buffer, use_buffer);
270 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
271 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
275 if (VALUE_BITSIZE (toval))
279 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
280 (char *) &v, sizeof v);
281 modify_field ((char *) &v, (int) value_as_long (fromval),
282 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
283 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
284 (char *) &v, sizeof v);
287 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
288 raw_buffer, use_buffer);
290 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
291 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
294 case lval_reg_frame_relative:
296 /* value is stored in a series of registers in the frame
297 specified by the structure. Copy that value out, modify
298 it, and copy it back in. */
299 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
300 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
301 int byte_offset = VALUE_OFFSET (toval) % reg_size;
302 int reg_offset = VALUE_OFFSET (toval) / reg_size;
304 char *buffer = (char *) alloca (amount_to_copy);
308 /* Figure out which frame this is in currently. */
309 for (frame = get_current_frame ();
310 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
311 frame = get_prev_frame (frame))
315 error ("Value being assigned to is no longer active.");
317 amount_to_copy += (reg_size - amount_to_copy % reg_size);
320 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
322 amount_copied < amount_to_copy;
323 amount_copied += reg_size, regno++)
325 get_saved_register (buffer + amount_copied,
326 (int *)NULL, (CORE_ADDR *)NULL,
327 frame, regno, (enum lval_type *)NULL);
330 /* Modify what needs to be modified. */
331 if (VALUE_BITSIZE (toval))
332 modify_field (buffer + byte_offset,
333 (int) value_as_long (fromval),
334 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
336 (void) memcpy (buffer + byte_offset, raw_buffer, use_buffer);
338 (void) memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
342 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
344 amount_copied < amount_to_copy;
345 amount_copied += reg_size, regno++)
351 /* Just find out where to put it. */
352 get_saved_register ((char *)NULL,
353 &optim, &addr, frame, regno, &lval);
356 error ("Attempt to assign to a value that was optimized out.");
357 if (lval == lval_memory)
358 write_memory (addr, buffer + amount_copied, reg_size);
359 else if (lval == lval_register)
360 write_register_bytes (addr, buffer + amount_copied, reg_size);
362 error ("Attempt to assign to an unmodifiable value.");
369 error ("Left side of = operation is not an lvalue.");
372 /* Return a value just like TOVAL except with the contents of FROMVAL
373 (except in the case of the type if TOVAL is an internalvar). */
375 if (VALUE_LVAL (toval) == lval_internalvar
376 || VALUE_LVAL (toval) == lval_internalvar_component)
378 type = VALUE_TYPE (fromval);
381 val = allocate_value (type);
382 (void) memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
383 (void) memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
385 VALUE_TYPE (val) = type;
390 /* Extend a value VAL to COUNT repetitions of its type. */
393 value_repeat (arg1, count)
399 if (VALUE_LVAL (arg1) != lval_memory)
400 error ("Only values in memory can be extended with '@'.");
402 error ("Invalid number %d of repetitions.", count);
404 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
406 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
407 VALUE_CONTENTS_RAW (val),
408 TYPE_LENGTH (VALUE_TYPE (val)) * count);
409 VALUE_LVAL (val) = lval_memory;
410 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
416 value_of_variable (var)
421 val = read_var_value (var, (FRAME) 0);
423 error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var));
427 /* Given a value which is an array, return a value which is
428 a pointer to its first (actually, zeroth) element.
429 FIXME, this should be subtracting the array's lower bound. */
432 value_coerce_array (arg1)
435 register struct type *type;
437 if (VALUE_LVAL (arg1) != lval_memory)
438 error ("Attempt to take address of value not located in memory.");
440 /* Get type of elements. */
441 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
442 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
444 /* A phony array made by value_repeat.
445 Its type is the type of the elements, not an array type. */
446 type = VALUE_TYPE (arg1);
448 return value_from_longest (lookup_pointer_type (type),
449 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
452 /* Given a value which is a function, return a value which is a pointer
456 value_coerce_function (arg1)
460 if (VALUE_LVAL (arg1) != lval_memory)
461 error ("Attempt to take address of value not located in memory.");
463 return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
464 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
467 /* Return a pointer value for the object for which ARG1 is the contents. */
473 struct type *type = VALUE_TYPE (arg1);
474 if (TYPE_CODE (type) == TYPE_CODE_REF)
476 /* Copy the value, but change the type from (T&) to (T*).
477 We keep the same location information, which is efficient,
478 and allows &(&X) to get the location containing the reference. */
479 value arg2 = value_copy (arg1);
480 VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
483 if (VALUE_REPEATED (arg1)
484 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
485 return value_coerce_array (arg1);
486 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
487 return value_coerce_function (arg1);
489 if (VALUE_LVAL (arg1) != lval_memory)
490 error ("Attempt to take address of value not located in memory.");
492 return value_from_longest (lookup_pointer_type (type),
493 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
496 /* Given a value of a pointer type, apply the C unary * operator to it. */
504 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
505 error ("not implemented: member types in value_ind");
507 /* Allow * on an integer so we can cast it to whatever we want.
508 This returns an int, which seems like the most C-like thing
509 to do. "long long" variables are rare enough that
510 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
511 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
512 return value_at (builtin_type_int,
513 (CORE_ADDR) value_as_long (arg1));
514 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
515 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
516 value_as_pointer (arg1));
517 error ("Attempt to take contents of a non-pointer value.");
518 return 0; /* For lint -- never reached */
521 /* Pushing small parts of stack frames. */
523 /* Push one word (the size of object that a register holds). */
526 push_word (sp, buffer)
528 REGISTER_TYPE buffer;
530 register int len = sizeof (REGISTER_TYPE);
532 SWAP_TARGET_AND_HOST (&buffer, len);
535 write_memory (sp, (char *)&buffer, len);
536 #else /* stack grows upward */
537 write_memory (sp, (char *)&buffer, len);
539 #endif /* stack grows upward */
544 /* Push LEN bytes with data at BUFFER. */
547 push_bytes (sp, buffer, len)
554 write_memory (sp, buffer, len);
555 #else /* stack grows upward */
556 write_memory (sp, buffer, len);
558 #endif /* stack grows upward */
563 /* Push onto the stack the specified value VALUE. */
567 register CORE_ADDR sp;
570 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
574 write_memory (sp, VALUE_CONTENTS (arg), len);
575 #else /* stack grows upward */
576 write_memory (sp, VALUE_CONTENTS (arg), len);
578 #endif /* stack grows upward */
583 /* Perform the standard coercions that are specified
584 for arguments to be passed to C functions. */
587 value_arg_coerce (arg)
590 register struct type *type;
594 type = VALUE_TYPE (arg);
596 if (TYPE_CODE (type) == TYPE_CODE_INT
597 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
598 return value_cast (builtin_type_int, arg);
600 if (TYPE_CODE (type) == TYPE_CODE_FLT
601 && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
602 return value_cast (builtin_type_double, arg);
607 /* Push the value ARG, first coercing it as an argument
611 value_arg_push (sp, arg)
612 register CORE_ADDR sp;
615 return value_push (sp, value_arg_coerce (arg));
618 /* Determine a function's address and its return type from its value.
619 Calls error() if the function is not valid for calling. */
622 find_function_addr (function, retval_type)
624 struct type **retval_type;
626 register struct type *ftype = VALUE_TYPE (function);
627 register enum type_code code = TYPE_CODE (ftype);
628 struct type *value_type;
631 /* If it's a member function, just look at the function
634 /* Determine address to call. */
635 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
637 funaddr = VALUE_ADDRESS (function);
638 value_type = TYPE_TARGET_TYPE (ftype);
640 else if (code == TYPE_CODE_PTR)
642 funaddr = value_as_pointer (function);
643 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
644 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
645 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
647 value_type = builtin_type_int;
649 else if (code == TYPE_CODE_INT)
651 /* Handle the case of functions lacking debugging info.
652 Their values are characters since their addresses are char */
653 if (TYPE_LENGTH (ftype) == 1)
654 funaddr = value_as_pointer (value_addr (function));
656 /* Handle integer used as address of a function. */
657 funaddr = (CORE_ADDR) value_as_long (function);
659 value_type = builtin_type_int;
662 error ("Invalid data type for function to be called.");
664 *retval_type = value_type;
668 #if defined (CALL_DUMMY)
669 /* All this stuff with a dummy frame may seem unnecessarily complicated
670 (why not just save registers in GDB?). The purpose of pushing a dummy
671 frame which looks just like a real frame is so that if you call a
672 function and then hit a breakpoint (get a signal, etc), "backtrace"
673 will look right. Whether the backtrace needs to actually show the
674 stack at the time the inferior function was called is debatable, but
675 it certainly needs to not display garbage. So if you are contemplating
676 making dummy frames be different from normal frames, consider that. */
678 /* Perform a function call in the inferior.
679 ARGS is a vector of values of arguments (NARGS of them).
680 FUNCTION is a value, the function to be called.
681 Returns a value representing what the function returned.
682 May fail to return, if a breakpoint or signal is hit
683 during the execution of the function. */
686 call_function_by_hand (function, nargs, args)
691 register CORE_ADDR sp;
694 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
695 is in host byte order. It is switched to target byte order before calling
697 static REGISTER_TYPE dummy[] = CALL_DUMMY;
698 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
700 struct type *value_type;
701 unsigned char struct_return;
702 CORE_ADDR struct_addr;
703 struct inferior_status inf_status;
704 struct cleanup *old_chain;
708 if (!target_has_execution)
711 save_inferior_status (&inf_status, 1);
712 old_chain = make_cleanup (restore_inferior_status, &inf_status);
714 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
715 (and POP_FRAME for restoring them). (At least on most machines)
716 they are saved on the stack in the inferior. */
719 old_sp = sp = read_register (SP_REGNUM);
721 #if 1 INNER_THAN 2 /* Stack grows down */
724 #else /* Stack grows up */
729 funaddr = find_function_addr (function, &value_type);
732 struct block *b = block_for_pc (funaddr);
733 /* If compiled without -g, assume GCC. */
734 using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
737 /* Are we returning a value using a structure return or a normal
740 struct_return = using_struct_return (function, funaddr, value_type,
743 /* Create a call sequence customized for this function
744 and the number of arguments for it. */
745 (void) memcpy (dummy1, dummy, sizeof dummy);
746 for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
747 SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE));
748 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
749 value_type, using_gcc);
751 #if CALL_DUMMY_LOCATION == ON_STACK
752 write_memory (start_sp, (char *)dummy1, sizeof dummy);
754 #else /* Not on stack. */
755 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
756 /* Convex Unix prohibits executing in the stack segment. */
757 /* Hope there is empty room at the top of the text segment. */
759 extern CORE_ADDR text_end;
762 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
763 if (read_memory_integer (start_sp, 1) != 0)
764 error ("text segment full -- no place to put call");
767 start_sp = text_end - sizeof dummy;
768 write_memory (start_sp, (char *)dummy1, sizeof dummy);
770 #else /* After text_end. */
772 extern CORE_ADDR text_end;
776 errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy);
778 error ("Cannot write text segment -- call_function failed");
780 #endif /* After text_end. */
781 #endif /* Not on stack. */
784 sp = old_sp; /* It really is used, for some ifdef's... */
788 /* If stack grows down, we must leave a hole at the top. */
792 /* Reserve space for the return structure to be written on the
793 stack, if necessary */
796 len += TYPE_LENGTH (value_type);
798 for (i = nargs - 1; i >= 0; i--)
799 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
800 #ifdef CALL_DUMMY_STACK_ADJUST
801 len += CALL_DUMMY_STACK_ADJUST;
804 sp -= STACK_ALIGN (len) - len;
806 sp += STACK_ALIGN (len) - len;
809 #endif /* STACK_ALIGN */
811 /* Reserve space for the return structure to be written on the
812 stack, if necessary */
817 sp -= TYPE_LENGTH (value_type);
821 sp += TYPE_LENGTH (value_type);
825 #if defined (REG_STRUCT_HAS_ADDR)
827 /* This is a machine like the sparc, where we need to pass a pointer
828 to the structure, not the structure itself. */
829 if (REG_STRUCT_HAS_ADDR (using_gcc))
830 for (i = nargs - 1; i >= 0; i--)
831 if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
834 #if !(1 INNER_THAN 2)
835 /* The stack grows up, so the address of the thing we push
836 is the stack pointer before we push it. */
839 /* Push the structure. */
840 sp = value_push (sp, args[i]);
842 /* The stack grows down, so the address of the thing we push
843 is the stack pointer after we push it. */
846 /* The value we're going to pass is the address of the thing
848 args[i] = value_from_longest (lookup_pointer_type (value_type),
852 #endif /* REG_STRUCT_HAS_ADDR. */
854 #ifdef PUSH_ARGUMENTS
855 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
856 #else /* !PUSH_ARGUMENTS */
857 for (i = nargs - 1; i >= 0; i--)
858 sp = value_arg_push (sp, args[i]);
859 #endif /* !PUSH_ARGUMENTS */
861 #ifdef CALL_DUMMY_STACK_ADJUST
863 sp -= CALL_DUMMY_STACK_ADJUST;
865 sp += CALL_DUMMY_STACK_ADJUST;
867 #endif /* CALL_DUMMY_STACK_ADJUST */
869 /* Store the address at which the structure is supposed to be
870 written. Note that this (and the code which reserved the space
871 above) assumes that gcc was used to compile this function. Since
872 it doesn't cost us anything but space and if the function is pcc
873 it will ignore this value, we will make that assumption.
875 Also note that on some machines (like the sparc) pcc uses a
876 convention like gcc's. */
879 STORE_STRUCT_RETURN (struct_addr, sp);
881 /* Write the stack pointer. This is here because the statements above
882 might fool with it. On SPARC, this write also stores the register
883 window into the right place in the new stack frame, which otherwise
884 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
885 write_register (SP_REGNUM, sp);
887 /* Figure out the value returned by the function. */
889 char retbuf[REGISTER_BYTES];
891 /* Execute the stack dummy routine, calling FUNCTION.
892 When it is done, discard the empty frame
893 after storing the contents of all regs into retbuf. */
894 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
896 do_cleanups (old_chain);
898 return value_being_returned (value_type, retbuf, struct_return);
901 #else /* no CALL_DUMMY. */
903 call_function_by_hand (function, nargs, args)
908 error ("Cannot invoke functions on this machine.");
910 #endif /* no CALL_DUMMY. */
912 /* Create a value for a string constant:
913 Call the function malloc in the inferior to get space for it,
914 then copy the data into that space
915 and then return the address with type char *.
916 PTR points to the string constant data; LEN is number of characters. */
919 value_string (ptr, len)
924 register struct symbol *sym;
926 register char *copy = (char *) alloca (len + 1);
928 register char *o = copy, *ibeg = ptr;
931 /* Copy the string into COPY, processing escapes.
932 We could not conveniently process them in the parser
933 because the string there wants to be a substring of the input. */
935 while (i - ibeg < len)
940 c = parse_escape (&i);
948 /* Get the length of the string after escapes are processed. */
952 /* Find the address of malloc in the inferior. */
954 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
957 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
958 error ("\"malloc\" exists in this program but is not a function.");
959 val = value_of_variable (sym);
963 struct minimal_symbol *msymbol;
964 msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL);
967 value_from_longest (lookup_pointer_type (lookup_function_type (
968 lookup_pointer_type (builtin_type_char))),
969 (LONGEST) msymbol -> address);
971 error ("String constants require the program to have a function \"malloc\".");
974 blocklen = value_from_longest (builtin_type_int, (LONGEST) (len + 1));
975 val = call_function_by_hand (val, 1, &blocklen);
976 if (value_zerop (val))
977 error ("No memory available for string constant.");
978 write_memory (value_as_pointer (val), copy, len + 1);
979 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
983 /* Helper function used by value_struct_elt to recurse through baseclasses.
984 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
985 and search in it assuming it has (class) type TYPE.
986 If found, return value, else return NULL.
988 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
989 look for a baseclass named NAME. */
992 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
996 register struct type *type;
997 int looking_for_baseclass;
1001 check_stub_type (type);
1003 if (! looking_for_baseclass)
1004 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1006 char *t_field_name = TYPE_FIELD_NAME (type, i);
1008 if (t_field_name && !strcmp (t_field_name, name))
1011 if (TYPE_FIELD_STATIC (type, i))
1013 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
1014 struct symbol *sym =
1015 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1017 "Internal error: could not find physical static variable named %s",
1019 v = value_at (TYPE_FIELD_TYPE (type, i),
1020 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1023 v = value_primitive_field (arg1, offset, i, type);
1025 error("there is no field named %s", name);
1030 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1033 /* If we are looking for baseclasses, this is what we get when we
1035 int found_baseclass = (looking_for_baseclass
1036 && !strcmp (name, TYPE_BASECLASS_NAME (type, i)));
1038 if (BASETYPE_VIA_VIRTUAL (type, i))
1041 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1044 error ("virtual baseclass botch");
1045 if (found_baseclass)
1047 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1048 looking_for_baseclass);
1050 else if (found_baseclass)
1051 v = value_primitive_field (arg1, offset, i, type);
1053 v = search_struct_field (name, arg1,
1054 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1055 TYPE_BASECLASS (type, i),
1056 looking_for_baseclass);
1062 /* Helper function used by value_struct_elt to recurse through baseclasses.
1063 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1064 and search in it assuming it has (class) type TYPE.
1065 If found, return value, else return NULL. */
1068 search_struct_method (name, arg1, args, offset, static_memfuncp, type)
1070 register value arg1, *args;
1071 int offset, *static_memfuncp;
1072 register struct type *type;
1076 check_stub_type (type);
1077 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1079 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1080 if (t_field_name && !strcmp (t_field_name, name))
1082 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1083 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1085 if (j > 0 && args == 0)
1086 error ("cannot resolve overloaded method `%s'", name);
1089 if (TYPE_FN_FIELD_STUB (f, j))
1090 check_stub_method (type, i, j);
1091 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1092 TYPE_FN_FIELD_ARGS (f, j), args))
1094 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1095 return (value)value_virtual_fn_field (arg1, f, j, type);
1096 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1097 *static_memfuncp = 1;
1098 return (value)value_fn_field (f, j);
1105 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1110 if (BASETYPE_VIA_VIRTUAL (type, i))
1112 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
1115 error ("virtual baseclass botch");
1121 base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
1123 v = search_struct_method (name, v2, args, base_offset,
1124 static_memfuncp, TYPE_BASECLASS (type, i));
1130 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1131 extract the component named NAME from the ultimate target structure/union
1132 and return it as a value with its appropriate type.
1133 ERR is used in the error message if *ARGP's type is wrong.
1135 C++: ARGS is a list of argument types to aid in the selection of
1136 an appropriate method. Also, handle derived types.
1138 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1139 where the truthvalue of whether the function that was resolved was
1140 a static member function or not is stored.
1142 ERR is an error message to be printed in case the field is not found. */
1145 value_struct_elt (argp, args, name, static_memfuncp, err)
1146 register value *argp, *args;
1148 int *static_memfuncp;
1151 register struct type *t;
1154 COERCE_ARRAY (*argp);
1156 t = VALUE_TYPE (*argp);
1158 /* Follow pointers until we get to a non-pointer. */
1160 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1162 *argp = value_ind (*argp);
1163 /* Don't coerce fn pointer to fn and then back again! */
1164 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1165 COERCE_ARRAY (*argp);
1166 t = VALUE_TYPE (*argp);
1169 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1170 error ("not implemented: member type in value_struct_elt");
1172 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1173 && TYPE_CODE (t) != TYPE_CODE_UNION)
1174 error ("Attempt to extract a component of a value that is not a %s.", err);
1176 /* Assume it's not, unless we see that it is. */
1177 if (static_memfuncp)
1178 *static_memfuncp =0;
1182 /* if there are no arguments ...do this... */
1184 /* Try as a field first, because if we succeed, there
1185 is less work to be done. */
1186 v = search_struct_field (name, *argp, 0, t, 0);
1190 /* C++: If it was not found as a data field, then try to
1191 return it as a pointer to a method. */
1193 if (destructor_name_p (name, t))
1194 error ("Cannot get value of destructor");
1196 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1200 if (TYPE_NFN_FIELDS (t))
1201 error ("There is no member or method named %s.", name);
1203 error ("There is no member named %s.", name);
1208 if (destructor_name_p (name, t))
1212 /* destructors are a special case. */
1213 return (value)value_fn_field (TYPE_FN_FIELDLIST1 (t, 0),
1214 TYPE_FN_FIELDLIST_LENGTH (t, 0));
1218 error ("destructor should not have any argument");
1222 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1226 /* See if user tried to invoke data as function. If so,
1227 hand it back. If it's not callable (i.e., a pointer to function),
1228 gdb should give an error. */
1229 v = search_struct_field (name, *argp, 0, t, 0);
1233 error ("Structure has no component named %s.", name);
1237 /* C++: return 1 is NAME is a legitimate name for the destructor
1238 of type TYPE. If TYPE does not have a destructor, or
1239 if NAME is inappropriate for TYPE, an error is signaled. */
1241 destructor_name_p (name, type)
1243 const struct type *type;
1245 /* destructors are a special case. */
1249 char *dname = type_name_no_tag (type);
1250 if (strcmp (dname, name+1))
1251 error ("name of destructor must equal name of class");
1258 /* Helper function for check_field: Given TYPE, a structure/union,
1259 return 1 if the component named NAME from the ultimate
1260 target structure/union is defined, otherwise, return 0. */
1263 check_field_in (type, name)
1264 register struct type *type;
1269 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1271 char *t_field_name = TYPE_FIELD_NAME (type, i);
1272 if (t_field_name && !strcmp (t_field_name, name))
1276 /* C++: If it was not found as a data field, then try to
1277 return it as a pointer to a method. */
1279 /* Destructors are a special case. */
1280 if (destructor_name_p (name, type))
1283 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1285 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name))
1289 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1290 if (check_field_in (TYPE_BASECLASS (type, i), name))
1297 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1298 return 1 if the component named NAME from the ultimate
1299 target structure/union is defined, otherwise, return 0. */
1302 check_field (arg1, name)
1303 register value arg1;
1306 register struct type *t;
1308 COERCE_ARRAY (arg1);
1310 t = VALUE_TYPE (arg1);
1312 /* Follow pointers until we get to a non-pointer. */
1314 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1315 t = TYPE_TARGET_TYPE (t);
1317 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1318 error ("not implemented: member type in check_field");
1320 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1321 && TYPE_CODE (t) != TYPE_CODE_UNION)
1322 error ("Internal error: `this' is not an aggregate");
1324 return check_field_in (t, name);
1327 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
1328 return the address of this member as a "pointer to member"
1329 type. If INTYPE is non-null, then it will be the type
1330 of the member we are looking for. This will help us resolve
1331 "pointers to member functions". This function is used
1332 to resolve user expressions of the form "DOMAIN::NAME". */
1335 value_struct_elt_for_reference (domain, offset, curtype, name, intype)
1336 struct type *domain, *curtype, *intype;
1340 register struct type *t = curtype;
1344 if ( TYPE_CODE (t) != TYPE_CODE_STRUCT
1345 && TYPE_CODE (t) != TYPE_CODE_UNION)
1346 error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
1348 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1350 char *t_field_name = TYPE_FIELD_NAME (t, i);
1352 if (t_field_name && !strcmp (t_field_name, name))
1354 if (TYPE_FIELD_STATIC (t, i))
1356 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1357 struct symbol *sym =
1358 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1361 "Internal error: could not find physical static variable named %s",
1363 return value_at (SYMBOL_TYPE (sym),
1364 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1366 if (TYPE_FIELD_PACKED (t, i))
1367 error ("pointers to bitfield members not allowed");
1369 return value_from_longest
1370 (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
1372 offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1376 /* C++: If it was not found as a data field, then try to
1377 return it as a pointer to a method. */
1379 /* Destructors are a special case. */
1380 if (destructor_name_p (name, t))
1382 error ("member pointers to destructors not implemented yet");
1385 /* Perform all necessary dereferencing. */
1386 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1387 intype = TYPE_TARGET_TYPE (intype);
1389 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1391 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1393 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1394 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1396 if (intype == 0 && j > 1)
1397 error ("non-unique member `%s' requires type instantiation", name);
1401 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1404 error ("no member function matches that type instantiation");
1409 if (TYPE_FN_FIELD_STUB (f, j))
1410 check_stub_method (t, i, j);
1411 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1413 return value_from_longest
1414 (lookup_reference_type
1415 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1417 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1421 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1422 0, VAR_NAMESPACE, 0, NULL);
1423 v = read_var_value (s, 0);
1425 VALUE_TYPE (v) = lookup_reference_type
1426 (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
1434 for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
1439 if (BASETYPE_VIA_VIRTUAL (t, i))
1442 base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
1443 v = value_struct_elt_for_reference (domain,
1444 offset + base_offset,
1445 TYPE_BASECLASS (t, i),
1454 /* Compare two argument lists and return the position in which they differ,
1457 STATICP is nonzero if the T1 argument list came from a
1458 static member function.
1460 For non-static member functions, we ignore the first argument,
1461 which is the type of the instance variable. This is because we want
1462 to handle calls with objects from derived classes. This is not
1463 entirely correct: we should actually check to make sure that a
1464 requested operation is type secure, shouldn't we? FIXME. */
1467 typecmp (staticp, t1, t2)
1476 if (staticp && t1 == 0)
1480 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1481 if (t1[!staticp] == 0) return 0;
1482 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1485 || t1[i]->code != t2[i]->type->code
1486 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1490 if (!t1[i]) return 0;
1491 return t2[i] ? i+1 : 0;
1494 /* C++: return the value of the class instance variable, if one exists.
1495 Flag COMPLAIN signals an error if the request is made in an
1496 inappropriate context. */
1498 value_of_this (complain)
1501 extern FRAME selected_frame;
1502 struct symbol *func, *sym;
1505 static const char funny_this[] = "this";
1508 if (selected_frame == 0)
1510 error ("no frame selected");
1513 func = get_frame_function (selected_frame);
1517 error ("no `this' in nameless context");
1521 b = SYMBOL_BLOCK_VALUE (func);
1522 i = BLOCK_NSYMS (b);
1525 error ("no args, no `this'");
1528 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1529 symbol instead of the LOC_ARG one (if both exist). */
1530 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1534 error ("current stack frame not in method");
1539 this = read_var_value (sym, selected_frame);
1540 if (this == 0 && complain)
1541 error ("`this' argument at unknown address");