valops.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /* Perform non-arithmetic operations on values, for GDB.
  2. Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the GDB General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute GDB,
  9. but only under the conditions described in the GDB General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with GDB so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, go ahead and share GDB, but don't try to stop
  15. anyone else from sharing it farther. Help stamp out software hoarding!
  16. */
  17. #include "defs.h"
  18. #include "initialize.h"
  19. #include "param.h"
  20. #include "symtab.h"
  21. #include "value.h"
  22. START_FILE
  23. /* Cast value ARG2 to type TYPE and return as a value.
  24. More general than a C cast: accepts any two types of the same length,
  25. and if ARG2 is an lvalue it can be cast into anything at all. */
  26. value
  27. value_cast (type, arg2)
  28. struct type *type;
  29. register value arg2;
  30. {
  31. register enum type_code code1;
  32. register enum type_code code2;
  33. register int scalar;
  34. COERCE_ARRAY (arg2);
  35. code1 = TYPE_CODE (type);
  36. code2 = TYPE_CODE (VALUE_TYPE (arg2));
  37. scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
  38. || code2 == TYPE_CODE_ENUM);
  39. if (code1 == TYPE_CODE_FLT && scalar)
  40. return value_from_double (type, value_as_double (arg2));
  41. else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
  42. && (scalar || code2 == TYPE_CODE_PTR))
  43. return value_from_long (type, value_as_long (arg2));
  44. else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
  45. {
  46. VALUE_TYPE (arg2) = type;
  47. return arg2;
  48. }
  49. else if (VALUE_LVAL (arg2) == lval_memory)
  50. return value_at (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
  51. else
  52. error ("Invalid cast.");
  53. }
  54. /* Return the value with a specified type located at specified address. */
  55. value
  56. value_at (type, addr)
  57. struct type *type;
  58. CORE_ADDR addr;
  59. {
  60. register value val = allocate_value (type);
  61. read_memory (addr, VALUE_CONTENTS (val), TYPE_LENGTH (type));
  62. VALUE_LVAL (val) = lval_memory;
  63. VALUE_ADDRESS (val) = addr;
  64. return val;
  65. }
  66. /* Store the contents of FROMVAL into the location of TOVAL.
  67. Return a new value with the location of TOVAL and contents of FROMVAL. */
  68. value
  69. value_assign (toval, fromval)
  70. register value toval, fromval;
  71. {
  72. register struct type *type = VALUE_TYPE (toval);
  73. register value val;
  74. char raw_buffer[MAX_REGISTER_RAW_SIZE];
  75. char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  76. int use_buffer = 0;
  77. COERCE_ARRAY (toval);
  78. COERCE_ARRAY (fromval);
  79. if (VALUE_LVAL (toval) != lval_internalvar)
  80. fromval = value_cast (type, fromval);
  81. /* If TOVAL is a special machine register requiring conversion
  82. of program values to a special raw format,
  83. convert FROMVAL's contents now, with result in `raw_buffer',
  84. and set USE_BUFFER to the number of bytes to write. */
  85. if (VALUE_REGNO (toval) >= 0
  86. && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
  87. {
  88. int regno = VALUE_REGNO (toval);
  89. if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
  90. fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
  91. bcopy (VALUE_CONTENTS (fromval), virtual_buffer,
  92. REGISTER_VIRTUAL_SIZE (regno));
  93. REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
  94. use_buffer = REGISTER_RAW_SIZE (regno);
  95. }
  96. switch (VALUE_LVAL (toval))
  97. {
  98. case lval_internalvar:
  99. set_internalvar (VALUE_INTERNALVAR (toval), fromval);
  100. break;
  101. case lval_internalvar_component:
  102. set_internalvar_component (VALUE_INTERNALVAR (toval),
  103. VALUE_OFFSET (toval),
  104. VALUE_BITPOS (toval),
  105. VALUE_BITSIZE (toval),
  106. fromval);
  107. break;
  108. case lval_memory:
  109. if (VALUE_BITSIZE (toval))
  110. {
  111. int val;
  112. read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  113. &val, sizeof val);
  114. modify_field (&val, value_as_long (fromval),
  115. VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  116. write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  117. &val, sizeof val);
  118. }
  119. else if (use_buffer)
  120. write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  121. raw_buffer, use_buffer);
  122. else
  123. write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  124. VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  125. break;
  126. case lval_register:
  127. if (VALUE_BITSIZE (toval))
  128. {
  129. int val;
  130. read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  131. &val, sizeof val);
  132. modify_field (&val, value_as_long (fromval),
  133. VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  134. write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  135. &val, sizeof val);
  136. }
  137. else if (use_buffer)
  138. write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  139. raw_buffer, use_buffer);
  140. else
  141. write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  142. VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  143. break;
  144. default:
  145. error ("Left side of = operation is not an lvalue.");
  146. }
  147. /* Return a value just like TOVAL except with the contents of FROMVAL. */
  148. val = allocate_value (type);
  149. bcopy (toval, val, VALUE_CONTENTS (val) - (char *) val);
  150. bcopy (VALUE_CONTENTS (fromval), VALUE_CONTENTS (val), TYPE_LENGTH (type));
  151. return val;
  152. }
  153. /* Extend a value VAL to COUNT repetitions of its type. */
  154. value
  155. value_repeat (arg1, count)
  156. value arg1;
  157. int count;
  158. {
  159. register value val;
  160. if (VALUE_LVAL (arg1) != lval_memory)
  161. error ("Only values in memory can be extended with '@'.");
  162. if (count < 1)
  163. error ("Invalid number %d of repetitions.", count);
  164. val = allocate_repeat_value (VALUE_TYPE (arg1), count);
  165. read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
  166. VALUE_CONTENTS (val),
  167. TYPE_LENGTH (VALUE_TYPE (val)) * count);
  168. VALUE_LVAL (val) = lval_memory;
  169. VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
  170. return val;
  171. }
  172. value
  173. value_of_variable (var)
  174. struct symbol *var;
  175. {
  176. return read_var_value (var, (CORE_ADDR) 0);
  177. }
  178. /* Given a value which is an array, return a value which is
  179. a pointer to its first element. */
  180. value
  181. value_coerce_array (arg1)
  182. value arg1;
  183. {
  184. register struct type *type;
  185. register value val;
  186. if (VALUE_LVAL (arg1) != lval_memory)
  187. error ("Attempt to take address of value not located in memory.");
  188. /* Get type of elements. */
  189. if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
  190. type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
  191. else
  192. /* A phony array made by value_repeat.
  193. Its type is the type of the elements, not an array type. */
  194. type = VALUE_TYPE (arg1);
  195. /* Get the type of the result. */
  196. type = lookup_pointer_type (type);
  197. val = value_from_long (builtin_type_long,
  198. VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
  199. VALUE_TYPE (val) = type;
  200. return val;
  201. }
  202. /* Return a pointer value for the object for which ARG1 is the contents. */
  203. value
  204. value_addr (arg1)
  205. value arg1;
  206. {
  207. register struct type *type;
  208. register value val, arg1_coerced;
  209. /* Taking the address of an array is really a no-op
  210. once the array is coerced to a pointer to its first element. */
  211. arg1_coerced = arg1;
  212. COERCE_ARRAY (arg1_coerced);
  213. if (arg1 != arg1_coerced)
  214. return arg1_coerced;
  215. if (VALUE_LVAL (arg1) != lval_memory)
  216. error ("Attempt to take address of value not located in memory.");
  217. /* Get the type of the result. */
  218. type = lookup_pointer_type (VALUE_TYPE (arg1));
  219. val = value_from_long (builtin_type_long,
  220. VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1));
  221. VALUE_TYPE (val) = type;
  222. return val;
  223. }
  224. /* Given a value of a pointer type, apply the C unary * operator to it. */
  225. value
  226. value_ind (arg1)
  227. value arg1;
  228. {
  229. COERCE_ARRAY (arg1);
  230. /* Allow * on an integer so we can cast it to whatever we want. */
  231. if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
  232. return value_at (builtin_type_long,
  233. (CORE_ADDR) value_as_long (arg1));
  234. else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  235. return value_at (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  236. (CORE_ADDR) value_as_long (arg1));
  237. error ("Attempt to take contents of a non-pointer value.");
  238. }
  239. /* Pushing small parts of stack frames. */
  240. /* Push one word (the size of object that a register holds). */
  241. CORE_ADDR
  242. push_word (sp, buffer)
  243. CORE_ADDR sp;
  244. REGISTER_TYPE buffer;
  245. {
  246. register int len = sizeof (REGISTER_TYPE);
  247. #if 1 INNER_THAN 2
  248. sp -= len;
  249. write_memory (sp, &buffer, len);
  250. #else /* stack grows upward */
  251. write_memory (sp, &buffer, len);
  252. sp += len;
  253. #endif /* stack grows upward */
  254. return sp;
  255. }
  256. /* Push LEN bytes with data at BUFFER. */
  257. CORE_ADDR
  258. push_bytes (sp, buffer, len)
  259. CORE_ADDR sp;
  260. char *buffer;
  261. int len;
  262. {
  263. #if 1 INNER_THAN 2
  264. sp -= len;
  265. write_memory (sp, buffer, len);
  266. #else /* stack grows upward */
  267. write_memory (sp, buffer, len);
  268. sp += len;
  269. #endif /* stack grows upward */
  270. return sp;
  271. }
  272. /* Push onto the stack the specified value VALUE. */
  273. CORE_ADDR
  274. value_push (sp, arg)
  275. register CORE_ADDR sp;
  276. value arg;
  277. {
  278. register int len = TYPE_LENGTH (VALUE_TYPE (arg));
  279. #if 1 INNER_THAN 2
  280. sp -= len;
  281. write_memory (sp, VALUE_CONTENTS (arg), len);
  282. #else /* stack grows upward */
  283. write_memory (sp, VALUE_CONTENTS (arg), len);
  284. sp += len;
  285. #endif /* stack grows upward */
  286. return sp;
  287. }
  288. /* Perform the standard coercions that are specified
  289. for arguments to be passed to C functions. */
  290. value
  291. value_arg_coerce (arg)
  292. value arg;
  293. {
  294. register struct type *type = VALUE_TYPE (arg);
  295. if (TYPE_CODE (type) == TYPE_CODE_INT
  296. && TYPE_LENGTH (type) < sizeof (int))
  297. return value_cast (builtin_type_int, arg);
  298. if (type == builtin_type_float)
  299. return value_cast (builtin_type_double, arg);
  300. return arg;
  301. }
  302. /* Push the value ARG, first coercing it as an argument
  303. to a C function. */
  304. CORE_ADDR
  305. value_arg_push (sp, arg)
  306. register CORE_ADDR sp;
  307. value arg;
  308. {
  309. return value_push (sp, value_arg_coerce (arg));
  310. }
  311. /* Perform a function call in the inferior.
  312. ARGS is a vector of values of arguments (NARGS of them).
  313. FUNCTION is a value, the function to be called.
  314. Returns a value representing what the function returned.
  315. May fail to return, if a breakpoint or signal is hit
  316. during the execution of the function. */
  317. value
  318. call_function (function, nargs, args)
  319. value function;
  320. int nargs;
  321. value *args;
  322. {
  323. register CORE_ADDR sp;
  324. register int i;
  325. CORE_ADDR start_sp;
  326. static REGISTER_TYPE dummy[] = CALL_DUMMY;
  327. REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
  328. CORE_ADDR old_sp;
  329. struct type *value_type;
  330. PUSH_DUMMY_FRAME;
  331. {
  332. register CORE_ADDR funaddr;
  333. register struct type *ftype = VALUE_TYPE (function);
  334. register enum type_code code = TYPE_CODE (ftype);
  335. /* Determine address to call. */
  336. if (code == TYPE_CODE_FUNC)
  337. {
  338. funaddr = VALUE_ADDRESS (function);
  339. value_type = TYPE_TARGET_TYPE (ftype);
  340. }
  341. else if (code == TYPE_CODE_PTR)
  342. {
  343. funaddr = value_as_long (function);
  344. if (TYPE_CODE (TYPE_TARGET_TYPE (ftype))
  345. == TYPE_CODE_FUNC)
  346. value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
  347. else
  348. value_type = builtin_type_int;
  349. }
  350. else if (code == TYPE_CODE_INT)
  351. {
  352. /* Handle the case of functions lacking debugging info.
  353. Their values are characters since their addresses are char */
  354. if (TYPE_LENGTH (ftype) == 1)
  355. funaddr = value_as_long (value_addr (function));
  356. else
  357. /* Handle integer used as address of a function. */
  358. funaddr = value_as_long (function);
  359. value_type = builtin_type_int;
  360. }
  361. else
  362. error ("Invalid data type for function to be called.");
  363. /* Create a call sequence customized for this function
  364. and the number of arguments for it. */
  365. bcopy (dummy, dummy1, sizeof dummy);
  366. FIX_CALL_DUMMY (dummy1, funaddr, nargs);
  367. }
  368. old_sp = sp = read_register (SP_REGNUM);
  369. #if 1 INNER_THAN 2 /* Stack grows down */
  370. sp -= sizeof dummy;
  371. write_memory (sp, dummy1, sizeof dummy);
  372. start_sp = sp;
  373. for (i = nargs - 1; i >= 0; i--)
  374. sp = value_arg_push (sp, args[i]);
  375. #else /* Stack grows up */
  376. start_sp = sp;
  377. write_memory (sp, dummy1, sizeof dummy);
  378. sp += sizeof dummy;
  379. for (i = 0; i < nargs; i++)
  380. sp = value_arg_push (sp, args[i]);
  381. #endif /* Stack grows up */
  382. write_register (SP_REGNUM, sp);
  383. /* Figure out the value returned by the function. */
  384. {
  385. REGISTER_TYPE retbuf[2];
  386. /* Execute the stack dummy routine, calling FUNCTION.
  387. When it is done, discard the empty frame
  388. after storing the contents of r0 and r1 into retbuf. */
  389. #ifndef CALL_DUMMY_START_OFFSET
  390. #define CALL_DUMMY_START_OFFSET 0
  391. #endif
  392. run_stack_dummy (start_sp + CALL_DUMMY_START_OFFSET, retbuf);
  393. return value_being_returned (value_type, retbuf);
  394. }
  395. }
  396. /* Create a value for a string constant:
  397. Call the function malloc in the inferior to get space for it,
  398. then copy the data into that space
  399. and then return the address with type char *.
  400. PTR points to the string constant data; LEN is number of characters. */
  401. value
  402. value_string (ptr, len)
  403. char *ptr;
  404. int len;
  405. {
  406. register value val;
  407. register struct symbol *sym;
  408. value blocklen;
  409. register char *copy = (char *) alloca (len + 1);
  410. char *i = ptr;
  411. register char *o = copy, *ibeg = ptr;
  412. register int c;
  413. /* Copy the string into COPY, processing escapes.
  414. We could not conveniently process them in expread
  415. because the string there wants to be a substring of the input. */
  416. while (i - ibeg < len)
  417. {
  418. c = *i++;
  419. if (c == '\\')
  420. {
  421. c = parse_escape (&i);
  422. if (c == -1)
  423. continue;
  424. }
  425. *o++ = c;
  426. }
  427. *o = 0;
  428. /* Get the length of the string after escapes are processed. */
  429. len = o - copy;
  430. /* Find the address of malloc in the inferior. */
  431. sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE);
  432. if (sym != 0)
  433. {
  434. if (SYMBOL_CLASS (sym) != LOC_BLOCK)
  435. error ("\"malloc\" exists in this program but is not a function.");
  436. val = value_of_variable (sym);
  437. }
  438. else
  439. {
  440. register int i;
  441. for (i = 0; i < misc_function_count; i++)
  442. if (!strcmp (misc_function_vector[i].name, "malloc"))
  443. break;
  444. if (i < misc_function_count)
  445. val = value_from_long (builtin_type_long,
  446. misc_function_vector[i].address);
  447. else
  448. error ("String constants require the program to have a function \"malloc\".");
  449. }
  450. blocklen = value_from_long (builtin_type_int, len + 1);
  451. val = call_function (val, 1, &blocklen);
  452. if (value_zerop (val))
  453. error ("No memory available for string constant.");
  454. write_memory (value_as_long (val), copy, len + 1);
  455. VALUE_TYPE (val) = lookup_pointer_type (builtin_type_char);
  456. return val;
  457. }
  458. /* Given ARG1, a value of type (pointer to a)* structure/union,
  459. extract the component named NAME from the ultimate target structure/union
  460. and return it as a value with its appropriate type.
  461. ERR is used in the error message if ARG1's type is wrong. */
  462. value
  463. value_struct_elt (arg1, name, err)
  464. register value arg1;
  465. char *name;
  466. char *err;
  467. {
  468. register struct type *t;
  469. register int i;
  470. COERCE_ARRAY (arg1);
  471. t = VALUE_TYPE (arg1);
  472. /* Follow pointers until we get to a non-pointer. */
  473. while (TYPE_CODE (t) == TYPE_CODE_PTR)
  474. {
  475. arg1 = value_ind (arg1);
  476. COERCE_ARRAY (arg1);
  477. t = VALUE_TYPE (arg1);
  478. }
  479. if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  480. &&
  481. TYPE_CODE (t) != TYPE_CODE_UNION)
  482. error ("Attempt to extract a component of a value that is not a %s.", err);
  483. for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  484. {
  485. if (!strcmp (TYPE_FIELD_NAME (t, i), name))
  486. break;
  487. }
  488. if (i < 0)
  489. error ("Structure has no component named %s.", name);
  490. return value_field (arg1, i);
  491. }
  492. static
  493. initialize ()
  494. { }
  495. END_FILE