1 /*===-- tracelib.c - Runtime routines for tracing ---------------*- C++ -*-===*
3 * Runtime routines for supporting tracing of execution for code generated by
6 *===----------------------------------------------------------------------===*/
13 #include "Support/DataTypes.h"
15 /*===---------------------------------------------------------------------=====
17 *===---------------------------------------------------------------------===*/
19 /* use #defines until we have inlining */
20 typedef uintptr_t Index; /* type of keys, size for hash table */
21 typedef uint32_t Generic; /* type of values stored in table */
23 /* Index IntegerHashFunc(const Generic value, const Index size) */
24 #define IntegerHashFunc(value, size) \
25 ( ((((Index) value) << 3) ^ (((Index) value) >> 3)) % size )
27 /* Index IntegerRehashFunc(const Generic oldHashValue, const Index size) */
28 #define IntegerRehashFunc(oldHashValue, size) \
29 ((Index) ((oldHashValue+16) % size)) /* 16 is relatively prime to a Mersenne prime! */
31 /* Index PointerHashFunc(const void* value, const Index size) */
32 #define PointerHashFunc(value, size) \
33 IntegerHashFunc((Index) value, size)
35 /* Index PointerRehashFunc(const void* value, const Index size) */
36 #define PointerRehashFunc(value, size) \
37 IntegerRehashFunc((Index) value, size)
39 /*===---------------------------------------------------------------------=====
40 * POINTER-TO-GENERIC HASH TABLE.
41 * These should be moved to a separate location: HashTable.[ch]
42 *===---------------------------------------------------------------------===*/
44 typedef enum { FIND, ENTER } ACTION;
45 typedef char FULLEMPTY;
46 const FULLEMPTY EMPTY = '\0';
47 const FULLEMPTY FULL = '\1';
49 const uint MAX_NUM_PROBES = 4;
51 typedef struct PtrValueHashEntry_struct {
56 typedef struct PtrValueHashTable_struct {
57 PtrValueHashEntry* table;
58 FULLEMPTY* fullEmptyFlags;
64 extern Generic LookupOrInsertPtr(PtrValueHashTable* ptrTable,
65 void* ptr, ACTION action);
67 extern void Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value);
69 extern void Delete(PtrValueHashTable* ptrTable, void* ptr);
71 /* Returns NULL if the item is not found. */
72 /* void* LookupPtr(PtrValueHashTable* ptrTable, void* ptr) */
73 #define LookupPtr(ptrTable, ptr) \
74 LookupOrInsertPtr(ptrTable, ptr, FIND)
77 InitializeTable(PtrValueHashTable* ptrTable, Index newSize)
79 ptrTable->table = (PtrValueHashEntry*) calloc(newSize,
80 sizeof(PtrValueHashEntry));
81 ptrTable->fullEmptyFlags = (FULLEMPTY*) calloc(newSize, sizeof(FULLEMPTY));
82 ptrTable->capacity = newSize;
87 CreateTable(Index initialSize)
89 PtrValueHashTable* ptrTable =
90 (PtrValueHashTable*) malloc(sizeof(PtrValueHashTable));
91 InitializeTable(ptrTable, initialSize);
96 ReallocTable(PtrValueHashTable* ptrTable, Index newSize)
98 if (newSize <= ptrTable->capacity)
102 printf("\n***\n*** REALLOCATING SPACE FOR POINTER HASH TABLE.\n");
103 printf("*** oldSize = %ld, oldCapacity = %ld\n***\n\n",
104 (long) ptrTable->size, (long) ptrTable->capacity);
108 PtrValueHashEntry* oldTable = ptrTable->table;
109 FULLEMPTY* oldFlags = ptrTable->fullEmptyFlags;
110 Index oldSize = ptrTable->size;
111 Index oldCapacity = ptrTable->capacity;
113 /* allocate the new storage and flags and re-insert the old entries */
114 InitializeTable(ptrTable, newSize);
115 for (i=0; i < oldCapacity; ++i)
116 if (oldFlags[i] == FULL)
117 Insert(ptrTable, oldTable[i].key, oldTable[i].value);
119 assert(ptrTable->size == oldSize && "Incorrect number of entries copied?");
122 for (i=0; i < oldCapacity; ++i)
124 assert(LookupPtr(ptrTable, oldTable[i].key) == oldTable[i].value);
132 DeleteTable(PtrValueHashTable* ptrTable)
134 free(ptrTable->table);
135 free(ptrTable->fullEmptyFlags);
136 memset(ptrTable, '\0', sizeof(PtrValueHashTable));
141 InsertAtIndex(PtrValueHashTable* ptrTable, void* ptr, Generic value, Index index)
143 assert(ptrTable->fullEmptyFlags[index] == EMPTY && "Slot is in use!");
144 ptrTable->table[index].key = ptr;
145 ptrTable->table[index].value = value;
146 ptrTable->fullEmptyFlags[index] = FULL;
151 DeleteAtIndex(PtrValueHashTable* ptrTable, Index index)
153 assert(ptrTable->fullEmptyFlags[index] == FULL && "Deleting empty slot!");
154 ptrTable->table[index].key = NULL;
155 ptrTable->table[index].value = (Generic) NULL;
156 ptrTable->fullEmptyFlags[index] = EMPTY;
161 FindIndex(PtrValueHashTable* ptrTable, void* ptr)
164 Index index = PointerHashFunc(ptr, ptrTable->capacity);
165 if (ptrTable->fullEmptyFlags[index] == FULL)
167 if (ptrTable->table[index].key == ptr)
170 /* First lookup failed on non-empty slot: probe further */
171 while (numProbes < MAX_NUM_PROBES)
173 index = PointerRehashFunc(index, ptrTable->capacity);
174 if (ptrTable->fullEmptyFlags[index] == EMPTY)
176 else if (ptrTable->table[index].key == ptr)
182 /* Lookup failed: item is not in the table. */
183 /* If last slot is empty, use that slot. */
184 /* Otherwise, table must have been reallocated, so search again. */
186 if (numProbes == MAX_NUM_PROBES)
187 { /* table is too full: reallocate and search again */
188 ReallocTable(ptrTable, 1 + 2*ptrTable->capacity);
189 return FindIndex(ptrTable, ptr);
193 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
194 "Stopped before finding an empty slot and before MAX probes!");
200 LookupOrInsertPtr(PtrValueHashTable* ptrTable, void* ptr, ACTION action)
202 Index index = FindIndex(ptrTable, ptr);
203 if (ptrTable->fullEmptyFlags[index] == FULL &&
204 ptrTable->table[index].key == ptr)
205 return ptrTable->table[index].value;
207 /* Lookup failed: item is not in the table */
209 return (Generic) NULL;
211 /* Insert item into the table and return its index */
212 InsertAtIndex(ptrTable, ptr, (Generic) NULL, index);
213 return (Generic) NULL;
217 Insert(PtrValueHashTable* ptrTable, void* ptr, Generic value)
219 Index index = FindIndex(ptrTable, ptr);
220 assert(ptrTable->fullEmptyFlags[index] == EMPTY &&
221 "ptr is already in the table: delete it first!");
222 InsertAtIndex(ptrTable, ptr, value, index);
226 Delete(PtrValueHashTable* ptrTable, void* ptr)
228 Index index = FindIndex(ptrTable, ptr);
229 if (ptrTable->fullEmptyFlags[index] == FULL &&
230 ptrTable->table[index].key == ptr)
232 DeleteAtIndex(ptrTable, index);
236 /*===---------------------------------------------------------------------=====
237 * RUNTIME ROUTINES TO MAP POINTERS TO SEQUENCE NUMBERS
238 *===---------------------------------------------------------------------===*/
240 PtrValueHashTable* SequenceNumberTable = NULL;
241 Index INITIAL_SIZE = 1 << 22;
243 #define MAX_NUM_SAVED 1024
245 typedef struct PointerSet_struct {
246 char* savedPointers[MAX_NUM_SAVED]; /* 1024 alloca'd ptrs shd suffice */
247 unsigned int numSaved;
248 struct PointerSet_struct* nextOnStack; /* implement a cheap stack */
251 PointerSet* topOfStack = NULL;
254 HashPointerToSeqNum(char* ptr)
256 static SequenceNumber count = 0;
257 SequenceNumber seqnum;
258 if (SequenceNumberTable == NULL) {
259 assert(MAX_NUM_PROBES < INITIAL_SIZE+1 && "Initial size too small");
260 SequenceNumberTable = CreateTable(INITIAL_SIZE);
262 seqnum = (SequenceNumber) LookupPtr(SequenceNumberTable, ptr);
265 Insert(SequenceNumberTable, ptr, ++count);
272 ReleasePointerSeqNum(char* ptr)
273 { /* if a sequence number was assigned to this ptr, release it */
274 if (SequenceNumberTable != NULL)
275 Delete(SequenceNumberTable, ptr);
281 PointerSet* newSet = (PointerSet*) malloc(sizeof(PointerSet));
282 newSet->numSaved = 0;
283 newSet->nextOnStack = topOfStack;
291 assert(topOfStack != NULL && "popping from empty stack!");
293 topOfStack = oldSet->nextOnStack;
294 assert(oldSet->numSaved == 0);
298 /* free the pointers! */
300 ReleaseRecordedPointers(char* savedPointers[MAX_NUM_SAVED],
301 unsigned int numSaved)
304 for (i=0; i < topOfStack->numSaved; ++i)
305 ReleasePointerSeqNum(topOfStack->savedPointers[i]);
309 ReleasePointersPopSet()
311 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
312 topOfStack->numSaved = 0;
317 RecordPointer(char* ptr)
318 { /* record pointers for release later */
319 if (topOfStack->numSaved == MAX_NUM_SAVED) {
320 printf("***\n*** WARNING: OUT OF ROOM FOR SAVED POINTERS."
321 " ALL POINTERS ARE BEING FREED.\n"
322 "*** THE SEQUENCE NUMBERS OF SAVED POINTERS WILL CHANGE!\n*** \n");
323 ReleaseRecordedPointers(topOfStack->savedPointers, topOfStack->numSaved);
324 topOfStack->numSaved = 0;
326 topOfStack->savedPointers[topOfStack->numSaved++] = ptr;
329 /*===---------------------------------------------------------------------=====
330 * TEST DRIVER FOR INSTRUMENTATION LIBRARY
331 *===---------------------------------------------------------------------===*/
333 #ifndef TEST_INSTRLIB
334 #undef TEST_INSTRLIB /* #define this to turn on by default */
339 main(int argc, char** argv)
344 INITIAL_SIZE = 5; /* start with small table to test realloc's*/
346 if (argc > 1 && ! strcmp(argv[1], "-r"))
352 for (i=0; i < argc; ++i)
353 for (j=0; argv[i][j]; ++j)
355 printf("Sequence number for argc[%d][%d] (%c) = Hash(%p) = %d\n",
356 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));
359 RecordPointer(argv[i]+j);
363 ReleasePointersPopSet();
365 /* print sequence numbers out again to compare with (-r) and w/o release */
366 for (i=argc-1; i >= 0; --i)
367 for (j=0; argv[i][j]; ++j)
368 printf("Sequence number for argc[%d][%d] (%c) = %d\n",
369 i, j, argv[i][j], argv[i]+j, HashPointerToSeqNum(argv[i]+j));