gdb-3.5
[deliverable/binutils-gdb.git] / gdb / valops.c
CommitLineData
7b4ac7e1 1/* Perform non-arithmetic operations on values, for GDB.
4187119d 2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6GDB is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GDB is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GDB; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
e91b87a3 20#include "stdio.h"
7b4ac7e1 21#include "defs.h"
7b4ac7e1 22#include "param.h"
23#include "symtab.h"
24#include "value.h"
e91b87a3 25#include "frame.h"
26#include "inferior.h"
7b4ac7e1 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
32value
33value_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)
e91b87a3 62 {
63 return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
64 }
7b4ac7e1 65 else
66 error ("Invalid cast.");
67}
68
4187119d 69/* Create a value of type TYPE that is zero, and return it. */
70
71value
72value_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
7b4ac7e1 84/* Return the value with a specified type located at specified address. */
85
86value
87value_at (type, addr)
88 struct type *type;
89 CORE_ADDR addr;
90{
91 register value val = allocate_value (type);
e91b87a3 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 }
7b4ac7e1 102
7b4ac7e1 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
112value
113value_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
e91b87a3 122 extern CORE_ADDR find_saved_register ();
123
7b4ac7e1 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. */
e91b87a3 133
7b4ac7e1 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);
e91b87a3 166 modify_field (&val, (int) value_as_long (fromval),
7b4ac7e1 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);
e91b87a3 186 modify_field (&val, (int) value_as_long (fromval),
7b4ac7e1 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
e91b87a3 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
7b4ac7e1 269 default:
270 error ("Left side of = operation is not an lvalue.");
271 }
272
4187119d 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 }
7b4ac7e1 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));
4187119d 285 VALUE_TYPE (val) = type;
286
7b4ac7e1 287 return val;
288}
289
290/* Extend a value VAL to COUNT repetitions of its type. */
291
292value
293value_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
315value
316value_of_variable (var)
317 struct symbol *var;
318{
4187119d 319 return read_var_value (var, (FRAME) 0);
7b4ac7e1 320}
321
322/* Given a value which is an array, return a value which is
323 a pointer to its first element. */
324
325value
326value_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,
e91b87a3 346 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
7b4ac7e1 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
353value
354value_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,
e91b87a3 373 (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
7b4ac7e1 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
380value
381value_ind (arg1)
382 value arg1;
383{
bb7592f0 384 /* Must do this before COERCE_ARRAY, otherwise an infinite loop
e91b87a3 385 will result */
bb7592f0 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
7b4ac7e1 390 COERCE_ARRAY (arg1);
391
bb7592f0 392 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
393 error ("not implemented: member types in value_ind");
394
1c997a4a 395 /* Allow * on an integer so we can cast it to whatever we want.
396 This returns an int, which seems like the most C-like thing
397 to do. "long long" variables are rare enough that
398 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
7b4ac7e1 399 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
1c997a4a 400 return value_at (builtin_type_int,
7b4ac7e1 401 (CORE_ADDR) value_as_long (arg1));
402 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
403 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
404 (CORE_ADDR) value_as_long (arg1));
bb7592f0 405 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF)
406 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
407 (CORE_ADDR) value_as_long (arg1));
7b4ac7e1 408 error ("Attempt to take contents of a non-pointer value.");
409}
410\f
411/* Pushing small parts of stack frames. */
412
413/* Push one word (the size of object that a register holds). */
414
415CORE_ADDR
416push_word (sp, buffer)
417 CORE_ADDR sp;
418 REGISTER_TYPE buffer;
419{
420 register int len = sizeof (REGISTER_TYPE);
421
422#if 1 INNER_THAN 2
423 sp -= len;
424 write_memory (sp, &buffer, len);
425#else /* stack grows upward */
426 write_memory (sp, &buffer, len);
427 sp += len;
428#endif /* stack grows upward */
429
430 return sp;
431}
432
433/* Push LEN bytes with data at BUFFER. */
434
435CORE_ADDR
436push_bytes (sp, buffer, len)
437 CORE_ADDR sp;
438 char *buffer;
439 int len;
440{
441#if 1 INNER_THAN 2
442 sp -= len;
443 write_memory (sp, buffer, len);
444#else /* stack grows upward */
445 write_memory (sp, buffer, len);
446 sp += len;
447#endif /* stack grows upward */
448
449 return sp;
450}
451
452/* Push onto the stack the specified value VALUE. */
453
454CORE_ADDR
455value_push (sp, arg)
456 register CORE_ADDR sp;
457 value arg;
458{
459 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
460
461#if 1 INNER_THAN 2
462 sp -= len;
463 write_memory (sp, VALUE_CONTENTS (arg), len);
464#else /* stack grows upward */
465 write_memory (sp, VALUE_CONTENTS (arg), len);
466 sp += len;
467#endif /* stack grows upward */
468
469 return sp;
470}
471
472/* Perform the standard coercions that are specified
473 for arguments to be passed to C functions. */
474
475value
476value_arg_coerce (arg)
477 value arg;
478{
479 register struct type *type;
480
481 COERCE_ENUM (arg);
482
483 type = VALUE_TYPE (arg);
484
485 if (TYPE_CODE (type) == TYPE_CODE_INT
486 && TYPE_LENGTH (type) < sizeof (int))
487 return value_cast (builtin_type_int, arg);
488
489 if (type == builtin_type_float)
490 return value_cast (builtin_type_double, arg);
491
492 return arg;
493}
494
495/* Push the value ARG, first coercing it as an argument
496 to a C function. */
497
498CORE_ADDR
499value_arg_push (sp, arg)
500 register CORE_ADDR sp;
501 value arg;
502{
503 return value_push (sp, value_arg_coerce (arg));
504}
505
506/* Perform a function call in the inferior.
507 ARGS is a vector of values of arguments (NARGS of them).
508 FUNCTION is a value, the function to be called.
509 Returns a value representing what the function returned.
510 May fail to return, if a breakpoint or signal is hit
511 during the execution of the function. */
512
513value
514call_function (function, nargs, args)
515 value function;
516 int nargs;
517 value *args;
518{
519 register CORE_ADDR sp;
520 register int i;
521 CORE_ADDR start_sp;
522 static REGISTER_TYPE dummy[] = CALL_DUMMY;
523 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
524 CORE_ADDR old_sp;
525 struct type *value_type;
e91b87a3 526 unsigned char struct_return;
527 CORE_ADDR struct_addr;
528 struct inferior_status inf_status;
529 struct cleanup *old_chain;
530
4187119d 531 if (!have_inferior_p ())
532 error ("Cannot invoke functions if the inferior is not running.");
533
e91b87a3 534 save_inferior_status (&inf_status, 1);
535 old_chain = make_cleanup (restore_inferior_status, &inf_status);
7b4ac7e1 536
7a67dd45 537 /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
538 (and POP_FRAME for restoring them). (At least on most machines)
539 they are saved on the stack in the inferior. */
7b4ac7e1 540 PUSH_DUMMY_FRAME;
541
bb7592f0 542 old_sp = sp = read_register (SP_REGNUM);
543
e91b87a3 544#if 1 INNER_THAN 2 /* Stack grows down */
bb7592f0 545 sp -= sizeof dummy;
546 start_sp = sp;
e91b87a3 547#else /* Stack grows up */
bb7592f0 548 start_sp = sp;
549 sp += sizeof dummy;
550#endif
551
7b4ac7e1 552 {
553 register CORE_ADDR funaddr;
554 register struct type *ftype = VALUE_TYPE (function);
555 register enum type_code code = TYPE_CODE (ftype);
556
bb7592f0 557 /* If it's a member function, just look at the function
558 part of it. */
bb7592f0 559
7b4ac7e1 560 /* Determine address to call. */
4187119d 561 if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
7b4ac7e1 562 {
563 funaddr = VALUE_ADDRESS (function);
564 value_type = TYPE_TARGET_TYPE (ftype);
565 }
566 else if (code == TYPE_CODE_PTR)
567 {
568 funaddr = value_as_long (function);
4187119d 569 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
570 || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
7b4ac7e1 571 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
572 else
573 value_type = builtin_type_int;
574 }
575 else if (code == TYPE_CODE_INT)
576 {
577 /* Handle the case of functions lacking debugging info.
578 Their values are characters since their addresses are char */
579 if (TYPE_LENGTH (ftype) == 1)
580 funaddr = value_as_long (value_addr (function));
581 else
582 /* Handle integer used as address of a function. */
583 funaddr = value_as_long (function);
584
585 value_type = builtin_type_int;
586 }
587 else
588 error ("Invalid data type for function to be called.");
589
e91b87a3 590 /* Are we returning a value using a structure return or a normal
591 value return? */
592
593 struct_return = using_struct_return (function, funaddr, value_type);
594
7b4ac7e1 595 /* Create a call sequence customized for this function
596 and the number of arguments for it. */
597 bcopy (dummy, dummy1, sizeof dummy);
bb7592f0 598 FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, value_type);
7b4ac7e1 599 }
600
4187119d 601#ifndef CANNOT_EXECUTE_STACK
bb7592f0 602 write_memory (start_sp, dummy1, sizeof dummy);
603
4187119d 604#else
e91b87a3 605 /* Convex Unix prohibits executing in the stack segment. */
606 /* Hope there is empty room at the top of the text segment. */
607 {
608 extern CORE_ADDR text_end;
609 static checked = 0;
610 if (!checked)
611 for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
612 if (read_memory_integer (start_sp, 1) != 0)
613 error ("text segment full -- no place to put call");
614 checked = 1;
4187119d 615 sp = old_sp;
e91b87a3 616 start_sp = text_end - sizeof dummy;
617 write_memory (start_sp, dummy1, sizeof dummy);
618 }
4187119d 619#endif /* CANNOT_EXECUTE_STACK */
bb7592f0 620#ifdef STACK_ALIGN
e91b87a3 621 /* If stack grows down, we must leave a hole at the top. */
bb7592f0 622 {
623 int len = 0;
e91b87a3 624
625 /* Reserve space for the return structure to be written on the
626 stack, if necessary */
627
628 if (struct_return)
629 len += TYPE_LENGTH (value_type);
630
bb7592f0 631 for (i = nargs - 1; i >= 0; i--)
4187119d 632 len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
e91b87a3 633#ifdef CALL_DUMMY_STACK_ADJUST
bb7592f0 634 len += CALL_DUMMY_STACK_ADJUST;
e91b87a3 635#endif
bb7592f0 636#if 1 INNER_THAN 2
637 sp -= STACK_ALIGN (len) - len;
638#else
639 sp += STACK_ALIGN (len) - len;
640#endif
641 }
e91b87a3 642#endif /* STACK_ALIGN */
643
644 /* Reserve space for the return structure to be written on the
645 stack, if necessary */
7b4ac7e1 646
e91b87a3 647 if (struct_return)
648 {
649#if 1 INNER_THAN 2
650 sp -= TYPE_LENGTH (value_type);
651 struct_addr = sp;
652#else
653 struct_addr = sp;
654 sp += TYPE_LENGTH (value_type);
655#endif
656 }
657
7b4ac7e1 658 for (i = nargs - 1; i >= 0; i--)
659 sp = value_arg_push (sp, args[i]);
bb7592f0 660
661#ifdef CALL_DUMMY_STACK_ADJUST
662#if 1 INNER_THAN 2
e91b87a3 663 sp -= CALL_DUMMY_STACK_ADJUST;
bb7592f0 664#else
e91b87a3 665 sp += CALL_DUMMY_STACK_ADJUST;
bb7592f0 666#endif
e91b87a3 667#endif /* CALL_DUMMY_STACK_ADJUST */
e91b87a3 668
669 /* Store the address at which the structure is supposed to be
670 written. Note that this (and the code which reserved the space
671 above) assumes that gcc was used to compile this function. Since
672 it doesn't cost us anything but space and if the function is pcc
673 it will ignore this value, we will make that assumption.
674
675 Also note that on some machines (like the sparc) pcc uses this
676 convention in a slightly twisted way also. */
7b4ac7e1 677
e91b87a3 678 if (struct_return)
679 STORE_STRUCT_RETURN (struct_addr, sp);
680
681 /* Write the stack pointer. This is here because the statement above
682 might fool with it */
7b4ac7e1 683 write_register (SP_REGNUM, sp);
684
685 /* Figure out the value returned by the function. */
686 {
687 char retbuf[REGISTER_BYTES];
688
689 /* Execute the stack dummy routine, calling FUNCTION.
690 When it is done, discard the empty frame
691 after storing the contents of all regs into retbuf. */
692 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
693
e91b87a3 694 do_cleanups (old_chain);
695
696 return value_being_returned (value_type, retbuf, struct_return);
7b4ac7e1 697 }
698}
699\f
700/* Create a value for a string constant:
701 Call the function malloc in the inferior to get space for it,
702 then copy the data into that space
703 and then return the address with type char *.
704 PTR points to the string constant data; LEN is number of characters. */
705
706value
707value_string (ptr, len)
708 char *ptr;
709 int len;
710{
711 register value val;
712 register struct symbol *sym;
713 value blocklen;
714 register char *copy = (char *) alloca (len + 1);
715 char *i = ptr;
716 register char *o = copy, *ibeg = ptr;
717 register int c;
718
719 /* Copy the string into COPY, processing escapes.
720 We could not conveniently process them in expread
721 because the string there wants to be a substring of the input. */
722
723 while (i - ibeg < len)
724 {
725 c = *i++;
726 if (c == '\\')
727 {
728 c = parse_escape (&i);
729 if (c == -1)
730 continue;
731 }
732 *o++ = c;
733 }
734 *o = 0;
735
736 /* Get the length of the string after escapes are processed. */
737
738 len = o - copy;
739
740 /* Find the address of malloc in the inferior. */
741
e91b87a3 742 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0);
7b4ac7e1 743 if (sym != 0)
744 {
745 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
746 error ("\"malloc\" exists in this program but is not a function.");
747 val = value_of_variable (sym);
748 }
749 else
750 {
751 register int i;
752 for (i = 0; i < misc_function_count; i++)
753 if (!strcmp (misc_function_vector[i].name, "malloc"))
754 break;
755 if (i < misc_function_count)
756 val = value_from_long (builtin_type_long,
e91b87a3 757 (LONGEST) misc_function_vector[i].address);
7b4ac7e1 758 else
759 error ("String constants require the program to have a function \"malloc\".");
760 }
761
e91b87a3 762 blocklen = value_from_long (builtin_type_int, (LONGEST) (len + 1));
7b4ac7e1 763 val = call_function (val, 1, &blocklen);
764 if (value_zerop (val))
765 error ("No memory available for string constant.");
e91b87a3 766 write_memory ((CORE_ADDR) value_as_long (val), copy, len + 1);
7b4ac7e1 767 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
768 return val;
769}
770\f
771/* Given ARG1, a value of type (pointer to a)* structure/union,
772 extract the component named NAME from the ultimate target structure/union
773 and return it as a value with its appropriate type.
bb7592f0 774 ERR is used in the error message if ARG1's type is wrong.
775
776 C++: ARGS is a list of argument types to aid in the selection of
777 an appropriate method. Also, handle derived types.
778
4187119d 779 STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
780 where the truthvalue of whether the function that was resolved was
781 a static member function or not.
782
bb7592f0 783 ERR is an error message to be printed in case the field is not found. */
784
7b4ac7e1 785value
4187119d 786value_struct_elt (arg1, args, name, static_memfuncp, err)
bb7592f0 787 register value arg1, *args;
7b4ac7e1 788 char *name;
4187119d 789 int *static_memfuncp;
7b4ac7e1 790 char *err;
791{
792 register struct type *t;
793 register int i;
bb7592f0 794 int found = 0;
795
796 struct type *baseclass;
7b4ac7e1 797
798 COERCE_ARRAY (arg1);
799
800 t = VALUE_TYPE (arg1);
801
802 /* Follow pointers until we get to a non-pointer. */
803
bb7592f0 804 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
7b4ac7e1 805 {
806 arg1 = value_ind (arg1);
807 COERCE_ARRAY (arg1);
808 t = VALUE_TYPE (arg1);
809 }
810
bb7592f0 811 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
812 error ("not implemented: member type in value_struct_elt");
813
7b4ac7e1 814 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
e91b87a3 815 && TYPE_CODE (t) != TYPE_CODE_UNION)
7b4ac7e1 816 error ("Attempt to extract a component of a value that is not a %s.", err);
817
bb7592f0 818 baseclass = t;
819
4187119d 820 /* Assume it's not, unless we see that it is. */
821 if (static_memfuncp)
822 *static_memfuncp =0;
823
bb7592f0 824 if (!args)
e91b87a3 825 {
4187119d 826 /* if there are no arguments ...do this... */
bb7592f0 827
4187119d 828 /* Try as a variable first, because if we succeed, there
829 is less work to be done. */
bb7592f0 830 while (t)
831 {
832 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
833 {
4187119d 834 char *t_field_name = TYPE_FIELD_NAME (t, i);
835 if (t_field_name && !strcmp (t_field_name, name))
bb7592f0 836 {
837 found = 1;
838 break;
839 }
840 }
e91b87a3 841
bb7592f0 842 if (i >= 0)
843 return TYPE_FIELD_STATIC (t, i)
844 ? value_static_field (t, name, i) : value_field (arg1, i);
845
846 if (TYPE_N_BASECLASSES (t) == 0)
847 break;
848
849 t = TYPE_BASECLASS (t, 1);
850 VALUE_TYPE (arg1) = t; /* side effect! */
851 }
852
853 /* C++: If it was not found as a data field, then try to
e91b87a3 854 return it as a pointer to a method. */
bb7592f0 855 t = baseclass;
856 VALUE_TYPE (arg1) = t; /* side effect! */
857
858 if (destructor_name_p (name, t))
859 error ("use `info method' command to print out value of destructor");
860
861 while (t)
862 {
863 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
864 {
865 if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
866 {
867 error ("use `info method' command to print value of method \"%s\"", name);
868 }
869 }
870
871 if (TYPE_N_BASECLASSES (t) == 0)
872 break;
873
874 t = TYPE_BASECLASS (t, 1);
875 }
876
877 if (found == 0)
7a67dd45 878 error ("There is no field named %s.", name);
bb7592f0 879 return 0;
880 }
881
882 if (destructor_name_p (name, t))
883 {
884 if (!args[1])
885 {
886 /* destructors are a special case. */
e91b87a3 887 return (value)value_fn_field (arg1, 0,
888 TYPE_FN_FIELDLIST_LENGTH (t, 0));
bb7592f0 889 }
890 else
891 {
892 error ("destructor should not have any argument");
893 }
894 }
895
896 /* This following loop is for methods with arguments. */
897 while (t)
898 {
899 /* Look up as method first, because that is where we
900 expect to find it first. */
901 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
902 {
903 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
904
905 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
906 {
907 int j;
908 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
909
910 found = 1;
911 for (j = TYPE_FN_FIELDLIST_LENGTH (t, i) - 1; j >= 0; --j)
4187119d 912 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
913 TYPE_FN_FIELD_ARGS (f, j), args))
914 {
915 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
916 return (value)value_virtual_fn_field (arg1, f, j, t);
917 if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
918 *static_memfuncp = 1;
919 return (value)value_fn_field (arg1, i, j);
920 }
bb7592f0 921 }
922 }
923
924 if (TYPE_N_BASECLASSES (t) == 0)
925 break;
e91b87a3 926
bb7592f0 927 t = TYPE_BASECLASS (t, 1);
928 VALUE_TYPE (arg1) = t; /* side effect! */
929 }
930
931 if (found)
932 {
933 error ("Structure method %s not defined for arglist.", name);
934 return 0;
935 }
936 else
937 {
938 /* See if user tried to invoke data as function */
939 t = baseclass;
940 while (t)
941 {
942 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
943 {
4187119d 944 char *t_field_name = TYPE_FIELD_NAME (t, i);
945 if (t_field_name && !strcmp (t_field_name, name))
bb7592f0 946 {
947 found = 1;
948 break;
949 }
950 }
e91b87a3 951
bb7592f0 952 if (i >= 0)
953 return TYPE_FIELD_STATIC (t, i)
954 ? value_static_field (t, name, i) : value_field (arg1, i);
955
956 if (TYPE_N_BASECLASSES (t) == 0)
957 break;
958
959 t = TYPE_BASECLASS (t, 1);
960 VALUE_TYPE (arg1) = t; /* side effect! */
961 }
962 error ("Structure has no component named %s.", name);
963 }
964}
965
966/* C++: return 1 is NAME is a legitimate name for the destructor
967 of type TYPE. If TYPE does not have a destructor, or
968 if NAME is inappropriate for TYPE, an error is signaled. */
969int
970destructor_name_p (name, type)
971 char *name;
972 struct type *type;
973{
974 /* destructors are a special case. */
975 char *dname = TYPE_NAME (type);
976
977 if (name[0] == '~')
978 {
979 if (! TYPE_HAS_DESTRUCTOR (type))
980 error ("type `%s' does not have destructor defined",
981 TYPE_NAME (type));
982 /* Skip past the "struct " at the front. */
983 while (*dname++ != ' ') ;
984 if (strcmp (dname, name+1))
985 error ("destructor specification error");
986 else
987 return 1;
988 }
989 return 0;
990}
991
992/* C++: Given ARG1, a value of type (pointer to a)* structure/union,
993 return 1 if the component named NAME from the ultimate
994 target structure/union is defined, otherwise, return 0. */
e91b87a3 995
bb7592f0 996int
997check_field (arg1, name)
998 register value arg1;
999 char *name;
1000{
1001 register struct type *t;
1002 register int i;
1003 int found = 0;
1004
1005 struct type *baseclass;
1006
1007 COERCE_ARRAY (arg1);
1008
1009 t = VALUE_TYPE (arg1);
1010
1011 /* Follow pointers until we get to a non-pointer. */
1012
1013 while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
7a67dd45 1014 t = TYPE_TARGET_TYPE (t);
bb7592f0 1015
1016 if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
1017 error ("not implemented: member type in check_field");
1018
1019 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1020 && TYPE_CODE (t) != TYPE_CODE_UNION)
1021 error ("Internal error: `this' is not an aggregate");
1022
1023 baseclass = t;
1024
1025 while (t)
7b4ac7e1 1026 {
bb7592f0 1027 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
1028 {
4187119d 1029 char *t_field_name = TYPE_FIELD_NAME (t, i);
1030 if (t_field_name && !strcmp (t_field_name, name))
bb7592f0 1031 {
1032 return 1;
1033 }
1034 }
bb7592f0 1035 if (TYPE_N_BASECLASSES (t) == 0)
3bf57d21 1036 break;
bb7592f0 1037
1038 t = TYPE_BASECLASS (t, 1);
7b4ac7e1 1039 }
1040
bb7592f0 1041 /* C++: If it was not found as a data field, then try to
e91b87a3 1042 return it as a pointer to a method. */
bb7592f0 1043 t = baseclass;
1044 VALUE_TYPE (arg1) = t; /* side effect! */
7b4ac7e1 1045
bb7592f0 1046 /* Destructors are a special case. */
1047 if (destructor_name_p (name, t))
1048 return 1;
1049
1050 while (t)
1051 {
1052 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1053 {
1054 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1055 return 1;
1056 }
1057
1058 if (TYPE_N_BASECLASSES (t) == 0)
1059 break;
1060
1061 t = TYPE_BASECLASS (t, 1);
1062 }
1063 return 0;
1064}
1065
1066/* C++: Given an aggregate type DOMAIN, and a member name NAME,
1067 return the address of this member as a pointer to member
1068 type. If INTYPE is non-null, then it will be the type
1069 of the member we are looking for. This will help us resolve
1070 pointers to member functions. */
e91b87a3 1071
bb7592f0 1072value
1073value_struct_elt_for_address (domain, intype, name)
1074 struct type *domain, *intype;
1075 char *name;
1076{
1077 register struct type *t = domain;
1078 register int i;
1079 int found = 0;
1080 value v;
1081
1082 struct type *baseclass;
1083
1084 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
1085 && TYPE_CODE (t) != TYPE_CODE_UNION)
1086 error ("Internal error: non-aggregate type to value_struct_elt_for_address");
1087
1088 baseclass = t;
1089
1090 while (t)
1091 {
1092 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
1093 {
4187119d 1094 char *t_field_name = TYPE_FIELD_NAME (t, i);
1095 if (t_field_name && !strcmp (t_field_name, name))
bb7592f0 1096 {
1097 if (TYPE_FIELD_PACKED (t, i))
1098 error ("pointers to bitfield members not allowed");
1099
e91b87a3 1100 v = value_from_long (builtin_type_int,
1101 (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
1102 VALUE_TYPE (v) = lookup_pointer_type (
1103 lookup_member_type (TYPE_FIELD_TYPE (t, i), baseclass));
bb7592f0 1104 return v;
1105 }
1106 }
1107
1108 if (TYPE_N_BASECLASSES (t) == 0)
1109 break;
1110
1111 t = TYPE_BASECLASS (t, 1);
1112 }
1113
1114 /* C++: If it was not found as a data field, then try to
e91b87a3 1115 return it as a pointer to a method. */
bb7592f0 1116 t = baseclass;
1117
1118 /* Destructors are a special case. */
1119 if (destructor_name_p (name, t))
1120 {
1121 error ("pointers to destructors not implemented yet");
1122 }
1123
1124 /* Perform all necessary dereferencing. */
1125 while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
1126 intype = TYPE_TARGET_TYPE (intype);
1127
1128 while (t)
1129 {
1130 for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
1131 {
1132 if (!strcmp (TYPE_FN_FIELDLIST_NAME (t, i), name))
1133 {
1134 int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
1135 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
1136
1137 if (intype == 0 && j > 1)
1138 error ("non-unique member `%s' requires type instantiation", name);
1139 if (intype)
1140 {
1141 while (j--)
1142 if (TYPE_FN_FIELD_TYPE (f, j) == intype)
1143 break;
1144 if (j < 0)
1145 error ("no member function matches that type instantiation");
1146 }
1147 else
1148 j = 0;
1149
1150 if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
1151 {
1152 v = value_from_long (builtin_type_long,
e91b87a3 1153 (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
bb7592f0 1154 }
1155 else
1156 {
1157 struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
e91b87a3 1158 0, VAR_NAMESPACE, 0);
bb7592f0 1159 v = locate_var_value (s, 0);
1160 }
1161 VALUE_TYPE (v) = lookup_pointer_type (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), baseclass));
1162 return v;
1163 }
1164 }
1165
1166 if (TYPE_N_BASECLASSES (t) == 0)
1167 break;
1168
1169 t = TYPE_BASECLASS (t, 1);
1170 }
1171 return 0;
1172}
1173
1174/* Compare two argument lists and return the position in which they differ,
4187119d 1175 or zero if equal.
1176
1177 STATICP is nonzero if the T1 argument list came from a
1178 static member function.
1179
1180 For non-static member functions, we ignore the first argument,
1181 which is the type of the instance variable. This is because we want
1182 to handle calls with objects from derived classes. This is not
1183 entirely correct: we should actually check to make sure that a
1184 requested operation is type secure, shouldn't we? */
1185
1186int
1187typecmp (staticp, t1, t2)
1188 int staticp;
bb7592f0 1189 struct type *t1[];
1190 value t2[];
1191{
1192 int i;
e91b87a3 1193
4187119d 1194 if (staticp && t1 == 0)
1195 return t2[1] != 0;
1196 if (t1 == 0)
1197 return 1;
bb7592f0 1198 if (t1[0]->code == TYPE_CODE_VOID) return 0;
4187119d 1199 if (t1[!staticp] == 0) return 0;
1200 for (i = !staticp; t1[i] && t1[i]->code != TYPE_CODE_VOID; i++)
bb7592f0 1201 {
1202 if (! t2[i]
1203 || t1[i]->code != t2[i]->type->code
1204 || t1[i]->target_type != t2[i]->type->target_type)
4187119d 1205 return i+1;
bb7592f0 1206 }
1207 if (!t1[i]) return 0;
1208 return t2[i] ? i+1 : 0;
1209}
1210
bb7592f0 1211/* C++: return the value of the class instance variable, if one exists.
1212 Flag COMPLAIN signals an error if the request is made in an
1213 inappropriate context. */
1214value
1215value_of_this (complain)
1216 int complain;
1217{
1218 extern FRAME selected_frame;
1219 struct symbol *func, *sym;
1220 char *funname = 0;
1221 struct block *b;
1222 int i;
1223
1224 if (selected_frame == 0)
1225 if (complain)
1226 error ("no frame selected");
1227 else return 0;
1228
1229 func = get_frame_function (selected_frame);
1230 if (func)
1231 funname = SYMBOL_NAME (func);
1232 else
1233 if (complain)
1234 error ("no `this' in nameless context");
1235 else return 0;
1236
1237 b = SYMBOL_BLOCK_VALUE (func);
1238 i = BLOCK_NSYMS (b);
1239 if (i <= 0)
1240 if (complain)
1241 error ("no args, no `this'");
1242 else return 0;
1243
1244 sym = BLOCK_SYM (b, 0);
1245 if (strncmp ("$this", SYMBOL_NAME (sym), 5))
1246 if (complain)
1247 error ("current stack frame not in method");
1248 else return 0;
1249
1250 return read_var_value (sym, selected_frame);
7b4ac7e1 1251}
This page took 0.101317 seconds and 4 git commands to generate.