1 /* Perform non-arithmetic operations on values, for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
29 /* Cast value ARG2 to type TYPE and return as a value.
30 More general than a C cast: accepts any two types of the same length,
31 and if ARG2 is an lvalue it can be cast into anything at all. */
34 value_cast (type, arg2)
38 register enum type_code code1;
39 register enum type_code code2;
42 /* Coerce arrays but not enums. Enums will work as-is
43 and coercing them would cause an infinite recursion. */
44 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
47 code1 = TYPE_CODE (type);
48 code2 = TYPE_CODE (VALUE_TYPE (arg2));
49 scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
50 || code2 == TYPE_CODE_ENUM);
52 if (code1 == TYPE_CODE_FLT && scalar)
53 return value_from_double (type, value_as_double (arg2));
54 else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
55 && (scalar || code2 == TYPE_CODE_PTR))
56 return value_from_long (type, value_as_long (arg2));
57 else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
59 VALUE_TYPE (arg2) = type;
62 else if (VALUE_LVAL (arg2) == lval_memory)
63 return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
65 error ("Invalid cast.");
68 /* Return the value with a specified type located at specified address. */
75 register value val = allocate_value (type);
77 read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
78 VALUE_LVAL (val) = lval_memory;
79 VALUE_ADDRESS (val) = addr;
84 /* Store the contents of FROMVAL into the location of TOVAL.
85 Return a new value with the location of TOVAL and contents of FROMVAL. */
88 value_assign (toval, fromval)
89 register value toval, fromval;
91 register struct type *type = VALUE_TYPE (toval);
93 char raw_buffer[MAX_REGISTER_RAW_SIZE];
94 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
97 COERCE_ARRAY (fromval);
99 if (VALUE_LVAL (toval) != lval_internalvar)
100 fromval = value_cast (type, fromval);
102 /* If TOVAL is a special machine register requiring conversion
103 of program values to a special raw format,
104 convert FROMVAL's contents now, with result in `raw_buffer',
105 and set USE_BUFFER to the number of bytes to write. */
107 if (VALUE_REGNO (toval) >= 0
108 && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
110 int regno = VALUE_REGNO (toval);
111 if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
112 fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
113 bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
114 REGISTER_VIRTUAL_SIZE (regno));
115 REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
116 use_buffer = REGISTER_RAW_SIZE (regno);
119 switch (VALUE_LVAL (toval))
121 case lval_internalvar:
122 set_internalvar (VALUE_INTERNALVAR (toval), fromval);
125 case lval_internalvar_component:
126 set_internalvar_component (VALUE_INTERNALVAR (toval),
127 VALUE_OFFSET (toval),
128 VALUE_BITPOS (toval),
129 VALUE_BITSIZE (toval),
134 if (VALUE_BITSIZE (toval))
137 read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
139 modify_field (&val, value_as_long (fromval),
140 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
141 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
145 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
146 raw_buffer, use_buffer);
148 write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
149 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
153 if (VALUE_BITSIZE (toval))
157 read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
159 modify_field (&val, value_as_long (fromval),
160 VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
161 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
165 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
166 raw_buffer, use_buffer);
168 write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
169 VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
173 error ("Left side of = operation is not an lvalue.");
176 /* Return a value just like TOVAL except with the contents of FROMVAL. */
178 val = allocate_value (type);
179 bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
180 bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
185 /* Extend a value VAL to COUNT repetitions of its type. */
188 value_repeat (arg1, count)
194 if (VALUE_LVAL (arg1) != lval_memory)
195 error ("Only values in memory can be extended with '@'.");
197 error ("Invalid number %d of repetitions.", count);
199 val = allocate_repeat_value (VALUE_TYPE (arg1), count);
201 read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
202 VALUE_CONTENTS (val),
203 TYPE_LENGTH (VALUE_TYPE (val)) * count);
204 VALUE_LVAL (val) = lval_memory;
205 VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
211 value_of_variable (var)
214 return read_var_value (var, (CORE_ADDR) 0);
217 /* Given a value which is an array, return a value which is
218 a pointer to its first element. */
221 value_coerce_array (arg1)
224 register struct type *type;
227 if (VALUE_LVAL (arg1) != lval_memory)
228 error ("Attempt to take address of value not located in memory.");
230 /* Get type of elements. */
231 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
232 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
234 /* A phony array made by value_repeat.
235 Its type is the type of the elements, not an array type. */
236 type = VALUE_TYPE (arg1);
238 /* Get the type of the result. */
239 type = lookup_pointer_type (type);
240 val = value_from_long (builtin_type_long,
241 VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
242 VALUE_TYPE (val) = type;
246 /* Return a pointer value for the object for which ARG1 is the contents. */
252 register struct type *type;
253 register value val, arg1_coerced;
255 /* Taking the address of an array is really a no-op
256 once the array is coerced to a pointer to its first element. */
258 COERCE_ARRAY (arg1_coerced);
259 if (arg1 != arg1_coerced)
262 if (VALUE_LVAL (arg1) != lval_memory)
263 error ("Attempt to take address of value not located in memory.");
265 /* Get the type of the result. */
266 type = lookup_pointer_type (VALUE_TYPE (arg1));
267 val = value_from_long (builtin_type_long,
268 VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
269 VALUE_TYPE (val) = type;
273 /* Given a value of a pointer type, apply the C unary * operator to it. */
281 /* Allow * on an integer so we can cast it to whatever we want. */
282 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
283 return value_at (builtin_type_long,
284 (CORE_ADDR) value_as_long (arg1));
285 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
286 return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
287 (CORE_ADDR) value_as_long (arg1));
288 error ("Attempt to take contents of a non-pointer value.");
291 /* Pushing small parts of stack frames. */
293 /* Push one word (the size of object that a register holds). */
296 push_word (sp, buffer)
298 REGISTER_TYPE buffer;
300 register int len = sizeof (REGISTER_TYPE);
304 write_memory (sp, &buffer, len);
305 #else /* stack grows upward */
306 write_memory (sp, &buffer, len);
308 #endif /* stack grows upward */
313 /* Push LEN bytes with data at BUFFER. */
316 push_bytes (sp, buffer, len)
323 write_memory (sp, buffer, len);
324 #else /* stack grows upward */
325 write_memory (sp, buffer, len);
327 #endif /* stack grows upward */
332 /* Push onto the stack the specified value VALUE. */
336 register CORE_ADDR sp;
339 register int len = TYPE_LENGTH (VALUE_TYPE (arg));
343 write_memory (sp, VALUE_CONTENTS (arg), len);
344 #else /* stack grows upward */
345 write_memory (sp, VALUE_CONTENTS (arg), len);
347 #endif /* stack grows upward */
352 /* Perform the standard coercions that are specified
353 for arguments to be passed to C functions. */
356 value_arg_coerce (arg)
359 register struct type *type;
363 type = VALUE_TYPE (arg);
365 if (TYPE_CODE (type) == TYPE_CODE_INT
366 && TYPE_LENGTH (type) < sizeof (int))
367 return value_cast (builtin_type_int, arg);
369 if (type == builtin_type_float)
370 return value_cast (builtin_type_double, arg);
375 /* Push the value ARG, first coercing it as an argument
379 value_arg_push (sp, arg)
380 register CORE_ADDR sp;
383 return value_push (sp, value_arg_coerce (arg));
386 /* Perform a function call in the inferior.
387 ARGS is a vector of values of arguments (NARGS of them).
388 FUNCTION is a value, the function to be called.
389 Returns a value representing what the function returned.
390 May fail to return, if a breakpoint or signal is hit
391 during the execution of the function. */
394 call_function (function, nargs, args)
399 register CORE_ADDR sp;
402 static REGISTER_TYPE dummy[] = CALL_DUMMY;
403 REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
405 struct type *value_type;
410 register CORE_ADDR funaddr;
411 register struct type *ftype = VALUE_TYPE (function);
412 register enum type_code code = TYPE_CODE (ftype);
414 /* Determine address to call. */
415 if (code == TYPE_CODE_FUNC)
417 funaddr = VALUE_ADDRESS (function);
418 value_type = TYPE_TARGET_TYPE (ftype);
420 else if (code == TYPE_CODE_PTR)
422 funaddr = value_as_long (function);
423 if (TYPE_CODE (TYPE_TARGET_TYPE (ftype))
425 value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
427 value_type = builtin_type_int;
429 else if (code == TYPE_CODE_INT)
431 /* Handle the case of functions lacking debugging info.
432 Their values are characters since their addresses are char */
433 if (TYPE_LENGTH (ftype) == 1)
434 funaddr = value_as_long (value_addr (function));
436 /* Handle integer used as address of a function. */
437 funaddr = value_as_long (function);
439 value_type = builtin_type_int;
442 error ("Invalid data type for function to be called.");
444 /* Create a call sequence customized for this function
445 and the number of arguments for it. */
446 bcopy (dummy, dummy1, sizeof dummy);
447 FIX_CALL_DUMMY (dummy1, funaddr, nargs);
450 old_sp = sp = read_register (SP_REGNUM);
452 #if 1 INNER_THAN 2 /* Stack grows down */
454 write_memory (sp, dummy1, sizeof dummy);
456 for (i = nargs - 1; i >= 0; i--)
457 sp = value_arg_push (sp, args[i]);
458 #else /* Stack grows up */
460 write_memory (sp, dummy1, sizeof dummy);
462 for (i = 0; i < nargs; i++)
463 sp = value_arg_push (sp, args[i]);
464 #endif /* Stack grows up */
466 write_register (SP_REGNUM, sp);
468 /* Figure out the value returned by the function. */
470 char retbuf[REGISTER_BYTES];
472 /* Execute the stack dummy routine, calling FUNCTION.
473 When it is done, discard the empty frame
474 after storing the contents of all regs into retbuf. */
475 run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
477 return value_being_returned (value_type, retbuf);
481 /* Create a value for a string constant:
482 Call the function malloc in the inferior to get space for it,
483 then copy the data into that space
484 and then return the address with type char *.
485 PTR points to the string constant data; LEN is number of characters. */
488 value_string (ptr, len)
493 register struct symbol *sym;
495 register char *copy = (char *) alloca (len + 1);
497 register char *o = copy, *ibeg = ptr;
500 /* Copy the string into COPY, processing escapes.
501 We could not conveniently process them in expread
502 because the string there wants to be a substring of the input. */
504 while (i - ibeg < len)
509 c = parse_escape (&i);
517 /* Get the length of the string after escapes are processed. */
521 /* Find the address of malloc in the inferior. */
523 sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE);
526 if (SYMBOL_CLASS (sym) != LOC_BLOCK)
527 error ("\"malloc\" exists in this program but is not a function.");
528 val = value_of_variable (sym);
533 for (i = 0; i < misc_function_count; i++)
534 if (!strcmp (misc_function_vector[i].name, "malloc"))
536 if (i < misc_function_count)
537 val = value_from_long (builtin_type_long,
538 misc_function_vector[i].address);
540 error ("String constants require the program to have a function \"malloc\".");
543 blocklen = value_from_long (builtin_type_int, len + 1);
544 val = call_function (val, 1, &blocklen);
545 if (value_zerop (val))
546 error ("No memory available for string constant.");
547 write_memory (value_as_long (val), copy, len + 1);
548 VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
552 /* Given ARG1, a value of type (pointer to a)* structure/union,
553 extract the component named NAME from the ultimate target structure/union
554 and return it as a value with its appropriate type.
555 ERR is used in the error message if ARG1's type is wrong. */
558 value_struct_elt (arg1, name, err)
563 register struct type *t;
568 t = VALUE_TYPE (arg1);
570 /* Follow pointers until we get to a non-pointer. */
572 while (TYPE_CODE (t) == TYPE_CODE_PTR)
574 arg1 = value_ind (arg1);
576 t = VALUE_TYPE (arg1);
579 if (TYPE_CODE (t) != TYPE_CODE_STRUCT
581 TYPE_CODE (t) != TYPE_CODE_UNION)
582 error ("Attempt to extract a component of a value that is not a %s.", err);
584 for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
586 if (!strcmp (TYPE_FIELD_NAME (t, i), name))
591 error ("Structure has no component named %s.", name);
593 return value_field (arg1, i);