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"
28 pv_t v
= { pvk_unknown
, 0, 0 };
35 pv_constant (CORE_ADDR k
)
39 v
.kind
= pvk_constant
;
40 v
.reg
= -1; /* for debugging */
48 pv_register (int reg
, CORE_ADDR k
)
52 v
.kind
= pvk_register
;
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
68 constant_last (pv_t
*a
, pv_t
*b
)
70 if (a
->kind
== pvk_constant
71 && b
->kind
!= pvk_constant
)
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). */
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
));
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
143 return pv_constant (a
.k
- b
.k
);
145 /* We don't know how to subtract anything else. */
147 return pv_unknown ();
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
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)
171 /* We can 'and' a register with itself. */
172 else if (a
.kind
== pvk_register
173 && b
.kind
== pvk_register
178 /* Otherwise, we don't know. */
180 return pv_unknown ();
185 /* Examining prologue values. */
188 pv_is_identical (pv_t a
, pv_t b
)
190 if (a
.kind
!= b
.kind
)
200 return (a
.reg
== b
.reg
&& a
.k
== b
.k
);
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
223 pv_is_register_k (pv_t a
, int r
, CORE_ADDR k
)
225 return (a
.kind
== pvk_register
232 pv_is_array_ref (pv_t addr
, CORE_ADDR size
,
233 pv_t array_addr
, CORE_ADDR array_len
,
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
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
258 *i
= offset
.k
/ elt_size
;
259 return pv_definite_yes
;
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
289 /* The size of this entry. Note that an entry may wrap around from
290 the end of the address space to the beginning. */
293 /* The value stored here. */
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
304 m_addr_mask (((((CORE_ADDR
) 1 << (addr_bit
- 1)) - 1) << 1) | 1),
309 /* See prologue-value.h. */
312 pv_area::clear_entries ()
314 struct area_entry
*e
= m_entry
;
318 /* This needs to be a do-while loop, in order to actually
319 process the node being checked for in the terminating
323 struct area_entry
*next
= e
->next
;
328 while (e
!= m_entry
);
341 /* See prologue-value.h. */
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
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
;
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
376 while (((e
->next
->offset
- offset
) & m_addr_mask
)
377 < ((e
->offset
- offset
) & m_addr_mask
))
380 /* If the previous entry would be better than the current one, then
382 while (((e
->prev
->offset
- offset
) & m_addr_mask
)
383 < ((e
->offset
- offset
) & m_addr_mask
))
386 /* In case there's some locality to the searches, set the area's
387 pointer to the entry we've found. */
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. */
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
))
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
;
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. */
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
441 if (value
.kind
== pvk_unknown
)
445 CORE_ADDR offset
= addr
.k
;
446 struct area_entry
*e
= XNEW (struct area_entry
);
454 e
->prev
= m_entry
->prev
;
456 e
->prev
->next
= e
->next
->prev
= e
;
460 e
->prev
= e
->next
= e
;
467 /* See prologue-value.h. */
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. */
475 || store_would_trash (addr
))
476 return pv_unknown ();
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
)
487 return pv_unknown ();
492 /* See prologue-value.h. */
495 pv_area::find_reg (struct gdbarch
*gdbarch
, int reg
, CORE_ADDR
*offset_p
)
497 struct area_entry
*e
= m_entry
;
502 if (e
->value
.kind
== pvk_register
503 && e
->value
.reg
== reg
505 && e
->size
== register_size (gdbarch
, reg
))
508 *offset_p
= e
->offset
;
514 while (e
!= m_entry
);
520 /* See prologue-value.h. */
523 pv_area::scan (void (*func
) (void *closure
,
529 struct area_entry
*e
= m_entry
;
532 addr
.kind
= pvk_register
;
533 addr
.reg
= m_base_reg
;
539 func (closure
, addr
, e
->size
, e
->value
);
542 while (e
!= m_entry
);