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