14 #define log(fmt, ...) fprintf(stderr, "%s: " fmt, __FUNCTION__, ## __VA_ARGS__)
16 #define log(...) do {;} while (0)
19 #define HASH_TABLE_BITS 8
20 #define HASH_TABLE_SIZE (1 << HASH_TABLE_BITS)
22 /* number of executables seen so far */
25 struct hash_table_entry {
27 /* only used for objecs, not for executables */
31 static struct hash_table_entry exe_table[HASH_TABLE_SIZE];
32 static struct hash_table_entry obj_table[HASH_TABLE_SIZE];
34 /* no need for anything sophisticated here */
35 static int hash_token(const char *tok)
37 uint32_t tmp = 31415927;
38 const uint8_t *src = (typeof(src))tok;
44 return tmp % HASH_TABLE_SIZE;
47 static inline bool slot_empty(int idx, struct hash_table_entry *table)
49 return table[idx].key == NULL;
52 static char *safe_strdup(const char *str)
54 char *result = strdup(str);
62 static bool lookup(const char *tok, struct hash_table_entry *table, int *idx)
64 int i, h = hash_token(tok);
66 for (i = 0; i < HASH_TABLE_SIZE; i++) {
67 *idx = (h + i) % HASH_TABLE_SIZE;
68 if (slot_empty(*idx, table))
70 if (!strcmp(table[*idx].key, tok))
73 log ("hash table full !?\n");
77 static bool insert(const char *tok, struct hash_table_entry *table, int *idx)
79 if (lookup(tok, table, idx))
80 return false; /* not inserted */
81 table[*idx].key = safe_strdup(tok);
85 static void process_token(char *tok)
88 size_t len = strlen(tok);
91 if (tok[len - 1] == ':') {
93 if (insert(tok, exe_table, &idx)) { /* new exe */
94 log("exe #%d: '%s', idx: %d\n", num_exe, tok, idx);
99 log("invalid input\n");
102 insert(tok, obj_table, &idx);
103 obj_table[idx].exe_bitmask |= (1 << (num_exe - 1));
107 static void print_ss_enum(int idx)
109 char *s = obj_table[idx].key;
113 printf("%c", toupper(*s));
117 static void dump_bipolar(void)
121 for (i = 0; i < HASH_TABLE_SIZE; i++) {
122 if (slot_empty(i, obj_table))
124 printf("#ifdef MAIN_INPUT_FILE_IS_%s\n", obj_table[i].key);
125 for (j = 0; j < HASH_TABLE_SIZE; j++) {
127 if (slot_empty(j, obj_table))
129 mi = obj_table[i].exe_bitmask;
130 mj = obj_table[j].exe_bitmask;
144 ret = scanf("%96s", tok);