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