@@ -12,18 +12,7 @@ block_list_t BLOCKS;
1212macro_t * MACROS ;
1313int macros_idx = 0 ;
1414
15- /* the first element is reserved for global scope */
16- func_t * FUNCS ;
17- int funcs_idx = 1 ;
18-
19- /* FUNC_TRIES is used to improve the performance of the find_func function.
20- * Instead of searching through all functions and comparing their names, we can
21- * utilize the trie data structure to search for existing functions efficiently.
22- * The index starts from 1 because the first trie node represents an empty input
23- * string, and it is not possible to record a function with an empty name.
24- */
25- trie_t * FUNC_TRIES ;
26- int func_tries_idx = 1 ;
15+ hashmap_t * FUNCS_MAP ;
2716
2817type_t * TYPES ;
2918int types_idx = 0 ;
@@ -72,72 +61,149 @@ char *elf_strtab;
7261char * elf_section ;
7362
7463/**
75- * insert_trie() - Inserts a new element into the trie structure.
76- * @trie: A pointer to the trie where the name will be inserted.
77- * @name: The name to be inserted into the trie.
78- * @funcs_index: The index of the pointer to the func_t. The index is recorded
79- * in a 1-indexed format. Because the first element of 'FUNCS' has been
80- * reserved, there is no need to shift it.
81- * Return: The index of the pointer to the func_t.
64+ * hash_index_hashmap() - hashses a string with FNV-1a hash function
65+ * and converts into usable hashmap index. The range of returned
66+ * hashmap index is ranged from "(0 ~ 2,147,483,647) mod size" due to
67+ * lack of unsigned integer implementation.
68+ * @map: The key of node. Must not be NULL.
69+ * @key: The key string. May be NULL.
8270 *
83- * If the function has been inserted, the return value is the index of the
84- * function in FUNCS. Otherwise, the return value is the value of the parameter
85- * @funcs_index.
71+ * @returns: The usable hashmap index.
8672 */
87- int insert_trie (trie_t * trie , char * name , int funcs_index )
88- {
89- char first_char ;
90- int fc ;
91-
92- while (1 ) {
93- first_char = * name ;
94- fc = first_char ;
95- if (!fc ) {
96- if (!trie -> index )
97- trie -> index = funcs_index ;
98- return trie -> index ;
99- }
100- if (!trie -> next [fc ]) {
101- /* FIXME: The func_tries_idx variable may exceed the maximum number,
102- * which can lead to a segmentation fault. This issue is affected by
103- * the number of functions and the length of their names. The proper
104- * way to handle this is to dynamically allocate a new element.
105- */
106- trie -> next [fc ] = func_tries_idx ++ ;
107- for (int i = 0 ; i < 128 ; i ++ )
108- FUNC_TRIES [trie -> next [fc ]].next [i ] = 0 ;
109- FUNC_TRIES [trie -> next [fc ]].index = 0 ;
110- }
111- trie = & FUNC_TRIES [trie -> next [fc ]];
112- name ++ ;
73+ int hash_index (int size , char * key )
74+ {
75+ int hash = 0x811c9dc5 , mask ;
76+
77+ for (; * key ; key ++ ) {
78+ hash ^= * key ;
79+ hash *= 0x01000193 ;
11380 }
81+
82+ mask = hash >> 31 ;
83+ return ((hash ^ mask ) - mask ) % size ;
11484}
11585
11686/**
117- * find_trie() - search the index of the function name in the trie
118- * @trie: A pointer to the trie where the name will be searched.
119- * @name: The name to be searched.
87+ * hashmap_create() - creates a hashmap on heap.
88+ * @size: The initial bucket size of hashmap.
12089 *
121- * Return: The index of the pointer to the func_t.
90+ * @returns: The pointer of created hashmap.
91+ */
92+ hashmap_t * hashmap_create (int size )
93+ {
94+ hashmap_t * map = malloc (sizeof (hashmap_t ));
95+ map -> size = size ;
96+ map -> buckets = malloc (size * sizeof (hashmap_node_t * ));
97+
98+ for (int i = 0 ; i < map -> size ; i ++ )
99+ map -> buckets [i ] = 0 ;
100+
101+ return map ;
102+ }
103+
104+ /**
105+ * hashmap_node_new() - creates a hashmap node on heap.
106+ * @key: The key of node. Must not be NULL.
107+ * @val: The value of node. Could be NULL.
122108 *
123- * 0 - the name not found.
124- * otherwise - the index of the founded index in the trie array.
109+ * @returns: The pointer of created node.
125110 */
126- int find_trie (trie_t * trie , char * name )
127- {
128- char first_char ;
129- int fc ;
130-
131- while (1 ) {
132- first_char = * name ;
133- fc = first_char ;
134- if (!fc )
135- return trie -> index ;
136- if (!trie -> next [fc ])
137- return 0 ;
138- trie = & FUNC_TRIES [trie -> next [fc ]];
139- name ++ ;
111+ hashmap_node_t * hashmap_node_new (char * key , void * val )
112+ {
113+ int len = strlen (key );
114+ hashmap_node_t * node = malloc (sizeof (hashmap_node_t ));
115+ node -> key = calloc (len + 1 , sizeof (char ));
116+ strcpy (node -> key , key );
117+ node -> val = val ;
118+ node -> next = NULL ;
119+ return node ;
120+ }
121+
122+ /**
123+ * hashmap_put() - puts a key-value pair into given hashmap.
124+ * If key already contains a value, then replace it with new
125+ * value, the old value will be freed.
126+ * @map: The hashmap to be put into. Must not be NULL.
127+ * @key: The key string. May be NULL.
128+ * @val: The value pointer. May be NULL. This value's lifetime
129+ * is held by hashmap.
130+ */
131+ void hashmap_put (hashmap_t * map , char * key , void * val )
132+ {
133+ int index = hash_index (map -> size , key );
134+ hashmap_node_t * cur = map -> buckets [index ];
135+
136+ if (!cur ) {
137+ map -> buckets [index ] = hashmap_node_new (key , val );
138+ } else {
139+ while (cur -> next )
140+ cur = cur -> next ;
141+ cur -> next = hashmap_node_new (key , val );
140142 }
143+
144+ /* TODO: Rehash if size exceeds size * load factor */
145+ }
146+
147+ /**
148+ * hashmap_get() - gets value from hashmap from given key.
149+ * @map: The hashmap to be looked up. Must no be NULL.
150+ * @key: The key string. May be NULL.
151+ *
152+ * @returns: The look up result, if the key-value pair entry
153+ * exists, then returns its value's address, NULL otherwise.
154+ */
155+ void * hashmap_get (hashmap_t * map , char * key )
156+ {
157+ int index = hash_index (map -> size , key );
158+ hashmap_node_t * cur = map -> buckets [index ];
159+
160+ while (cur ) {
161+ if (!strcmp (cur -> key , key ))
162+ return cur -> val ;
163+
164+ cur = cur -> next ;
165+ }
166+
167+ return NULL ;
168+ }
169+
170+ /**
171+ * hashmap_contains() - checks if the key-value pair entry exists
172+ * from given key.
173+ * @map: The hashmap to be looked up. Must no be NULL.
174+ * @key: The key string. May be NULL.
175+ *
176+ * @returns: The look up result, if the key-value pair entry
177+ * exists, then returns true, false otherwise.
178+ */
179+ bool hashmap_contains (hashmap_t * map , char * key )
180+ {
181+ return hashmap_get (map , key ) != NULL ;
182+ }
183+
184+ /**
185+ * hashmap_free() - frees the hashmap, this also frees key-value pair
186+ * entry's value.
187+ * @map: The hashmap to be looked up. Must no be NULL.
188+ */
189+ void hashmap_free (hashmap_t * map )
190+ {
191+ for (int i = 0 ; i < map -> size ; i ++ ) {
192+ hashmap_node_t * cur = map -> buckets [i ], * next ;
193+
194+ while (cur ) {
195+ next = cur -> next ;
196+ free (cur -> key );
197+ free (cur -> val );
198+ /* FIXME: Remove this if-clause will cause double free error */
199+ if (cur != map -> buckets [0 ])
200+ free (cur );
201+ cur = next ;
202+ }
203+ }
204+
205+ free (map -> buckets );
206+ free (map );
141207}
142208
143209/* options */
@@ -318,12 +384,14 @@ int find_macro_param_src_idx(char *name, block_t *parent)
318384func_t * add_func (char * name )
319385{
320386 func_t * fn ;
321- int index = insert_trie (FUNC_TRIES , name , funcs_idx );
322- if (index == funcs_idx ) {
323- fn = & FUNCS [funcs_idx ++ ];
387+ if (hashmap_contains (FUNCS_MAP , name )) {
388+ fn = hashmap_get (FUNCS_MAP , name );
389+ } else {
390+ fn = malloc (sizeof (func_t ));
391+ hashmap_put (FUNCS_MAP , name , fn );
324392 strcpy (fn -> return_def .var_name , name );
325393 }
326- fn = & FUNCS [ index ];
394+
327395 fn -> stack_size = 4 ; /* starting point of stack */
328396 return fn ;
329397}
@@ -358,10 +426,7 @@ constant_t *find_constant(char alias[])
358426
359427func_t * find_func (char func_name [])
360428{
361- int index = find_trie (FUNC_TRIES , func_name );
362- if (index )
363- return & FUNCS [index ];
364- return NULL ;
429+ return hashmap_get (FUNCS_MAP , func_name );
365430}
366431
367432var_t * find_member (char token [], type_t * type )
@@ -597,8 +662,7 @@ void global_init()
597662 BLOCKS .head = NULL ;
598663 BLOCKS .tail = NULL ;
599664 MACROS = malloc (MAX_ALIASES * sizeof (macro_t ));
600- FUNCS = malloc (MAX_FUNCS * sizeof (func_t ));
601- FUNC_TRIES = malloc (MAX_FUNC_TRIES * sizeof (trie_t ));
665+ FUNCS_MAP = hashmap_create (MAX_FUNCS );
602666 TYPES = malloc (MAX_TYPES * sizeof (type_t ));
603667 GLOBAL_IR = malloc (MAX_GLOBAL_IR * sizeof (ph1_ir_t ));
604668 PH1_IR = malloc (MAX_IR_INSTR * sizeof (ph1_ir_t ));
@@ -616,7 +680,8 @@ void global_init()
616680 elf_section = malloc (MAX_SECTION );
617681
618682 /* set starting point of global stack manually */
619- FUNCS [0 ].stack_size = 4 ;
683+ func_t * global_func = add_func ("" );
684+ global_func -> stack_size = 4 ;
620685}
621686
622687void global_release ()
@@ -627,8 +692,7 @@ void global_release()
627692 BLOCKS .head = next ;
628693 }
629694 free (MACROS );
630- free (FUNCS );
631- free (FUNC_TRIES );
695+ hashmap_free (FUNCS_MAP );
632696 free (TYPES );
633697 free (GLOBAL_IR );
634698 free (PH1_IR );
0 commit comments