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