Automatic date update in version.in
[binutils-gdb.git] / gdb / prologue-value.c
blob2c0ce52421b5fb9667b0fe3933858fa67f998274
1 /* Prologue value handling for GDB.
2 Copyright (C) 2003-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "prologue-value.h"
20 #include "regcache.h"
23 /* Constructors. */
25 pv_t
26 pv_unknown (void)
28 pv_t v = { pvk_unknown, 0, 0 };
30 return v;
34 pv_t
35 pv_constant (CORE_ADDR k)
37 pv_t v;
39 v.kind = pvk_constant;
40 v.reg = -1; /* for debugging */
41 v.k = k;
43 return v;
47 pv_t
48 pv_register (int reg, CORE_ADDR k)
50 pv_t v;
52 v.kind = pvk_register;
53 v.reg = reg;
54 v.k = k;
56 return v;
61 /* Arithmetic operations. */
63 /* If one of *A and *B is a constant, and the other isn't, swap the
64 values as necessary to ensure that *B is the constant. This can
65 reduce the number of cases we need to analyze in the functions
66 below. */
67 static void
68 constant_last (pv_t *a, pv_t *b)
70 if (a->kind == pvk_constant
71 && b->kind != pvk_constant)
73 pv_t temp = *a;
74 *a = *b;
75 *b = temp;
80 pv_t
81 pv_add (pv_t a, pv_t b)
83 constant_last (&a, &b);
85 /* We can add a constant to a register. */
86 if (a.kind == pvk_register
87 && b.kind == pvk_constant)
88 return pv_register (a.reg, a.k + b.k);
90 /* We can add a constant to another constant. */
91 else if (a.kind == pvk_constant
92 && b.kind == pvk_constant)
93 return pv_constant (a.k + b.k);
95 /* Anything else we don't know how to add. We don't have a
96 representation for, say, the sum of two registers, or a multiple
97 of a register's value (adding a register to itself). */
98 else
99 return pv_unknown ();
103 pv_t
104 pv_add_constant (pv_t v, CORE_ADDR k)
106 /* Rather than thinking of all the cases we can and can't handle,
107 we'll just let pv_add take care of that for us. */
108 return pv_add (v, pv_constant (k));
112 pv_t
113 pv_subtract (pv_t a, pv_t b)
115 /* This isn't quite the same as negating B and adding it to A, since
116 we don't have a representation for the negation of anything but a
117 constant. For example, we can't negate { pvk_register, R1, 10 },
118 but we do know that { pvk_register, R1, 10 } minus { pvk_register,
119 R1, 5 } is { pvk_constant, <ignored>, 5 }.
121 This means, for example, that we could subtract two stack
122 addresses; they're both relative to the original SP. Since the
123 frame pointer is set based on the SP, its value will be the
124 original SP plus some constant (probably zero), so we can use its
125 value just fine, too. */
127 constant_last (&a, &b);
129 /* We can subtract two constants. */
130 if (a.kind == pvk_constant
131 && b.kind == pvk_constant)
132 return pv_constant (a.k - b.k);
134 /* We can subtract a constant from a register. */
135 else if (a.kind == pvk_register
136 && b.kind == pvk_constant)
137 return pv_register (a.reg, a.k - b.k);
139 /* We can subtract a register from itself, yielding a constant. */
140 else if (a.kind == pvk_register
141 && b.kind == pvk_register
142 && a.reg == b.reg)
143 return pv_constant (a.k - b.k);
145 /* We don't know how to subtract anything else. */
146 else
147 return pv_unknown ();
151 pv_t
152 pv_logical_and (pv_t a, pv_t b)
154 constant_last (&a, &b);
156 /* We can 'and' two constants. */
157 if (a.kind == pvk_constant
158 && b.kind == pvk_constant)
159 return pv_constant (a.k & b.k);
161 /* We can 'and' anything with the constant zero. */
162 else if (b.kind == pvk_constant
163 && b.k == 0)
164 return pv_constant (0);
166 /* We can 'and' anything with ~0. */
167 else if (b.kind == pvk_constant
168 && b.k == ~ (CORE_ADDR) 0)
169 return a;
171 /* We can 'and' a register with itself. */
172 else if (a.kind == pvk_register
173 && b.kind == pvk_register
174 && a.reg == b.reg
175 && a.k == b.k)
176 return a;
178 /* Otherwise, we don't know. */
179 else
180 return pv_unknown ();
185 /* Examining prologue values. */
188 pv_is_identical (pv_t a, pv_t b)
190 if (a.kind != b.kind)
191 return 0;
193 switch (a.kind)
195 case pvk_unknown:
196 return 1;
197 case pvk_constant:
198 return (a.k == b.k);
199 case pvk_register:
200 return (a.reg == b.reg && a.k == b.k);
201 default:
202 gdb_assert_not_reached ("unexpected prologue value kind");
208 pv_is_constant (pv_t a)
210 return (a.kind == pvk_constant);
215 pv_is_register (pv_t a, int r)
217 return (a.kind == pvk_register
218 && a.reg == r);
223 pv_is_register_k (pv_t a, int r, CORE_ADDR k)
225 return (a.kind == pvk_register
226 && a.reg == r
227 && a.k == k);
231 enum pv_boolean
232 pv_is_array_ref (pv_t addr, CORE_ADDR size,
233 pv_t array_addr, CORE_ADDR array_len,
234 CORE_ADDR elt_size,
235 int *i)
237 /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
238 addr is *before* the start of the array, then this isn't going to
239 be negative... */
240 pv_t offset = pv_subtract (addr, array_addr);
242 if (offset.kind == pvk_constant)
244 /* This is a rather odd test. We want to know if the SIZE bytes
245 at ADDR don't overlap the array at all, so you'd expect it to
246 be an || expression: "if we're completely before || we're
247 completely after". But with unsigned arithmetic, things are
248 different: since it's a number circle, not a number line, the
249 right values for offset.k are actually one contiguous range. */
250 if (offset.k <= -size
251 && offset.k >= array_len * elt_size)
252 return pv_definite_no;
253 else if (offset.k % elt_size != 0
254 || size != elt_size)
255 return pv_maybe;
256 else
258 *i = offset.k / elt_size;
259 return pv_definite_yes;
262 else
263 return pv_maybe;
268 /* Areas. */
271 /* A particular value known to be stored in an area.
273 Entries form a ring, sorted by unsigned offset from the area's base
274 register's value. Since entries can straddle the wrap-around point,
275 unsigned offsets form a circle, not a number line, so the list
276 itself is structured the same way --- there is no inherent head.
277 The entry with the lowest offset simply follows the entry with the
278 highest offset. Entries may abut, but never overlap. The area's
279 'entry' pointer points to an arbitrary node in the ring. */
280 struct pv_area::area_entry
282 /* Links in the doubly-linked ring. */
283 struct area_entry *prev, *next;
285 /* Offset of this entry's address from the value of the base
286 register. */
287 CORE_ADDR offset;
289 /* The size of this entry. Note that an entry may wrap around from
290 the end of the address space to the beginning. */
291 CORE_ADDR size;
293 /* The value stored here. */
294 pv_t value;
298 /* See prologue-value.h. */
300 pv_area::pv_area (int base_reg, int addr_bit)
301 : m_base_reg (base_reg),
302 /* Remember that shift amounts equal to the type's width are
303 undefined. */
304 m_addr_mask (((((CORE_ADDR) 1 << (addr_bit - 1)) - 1) << 1) | 1),
305 m_entry (nullptr)
309 /* See prologue-value.h. */
311 void
312 pv_area::clear_entries ()
314 struct area_entry *e = m_entry;
316 if (e)
318 /* This needs to be a do-while loop, in order to actually
319 process the node being checked for in the terminating
320 condition. */
323 struct area_entry *next = e->next;
325 xfree (e);
326 e = next;
328 while (e != m_entry);
330 m_entry = 0;
335 pv_area::~pv_area ()
337 clear_entries ();
341 /* See prologue-value.h. */
343 bool
344 pv_area::store_would_trash (pv_t addr)
346 /* It may seem odd that pvk_constant appears here --- after all,
347 that's the case where we know the most about the address! But
348 pv_areas are always relative to a register, and we don't know the
349 value of the register, so we can't compare entry addresses to
350 constants. */
351 return (addr.kind == pvk_unknown
352 || addr.kind == pvk_constant
353 || (addr.kind == pvk_register && addr.reg != m_base_reg));
357 /* See prologue-value.h. */
359 struct pv_area::area_entry *
360 pv_area::find_entry (CORE_ADDR offset)
362 struct area_entry *e = m_entry;
364 if (! e)
365 return 0;
367 /* If the next entry would be better than the current one, then scan
368 forward. Since we use '<' in this loop, it always terminates.
370 Note that, even setting aside the addr_mask stuff, we must not
371 simplify this, in high school algebra fashion, to
372 (e->next->offset < e->offset), because of the way < interacts
373 with wrap-around. We have to subtract offset from both sides to
374 make sure both things we're comparing are on the same side of the
375 discontinuity. */
376 while (((e->next->offset - offset) & m_addr_mask)
377 < ((e->offset - offset) & m_addr_mask))
378 e = e->next;
380 /* If the previous entry would be better than the current one, then
381 scan backwards. */
382 while (((e->prev->offset - offset) & m_addr_mask)
383 < ((e->offset - offset) & m_addr_mask))
384 e = e->prev;
386 /* In case there's some locality to the searches, set the area's
387 pointer to the entry we've found. */
388 m_entry = e;
390 return e;
394 /* See prologue-value.h. */
397 pv_area::overlaps (struct area_entry *entry, CORE_ADDR offset, CORE_ADDR size)
399 /* Think carefully about wrap-around before simplifying this. */
400 return (((entry->offset - offset) & m_addr_mask) < size
401 || ((offset - entry->offset) & m_addr_mask) < entry->size);
405 /* See prologue-value.h. */
407 void
408 pv_area::store (pv_t addr, CORE_ADDR size, pv_t value)
410 /* Remove any (potentially) overlapping entries. */
411 if (store_would_trash (addr))
412 clear_entries ();
413 else
415 CORE_ADDR offset = addr.k;
416 struct area_entry *e = find_entry (offset);
418 /* Delete all entries that we would overlap. */
419 while (e && overlaps (e, offset, size))
421 struct area_entry *next = (e->next == e) ? 0 : e->next;
423 e->prev->next = e->next;
424 e->next->prev = e->prev;
426 xfree (e);
427 e = next;
430 /* Move the area's pointer to the next remaining entry. This
431 will also zero the pointer if we've deleted all the entries. */
432 m_entry = e;
435 /* Now, there are no entries overlapping us, and m_entry is
436 either zero or pointing at the closest entry after us. We can
437 just insert ourselves before that.
439 But if we're storing an unknown value, don't bother --- that's
440 the default. */
441 if (value.kind == pvk_unknown)
442 return;
443 else
445 CORE_ADDR offset = addr.k;
446 struct area_entry *e = XNEW (struct area_entry);
448 e->offset = offset;
449 e->size = size;
450 e->value = value;
452 if (m_entry)
454 e->prev = m_entry->prev;
455 e->next = m_entry;
456 e->prev->next = e->next->prev = e;
458 else
460 e->prev = e->next = e;
461 m_entry = e;
467 /* See prologue-value.h. */
469 pv_t
470 pv_area::fetch (pv_t addr, CORE_ADDR size)
472 /* If we have no entries, or we can't decide how ADDR relates to the
473 entries we do have, then the value is unknown. */
474 if (! m_entry
475 || store_would_trash (addr))
476 return pv_unknown ();
477 else
479 CORE_ADDR offset = addr.k;
480 struct area_entry *e = find_entry (offset);
482 /* If this entry exactly matches what we're looking for, then
483 we're set. Otherwise, say it's unknown. */
484 if (e->offset == offset && e->size == size)
485 return e->value;
486 else
487 return pv_unknown ();
492 /* See prologue-value.h. */
494 bool
495 pv_area::find_reg (struct gdbarch *gdbarch, int reg, CORE_ADDR *offset_p)
497 struct area_entry *e = m_entry;
499 if (e)
502 if (e->value.kind == pvk_register
503 && e->value.reg == reg
504 && e->value.k == 0
505 && e->size == register_size (gdbarch, reg))
507 if (offset_p)
508 *offset_p = e->offset;
509 return true;
512 e = e->next;
514 while (e != m_entry);
516 return false;
520 /* See prologue-value.h. */
522 void
523 pv_area::scan (void (*func) (void *closure,
524 pv_t addr,
525 CORE_ADDR size,
526 pv_t value),
527 void *closure)
529 struct area_entry *e = m_entry;
530 pv_t addr;
532 addr.kind = pvk_register;
533 addr.reg = m_base_reg;
535 if (e)
538 addr.k = e->offset;
539 func (closure, addr, e->size, e->value);
540 e = e->next;
542 while (e != m_entry);