@@ -15,18 +15,13 @@ block_list_t BLOCKS;
15
15
macro_t * MACROS ;
16
16
int macros_idx = 0 ;
17
17
18
- /* the first element is reserved for global scope */
19
- func_t * FUNCS ;
20
- int funcs_idx = 1 ;
21
-
22
- /* FUNC_TRIES is used to improve the performance of the find_func function.
23
- * Instead of searching through all functions and comparing their names, we can
24
- * utilize the trie data structure to search for existing functions efficiently.
25
- * The index starts from 1 because the first trie node represents an empty input
26
- * string, and it is not possible to record a function with an empty name.
18
+ /* FUNCS_MAP is used to integerate function storing and boost lookup
19
+ * performance, currently it uses FNV-1a hash function to hash function
20
+ * name. The bucket size defaults to MAX_FUNCS. Ideally, it should be a small
21
+ * number, but due to lack of rehashing implementation, to prevent collision,
22
+ * we have to initially create large amount of buckets.
27
23
*/
28
- trie_t * FUNC_TRIES ;
29
- int func_tries_idx = 1 ;
24
+ hashmap_t * FUNCS_MAP ;
30
25
31
26
type_t * TYPES ;
32
27
int types_idx = 0 ;
@@ -75,72 +70,143 @@ char *elf_strtab;
75
70
char * elf_section ;
76
71
77
72
/**
78
- * insert_trie() - Inserts a new element into the trie structure.
79
- * @trie: A pointer to the trie where the name will be inserted.
80
- * @name: The name to be inserted into the trie.
81
- * @funcs_index: The index of the pointer to the func_t. The index is recorded
82
- * in a 1-indexed format. Because the first element of 'FUNCS' has been
83
- * reserved, there is no need to shift it.
84
- * Return: The index of the pointer to the func_t.
73
+ * hashmap_hash_index() - hashses a string with FNV-1a hash function
74
+ * and converts into usable hashmap index. The range of returned
75
+ * hashmap index is ranged from "(0 ~ 2,147,483,647) mod size" due to
76
+ * lack of unsigned integer implementation.
77
+ * @size: The size of map. Must not be negative or 0.
78
+ * @key: The key string. May be NULL.
85
79
*
86
- * If the function has been inserted, the return value is the index of the
87
- * function in FUNCS. Otherwise, the return value is the value of the parameter
88
- * @funcs_index.
80
+ * @returns: The usable hashmap index.
89
81
*/
90
- int insert_trie (trie_t * trie , char * name , int funcs_index )
91
- {
92
- char first_char ;
93
- int fc ;
94
-
95
- while (1 ) {
96
- first_char = * name ;
97
- fc = first_char ;
98
- if (!fc ) {
99
- if (!trie -> index )
100
- trie -> index = funcs_index ;
101
- return trie -> index ;
102
- }
103
- if (!trie -> next [fc ]) {
104
- /* FIXME: The func_tries_idx variable may exceed the maximum number,
105
- * which can lead to a segmentation fault. This issue is affected by
106
- * the number of functions and the length of their names. The proper
107
- * way to handle this is to dynamically allocate a new element.
108
- */
109
- trie -> next [fc ] = func_tries_idx ++ ;
110
- for (int i = 0 ; i < 128 ; i ++ )
111
- FUNC_TRIES [trie -> next [fc ]].next [i ] = 0 ;
112
- FUNC_TRIES [trie -> next [fc ]].index = 0 ;
113
- }
114
- trie = & FUNC_TRIES [trie -> next [fc ]];
115
- name ++ ;
82
+ int hashmap_hash_index (int size , char * key )
83
+ {
84
+ int hash = 0x811c9dc5 , mask ;
85
+
86
+ for (; * key ; key ++ ) {
87
+ hash ^= * key ;
88
+ hash *= 0x01000193 ;
89
+ }
90
+
91
+ mask = hash >> 31 ;
92
+ return ((hash ^ mask ) - mask ) % size ;
93
+ }
94
+
95
+ /**
96
+ * hashmap_create() - creates a hashmap on heap.
97
+ * @size: The initial bucket size of hashmap. Must not be 0.
98
+ *
99
+ * @returns: The pointer of created hashmap.
100
+ */
101
+ hashmap_t * hashmap_create (int size )
102
+ {
103
+ hashmap_t * map = malloc (sizeof (hashmap_t ));
104
+ map -> size = size ;
105
+ map -> buckets = malloc (size * sizeof (hashmap_node_t * ));
106
+
107
+ for (int i = 0 ; i < map -> size ; i ++ )
108
+ map -> buckets [i ] = 0 ;
109
+
110
+ return map ;
111
+ }
112
+
113
+ /**
114
+ * hashmap_node_new() - creates a hashmap node on heap.
115
+ * @key: The key of node. Must not be NULL.
116
+ * @val: The value of node. Could be NULL.
117
+ *
118
+ * @returns: The pointer of created node.
119
+ */
120
+ hashmap_node_t * hashmap_node_new (char * key , void * val )
121
+ {
122
+ int len = strlen (key );
123
+ hashmap_node_t * node = malloc (sizeof (hashmap_node_t ));
124
+ node -> key = calloc (len + 1 , sizeof (char ));
125
+ strcpy (node -> key , key );
126
+ node -> val = val ;
127
+ node -> next = NULL ;
128
+ return node ;
129
+ }
130
+
131
+ /**
132
+ * hashmap_put() - puts a key-value pair into given hashmap.
133
+ * If key already contains a value, then replace it with new
134
+ * value, the old value will be freed.
135
+ * @map: The hashmap to be put into. Must not be NULL.
136
+ * @key: The key string. May be NULL.
137
+ * @val: The value pointer. May be NULL. This value's lifetime
138
+ * is held by hashmap.
139
+ */
140
+ void hashmap_put (hashmap_t * map , char * key , void * val )
141
+ {
142
+ int index = hashmap_hash_index (map -> size , key );
143
+ hashmap_node_t * cur = map -> buckets [index ];
144
+
145
+ if (!cur ) {
146
+ map -> buckets [index ] = hashmap_node_new (key , val );
147
+ } else {
148
+ while (cur -> next )
149
+ cur = cur -> next ;
150
+ cur -> next = hashmap_node_new (key , val );
116
151
}
152
+
153
+ /* TODO: Rehash if size exceeds size * load factor */
117
154
}
118
155
119
156
/**
120
- * find_trie () - search the index of the function name in the trie
121
- * @trie: A pointer to the trie where the name will be searched .
122
- * @name : The name to be searched .
157
+ * hashmap_get () - gets value from hashmap from given key.
158
+ * @map: The hashmap to be looked up. Must no be NULL .
159
+ * @key : The key string. May be NULL .
123
160
*
124
- * Return: The index of the pointer to the func_t.
161
+ * @returns: The look up result, if the key-value pair entry
162
+ * exists, then returns its value's address, NULL otherwise.
163
+ */
164
+ void * hashmap_get (hashmap_t * map , char * key )
165
+ {
166
+ int index = hashmap_hash_index (map -> size , key );
167
+
168
+ for (hashmap_node_t * cur = map -> buckets [index ]; cur ; cur = cur -> next )
169
+ if (!strcmp (cur -> key , key ))
170
+ return cur -> val ;
171
+
172
+ return NULL ;
173
+ }
174
+
175
+ /**
176
+ * hashmap_contains() - checks if the key-value pair entry exists
177
+ * from given key.
178
+ * @map: The hashmap to be looked up. Must no be NULL.
179
+ * @key: The key string. May be NULL.
125
180
*
126
- * 0 - the name not found.
127
- * otherwise - the index of the founded index in the trie array.
181
+ * @returns: The look up result, if the key-value pair entry
182
+ * exists, then returns true, false otherwise.
183
+ */
184
+ bool hashmap_contains (hashmap_t * map , char * key )
185
+ {
186
+ return hashmap_get (map , key );
187
+ }
188
+
189
+ /**
190
+ * hashmap_free() - frees the hashmap, this also frees key-value pair
191
+ * entry's value.
192
+ * @map: The hashmap to be looked up. Must no be NULL.
128
193
*/
129
- int find_trie (trie_t * trie , char * name )
130
- {
131
- char first_char ;
132
- int fc ;
133
-
134
- while (1 ) {
135
- first_char = * name ;
136
- fc = first_char ;
137
- if (!fc )
138
- return trie -> index ;
139
- if (!trie -> next [fc ])
140
- return 0 ;
141
- trie = & FUNC_TRIES [trie -> next [fc ]];
142
- name ++ ;
194
+ void hashmap_free (hashmap_t * map )
195
+ {
196
+ for (int i = 0 ; i < map -> size ; i ++ ) {
197
+ for (hashmap_node_t * cur = map -> buckets [i ], * next ; cur ; cur = next ) {
198
+ next = cur -> next ;
199
+ free (cur -> key );
200
+ free (cur -> val );
201
+ /* FIXME: Remove this if-clause will cause double free error */
202
+ if (cur != map -> buckets [0 ])
203
+ free (cur );
204
+ cur = next ;
205
+ }
143
206
}
207
+
208
+ free (map -> buckets );
209
+ free (map );
144
210
}
145
211
146
212
/* options */
@@ -321,12 +387,14 @@ int find_macro_param_src_idx(char *name, block_t *parent)
321
387
func_t * add_func (char * name )
322
388
{
323
389
func_t * fn ;
324
- int index = insert_trie (FUNC_TRIES , name , funcs_idx );
325
- if (index == funcs_idx ) {
326
- fn = & FUNCS [funcs_idx ++ ];
390
+ if (hashmap_contains (FUNCS_MAP , name )) {
391
+ fn = hashmap_get (FUNCS_MAP , name );
392
+ } else {
393
+ fn = malloc (sizeof (func_t ));
394
+ hashmap_put (FUNCS_MAP , name , fn );
327
395
strcpy (fn -> return_def .var_name , name );
328
396
}
329
- fn = & FUNCS [ index ];
397
+
330
398
fn -> stack_size = 4 ; /* starting point of stack */
331
399
return fn ;
332
400
}
@@ -361,10 +429,7 @@ constant_t *find_constant(char alias[])
361
429
362
430
func_t * find_func (char func_name [])
363
431
{
364
- int index = find_trie (FUNC_TRIES , func_name );
365
- if (index )
366
- return & FUNCS [index ];
367
- return NULL ;
432
+ return hashmap_get (FUNCS_MAP , func_name );
368
433
}
369
434
370
435
var_t * find_member (char token [], type_t * type )
@@ -600,8 +665,7 @@ void global_init()
600
665
BLOCKS .head = NULL ;
601
666
BLOCKS .tail = NULL ;
602
667
MACROS = malloc (MAX_ALIASES * sizeof (macro_t ));
603
- FUNCS = malloc (MAX_FUNCS * sizeof (func_t ));
604
- FUNC_TRIES = malloc (MAX_FUNC_TRIES * sizeof (trie_t ));
668
+ FUNCS_MAP = hashmap_create (MAX_FUNCS );
605
669
TYPES = malloc (MAX_TYPES * sizeof (type_t ));
606
670
GLOBAL_IR = malloc (MAX_GLOBAL_IR * sizeof (ph1_ir_t ));
607
671
PH1_IR = malloc (MAX_IR_INSTR * sizeof (ph1_ir_t ));
@@ -619,7 +683,8 @@ void global_init()
619
683
elf_section = malloc (MAX_SECTION );
620
684
621
685
/* set starting point of global stack manually */
622
- FUNCS [0 ].stack_size = 4 ;
686
+ func_t * global_func = add_func ("" );
687
+ global_func -> stack_size = 4 ;
623
688
}
624
689
625
690
void global_release ()
@@ -630,8 +695,7 @@ void global_release()
630
695
BLOCKS .head = next ;
631
696
}
632
697
free (MACROS );
633
- free (FUNCS );
634
- free (FUNC_TRIES );
698
+ hashmap_free (FUNCS_MAP );
635
699
free (TYPES );
636
700
free (GLOBAL_IR );
637
701
free (PH1_IR );
0 commit comments