1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 /* Local functions. */
33 static value search_struct_field ();
35 /* Cast value ARG2 to type TYPE and return as a value.
36 More general than a C cast: accepts any two types of the same length,
37 and if ARG2 is an lvalue it can be cast into anything at all. */
38 /* In C++, casts may change pointer representations. */
41 value_cast (type, arg2)
45 register enum type_code code1;
46 register enum type_code code2;
49 /* Coerce arrays but not enums. Enums will work as-is
50 and coercing them would cause an infinite recursion. */
51 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
54 code1 = TYPE_CODE (type);
55 code2 = TYPE_CODE (VALUE_TYPE (arg2));
56 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
57 || code2 == TYPE_CODE_ENUM);
59 if (code1 == TYPE_CODE_FLT && scalar)
60 return value_from_double (type, value_as_double (arg2));
61 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
62 && (scalar || code2 == TYPE_CODE_PTR))
63 return value_from_long (type, value_as_long (arg2));
64 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
66 if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
68 /* Look in the type of the source to see if it contains the
69 type of the target as a superclass. If so, we'll need to
70 offset the pointer rather than just change its type. */
71 struct type *t1 = TYPE_TARGET_TYPE (type);
72 struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
73 if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
74 && TYPE_CODE (t2) == TYPE_CODE_STRUCT
75 && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
77 value v = search_struct_field (type_name_no_tag (t1),
78 value_ind (arg2), 0, t2, 1);
82 VALUE_TYPE (v) = type;
86 /* No superclass found, just fall through to change ptr type. */
88 VALUE_TYPE (arg2) = type;
91 else if (VALUE_LVAL (arg2) == lval_memory)
93 return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
97 error ("Invalid cast.");
102 /* Create a value of type TYPE that is zero, and return it. */
105 value_zero (type, lv)
109 register value val = allocate_value (type);
111 bzero (VALUE_CONTENTS (val), TYPE_LENGTH (type));
112 VALUE_LVAL (val) = lv;
117 /* Return a value with type TYPE located at ADDR.
119 Call value_at only if the data needs to be fetched immediately;
120 if we can be 'lazy' and defer the fetch, perhaps indefinately, call
121 value_at_lazy instead. value_at_lazy simply records the address of
122 the data and sets the lazy-evaluation-required flag. The lazy flag
123 is tested in the VALUE_CONTENTS macro, which is used if and when
124 the contents are actually required. */
127 value_at (type, addr)
131 register value val = allocate_value (type);
133 read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
135 VALUE_LVAL (val) = lval_memory;
136 VALUE_ADDRESS (val) = addr;
141 /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */
144 value_at_lazy (type, addr)
148 register value val = allocate_value (type);
150 VALUE_LVAL (val) = lval_memory;
151 VALUE_ADDRESS (val) = addr;
152 VALUE_LAZY (val) = 1;
157 /* Called only from the VALUE_CONTENTS macro, if the current data for
158 a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the
159 data from the user's process, and clears the lazy flag to indicate
160 that the data in the buffer is valid.
162 This function returns a value because it is used in the VALUE_CONTENTS
163 macro as part of an expression, where a void would not work. The
167 value_fetch_lazy (val)
170 CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
172 read_memory (addr, VALUE_CONTENTS_RAW (val),
173 TYPE_LENGTH (VALUE_TYPE (val)));
174 VALUE_LAZY (val) = 0;
179 /* Store the contents of FROMVAL into the location of TOVAL.
180 Return a new value with the location of TOVAL and contents of FROMVAL. */
183 value_assign (toval, fromval)
184 register value toval, fromval;
186 register struct type *type = VALUE_TYPE (toval);
188 char raw_buffer[MAX_REGISTER_RAW_SIZE];
189 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
192 COERCE_ARRAY (fromval);
194 if (VALUE_LVAL (toval) != lval_internalvar)
195 fromval = value_cast (type, fromval);
197 /* If TOVAL is a special machine register requiring conversion
198 of program values to a special raw format,
199 convert FROMVAL's contents now, with result in `raw_buffer',
200 and set USE_BUFFER to the number of bytes to write. */
202 if (VALUE_REGNO (toval) >= 0
203 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
205 int regno = VALUE_REGNO (toval);
206 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
207 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
208 bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
209 REGISTER_VIRTUAL_SIZE (regno));
210 target_convert_from_virtual (regno, virtual_buffer, raw_buffer);
211 use_buffer = REGISTER_RAW_SIZE (regno);
214 switch (VALUE_LVAL (toval))
216 case lval_internalvar:
217 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
220 case lval_internalvar_component:
221 set_internalvar_component (VALUE_INTERNALVAR (toval),
222 VALUE_OFFSET (toval),
223 VALUE_BITPOS (toval),
224 VALUE_BITSIZE (toval),
229 if (VALUE_BITSIZE (toval))
231 int v; /* FIXME, this won't work for large bitfields */
232 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
234 modify_field (&v, (int) value_as_long (fromval),
235 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
236 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
237 (char *)&v, sizeof v);
240 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
241 raw_buffer, use_buffer);
243 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
244 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
248 if (VALUE_BITSIZE (toval))
252 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
254 modify_field (&v, (int) value_as_long (fromval),
255 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
256 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
260 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
261 raw_buffer, use_buffer);
263 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
264 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
267 case lval_reg_frame_relative:
269 /* value is stored in a series of registers in the frame
270 specified by the structure. Copy that value out, modify
271 it, and copy it back in. */
272 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
273 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
274 int byte_offset = VALUE_OFFSET (toval) % reg_size;
275 int reg_offset = VALUE_OFFSET (toval) / reg_size;
277 char *buffer = (char *) alloca (amount_to_copy);
281 /* Figure out which frame this is in currently. */
282 for (frame = get_current_frame ();
283 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
284 frame = get_prev_frame (frame))
288 error ("Value being assigned to is no longer active.");
290 amount_to_copy += (reg_size - amount_to_copy % reg_size);
293 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
295 amount_copied < amount_to_copy;
296 amount_copied += reg_size, regno++)
298 get_saved_register (buffer + amount_copied,
299 (int *)NULL, (CORE_ADDR)NULL,
300 frame, regno, (enum lval_type *)NULL);
303 /* Modify what needs to be modified. */
304 if (VALUE_BITSIZE (toval))
305 modify_field (buffer + byte_offset,
306 (int) value_as_long (fromval),
307 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
309 bcopy (raw_buffer, buffer + byte_offset, use_buffer);
311 bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset,
315 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
317 amount_copied < amount_to_copy;
318 amount_copied += reg_size, regno++)
324 /* Just find out where to put it. */
325 get_saved_register ((char *)NULL,
326 &optim, &addr, frame, regno, &lval);
329 error ("Attempt to assign to a value that was optimized out.");
330 if (lval == lval_memory)
331 write_memory (addr, buffer + amount_copied, reg_size);
332 else if (lval == lval_register)
333 write_register_bytes (addr, buffer + amount_copied, reg_size);
335 error ("Attempt to assign to an unmodifiable value.");
342 error ("Left side of = operation is not an lvalue.");
345 /* Return a value just like TOVAL except with the contents of FROMVAL
346 (except in the case of the type if TOVAL is an internalvar). */
348 if (VALUE_LVAL (toval) == lval_internalvar
349 || VALUE_LVAL (toval) == lval_internalvar_component)
351 type = VALUE_TYPE (fromval);
354 val = allocate_value (type);
355 bcopy (toval, val, VALUE_CONTENTS_RAW (val) - (char *) val);
356 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
357 VALUE_TYPE (val) = type;
362 /* Extend a value VAL to COUNT repetitions of its type. */
365 value_repeat (arg1, count)
371 if (VALUE_LVAL (arg1) != lval_memory)
372 error ("Only values in memory can be extended with '@'.");
374 error ("Invalid number %d of repetitions.", count);
376 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
378 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
379 VALUE_CONTENTS_RAW (val),
380 TYPE_LENGTH (VALUE_TYPE (val)) * count);
381 VALUE_LVAL (val) = lval_memory;
382 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
388 value_of_variable (var)
393 val = read_var_value (var, (FRAME) 0);
395 error ("Address of symbol \"%s\" is unknown.", SYMBOL_NAME (var));
399 /* Given a value which is an array, return a value which is
400 a pointer to its first element. */
403 value_coerce_array (arg1)
406 register struct type *type;
409 if (VALUE_LVAL (arg1) != lval_memory)
410 error ("Attempt to take address of value not located in memory.");
412 /* Get type of elements. */
413 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
414 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
416 /* A phony array made by value_repeat.
417 Its type is the type of the elements, not an array type. */
418 type = VALUE_TYPE (arg1);
420 /* Get the type of the result. */
421 type = lookup_pointer_type (type);
422 val = value_from_long (builtin_type_long,
423 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
424 VALUE_TYPE (val) = type;
428 /* Given a value which is a function, return a value which is a pointer
432 value_coerce_function (arg1)
435 register struct type *type;
438 if (VALUE_LVAL (arg1) != lval_memory)
439 error ("Attempt to take address of value not located in memory.");
441 /* Get the type of the result. */
442 type = lookup_pointer_type (VALUE_TYPE (arg1));
443 val = value_from_long (builtin_type_long,
444 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
445 VALUE_TYPE (val) = type;
449 /* Return a pointer value for the object for which ARG1 is the contents. */
455 register struct type *type;
459 /* Taking the address of an array is really a no-op
460 once the array is coerced to a pointer to its first element. */
461 if (VALUE_REPEATED (arg1)
462 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
463 return value_coerce_array (arg1);
464 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_FUNC)
465 return value_coerce_function (arg1);
467 if (VALUE_LVAL (arg1) != lval_memory)
468 error ("Attempt to take address of value not located in memory.");
470 /* Get the type of the result. */
471 type = lookup_pointer_type (VALUE_TYPE (arg1));
472 val = value_from_long (builtin_type_long,
473 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
474 VALUE_TYPE (val) = type;
478 /* Given a value of a pointer type, apply the C unary * operator to it. */
486 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
487 error ("not implemented: member types in value_ind");
489 /* Allow * on an integer so we can cast it to whatever we want.
490 This returns an int, which seems like the most C-like thing
491 to do. "long long" variables are rare enough that
492 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
493 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
494 return value_at (builtin_type_int,
495 (CORE_ADDR) value_as_long (arg1));
496 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
497 return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
498 (CORE_ADDR) value_as_long (arg1));
499 error ("Attempt to take contents of a non-pointer value.");
500 return 0; /* For lint -- never reached */
503 /* Pushing small parts of stack frames. */
505 /* Push one word (the size of object that a register holds). */
508 push_word (sp, buffer)
510 REGISTER_TYPE buffer;
512 register int len = sizeof (REGISTER_TYPE);
514 SWAP_TARGET_AND_HOST (&buffer, len);
517 write_memory (sp, (char *)&buffer, len);
518 #else /* stack grows upward */
519 write_memory (sp, (char *)&buffer, len);
521 #endif /* stack grows upward */
526 /* Push LEN bytes with data at BUFFER. */
529 push_bytes (sp, buffer, len)
536 write_memory (sp, buffer, len);
537 #else /* stack grows upward */
538 write_memory (sp, buffer, len);
540 #endif /* stack grows upward */
545 /* Push onto the stack the specified value VALUE. */
549 register CORE_ADDR sp;
552 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
556 write_memory (sp, VALUE_CONTENTS (arg), len);
557 #else /* stack grows upward */
558 write_memory (sp, VALUE_CONTENTS (arg), len);
560 #endif /* stack grows upward */
565 /* Perform the standard coercions that are specified
566 for arguments to be passed to C functions. */
569 value_arg_coerce (arg)
572 register struct type *type;
576 type = VALUE_TYPE (arg);
578 if (TYPE_CODE (type) == TYPE_CODE_INT
579 && TYPE_LENGTH (type) < sizeof (int))
580 return value_cast (builtin_type_int, arg);
582 if (type == builtin_type_float)
583 return value_cast (builtin_type_double, arg);
588 /* Push the value ARG, first coercing it as an argument
592 value_arg_push (sp, arg)
593 register CORE_ADDR sp;
596 return value_push (sp, value_arg_coerce (arg));
599 /* Determine a function's address and its return type from its value.
600 Calls error() if the function is not valid for calling. */
603 find_function_addr (function, retval_type)
605 struct type **retval_type;
607 register struct type *ftype = VALUE_TYPE (function);
608 register enum type_code code = TYPE_CODE (ftype);
609 struct type *value_type;
612 /* If it's a member function, just look at the function
615 /* Determine address to call. */
616 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
618 funaddr = VALUE_ADDRESS (function);
619 value_type = TYPE_TARGET_TYPE (ftype);
621 else if (code == TYPE_CODE_PTR)
623 funaddr = value_as_long (function);
624 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
625 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
626 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
628 value_type = builtin_type_int;
630 else if (code == TYPE_CODE_INT)
632 /* Handle the case of functions lacking debugging info.
633 Their values are characters since their addresses are char */
634 if (TYPE_LENGTH (ftype) == 1)
635 funaddr = value_as_long (value_addr (function));
637 /* Handle integer used as address of a function. */
638 funaddr = value_as_long (function);
640 value_type = builtin_type_int;
643 error ("Invalid data type for function to be called.");
645 *retval_type = value_type;
649 #if defined (CALL_DUMMY)
650 /* All this stuff with a dummy frame may seem unnecessarily complicated
651 (why not just save registers in GDB?). The purpose of pushing a dummy
652 frame which looks just like a real frame is so that if you call a
653 function and then hit a breakpoint (get a signal, etc), "backtrace"
654 will look right. Whether the backtrace needs to actually show the
655 stack at the time the inferior function was called is debatable, but
656 it certainly needs to not display garbage. So if you are contemplating
657 making dummy frames be different from normal frames, consider that. */
659 /* Perform a function call in the inferior.
660 ARGS is a vector of values of arguments (NARGS of them).
661 FUNCTION is a value, the function to be called.
662 Returns a value representing what the function returned.
663 May fail to return, if a breakpoint or signal is hit
664 during the execution of the function. */
667 call_function_by_hand (function, nargs, args)
672 register CORE_ADDR sp;
675 /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
676 in in host byte order. It is switched to target byte order before calling
678 static REGISTER_TYPE dummy[] = CALL_DUMMY;
679 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
681 struct type *value_type;
682 unsigned char struct_return;
683 CORE_ADDR struct_addr;
684 struct inferior_status inf_status;
685 struct cleanup *old_chain;
689 save_inferior_status (&inf_status, 1);
690 old_chain = make_cleanup (restore_inferior_status, &inf_status);
692 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
693 (and POP_FRAME for restoring them). (At least on most machines)
694 they are saved on the stack in the inferior. */
697 old_sp = sp = read_register (SP_REGNUM);
699 #if 1 INNER_THAN 2 /* Stack grows down */
702 #else /* Stack grows up */
707 funaddr = find_function_addr (function, &value_type);
710 struct block *b = block_for_pc (funaddr);
711 /* If compiled without -g, assume GCC. */
712 using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
715 /* Are we returning a value using a structure return or a normal
718 struct_return = using_struct_return (function, funaddr, value_type,
721 /* Create a call sequence customized for this function
722 and the number of arguments for it. */
723 bcopy (dummy, dummy1, sizeof dummy);
724 for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
725 SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE));
726 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
727 value_type, using_gcc);
729 #if CALL_DUMMY_LOCATION == ON_STACK
730 write_memory (start_sp, (char *)dummy1, sizeof dummy);
732 #else /* Not on stack. */
733 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
734 /* Convex Unix prohibits executing in the stack segment. */
735 /* Hope there is empty room at the top of the text segment. */
739 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
740 if (read_memory_integer (start_sp, 1) != 0)
741 error ("text segment full -- no place to put call");
744 start_sp = text_end - sizeof dummy;
745 write_memory (start_sp, (char *)dummy1, sizeof dummy);
747 #else /* After text_end. */
752 errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy);
754 error ("Cannot write text segment -- call_function failed");
756 #endif /* After text_end. */
757 #endif /* Not on stack. */
760 sp = old_sp; /* It really is used, for some ifdef's... */
764 /* If stack grows down, we must leave a hole at the top. */
768 /* Reserve space for the return structure to be written on the
769 stack, if necessary */
772 len += TYPE_LENGTH (value_type);
774 for (i = nargs - 1; i >= 0; i--)
775 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
776 #ifdef CALL_DUMMY_STACK_ADJUST
777 len += CALL_DUMMY_STACK_ADJUST;
780 sp -= STACK_ALIGN (len) - len;
782 sp += STACK_ALIGN (len) - len;
785 #endif /* STACK_ALIGN */
787 /* Reserve space for the return structure to be written on the
788 stack, if necessary */
793 sp -= TYPE_LENGTH (value_type);
797 sp += TYPE_LENGTH (value_type);
801 #if defined (REG_STRUCT_HAS_ADDR)
803 /* This is a machine like the sparc, where we need to pass a pointer
804 to the structure, not the structure itself. */
805 if (REG_STRUCT_HAS_ADDR (using_gcc))
806 for (i = nargs - 1; i >= 0; i--)
807 if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
810 #if !(1 INNER_THAN 2)
811 /* The stack grows up, so the address of the thing we push
812 is the stack pointer before we push it. */
815 /* Push the structure. */
816 sp = value_push (sp, args[i]);
818 /* The stack grows down, so the address of the thing we push
819 is the stack pointer after we push it. */
822 /* The value we're going to pass is the address of the thing
824 args[i] = value_from_long (builtin_type_long, (LONGEST) addr);
827 #endif /* REG_STRUCT_HAS_ADDR. */
829 #ifdef PUSH_ARGUMENTS
830 PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
831 #else /* !PUSH_ARGUMENTS */
832 for (i = nargs - 1; i >= 0; i--)
833 sp = value_arg_push (sp, args[i]);
834 #endif /* !PUSH_ARGUMENTS */
836 #ifdef CALL_DUMMY_STACK_ADJUST
838 sp -= CALL_DUMMY_STACK_ADJUST;
840 sp += CALL_DUMMY_STACK_ADJUST;
842 #endif /* CALL_DUMMY_STACK_ADJUST */
844 /* Store the address at which the structure is supposed to be
845 written. Note that this (and the code which reserved the space
846 above) assumes that gcc was used to compile this function. Since
847 it doesn't cost us anything but space and if the function is pcc
848 it will ignore this value, we will make that assumption.
850 Also note that on some machines (like the sparc) pcc uses a
851 convention like gcc's. */
854 STORE_STRUCT_RETURN (struct_addr, sp);
856 /* Write the stack pointer. This is here because the statements above
857 might fool with it. On SPARC, this write also stores the register
858 window into the right place in the new stack frame, which otherwise
859 wouldn't happen. (See write_inferior_registers in sparc-xdep.c.) */
860 write_register (SP_REGNUM, sp);
862 /* Figure out the value returned by the function. */
864 char retbuf[REGISTER_BYTES];
866 /* Execute the stack dummy routine, calling FUNCTION.
867 When it is done, discard the empty frame
868 after storing the contents of all regs into retbuf. */
869 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
871 do_cleanups (old_chain);
873 return value_being_returned (value_type, retbuf, struct_return);
876 #else /* no CALL_DUMMY. */
878 call_function_by_hand (function, nargs, args)
883 error ("Cannot invoke functions on this machine.");
885 #endif /* no CALL_DUMMY. */
887 /* Create a value for a string constant:
888 Call the function malloc in the inferior to get space for it,
889 then copy the data into that space
890 and then return the address with type char *.
891 PTR points to the string constant data; LEN is number of characters. */
894 value_string (ptr, len)
899 register struct symbol *sym;
901 register char *copy = (char *) alloca (len + 1);
903 register char *o = copy, *ibeg = ptr;
906 /* Copy the string into COPY, processing escapes.
907 We could not conveniently process them in expread
908 because the string there wants to be a substring of the input. */
910 while (i - ibeg < len)
915 c = parse_escape (&i);
923 /* Get the length of the string after escapes are processed. */
927 /* Find the address of malloc in the inferior. */
929 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
932 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
933 error ("\"malloc\" exists in this program but is not a function.");
934 val = value_of_variable (sym);
939 for (j = 0; j < misc_function_count; j++)
940 if (!strcmp (misc_function_vector[j].name, "malloc"))
942 if (j < misc_function_count)
943 val = value_from_long (builtin_type_long,
944 (LONGEST) misc_function_vector[j].address);
946 error ("String constants require the program to have a function \"malloc\".");
949 blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
950 val = target_call_function (val, 1, &blocklen);
951 if (value_zerop (val))
952 error ("No memory available for string constant.");
953 write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
954 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
958 /* Helper function used by value_struct_elt to recurse through baseclasses.
959 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
960 and treat the result as having type TYPE.
961 If found, return value, else return NULL.
963 If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
964 look for a baseclass named NAME. */
967 search_struct_field (name, arg1, offset, type, looking_for_baseclass)
971 register struct type *type;
972 int looking_for_baseclass;
976 check_stub_type (type);
978 if (! looking_for_baseclass)
979 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
981 char *t_field_name = TYPE_FIELD_NAME (type, i);
983 if (t_field_name && !strcmp (t_field_name, name))
985 value v = (TYPE_FIELD_STATIC (type, i)
986 ? value_static_field (type, name, i)
987 : value_primitive_field (arg1, offset, i, type));
989 error("there is no field named %s", name);
994 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
997 /* If we are looking for baseclasses, this is what we get when we
999 int found_baseclass = (looking_for_baseclass
1000 && !strcmp (name, TYPE_BASECLASS_NAME (type, i)));
1002 if (BASETYPE_VIA_VIRTUAL (type, i))
1005 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
1007 error ("virtual baseclass botch");
1008 if (found_baseclass)
1010 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
1011 looking_for_baseclass);
1015 if (found_baseclass)
1016 v = value_primitive_field (arg1, offset, i, type);
1018 v = search_struct_field (name, arg1,
1019 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1020 TYPE_BASECLASS (type, i),
1021 looking_for_baseclass);
1027 /* Helper function used by value_struct_elt to recurse through baseclasses.
1028 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1029 and treat the result as having type TYPE.
1030 If found, return value, else return NULL. */
1033 search_struct_method (name, arg1, args, offset, static_memfuncp, type)
1035 register value arg1, *args;
1036 int offset, *static_memfuncp;
1037 register struct type *type;
1041 check_stub_type (type);
1042 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1044 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1045 if (t_field_name && !strcmp (t_field_name, name))
1047 int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
1048 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1050 if (j > 0 && args == 0)
1051 error ("cannot resolve overloaded method `%s'", name);
1054 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1055 check_stub_method (type, i, j);
1056 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1057 TYPE_FN_FIELD_ARGS (f, j), args))
1059 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1060 return (value)value_virtual_fn_field (arg1, f, j, type);
1061 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1062 *static_memfuncp = 1;
1063 return (value)value_fn_field (arg1, i, j);
1070 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1074 if (BASETYPE_VIA_VIRTUAL (type, i))
1077 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
1079 error ("virtual baseclass botch");
1080 v = search_struct_method (name, v2, args, 0,
1081 static_memfuncp, TYPE_BASECLASS (type, i));
1086 v = search_struct_method (name, arg1, args,
1087 TYPE_BASECLASS_BITPOS (type, i) / 8,
1088 static_memfuncp, TYPE_BASECLASS (type, i));
1094 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1095 extract the component named NAME from the ultimate target structure/union
1096 and return it as a value with its appropriate type.
1097 ERR is used in the error message if *ARGP's type is wrong.
1099 C++: ARGS is a list of argument types to aid in the selection of
1100 an appropriate method. Also, handle derived types.
1102 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1103 where the truthvalue of whether the function that was resolved was
1104 a static member function or not is stored.
1106 ERR is an error message to be printed in case the field is not found. */
1109 value_struct_elt (argp, args, name, static_memfuncp, err)
1110 register value *argp, *args;
1112 int *static_memfuncp;
1115 register struct type *t;
1118 COERCE_ARRAY (*argp);
1120 t = VALUE_TYPE (*argp);
1122 /* Follow pointers until we get to a non-pointer. */
1124 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1126 *argp = value_ind (*argp);
1127 /* Don't coerce fn pointer to fn and then back again! */
1128 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1129 COERCE_ARRAY (*argp);
1130 t = VALUE_TYPE (*argp);
1133 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1134 error ("not implemented: member type in value_struct_elt");
1136 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1137 && TYPE_CODE (t) != TYPE_CODE_UNION)
1138 error ("Attempt to extract a component of a value that is not a %s.", err);
1140 /* Assume it's not, unless we see that it is. */
1141 if (static_memfuncp)
1142 *static_memfuncp =0;
1146 /* if there are no arguments ...do this... */
1148 /* Try as a field first, because if we succeed, there
1149 is less work to be done. */
1150 v = search_struct_field (name, *argp, 0, t, 0);
1154 /* C++: If it was not found as a data field, then try to
1155 return it as a pointer to a method. */
1157 if (destructor_name_p (name, t))
1158 error ("Cannot get value of destructor");
1160 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1164 if (TYPE_NFN_FIELDS (t))
1165 error ("There is no member or method named %s.", name);
1167 error ("There is no member named %s.", name);
1172 if (destructor_name_p (name, t))
1176 /* destructors are a special case. */
1177 return (value)value_fn_field (*argp, 0,
1178 TYPE_FN_FIELDLIST_LENGTH (t, 0));
1182 error ("destructor should not have any argument");
1186 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1190 /* See if user tried to invoke data as function. If so,
1191 hand it back. If it's not callable (i.e., a pointer to function),
1192 gdb should give an error. */
1193 v = search_struct_field (name, *argp, 0, t, 0);
1197 error ("Structure has no component named %s.", name);
1201 /* C++: return 1 is NAME is a legitimate name for the destructor
1202 of type TYPE. If TYPE does not have a destructor, or
1203 if NAME is inappropriate for TYPE, an error is signaled. */
1205 destructor_name_p (name, type)
1209 /* destructors are a special case. */
1213 char *dname = type_name_no_tag (type);
1215 if (! TYPE_HAS_DESTRUCTOR (type))
1216 error ("type `%s' does not have destructor defined", dname);
1217 if (strcmp (dname, name+1))
1218 error ("name of destructor must equal name of class");
1225 /* Helper function for check_field: Given TYPE, a structure/union,
1226 return 1 if the component named NAME from the ultimate
1227 target structure/union is defined, otherwise, return 0. */
1230 check_field_in (type, name)
1231 register struct type *type;
1236 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1238 char *t_field_name = TYPE_FIELD_NAME (type, i);
1239 if (t_field_name && !strcmp (t_field_name, name))
1243 /* C++: If it was not found as a data field, then try to
1244 return it as a pointer to a method. */
1246 /* Destructors are a special case. */
1247 if (destructor_name_p (name, type))
1250 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1252 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name))
1256 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1257 if (check_field_in (TYPE_BASECLASS (type, i), name))
1264 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1265 return 1 if the component named NAME from the ultimate
1266 target structure/union is defined, otherwise, return 0. */
1269 check_field (arg1, name)
1270 register value arg1;
1273 register struct type *t;
1275 COERCE_ARRAY (arg1);
1277 t = VALUE_TYPE (arg1);
1279 /* Follow pointers until we get to a non-pointer. */
1281 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1282 t = TYPE_TARGET_TYPE (t);
1284 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1285 error ("not implemented: member type in check_field");
1287 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1288 && TYPE_CODE (t) != TYPE_CODE_UNION)
1289 error ("Internal error: `this' is not an aggregate");
1291 return check_field_in (t, name);
1294 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1295 return the address of this member as a pointer to member
1296 type. If INTYPE is non-null, then it will be the type
1297 of the member we are looking for. This will help us resolve
1298 pointers to member functions. */
1301 value_struct_elt_for_address (domain, intype, name)
1302 struct type *domain, *intype;
1305 register struct type *t = domain;
1309 struct type *baseclass;
1311 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1312 && TYPE_CODE (t) != TYPE_CODE_UNION)
1313 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1319 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1321 char *t_field_name = TYPE_FIELD_NAME (t, i);
1322 if (t_field_name && !strcmp (t_field_name, name))
1324 if (TYPE_FIELD_STATIC (t, i))
1326 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1327 struct symbol *sym =
1328 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1329 if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
1330 v = value_from_long(builtin_type_long,
1331 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1332 VALUE_TYPE(v) = lookup_pointer_type (TYPE_FIELD_TYPE (t, i));
1335 if (TYPE_FIELD_PACKED (t, i))
1336 error ("pointers to bitfield members not allowed");
1338 v = value_from_long (builtin_type_int,
1339 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1341 = lookup_pointer_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
1346 if (TYPE_N_BASECLASSES (t) == 0)
1349 t = TYPE_BASECLASS (t, 0);
1352 /* C++: If it was not found as a data field, then try to
1353 return it as a pointer to a method. */
1356 /* Destructors are a special case. */
1357 if (destructor_name_p (name, t))
1359 error ("pointers to destructors not implemented yet");
1362 /* Perform all necessary dereferencing. */
1363 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1364 intype = TYPE_TARGET_TYPE (intype);
1368 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1370 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1372 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1373 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1375 if (intype == 0 && j > 1)
1376 error ("non-unique member `%s' requires type instantiation", name);
1380 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1383 error ("no member function matches that type instantiation");
1388 check_stub_method (t, i, j);
1389 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1391 v = value_from_long (builtin_type_long,
1392 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1396 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1397 0, VAR_NAMESPACE, 0, NULL);
1398 v = locate_var_value (s, 0);
1400 VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1405 if (TYPE_N_BASECLASSES (t) == 0)
1408 t = TYPE_BASECLASS (t, 0);
1413 /* Compare two argument lists and return the position in which they differ,
1416 STATICP is nonzero if the T1 argument list came from a
1417 static member function.
1419 For non-static member functions, we ignore the first argument,
1420 which is the type of the instance variable. This is because we want
1421 to handle calls with objects from derived classes. This is not
1422 entirely correct: we should actually check to make sure that a
1423 requested operation is type secure, shouldn't we? FIXME. */
1426 typecmp (staticp, t1, t2)
1435 if (staticp && t1 == 0)
1439 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1440 if (t1[!staticp] == 0) return 0;
1441 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1444 || t1[i]->code != t2[i]->type->code
1445 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1449 if (!t1[i]) return 0;
1450 return t2[i] ? i+1 : 0;
1453 /* C++: return the value of the class instance variable, if one exists.
1454 Flag COMPLAIN signals an error if the request is made in an
1455 inappropriate context. */
1457 value_of_this (complain)
1460 extern FRAME selected_frame;
1461 struct symbol *func, *sym;
1464 static const char funny_this[] = "this";
1467 if (selected_frame == 0)
1469 error ("no frame selected");
1472 func = get_frame_function (selected_frame);
1476 error ("no `this' in nameless context");
1480 b = SYMBOL_BLOCK_VALUE (func);
1481 i = BLOCK_NSYMS (b);
1484 error ("no args, no `this'");
1487 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1488 symbol instead of the LOC_ARG one (if both exist). */
1489 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1493 error ("current stack frame not in method");
1498 this = read_var_value (sym, selected_frame);
1499 if (this == 0 && complain)
1500 error ("`this' argument at unknown address");