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