prologue-value.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* Prologue value handling for GDB.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "prologue-value.h"
  16. #include "regcache.h"
  17. /* Constructors. */
  18. pv_t
  19. pv_unknown (void)
  20. {
  21. pv_t v = { pvk_unknown, 0, 0 };
  22. return v;
  23. }
  24. pv_t
  25. pv_constant (CORE_ADDR k)
  26. {
  27. pv_t v;
  28. v.kind = pvk_constant;
  29. v.reg = -1; /* for debugging */
  30. v.k = k;
  31. return v;
  32. }
  33. pv_t
  34. pv_register (int reg, CORE_ADDR k)
  35. {
  36. pv_t v;
  37. v.kind = pvk_register;
  38. v.reg = reg;
  39. v.k = k;
  40. return v;
  41. }
  42. /* Arithmetic operations. */
  43. /* If one of *A and *B is a constant, and the other isn't, swap the
  44. values as necessary to ensure that *B is the constant. This can
  45. reduce the number of cases we need to analyze in the functions
  46. below. */
  47. static void
  48. constant_last (pv_t *a, pv_t *b)
  49. {
  50. if (a->kind == pvk_constant
  51. && b->kind != pvk_constant)
  52. {
  53. pv_t temp = *a;
  54. *a = *b;
  55. *b = temp;
  56. }
  57. }
  58. pv_t
  59. pv_add (pv_t a, pv_t b)
  60. {
  61. constant_last (&a, &b);
  62. /* We can add a constant to a register. */
  63. if (a.kind == pvk_register
  64. && b.kind == pvk_constant)
  65. return pv_register (a.reg, a.k + b.k);
  66. /* We can add a constant to another constant. */
  67. else if (a.kind == pvk_constant
  68. && b.kind == pvk_constant)
  69. return pv_constant (a.k + b.k);
  70. /* Anything else we don't know how to add. We don't have a
  71. representation for, say, the sum of two registers, or a multiple
  72. of a register's value (adding a register to itself). */
  73. else
  74. return pv_unknown ();
  75. }
  76. pv_t
  77. pv_add_constant (pv_t v, CORE_ADDR k)
  78. {
  79. /* Rather than thinking of all the cases we can and can't handle,
  80. we'll just let pv_add take care of that for us. */
  81. return pv_add (v, pv_constant (k));
  82. }
  83. pv_t
  84. pv_subtract (pv_t a, pv_t b)
  85. {
  86. /* This isn't quite the same as negating B and adding it to A, since
  87. we don't have a representation for the negation of anything but a
  88. constant. For example, we can't negate { pvk_register, R1, 10 },
  89. but we do know that { pvk_register, R1, 10 } minus { pvk_register,
  90. R1, 5 } is { pvk_constant, <ignored>, 5 }.
  91. This means, for example, that we could subtract two stack
  92. addresses; they're both relative to the original SP. Since the
  93. frame pointer is set based on the SP, its value will be the
  94. original SP plus some constant (probably zero), so we can use its
  95. value just fine, too. */
  96. constant_last (&a, &b);
  97. /* We can subtract two constants. */
  98. if (a.kind == pvk_constant
  99. && b.kind == pvk_constant)
  100. return pv_constant (a.k - b.k);
  101. /* We can subtract a constant from a register. */
  102. else if (a.kind == pvk_register
  103. && b.kind == pvk_constant)
  104. return pv_register (a.reg, a.k - b.k);
  105. /* We can subtract a register from itself, yielding a constant. */
  106. else if (a.kind == pvk_register
  107. && b.kind == pvk_register
  108. && a.reg == b.reg)
  109. return pv_constant (a.k - b.k);
  110. /* We don't know how to subtract anything else. */
  111. else
  112. return pv_unknown ();
  113. }
  114. pv_t
  115. pv_logical_and (pv_t a, pv_t b)
  116. {
  117. constant_last (&a, &b);
  118. /* We can 'and' two constants. */
  119. if (a.kind == pvk_constant
  120. && b.kind == pvk_constant)
  121. return pv_constant (a.k & b.k);
  122. /* We can 'and' anything with the constant zero. */
  123. else if (b.kind == pvk_constant
  124. && b.k == 0)
  125. return pv_constant (0);
  126. /* We can 'and' anything with ~0. */
  127. else if (b.kind == pvk_constant
  128. && b.k == ~ (CORE_ADDR) 0)
  129. return a;
  130. /* We can 'and' a register with itself. */
  131. else if (a.kind == pvk_register
  132. && b.kind == pvk_register
  133. && a.reg == b.reg
  134. && a.k == b.k)
  135. return a;
  136. /* Otherwise, we don't know. */
  137. else
  138. return pv_unknown ();
  139. }
  140. /* Examining prologue values. */
  141. int
  142. pv_is_identical (pv_t a, pv_t b)
  143. {
  144. if (a.kind != b.kind)
  145. return 0;
  146. switch (a.kind)
  147. {
  148. case pvk_unknown:
  149. return 1;
  150. case pvk_constant:
  151. return (a.k == b.k);
  152. case pvk_register:
  153. return (a.reg == b.reg && a.k == b.k);
  154. default:
  155. gdb_assert_not_reached ("unexpected prologue value kind");
  156. }
  157. }
  158. int
  159. pv_is_constant (pv_t a)
  160. {
  161. return (a.kind == pvk_constant);
  162. }
  163. int
  164. pv_is_register (pv_t a, int r)
  165. {
  166. return (a.kind == pvk_register
  167. && a.reg == r);
  168. }
  169. int
  170. pv_is_register_k (pv_t a, int r, CORE_ADDR k)
  171. {
  172. return (a.kind == pvk_register
  173. && a.reg == r
  174. && a.k == k);
  175. }
  176. enum pv_boolean
  177. pv_is_array_ref (pv_t addr, CORE_ADDR size,
  178. pv_t array_addr, CORE_ADDR array_len,
  179. CORE_ADDR elt_size,
  180. int *i)
  181. {
  182. /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
  183. addr is *before* the start of the array, then this isn't going to
  184. be negative... */
  185. pv_t offset = pv_subtract (addr, array_addr);
  186. if (offset.kind == pvk_constant)
  187. {
  188. /* This is a rather odd test. We want to know if the SIZE bytes
  189. at ADDR don't overlap the array at all, so you'd expect it to
  190. be an || expression: "if we're completely before || we're
  191. completely after". But with unsigned arithmetic, things are
  192. different: since it's a number circle, not a number line, the
  193. right values for offset.k are actually one contiguous range. */
  194. if (offset.k <= -size
  195. && offset.k >= array_len * elt_size)
  196. return pv_definite_no;
  197. else if (offset.k % elt_size != 0
  198. || size != elt_size)
  199. return pv_maybe;
  200. else
  201. {
  202. *i = offset.k / elt_size;
  203. return pv_definite_yes;
  204. }
  205. }
  206. else
  207. return pv_maybe;
  208. }
  209. /* Areas. */
  210. /* A particular value known to be stored in an area.
  211. Entries form a ring, sorted by unsigned offset from the area's base
  212. register's value. Since entries can straddle the wrap-around point,
  213. unsigned offsets form a circle, not a number line, so the list
  214. itself is structured the same way --- there is no inherent head.
  215. The entry with the lowest offset simply follows the entry with the
  216. highest offset. Entries may abut, but never overlap. The area's
  217. 'entry' pointer points to an arbitrary node in the ring. */
  218. struct pv_area::area_entry
  219. {
  220. /* Links in the doubly-linked ring. */
  221. struct area_entry *prev, *next;
  222. /* Offset of this entry's address from the value of the base
  223. register. */
  224. CORE_ADDR offset;
  225. /* The size of this entry. Note that an entry may wrap around from
  226. the end of the address space to the beginning. */
  227. CORE_ADDR size;
  228. /* The value stored here. */
  229. pv_t value;
  230. };
  231. /* See prologue-value.h. */
  232. pv_area::pv_area (int base_reg, int addr_bit)
  233. : m_base_reg (base_reg),
  234. /* Remember that shift amounts equal to the type's width are
  235. undefined. */
  236. m_addr_mask (((((CORE_ADDR) 1 << (addr_bit - 1)) - 1) << 1) | 1),
  237. m_entry (nullptr)
  238. {
  239. }
  240. /* See prologue-value.h. */
  241. void
  242. pv_area::clear_entries ()
  243. {
  244. struct area_entry *e = m_entry;
  245. if (e)
  246. {
  247. /* This needs to be a do-while loop, in order to actually
  248. process the node being checked for in the terminating
  249. condition. */
  250. do
  251. {
  252. struct area_entry *next = e->next;
  253. xfree (e);
  254. e = next;
  255. }
  256. while (e != m_entry);
  257. m_entry = 0;
  258. }
  259. }
  260. pv_area::~pv_area ()
  261. {
  262. clear_entries ();
  263. }
  264. /* See prologue-value.h. */
  265. bool
  266. pv_area::store_would_trash (pv_t addr)
  267. {
  268. /* It may seem odd that pvk_constant appears here --- after all,
  269. that's the case where we know the most about the address! But
  270. pv_areas are always relative to a register, and we don't know the
  271. value of the register, so we can't compare entry addresses to
  272. constants. */
  273. return (addr.kind == pvk_unknown
  274. || addr.kind == pvk_constant
  275. || (addr.kind == pvk_register && addr.reg != m_base_reg));
  276. }
  277. /* See prologue-value.h. */
  278. struct pv_area::area_entry *
  279. pv_area::find_entry (CORE_ADDR offset)
  280. {
  281. struct area_entry *e = m_entry;
  282. if (! e)
  283. return 0;
  284. /* If the next entry would be better than the current one, then scan
  285. forward. Since we use '<' in this loop, it always terminates.
  286. Note that, even setting aside the addr_mask stuff, we must not
  287. simplify this, in high school algebra fashion, to
  288. (e->next->offset < e->offset), because of the way < interacts
  289. with wrap-around. We have to subtract offset from both sides to
  290. make sure both things we're comparing are on the same side of the
  291. discontinuity. */
  292. while (((e->next->offset - offset) & m_addr_mask)
  293. < ((e->offset - offset) & m_addr_mask))
  294. e = e->next;
  295. /* If the previous entry would be better than the current one, then
  296. scan backwards. */
  297. while (((e->prev->offset - offset) & m_addr_mask)
  298. < ((e->offset - offset) & m_addr_mask))
  299. e = e->prev;
  300. /* In case there's some locality to the searches, set the area's
  301. pointer to the entry we've found. */
  302. m_entry = e;
  303. return e;
  304. }
  305. /* See prologue-value.h. */
  306. int
  307. pv_area::overlaps (struct area_entry *entry, CORE_ADDR offset, CORE_ADDR size)
  308. {
  309. /* Think carefully about wrap-around before simplifying this. */
  310. return (((entry->offset - offset) & m_addr_mask) < size
  311. || ((offset - entry->offset) & m_addr_mask) < entry->size);
  312. }
  313. /* See prologue-value.h. */
  314. void
  315. pv_area::store (pv_t addr, CORE_ADDR size, pv_t value)
  316. {
  317. /* Remove any (potentially) overlapping entries. */
  318. if (store_would_trash (addr))
  319. clear_entries ();
  320. else
  321. {
  322. CORE_ADDR offset = addr.k;
  323. struct area_entry *e = find_entry (offset);
  324. /* Delete all entries that we would overlap. */
  325. while (e && overlaps (e, offset, size))
  326. {
  327. struct area_entry *next = (e->next == e) ? 0 : e->next;
  328. e->prev->next = e->next;
  329. e->next->prev = e->prev;
  330. xfree (e);
  331. e = next;
  332. }
  333. /* Move the area's pointer to the next remaining entry. This
  334. will also zero the pointer if we've deleted all the entries. */
  335. m_entry = e;
  336. }
  337. /* Now, there are no entries overlapping us, and m_entry is
  338. either zero or pointing at the closest entry after us. We can
  339. just insert ourselves before that.
  340. But if we're storing an unknown value, don't bother --- that's
  341. the default. */
  342. if (value.kind == pvk_unknown)
  343. return;
  344. else
  345. {
  346. CORE_ADDR offset = addr.k;
  347. struct area_entry *e = XNEW (struct area_entry);
  348. e->offset = offset;
  349. e->size = size;
  350. e->value = value;
  351. if (m_entry)
  352. {
  353. e->prev = m_entry->prev;
  354. e->next = m_entry;
  355. e->prev->next = e->next->prev = e;
  356. }
  357. else
  358. {
  359. e->prev = e->next = e;
  360. m_entry = e;
  361. }
  362. }
  363. }
  364. /* See prologue-value.h. */
  365. pv_t
  366. pv_area::fetch (pv_t addr, CORE_ADDR size)
  367. {
  368. /* If we have no entries, or we can't decide how ADDR relates to the
  369. entries we do have, then the value is unknown. */
  370. if (! m_entry
  371. || store_would_trash (addr))
  372. return pv_unknown ();
  373. else
  374. {
  375. CORE_ADDR offset = addr.k;
  376. struct area_entry *e = find_entry (offset);
  377. /* If this entry exactly matches what we're looking for, then
  378. we're set. Otherwise, say it's unknown. */
  379. if (e->offset == offset && e->size == size)
  380. return e->value;
  381. else
  382. return pv_unknown ();
  383. }
  384. }
  385. /* See prologue-value.h. */
  386. bool
  387. pv_area::find_reg (struct gdbarch *gdbarch, int reg, CORE_ADDR *offset_p)
  388. {
  389. struct area_entry *e = m_entry;
  390. if (e)
  391. do
  392. {
  393. if (e->value.kind == pvk_register
  394. && e->value.reg == reg
  395. && e->value.k == 0
  396. && e->size == register_size (gdbarch, reg))
  397. {
  398. if (offset_p)
  399. *offset_p = e->offset;
  400. return true;
  401. }
  402. e = e->next;
  403. }
  404. while (e != m_entry);
  405. return false;
  406. }
  407. /* See prologue-value.h. */
  408. void
  409. pv_area::scan (void (*func) (void *closure,
  410. pv_t addr,
  411. CORE_ADDR size,
  412. pv_t value),
  413. void *closure)
  414. {
  415. struct area_entry *e = m_entry;
  416. pv_t addr;
  417. addr.kind = pvk_register;
  418. addr.reg = m_base_reg;
  419. if (e)
  420. do
  421. {
  422. addr.k = e->offset;
  423. func (closure, addr, e->size, e->value);
  424. e = e->next;
  425. }
  426. while (e != m_entry);
  427. }