1 /* Prologue value handling for GDB.
2 Copyright 2003, 2004, 2005 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 2 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, write to:
19 Free Software Foundation, Inc.
20 51 Franklin St - Fifth Floor
25 #include "gdb_string.h"
26 #include "gdb_assert.h"
27 #include "prologue-value.h"
36 pv_t v
= { pvk_unknown
, 0, 0 };
43 pv_constant (CORE_ADDR k
)
47 v
.kind
= pvk_constant
;
48 v
.reg
= -1; /* for debugging */
56 pv_register (int reg
, CORE_ADDR k
)
60 v
.kind
= pvk_register
;
69 /* Arithmetic operations. */
71 /* If one of *A and *B is a constant, and the other isn't, swap the
72 values as necessary to ensure that *B is the constant. This can
73 reduce the number of cases we need to analyze in the functions
76 constant_last (pv_t
*a
, pv_t
*b
)
78 if (a
->kind
== pvk_constant
79 && b
->kind
!= pvk_constant
)
89 pv_add (pv_t a
, pv_t b
)
91 constant_last (&a
, &b
);
93 /* We can add a constant to a register. */
94 if (a
.kind
== pvk_register
95 && b
.kind
== pvk_constant
)
96 return pv_register (a
.reg
, a
.k
+ b
.k
);
98 /* We can add a constant to another constant. */
99 else if (a
.kind
== pvk_constant
100 && b
.kind
== pvk_constant
)
101 return pv_constant (a
.k
+ b
.k
);
103 /* Anything else we don't know how to add. We don't have a
104 representation for, say, the sum of two registers, or a multiple
105 of a register's value (adding a register to itself). */
107 return pv_unknown ();
112 pv_add_constant (pv_t v
, CORE_ADDR k
)
114 /* Rather than thinking of all the cases we can and can't handle,
115 we'll just let pv_add take care of that for us. */
116 return pv_add (v
, pv_constant (k
));
121 pv_subtract (pv_t a
, pv_t b
)
123 /* This isn't quite the same as negating B and adding it to A, since
124 we don't have a representation for the negation of anything but a
125 constant. For example, we can't negate { pvk_register, R1, 10 },
126 but we do know that { pvk_register, R1, 10 } minus { pvk_register,
127 R1, 5 } is { pvk_constant, <ignored>, 5 }.
129 This means, for example, that we could subtract two stack
130 addresses; they're both relative to the original SP. Since the
131 frame pointer is set based on the SP, its value will be the
132 original SP plus some constant (probably zero), so we can use its
133 value just fine, too. */
135 constant_last (&a
, &b
);
137 /* We can subtract two constants. */
138 if (a
.kind
== pvk_constant
139 && b
.kind
== pvk_constant
)
140 return pv_constant (a
.k
- b
.k
);
142 /* We can subtract a constant from a register. */
143 else if (a
.kind
== pvk_register
144 && b
.kind
== pvk_constant
)
145 return pv_register (a
.reg
, a
.k
- b
.k
);
147 /* We can subtract a register from itself, yielding a constant. */
148 else if (a
.kind
== pvk_register
149 && b
.kind
== pvk_register
151 return pv_constant (a
.k
- b
.k
);
153 /* We don't know how to subtract anything else. */
155 return pv_unknown ();
160 pv_logical_and (pv_t a
, pv_t b
)
162 constant_last (&a
, &b
);
164 /* We can 'and' two constants. */
165 if (a
.kind
== pvk_constant
166 && b
.kind
== pvk_constant
)
167 return pv_constant (a
.k
& b
.k
);
169 /* We can 'and' anything with the constant zero. */
170 else if (b
.kind
== pvk_constant
172 return pv_constant (0);
174 /* We can 'and' anything with ~0. */
175 else if (b
.kind
== pvk_constant
176 && b
.k
== ~ (CORE_ADDR
) 0)
179 /* We can 'and' a register with itself. */
180 else if (a
.kind
== pvk_register
181 && b
.kind
== pvk_register
186 /* Otherwise, we don't know. */
188 return pv_unknown ();
193 /* Examining prologue values. */
196 pv_is_identical (pv_t a
, pv_t b
)
198 if (a
.kind
!= b
.kind
)
208 return (a
.reg
== b
.reg
&& a
.k
== b
.k
);
216 pv_is_constant (pv_t a
)
218 return (a
.kind
== pvk_constant
);
223 pv_is_register (pv_t a
, int r
)
225 return (a
.kind
== pvk_register
231 pv_is_register_k (pv_t a
, int r
, CORE_ADDR k
)
233 return (a
.kind
== pvk_register
240 pv_is_array_ref (pv_t addr
, CORE_ADDR size
,
241 pv_t array_addr
, CORE_ADDR array_len
,
245 /* Note that, since .k is a CORE_ADDR, and CORE_ADDR is unsigned, if
246 addr is *before* the start of the array, then this isn't going to
248 pv_t offset
= pv_subtract (addr
, array_addr
);
250 if (offset
.kind
== pvk_constant
)
252 /* This is a rather odd test. We want to know if the SIZE bytes
253 at ADDR don't overlap the array at all, so you'd expect it to
254 be an || expression: "if we're completely before || we're
255 completely after". But with unsigned arithmetic, things are
256 different: since it's a number circle, not a number line, the
257 right values for offset.k are actually one contiguous range. */
258 if (offset
.k
<= -size
259 && offset
.k
>= array_len
* elt_size
)
260 return pv_definite_no
;
261 else if (offset
.k
% elt_size
!= 0
266 *i
= offset
.k
/ elt_size
;
267 return pv_definite_yes
;
279 /* A particular value known to be stored in an area.
281 Entries form a ring, sorted by unsigned offset from the area's base
282 register's value. Since entries can straddle the wrap-around point,
283 unsigned offsets form a circle, not a number line, so the list
284 itself is structured the same way --- there is no inherent head.
285 The entry with the lowest offset simply follows the entry with the
286 highest offset. Entries may abut, but never overlap. The area's
287 'entry' pointer points to an arbitrary node in the ring. */
290 /* Links in the doubly-linked ring. */
291 struct area_entry
*prev
, *next
;
293 /* Offset of this entry's address from the value of the base
297 /* The size of this entry. Note that an entry may wrap around from
298 the end of the address space to the beginning. */
301 /* The value stored here. */
308 /* This area's base register. */
311 /* The mask to apply to addresses, to make the wrap-around happen at
315 /* An element of the doubly-linked ring of entries, or zero if we
317 struct area_entry
*entry
;
322 make_pv_area (int base_reg
)
324 struct pv_area
*a
= (struct pv_area
*) xmalloc (sizeof (*a
));
326 memset (a
, 0, sizeof (*a
));
328 a
->base_reg
= base_reg
;
331 /* Remember that shift amounts equal to the type's width are
333 a
->addr_mask
= ((((CORE_ADDR
) 1 << (TARGET_ADDR_BIT
- 1)) - 1) << 1) | 1;
339 /* Delete all entries from AREA. */
341 clear_entries (struct pv_area
*area
)
343 struct area_entry
*e
= area
->entry
;
347 /* This needs to be a do-while loop, in order to actually
348 process the node being checked for in the terminating
352 struct area_entry
*next
= e
->next
;
355 while (e
!= area
->entry
);
363 free_pv_area (struct pv_area
*area
)
365 clear_entries (area
);
371 do_free_pv_area_cleanup (void *arg
)
373 free_pv_area ((struct pv_area
*) arg
);
378 make_cleanup_free_pv_area (struct pv_area
*area
)
380 return make_cleanup (do_free_pv_area_cleanup
, (void *) area
);
385 pv_area_store_would_trash (struct pv_area
*area
, pv_t addr
)
387 /* It may seem odd that pvk_constant appears here --- after all,
388 that's the case where we know the most about the address! But
389 pv_areas are always relative to a register, and we don't know the
390 value of the register, so we can't compare entry addresses to
392 return (addr
.kind
== pvk_unknown
393 || addr
.kind
== pvk_constant
394 || (addr
.kind
== pvk_register
&& addr
.reg
!= area
->base_reg
));
398 /* Return a pointer to the first entry we hit in AREA starting at
399 OFFSET and going forward.
401 This may return zero, if AREA has no entries.
403 And since the entries are a ring, this may return an entry that
404 entirely preceeds OFFSET. This is the correct behavior: depending
405 on the sizes involved, we could still overlap such an area, with
407 static struct area_entry
*
408 find_entry (struct pv_area
*area
, CORE_ADDR offset
)
410 struct area_entry
*e
= area
->entry
;
415 /* If the next entry would be better than the current one, then scan
416 forward. Since we use '<' in this loop, it always terminates.
418 Note that, even setting aside the addr_mask stuff, we must not
419 simplify this, in high school algebra fashion, to
420 (e->next->offset < e->offset), because of the way < interacts
421 with wrap-around. We have to subtract offset from both sides to
422 make sure both things we're comparing are on the same side of the
424 while (((e
->next
->offset
- offset
) & area
->addr_mask
)
425 < ((e
->offset
- offset
) & area
->addr_mask
))
428 /* If the previous entry would be better than the current one, then
430 while (((e
->prev
->offset
- offset
) & area
->addr_mask
)
431 < ((e
->offset
- offset
) & area
->addr_mask
))
434 /* In case there's some locality to the searches, set the area's
435 pointer to the entry we've found. */
442 /* Return non-zero if the SIZE bytes at OFFSET would overlap ENTRY;
443 return zero otherwise. AREA is the area to which ENTRY belongs. */
445 overlaps (struct pv_area
*area
,
446 struct area_entry
*entry
,
450 /* Think carefully about wrap-around before simplifying this. */
451 return (((entry
->offset
- offset
) & area
->addr_mask
) < size
452 || ((offset
- entry
->offset
) & area
->addr_mask
) < entry
->size
);
457 pv_area_store (struct pv_area
*area
,
462 /* Remove any (potentially) overlapping entries. */
463 if (pv_area_store_would_trash (area
, addr
))
464 clear_entries (area
);
467 CORE_ADDR offset
= addr
.k
;
468 struct area_entry
*e
= find_entry (area
, offset
);
470 /* Delete all entries that we would overlap. */
471 while (e
&& overlaps (area
, e
, offset
, size
))
473 struct area_entry
*next
= (e
->next
== e
) ? 0 : e
->next
;
474 e
->prev
->next
= e
->next
;
475 e
->next
->prev
= e
->prev
;
481 /* Move the area's pointer to the next remaining entry. This
482 will also zero the pointer if we've deleted all the entries. */
486 /* Now, there are no entries overlapping us, and area->entry is
487 either zero or pointing at the closest entry after us. We can
488 just insert ourselves before that.
490 But if we're storing an unknown value, don't bother --- that's
492 if (value
.kind
== pvk_unknown
)
496 CORE_ADDR offset
= addr
.k
;
497 struct area_entry
*e
= (struct area_entry
*) xmalloc (sizeof (*e
));
504 e
->prev
= area
->entry
->prev
;
505 e
->next
= area
->entry
;
506 e
->prev
->next
= e
->next
->prev
= e
;
510 e
->prev
= e
->next
= e
;
518 pv_area_fetch (struct pv_area
*area
, pv_t addr
, CORE_ADDR size
)
520 /* If we have no entries, or we can't decide how ADDR relates to the
521 entries we do have, then the value is unknown. */
523 || pv_area_store_would_trash (area
, addr
))
524 return pv_unknown ();
527 CORE_ADDR offset
= addr
.k
;
528 struct area_entry
*e
= find_entry (area
, offset
);
530 /* If this entry exactly matches what we're looking for, then
531 we're set. Otherwise, say it's unknown. */
532 if (e
->offset
== offset
&& e
->size
== size
)
535 return pv_unknown ();
541 pv_area_find_reg (struct pv_area
*area
,
542 struct gdbarch
*gdbarch
,
546 struct area_entry
*e
= area
->entry
;
551 if (e
->value
.kind
== pvk_register
552 && e
->value
.reg
== reg
554 && e
->size
== register_size (gdbarch
, reg
))
557 *offset_p
= e
->offset
;
563 while (e
!= area
->entry
);
570 pv_area_scan (struct pv_area
*area
,
571 void (*func
) (void *closure
,
577 struct area_entry
*e
= area
->entry
;
580 addr
.kind
= pvk_register
;
581 addr
.reg
= area
->base_reg
;
587 func (closure
, addr
, e
->size
, e
->value
);
590 while (e
!= area
->entry
);