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);
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. */
964 search_struct_field (name, arg1, offset, type)
968 register struct type *type;
972 check_stub_type (type);
974 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
976 char *t_field_name = TYPE_FIELD_NAME (type, i);
977 if (t_field_name && !strcmp (t_field_name, name))
978 return TYPE_FIELD_STATIC (type, i)
979 ? value_static_field (type, name, i)
980 : value_primitive_field (arg1, offset, i, type);
983 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
986 /* If we are looking for baseclasses, this is what we get when we
988 int found_baseclass = !strcmp (name, TYPE_BASECLASS_NAME (type, i));
990 if (BASETYPE_VIA_VIRTUAL (type, i))
993 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
995 error ("virtual baseclass botch");
998 v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i));
1002 if (found_baseclass)
1003 v = value_primitive_field (arg1, offset, i, type);
1005 v = search_struct_field (name, arg1,
1006 offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
1007 TYPE_BASECLASS (type, i));
1013 /* Helper function used by value_struct_elt to recurse through baseclasses.
1014 Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
1015 and treat the result as having type TYPE.
1016 If found, return value, else return NULL. */
1019 search_struct_method (name, arg1, args, offset, static_memfuncp, type)
1021 register value arg1, *args;
1022 int offset, *static_memfuncp;
1023 register struct type *type;
1027 check_stub_type (type);
1028 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
1030 char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
1031 if (t_field_name && !strcmp (t_field_name, name))
1034 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
1036 for (j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; j >= 0; --j)
1038 if (TYPE_FLAGS (TYPE_FN_FIELD_TYPE (f, j)) & TYPE_FLAG_STUB)
1039 check_stub_method (type, i, j);
1040 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1041 TYPE_FN_FIELD_ARGS (f, j), args))
1043 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1044 return (value)value_virtual_fn_field (arg1, f, j);
1045 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1046 *static_memfuncp = 1;
1047 return (value)value_fn_field (arg1, i, j);
1053 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1057 if (BASETYPE_VIA_VIRTUAL (type, i))
1060 baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset, &v2);
1062 error ("virtual baseclass botch");
1063 v = search_struct_method (name, v2, args, 0,
1064 static_memfuncp, TYPE_BASECLASS (type, i));
1069 v = search_struct_method (name, arg1, args,
1070 TYPE_BASECLASS_BITPOS (type, i) / 8,
1071 static_memfuncp, TYPE_BASECLASS (type, i));
1077 /* Given *ARGP, a value of type (pointer to a)* structure/union,
1078 extract the component named NAME from the ultimate target structure/union
1079 and return it as a value with its appropriate type.
1080 ERR is used in the error message if *ARGP's type is wrong.
1082 C++: ARGS is a list of argument types to aid in the selection of
1083 an appropriate method. Also, handle derived types.
1085 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
1086 where the truthvalue of whether the function that was resolved was
1087 a static member function or not is stored.
1089 ERR is an error message to be printed in case the field is not found. */
1092 value_struct_elt (argp, args, name, static_memfuncp, err)
1093 register value *argp, *args;
1095 int *static_memfuncp;
1098 register struct type *t;
1099 int found = 0; /* FIXME, half the time this doesn't get set */
1100 value arg1_as_ptr = *argp; /* FIXME, set but not used! */
1103 COERCE_ARRAY (*argp);
1105 t = VALUE_TYPE (*argp);
1107 /* Follow pointers until we get to a non-pointer. */
1109 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1111 arg1_as_ptr = *argp;
1112 *argp = value_ind (*argp);
1113 /* Don't coerce fn pointer to fn and then back again! */
1114 if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
1115 COERCE_ARRAY (*argp);
1116 t = VALUE_TYPE (*argp);
1119 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1120 error ("not implemented: member type in value_struct_elt");
1122 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1123 && TYPE_CODE (t) != TYPE_CODE_UNION)
1124 error ("Attempt to extract a component of a value that is not a %s.", err);
1126 /* Assume it's not, unless we see that it is. */
1127 if (static_memfuncp)
1128 *static_memfuncp =0;
1132 /* if there are no arguments ...do this... */
1134 /* Try as a variable first, because if we succeed, there
1135 is less work to be done. */
1136 v = search_struct_field (name, *argp, 0, t);
1140 /* C++: If it was not found as a data field, then try to
1141 return it as a pointer to a method. */
1143 if (destructor_name_p (name, t))
1144 error ("Cannot get value of destructor");
1146 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1150 if (TYPE_NFN_FIELDS (t))
1151 error ("There is no member or method named %s.", name);
1153 error ("There is no member named %s.", name);
1158 if (destructor_name_p (name, t))
1162 /* destructors are a special case. */
1163 return (value)value_fn_field (*argp, 0,
1164 TYPE_FN_FIELDLIST_LENGTH (t, 0));
1168 error ("destructor should not have any argument");
1172 v = search_struct_method (name, *argp, args, 0, static_memfuncp, t);
1176 /* See if user tried to invoke data as function. If so,
1177 hand it back. If it's not callable (i.e., a pointer to function),
1178 gdb should give an error. */
1179 v = search_struct_field (name, *argp, 0, t);
1183 error ("Structure has no component named %s.", name);
1187 /* C++: return 1 is NAME is a legitimate name for the destructor
1188 of type TYPE. If TYPE does not have a destructor, or
1189 if NAME is inappropriate for TYPE, an error is signaled. */
1191 destructor_name_p (name, type)
1195 /* destructors are a special case. */
1199 char *dname = type_name_no_tag (type);
1201 if (! TYPE_HAS_DESTRUCTOR (type))
1202 error ("type `%s' does not have destructor defined", dname);
1203 if (strcmp (dname, name+1))
1204 error ("name of destructor must equal name of class");
1211 /* Helper function for check_field: Given TYPE, a structure/union,
1212 return 1 if the component named NAME from the ultimate
1213 target structure/union is defined, otherwise, return 0. */
1216 check_field_in (type, name)
1217 register struct type *type;
1222 for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1224 char *t_field_name = TYPE_FIELD_NAME (type, i);
1225 if (t_field_name && !strcmp (t_field_name, name))
1229 /* C++: If it was not found as a data field, then try to
1230 return it as a pointer to a method. */
1232 /* Destructors are a special case. */
1233 if (destructor_name_p (name, type))
1236 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
1238 if (!strcmp (TYPE_FN_FIELDLIST_NAME (type, i), name))
1242 for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1243 if (check_field_in (TYPE_BASECLASS (type, i), name))
1250 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1251 return 1 if the component named NAME from the ultimate
1252 target structure/union is defined, otherwise, return 0. */
1255 check_field (arg1, name)
1256 register value arg1;
1259 register struct type *t;
1261 COERCE_ARRAY (arg1);
1263 t = VALUE_TYPE (arg1);
1265 /* Follow pointers until we get to a non-pointer. */
1267 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1268 t = TYPE_TARGET_TYPE (t);
1270 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1271 error ("not implemented: member type in check_field");
1273 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1274 && TYPE_CODE (t) != TYPE_CODE_UNION)
1275 error ("Internal error: `this' is not an aggregate");
1277 return check_field_in (t, name);
1280 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1281 return the address of this member as a pointer to member
1282 type. If INTYPE is non-null, then it will be the type
1283 of the member we are looking for. This will help us resolve
1284 pointers to member functions. */
1287 value_struct_elt_for_address (domain, intype, name)
1288 struct type *domain, *intype;
1291 register struct type *t = domain;
1295 struct type *baseclass;
1297 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1298 && TYPE_CODE (t) != TYPE_CODE_UNION)
1299 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1305 for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
1307 char *t_field_name = TYPE_FIELD_NAME (t, i);
1308 if (t_field_name && !strcmp (t_field_name, name))
1310 if (TYPE_FIELD_STATIC (t, i))
1312 char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
1313 struct symbol *sym =
1314 lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
1315 if (! sym) error ("Internal error: could not find physical static variable named %s", phys_name);
1316 v = value_from_long(builtin_type_long,
1317 (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
1318 VALUE_TYPE(v) = lookup_pointer_type (TYPE_FIELD_TYPE (t, i));
1321 if (TYPE_FIELD_PACKED (t, i))
1322 error ("pointers to bitfield members not allowed");
1324 v = value_from_long (builtin_type_int,
1325 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1327 = lookup_pointer_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
1332 if (TYPE_N_BASECLASSES (t) == 0)
1335 t = TYPE_BASECLASS (t, 0);
1338 /* C++: If it was not found as a data field, then try to
1339 return it as a pointer to a method. */
1342 /* Destructors are a special case. */
1343 if (destructor_name_p (name, t))
1345 error ("pointers to destructors not implemented yet");
1348 /* Perform all necessary dereferencing. */
1349 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1350 intype = TYPE_TARGET_TYPE (intype);
1354 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1356 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1358 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1359 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1361 if (intype == 0 && j > 1)
1362 error ("non-unique member `%s' requires type instantiation", name);
1366 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1369 error ("no member function matches that type instantiation");
1374 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1376 v = value_from_long (builtin_type_long,
1377 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1381 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1382 0, VAR_NAMESPACE, 0, NULL);
1383 v = locate_var_value (s, 0);
1385 VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1390 if (TYPE_N_BASECLASSES (t) == 0)
1393 t = TYPE_BASECLASS (t, 0);
1398 /* Compare two argument lists and return the position in which they differ,
1401 STATICP is nonzero if the T1 argument list came from a
1402 static member function.
1404 For non-static member functions, we ignore the first argument,
1405 which is the type of the instance variable. This is because we want
1406 to handle calls with objects from derived classes. This is not
1407 entirely correct: we should actually check to make sure that a
1408 requested operation is type secure, shouldn't we? FIXME. */
1411 typecmp (staticp, t1, t2)
1418 if (staticp && t1 == 0)
1422 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1423 if (t1[!staticp] == 0) return 0;
1424 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1427 || t1[i]->code != t2[i]->type->code
1428 /* Too pessimistic: || t1[i]->target_type != t2[i]->type->target_type */
1432 if (!t1[i]) return 0;
1433 return t2[i] ? i+1 : 0;
1436 /* C++: return the value of the class instance variable, if one exists.
1437 Flag COMPLAIN signals an error if the request is made in an
1438 inappropriate context. */
1440 value_of_this (complain)
1443 extern FRAME selected_frame;
1444 struct symbol *func, *sym;
1447 static const char funny_this[] = "this";
1451 if (selected_frame == 0)
1453 error ("no frame selected");
1456 func = get_frame_function (selected_frame);
1460 error ("no `this' in nameless context");
1464 b = SYMBOL_BLOCK_VALUE (func);
1465 i = BLOCK_NSYMS (b);
1468 error ("no args, no `this'");
1471 /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
1472 symbol instead of the LOC_ARG one (if both exist). */
1473 sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
1477 error ("current stack frame not in method");
1482 this = read_var_value (sym, selected_frame);
1483 if (this == 0 && complain)
1484 error ("`this' argument at unknown address");