1 /*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
7 */
8
9 #ifndef lint
10 static char sccsid[] = "@(#)valops.c 6.4 (Berkeley) 5/8/91";
11 #endif /* not lint */
12
13 /* Perform non-arithmetic operations on values, for GDB.
14 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
15
16 This file is part of GDB.
17
18 GDB is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 1, or (at your option)
21 any later version.
22
23 GDB is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with GDB; see the file COPYING. If not, write to
30 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31
32 #include "stdio.h"
33 #include "defs.h"
34 #include "param.h"
35 #include "symtab.h"
36 #include "value.h"
37 #include "frame.h"
38 #include "inferior.h"
39
40 /* Cast value ARG2 to type TYPE and return as a value.
41 More general than a C cast: accepts any two types of the same length,
42 and if ARG2 is an lvalue it can be cast into anything at all. */
43
44 value
value_cast(type,arg2)45 value_cast (type, arg2)
46 struct type *type;
47 register value arg2;
48 {
49 register enum type_code code1;
50 register enum type_code code2;
51 register int scalar;
52
53 /* Coerce arrays but not enums. Enums will work as-is
54 and coercing them would cause an infinite recursion. */
55 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
56 COERCE_ARRAY (arg2);
57
58 code1 = TYPE_CODE (type);
59 code2 = TYPE_CODE (VALUE_TYPE (arg2));
60 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
61 || code2 == TYPE_CODE_ENUM);
62
63 if (code1 == TYPE_CODE_FLT && scalar)
64 return value_from_double (type, value_as_double (arg2));
65 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
66 && (scalar || code2 == TYPE_CODE_PTR))
67 return value_from_long (type, value_as_long (arg2));
68 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
69 {
70 VALUE_TYPE (arg2) = type;
71 return arg2;
72 }
73 else if (VALUE_LVAL (arg2) == lval_memory)
74 {
75 return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
76 }
77 else
78 error ("Invalid cast.");
79 }
80
81 /* Create a value of type TYPE that is zero, and return it. */
82
83 value
value_zero(type,lv)84 value_zero (type, lv)
85 struct type *type;
86 enum lval_type lv;
87 {
88 register value val = allocate_value (type);
89
90 bzero (VALUE_CONTENTS (val), TYPE_LENGTH (type));
91 VALUE_LVAL (val) = lv;
92
93 return val;
94 }
95
96 /* Return the value with a specified type located at specified address. */
97
98 value
value_at(type,addr)99 value_at (type, addr)
100 struct type *type;
101 CORE_ADDR addr;
102 {
103 register value val = allocate_value (type);
104 int temp;
105
106 temp = read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
107 if (temp)
108 {
109 if (have_inferior_p () && !remote_debugging)
110 print_sys_errmsg ("ptrace", temp);
111 /* Actually, address between addr and addr + len was out of bounds. */
112 error ("Cannot read memory: address 0x%x out of bounds.", addr);
113 }
114
115 VALUE_LVAL (val) = lval_memory;
116 VALUE_ADDRESS (val) = addr;
117
118 return val;
119 }
120
121 /* Store the contents of FROMVAL into the location of TOVAL.
122 Return a new value with the location of TOVAL and contents of FROMVAL. */
123
124 value
value_assign(toval,fromval)125 value_assign (toval, fromval)
126 register value toval, fromval;
127 {
128 register struct type *type = VALUE_TYPE (toval);
129 register value val;
130 char raw_buffer[MAX_REGISTER_RAW_SIZE];
131 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
132 int use_buffer = 0;
133
134 extern CORE_ADDR find_saved_register ();
135
136 COERCE_ARRAY (fromval);
137
138 if (VALUE_LVAL (toval) != lval_internalvar)
139 fromval = value_cast (type, fromval);
140
141 /* If TOVAL is a special machine register requiring conversion
142 of program values to a special raw format,
143 convert FROMVAL's contents now, with result in `raw_buffer',
144 and set USE_BUFFER to the number of bytes to write. */
145
146 if (VALUE_REGNO (toval) >= 0
147 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
148 {
149 int regno = VALUE_REGNO (toval);
150 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
151 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
152 bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
153 REGISTER_VIRTUAL_SIZE (regno));
154 REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
155 use_buffer = REGISTER_RAW_SIZE (regno);
156 }
157
158 switch (VALUE_LVAL (toval))
159 {
160 case lval_internalvar:
161 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
162 break;
163
164 case lval_internalvar_component:
165 set_internalvar_component (VALUE_INTERNALVAR (toval),
166 VALUE_OFFSET (toval),
167 VALUE_BITPOS (toval),
168 VALUE_BITSIZE (toval),
169 fromval);
170 break;
171
172 case lval_memory:
173 if (VALUE_BITSIZE (toval))
174 {
175 int val;
176 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
177 &val, sizeof val);
178 modify_field (&val, (int) value_as_long (fromval),
179 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
180 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
181 &val, sizeof val);
182 }
183 else if (use_buffer)
184 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
185 raw_buffer, use_buffer);
186 else
187 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
188 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
189 break;
190
191 case lval_register:
192 if (VALUE_BITSIZE (toval))
193 {
194 int val;
195
196 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
197 &val, sizeof val);
198 modify_field (&val, (int) value_as_long (fromval),
199 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
200 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
201 &val, sizeof val);
202 }
203 else if (use_buffer)
204 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
205 raw_buffer, use_buffer);
206 else
207 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
208 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
209 break;
210
211 case lval_reg_frame_relative:
212 {
213 /* value is stored in a series of registers in the frame
214 specified by the structure. Copy that value out, modify
215 it, and copy it back in. */
216 int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
217 int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
218 int byte_offset = VALUE_OFFSET (toval) % reg_size;
219 int reg_offset = VALUE_OFFSET (toval) / reg_size;
220 int amount_copied;
221 char *buffer = (char *) alloca (amount_to_copy);
222 int regno;
223 FRAME frame;
224 CORE_ADDR addr;
225
226 /* Figure out which frame this is in currently. */
227 for (frame = get_current_frame ();
228 frame && FRAME_FP (frame) != VALUE_FRAME (toval);
229 frame = get_prev_frame (frame))
230 ;
231
232 if (!frame)
233 error ("Value being assigned to is no longer active.");
234
235 amount_to_copy += (reg_size - amount_to_copy % reg_size);
236
237 /* Copy it out. */
238 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
239 amount_copied = 0);
240 amount_copied < amount_to_copy;
241 amount_copied += reg_size, regno++)
242 {
243 addr = find_saved_register (frame, regno);
244 if (addr == 0)
245 read_register_bytes (REGISTER_BYTE (regno),
246 buffer + amount_copied,
247 reg_size);
248 else
249 read_memory (addr, buffer + amount_copied, reg_size);
250 }
251
252 /* Modify what needs to be modified. */
253 if (VALUE_BITSIZE (toval))
254 modify_field (buffer + byte_offset,
255 (int) value_as_long (fromval),
256 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
257 else if (use_buffer)
258 bcopy (raw_buffer, buffer + byte_offset, use_buffer);
259 else
260 bcopy (VALUE_CONTENTS (fromval), buffer + byte_offset,
261 TYPE_LENGTH (type));
262
263 /* Copy it back. */
264 for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
265 amount_copied = 0);
266 amount_copied < amount_to_copy;
267 amount_copied += reg_size, regno++)
268 {
269 addr = find_saved_register (frame, regno);
270 if (addr == 0)
271 write_register_bytes (REGISTER_BYTE (regno),
272 buffer + amount_copied,
273 reg_size);
274 else
275 write_memory (addr, buffer + amount_copied, reg_size);
276 }
277 }
278 break;
279
280
281 default:
282 error ("Left side of = operation is not an lvalue.");
283 }
284
285 /* Return a value just like TOVAL except with the contents of FROMVAL
286 (except in the case of the type if TOVAL is an internalvar). */
287
288 if (VALUE_LVAL (toval) == lval_internalvar
289 || VALUE_LVAL (toval) == lval_internalvar_component)
290 {
291 type = VALUE_TYPE (fromval);
292 }
293
294 val = allocate_value (type);
295 bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
296 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
297 VALUE_TYPE (val) = type;
298
299 return val;
300 }
301
302 /* Extend a value VAL to COUNT repetitions of its type. */
303
304 value
value_repeat(arg1,count)305 value_repeat (arg1, count)
306 value arg1;
307 int count;
308 {
309 register value val;
310
311 if (VALUE_LVAL (arg1) != lval_memory)
312 error ("Only values in memory can be extended with '@'.");
313 if (count < 1)
314 error ("Invalid number %d of repetitions.", count);
315
316 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
317
318 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
319 VALUE_CONTENTS (val),
320 TYPE_LENGTH (VALUE_TYPE (val)) * count);
321 VALUE_LVAL (val) = lval_memory;
322 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
323
324 return val;
325 }
326
327 value
value_of_variable(var)328 value_of_variable (var)
329 struct symbol *var;
330 {
331 return read_var_value (var, (FRAME) 0);
332 }
333
334 /* Given a value which is an array, return a value which is
335 a pointer to its first element. */
336
337 value
value_coerce_array(arg1)338 value_coerce_array (arg1)
339 value arg1;
340 {
341 register struct type *type;
342 register value val;
343
344 if (VALUE_LVAL (arg1) != lval_memory)
345 error ("Attempt to take address of value not located in memory.");
346
347 /* Get type of elements. */
348 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
349 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
350 else
351 /* A phony array made by value_repeat.
352 Its type is the type of the elements, not an array type. */
353 type = VALUE_TYPE (arg1);
354
355 /* Get the type of the result. */
356 type = lookup_pointer_type (type);
357 val = value_from_long (builtin_type_long,
358 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
359 VALUE_TYPE (val) = type;
360 return val;
361 }
362
363 /* Return a pointer value for the object for which ARG1 is the contents. */
364
365 value
value_addr(arg1)366 value_addr (arg1)
367 value arg1;
368 {
369 register struct type *type;
370 register value val, arg1_coerced;
371
372 /* Taking the address of an array is really a no-op
373 once the array is coerced to a pointer to its first element. */
374 arg1_coerced = arg1;
375 COERCE_ARRAY (arg1_coerced);
376 if (arg1 != arg1_coerced)
377 return arg1_coerced;
378
379 if (VALUE_LVAL (arg1) != lval_memory)
380 error ("Attempt to take address of value not located in memory.");
381
382 /* Get the type of the result. */
383 type = lookup_pointer_type (VALUE_TYPE (arg1));
384 val = value_from_long (builtin_type_long,
385 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
386 VALUE_TYPE (val) = type;
387 return val;
388 }
389
390 /* Given a value of a pointer type, apply the C unary * operator to it. */
391
392 value
value_ind(arg1)393 value_ind (arg1)
394 value arg1;
395 {
396 /* Must do this before COERCE_ARRAY, otherwise an infinite loop
397 will result */
398 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
399 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
400 (CORE_ADDR) value_as_long (arg1));
401
402 COERCE_ARRAY (arg1);
403
404 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
405 error ("not implemented: member types in value_ind");
406
407 /* Allow * on an integer so we can cast it to whatever we want.
408 This returns an int, which seems like the most C-like thing
409 to do. "long long" variables are rare enough that
410 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
411 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
412 return value_at (builtin_type_int,
413 (CORE_ADDR) value_as_long (arg1));
414 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
415 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
416 (CORE_ADDR) value_as_long (arg1));
417 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
418 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
419 (CORE_ADDR) value_as_long (arg1));
420 error ("Attempt to take contents of a non-pointer value.");
421 }
422
423 /* Pushing small parts of stack frames. */
424
425 /* Push one word (the size of object that a register holds). */
426
427 CORE_ADDR
push_word(sp,buffer)428 push_word (sp, buffer)
429 CORE_ADDR sp;
430 REGISTER_TYPE buffer;
431 {
432 register int len = sizeof (REGISTER_TYPE);
433
434 #if 1 INNER_THAN 2
435 sp -= len;
436 write_memory (sp, &buffer, len);
437 #else /* stack grows upward */
438 write_memory (sp, &buffer, len);
439 sp += len;
440 #endif /* stack grows upward */
441
442 return sp;
443 }
444
445 /* Push LEN bytes with data at BUFFER. */
446
447 CORE_ADDR
push_bytes(sp,buffer,len)448 push_bytes (sp, buffer, len)
449 CORE_ADDR sp;
450 char *buffer;
451 int len;
452 {
453 #if 1 INNER_THAN 2
454 sp -= len;
455 write_memory (sp, buffer, len);
456 #else /* stack grows upward */
457 write_memory (sp, buffer, len);
458 sp += len;
459 #endif /* stack grows upward */
460
461 return sp;
462 }
463
464 /* Push onto the stack the specified value VALUE. */
465
466 CORE_ADDR
value_push(sp,arg)467 value_push (sp, arg)
468 register CORE_ADDR sp;
469 value arg;
470 {
471 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
472
473 #if 1 INNER_THAN 2
474 sp -= len;
475 write_memory (sp, VALUE_CONTENTS (arg), len);
476 #else /* stack grows upward */
477 write_memory (sp, VALUE_CONTENTS (arg), len);
478 sp += len;
479 #endif /* stack grows upward */
480
481 return sp;
482 }
483
484 /* Perform the standard coercions that are specified
485 for arguments to be passed to C functions. */
486
487 value
value_arg_coerce(arg)488 value_arg_coerce (arg)
489 value arg;
490 {
491 register struct type *type;
492
493 COERCE_ENUM (arg);
494
495 type = VALUE_TYPE (arg);
496
497 if (TYPE_CODE (type) == TYPE_CODE_INT
498 && TYPE_LENGTH (type) < sizeof (int))
499 return value_cast (builtin_type_int, arg);
500
501 if (type == builtin_type_float)
502 return value_cast (builtin_type_double, arg);
503
504 return arg;
505 }
506
507 /* Push the value ARG, first coercing it as an argument
508 to a C function. */
509
510 CORE_ADDR
value_arg_push(sp,arg)511 value_arg_push (sp, arg)
512 register CORE_ADDR sp;
513 value arg;
514 {
515 return value_push (sp, value_arg_coerce (arg));
516 }
517
518 #ifdef NEW_CALL_FUNCTION
519
520 int
arg_stacklen(nargs,args)521 arg_stacklen(nargs, args)
522 int nargs;
523 value *args;
524 {
525 int len = 0;
526
527 while (--nargs >= 0)
528 len += TYPE_LENGTH(VALUE_TYPE(value_arg_coerce(args[nargs])));
529
530 return len;
531 }
532
533 CORE_ADDR
function_address(function,type)534 function_address(function, type)
535 value function;
536 struct type **type;
537 {
538 register CORE_ADDR funaddr;
539 register struct type *ftype = VALUE_TYPE(function);
540 register enum type_code code = TYPE_CODE(ftype);
541
542 /*
543 * If it's a member function, just look at the function part
544 * of it.
545 */
546
547 /* Determine address to call. */
548 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) {
549 funaddr = VALUE_ADDRESS(function);
550 *type = TYPE_TARGET_TYPE(ftype);
551 } else if (code == TYPE_CODE_PTR) {
552 funaddr = value_as_long(function);
553 if (TYPE_CODE(TYPE_TARGET_TYPE(ftype)) == TYPE_CODE_FUNC
554 || TYPE_CODE(TYPE_TARGET_TYPE(ftype)) == TYPE_CODE_METHOD)
555 *type = TYPE_TARGET_TYPE(TYPE_TARGET_TYPE(ftype));
556 else
557 *type = builtin_type_int;
558 } else if (code == TYPE_CODE_INT) {
559 /*
560 * Handle the case of functions lacking debugging
561 * info. Their values are characters since their
562 * addresses are char
563 */
564 if (TYPE_LENGTH(ftype) == 1)
565
566 funaddr = value_as_long(value_addr(function));
567 else
568 /*
569 * Handle integer used as address of a
570 * function.
571 */
572 funaddr = value_as_long(function);
573
574 *type = builtin_type_int;
575 } else
576 error("Invalid data type for function to be called.");
577
578 return funaddr;
579 }
580
581 /* Perform a function call in the inferior.
582 ARGS is a vector of values of arguments (NARGS of them).
583 FUNCTION is a value, the function to be called.
584 Returns a value representing what the function returned.
585 May fail to return, if a breakpoint or signal is hit
586 during the execution of the function. */
587
588 value
call_function(function,nargs,args)589 call_function(function, nargs, args)
590 value function;
591 int nargs;
592 value *args;
593 {
594 register CORE_ADDR sp, pc;
595 struct type *value_type;
596 struct inferior_status inf_status;
597 struct cleanup *old_chain;
598 register CORE_ADDR funaddr;
599 int struct_return_bytes;
600 char retbuf[REGISTER_BYTES];
601
602 if (!have_inferior_p())
603 error("Cannot invoke functions if the inferior is not running.");
604
605 save_inferior_status(&inf_status, 1);
606 old_chain = make_cleanup(restore_inferior_status, &inf_status);
607
608 sp = read_register(SP_REGNUM);
609 funaddr = function_address(function, &value_type);
610 /*
611 * Are we returning a value using a structure return or a
612 * normal value return?
613 */
614 if (using_struct_return(function, funaddr, value_type))
615 struct_return_bytes = TYPE_LENGTH(value_type);
616 else
617 struct_return_bytes = 0;
618 /*
619 * Create a call sequence customized for this function and
620 * the number of arguments for it.
621 */
622 pc = setup_dummy(sp, funaddr, nargs, args,
623 struct_return_bytes, value_arg_push);
624
625 /*
626 * Execute the stack dummy stub. The register state will be
627 * returned in retbuf. It is restored below.
628 */
629 run_stack_dummy(pc, retbuf);
630
631 /*
632 * This will restore the register context that existed before
633 * we called the dummy function.
634 */
635 do_cleanups(old_chain);
636
637 return value_being_returned(value_type, retbuf, struct_return_bytes);
638 }
639 #else
640
641 /* Perform a function call in the inferior.
642 ARGS is a vector of values of arguments (NARGS of them).
643 FUNCTION is a value, the function to be called.
644 Returns a value representing what the function returned.
645 May fail to return, if a breakpoint or signal is hit
646 during the execution of the function. */
647
648 value
call_function(function,nargs,args)649 call_function (function, nargs, args)
650 value function;
651 int nargs;
652 value *args;
653 {
654 register CORE_ADDR sp;
655 register int i;
656 CORE_ADDR start_sp;
657 static REGISTER_TYPE dummy[] = CALL_DUMMY;
658 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
659 CORE_ADDR old_sp;
660 struct type *value_type;
661 unsigned char struct_return;
662 CORE_ADDR struct_addr;
663 struct inferior_status inf_status;
664 struct cleanup *old_chain;
665
666 if (!have_inferior_p ())
667 error ("Cannot invoke functions if the inferior is not running.");
668
669 save_inferior_status (&inf_status, 1);
670 old_chain = make_cleanup (restore_inferior_status, &inf_status);
671
672 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
673 (and POP_FRAME for restoring them). (At least on most machines)
674 they are saved on the stack in the inferior. */
675 PUSH_DUMMY_FRAME;
676
677 old_sp = sp = read_register (SP_REGNUM);
678
679 #if 1 INNER_THAN 2 /* Stack grows down */
680 sp -= sizeof dummy;
681 start_sp = sp;
682 #else /* Stack grows up */
683 start_sp = sp;
684 sp += sizeof dummy;
685 #endif
686
687 {
688 register CORE_ADDR funaddr;
689 register struct type *ftype = VALUE_TYPE (function);
690 register enum type_code code = TYPE_CODE (ftype);
691
692 /* If it's a member function, just look at the function
693 part of it. */
694
695 /* Determine address to call. */
696 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
697 {
698 funaddr = VALUE_ADDRESS (function);
699 value_type = TYPE_TARGET_TYPE (ftype);
700 }
701 else if (code == TYPE_CODE_PTR)
702 {
703 funaddr = value_as_long (function);
704 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
705 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
706 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
707 else
708 value_type = builtin_type_int;
709 }
710 else if (code == TYPE_CODE_INT)
711 {
712 /* Handle the case of functions lacking debugging info.
713 Their values are characters since their addresses are char */
714 if (TYPE_LENGTH (ftype) == 1)
715 funaddr = value_as_long (value_addr (function));
716 else
717 /* Handle integer used as address of a function. */
718 funaddr = value_as_long (function);
719
720 value_type = builtin_type_int;
721 }
722 else
723 error ("Invalid data type for function to be called.");
724
725 /* Are we returning a value using a structure return or a normal
726 value return? */
727
728 struct_return = using_struct_return (function, funaddr, value_type);
729
730 /* Create a call sequence customized for this function
731 and the number of arguments for it. */
732 bcopy (dummy, dummy1, sizeof dummy);
733 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, value_type);
734 }
735
736 #ifndef CANNOT_EXECUTE_STACK
737 write_memory (start_sp, dummy1, sizeof dummy);
738
739 #else
740 /* Convex Unix prohibits executing in the stack segment. */
741 /* Hope there is empty room at the top of the text segment. */
742 {
743 extern CORE_ADDR text_end;
744 static checked = 0;
745 if (!checked)
746 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
747 if (read_memory_integer (start_sp, 1) != 0)
748 error ("text segment full -- no place to put call");
749 checked = 1;
750 sp = old_sp;
751 start_sp = text_end - sizeof dummy;
752 write_memory (start_sp, dummy1, sizeof dummy);
753 }
754 #endif /* CANNOT_EXECUTE_STACK */
755 #ifdef STACK_ALIGN
756 /* If stack grows down, we must leave a hole at the top. */
757 {
758 int len = 0;
759
760 /* Reserve space for the return structure to be written on the
761 stack, if necessary */
762
763 if (struct_return)
764 len += TYPE_LENGTH (value_type);
765
766 for (i = nargs - 1; i >= 0; i--)
767 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
768 #ifdef CALL_DUMMY_STACK_ADJUST
769 len += CALL_DUMMY_STACK_ADJUST;
770 #endif
771 #if 1 INNER_THAN 2
772 sp -= STACK_ALIGN (len) - len;
773 #else
774 sp += STACK_ALIGN (len) - len;
775 #endif
776 }
777 #endif /* STACK_ALIGN */
778
779 /* Reserve space for the return structure to be written on the
780 stack, if necessary */
781
782 if (struct_return)
783 {
784 #if 1 INNER_THAN 2
785 sp -= TYPE_LENGTH (value_type);
786 struct_addr = sp;
787 #else
788 struct_addr = sp;
789 sp += TYPE_LENGTH (value_type);
790 #endif
791 }
792
793 for (i = nargs - 1; i >= 0; i--)
794 sp = value_arg_push (sp, args[i]);
795
796 #ifdef CALL_DUMMY_STACK_ADJUST
797 #if 1 INNER_THAN 2
798 sp -= CALL_DUMMY_STACK_ADJUST;
799 #else
800 sp += CALL_DUMMY_STACK_ADJUST;
801 #endif
802 #endif /* CALL_DUMMY_STACK_ADJUST */
803
804 /* Store the address at which the structure is supposed to be
805 written. Note that this (and the code which reserved the space
806 above) assumes that gcc was used to compile this function. Since
807 it doesn't cost us anything but space and if the function is pcc
808 it will ignore this value, we will make that assumption.
809
810 Also note that on some machines (like the sparc) pcc uses this
811 convention in a slightly twisted way also. */
812
813 if (struct_return)
814 STORE_STRUCT_RETURN (struct_addr, sp);
815
816 /* Write the stack pointer. This is here because the statement above
817 might fool with it */
818 write_register (SP_REGNUM, sp);
819
820 /* Figure out the value returned by the function. */
821 {
822 char retbuf[REGISTER_BYTES];
823
824 /* Execute the stack dummy routine, calling FUNCTION.
825 When it is done, discard the empty frame
826 after storing the contents of all regs into retbuf. */
827 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
828
829 do_cleanups (old_chain);
830
831 return value_being_returned (value_type, retbuf, struct_return);
832 }
833 }
834 #endif
835
836 /* Create a value for a string constant:
837 Call the function malloc in the inferior to get space for it,
838 then copy the data into that space
839 and then return the address with type char *.
840 PTR points to the string constant data; LEN is number of characters. */
841
842 value
value_string(ptr,len)843 value_string (ptr, len)
844 char *ptr;
845 int len;
846 {
847 register value val;
848 register struct symbol *sym;
849 value blocklen;
850 register char *copy = (char *) alloca (len + 1);
851 char *i = ptr;
852 register char *o = copy, *ibeg = ptr;
853 register int c;
854 #ifdef KERNELDEBUG
855 extern int kernel_debugging;
856
857 if (kernel_debugging)
858 error("Can't stuff string constants into kernel (yet).");
859 #endif
860
861 /* Copy the string into COPY, processing escapes.
862 We could not conveniently process them in expread
863 because the string there wants to be a substring of the input. */
864
865 while (i - ibeg < len)
866 {
867 c = *i++;
868 if (c == '\\')
869 {
870 c = parse_escape (&i);
871 if (c == -1)
872 continue;
873 }
874 *o++ = c;
875 }
876 *o = 0;
877
878 /* Get the length of the string after escapes are processed. */
879
880 len = o - copy;
881
882 /* Find the address of malloc in the inferior. */
883
884 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0);
885 if (sym != 0)
886 {
887 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
888 error ("\"malloc\" exists in this program but is not a function.");
889 val = value_of_variable (sym);
890 }
891 else
892 {
893 register int i;
894 for (i = 0; i < misc_function_count; i++)
895 if (!strcmp (misc_function_vector[i].name, "malloc"))
896 break;
897 if (i < misc_function_count)
898 val = value_from_long (builtin_type_long,
899 (LONGEST) misc_function_vector[i].address);
900 else
901 error ("String constants require the program to have a function \"malloc\".");
902 }
903
904 blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
905 val = call_function (val, 1, &blocklen);
906 if (value_zerop (val))
907 error ("No memory available for string constant.");
908 write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
909 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
910 return val;
911 }
912
913 static int
type_field_index(t,name)914 type_field_index(t, name)
915 register struct type *t;
916 register char *name;
917 {
918 register int i;
919
920 for (i = TYPE_NFIELDS(t); --i >= 0;)
921 {
922 register char *t_field_name = TYPE_FIELD_NAME (t, i);
923
924 if (t_field_name && !strcmp (t_field_name, name))
925 break;
926 }
927 return (i);
928 }
929
930 /* Given ARG1, a value of type (pointer to a)* structure/union,
931 extract the component named NAME from the ultimate target structure/union
932 and return it as a value with its appropriate type.
933 ERR is used in the error message if ARG1's type is wrong.
934
935 C++: ARGS is a list of argument types to aid in the selection of
936 an appropriate method. Also, handle derived types.
937
938 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
939 where the truthvalue of whether the function that was resolved was
940 a static member function or not.
941
942 ERR is an error message to be printed in case the field is not found. */
943
944 value
value_struct_elt(arg1,args,name,static_memfuncp,err)945 value_struct_elt (arg1, args, name, static_memfuncp, err)
946 register value arg1, *args;
947 char *name;
948 int *static_memfuncp;
949 char *err;
950 {
951 register struct type *t;
952 register int i;
953 int found = 0;
954
955 struct type *baseclass;
956
957 COERCE_ARRAY (arg1);
958
959 t = VALUE_TYPE (arg1);
960
961 /* Check for the usual case: we have pointer, target type is a struct
962 * and `name' is a legal field of the struct. In this case, we can
963 * just snarf the value of the field & not waste time while value_ind
964 * sucks over the entire struct. */
965 if (! args)
966 {
967 if (TYPE_CODE(t) == TYPE_CODE_PTR
968 && (TYPE_CODE((baseclass = TYPE_TARGET_TYPE(t))) == TYPE_CODE_STRUCT
969 || TYPE_CODE(baseclass) == TYPE_CODE_UNION)
970 && (i = type_field_index(baseclass, name)) >= 0)
971 {
972 register int offset;
973 register struct type *f = TYPE_FIELD_TYPE(baseclass, i);
974
975 offset = TYPE_FIELD_BITPOS(baseclass, i) >> 3;
976 if (TYPE_FIELD_BITSIZE(baseclass, i) == 0)
977 return value_at(f, (CORE_ADDR)(value_as_long(arg1) + offset));
978 }
979 }
980
981 /* Follow pointers until we get to a non-pointer. */
982
983 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
984 {
985 arg1 = value_ind (arg1);
986 COERCE_ARRAY (arg1);
987 t = VALUE_TYPE (arg1);
988 }
989
990 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
991 error ("not implemented: member type in value_struct_elt");
992
993 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
994 && TYPE_CODE (t) != TYPE_CODE_UNION)
995 error ("Attempt to extract a component of a value that is not a %s.", err);
996
997 baseclass = t;
998
999 /* Assume it's not, unless we see that it is. */
1000 if (static_memfuncp)
1001 *static_memfuncp =0;
1002
1003 if (!args)
1004 {
1005 /* if there are no arguments ...do this... */
1006
1007 /* Try as a variable first, because if we succeed, there
1008 is less work to be done. */
1009 while (t)
1010 {
1011 i = type_field_index(t, name);
1012 if (i >= 0)
1013 return TYPE_FIELD_STATIC (t, i)
1014 ? value_static_field (t, name, i) : value_field (arg1, i);
1015
1016 if (TYPE_N_BASECLASSES (t) == 0)
1017 break;
1018
1019 t = TYPE_BASECLASS (t, 1);
1020 VALUE_TYPE (arg1) = t; /* side effect! */
1021 }
1022
1023 /* C++: If it was not found as a data field, then try to
1024 return it as a pointer to a method. */
1025 t = baseclass;
1026 VALUE_TYPE (arg1) = t; /* side effect! */
1027
1028 if (destructor_name_p (name, t))
1029 error ("use `info method' command to print out value of destructor");
1030
1031 while (t)
1032 {
1033 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1034 {
1035 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1036 {
1037 error ("use `info method' command to print value of method \"%s\"", name);
1038 }
1039 }
1040
1041 if (TYPE_N_BASECLASSES (t) == 0)
1042 break;
1043
1044 t = TYPE_BASECLASS (t, 1);
1045 }
1046
1047 error ("There is no field named %s.", name);
1048 return 0;
1049 }
1050
1051 if (destructor_name_p (name, t))
1052 {
1053 if (!args[1])
1054 {
1055 /* destructors are a special case. */
1056 return (value)value_fn_field (arg1, 0,
1057 TYPE_FN_FIELDLIST_LENGTH (t, 0));
1058 }
1059 else
1060 {
1061 error ("destructor should not have any argument");
1062 }
1063 }
1064
1065 /* This following loop is for methods with arguments. */
1066 while (t)
1067 {
1068 /* Look up as method first, because that is where we
1069 expect to find it first. */
1070 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
1071 {
1072 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1073
1074 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1075 {
1076 int j;
1077 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1078
1079 found = 1;
1080 for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j)
1081 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
1082 TYPE_FN_FIELD_ARGS (f, j), args))
1083 {
1084 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1085 return (value)value_virtual_fn_field (arg1, f, j, t);
1086 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
1087 *static_memfuncp = 1;
1088 return (value)value_fn_field (arg1, i, j);
1089 }
1090 }
1091 }
1092
1093 if (TYPE_N_BASECLASSES (t) == 0)
1094 break;
1095
1096 t = TYPE_BASECLASS (t, 1);
1097 VALUE_TYPE (arg1) = t; /* side effect! */
1098 }
1099
1100 if (found)
1101 {
1102 error ("Structure method %s not defined for arglist.", name);
1103 return 0;
1104 }
1105 else
1106 {
1107 /* See if user tried to invoke data as function */
1108 t = baseclass;
1109 while (t)
1110 {
1111 i = type_field_index(t, name);
1112 if (i >= 0)
1113 return TYPE_FIELD_STATIC (t, i)
1114 ? value_static_field (t, name, i) : value_field (arg1, i);
1115
1116 if (TYPE_N_BASECLASSES (t) == 0)
1117 break;
1118
1119 t = TYPE_BASECLASS (t, 1);
1120 VALUE_TYPE (arg1) = t; /* side effect! */
1121 }
1122 error ("Structure has no component named %s.", name);
1123 }
1124 }
1125
1126 /* C++: return 1 is NAME is a legitimate name for the destructor
1127 of type TYPE. If TYPE does not have a destructor, or
1128 if NAME is inappropriate for TYPE, an error is signaled. */
1129 int
destructor_name_p(name,type)1130 destructor_name_p (name, type)
1131 char *name;
1132 struct type *type;
1133 {
1134 /* destructors are a special case. */
1135 char *dname = TYPE_NAME (type);
1136
1137 if (name[0] == '~')
1138 {
1139 if (! TYPE_HAS_DESTRUCTOR (type))
1140 error ("type `%s' does not have destructor defined",
1141 TYPE_NAME (type));
1142 /* Skip past the "struct " at the front. */
1143 while (*dname++ != ' ') ;
1144 if (strcmp (dname, name+1))
1145 error ("destructor specification error");
1146 else
1147 return 1;
1148 }
1149 return 0;
1150 }
1151
1152 /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
1153 return 1 if the component named NAME from the ultimate
1154 target structure/union is defined, otherwise, return 0. */
1155
1156 int
check_field(arg1,name)1157 check_field (arg1, name)
1158 register value arg1;
1159 char *name;
1160 {
1161 register struct type *t;
1162 register int i;
1163 int found = 0;
1164
1165 struct type *baseclass;
1166
1167 COERCE_ARRAY (arg1);
1168
1169 t = VALUE_TYPE (arg1);
1170
1171 /* Follow pointers until we get to a non-pointer. */
1172
1173 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1174 t = TYPE_TARGET_TYPE (t);
1175
1176 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1177 error ("not implemented: member type in check_field");
1178
1179 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1180 && TYPE_CODE (t) != TYPE_CODE_UNION)
1181 error ("Internal error: `this' is not an aggregate");
1182
1183 baseclass = t;
1184
1185 while (t)
1186 {
1187 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
1188 {
1189 char *t_field_name = TYPE_FIELD_NAME (t, i);
1190 if (t_field_name && !strcmp (t_field_name, name))
1191 goto success;
1192 }
1193 if (TYPE_N_BASECLASSES (t) == 0)
1194 break;
1195
1196 t = TYPE_BASECLASS (t, 1);
1197 VALUE_TYPE (arg1) = t; /* side effect! */
1198 }
1199
1200 /* C++: If it was not found as a data field, then try to
1201 return it as a pointer to a method. */
1202 t = baseclass;
1203
1204 /* Destructors are a special case. */
1205 if (destructor_name_p (name, t))
1206 goto success;
1207
1208 while (t)
1209 {
1210 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1211 {
1212 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1213 return 1;
1214 }
1215
1216 if (TYPE_N_BASECLASSES (t) == 0)
1217 break;
1218
1219 t = TYPE_BASECLASS (t, 1);
1220 }
1221 return 0;
1222
1223 success:
1224 t = VALUE_TYPE (arg1);
1225 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
1226 {
1227 arg1 = value_ind (arg1);
1228 COERCE_ARRAY (arg1);
1229 t = VALUE_TYPE (arg1);
1230 }
1231 }
1232
1233 /* C++: Given an aggregate type DOMAIN, and a member name NAME,
1234 return the address of this member as a pointer to member
1235 type. If INTYPE is non-null, then it will be the type
1236 of the member we are looking for. This will help us resolve
1237 pointers to member functions. */
1238
1239 value
value_struct_elt_for_address(domain,intype,name)1240 value_struct_elt_for_address (domain, intype, name)
1241 struct type *domain, *intype;
1242 char *name;
1243 {
1244 register struct type *t = domain;
1245 register int i;
1246 int found = 0;
1247 value v;
1248
1249 struct type *baseclass;
1250
1251 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1252 && TYPE_CODE (t) != TYPE_CODE_UNION)
1253 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1254
1255 baseclass = t;
1256
1257 while (t)
1258 {
1259 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
1260 {
1261 char *t_field_name = TYPE_FIELD_NAME (t, i);
1262 if (t_field_name && !strcmp (t_field_name, name))
1263 {
1264 if (TYPE_FIELD_PACKED (t, i))
1265 error ("pointers to bitfield members not allowed");
1266
1267 v = value_from_long (builtin_type_int,
1268 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1269 VALUE_TYPE (v) = lookup_pointer_type (
1270 lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
1271 return v;
1272 }
1273 }
1274
1275 if (TYPE_N_BASECLASSES (t) == 0)
1276 break;
1277
1278 t = TYPE_BASECLASS (t, 1);
1279 }
1280
1281 /* C++: If it was not found as a data field, then try to
1282 return it as a pointer to a method. */
1283 t = baseclass;
1284
1285 /* Destructors are a special case. */
1286 if (destructor_name_p (name, t))
1287 {
1288 error ("pointers to destructors not implemented yet");
1289 }
1290
1291 /* Perform all necessary dereferencing. */
1292 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1293 intype = TYPE_TARGET_TYPE (intype);
1294
1295 while (t)
1296 {
1297 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1298 {
1299 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1300 {
1301 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1302 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1303
1304 if (intype == 0 && j > 1)
1305 error ("non-unique member `%s' requires type instantiation", name);
1306 if (intype)
1307 {
1308 while (j--)
1309 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1310 break;
1311 if (j < 0)
1312 error ("no member function matches that type instantiation");
1313 }
1314 else
1315 j = 0;
1316
1317 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1318 {
1319 v = value_from_long (builtin_type_long,
1320 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
1321 }
1322 else
1323 {
1324 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
1325 0, VAR_NAMESPACE, 0);
1326 v = locate_var_value (s, 0);
1327 }
1328 VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1329 return v;
1330 }
1331 }
1332
1333 if (TYPE_N_BASECLASSES (t) == 0)
1334 break;
1335
1336 t = TYPE_BASECLASS (t, 1);
1337 }
1338 return 0;
1339 }
1340
1341 /* Compare two argument lists and return the position in which they differ,
1342 or zero if equal.
1343
1344 STATICP is nonzero if the T1 argument list came from a
1345 static member function.
1346
1347 For non-static member functions, we ignore the first argument,
1348 which is the type of the instance variable. This is because we want
1349 to handle calls with objects from derived classes. This is not
1350 entirely correct: we should actually check to make sure that a
1351 requested operation is type secure, shouldn't we? */
1352
1353 int
typecmp(staticp,t1,t2)1354 typecmp (staticp, t1, t2)
1355 int staticp;
1356 struct type *t1[];
1357 value t2[];
1358 {
1359 int i;
1360
1361 if (staticp && t1 == 0)
1362 return t2[1] != 0;
1363 if (t1 == 0)
1364 return 1;
1365 if (t1[0]->code == TYPE_CODE_VOID) return 0;
1366 if (t1[!staticp] == 0) return 0;
1367 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
1368 {
1369 if (! t2[i]
1370 || t1[i]->code != t2[i]->type->code
1371 || t1[i]->target_type != t2[i]->type->target_type)
1372 return i+1;
1373 }
1374 if (!t1[i]) return 0;
1375 return t2[i] ? i+1 : 0;
1376 }
1377
1378 /* C++: return the value of the class instance variable, if one exists.
1379 Flag COMPLAIN signals an error if the request is made in an
1380 inappropriate context. */
1381 value
value_of_this(complain)1382 value_of_this (complain)
1383 int complain;
1384 {
1385 extern FRAME selected_frame;
1386 struct symbol *func, *sym;
1387 char *funname = 0;
1388 struct block *b;
1389 int i;
1390
1391 if (selected_frame == 0)
1392 if (complain)
1393 error ("no frame selected");
1394 else return 0;
1395
1396 func = get_frame_function (selected_frame);
1397 if (func)
1398 funname = SYMBOL_NAME (func);
1399 else
1400 if (complain)
1401 error ("no `this' in nameless context");
1402 else return 0;
1403
1404 b = SYMBOL_BLOCK_VALUE (func);
1405 i = BLOCK_NSYMS (b);
1406 if (i <= 0)
1407 if (complain)
1408 error ("no args, no `this'");
1409 else return 0;
1410
1411 sym = BLOCK_SYM (b, 0);
1412 if (strncmp ("$this", SYMBOL_NAME (sym), 5))
1413 if (complain)
1414 error ("current stack frame not in method");
1415 else return 0;
1416
1417 return read_var_value (sym, selected_frame);
1418 }
1419