1 /* Prologue value handling for GDB.
2 Copyright (C) 2003-2014 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/>. */
21 #include "prologue-value.h"
30 pv_t v
= { pvk_unknown
, 0, 0 };
37 pv_constant (CORE_ADDR k
)
41 v
.kind
= pvk_constant
;
42 v
.reg
= -1; /* for debugging */
50 pv_register (int reg
, CORE_ADDR k
)
54 v
.kind
= pvk_register
;
63 /* Arithmetic operations. */
65 /* If one of *A and *B is a constant, and the other isn't, swap the
66 values as necessary to ensure that *B is the constant. This can
67 reduce the number of cases we need to analyze in the functions
70 constant_last (pv_t
*a
, pv_t
*b
)
72 if (a
->kind
== pvk_constant
73 && b
->kind
!= pvk_constant
)
83 pv_add (pv_t a
, pv_t b
)
85 constant_last (&a
, &b
);
87 /* We can add a constant to a register. */
88 if (a
.kind
== pvk_register
89 && b
.kind
== pvk_constant
)
90 return pv_register (a
.reg
, a
.k
+ b
.k
);
92 /* We can add a constant to another constant. */
93 else if (a
.kind
== pvk_constant
94 && b
.kind
== pvk_constant
)
95 return pv_constant (a
.k
+ b
.k
);
97 /* Anything else we don't know how to add. We don't have a
98 representation for, say, the sum of two registers, or a multiple
99 of a register's value (adding a register to itself). */
101 return pv_unknown ();
106 pv_add_constant (pv_t v
, CORE_ADDR k
)
108 /* Rather than thinking of all the cases we can and can't handle,
109 we'll just let pv_add take care of that for us. */
110 return pv_add (v
, pv_constant (k
));
115 pv_subtract (pv_t a
, pv_t b
)
117 /* This isn't quite the same as negating B and adding it to A, since
118 we don't have a representation for the negation of anything but a
119 constant. For example, we can't negate { pvk_register, R1, 10 },
120 but we do know that { pvk_register, R1, 10 } minus { pvk_register,
121 R1, 5 } is { pvk_constant, <ignored>, 5 }.
123 This means, for example, that we could subtract two stack
124 addresses; they're both relative to the original SP. Since the
125 frame pointer is set based on the SP, its value will be the
126 original SP plus some constant (probably zero), so we can use its
127 value just fine, too. */
129 constant_last (&a
, &b
);
131 /* We can subtract two constants. */
132 if (a
.kind
== pvk_constant
133 && b
.kind
== pvk_constant
)
134 return pv_constant (a
.k
- b
.k
);
136 /* We can subtract a constant from a register. */
137 else if (a
.kind
== pvk_register
138 && b
.kind
== pvk_constant
)
139 return pv_register (a
.reg
, a
.k
- b
.k
);
141 /* We can subtract a register from itself, yielding a constant. */
142 else if (a
.kind
== pvk_register
143 && b
.kind
== pvk_register
145 return pv_constant (a
.k
- b
.k
);
147 /* We don't know how to subtract anything else. */
149 return pv_unknown ();
154 pv_logical_and (pv_t a
, pv_t b
)
156 constant_last (&a
, &b
);
158 /* We can 'and' two constants. */
159 if (a
.kind
== pvk_constant
160 && b
.kind
== pvk_constant
)
161 return pv_constant (a
.k
& b
.k
);
163 /* We can 'and' anything with the constant zero. */
164 else if (b
.kind
== pvk_constant
166 return pv_constant (0);
168 /* We can 'and' anything with ~0. */
169 else if (b
.kind
== pvk_constant
170 && b
.k
== ~ (CORE_ADDR
) 0)
173 /* We can 'and' a register with itself. */
174 else if (a
.kind
== pvk_register
175 && b
.kind
== pvk_register
180 /* Otherwise, we don't know. */
182 return pv_unknown ();
187 /* Examining prologue values. */
190 pv_is_identical (pv_t a
, pv_t b
)
192 if (a
.kind
!= b
.kind
)
202 return (a
.reg
== b
.reg
&& a
.k
== b
.k
);
204 gdb_assert_not_reached ("unexpected prologue value kind");
210 pv_is_constant (pv_t a
)
212 return (a
.kind
== pvk_constant
);
217 pv_is_register (pv_t a
, int r
)
219 return (a
.kind
== pvk_register
225 pv_is_register_k (pv_t a
, int r
, CORE_ADDR k
)
227 return (a
.kind
== pvk_register
234 pv_is_array_ref (pv_t addr
, CORE_ADDR size
,
235 pv_t array_addr
, CORE_ADDR array_len
,
239 /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
240 addr is *before* the start of the array, then this isn't going to
242 pv_t offset
= pv_subtract (addr
, array_addr
);
244 if (offset
.kind
== pvk_constant
)
246 /* This is a rather odd test. We want to know if the SIZE bytes
247 at ADDR don't overlap the array at all, so you'd expect it to
248 be an || expression: "if we're completely before || we're
249 completely after". But with unsigned arithmetic, things are
250 different: since it's a number circle, not a number line, the
251 right values for offset.k are actually one contiguous range. */
252 if (offset
.k
<= -size
253 && offset
.k
>= array_len
* elt_size
)
254 return pv_definite_no
;
255 else if (offset
.k
% elt_size
!= 0
260 *i
= offset
.k
/ elt_size
;
261 return pv_definite_yes
;
273 /* A particular value known to be stored in an area.
275 Entries form a ring, sorted by unsigned offset from the area's base
276 register's value. Since entries can straddle the wrap-around point,
277 unsigned offsets form a circle, not a number line, so the list
278 itself is structured the same way --- there is no inherent head.
279 The entry with the lowest offset simply follows the entry with the
280 highest offset. Entries may abut, but never overlap. The area's
281 'entry' pointer points to an arbitrary node in the ring. */
284 /* Links in the doubly-linked ring. */
285 struct area_entry
*prev
, *next
;
287 /* Offset of this entry's address from the value of the base
291 /* The size of this entry. Note that an entry may wrap around from
292 the end of the address space to the beginning. */
295 /* The value stored here. */
302 /* This area's base register. */
305 /* The mask to apply to addresses, to make the wrap-around happen at
309 /* An element of the doubly-linked ring of entries, or zero if we
311 struct area_entry
*entry
;
316 make_pv_area (int base_reg
, int addr_bit
)
318 struct pv_area
*a
= (struct pv_area
*) xmalloc (sizeof (*a
));
320 memset (a
, 0, sizeof (*a
));
322 a
->base_reg
= base_reg
;
325 /* Remember that shift amounts equal to the type's width are
327 a
->addr_mask
= ((((CORE_ADDR
) 1 << (addr_bit
- 1)) - 1) << 1) | 1;
333 /* Delete all entries from AREA. */
335 clear_entries (struct pv_area
*area
)
337 struct area_entry
*e
= area
->entry
;
341 /* This needs to be a do-while loop, in order to actually
342 process the node being checked for in the terminating
346 struct area_entry
*next
= e
->next
;
351 while (e
!= area
->entry
);
359 free_pv_area (struct pv_area
*area
)
361 clear_entries (area
);
367 do_free_pv_area_cleanup (void *arg
)
369 free_pv_area ((struct pv_area
*) arg
);
374 make_cleanup_free_pv_area (struct pv_area
*area
)
376 return make_cleanup (do_free_pv_area_cleanup
, (void *) area
);
381 pv_area_store_would_trash (struct pv_area
*area
, pv_t addr
)
383 /* It may seem odd that pvk_constant appears here --- after all,
384 that's the case where we know the most about the address! But
385 pv_areas are always relative to a register, and we don't know the
386 value of the register, so we can't compare entry addresses to
388 return (addr
.kind
== pvk_unknown
389 || addr
.kind
== pvk_constant
390 || (addr
.kind
== pvk_register
&& addr
.reg
!= area
->base_reg
));
394 /* Return a pointer to the first entry we hit in AREA starting at
395 OFFSET and going forward.
397 This may return zero, if AREA has no entries.
399 And since the entries are a ring, this may return an entry that
400 entirely precedes OFFSET. This is the correct behavior: depending
401 on the sizes involved, we could still overlap such an area, with
403 static struct area_entry
*
404 find_entry (struct pv_area
*area
, CORE_ADDR offset
)
406 struct area_entry
*e
= area
->entry
;
411 /* If the next entry would be better than the current one, then scan
412 forward. Since we use '<' in this loop, it always terminates.
414 Note that, even setting aside the addr_mask stuff, we must not
415 simplify this, in high school algebra fashion, to
416 (e->next->offset < e->offset), because of the way < interacts
417 with wrap-around. We have to subtract offset from both sides to
418 make sure both things we're comparing are on the same side of the
420 while (((e
->next
->offset
- offset
) & area
->addr_mask
)
421 < ((e
->offset
- offset
) & area
->addr_mask
))
424 /* If the previous entry would be better than the current one, then
426 while (((e
->prev
->offset
- offset
) & area
->addr_mask
)
427 < ((e
->offset
- offset
) & area
->addr_mask
))
430 /* In case there's some locality to the searches, set the area's
431 pointer to the entry we've found. */
438 /* Return non-zero if the SIZE bytes at OFFSET would overlap ENTRY;
439 return zero otherwise. AREA is the area to which ENTRY belongs. */
441 overlaps (struct pv_area
*area
,
442 struct area_entry
*entry
,
446 /* Think carefully about wrap-around before simplifying this. */
447 return (((entry
->offset
- offset
) & area
->addr_mask
) < size
448 || ((offset
- entry
->offset
) & area
->addr_mask
) < entry
->size
);
453 pv_area_store (struct pv_area
*area
,
458 /* Remove any (potentially) overlapping entries. */
459 if (pv_area_store_would_trash (area
, addr
))
460 clear_entries (area
);
463 CORE_ADDR offset
= addr
.k
;
464 struct area_entry
*e
= find_entry (area
, offset
);
466 /* Delete all entries that we would overlap. */
467 while (e
&& overlaps (area
, e
, offset
, size
))
469 struct area_entry
*next
= (e
->next
== e
) ? 0 : e
->next
;
471 e
->prev
->next
= e
->next
;
472 e
->next
->prev
= e
->prev
;
478 /* Move the area's pointer to the next remaining entry. This
479 will also zero the pointer if we've deleted all the entries. */
483 /* Now, there are no entries overlapping us, and area->entry is
484 either zero or pointing at the closest entry after us. We can
485 just insert ourselves before that.
487 But if we're storing an unknown value, don't bother --- that's
489 if (value
.kind
== pvk_unknown
)
493 CORE_ADDR offset
= addr
.k
;
494 struct area_entry
*e
= (struct area_entry
*) xmalloc (sizeof (*e
));
502 e
->prev
= area
->entry
->prev
;
503 e
->next
= area
->entry
;
504 e
->prev
->next
= e
->next
->prev
= e
;
508 e
->prev
= e
->next
= e
;
516 pv_area_fetch (struct pv_area
*area
, pv_t addr
, CORE_ADDR size
)
518 /* If we have no entries, or we can't decide how ADDR relates to the
519 entries we do have, then the value is unknown. */
521 || pv_area_store_would_trash (area
, addr
))
522 return pv_unknown ();
525 CORE_ADDR offset
= addr
.k
;
526 struct area_entry
*e
= find_entry (area
, offset
);
528 /* If this entry exactly matches what we're looking for, then
529 we're set. Otherwise, say it's unknown. */
530 if (e
->offset
== offset
&& e
->size
== size
)
533 return pv_unknown ();
539 pv_area_find_reg (struct pv_area
*area
,
540 struct gdbarch
*gdbarch
,
544 struct area_entry
*e
= area
->entry
;
549 if (e
->value
.kind
== pvk_register
550 && e
->value
.reg
== reg
552 && e
->size
== register_size (gdbarch
, reg
))
555 *offset_p
= e
->offset
;
561 while (e
!= area
->entry
);
568 pv_area_scan (struct pv_area
*area
,
569 void (*func
) (void *closure
,
575 struct area_entry
*e
= area
->entry
;
578 addr
.kind
= pvk_register
;
579 addr
.reg
= area
->base_reg
;
585 func (closure
, addr
, e
->size
, e
->value
);
588 while (e
!= area
->entry
);
This page took 0.051464 seconds and 5 git commands to generate.