-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.c
More file actions
249 lines (233 loc) · 6.9 KB
/
hashmap.c
File metadata and controls
249 lines (233 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <stdlib.h>
#include <stdint.h>
#include "hashmap.h"
#include "slice2.h"
#include "main.h"
#include "statement.h"
/**
* This gets the offset that the slice is from the rbp stored on the hashmap, returns 0 if the variable does not exist
*/
int32_t get_map_offset(struct map* map, Slice* key) {
bool* visited = map->visited;
hash_map* symbol_table = map->map;
uint32_t size = map->size;
int hash = sliceHash(key) % size;
if (visited[hash])
{
struct symbol_table *table = &symbol_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
return table->bins[i].value;
}
}
}
return 0;
}
/**
* This adds the offset that the variable is stored at to the hashmap
*/
void add_map_offset(struct map* map, Slice* key, int32_t value) {
bool *visited = map->visited;
hash_map *symbol_table = map->map;
uint32_t size = map->size;
int hash = sliceHash(key) % size;
if (visited[hash]) //if the bin has already been created
{
struct symbol_table *table = &symbol_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
table->bins[i].value = value;
return;
}
}
// doubles the current bin if it is full
if (table->cur == table->size)
{
table->size *= 2;
table->bins = realloc(table->bins, table->size * sizeof(struct bin));
}
table->bins[table->cur].key = key;
table->bins[table->cur].value = value;
table->cur += 1;
}
else
{ //create a new bin
visited[hash] = true;
symbol_table[hash].size = 2;
symbol_table[hash].cur = 1;
symbol_table[hash].bins = malloc(2 * sizeof(struct bin));
symbol_table[hash].bins[0].key = key;
symbol_table[hash].bins[0].value = value;
}
}
/**
* Checks if a key existins in a given map
*/
bool symbol_exists(bool *visited, hash_map *map, uint32_t size, Slice *key)
{
int hash = sliceHash(key) % size;
if (visited[hash])
{
struct symbol_table *table = &map[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
return true;
}
}
}
return false;
}
// adds a key value pair to the map
void addSymbol(Interpreter *in, struct map *map, Slice *key, uint64_t value, bool force_local)
{
bool *visited;
hash_map *symbol_table;
uint32_t size;
if (map->main || force_local) //Checks whether the symbol should be added to the local map or the global map
{
visited = map->visited;
symbol_table = map->map;
size = map->size;
}
else if (!symbol_exists(map->visited, map->map, map->size, key) && symbol_exists(in->visited, in->symbol_table, MAX_SYMBOLS, key))
{ //checks taht the symbol does not exist in the local map and that it exists in the global map
visited = in->visited;
symbol_table = in->symbol_table;
size = MAX_SYMBOLS;
}
else
{
visited = map->visited;
symbol_table = map->map;
size = map->size;
}
int hash = sliceHash(key) % size;
if (visited[hash])
{
struct symbol_table *table = &symbol_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
table->bins[i].value = value;
return;
}
}
// doubles the current bin if it is full
if (table->cur == table->size)
{
table->size *= 2;
table->bins = realloc(table->bins, table->size * sizeof(struct bin));
}
table->bins[table->cur].key = key;
table->bins[table->cur].value = value;
table->cur += 1;
}
else
{
visited[hash] = true;
symbol_table[hash].size = 2;
symbol_table[hash].cur = 1;
symbol_table[hash].bins = malloc(2 * sizeof(struct bin));
symbol_table[hash].bins[0].key = key;
symbol_table[hash].bins[0].value = value;
}
}
// retrives the key from the map, if it is not found it returns 0
uint64_t getSymbol(Interpreter *in, struct map *map, Slice *key)
{
bool *visited = map->visited;
hash_map *symbol_table = map->map;
uint32_t size = map->size;
int hash = sliceHash(key) % size;
if (visited[hash]) //checks the local map
{
struct symbol_table *table = &symbol_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
return table->bins[i].value;
}
}
}
if (!map->main) //checks the global map
{
visited = in->visited;
symbol_table = in->symbol_table;
size = MAX_SYMBOLS;
hash = sliceHash(key) % size;
if (visited[hash])
{
struct symbol_table *table = &symbol_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].key, key))
{
return table->bins[i].value;
}
}
}
}
return 0;
}
/**
* Adds a function to the function table
*/
void add_function(Interpreter *in, struct declare *function)
{
int hash = sliceHash(function->name) % MAX_SYMBOLS;
if (in->visited_func[hash])
{
struct function_table *table = &in->function_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].name, function->name))
{
table->bins[i] = *function; // Not needed unless we want to change function declaration
return;
}
}
// doubles the current bin if it is full
if (table->cur == table->size)
{
table->size *= 2;
table->bins = realloc(table->bins, table->size * sizeof(struct declare));
}
table->bins[table->cur] = *function;
table->cur += 1;
}
else
{
in->visited_func[hash] = true;
in->function_table[hash].size = 2;
in->function_table[hash].cur = 1;
in->function_table[hash].bins = malloc(2 * sizeof(struct declare));
in->function_table[hash].bins[0] = *function;
}
}
/**
* Gets the function with the Slice* name from the function table
*/
struct declare *get_function(Interpreter *in, Slice *name)
{
int hash = sliceHash(name) % MAX_SYMBOLS;
if (in->visited_func[hash])
{
struct function_table *table = &in->function_table[hash];
for (int i = 0; i < table->cur; i++)
{
if (slice_eq2(table->bins[i].name, name))
{
return &table->bins[i];
}
}
}
return NULL;
}