Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Perform non-arithmetic operations on values, for GDB. |
2b576293 | 2 | Copyright 1986, 1987, 1989, 1991, 1992, 1993, 1994, 1995 |
67e9b3b3 | 3 | Free Software Foundation, Inc. |
bd5635a1 RP |
4 | |
5 | This file is part of GDB. | |
6 | ||
06b6c733 | 7 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 8 | it under the terms of the GNU General Public License as published by |
06b6c733 JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
bd5635a1 | 11 | |
06b6c733 | 12 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
06b6c733 | 18 | along with this program; if not, write to the Free Software |
b4680522 | 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
bd5635a1 | 20 | |
bd5635a1 | 21 | #include "defs.h" |
bd5635a1 | 22 | #include "symtab.h" |
01be6913 | 23 | #include "gdbtypes.h" |
bd5635a1 RP |
24 | #include "value.h" |
25 | #include "frame.h" | |
26 | #include "inferior.h" | |
27 | #include "gdbcore.h" | |
28 | #include "target.h" | |
2e4964ad | 29 | #include "demangle.h" |
54023465 | 30 | #include "language.h" |
bd5635a1 RP |
31 | |
32 | #include <errno.h> | |
2b576293 | 33 | #include "gdb_string.h" |
bd5635a1 | 34 | |
75225aa2 FF |
35 | /* Default to coercing float to double in function calls only when there is |
36 | no prototype. Otherwise on targets where the debug information is incorrect | |
37 | for either the prototype or non-prototype case, we can force it by defining | |
38 | COERCE_FLOAT_TO_DOUBLE in the target configuration file. */ | |
39 | ||
40 | #ifndef COERCE_FLOAT_TO_DOUBLE | |
41 | #define COERCE_FLOAT_TO_DOUBLE (param_type == NULL) | |
42 | #endif | |
43 | ||
bd5635a1 | 44 | /* Local functions. */ |
01be6913 | 45 | |
a91a6192 | 46 | static int typecmp PARAMS ((int staticp, struct type *t1[], value_ptr t2[])); |
01be6913 | 47 | |
a91a6192 | 48 | static CORE_ADDR find_function_addr PARAMS ((value_ptr, struct type **)); |
01be6913 | 49 | |
3f550b59 | 50 | #ifndef PUSH_ARGUMENTS |
a91a6192 | 51 | static CORE_ADDR value_push PARAMS ((CORE_ADDR, value_ptr)); |
3f550b59 | 52 | #endif |
01be6913 | 53 | |
a91a6192 SS |
54 | static value_ptr search_struct_field PARAMS ((char *, value_ptr, int, |
55 | struct type *, int)); | |
01be6913 | 56 | |
a91a6192 SS |
57 | static value_ptr search_struct_method PARAMS ((char *, value_ptr *, |
58 | value_ptr *, | |
59 | int, int *, struct type *)); | |
01be6913 | 60 | |
a91a6192 | 61 | static int check_field_in PARAMS ((struct type *, const char *)); |
a163ddec | 62 | |
a91a6192 | 63 | static CORE_ADDR allocate_space_in_inferior PARAMS ((int)); |
9ed8604f | 64 | |
5222ca60 | 65 | static value_ptr cast_into_complex PARAMS ((struct type *, value_ptr)); |
9ed8604f | 66 | |
b607efe7 FF |
67 | static value_ptr value_arg_coerce PARAMS ((value_ptr, struct type *)); |
68 | ||
9ed8604f PS |
69 | #define VALUE_SUBSTRING_START(VAL) VALUE_FRAME(VAL) |
70 | ||
5e548861 PB |
71 | /* Flag for whether we want to abandon failed expression evals by default. */ |
72 | ||
b52cac6b | 73 | #if 0 |
5e548861 | 74 | static int auto_abandon = 0; |
b52cac6b | 75 | #endif |
5e548861 | 76 | |
bd5635a1 | 77 | \f |
09af5868 | 78 | /* Find the address of function name NAME in the inferior. */ |
a163ddec | 79 | |
09af5868 PS |
80 | value_ptr |
81 | find_function_in_inferior (name) | |
82 | char *name; | |
a163ddec | 83 | { |
a163ddec | 84 | register struct symbol *sym; |
09af5868 | 85 | sym = lookup_symbol (name, 0, VAR_NAMESPACE, 0, NULL); |
a163ddec MT |
86 | if (sym != NULL) |
87 | { | |
88 | if (SYMBOL_CLASS (sym) != LOC_BLOCK) | |
89 | { | |
09af5868 PS |
90 | error ("\"%s\" exists in this program but is not a function.", |
91 | name); | |
a163ddec | 92 | } |
09af5868 | 93 | return value_of_variable (sym, NULL); |
a163ddec MT |
94 | } |
95 | else | |
96 | { | |
09af5868 | 97 | struct minimal_symbol *msymbol = lookup_minimal_symbol(name, NULL, NULL); |
a163ddec MT |
98 | if (msymbol != NULL) |
99 | { | |
09af5868 PS |
100 | struct type *type; |
101 | LONGEST maddr; | |
a163ddec MT |
102 | type = lookup_pointer_type (builtin_type_char); |
103 | type = lookup_function_type (type); | |
104 | type = lookup_pointer_type (type); | |
105 | maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol); | |
09af5868 | 106 | return value_from_longest (type, maddr); |
a163ddec MT |
107 | } |
108 | else | |
109 | { | |
09af5868 | 110 | error ("evaluation of this expression requires the program to have a function \"%s\".", name); |
a163ddec MT |
111 | } |
112 | } | |
09af5868 PS |
113 | } |
114 | ||
115 | /* Allocate NBYTES of space in the inferior using the inferior's malloc | |
116 | and return a value that is a pointer to the allocated space. */ | |
117 | ||
118 | value_ptr | |
119 | value_allocate_space_in_inferior (len) | |
120 | int len; | |
121 | { | |
122 | value_ptr blocklen; | |
123 | register value_ptr val = find_function_in_inferior ("malloc"); | |
a163ddec MT |
124 | |
125 | blocklen = value_from_longest (builtin_type_int, (LONGEST) len); | |
126 | val = call_function_by_hand (val, 1, &blocklen); | |
127 | if (value_logical_not (val)) | |
128 | { | |
129 | error ("No memory available to program."); | |
130 | } | |
09af5868 PS |
131 | return val; |
132 | } | |
133 | ||
134 | static CORE_ADDR | |
135 | allocate_space_in_inferior (len) | |
136 | int len; | |
137 | { | |
138 | return value_as_long (value_allocate_space_in_inferior (len)); | |
a163ddec MT |
139 | } |
140 | ||
bd5635a1 RP |
141 | /* Cast value ARG2 to type TYPE and return as a value. |
142 | More general than a C cast: accepts any two types of the same length, | |
143 | and if ARG2 is an lvalue it can be cast into anything at all. */ | |
54023465 | 144 | /* In C++, casts may change pointer or object representations. */ |
bd5635a1 | 145 | |
a91a6192 | 146 | value_ptr |
bd5635a1 RP |
147 | value_cast (type, arg2) |
148 | struct type *type; | |
a91a6192 | 149 | register value_ptr arg2; |
bd5635a1 | 150 | { |
5e548861 | 151 | register enum type_code code1; |
bd5635a1 RP |
152 | register enum type_code code2; |
153 | register int scalar; | |
5e548861 | 154 | struct type *type2; |
bd5635a1 | 155 | |
f91a9e05 PB |
156 | if (VALUE_TYPE (arg2) == type) |
157 | return arg2; | |
158 | ||
5e548861 PB |
159 | CHECK_TYPEDEF (type); |
160 | code1 = TYPE_CODE (type); | |
f7a69ed7 | 161 | COERCE_REF(arg2); |
5e548861 | 162 | type2 = check_typedef (VALUE_TYPE (arg2)); |
13ffa6be JL |
163 | |
164 | /* A cast to an undetermined-length array_type, such as (TYPE [])OBJECT, | |
165 | is treated like a cast to (TYPE [N])OBJECT, | |
166 | where N is sizeof(OBJECT)/sizeof(TYPE). */ | |
5e548861 | 167 | if (code1 == TYPE_CODE_ARRAY) |
13ffa6be JL |
168 | { |
169 | struct type *element_type = TYPE_TARGET_TYPE (type); | |
5e548861 PB |
170 | unsigned element_length = TYPE_LENGTH (check_typedef (element_type)); |
171 | if (element_length > 0 | |
172 | && TYPE_ARRAY_UPPER_BOUND_TYPE (type) == BOUND_CANNOT_BE_DETERMINED) | |
173 | { | |
174 | struct type *range_type = TYPE_INDEX_TYPE (type); | |
175 | int val_length = TYPE_LENGTH (type2); | |
176 | LONGEST low_bound, high_bound, new_length; | |
177 | if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0) | |
178 | low_bound = 0, high_bound = 0; | |
179 | new_length = val_length / element_length; | |
180 | if (val_length % element_length != 0) | |
181 | warning("array element type size does not divide object size in cast"); | |
182 | /* FIXME-type-allocation: need a way to free this type when we are | |
183 | done with it. */ | |
184 | range_type = create_range_type ((struct type *) NULL, | |
185 | TYPE_TARGET_TYPE (range_type), | |
186 | low_bound, | |
187 | new_length + low_bound - 1); | |
188 | VALUE_TYPE (arg2) = create_array_type ((struct type *) NULL, | |
189 | element_type, range_type); | |
190 | return arg2; | |
191 | } | |
13ffa6be | 192 | } |
9ed8604f | 193 | |
f7a69ed7 | 194 | if (current_language->c_style_arrays |
5e548861 | 195 | && TYPE_CODE (type2) == TYPE_CODE_ARRAY) |
e70bba9f | 196 | arg2 = value_coerce_array (arg2); |
f7a69ed7 | 197 | |
5e548861 | 198 | if (TYPE_CODE (type2) == TYPE_CODE_FUNC) |
f7a69ed7 PB |
199 | arg2 = value_coerce_function (arg2); |
200 | ||
5e548861 PB |
201 | type2 = check_typedef (VALUE_TYPE (arg2)); |
202 | COERCE_VARYING_ARRAY (arg2, type2); | |
203 | code2 = TYPE_CODE (type2); | |
f7a69ed7 | 204 | |
34cfa2da PB |
205 | if (code1 == TYPE_CODE_COMPLEX) |
206 | return cast_into_complex (type, arg2); | |
207 | if (code1 == TYPE_CODE_BOOL || code1 == TYPE_CODE_CHAR) | |
f7a69ed7 | 208 | code1 = TYPE_CODE_INT; |
34cfa2da | 209 | if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR) |
f7a69ed7 PB |
210 | code2 = TYPE_CODE_INT; |
211 | ||
bd5635a1 | 212 | scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT |
f91a9e05 | 213 | || code2 == TYPE_CODE_ENUM || code2 == TYPE_CODE_RANGE); |
bd5635a1 | 214 | |
54023465 JK |
215 | if ( code1 == TYPE_CODE_STRUCT |
216 | && code2 == TYPE_CODE_STRUCT | |
217 | && TYPE_NAME (type) != 0) | |
218 | { | |
219 | /* Look in the type of the source to see if it contains the | |
220 | type of the target as a superclass. If so, we'll need to | |
221 | offset the object in addition to changing its type. */ | |
a91a6192 | 222 | value_ptr v = search_struct_field (type_name_no_tag (type), |
5e548861 | 223 | arg2, 0, type2, 1); |
54023465 JK |
224 | if (v) |
225 | { | |
226 | VALUE_TYPE (v) = type; | |
227 | return v; | |
228 | } | |
229 | } | |
bd5635a1 RP |
230 | if (code1 == TYPE_CODE_FLT && scalar) |
231 | return value_from_double (type, value_as_double (arg2)); | |
f91a9e05 PB |
232 | else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM |
233 | || code1 == TYPE_CODE_RANGE) | |
bd5635a1 | 234 | && (scalar || code2 == TYPE_CODE_PTR)) |
06b6c733 | 235 | return value_from_longest (type, value_as_long (arg2)); |
5e548861 | 236 | else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2)) |
bd5635a1 RP |
237 | { |
238 | if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR) | |
239 | { | |
240 | /* Look in the type of the source to see if it contains the | |
241 | type of the target as a superclass. If so, we'll need to | |
242 | offset the pointer rather than just change its type. */ | |
5e548861 PB |
243 | struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type)); |
244 | struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2)); | |
2a5ec41d | 245 | if ( TYPE_CODE (t1) == TYPE_CODE_STRUCT |
bd5635a1 RP |
246 | && TYPE_CODE (t2) == TYPE_CODE_STRUCT |
247 | && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */ | |
248 | { | |
a91a6192 SS |
249 | value_ptr v = search_struct_field (type_name_no_tag (t1), |
250 | value_ind (arg2), 0, t2, 1); | |
bd5635a1 RP |
251 | if (v) |
252 | { | |
253 | v = value_addr (v); | |
254 | VALUE_TYPE (v) = type; | |
255 | return v; | |
256 | } | |
257 | } | |
258 | /* No superclass found, just fall through to change ptr type. */ | |
259 | } | |
260 | VALUE_TYPE (arg2) = type; | |
261 | return arg2; | |
262 | } | |
f91a9e05 PB |
263 | else if (chill_varying_type (type)) |
264 | { | |
265 | struct type *range1, *range2, *eltype1, *eltype2; | |
266 | value_ptr val; | |
267 | int count1, count2; | |
5e548861 | 268 | LONGEST low_bound, high_bound; |
f91a9e05 PB |
269 | char *valaddr, *valaddr_data; |
270 | if (code2 == TYPE_CODE_BITSTRING) | |
271 | error ("not implemented: converting bitstring to varying type"); | |
272 | if ((code2 != TYPE_CODE_ARRAY && code2 != TYPE_CODE_STRING) | |
5e548861 PB |
273 | || (eltype1 = check_typedef (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1))), |
274 | eltype2 = check_typedef (TYPE_TARGET_TYPE (type2)), | |
f91a9e05 PB |
275 | (TYPE_LENGTH (eltype1) != TYPE_LENGTH (eltype2) |
276 | /* || TYPE_CODE (eltype1) != TYPE_CODE (eltype2) */ ))) | |
277 | error ("Invalid conversion to varying type"); | |
278 | range1 = TYPE_FIELD_TYPE (TYPE_FIELD_TYPE (type, 1), 0); | |
5e548861 PB |
279 | range2 = TYPE_FIELD_TYPE (type2, 0); |
280 | if (get_discrete_bounds (range1, &low_bound, &high_bound) < 0) | |
281 | count1 = -1; | |
282 | else | |
283 | count1 = high_bound - low_bound + 1; | |
284 | if (get_discrete_bounds (range2, &low_bound, &high_bound) < 0) | |
285 | count1 = -1, count2 = 0; /* To force error before */ | |
286 | else | |
287 | count2 = high_bound - low_bound + 1; | |
f91a9e05 PB |
288 | if (count2 > count1) |
289 | error ("target varying type is too small"); | |
290 | val = allocate_value (type); | |
291 | valaddr = VALUE_CONTENTS_RAW (val); | |
292 | valaddr_data = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8; | |
293 | /* Set val's __var_length field to count2. */ | |
294 | store_signed_integer (valaddr, TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)), | |
295 | count2); | |
296 | /* Set the __var_data field to count2 elements copied from arg2. */ | |
297 | memcpy (valaddr_data, VALUE_CONTENTS (arg2), | |
298 | count2 * TYPE_LENGTH (eltype2)); | |
299 | /* Zero the rest of the __var_data field of val. */ | |
300 | memset (valaddr_data + count2 * TYPE_LENGTH (eltype2), '\0', | |
301 | (count1 - count2) * TYPE_LENGTH (eltype2)); | |
302 | return val; | |
303 | } | |
bd5635a1 RP |
304 | else if (VALUE_LVAL (arg2) == lval_memory) |
305 | { | |
306 | return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2)); | |
307 | } | |
d11c44f1 JG |
308 | else if (code1 == TYPE_CODE_VOID) |
309 | { | |
310 | return value_zero (builtin_type_void, not_lval); | |
311 | } | |
bd5635a1 RP |
312 | else |
313 | { | |
314 | error ("Invalid cast."); | |
315 | return 0; | |
316 | } | |
317 | } | |
318 | ||
319 | /* Create a value of type TYPE that is zero, and return it. */ | |
320 | ||
a91a6192 | 321 | value_ptr |
bd5635a1 RP |
322 | value_zero (type, lv) |
323 | struct type *type; | |
324 | enum lval_type lv; | |
325 | { | |
a91a6192 | 326 | register value_ptr val = allocate_value (type); |
bd5635a1 | 327 | |
5e548861 | 328 | memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (check_typedef (type))); |
bd5635a1 RP |
329 | VALUE_LVAL (val) = lv; |
330 | ||
331 | return val; | |
332 | } | |
333 | ||
334 | /* Return a value with type TYPE located at ADDR. | |
335 | ||
336 | Call value_at only if the data needs to be fetched immediately; | |
337 | if we can be 'lazy' and defer the fetch, perhaps indefinately, call | |
338 | value_at_lazy instead. value_at_lazy simply records the address of | |
339 | the data and sets the lazy-evaluation-required flag. The lazy flag | |
340 | is tested in the VALUE_CONTENTS macro, which is used if and when | |
341 | the contents are actually required. */ | |
342 | ||
a91a6192 | 343 | value_ptr |
bd5635a1 RP |
344 | value_at (type, addr) |
345 | struct type *type; | |
346 | CORE_ADDR addr; | |
347 | { | |
a91a6192 SS |
348 | register value_ptr val; |
349 | ||
5e548861 | 350 | if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID) |
a91a6192 SS |
351 | error ("Attempt to dereference a generic pointer."); |
352 | ||
353 | val = allocate_value (type); | |
bd5635a1 | 354 | |
dc1b349d MS |
355 | /* start-sanitize-d10v */ |
356 | #ifdef GDB_TARGET_IS_D10V | |
357 | if (TYPE_TARGET_TYPE(type) && TYPE_CODE(TYPE_TARGET_TYPE(type)) == TYPE_CODE_FUNC) | |
358 | { | |
359 | int num; | |
360 | short snum; | |
361 | read_memory (addr, (char *)&snum, 2); | |
362 | num = D10V_MAKE_IADDR(snum); | |
363 | memcpy( VALUE_CONTENTS_RAW (val), &num, 4); | |
364 | } | |
365 | else | |
366 | #endif | |
367 | /* end-sanitize-d10v */ | |
368 | ||
bd5635a1 RP |
369 | read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type)); |
370 | ||
371 | VALUE_LVAL (val) = lval_memory; | |
372 | VALUE_ADDRESS (val) = addr; | |
373 | ||
374 | return val; | |
375 | } | |
376 | ||
377 | /* Return a lazy value with type TYPE located at ADDR (cf. value_at). */ | |
378 | ||
a91a6192 | 379 | value_ptr |
bd5635a1 RP |
380 | value_at_lazy (type, addr) |
381 | struct type *type; | |
382 | CORE_ADDR addr; | |
383 | { | |
a91a6192 SS |
384 | register value_ptr val; |
385 | ||
5e548861 | 386 | if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID) |
a91a6192 SS |
387 | error ("Attempt to dereference a generic pointer."); |
388 | ||
389 | val = allocate_value (type); | |
bd5635a1 RP |
390 | |
391 | VALUE_LVAL (val) = lval_memory; | |
392 | VALUE_ADDRESS (val) = addr; | |
393 | VALUE_LAZY (val) = 1; | |
394 | ||
395 | return val; | |
396 | } | |
397 | ||
398 | /* Called only from the VALUE_CONTENTS macro, if the current data for | |
399 | a variable needs to be loaded into VALUE_CONTENTS(VAL). Fetches the | |
400 | data from the user's process, and clears the lazy flag to indicate | |
401 | that the data in the buffer is valid. | |
402 | ||
9cb602e1 JG |
403 | If the value is zero-length, we avoid calling read_memory, which would |
404 | abort. We mark the value as fetched anyway -- all 0 bytes of it. | |
405 | ||
bd5635a1 RP |
406 | This function returns a value because it is used in the VALUE_CONTENTS |
407 | macro as part of an expression, where a void would not work. The | |
408 | value is ignored. */ | |
409 | ||
410 | int | |
411 | value_fetch_lazy (val) | |
a91a6192 | 412 | register value_ptr val; |
bd5635a1 RP |
413 | { |
414 | CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val); | |
5e548861 | 415 | int length = TYPE_LENGTH (VALUE_TYPE (val)); |
bd5635a1 | 416 | |
dc1b349d MS |
417 | /* start-sanitize-d10v */ |
418 | #ifdef GDB_TARGET_IS_D10V | |
419 | struct type *type = VALUE_TYPE(val); | |
420 | if (TYPE_TARGET_TYPE(type) && TYPE_CODE(TYPE_TARGET_TYPE(type)) == TYPE_CODE_FUNC) | |
421 | { | |
422 | int num; | |
423 | short snum; | |
424 | read_memory (addr, (char *)&snum, 2); | |
425 | num = D10V_MAKE_IADDR(snum); | |
426 | memcpy( VALUE_CONTENTS_RAW (val), &num, 4); | |
427 | } | |
428 | else | |
429 | #endif | |
430 | /* end-sanitize-d10v */ | |
431 | ||
5e548861 PB |
432 | if (length) |
433 | read_memory (addr, VALUE_CONTENTS_RAW (val), length); | |
bd5635a1 RP |
434 | VALUE_LAZY (val) = 0; |
435 | return 0; | |
436 | } | |
437 | ||
438 | ||
439 | /* Store the contents of FROMVAL into the location of TOVAL. | |
440 | Return a new value with the location of TOVAL and contents of FROMVAL. */ | |
441 | ||
a91a6192 | 442 | value_ptr |
bd5635a1 | 443 | value_assign (toval, fromval) |
a91a6192 | 444 | register value_ptr toval, fromval; |
bd5635a1 | 445 | { |
67e9b3b3 | 446 | register struct type *type; |
a91a6192 | 447 | register value_ptr val; |
bd5635a1 | 448 | char raw_buffer[MAX_REGISTER_RAW_SIZE]; |
bd5635a1 RP |
449 | int use_buffer = 0; |
450 | ||
30974778 JK |
451 | if (!toval->modifiable) |
452 | error ("Left operand of assignment is not a modifiable lvalue."); | |
453 | ||
8e9a3f3b | 454 | COERCE_REF (toval); |
bd5635a1 | 455 | |
67e9b3b3 | 456 | type = VALUE_TYPE (toval); |
bd5635a1 RP |
457 | if (VALUE_LVAL (toval) != lval_internalvar) |
458 | fromval = value_cast (type, fromval); | |
aa220473 SG |
459 | else |
460 | COERCE_ARRAY (fromval); | |
5e548861 | 461 | CHECK_TYPEDEF (type); |
bd5635a1 RP |
462 | |
463 | /* If TOVAL is a special machine register requiring conversion | |
464 | of program values to a special raw format, | |
465 | convert FROMVAL's contents now, with result in `raw_buffer', | |
466 | and set USE_BUFFER to the number of bytes to write. */ | |
467 | ||
ad09cb2b | 468 | #ifdef REGISTER_CONVERTIBLE |
bd5635a1 RP |
469 | if (VALUE_REGNO (toval) >= 0 |
470 | && REGISTER_CONVERTIBLE (VALUE_REGNO (toval))) | |
471 | { | |
472 | int regno = VALUE_REGNO (toval); | |
ad09cb2b PS |
473 | if (REGISTER_CONVERTIBLE (regno)) |
474 | { | |
5e548861 PB |
475 | struct type *fromtype = check_typedef (VALUE_TYPE (fromval)); |
476 | REGISTER_CONVERT_TO_RAW (fromtype, regno, | |
ad09cb2b PS |
477 | VALUE_CONTENTS (fromval), raw_buffer); |
478 | use_buffer = REGISTER_RAW_SIZE (regno); | |
479 | } | |
bd5635a1 | 480 | } |
ad09cb2b | 481 | #endif |
bd5635a1 RP |
482 | |
483 | switch (VALUE_LVAL (toval)) | |
484 | { | |
485 | case lval_internalvar: | |
486 | set_internalvar (VALUE_INTERNALVAR (toval), fromval); | |
75225aa2 | 487 | return value_copy (VALUE_INTERNALVAR (toval)->value); |
bd5635a1 RP |
488 | |
489 | case lval_internalvar_component: | |
490 | set_internalvar_component (VALUE_INTERNALVAR (toval), | |
491 | VALUE_OFFSET (toval), | |
492 | VALUE_BITPOS (toval), | |
493 | VALUE_BITSIZE (toval), | |
494 | fromval); | |
495 | break; | |
496 | ||
497 | case lval_memory: | |
498 | if (VALUE_BITSIZE (toval)) | |
499 | { | |
4d52ec86 JK |
500 | char buffer[sizeof (LONGEST)]; |
501 | /* We assume that the argument to read_memory is in units of | |
502 | host chars. FIXME: Is that correct? */ | |
503 | int len = (VALUE_BITPOS (toval) | |
504 | + VALUE_BITSIZE (toval) | |
505 | + HOST_CHAR_BIT - 1) | |
506 | / HOST_CHAR_BIT; | |
ad09cb2b | 507 | |
b52cac6b | 508 | if (len > (int) sizeof (LONGEST)) |
ad09cb2b PS |
509 | error ("Can't handle bitfields which don't fit in a %d bit word.", |
510 | sizeof (LONGEST) * HOST_CHAR_BIT); | |
4d52ec86 | 511 | |
bd5635a1 | 512 | read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), |
4d52ec86 JK |
513 | buffer, len); |
514 | modify_field (buffer, value_as_long (fromval), | |
bd5635a1 RP |
515 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); |
516 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
4d52ec86 | 517 | buffer, len); |
bd5635a1 RP |
518 | } |
519 | else if (use_buffer) | |
520 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
521 | raw_buffer, use_buffer); | |
522 | else | |
523 | write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
524 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
525 | break; | |
526 | ||
527 | case lval_register: | |
528 | if (VALUE_BITSIZE (toval)) | |
529 | { | |
ad09cb2b | 530 | char buffer[sizeof (LONGEST)]; |
4d52ec86 | 531 | int len = REGISTER_RAW_SIZE (VALUE_REGNO (toval)); |
ad09cb2b | 532 | |
b52cac6b | 533 | if (len > (int) sizeof (LONGEST)) |
ad09cb2b PS |
534 | error ("Can't handle bitfields in registers larger than %d bits.", |
535 | sizeof (LONGEST) * HOST_CHAR_BIT); | |
536 | ||
537 | if (VALUE_BITPOS (toval) + VALUE_BITSIZE (toval) | |
538 | > len * HOST_CHAR_BIT) | |
539 | /* Getting this right would involve being very careful about | |
540 | byte order. */ | |
541 | error ("\ | |
542 | Can't handle bitfield which doesn't fit in a single register."); | |
543 | ||
4d52ec86 JK |
544 | read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), |
545 | buffer, len); | |
546 | modify_field (buffer, value_as_long (fromval), | |
547 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); | |
548 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
549 | buffer, len); | |
bd5635a1 RP |
550 | } |
551 | else if (use_buffer) | |
552 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
553 | raw_buffer, use_buffer); | |
554 | else | |
54023465 JK |
555 | { |
556 | /* Do any conversion necessary when storing this type to more | |
557 | than one register. */ | |
558 | #ifdef REGISTER_CONVERT_FROM_TYPE | |
559 | memcpy (raw_buffer, VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
560 | REGISTER_CONVERT_FROM_TYPE(VALUE_REGNO (toval), type, raw_buffer); | |
561 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
562 | raw_buffer, TYPE_LENGTH (type)); | |
563 | #else | |
564 | write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval), | |
565 | VALUE_CONTENTS (fromval), TYPE_LENGTH (type)); | |
566 | #endif | |
567 | } | |
79971d11 JK |
568 | /* Assigning to the stack pointer, frame pointer, and other |
569 | (architecture and calling convention specific) registers may | |
570 | cause the frame cache to be out of date. We just do this | |
571 | on all assignments to registers for simplicity; I doubt the slowdown | |
572 | matters. */ | |
573 | reinit_frame_cache (); | |
bd5635a1 RP |
574 | break; |
575 | ||
576 | case lval_reg_frame_relative: | |
577 | { | |
578 | /* value is stored in a series of registers in the frame | |
579 | specified by the structure. Copy that value out, modify | |
580 | it, and copy it back in. */ | |
581 | int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type)); | |
582 | int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval)); | |
583 | int byte_offset = VALUE_OFFSET (toval) % reg_size; | |
584 | int reg_offset = VALUE_OFFSET (toval) / reg_size; | |
585 | int amount_copied; | |
4d52ec86 JK |
586 | |
587 | /* Make the buffer large enough in all cases. */ | |
588 | char *buffer = (char *) alloca (amount_to_copy | |
589 | + sizeof (LONGEST) | |
590 | + MAX_REGISTER_RAW_SIZE); | |
591 | ||
bd5635a1 | 592 | int regno; |
6d34c236 | 593 | struct frame_info *frame; |
bd5635a1 RP |
594 | |
595 | /* Figure out which frame this is in currently. */ | |
596 | for (frame = get_current_frame (); | |
597 | frame && FRAME_FP (frame) != VALUE_FRAME (toval); | |
598 | frame = get_prev_frame (frame)) | |
599 | ; | |
600 | ||
601 | if (!frame) | |
602 | error ("Value being assigned to is no longer active."); | |
603 | ||
604 | amount_to_copy += (reg_size - amount_to_copy % reg_size); | |
605 | ||
606 | /* Copy it out. */ | |
607 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
608 | amount_copied = 0); | |
609 | amount_copied < amount_to_copy; | |
610 | amount_copied += reg_size, regno++) | |
611 | { | |
612 | get_saved_register (buffer + amount_copied, | |
51b57ded | 613 | (int *)NULL, (CORE_ADDR *)NULL, |
bd5635a1 RP |
614 | frame, regno, (enum lval_type *)NULL); |
615 | } | |
616 | ||
617 | /* Modify what needs to be modified. */ | |
618 | if (VALUE_BITSIZE (toval)) | |
619 | modify_field (buffer + byte_offset, | |
479fdd26 | 620 | value_as_long (fromval), |
bd5635a1 RP |
621 | VALUE_BITPOS (toval), VALUE_BITSIZE (toval)); |
622 | else if (use_buffer) | |
4ed3a9ea | 623 | memcpy (buffer + byte_offset, raw_buffer, use_buffer); |
bd5635a1 | 624 | else |
4ed3a9ea FF |
625 | memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval), |
626 | TYPE_LENGTH (type)); | |
bd5635a1 RP |
627 | |
628 | /* Copy it back. */ | |
629 | for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset, | |
630 | amount_copied = 0); | |
631 | amount_copied < amount_to_copy; | |
632 | amount_copied += reg_size, regno++) | |
633 | { | |
634 | enum lval_type lval; | |
635 | CORE_ADDR addr; | |
636 | int optim; | |
637 | ||
638 | /* Just find out where to put it. */ | |
639 | get_saved_register ((char *)NULL, | |
640 | &optim, &addr, frame, regno, &lval); | |
641 | ||
642 | if (optim) | |
643 | error ("Attempt to assign to a value that was optimized out."); | |
644 | if (lval == lval_memory) | |
645 | write_memory (addr, buffer + amount_copied, reg_size); | |
646 | else if (lval == lval_register) | |
647 | write_register_bytes (addr, buffer + amount_copied, reg_size); | |
648 | else | |
649 | error ("Attempt to assign to an unmodifiable value."); | |
650 | } | |
651 | } | |
652 | break; | |
653 | ||
654 | ||
655 | default: | |
30974778 | 656 | error ("Left operand of assignment is not an lvalue."); |
bd5635a1 RP |
657 | } |
658 | ||
b4680522 PB |
659 | /* If the field does not entirely fill a LONGEST, then zero the sign bits. |
660 | If the field is signed, and is negative, then sign extend. */ | |
661 | if ((VALUE_BITSIZE (toval) > 0) | |
b52cac6b | 662 | && (VALUE_BITSIZE (toval) < 8 * (int) sizeof (LONGEST))) |
b4680522 PB |
663 | { |
664 | LONGEST fieldval = value_as_long (fromval); | |
dc1b349d | 665 | LONGEST valmask = (((ULONGEST) 1) << VALUE_BITSIZE (toval)) - 1; |
b4680522 PB |
666 | |
667 | fieldval &= valmask; | |
668 | if (!TYPE_UNSIGNED (type) && (fieldval & (valmask ^ (valmask >> 1)))) | |
669 | fieldval |= ~valmask; | |
670 | ||
671 | fromval = value_from_longest (type, fieldval); | |
672 | } | |
673 | ||
b4680522 | 674 | val = value_copy (toval); |
4ed3a9ea FF |
675 | memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval), |
676 | TYPE_LENGTH (type)); | |
bd5635a1 RP |
677 | VALUE_TYPE (val) = type; |
678 | ||
679 | return val; | |
680 | } | |
681 | ||
682 | /* Extend a value VAL to COUNT repetitions of its type. */ | |
683 | ||
a91a6192 | 684 | value_ptr |
bd5635a1 | 685 | value_repeat (arg1, count) |
a91a6192 | 686 | value_ptr arg1; |
bd5635a1 RP |
687 | int count; |
688 | { | |
a91a6192 | 689 | register value_ptr val; |
bd5635a1 RP |
690 | |
691 | if (VALUE_LVAL (arg1) != lval_memory) | |
692 | error ("Only values in memory can be extended with '@'."); | |
693 | if (count < 1) | |
694 | error ("Invalid number %d of repetitions.", count); | |
695 | ||
696 | val = allocate_repeat_value (VALUE_TYPE (arg1), count); | |
697 | ||
698 | read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1), | |
699 | VALUE_CONTENTS_RAW (val), | |
09af5868 | 700 | TYPE_LENGTH (VALUE_TYPE (val))); |
bd5635a1 RP |
701 | VALUE_LVAL (val) = lval_memory; |
702 | VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1); | |
703 | ||
704 | return val; | |
705 | } | |
706 | ||
a91a6192 | 707 | value_ptr |
479fdd26 | 708 | value_of_variable (var, b) |
bd5635a1 | 709 | struct symbol *var; |
479fdd26 | 710 | struct block *b; |
bd5635a1 | 711 | { |
a91a6192 | 712 | value_ptr val; |
6d34c236 | 713 | struct frame_info *frame; |
bd5635a1 | 714 | |
dc1b349d MS |
715 | if (!b) |
716 | frame = NULL; /* Use selected frame. */ | |
717 | else if (symbol_read_needs_frame (var)) | |
479fdd26 | 718 | { |
6d34c236 | 719 | frame = block_innermost_frame (b); |
dc1b349d MS |
720 | if (!frame) |
721 | if (BLOCK_FUNCTION (b) | |
722 | && SYMBOL_NAME (BLOCK_FUNCTION (b))) | |
723 | error ("No frame is currently executing in block %s.", | |
724 | SYMBOL_NAME (BLOCK_FUNCTION (b))); | |
725 | else | |
726 | error ("No frame is currently executing in specified block"); | |
479fdd26 | 727 | } |
dc1b349d | 728 | |
6d34c236 | 729 | val = read_var_value (var, frame); |
dc1b349d | 730 | if (!val) |
2e4964ad | 731 | error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var)); |
dc1b349d | 732 | |
bd5635a1 RP |
733 | return val; |
734 | } | |
735 | ||
a163ddec MT |
736 | /* Given a value which is an array, return a value which is a pointer to its |
737 | first element, regardless of whether or not the array has a nonzero lower | |
738 | bound. | |
739 | ||
740 | FIXME: A previous comment here indicated that this routine should be | |
741 | substracting the array's lower bound. It's not clear to me that this | |
742 | is correct. Given an array subscripting operation, it would certainly | |
743 | work to do the adjustment here, essentially computing: | |
744 | ||
745 | (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0]) | |
746 | ||
747 | However I believe a more appropriate and logical place to account for | |
748 | the lower bound is to do so in value_subscript, essentially computing: | |
749 | ||
750 | (&array[0] + ((index - lowerbound) * sizeof array[0])) | |
751 | ||
752 | As further evidence consider what would happen with operations other | |
753 | than array subscripting, where the caller would get back a value that | |
754 | had an address somewhere before the actual first element of the array, | |
755 | and the information about the lower bound would be lost because of | |
756 | the coercion to pointer type. | |
757 | */ | |
bd5635a1 | 758 | |
a91a6192 | 759 | value_ptr |
bd5635a1 | 760 | value_coerce_array (arg1) |
a91a6192 | 761 | value_ptr arg1; |
bd5635a1 | 762 | { |
5e548861 | 763 | register struct type *type = check_typedef (VALUE_TYPE (arg1)); |
bd5635a1 RP |
764 | |
765 | if (VALUE_LVAL (arg1) != lval_memory) | |
766 | error ("Attempt to take address of value not located in memory."); | |
767 | ||
5e548861 | 768 | return value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)), |
bd5635a1 | 769 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
770 | } |
771 | ||
772 | /* Given a value which is a function, return a value which is a pointer | |
773 | to it. */ | |
774 | ||
a91a6192 | 775 | value_ptr |
bd5635a1 | 776 | value_coerce_function (arg1) |
a91a6192 | 777 | value_ptr arg1; |
bd5635a1 | 778 | { |
bd5635a1 RP |
779 | |
780 | if (VALUE_LVAL (arg1) != lval_memory) | |
781 | error ("Attempt to take address of value not located in memory."); | |
782 | ||
06b6c733 | 783 | return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)), |
bd5635a1 | 784 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
785 | } |
786 | ||
787 | /* Return a pointer value for the object for which ARG1 is the contents. */ | |
788 | ||
a91a6192 | 789 | value_ptr |
bd5635a1 | 790 | value_addr (arg1) |
a91a6192 | 791 | value_ptr arg1; |
bd5635a1 | 792 | { |
5e548861 | 793 | struct type *type = check_typedef (VALUE_TYPE (arg1)); |
8e9a3f3b PB |
794 | if (TYPE_CODE (type) == TYPE_CODE_REF) |
795 | { | |
796 | /* Copy the value, but change the type from (T&) to (T*). | |
797 | We keep the same location information, which is efficient, | |
798 | and allows &(&X) to get the location containing the reference. */ | |
a91a6192 | 799 | value_ptr arg2 = value_copy (arg1); |
8e9a3f3b PB |
800 | VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type)); |
801 | return arg2; | |
802 | } | |
8e9a3f3b | 803 | if (TYPE_CODE (type) == TYPE_CODE_FUNC) |
bd5635a1 RP |
804 | return value_coerce_function (arg1); |
805 | ||
806 | if (VALUE_LVAL (arg1) != lval_memory) | |
807 | error ("Attempt to take address of value not located in memory."); | |
808 | ||
5e548861 | 809 | return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)), |
bd5635a1 | 810 | (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1))); |
bd5635a1 RP |
811 | } |
812 | ||
813 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
814 | ||
a91a6192 | 815 | value_ptr |
bd5635a1 | 816 | value_ind (arg1) |
a91a6192 | 817 | value_ptr arg1; |
bd5635a1 | 818 | { |
5e548861 | 819 | struct type *type1; |
bd5635a1 | 820 | COERCE_ARRAY (arg1); |
5e548861 | 821 | type1 = check_typedef (VALUE_TYPE (arg1)); |
bd5635a1 | 822 | |
5e548861 | 823 | if (TYPE_CODE (type1) == TYPE_CODE_MEMBER) |
bd5635a1 RP |
824 | error ("not implemented: member types in value_ind"); |
825 | ||
826 | /* Allow * on an integer so we can cast it to whatever we want. | |
827 | This returns an int, which seems like the most C-like thing | |
828 | to do. "long long" variables are rare enough that | |
829 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
5e548861 | 830 | if (TYPE_CODE (type1) == TYPE_CODE_INT) |
bd5635a1 RP |
831 | return value_at (builtin_type_int, |
832 | (CORE_ADDR) value_as_long (arg1)); | |
5e548861 PB |
833 | else if (TYPE_CODE (type1) == TYPE_CODE_PTR) |
834 | return value_at_lazy (TYPE_TARGET_TYPE (type1), value_as_pointer (arg1)); | |
bd5635a1 RP |
835 | error ("Attempt to take contents of a non-pointer value."); |
836 | return 0; /* For lint -- never reached */ | |
837 | } | |
838 | \f | |
839 | /* Pushing small parts of stack frames. */ | |
840 | ||
841 | /* Push one word (the size of object that a register holds). */ | |
842 | ||
843 | CORE_ADDR | |
34df79fc | 844 | push_word (sp, word) |
bd5635a1 | 845 | CORE_ADDR sp; |
dc1b349d | 846 | ULONGEST word; |
bd5635a1 | 847 | { |
67e9b3b3 | 848 | register int len = REGISTER_SIZE; |
479fdd26 | 849 | char buffer[MAX_REGISTER_RAW_SIZE]; |
bd5635a1 | 850 | |
479fdd26 | 851 | store_unsigned_integer (buffer, len, word); |
bd5635a1 RP |
852 | #if 1 INNER_THAN 2 |
853 | sp -= len; | |
479fdd26 | 854 | write_memory (sp, buffer, len); |
bd5635a1 | 855 | #else /* stack grows upward */ |
479fdd26 | 856 | write_memory (sp, buffer, len); |
bd5635a1 RP |
857 | sp += len; |
858 | #endif /* stack grows upward */ | |
859 | ||
860 | return sp; | |
861 | } | |
862 | ||
863 | /* Push LEN bytes with data at BUFFER. */ | |
864 | ||
865 | CORE_ADDR | |
866 | push_bytes (sp, buffer, len) | |
867 | CORE_ADDR sp; | |
868 | char *buffer; | |
869 | int len; | |
870 | { | |
871 | #if 1 INNER_THAN 2 | |
872 | sp -= len; | |
873 | write_memory (sp, buffer, len); | |
874 | #else /* stack grows upward */ | |
875 | write_memory (sp, buffer, len); | |
876 | sp += len; | |
877 | #endif /* stack grows upward */ | |
878 | ||
879 | return sp; | |
880 | } | |
881 | ||
882 | /* Push onto the stack the specified value VALUE. */ | |
883 | ||
3f550b59 FF |
884 | #ifndef PUSH_ARGUMENTS |
885 | ||
01be6913 | 886 | static CORE_ADDR |
bd5635a1 RP |
887 | value_push (sp, arg) |
888 | register CORE_ADDR sp; | |
a91a6192 | 889 | value_ptr arg; |
bd5635a1 RP |
890 | { |
891 | register int len = TYPE_LENGTH (VALUE_TYPE (arg)); | |
892 | ||
893 | #if 1 INNER_THAN 2 | |
894 | sp -= len; | |
895 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
896 | #else /* stack grows upward */ | |
897 | write_memory (sp, VALUE_CONTENTS (arg), len); | |
898 | sp += len; | |
899 | #endif /* stack grows upward */ | |
900 | ||
901 | return sp; | |
902 | } | |
903 | ||
3f550b59 FF |
904 | #endif /* !PUSH_ARGUMENTS */ |
905 | ||
bd5635a1 | 906 | /* Perform the standard coercions that are specified |
5222ca60 | 907 | for arguments to be passed to C functions. |
bd5635a1 | 908 | |
5222ca60 PB |
909 | If PARAM_TYPE is non-NULL, it is the expected parameter type. */ |
910 | ||
911 | static value_ptr | |
912 | value_arg_coerce (arg, param_type) | |
a91a6192 | 913 | value_ptr arg; |
5222ca60 | 914 | struct type *param_type; |
bd5635a1 | 915 | { |
5e548861 PB |
916 | register struct type *arg_type = check_typedef (VALUE_TYPE (arg)); |
917 | register struct type *type | |
918 | = param_type ? check_typedef (param_type) : arg_type; | |
bd5635a1 | 919 | |
5222ca60 PB |
920 | switch (TYPE_CODE (type)) |
921 | { | |
922 | case TYPE_CODE_REF: | |
5e548861 | 923 | if (TYPE_CODE (arg_type) != TYPE_CODE_REF) |
5222ca60 PB |
924 | { |
925 | arg = value_addr (arg); | |
926 | VALUE_TYPE (arg) = param_type; | |
927 | return arg; | |
928 | } | |
929 | break; | |
930 | case TYPE_CODE_INT: | |
931 | case TYPE_CODE_CHAR: | |
932 | case TYPE_CODE_BOOL: | |
933 | case TYPE_CODE_ENUM: | |
934 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int)) | |
935 | type = builtin_type_int; | |
936 | break; | |
aa220473 SG |
937 | case TYPE_CODE_FLT: |
938 | /* coerce float to double, unless the function prototype specifies float */ | |
75225aa2 | 939 | if (COERCE_FLOAT_TO_DOUBLE) |
aa220473 SG |
940 | { |
941 | if (TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double)) | |
942 | type = builtin_type_double; | |
943 | else if (TYPE_LENGTH (type) > TYPE_LENGTH (builtin_type_double)) | |
944 | type = builtin_type_long_double; | |
945 | } | |
946 | break; | |
5222ca60 PB |
947 | case TYPE_CODE_FUNC: |
948 | type = lookup_pointer_type (type); | |
949 | break; | |
5e548861 PB |
950 | case TYPE_CODE_ARRAY: |
951 | if (current_language->c_style_arrays) | |
952 | type = lookup_pointer_type (TYPE_TARGET_TYPE (type)); | |
953 | break; | |
2b576293 C |
954 | case TYPE_CODE_UNDEF: |
955 | case TYPE_CODE_PTR: | |
2b576293 C |
956 | case TYPE_CODE_STRUCT: |
957 | case TYPE_CODE_UNION: | |
958 | case TYPE_CODE_VOID: | |
959 | case TYPE_CODE_SET: | |
960 | case TYPE_CODE_RANGE: | |
961 | case TYPE_CODE_STRING: | |
962 | case TYPE_CODE_BITSTRING: | |
963 | case TYPE_CODE_ERROR: | |
964 | case TYPE_CODE_MEMBER: | |
965 | case TYPE_CODE_METHOD: | |
966 | case TYPE_CODE_COMPLEX: | |
967 | default: | |
968 | break; | |
5222ca60 | 969 | } |
479fdd26 | 970 | |
5222ca60 | 971 | return value_cast (type, arg); |
bd5635a1 RP |
972 | } |
973 | ||
974 | /* Determine a function's address and its return type from its value. | |
975 | Calls error() if the function is not valid for calling. */ | |
976 | ||
01be6913 | 977 | static CORE_ADDR |
bd5635a1 | 978 | find_function_addr (function, retval_type) |
a91a6192 | 979 | value_ptr function; |
bd5635a1 RP |
980 | struct type **retval_type; |
981 | { | |
5e548861 | 982 | register struct type *ftype = check_typedef (VALUE_TYPE (function)); |
bd5635a1 RP |
983 | register enum type_code code = TYPE_CODE (ftype); |
984 | struct type *value_type; | |
985 | CORE_ADDR funaddr; | |
986 | ||
987 | /* If it's a member function, just look at the function | |
988 | part of it. */ | |
989 | ||
990 | /* Determine address to call. */ | |
991 | if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD) | |
992 | { | |
993 | funaddr = VALUE_ADDRESS (function); | |
994 | value_type = TYPE_TARGET_TYPE (ftype); | |
995 | } | |
996 | else if (code == TYPE_CODE_PTR) | |
997 | { | |
d11c44f1 | 998 | funaddr = value_as_pointer (function); |
5e548861 PB |
999 | ftype = check_typedef (TYPE_TARGET_TYPE (ftype)); |
1000 | if (TYPE_CODE (ftype) == TYPE_CODE_FUNC | |
1001 | || TYPE_CODE (ftype) == TYPE_CODE_METHOD) | |
9ed8604f PS |
1002 | { |
1003 | #ifdef CONVERT_FROM_FUNC_PTR_ADDR | |
1004 | /* FIXME: This is a workaround for the unusual function | |
1005 | pointer representation on the RS/6000, see comment | |
1006 | in config/rs6000/tm-rs6000.h */ | |
1007 | funaddr = CONVERT_FROM_FUNC_PTR_ADDR (funaddr); | |
1008 | #endif | |
5e548861 | 1009 | value_type = TYPE_TARGET_TYPE (ftype); |
9ed8604f | 1010 | } |
bd5635a1 RP |
1011 | else |
1012 | value_type = builtin_type_int; | |
1013 | } | |
1014 | else if (code == TYPE_CODE_INT) | |
1015 | { | |
1016 | /* Handle the case of functions lacking debugging info. | |
1017 | Their values are characters since their addresses are char */ | |
1018 | if (TYPE_LENGTH (ftype) == 1) | |
d11c44f1 | 1019 | funaddr = value_as_pointer (value_addr (function)); |
bd5635a1 RP |
1020 | else |
1021 | /* Handle integer used as address of a function. */ | |
d11c44f1 | 1022 | funaddr = (CORE_ADDR) value_as_long (function); |
bd5635a1 RP |
1023 | |
1024 | value_type = builtin_type_int; | |
1025 | } | |
1026 | else | |
1027 | error ("Invalid data type for function to be called."); | |
1028 | ||
1029 | *retval_type = value_type; | |
1030 | return funaddr; | |
1031 | } | |
1032 | ||
1033 | #if defined (CALL_DUMMY) | |
1034 | /* All this stuff with a dummy frame may seem unnecessarily complicated | |
1035 | (why not just save registers in GDB?). The purpose of pushing a dummy | |
1036 | frame which looks just like a real frame is so that if you call a | |
1037 | function and then hit a breakpoint (get a signal, etc), "backtrace" | |
1038 | will look right. Whether the backtrace needs to actually show the | |
1039 | stack at the time the inferior function was called is debatable, but | |
1040 | it certainly needs to not display garbage. So if you are contemplating | |
1041 | making dummy frames be different from normal frames, consider that. */ | |
1042 | ||
1043 | /* Perform a function call in the inferior. | |
1044 | ARGS is a vector of values of arguments (NARGS of them). | |
1045 | FUNCTION is a value, the function to be called. | |
1046 | Returns a value representing what the function returned. | |
1047 | May fail to return, if a breakpoint or signal is hit | |
5222ca60 PB |
1048 | during the execution of the function. |
1049 | ||
1050 | ARGS is modified to contain coerced values. */ | |
bd5635a1 | 1051 | |
a91a6192 | 1052 | value_ptr |
bd5635a1 | 1053 | call_function_by_hand (function, nargs, args) |
a91a6192 | 1054 | value_ptr function; |
bd5635a1 | 1055 | int nargs; |
a91a6192 | 1056 | value_ptr *args; |
bd5635a1 RP |
1057 | { |
1058 | register CORE_ADDR sp; | |
1059 | register int i; | |
1060 | CORE_ADDR start_sp; | |
67e9b3b3 PS |
1061 | /* CALL_DUMMY is an array of words (REGISTER_SIZE), but each word |
1062 | is in host byte order. Before calling FIX_CALL_DUMMY, we byteswap it | |
dc1b349d | 1063 | and remove any extra bytes which might exist because ULONGEST is |
67e9b3b3 | 1064 | bigger than REGISTER_SIZE. */ |
dc1b349d MS |
1065 | static ULONGEST dummy[] = CALL_DUMMY; |
1066 | char dummy1[REGISTER_SIZE * sizeof dummy / sizeof (ULONGEST)]; | |
bd5635a1 RP |
1067 | CORE_ADDR old_sp; |
1068 | struct type *value_type; | |
1069 | unsigned char struct_return; | |
b607efe7 | 1070 | CORE_ADDR struct_addr = 0; |
bd5635a1 RP |
1071 | struct inferior_status inf_status; |
1072 | struct cleanup *old_chain; | |
1073 | CORE_ADDR funaddr; | |
dc1b349d | 1074 | int using_gcc; /* Set to version of gcc in use, or zero if not gcc */ |
9f739abd | 1075 | CORE_ADDR real_pc; |
5e548861 | 1076 | struct type *ftype = check_typedef (SYMBOL_TYPE (function)); |
bd5635a1 | 1077 | |
e17960fb JG |
1078 | if (!target_has_execution) |
1079 | noprocess(); | |
1080 | ||
bd5635a1 RP |
1081 | save_inferior_status (&inf_status, 1); |
1082 | old_chain = make_cleanup (restore_inferior_status, &inf_status); | |
1083 | ||
1084 | /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers | |
1085 | (and POP_FRAME for restoring them). (At least on most machines) | |
1086 | they are saved on the stack in the inferior. */ | |
1087 | PUSH_DUMMY_FRAME; | |
1088 | ||
54023465 | 1089 | old_sp = sp = read_sp (); |
bd5635a1 RP |
1090 | |
1091 | #if 1 INNER_THAN 2 /* Stack grows down */ | |
9ed8604f | 1092 | sp -= sizeof dummy1; |
bd5635a1 RP |
1093 | start_sp = sp; |
1094 | #else /* Stack grows up */ | |
1095 | start_sp = sp; | |
9ed8604f | 1096 | sp += sizeof dummy1; |
bd5635a1 RP |
1097 | #endif |
1098 | ||
1099 | funaddr = find_function_addr (function, &value_type); | |
5e548861 | 1100 | CHECK_TYPEDEF (value_type); |
bd5635a1 RP |
1101 | |
1102 | { | |
1103 | struct block *b = block_for_pc (funaddr); | |
dc1b349d MS |
1104 | /* If compiled without -g, assume GCC 2. */ |
1105 | using_gcc = (b == NULL ? 2 : BLOCK_GCC_COMPILED (b)); | |
bd5635a1 RP |
1106 | } |
1107 | ||
1108 | /* Are we returning a value using a structure return or a normal | |
1109 | value return? */ | |
1110 | ||
1111 | struct_return = using_struct_return (function, funaddr, value_type, | |
1112 | using_gcc); | |
1113 | ||
1114 | /* Create a call sequence customized for this function | |
1115 | and the number of arguments for it. */ | |
b52cac6b | 1116 | for (i = 0; i < (int) (sizeof (dummy) / sizeof (dummy[0])); i++) |
67e9b3b3 PS |
1117 | store_unsigned_integer (&dummy1[i * REGISTER_SIZE], |
1118 | REGISTER_SIZE, | |
dc1b349d | 1119 | (ULONGEST)dummy[i]); |
9f739abd SG |
1120 | |
1121 | #ifdef GDB_TARGET_IS_HPPA | |
b5728692 SG |
1122 | real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, |
1123 | value_type, using_gcc); | |
9f739abd | 1124 | #else |
bd5635a1 RP |
1125 | FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args, |
1126 | value_type, using_gcc); | |
9f739abd SG |
1127 | real_pc = start_sp; |
1128 | #endif | |
bd5635a1 RP |
1129 | |
1130 | #if CALL_DUMMY_LOCATION == ON_STACK | |
9ed8604f | 1131 | write_memory (start_sp, (char *)dummy1, sizeof dummy1); |
cef4c2e7 | 1132 | #endif /* On stack. */ |
bd5635a1 | 1133 | |
bd5635a1 RP |
1134 | #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END |
1135 | /* Convex Unix prohibits executing in the stack segment. */ | |
1136 | /* Hope there is empty room at the top of the text segment. */ | |
1137 | { | |
84d82b1c | 1138 | extern CORE_ADDR text_end; |
bd5635a1 RP |
1139 | static checked = 0; |
1140 | if (!checked) | |
9ed8604f | 1141 | for (start_sp = text_end - sizeof dummy1; start_sp < text_end; ++start_sp) |
bd5635a1 RP |
1142 | if (read_memory_integer (start_sp, 1) != 0) |
1143 | error ("text segment full -- no place to put call"); | |
1144 | checked = 1; | |
1145 | sp = old_sp; | |
9ed8604f PS |
1146 | real_pc = text_end - sizeof dummy1; |
1147 | write_memory (real_pc, (char *)dummy1, sizeof dummy1); | |
bd5635a1 | 1148 | } |
cef4c2e7 PS |
1149 | #endif /* Before text_end. */ |
1150 | ||
1151 | #if CALL_DUMMY_LOCATION == AFTER_TEXT_END | |
bd5635a1 | 1152 | { |
84d82b1c | 1153 | extern CORE_ADDR text_end; |
bd5635a1 RP |
1154 | int errcode; |
1155 | sp = old_sp; | |
30d20d15 | 1156 | real_pc = text_end; |
9ed8604f | 1157 | errcode = target_write_memory (real_pc, (char *)dummy1, sizeof dummy1); |
bd5635a1 RP |
1158 | if (errcode != 0) |
1159 | error ("Cannot write text segment -- call_function failed"); | |
1160 | } | |
1161 | #endif /* After text_end. */ | |
cef4c2e7 PS |
1162 | |
1163 | #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT | |
1164 | real_pc = funaddr; | |
1165 | #endif /* At entry point. */ | |
bd5635a1 RP |
1166 | |
1167 | #ifdef lint | |
1168 | sp = old_sp; /* It really is used, for some ifdef's... */ | |
1169 | #endif | |
1170 | ||
f7a69ed7 PB |
1171 | if (nargs < TYPE_NFIELDS (ftype)) |
1172 | error ("too few arguments in function call"); | |
1173 | ||
5222ca60 PB |
1174 | for (i = nargs - 1; i >= 0; i--) |
1175 | { | |
1176 | struct type *param_type; | |
1177 | if (TYPE_NFIELDS (ftype) > i) | |
1178 | param_type = TYPE_FIELD_TYPE (ftype, i); | |
1179 | else | |
1180 | param_type = 0; | |
1181 | args[i] = value_arg_coerce (args[i], param_type); | |
1182 | } | |
1183 | ||
bd5635a1 RP |
1184 | #if defined (REG_STRUCT_HAS_ADDR) |
1185 | { | |
a91a6192 | 1186 | /* This is a machine like the sparc, where we may need to pass a pointer |
bd5635a1 | 1187 | to the structure, not the structure itself. */ |
a91a6192 | 1188 | for (i = nargs - 1; i >= 0; i--) |
5e548861 PB |
1189 | { |
1190 | struct type *arg_type = check_typedef (VALUE_TYPE (args[i])); | |
1191 | if ((TYPE_CODE (arg_type) == TYPE_CODE_STRUCT | |
1192 | || TYPE_CODE (arg_type) == TYPE_CODE_UNION | |
1193 | || TYPE_CODE (arg_type) == TYPE_CODE_ARRAY | |
34cfa2da PB |
1194 | || TYPE_CODE (arg_type) == TYPE_CODE_STRING |
1195 | || TYPE_CODE (arg_type) == TYPE_CODE_BITSTRING | |
aa220473 SG |
1196 | || TYPE_CODE (arg_type) == TYPE_CODE_SET |
1197 | || (TYPE_CODE (arg_type) == TYPE_CODE_FLT | |
1198 | && TYPE_LENGTH (arg_type) > 8) | |
1199 | ) | |
5e548861 PB |
1200 | && REG_STRUCT_HAS_ADDR (using_gcc, arg_type)) |
1201 | { | |
1202 | CORE_ADDR addr; | |
1203 | int len = TYPE_LENGTH (arg_type); | |
f7a69ed7 | 1204 | #ifdef STACK_ALIGN |
dc1b349d MS |
1205 | /* MVS 11/22/96: I think at least some of this stack_align code is |
1206 | really broken. Better to let PUSH_ARGUMENTS adjust the stack in | |
1207 | a target-defined manner. */ | |
5e548861 | 1208 | int aligned_len = STACK_ALIGN (len); |
f7a69ed7 | 1209 | #else |
5e548861 | 1210 | int aligned_len = len; |
f7a69ed7 | 1211 | #endif |
bd5635a1 | 1212 | #if !(1 INNER_THAN 2) |
5e548861 PB |
1213 | /* The stack grows up, so the address of the thing we push |
1214 | is the stack pointer before we push it. */ | |
1215 | addr = sp; | |
f7a69ed7 | 1216 | #else |
5e548861 | 1217 | sp -= aligned_len; |
bd5635a1 | 1218 | #endif |
5e548861 PB |
1219 | /* Push the structure. */ |
1220 | write_memory (sp, VALUE_CONTENTS (args[i]), len); | |
bd5635a1 | 1221 | #if 1 INNER_THAN 2 |
5e548861 PB |
1222 | /* The stack grows down, so the address of the thing we push |
1223 | is the stack pointer after we push it. */ | |
1224 | addr = sp; | |
f7a69ed7 | 1225 | #else |
5e548861 | 1226 | sp += aligned_len; |
bd5635a1 | 1227 | #endif |
5e548861 PB |
1228 | /* The value we're going to pass is the address of the thing |
1229 | we just pushed. */ | |
1230 | args[i] = value_from_longest (lookup_pointer_type (value_type), | |
1231 | (LONGEST) addr); | |
1232 | } | |
1233 | } | |
bd5635a1 RP |
1234 | } |
1235 | #endif /* REG_STRUCT_HAS_ADDR. */ | |
1236 | ||
f7a69ed7 PB |
1237 | /* Reserve space for the return structure to be written on the |
1238 | stack, if necessary */ | |
1239 | ||
1240 | if (struct_return) | |
1241 | { | |
1242 | int len = TYPE_LENGTH (value_type); | |
1243 | #ifdef STACK_ALIGN | |
dc1b349d MS |
1244 | /* MVS 11/22/96: I think at least some of this stack_align code is |
1245 | really broken. Better to let PUSH_ARGUMENTS adjust the stack in | |
1246 | a target-defined manner. */ | |
f7a69ed7 PB |
1247 | len = STACK_ALIGN (len); |
1248 | #endif | |
1249 | #if 1 INNER_THAN 2 | |
1250 | sp -= len; | |
1251 | struct_addr = sp; | |
1252 | #else | |
1253 | struct_addr = sp; | |
1254 | sp += len; | |
1255 | #endif | |
1256 | } | |
1257 | ||
dc1b349d MS |
1258 | #if defined(STACK_ALIGN) && (1 INNER_THAN 2) |
1259 | /* MVS 11/22/96: I think at least some of this stack_align code is | |
1260 | really broken. Better to let PUSH_ARGUMENTS adjust the stack in | |
1261 | a target-defined manner. */ | |
f7a69ed7 | 1262 | { |
dc1b349d | 1263 | /* If stack grows down, we must leave a hole at the top. */ |
f7a69ed7 PB |
1264 | int len = 0; |
1265 | ||
1266 | for (i = nargs - 1; i >= 0; i--) | |
1267 | len += TYPE_LENGTH (VALUE_TYPE (args[i])); | |
1268 | #ifdef CALL_DUMMY_STACK_ADJUST | |
1269 | len += CALL_DUMMY_STACK_ADJUST; | |
1270 | #endif | |
f7a69ed7 | 1271 | sp -= STACK_ALIGN (len) - len; |
f7a69ed7 PB |
1272 | } |
1273 | #endif /* STACK_ALIGN */ | |
1274 | ||
bd5635a1 RP |
1275 | #ifdef PUSH_ARGUMENTS |
1276 | PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr); | |
1277 | #else /* !PUSH_ARGUMENTS */ | |
1278 | for (i = nargs - 1; i >= 0; i--) | |
5222ca60 | 1279 | sp = value_push (sp, args[i]); |
bd5635a1 RP |
1280 | #endif /* !PUSH_ARGUMENTS */ |
1281 | ||
dc1b349d MS |
1282 | #ifdef PUSH_RETURN_ADDRESS /* for targets that use no CALL_DUMMY */ |
1283 | /* There are a number of targets now which actually don't write any | |
1284 | CALL_DUMMY instructions into the target, but instead just save the | |
1285 | machine state, push the arguments, and jump directly to the callee | |
1286 | function. Since this doesn't actually involve executing a JSR/BSR | |
1287 | instruction, the return address must be set up by hand, either by | |
1288 | pushing onto the stack or copying into a return-address register | |
1289 | as appropriate. Formerly this has been done in PUSH_ARGUMENTS, | |
1290 | but that's overloading its functionality a bit, so I'm making it | |
1291 | explicit to do it here. */ | |
1292 | sp = PUSH_RETURN_ADDRESS(real_pc, sp); | |
1293 | #endif /* PUSH_RETURN_ADDRESS */ | |
1294 | ||
1295 | #if defined(STACK_ALIGN) && !(1 INNER_THAN 2) | |
1296 | { | |
1297 | /* If stack grows up, we must leave a hole at the bottom, note | |
1298 | that sp already has been advanced for the arguments! */ | |
1299 | #ifdef CALL_DUMMY_STACK_ADJUST | |
1300 | sp += CALL_DUMMY_STACK_ADJUST; | |
1301 | #endif | |
1302 | sp = STACK_ALIGN (sp); | |
1303 | } | |
1304 | #endif /* STACK_ALIGN */ | |
1305 | ||
1306 | /* XXX This seems wrong. For stacks that grow down we shouldn't do | |
1307 | anything here! */ | |
1308 | /* MVS 11/22/96: I think at least some of this stack_align code is | |
1309 | really broken. Better to let PUSH_ARGUMENTS adjust the stack in | |
1310 | a target-defined manner. */ | |
bd5635a1 RP |
1311 | #ifdef CALL_DUMMY_STACK_ADJUST |
1312 | #if 1 INNER_THAN 2 | |
1313 | sp -= CALL_DUMMY_STACK_ADJUST; | |
bd5635a1 RP |
1314 | #endif |
1315 | #endif /* CALL_DUMMY_STACK_ADJUST */ | |
1316 | ||
1317 | /* Store the address at which the structure is supposed to be | |
1318 | written. Note that this (and the code which reserved the space | |
1319 | above) assumes that gcc was used to compile this function. Since | |
1320 | it doesn't cost us anything but space and if the function is pcc | |
1321 | it will ignore this value, we will make that assumption. | |
1322 | ||
1323 | Also note that on some machines (like the sparc) pcc uses a | |
1324 | convention like gcc's. */ | |
1325 | ||
1326 | if (struct_return) | |
1327 | STORE_STRUCT_RETURN (struct_addr, sp); | |
1328 | ||
1329 | /* Write the stack pointer. This is here because the statements above | |
1330 | might fool with it. On SPARC, this write also stores the register | |
1331 | window into the right place in the new stack frame, which otherwise | |
5632cd56 | 1332 | wouldn't happen. (See store_inferior_registers in sparc-nat.c.) */ |
54023465 | 1333 | write_sp (sp); |
bd5635a1 | 1334 | |
bd5635a1 RP |
1335 | { |
1336 | char retbuf[REGISTER_BYTES]; | |
54023465 JK |
1337 | char *name; |
1338 | struct symbol *symbol; | |
1339 | ||
1340 | name = NULL; | |
1341 | symbol = find_pc_function (funaddr); | |
1342 | if (symbol) | |
1343 | { | |
1344 | name = SYMBOL_SOURCE_NAME (symbol); | |
1345 | } | |
1346 | else | |
1347 | { | |
1348 | /* Try the minimal symbols. */ | |
1349 | struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (funaddr); | |
1350 | ||
1351 | if (msymbol) | |
1352 | { | |
1353 | name = SYMBOL_SOURCE_NAME (msymbol); | |
1354 | } | |
1355 | } | |
1356 | if (name == NULL) | |
1357 | { | |
1358 | char format[80]; | |
1359 | sprintf (format, "at %s", local_hex_format ()); | |
1360 | name = alloca (80); | |
30974778 | 1361 | /* FIXME-32x64: assumes funaddr fits in a long. */ |
cef4c2e7 | 1362 | sprintf (name, format, (unsigned long) funaddr); |
54023465 | 1363 | } |
bd5635a1 RP |
1364 | |
1365 | /* Execute the stack dummy routine, calling FUNCTION. | |
1366 | When it is done, discard the empty frame | |
1367 | after storing the contents of all regs into retbuf. */ | |
860a1754 JK |
1368 | if (run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf)) |
1369 | { | |
1370 | /* We stopped somewhere besides the call dummy. */ | |
1371 | ||
1372 | /* If we did the cleanups, we would print a spurious error message | |
1373 | (Unable to restore previously selected frame), would write the | |
1374 | registers from the inf_status (which is wrong), and would do other | |
1375 | wrong things (like set stop_bpstat to the wrong thing). */ | |
1376 | discard_cleanups (old_chain); | |
1377 | /* Prevent memory leak. */ | |
30d20d15 | 1378 | bpstat_clear (&inf_status.stop_bpstat); |
860a1754 JK |
1379 | |
1380 | /* The following error message used to say "The expression | |
1381 | which contained the function call has been discarded." It | |
1382 | is a hard concept to explain in a few words. Ideally, GDB | |
1383 | would be able to resume evaluation of the expression when | |
1384 | the function finally is done executing. Perhaps someday | |
1385 | this will be implemented (it would not be easy). */ | |
1386 | ||
1387 | /* FIXME: Insert a bunch of wrap_here; name can be very long if it's | |
1388 | a C++ name with arguments and stuff. */ | |
1389 | error ("\ | |
1390 | The program being debugged stopped while in a function called from GDB.\n\ | |
1391 | When the function (%s) is done executing, GDB will silently\n\ | |
1392 | stop (instead of continuing to evaluate the expression containing\n\ | |
1393 | the function call).", name); | |
1394 | } | |
bd5635a1 RP |
1395 | |
1396 | do_cleanups (old_chain); | |
1397 | ||
860a1754 | 1398 | /* Figure out the value returned by the function. */ |
bd5635a1 RP |
1399 | return value_being_returned (value_type, retbuf, struct_return); |
1400 | } | |
1401 | } | |
1402 | #else /* no CALL_DUMMY. */ | |
a91a6192 | 1403 | value_ptr |
bd5635a1 | 1404 | call_function_by_hand (function, nargs, args) |
a91a6192 | 1405 | value_ptr function; |
bd5635a1 | 1406 | int nargs; |
a91a6192 | 1407 | value_ptr *args; |
bd5635a1 RP |
1408 | { |
1409 | error ("Cannot invoke functions on this machine."); | |
1410 | } | |
1411 | #endif /* no CALL_DUMMY. */ | |
a163ddec | 1412 | |
bd5635a1 | 1413 | \f |
a163ddec MT |
1414 | /* Create a value for an array by allocating space in the inferior, copying |
1415 | the data into that space, and then setting up an array value. | |
1416 | ||
1417 | The array bounds are set from LOWBOUND and HIGHBOUND, and the array is | |
1418 | populated from the values passed in ELEMVEC. | |
1419 | ||
1420 | The element type of the array is inherited from the type of the | |
1421 | first element, and all elements must have the same size (though we | |
1422 | don't currently enforce any restriction on their types). */ | |
bd5635a1 | 1423 | |
a91a6192 | 1424 | value_ptr |
a163ddec MT |
1425 | value_array (lowbound, highbound, elemvec) |
1426 | int lowbound; | |
1427 | int highbound; | |
a91a6192 | 1428 | value_ptr *elemvec; |
bd5635a1 | 1429 | { |
a163ddec MT |
1430 | int nelem; |
1431 | int idx; | |
b52cac6b | 1432 | unsigned int typelength; |
a91a6192 | 1433 | value_ptr val; |
a163ddec MT |
1434 | struct type *rangetype; |
1435 | struct type *arraytype; | |
1436 | CORE_ADDR addr; | |
bd5635a1 | 1437 | |
a163ddec MT |
1438 | /* Validate that the bounds are reasonable and that each of the elements |
1439 | have the same size. */ | |
bd5635a1 | 1440 | |
a163ddec MT |
1441 | nelem = highbound - lowbound + 1; |
1442 | if (nelem <= 0) | |
bd5635a1 | 1443 | { |
a163ddec | 1444 | error ("bad array bounds (%d, %d)", lowbound, highbound); |
bd5635a1 | 1445 | } |
a163ddec | 1446 | typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0])); |
5e548861 | 1447 | for (idx = 1; idx < nelem; idx++) |
bd5635a1 | 1448 | { |
a163ddec MT |
1449 | if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength) |
1450 | { | |
1451 | error ("array elements must all be the same size"); | |
1452 | } | |
bd5635a1 RP |
1453 | } |
1454 | ||
aa220473 SG |
1455 | rangetype = create_range_type ((struct type *) NULL, builtin_type_int, |
1456 | lowbound, highbound); | |
1457 | arraytype = create_array_type ((struct type *) NULL, | |
1458 | VALUE_TYPE (elemvec[0]), rangetype); | |
1459 | ||
1460 | if (!current_language->c_style_arrays) | |
1461 | { | |
1462 | val = allocate_value (arraytype); | |
1463 | for (idx = 0; idx < nelem; idx++) | |
1464 | { | |
1465 | memcpy (VALUE_CONTENTS_RAW (val) + (idx * typelength), | |
1466 | VALUE_CONTENTS (elemvec[idx]), | |
1467 | typelength); | |
1468 | } | |
1469 | return val; | |
1470 | } | |
1471 | ||
a163ddec MT |
1472 | /* Allocate space to store the array in the inferior, and then initialize |
1473 | it by copying in each element. FIXME: Is it worth it to create a | |
1474 | local buffer in which to collect each value and then write all the | |
1475 | bytes in one operation? */ | |
1476 | ||
1477 | addr = allocate_space_in_inferior (nelem * typelength); | |
1478 | for (idx = 0; idx < nelem; idx++) | |
1479 | { | |
1480 | write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]), | |
1481 | typelength); | |
1482 | } | |
1483 | ||
1484 | /* Create the array type and set up an array value to be evaluated lazily. */ | |
1485 | ||
a163ddec MT |
1486 | val = value_at_lazy (arraytype, addr); |
1487 | return (val); | |
1488 | } | |
1489 | ||
1490 | /* Create a value for a string constant by allocating space in the inferior, | |
1491 | copying the data into that space, and returning the address with type | |
1492 | TYPE_CODE_STRING. PTR points to the string constant data; LEN is number | |
1493 | of characters. | |
1494 | Note that string types are like array of char types with a lower bound of | |
1495 | zero and an upper bound of LEN - 1. Also note that the string may contain | |
1496 | embedded null bytes. */ | |
1497 | ||
a91a6192 | 1498 | value_ptr |
a163ddec MT |
1499 | value_string (ptr, len) |
1500 | char *ptr; | |
1501 | int len; | |
1502 | { | |
a91a6192 | 1503 | value_ptr val; |
5222ca60 | 1504 | int lowbound = current_language->string_lower_bound; |
f91a9e05 | 1505 | struct type *rangetype = create_range_type ((struct type *) NULL, |
5222ca60 PB |
1506 | builtin_type_int, |
1507 | lowbound, len + lowbound - 1); | |
f91a9e05 PB |
1508 | struct type *stringtype |
1509 | = create_string_type ((struct type *) NULL, rangetype); | |
a163ddec MT |
1510 | CORE_ADDR addr; |
1511 | ||
f91a9e05 PB |
1512 | if (current_language->c_style_arrays == 0) |
1513 | { | |
1514 | val = allocate_value (stringtype); | |
1515 | memcpy (VALUE_CONTENTS_RAW (val), ptr, len); | |
1516 | return val; | |
1517 | } | |
1518 | ||
1519 | ||
a163ddec MT |
1520 | /* Allocate space to store the string in the inferior, and then |
1521 | copy LEN bytes from PTR in gdb to that address in the inferior. */ | |
1522 | ||
1523 | addr = allocate_space_in_inferior (len); | |
1524 | write_memory (addr, ptr, len); | |
1525 | ||
a163ddec MT |
1526 | val = value_at_lazy (stringtype, addr); |
1527 | return (val); | |
bd5635a1 | 1528 | } |
6d34c236 PB |
1529 | |
1530 | value_ptr | |
1531 | value_bitstring (ptr, len) | |
1532 | char *ptr; | |
1533 | int len; | |
1534 | { | |
1535 | value_ptr val; | |
1536 | struct type *domain_type = create_range_type (NULL, builtin_type_int, | |
1537 | 0, len - 1); | |
1538 | struct type *type = create_set_type ((struct type*) NULL, domain_type); | |
1539 | TYPE_CODE (type) = TYPE_CODE_BITSTRING; | |
1540 | val = allocate_value (type); | |
b4680522 | 1541 | memcpy (VALUE_CONTENTS_RAW (val), ptr, TYPE_LENGTH (type)); |
6d34c236 PB |
1542 | return val; |
1543 | } | |
bd5635a1 | 1544 | \f |
479fdd26 JK |
1545 | /* See if we can pass arguments in T2 to a function which takes arguments |
1546 | of types T1. Both t1 and t2 are NULL-terminated vectors. If some | |
1547 | arguments need coercion of some sort, then the coerced values are written | |
1548 | into T2. Return value is 0 if the arguments could be matched, or the | |
1549 | position at which they differ if not. | |
a163ddec MT |
1550 | |
1551 | STATICP is nonzero if the T1 argument list came from a | |
1552 | static member function. | |
1553 | ||
1554 | For non-static member functions, we ignore the first argument, | |
1555 | which is the type of the instance variable. This is because we want | |
1556 | to handle calls with objects from derived classes. This is not | |
1557 | entirely correct: we should actually check to make sure that a | |
1558 | requested operation is type secure, shouldn't we? FIXME. */ | |
1559 | ||
1560 | static int | |
1561 | typecmp (staticp, t1, t2) | |
1562 | int staticp; | |
1563 | struct type *t1[]; | |
a91a6192 | 1564 | value_ptr t2[]; |
a163ddec MT |
1565 | { |
1566 | int i; | |
1567 | ||
1568 | if (t2 == 0) | |
1569 | return 1; | |
1570 | if (staticp && t1 == 0) | |
1571 | return t2[1] != 0; | |
1572 | if (t1 == 0) | |
1573 | return 1; | |
1574 | if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0; | |
1575 | if (t1[!staticp] == 0) return 0; | |
1576 | for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++) | |
1577 | { | |
40620258 | 1578 | struct type *tt1, *tt2; |
a163ddec MT |
1579 | if (! t2[i]) |
1580 | return i+1; | |
5e548861 PB |
1581 | tt1 = check_typedef (t1[i]); |
1582 | tt2 = check_typedef (VALUE_TYPE(t2[i])); | |
40620258 | 1583 | if (TYPE_CODE (tt1) == TYPE_CODE_REF |
479fdd26 | 1584 | /* We should be doing hairy argument matching, as below. */ |
5e548861 | 1585 | && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1))) == TYPE_CODE (tt2))) |
479fdd26 | 1586 | { |
09af5868 | 1587 | if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY) |
2b576293 C |
1588 | t2[i] = value_coerce_array (t2[i]); |
1589 | else | |
1590 | t2[i] = value_addr (t2[i]); | |
479fdd26 JK |
1591 | continue; |
1592 | } | |
1593 | ||
40620258 | 1594 | while (TYPE_CODE (tt1) == TYPE_CODE_PTR |
5e548861 PB |
1595 | && ( TYPE_CODE (tt2) == TYPE_CODE_ARRAY |
1596 | || TYPE_CODE (tt2) == TYPE_CODE_PTR)) | |
40620258 | 1597 | { |
5e548861 PB |
1598 | tt1 = check_typedef (TYPE_TARGET_TYPE(tt1)); |
1599 | tt2 = check_typedef (TYPE_TARGET_TYPE(tt2)); | |
40620258 KH |
1600 | } |
1601 | if (TYPE_CODE(tt1) == TYPE_CODE(tt2)) continue; | |
1602 | /* Array to pointer is a `trivial conversion' according to the ARM. */ | |
479fdd26 JK |
1603 | |
1604 | /* We should be doing much hairier argument matching (see section 13.2 | |
1605 | of the ARM), but as a quick kludge, just check for the same type | |
1606 | code. */ | |
a163ddec MT |
1607 | if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i]))) |
1608 | return i+1; | |
1609 | } | |
1610 | if (!t1[i]) return 0; | |
1611 | return t2[i] ? i+1 : 0; | |
1612 | } | |
1613 | ||
bd5635a1 RP |
1614 | /* Helper function used by value_struct_elt to recurse through baseclasses. |
1615 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 1616 | and search in it assuming it has (class) type TYPE. |
d3bab255 JK |
1617 | If found, return value, else return NULL. |
1618 | ||
1619 | If LOOKING_FOR_BASECLASS, then instead of looking for struct fields, | |
1620 | look for a baseclass named NAME. */ | |
bd5635a1 | 1621 | |
a91a6192 | 1622 | static value_ptr |
d3bab255 | 1623 | search_struct_field (name, arg1, offset, type, looking_for_baseclass) |
bd5635a1 | 1624 | char *name; |
a91a6192 | 1625 | register value_ptr arg1; |
bd5635a1 RP |
1626 | int offset; |
1627 | register struct type *type; | |
d3bab255 | 1628 | int looking_for_baseclass; |
bd5635a1 RP |
1629 | { |
1630 | int i; | |
1631 | ||
5e548861 | 1632 | CHECK_TYPEDEF (type); |
bd5635a1 | 1633 | |
d3bab255 JK |
1634 | if (! looking_for_baseclass) |
1635 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1636 | { | |
1637 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
1638 | ||
2e4964ad | 1639 | if (t_field_name && STREQ (t_field_name, name)) |
d3bab255 | 1640 | { |
a91a6192 | 1641 | value_ptr v; |
01be6913 PB |
1642 | if (TYPE_FIELD_STATIC (type, i)) |
1643 | { | |
1644 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i); | |
1645 | struct symbol *sym = | |
2e4964ad FF |
1646 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); |
1647 | if (sym == NULL) | |
1648 | error ("Internal error: could not find physical static variable named %s", | |
1649 | phys_name); | |
01be6913 PB |
1650 | v = value_at (TYPE_FIELD_TYPE (type, i), |
1651 | (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym)); | |
1652 | } | |
1653 | else | |
1654 | v = value_primitive_field (arg1, offset, i, type); | |
d3bab255 JK |
1655 | if (v == 0) |
1656 | error("there is no field named %s", name); | |
1657 | return v; | |
1658 | } | |
37d190e0 | 1659 | |
4c2260aa PB |
1660 | if (t_field_name |
1661 | && (t_field_name[0] == '\0' | |
1662 | || (TYPE_CODE (type) == TYPE_CODE_UNION | |
1663 | && STREQ (t_field_name, "else")))) | |
6d34c236 | 1664 | { |
37d190e0 PB |
1665 | struct type *field_type = TYPE_FIELD_TYPE (type, i); |
1666 | if (TYPE_CODE (field_type) == TYPE_CODE_UNION | |
1667 | || TYPE_CODE (field_type) == TYPE_CODE_STRUCT) | |
1668 | { | |
1669 | /* Look for a match through the fields of an anonymous union, | |
1670 | or anonymous struct. C++ provides anonymous unions. | |
1671 | ||
1672 | In the GNU Chill implementation of variant record types, | |
1673 | each <alternative field> has an (anonymous) union type, | |
1674 | each member of the union represents a <variant alternative>. | |
1675 | Each <variant alternative> is represented as a struct, | |
1676 | with a member for each <variant field>. */ | |
1677 | ||
1678 | value_ptr v; | |
1679 | int new_offset = offset; | |
1680 | ||
1681 | /* This is pretty gross. In G++, the offset in an anonymous | |
1682 | union is relative to the beginning of the enclosing struct. | |
1683 | In the GNU Chill implementation of variant records, | |
1684 | the bitpos is zero in an anonymous union field, so we | |
1685 | have to add the offset of the union here. */ | |
1686 | if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT | |
1687 | || (TYPE_NFIELDS (field_type) > 0 | |
1688 | && TYPE_FIELD_BITPOS (field_type, 0) == 0)) | |
1689 | new_offset += TYPE_FIELD_BITPOS (type, i) / 8; | |
1690 | ||
1691 | v = search_struct_field (name, arg1, new_offset, field_type, | |
1692 | looking_for_baseclass); | |
1693 | if (v) | |
1694 | return v; | |
1695 | } | |
6d34c236 | 1696 | } |
d3bab255 | 1697 | } |
bd5635a1 RP |
1698 | |
1699 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1700 | { | |
a91a6192 | 1701 | value_ptr v; |
5e548861 | 1702 | struct type *basetype = check_typedef (TYPE_BASECLASS (type, i)); |
bd5635a1 | 1703 | /* If we are looking for baseclasses, this is what we get when we |
54023465 JK |
1704 | hit them. But it could happen that the base part's member name |
1705 | is not yet filled in. */ | |
d3bab255 | 1706 | int found_baseclass = (looking_for_baseclass |
54023465 | 1707 | && TYPE_BASECLASS_NAME (type, i) != NULL |
2e4964ad | 1708 | && STREQ (name, TYPE_BASECLASS_NAME (type, i))); |
bd5635a1 RP |
1709 | |
1710 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1711 | { | |
5e548861 PB |
1712 | int boffset = VALUE_OFFSET (arg1) + offset; |
1713 | boffset = baseclass_offset (type, i, | |
1714 | VALUE_CONTENTS (arg1) + boffset, | |
1715 | VALUE_ADDRESS (arg1) + boffset); | |
1716 | if (boffset == -1) | |
bd5635a1 RP |
1717 | error ("virtual baseclass botch"); |
1718 | if (found_baseclass) | |
5e548861 PB |
1719 | { |
1720 | value_ptr v2 = allocate_value (basetype); | |
1721 | VALUE_LVAL (v2) = VALUE_LVAL (arg1); | |
1722 | VALUE_ADDRESS (v2) = VALUE_ADDRESS (arg1); | |
1723 | VALUE_OFFSET (v2) = VALUE_OFFSET (arg1) + offset + boffset; | |
1724 | if (VALUE_LAZY (arg1)) | |
1725 | VALUE_LAZY (v2) = 1; | |
1726 | else | |
1727 | memcpy (VALUE_CONTENTS_RAW (v2), | |
1728 | VALUE_CONTENTS_RAW (arg1) + offset + boffset, | |
1729 | TYPE_LENGTH (basetype)); | |
1730 | return v2; | |
1731 | } | |
1732 | v = search_struct_field (name, arg1, offset + boffset, | |
1733 | TYPE_BASECLASS (type, i), | |
d3bab255 | 1734 | looking_for_baseclass); |
bd5635a1 | 1735 | } |
01be6913 | 1736 | else if (found_baseclass) |
bd5635a1 RP |
1737 | v = value_primitive_field (arg1, offset, i, type); |
1738 | else | |
1739 | v = search_struct_field (name, arg1, | |
1740 | offset + TYPE_BASECLASS_BITPOS (type, i) / 8, | |
5e548861 | 1741 | basetype, looking_for_baseclass); |
bd5635a1 RP |
1742 | if (v) return v; |
1743 | } | |
1744 | return NULL; | |
1745 | } | |
1746 | ||
1747 | /* Helper function used by value_struct_elt to recurse through baseclasses. | |
1748 | Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes, | |
2a5ec41d | 1749 | and search in it assuming it has (class) type TYPE. |
cef4c2e7 | 1750 | If found, return value, else if name matched and args not return (value)-1, |
5b5c6d94 | 1751 | else return NULL. */ |
bd5635a1 | 1752 | |
a91a6192 | 1753 | static value_ptr |
bac89d6c | 1754 | search_struct_method (name, arg1p, args, offset, static_memfuncp, type) |
bd5635a1 | 1755 | char *name; |
a91a6192 | 1756 | register value_ptr *arg1p, *args; |
bd5635a1 RP |
1757 | int offset, *static_memfuncp; |
1758 | register struct type *type; | |
1759 | { | |
1760 | int i; | |
a91a6192 | 1761 | value_ptr v; |
67e9b3b3 | 1762 | int name_matched = 0; |
6ebc9cdd | 1763 | char dem_opname[64]; |
bd5635a1 | 1764 | |
5e548861 | 1765 | CHECK_TYPEDEF (type); |
bd5635a1 RP |
1766 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--) |
1767 | { | |
1768 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i); | |
b607efe7 | 1769 | /* FIXME! May need to check for ARM demangling here */ |
6ebc9cdd KH |
1770 | if (strncmp(t_field_name, "__", 2)==0 || |
1771 | strncmp(t_field_name, "op", 2)==0 || | |
1772 | strncmp(t_field_name, "type", 4)==0 ) | |
1773 | { | |
1774 | if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI)) | |
1775 | t_field_name = dem_opname; | |
1776 | else if (cplus_demangle_opname(t_field_name, dem_opname, 0)) | |
1777 | t_field_name = dem_opname; | |
1778 | } | |
2e4964ad | 1779 | if (t_field_name && STREQ (t_field_name, name)) |
bd5635a1 | 1780 | { |
d3bab255 | 1781 | int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1; |
bd5635a1 | 1782 | struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i); |
5b5c6d94 | 1783 | name_matched = 1; |
bd5635a1 | 1784 | |
d3bab255 JK |
1785 | if (j > 0 && args == 0) |
1786 | error ("cannot resolve overloaded method `%s'", name); | |
1787 | while (j >= 0) | |
bd5635a1 | 1788 | { |
8e9a3f3b | 1789 | if (TYPE_FN_FIELD_STUB (f, j)) |
bd5635a1 RP |
1790 | check_stub_method (type, i, j); |
1791 | if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j), | |
1792 | TYPE_FN_FIELD_ARGS (f, j), args)) | |
1793 | { | |
1794 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
a91a6192 | 1795 | return value_virtual_fn_field (arg1p, f, j, type, offset); |
bd5635a1 RP |
1796 | if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp) |
1797 | *static_memfuncp = 1; | |
a91a6192 SS |
1798 | v = value_fn_field (arg1p, f, j, type, offset); |
1799 | if (v != NULL) return v; | |
bd5635a1 | 1800 | } |
d3bab255 | 1801 | j--; |
bd5635a1 RP |
1802 | } |
1803 | } | |
1804 | } | |
1805 | ||
1806 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
1807 | { | |
01be6913 | 1808 | int base_offset; |
bd5635a1 RP |
1809 | |
1810 | if (BASETYPE_VIA_VIRTUAL (type, i)) | |
1811 | { | |
5e548861 PB |
1812 | base_offset = VALUE_OFFSET (*arg1p) + offset; |
1813 | base_offset = | |
1814 | baseclass_offset (type, i, | |
1815 | VALUE_CONTENTS (*arg1p) + base_offset, | |
1816 | VALUE_ADDRESS (*arg1p) + base_offset); | |
bac89d6c | 1817 | if (base_offset == -1) |
bd5635a1 | 1818 | error ("virtual baseclass botch"); |
bd5635a1 | 1819 | } |
01be6913 PB |
1820 | else |
1821 | { | |
01be6913 PB |
1822 | base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8; |
1823 | } | |
bac89d6c | 1824 | v = search_struct_method (name, arg1p, args, base_offset + offset, |
bd5635a1 | 1825 | static_memfuncp, TYPE_BASECLASS (type, i)); |
a91a6192 | 1826 | if (v == (value_ptr) -1) |
5b5c6d94 KH |
1827 | { |
1828 | name_matched = 1; | |
1829 | } | |
1830 | else if (v) | |
bac89d6c FF |
1831 | { |
1832 | /* FIXME-bothner: Why is this commented out? Why is it here? */ | |
1833 | /* *arg1p = arg1_tmp;*/ | |
1834 | return v; | |
1835 | } | |
bd5635a1 | 1836 | } |
a91a6192 | 1837 | if (name_matched) return (value_ptr) -1; |
5b5c6d94 | 1838 | else return NULL; |
bd5635a1 RP |
1839 | } |
1840 | ||
1841 | /* Given *ARGP, a value of type (pointer to a)* structure/union, | |
1842 | extract the component named NAME from the ultimate target structure/union | |
1843 | and return it as a value with its appropriate type. | |
1844 | ERR is used in the error message if *ARGP's type is wrong. | |
1845 | ||
1846 | C++: ARGS is a list of argument types to aid in the selection of | |
1847 | an appropriate method. Also, handle derived types. | |
1848 | ||
1849 | STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location | |
1850 | where the truthvalue of whether the function that was resolved was | |
1851 | a static member function or not is stored. | |
1852 | ||
1853 | ERR is an error message to be printed in case the field is not found. */ | |
1854 | ||
a91a6192 | 1855 | value_ptr |
bd5635a1 | 1856 | value_struct_elt (argp, args, name, static_memfuncp, err) |
a91a6192 | 1857 | register value_ptr *argp, *args; |
bd5635a1 RP |
1858 | char *name; |
1859 | int *static_memfuncp; | |
1860 | char *err; | |
1861 | { | |
1862 | register struct type *t; | |
a91a6192 | 1863 | value_ptr v; |
bd5635a1 RP |
1864 | |
1865 | COERCE_ARRAY (*argp); | |
1866 | ||
5e548861 | 1867 | t = check_typedef (VALUE_TYPE (*argp)); |
bd5635a1 RP |
1868 | |
1869 | /* Follow pointers until we get to a non-pointer. */ | |
1870 | ||
1871 | while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF) | |
1872 | { | |
bd5635a1 | 1873 | *argp = value_ind (*argp); |
f2ebc25f JK |
1874 | /* Don't coerce fn pointer to fn and then back again! */ |
1875 | if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC) | |
1876 | COERCE_ARRAY (*argp); | |
5e548861 | 1877 | t = check_typedef (VALUE_TYPE (*argp)); |
bd5635a1 RP |
1878 | } |
1879 | ||
1880 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
1881 | error ("not implemented: member type in value_struct_elt"); | |
1882 | ||
2a5ec41d | 1883 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
1884 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
1885 | error ("Attempt to extract a component of a value that is not a %s.", err); | |
1886 | ||
1887 | /* Assume it's not, unless we see that it is. */ | |
1888 | if (static_memfuncp) | |
1889 | *static_memfuncp =0; | |
1890 | ||
1891 | if (!args) | |
1892 | { | |
1893 | /* if there are no arguments ...do this... */ | |
1894 | ||
d3bab255 | 1895 | /* Try as a field first, because if we succeed, there |
bd5635a1 | 1896 | is less work to be done. */ |
d3bab255 | 1897 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1898 | if (v) |
1899 | return v; | |
1900 | ||
1901 | /* C++: If it was not found as a data field, then try to | |
1902 | return it as a pointer to a method. */ | |
1903 | ||
1904 | if (destructor_name_p (name, t)) | |
1905 | error ("Cannot get value of destructor"); | |
1906 | ||
bac89d6c | 1907 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); |
bd5635a1 | 1908 | |
a91a6192 | 1909 | if (v == (value_ptr) -1) |
67e9b3b3 PS |
1910 | error ("Cannot take address of a method"); |
1911 | else if (v == 0) | |
bd5635a1 RP |
1912 | { |
1913 | if (TYPE_NFN_FIELDS (t)) | |
1914 | error ("There is no member or method named %s.", name); | |
1915 | else | |
1916 | error ("There is no member named %s.", name); | |
1917 | } | |
1918 | return v; | |
1919 | } | |
1920 | ||
1921 | if (destructor_name_p (name, t)) | |
1922 | { | |
1923 | if (!args[1]) | |
1924 | { | |
1925 | /* destructors are a special case. */ | |
a91a6192 SS |
1926 | v = value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0), |
1927 | TYPE_FN_FIELDLIST_LENGTH (t, 0), 0, 0); | |
40620258 KH |
1928 | if (!v) error("could not find destructor function named %s.", name); |
1929 | else return v; | |
bd5635a1 RP |
1930 | } |
1931 | else | |
1932 | { | |
1933 | error ("destructor should not have any argument"); | |
1934 | } | |
1935 | } | |
1936 | else | |
bac89d6c | 1937 | v = search_struct_method (name, argp, args, 0, static_memfuncp, t); |
bd5635a1 | 1938 | |
a91a6192 | 1939 | if (v == (value_ptr) -1) |
5b5c6d94 KH |
1940 | { |
1941 | error("Argument list of %s mismatch with component in the structure.", name); | |
1942 | } | |
1943 | else if (v == 0) | |
bd5635a1 RP |
1944 | { |
1945 | /* See if user tried to invoke data as function. If so, | |
1946 | hand it back. If it's not callable (i.e., a pointer to function), | |
1947 | gdb should give an error. */ | |
d3bab255 | 1948 | v = search_struct_field (name, *argp, 0, t, 0); |
bd5635a1 RP |
1949 | } |
1950 | ||
1951 | if (!v) | |
1952 | error ("Structure has no component named %s.", name); | |
1953 | return v; | |
1954 | } | |
1955 | ||
1956 | /* C++: return 1 is NAME is a legitimate name for the destructor | |
1957 | of type TYPE. If TYPE does not have a destructor, or | |
1958 | if NAME is inappropriate for TYPE, an error is signaled. */ | |
1959 | int | |
1960 | destructor_name_p (name, type) | |
7919c3ed JG |
1961 | const char *name; |
1962 | const struct type *type; | |
bd5635a1 RP |
1963 | { |
1964 | /* destructors are a special case. */ | |
1965 | ||
1966 | if (name[0] == '~') | |
1967 | { | |
1968 | char *dname = type_name_no_tag (type); | |
6d34c236 | 1969 | char *cp = strchr (dname, '<'); |
b52cac6b | 1970 | unsigned int len; |
6d34c236 PB |
1971 | |
1972 | /* Do not compare the template part for template classes. */ | |
1973 | if (cp == NULL) | |
1974 | len = strlen (dname); | |
1975 | else | |
1976 | len = cp - dname; | |
1977 | if (strlen (name + 1) != len || !STREQN (dname, name + 1, len)) | |
bd5635a1 RP |
1978 | error ("name of destructor must equal name of class"); |
1979 | else | |
1980 | return 1; | |
1981 | } | |
1982 | return 0; | |
1983 | } | |
1984 | ||
1985 | /* Helper function for check_field: Given TYPE, a structure/union, | |
1986 | return 1 if the component named NAME from the ultimate | |
1987 | target structure/union is defined, otherwise, return 0. */ | |
1988 | ||
1989 | static int | |
1990 | check_field_in (type, name) | |
1991 | register struct type *type; | |
01be6913 | 1992 | const char *name; |
bd5635a1 RP |
1993 | { |
1994 | register int i; | |
1995 | ||
1996 | for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--) | |
1997 | { | |
1998 | char *t_field_name = TYPE_FIELD_NAME (type, i); | |
2e4964ad | 1999 | if (t_field_name && STREQ (t_field_name, name)) |
bd5635a1 RP |
2000 | return 1; |
2001 | } | |
2002 | ||
2003 | /* C++: If it was not found as a data field, then try to | |
2004 | return it as a pointer to a method. */ | |
2005 | ||
2006 | /* Destructors are a special case. */ | |
2007 | if (destructor_name_p (name, type)) | |
2008 | return 1; | |
2009 | ||
2010 | for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i) | |
2011 | { | |
2e4964ad | 2012 | if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name)) |
bd5635a1 RP |
2013 | return 1; |
2014 | } | |
2015 | ||
2016 | for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--) | |
2017 | if (check_field_in (TYPE_BASECLASS (type, i), name)) | |
2018 | return 1; | |
2019 | ||
2020 | return 0; | |
2021 | } | |
2022 | ||
2023 | ||
2024 | /* C++: Given ARG1, a value of type (pointer to a)* structure/union, | |
2025 | return 1 if the component named NAME from the ultimate | |
2026 | target structure/union is defined, otherwise, return 0. */ | |
2027 | ||
2028 | int | |
2029 | check_field (arg1, name) | |
a91a6192 | 2030 | register value_ptr arg1; |
7919c3ed | 2031 | const char *name; |
bd5635a1 RP |
2032 | { |
2033 | register struct type *t; | |
2034 | ||
2035 | COERCE_ARRAY (arg1); | |
2036 | ||
2037 | t = VALUE_TYPE (arg1); | |
2038 | ||
2039 | /* Follow pointers until we get to a non-pointer. */ | |
2040 | ||
5e548861 PB |
2041 | for (;;) |
2042 | { | |
2043 | CHECK_TYPEDEF (t); | |
2044 | if (TYPE_CODE (t) != TYPE_CODE_PTR && TYPE_CODE (t) != TYPE_CODE_REF) | |
2045 | break; | |
2046 | t = TYPE_TARGET_TYPE (t); | |
2047 | } | |
bd5635a1 RP |
2048 | |
2049 | if (TYPE_CODE (t) == TYPE_CODE_MEMBER) | |
2050 | error ("not implemented: member type in check_field"); | |
2051 | ||
2a5ec41d | 2052 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 RP |
2053 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
2054 | error ("Internal error: `this' is not an aggregate"); | |
2055 | ||
2056 | return check_field_in (t, name); | |
2057 | } | |
2058 | ||
01be6913 | 2059 | /* C++: Given an aggregate type CURTYPE, and a member name NAME, |
2a5ec41d | 2060 | return the address of this member as a "pointer to member" |
bd5635a1 RP |
2061 | type. If INTYPE is non-null, then it will be the type |
2062 | of the member we are looking for. This will help us resolve | |
01be6913 PB |
2063 | "pointers to member functions". This function is used |
2064 | to resolve user expressions of the form "DOMAIN::NAME". */ | |
bd5635a1 | 2065 | |
a91a6192 | 2066 | value_ptr |
51b57ded | 2067 | value_struct_elt_for_reference (domain, offset, curtype, name, intype) |
01be6913 | 2068 | struct type *domain, *curtype, *intype; |
51b57ded | 2069 | int offset; |
bd5635a1 RP |
2070 | char *name; |
2071 | { | |
01be6913 | 2072 | register struct type *t = curtype; |
bd5635a1 | 2073 | register int i; |
a91a6192 | 2074 | value_ptr v; |
bd5635a1 | 2075 | |
2a5ec41d | 2076 | if ( TYPE_CODE (t) != TYPE_CODE_STRUCT |
bd5635a1 | 2077 | && TYPE_CODE (t) != TYPE_CODE_UNION) |
01be6913 | 2078 | error ("Internal error: non-aggregate type to value_struct_elt_for_reference"); |
bd5635a1 | 2079 | |
01be6913 | 2080 | for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--) |
bd5635a1 | 2081 | { |
01be6913 PB |
2082 | char *t_field_name = TYPE_FIELD_NAME (t, i); |
2083 | ||
2e4964ad | 2084 | if (t_field_name && STREQ (t_field_name, name)) |
bd5635a1 | 2085 | { |
01be6913 | 2086 | if (TYPE_FIELD_STATIC (t, i)) |
bd5635a1 | 2087 | { |
01be6913 PB |
2088 | char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i); |
2089 | struct symbol *sym = | |
2090 | lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL); | |
2e4964ad FF |
2091 | if (sym == NULL) |
2092 | error ("Internal error: could not find physical static variable named %s", | |
01be6913 PB |
2093 | phys_name); |
2094 | return value_at (SYMBOL_TYPE (sym), | |
2095 | (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym)); | |
bd5635a1 | 2096 | } |
01be6913 PB |
2097 | if (TYPE_FIELD_PACKED (t, i)) |
2098 | error ("pointers to bitfield members not allowed"); | |
2099 | ||
2100 | return value_from_longest | |
2101 | (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i), | |
2102 | domain)), | |
51b57ded | 2103 | offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3)); |
bd5635a1 | 2104 | } |
bd5635a1 RP |
2105 | } |
2106 | ||
2107 | /* C++: If it was not found as a data field, then try to | |
2108 | return it as a pointer to a method. */ | |
bd5635a1 RP |
2109 | |
2110 | /* Destructors are a special case. */ | |
2111 | if (destructor_name_p (name, t)) | |
2112 | { | |
2a5ec41d | 2113 | error ("member pointers to destructors not implemented yet"); |
bd5635a1 RP |
2114 | } |
2115 | ||
2116 | /* Perform all necessary dereferencing. */ | |
2117 | while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR) | |
2118 | intype = TYPE_TARGET_TYPE (intype); | |
2119 | ||
01be6913 | 2120 | for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i) |
bd5635a1 | 2121 | { |
852b3831 PB |
2122 | char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i); |
2123 | char dem_opname[64]; | |
2124 | ||
2125 | if (strncmp(t_field_name, "__", 2)==0 || | |
2126 | strncmp(t_field_name, "op", 2)==0 || | |
2127 | strncmp(t_field_name, "type", 4)==0 ) | |
2128 | { | |
2129 | if (cplus_demangle_opname(t_field_name, dem_opname, DMGL_ANSI)) | |
2130 | t_field_name = dem_opname; | |
2131 | else if (cplus_demangle_opname(t_field_name, dem_opname, 0)) | |
2132 | t_field_name = dem_opname; | |
2133 | } | |
2134 | if (t_field_name && STREQ (t_field_name, name)) | |
bd5635a1 | 2135 | { |
01be6913 PB |
2136 | int j = TYPE_FN_FIELDLIST_LENGTH (t, i); |
2137 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i); | |
2138 | ||
2139 | if (intype == 0 && j > 1) | |
2140 | error ("non-unique member `%s' requires type instantiation", name); | |
2141 | if (intype) | |
bd5635a1 | 2142 | { |
01be6913 PB |
2143 | while (j--) |
2144 | if (TYPE_FN_FIELD_TYPE (f, j) == intype) | |
2145 | break; | |
2146 | if (j < 0) | |
2147 | error ("no member function matches that type instantiation"); | |
2148 | } | |
2149 | else | |
2150 | j = 0; | |
2151 | ||
2152 | if (TYPE_FN_FIELD_STUB (f, j)) | |
2153 | check_stub_method (t, i, j); | |
2154 | if (TYPE_FN_FIELD_VIRTUAL_P (f, j)) | |
2155 | { | |
2156 | return value_from_longest | |
2157 | (lookup_reference_type | |
2158 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
2159 | domain)), | |
13ffa6be | 2160 | (LONGEST) METHOD_PTR_FROM_VOFFSET (TYPE_FN_FIELD_VOFFSET (f, j))); |
01be6913 PB |
2161 | } |
2162 | else | |
2163 | { | |
2164 | struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j), | |
2165 | 0, VAR_NAMESPACE, 0, NULL); | |
35fcebce PB |
2166 | if (s == NULL) |
2167 | { | |
2168 | v = 0; | |
2169 | } | |
2170 | else | |
2171 | { | |
2172 | v = read_var_value (s, 0); | |
01be6913 | 2173 | #if 0 |
35fcebce PB |
2174 | VALUE_TYPE (v) = lookup_reference_type |
2175 | (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j), | |
2176 | domain)); | |
01be6913 | 2177 | #endif |
bd5635a1 | 2178 | } |
35fcebce | 2179 | return v; |
bd5635a1 RP |
2180 | } |
2181 | } | |
35fcebce | 2182 | } |
01be6913 PB |
2183 | for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--) |
2184 | { | |
a91a6192 | 2185 | value_ptr v; |
51b57ded FF |
2186 | int base_offset; |
2187 | ||
2188 | if (BASETYPE_VIA_VIRTUAL (t, i)) | |
2189 | base_offset = 0; | |
2190 | else | |
2191 | base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8; | |
01be6913 | 2192 | v = value_struct_elt_for_reference (domain, |
51b57ded | 2193 | offset + base_offset, |
01be6913 PB |
2194 | TYPE_BASECLASS (t, i), |
2195 | name, | |
2196 | intype); | |
2197 | if (v) | |
2198 | return v; | |
bd5635a1 RP |
2199 | } |
2200 | return 0; | |
2201 | } | |
2202 | ||
bd5635a1 RP |
2203 | /* C++: return the value of the class instance variable, if one exists. |
2204 | Flag COMPLAIN signals an error if the request is made in an | |
2205 | inappropriate context. */ | |
6d34c236 | 2206 | |
a91a6192 | 2207 | value_ptr |
bd5635a1 RP |
2208 | value_of_this (complain) |
2209 | int complain; | |
2210 | { | |
bd5635a1 RP |
2211 | struct symbol *func, *sym; |
2212 | struct block *b; | |
2213 | int i; | |
2214 | static const char funny_this[] = "this"; | |
a91a6192 | 2215 | value_ptr this; |
bd5635a1 RP |
2216 | |
2217 | if (selected_frame == 0) | |
2218 | if (complain) | |
2219 | error ("no frame selected"); | |
2220 | else return 0; | |
2221 | ||
2222 | func = get_frame_function (selected_frame); | |
2223 | if (!func) | |
2224 | { | |
2225 | if (complain) | |
2226 | error ("no `this' in nameless context"); | |
2227 | else return 0; | |
2228 | } | |
2229 | ||
2230 | b = SYMBOL_BLOCK_VALUE (func); | |
2231 | i = BLOCK_NSYMS (b); | |
2232 | if (i <= 0) | |
2233 | if (complain) | |
2234 | error ("no args, no `this'"); | |
2235 | else return 0; | |
2236 | ||
2237 | /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER | |
2238 | symbol instead of the LOC_ARG one (if both exist). */ | |
2239 | sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE); | |
2240 | if (sym == NULL) | |
2241 | { | |
2242 | if (complain) | |
2243 | error ("current stack frame not in method"); | |
2244 | else | |
2245 | return NULL; | |
2246 | } | |
2247 | ||
2248 | this = read_var_value (sym, selected_frame); | |
2249 | if (this == 0 && complain) | |
2250 | error ("`this' argument at unknown address"); | |
2251 | return this; | |
2252 | } | |
a91a6192 | 2253 | |
f91a9e05 PB |
2254 | /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH elements |
2255 | long, starting at LOWBOUND. The result has the same lower bound as | |
2256 | the original ARRAY. */ | |
2257 | ||
2258 | value_ptr | |
2259 | value_slice (array, lowbound, length) | |
2260 | value_ptr array; | |
2261 | int lowbound, length; | |
2262 | { | |
5f3e7bfc PB |
2263 | struct type *slice_range_type, *slice_type, *range_type; |
2264 | LONGEST lowerbound, upperbound, offset; | |
2265 | value_ptr slice; | |
5e548861 PB |
2266 | struct type *array_type; |
2267 | array_type = check_typedef (VALUE_TYPE (array)); | |
2268 | COERCE_VARYING_ARRAY (array, array_type); | |
5e548861 | 2269 | if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY |
5f3e7bfc PB |
2270 | && TYPE_CODE (array_type) != TYPE_CODE_STRING |
2271 | && TYPE_CODE (array_type) != TYPE_CODE_BITSTRING) | |
f91a9e05 | 2272 | error ("cannot take slice of non-array"); |
5f3e7bfc PB |
2273 | range_type = TYPE_INDEX_TYPE (array_type); |
2274 | if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0) | |
2275 | error ("slice from bad array or bitstring"); | |
2276 | if (lowbound < lowerbound || length < 0 | |
2277 | || lowbound + length - 1 > upperbound | |
2278 | /* Chill allows zero-length strings but not arrays. */ | |
2279 | || (current_language->la_language == language_chill | |
2280 | && length == 0 && TYPE_CODE (array_type) == TYPE_CODE_ARRAY)) | |
2281 | error ("slice out of range"); | |
2282 | /* FIXME-type-allocation: need a way to free this type when we are | |
2283 | done with it. */ | |
2284 | slice_range_type = create_range_type ((struct type*) NULL, | |
2285 | TYPE_TARGET_TYPE (range_type), | |
b607efe7 | 2286 | lowbound, lowbound + length - 1); |
5f3e7bfc PB |
2287 | if (TYPE_CODE (array_type) == TYPE_CODE_BITSTRING) |
2288 | { | |
2289 | int i; | |
2290 | slice_type = create_set_type ((struct type*) NULL, slice_range_type); | |
2291 | TYPE_CODE (slice_type) = TYPE_CODE_BITSTRING; | |
2292 | slice = value_zero (slice_type, not_lval); | |
2293 | for (i = 0; i < length; i++) | |
2294 | { | |
2295 | int element = value_bit_index (array_type, | |
2296 | VALUE_CONTENTS (array), | |
2297 | lowbound + i); | |
2298 | if (element < 0) | |
2299 | error ("internal error accessing bitstring"); | |
2300 | else if (element > 0) | |
2301 | { | |
2302 | int j = i % TARGET_CHAR_BIT; | |
2303 | if (BITS_BIG_ENDIAN) | |
2304 | j = TARGET_CHAR_BIT - 1 - j; | |
2305 | VALUE_CONTENTS_RAW (slice)[i / TARGET_CHAR_BIT] |= (1 << j); | |
2306 | } | |
2307 | } | |
2308 | /* We should set the address, bitssize, and bitspos, so the clice | |
2309 | can be used on the LHS, but that may require extensions to | |
2310 | value_assign. For now, just leave as a non_lval. FIXME. */ | |
2311 | } | |
f91a9e05 PB |
2312 | else |
2313 | { | |
5e548861 | 2314 | struct type *element_type = TYPE_TARGET_TYPE (array_type); |
5e548861 PB |
2315 | offset |
2316 | = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type)); | |
f91a9e05 PB |
2317 | slice_type = create_array_type ((struct type*) NULL, element_type, |
2318 | slice_range_type); | |
5e548861 | 2319 | TYPE_CODE (slice_type) = TYPE_CODE (array_type); |
f91a9e05 PB |
2320 | slice = allocate_value (slice_type); |
2321 | if (VALUE_LAZY (array)) | |
2322 | VALUE_LAZY (slice) = 1; | |
2323 | else | |
2324 | memcpy (VALUE_CONTENTS (slice), VALUE_CONTENTS (array) + offset, | |
2325 | TYPE_LENGTH (slice_type)); | |
2326 | if (VALUE_LVAL (array) == lval_internalvar) | |
2327 | VALUE_LVAL (slice) = lval_internalvar_component; | |
2328 | else | |
2329 | VALUE_LVAL (slice) = VALUE_LVAL (array); | |
2330 | VALUE_ADDRESS (slice) = VALUE_ADDRESS (array); | |
2331 | VALUE_OFFSET (slice) = VALUE_OFFSET (array) + offset; | |
f91a9e05 | 2332 | } |
5f3e7bfc | 2333 | return slice; |
f91a9e05 PB |
2334 | } |
2335 | ||
2336 | /* Assuming chill_varying_type (VARRAY) is true, return an equivalent | |
2337 | value as a fixed-length array. */ | |
2338 | ||
2339 | value_ptr | |
2340 | varying_to_slice (varray) | |
2341 | value_ptr varray; | |
2342 | { | |
5e548861 | 2343 | struct type *vtype = check_typedef (VALUE_TYPE (varray)); |
f91a9e05 PB |
2344 | LONGEST length = unpack_long (TYPE_FIELD_TYPE (vtype, 0), |
2345 | VALUE_CONTENTS (varray) | |
2346 | + TYPE_FIELD_BITPOS (vtype, 0) / 8); | |
2347 | return value_slice (value_primitive_field (varray, 0, 1, vtype), 0, length); | |
2348 | } | |
2349 | ||
a91a6192 SS |
2350 | /* Create a value for a FORTRAN complex number. Currently most of |
2351 | the time values are coerced to COMPLEX*16 (i.e. a complex number | |
2352 | composed of 2 doubles. This really should be a smarter routine | |
2353 | that figures out precision inteligently as opposed to assuming | |
2354 | doubles. FIXME: fmb */ | |
2355 | ||
2356 | value_ptr | |
5222ca60 | 2357 | value_literal_complex (arg1, arg2, type) |
a91a6192 SS |
2358 | value_ptr arg1; |
2359 | value_ptr arg2; | |
5222ca60 | 2360 | struct type *type; |
a91a6192 | 2361 | { |
a91a6192 | 2362 | register value_ptr val; |
5222ca60 | 2363 | struct type *real_type = TYPE_TARGET_TYPE (type); |
a91a6192 | 2364 | |
5222ca60 PB |
2365 | val = allocate_value (type); |
2366 | arg1 = value_cast (real_type, arg1); | |
2367 | arg2 = value_cast (real_type, arg2); | |
a91a6192 | 2368 | |
5222ca60 PB |
2369 | memcpy (VALUE_CONTENTS_RAW (val), |
2370 | VALUE_CONTENTS (arg1), TYPE_LENGTH (real_type)); | |
2371 | memcpy (VALUE_CONTENTS_RAW (val) + TYPE_LENGTH (real_type), | |
2372 | VALUE_CONTENTS (arg2), TYPE_LENGTH (real_type)); | |
a91a6192 SS |
2373 | return val; |
2374 | } | |
9ed8604f | 2375 | |
5222ca60 | 2376 | /* Cast a value into the appropriate complex data type. */ |
9ed8604f PS |
2377 | |
2378 | static value_ptr | |
5222ca60 | 2379 | cast_into_complex (type, val) |
9ed8604f PS |
2380 | struct type *type; |
2381 | register value_ptr val; | |
2382 | { | |
5222ca60 PB |
2383 | struct type *real_type = TYPE_TARGET_TYPE (type); |
2384 | if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_COMPLEX) | |
9ed8604f | 2385 | { |
5222ca60 PB |
2386 | struct type *val_real_type = TYPE_TARGET_TYPE (VALUE_TYPE (val)); |
2387 | value_ptr re_val = allocate_value (val_real_type); | |
2388 | value_ptr im_val = allocate_value (val_real_type); | |
9ed8604f | 2389 | |
5222ca60 PB |
2390 | memcpy (VALUE_CONTENTS_RAW (re_val), |
2391 | VALUE_CONTENTS (val), TYPE_LENGTH (val_real_type)); | |
2392 | memcpy (VALUE_CONTENTS_RAW (im_val), | |
2393 | VALUE_CONTENTS (val) + TYPE_LENGTH (val_real_type), | |
2394 | TYPE_LENGTH (val_real_type)); | |
9ed8604f | 2395 | |
5222ca60 | 2396 | return value_literal_complex (re_val, im_val, type); |
9ed8604f | 2397 | } |
5222ca60 PB |
2398 | else if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT |
2399 | || TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT) | |
2400 | return value_literal_complex (val, value_zero (real_type, not_lval), type); | |
9ed8604f | 2401 | else |
5222ca60 | 2402 | error ("cannot cast non-number to complex"); |
9ed8604f | 2403 | } |
5e548861 PB |
2404 | |
2405 | void | |
2406 | _initialize_valops () | |
2407 | { | |
2408 | #if 0 | |
2409 | add_show_from_set | |
2410 | (add_set_cmd ("abandon", class_support, var_boolean, (char *)&auto_abandon, | |
2411 | "Set automatic abandonment of expressions upon failure.", | |
2412 | &setlist), | |
2413 | &showlist); | |
2414 | #endif | |
2415 | } |