|
| 1 | +/* VgHashTable-based implementation for inline lookup table. |
| 2 | + Maps addresses to inline function info indices. */ |
| 3 | + |
| 4 | +#include "pub_core_basics.h" |
| 5 | +#include "pub_core_libcbase.h" |
| 6 | +#include "pub_core_libcassert.h" |
| 7 | +#include "pub_core_mallocfree.h" |
| 8 | +#include "pub_core_hashtable.h" |
| 9 | +#include "priv_storage.h" |
| 10 | + |
| 11 | +/* Node structure for VgHashTable |
| 12 | + Extends VgHashNode with our key (Addr) and value (Word) */ |
| 13 | +typedef struct { |
| 14 | + VgHashNode node; /* Must be first for VgHashTable API */ |
| 15 | + Word value; /* The inline table index */ |
| 16 | +} InlLookupNode; |
| 17 | + |
| 18 | +/* Binary search for an inline function entry containing address 'a'. |
| 19 | + Uses the sorted inltab array to efficiently find candidates. |
| 20 | + Returns the index of the matching entry, or -1 if not found. |
| 21 | +
|
| 22 | + The inltab is sorted by addr_lo, so we: |
| 23 | + 1. Binary search to find entries where addr_lo <= a |
| 24 | + 2. Scan backwards to find an entry that contains address a |
| 25 | + (this finds the innermost inline function if multiple are nested) |
| 26 | +*/ |
| 27 | +static Word binary_search_inltab(const DebugInfo* di, Addr a) { |
| 28 | + if (di == NULL || di->inltab == NULL || di->inltab_used == 0) |
| 29 | + return -1; |
| 30 | + |
| 31 | + Word lo = 0, hi = di->inltab_used; |
| 32 | + |
| 33 | + /* Binary search: find the first entry where addr_lo > a */ |
| 34 | + while (lo < hi) { |
| 35 | + Word mid = lo + (hi - lo) / 2; |
| 36 | + if (a < di->inltab[mid].addr_lo) { |
| 37 | + hi = mid; |
| 38 | + } else { |
| 39 | + lo = mid + 1; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /* lo now points to the first entry where addr_lo > a. |
| 44 | + Search backwards from lo-1 to find an entry that contains address a */ |
| 45 | + for (Word i = lo - 1; i >= 0; i--) { |
| 46 | + if (di->inltab[i].addr_lo <= a && a < di->inltab[i].addr_hi) { |
| 47 | + /* Found it! Return the index */ |
| 48 | + return i; |
| 49 | + } |
| 50 | + /* Since we're scanning backwards through sorted addr_lo entries, |
| 51 | + if we encounter an addr_hi <= a, we can stop as we've passed |
| 52 | + the potential range */ |
| 53 | + if (di->inltab[i].addr_hi <= a) { |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return -1; |
| 59 | +} |
| 60 | + |
| 61 | +/* Public API for inline lookup table */ |
| 62 | + |
| 63 | +VgHashTable *VG_(inltab_lookup_new)(void) { |
| 64 | + return VG_(HT_construct)("inltab_lookup"); |
| 65 | +} |
| 66 | + |
| 67 | +void VG_(inltab_lookup_insert)(VgHashTable *ht, Addr addr, Word inl_idx) { |
| 68 | + if (ht == NULL) |
| 69 | + ht = VG_(inltab_lookup_new)(); |
| 70 | + |
| 71 | + /* Check if entry already exists */ |
| 72 | + InlLookupNode *existing = VG_(HT_lookup)(ht, (UWord)addr); |
| 73 | + if (existing) { |
| 74 | + /* Update existing value */ |
| 75 | + existing->value = inl_idx; |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + /* Allocate new node */ |
| 80 | + InlLookupNode *node = VG_(malloc)("di.inltab_vghash.node", sizeof(InlLookupNode)); |
| 81 | + if (!node) |
| 82 | + return; |
| 83 | + node->node.key = (UWord)addr; |
| 84 | + node->value = inl_idx; |
| 85 | + |
| 86 | + VG_(HT_add_node)(ht, node); |
| 87 | +} |
| 88 | + |
| 89 | +/* Enhanced lookup that combines hash table lookup with binary search fallback. |
| 90 | + First tries the hash table for exact addr_lo matches. |
| 91 | + If not found, uses binary search on the sorted inltab array. |
| 92 | + If found via binary search, caches the result in the hash table. |
| 93 | +*/ |
| 94 | +Bool VG_(inltab_lookup_get)(VgHashTable *ht, Addr addr, Word *inl_idx, |
| 95 | + const DebugInfo* di) { |
| 96 | + if (ht == NULL) |
| 97 | + return False; |
| 98 | + |
| 99 | + /* Try hash table lookup first */ |
| 100 | + InlLookupNode *node = VG_(HT_lookup)(ht, (UWord)addr); |
| 101 | + if (node) { |
| 102 | + *inl_idx = node->value; |
| 103 | + return True; |
| 104 | + } |
| 105 | + |
| 106 | + /* Hash table miss, try binary search on the inltab array */ |
| 107 | + Word idx = binary_search_inltab(di, addr); |
| 108 | + if (idx >= 0) { |
| 109 | + /* Found via binary search, cache it for future lookups */ |
| 110 | + *inl_idx = idx; |
| 111 | + VG_(inltab_lookup_insert)(ht, addr, idx); |
| 112 | + return True; |
| 113 | + } |
| 114 | + |
| 115 | + return False; |
| 116 | +} |
| 117 | + |
| 118 | +void VG_(inltab_lookup_cleanup)(VgHashTable *ht) { |
| 119 | + if (ht == NULL) |
| 120 | + return; |
| 121 | + |
| 122 | + /* Free all nodes */ |
| 123 | + VG_(HT_destruct)(ht, VG_(free)); |
| 124 | +} |
0 commit comments