-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathhelper.c
430 lines (373 loc) · 10 KB
/
helper.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "compiler.h"
#include "helpers/vector.h"
#include <assert.h>
size_t variable_size(struct node *var_node)
{
assert(var_node->type == NODE_TYPE_VARIABLE);
return datatype_size(&var_node->var.type);
}
struct datatype* datatype_thats_a_pointer(struct datatype* d1, struct datatype* d2)
{
if (d1->flags & DATATYPE_FLAG_IS_POINTER)
{
return d1;
}
if (d2->flags & DATATYPE_FLAG_IS_POINTER)
{
return d2;
}
return NULL;
}
bool is_logical_operator(const char* op)
{
return S_EQ(op, "&&") || S_EQ(op, "||");
}
bool is_logical_node(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION && is_logical_operator(node->exp.op);
}
struct datatype* datatype_pointer_reduce(struct datatype* datatype, int by)
{
struct datatype* new_datatype = calloc(1, sizeof(struct datatype));
memcpy(new_datatype, datatype, sizeof(struct datatype));
new_datatype->pointer_depth -= by;
if (new_datatype->pointer_depth <= 0)
{
new_datatype->flags &= ~DATATYPE_FLAG_IS_POINTER;
new_datatype->pointer_depth = 0;
}
return new_datatype;
}
size_t variable_size_for_list(struct node *var_list_node)
{
assert(var_list_node->type == NODE_TYPE_VARIABLE_LIST);
size_t size = 0;
vector_set_peek_pointer(var_list_node->var_list.list, 0);
struct node *var_node = vector_peek_ptr(var_list_node->var_list.list);
while (var_node)
{
size += variable_size(var_node);
var_node = vector_peek_ptr(var_list_node->var_list.list);
}
return size;
}
struct node *variable_struct_or_union_body_node(struct node *node)
{
if (!node_is_struct_or_union_variable(node))
{
return NULL;
}
if (node->var.type.type == DATA_TYPE_STRUCT)
{
return node->var.type.struct_node->_struct.body_n;
}
// return the union body.
if (node->var.type.type == DATA_TYPE_UNION)
{
return node->var.type.union_node->_union.body_n;
}
return NULL;
}
int padding(int val, int to)
{
if (to <= 0)
{
return 0;
}
if ((val % to) == 0)
{
return 0;
}
return to - (val % to) % to;
}
int align_value(int val, int to)
{
if (val % to)
{
val += padding(val, to);
}
return val;
}
int align_value_treat_positive(int val, int to)
{
assert(to >= 0);
if (val < 0)
{
to = -to;
}
return align_value(val, to);
}
int compute_sum_padding(struct vector *vec)
{
int padding = 0;
int last_type = -1;
bool mixed_types = false;
vector_set_peek_pointer(vec, 0);
struct node *cur_node = vector_peek_ptr(vec);
struct node *last_node = NULL;
while (cur_node)
{
if (cur_node->type != NODE_TYPE_VARIABLE)
{
cur_node = vector_peek_ptr(vec);
continue;
}
padding += cur_node->var.padding;
last_type = cur_node->var.type.type;
last_node = cur_node;
cur_node = vector_peek_ptr(vec);
}
return padding;
}
int array_multiplier(struct datatype *dtype, int index, int index_value)
{
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY))
{
return index_value;
}
vector_set_peek_pointer(dtype->array.brackets->n_brackets, index + 1);
int size_sum = index_value;
struct node *bracket_node = vector_peek_ptr(dtype->array.brackets->n_brackets);
while (bracket_node)
{
assert(bracket_node->bracket.inner->type == NODE_TYPE_NUMBER);
int declared_index = bracket_node->bracket.inner->llnum;
int size_value = declared_index;
size_sum *= size_value;
bracket_node = vector_peek_ptr(dtype->array.brackets->n_brackets);
}
return size_sum;
}
int array_offset(struct datatype *dtype, int index, int index_value)
{
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY) ||
(index == vector_count(dtype->array.brackets->n_brackets) - 1))
{
return index_value * datatype_element_size(dtype);
}
return array_multiplier(dtype, index, index_value) * datatype_element_size(dtype);
}
struct node* body_largest_variable_node(struct node* body_node)
{
if (!body_node)
{
return NULL;
}
if(body_node->type != NODE_TYPE_BODY)
{
return NULL;
}
return body_node->body.largest_var_node;
}
struct node* variable_struct_or_union_largest_variable_node(struct node* var_node)
{
return body_largest_variable_node(variable_struct_or_union_body_node(var_node));
}
int struct_offset(struct compile_process* compile_proc, const char* struct_name, const char* var_name, struct node** var_node_out, int last_pos, int flags)
{
struct symbol* struct_sym = symresolver_get_symbol(compile_proc, struct_name);
assert(struct_sym && struct_sym->type == SYMBOL_TYPE_NODE);
struct node* node = struct_sym->data;
assert(node_is_struct_or_union(node));
struct vector* struct_vars_vec = node->_struct.body_n->body.statements;
vector_set_peek_pointer(struct_vars_vec, 0);
if (flags & STRUCT_ACCESS_BACKWARDS)
{
vector_set_peek_pointer_end(struct_vars_vec);
vector_set_flag(struct_vars_vec, VECTOR_FLAG_PEEK_DECREMENT);
}
struct node* var_node_cur = variable_node(vector_peek_ptr(struct_vars_vec));
struct node* var_node_last = NULL;
int position = last_pos;
*var_node_out = NULL;
while(var_node_cur)
{
*var_node_out = var_node_cur;
if (var_node_last)
{
position += variable_size(var_node_last);
if (variable_node_is_primitive(var_node_cur))
{
position = align_value_treat_positive(position, var_node_cur->var.type.size);
}
else
{
position = align_value_treat_positive(position, variable_struct_or_union_largest_variable_node(var_node_cur)->var.type.size);
}
}
// Have we found the variable? then we are done
if (S_EQ(var_node_cur->var.name, var_name))
{
break;
}
var_node_last = var_node_cur;
var_node_cur = variable_node(vector_peek_ptr(struct_vars_vec));
}
vector_unset_flag(struct_vars_vec, VECTOR_FLAG_PEEK_DECREMENT);
return position;
}
bool is_access_operator(const char* op)
{
return S_EQ(op, "->") || S_EQ(op, ".");
}
bool is_access_node(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION && is_access_operator(node->exp.op);
}
bool is_access_node_with_op(struct node* node, const char* op)
{
return is_access_node(node) && S_EQ(node->exp.op, op);
}
bool is_array_operator(const char* op)
{
return S_EQ(op, "[]");
}
bool is_array_node(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION && is_array_operator(node->exp.op);
}
bool is_parentheses_operator(const char* op)
{
return S_EQ(op, "()");
}
bool is_parentheses_node(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION && is_parentheses_operator(node->exp.op);
}
bool is_argument_operator(const char* op)
{
return S_EQ(op, ",");
}
bool is_argument_node(struct node* node)
{
return node->type == NODE_TYPE_EXPRESSION && is_argument_operator(node->exp.op);
}
bool is_unary_operator(const char* op)
{
return S_EQ(op, "-") || S_EQ(op, "!") || S_EQ(op, "~") || S_EQ(op, "*") || S_EQ(op, "&") || S_EQ(op, "++") || S_EQ(op, "--");
}
bool op_is_indirection(const char* op)
{
return S_EQ(op, "*");
}
bool op_is_address(const char* op)
{
return S_EQ(op, "&");
}
bool file_exists(const char* filename)
{
FILE* f = fopen(filename, "r");
if (!f)
{
return false;
}
fclose(f);
return true;
}
struct datatype datatype_for_numeric()
{
struct datatype dtype = {};
dtype.flags |= DATATYPE_FLAG_IS_LITERAL;
dtype.type = DATA_TYPE_INTEGER;
dtype.type_str = "int";
dtype.size = DATA_SIZE_DWORD;
return dtype;
}
struct datatype datatype_for_string()
{
struct datatype dtype = {};
dtype.type = DATA_TYPE_INTEGER;
dtype.type_str = "char";
dtype.flags |= DATATYPE_FLAG_IS_POINTER | DATATYPE_FLAG_IS_LITERAL;
dtype.pointer_depth = 1;
dtype.size = DATA_SIZE_DWORD;
return dtype;
}
bool is_parentheses(const char* op)
{
return (S_EQ(op, "("));
}
bool is_left_operanded_unary_operator(const char* op)
{
return S_EQ(op, "++") || S_EQ(op, "--");
}
bool unary_operand_compatible(struct token* token)
{
return is_access_operator(token->sval) ||
is_array_operator(token->sval) ||
is_parentheses(token->sval);
}
void datatype_decrement_pointer(struct datatype* dtype)
{
dtype->pointer_depth--;
if (dtype->pointer_depth <= 0)
{
dtype->flags &= ~DATATYPE_FLAG_IS_POINTER;
}
}
long arithmetic(struct compile_process* compiler, long left_operand, long right_operand, const char* op, bool* success)
{
*success = true;
int result = 0;
if (S_EQ(op, "*"))
{
result = left_operand * right_operand;
}
else if(S_EQ(op, "/"))
{
result = left_operand / right_operand;
}
else if(S_EQ(op, "+"))
{
result = left_operand + right_operand;
}
else if(S_EQ(op, "-"))
{
result = left_operand - right_operand;
}
else if(S_EQ(op, "=="))
{
result = left_operand == right_operand;
}
else if(S_EQ(op, "!="))
{
result = left_operand != right_operand;
}
else if(S_EQ(op, ">"))
{
result = left_operand > right_operand;
}
else if(S_EQ(op, "<"))
{
result = left_operand < right_operand;
}
else if(S_EQ(op, ">="))
{
result = left_operand >= right_operand;
}
else if(S_EQ(op, "<="))
{
result = left_operand <= right_operand;
}
else if(S_EQ(op, "<<"))
{
result = left_operand << right_operand;
}
else if(S_EQ(op, ">>"))
{
result = left_operand >> right_operand;
}
else if(S_EQ(op, "&&"))
{
result = left_operand && right_operand;
}
else if(S_EQ(op, "||"))
{
result = left_operand || right_operand;
}
else
{
*success = false;
}
return result;
}