-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.c
319 lines (262 loc) · 8.24 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
#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);
}
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;
}
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;
}
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;
}
if (node->var.type.type == DATA_TYPE_UNION) {
return node->var.type.union_node->_union.body_n;
}
return NULL;
}
int array_multiplier(struct datatype *dtype, int index, int index_val) {
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY)) {
return index_val;
}
vector_set_peek_pointer(dtype->array.brackets->n_brackets, index + 1);
int size_sum = index_val;
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_val = declared_index;
size_sum *= size_val;
bracket_node = vector_peek_ptr(dtype->array.brackets->n_brackets);
}
return size_sum;
}
int array_offset(struct datatype *dtype, int index, int index_val) {
if (!(dtype->flags & DATATYPE_FLAG_IS_ARRAY) ||
(index == vector_count(dtype->array.brackets->n_brackets) - 1)) {
return index_val * datatype_element_size(dtype);
}
return array_multiplier(dtype, index, index_val) *
datatype_element_size(dtype);
}
struct node *body_larest_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_variable_node;
}
struct node *
variable_struct_or_union_largest_variable_node(struct node *var_node) {
return body_larest_variable_node(
variable_struct_or_union_body_node(var_node));
}
int struct_offset(struct compile_process *compile_process,
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_process, 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);
}
}
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_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_access_node_with_op(struct node *node, const char *op) {
return is_access_node(node) && S_EQ(node->exp.op, 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);
}
void datatype_decrement_pointer(struct datatype *dtype) {
dtype->pointer_depth--;
if (dtype->pointer_depth <= 0) {
dtype->flags &= ~DATATYPE_FLAG_IS_POINTER;
}
}
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, "++") || 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 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);
}
bool is_parentheses(const char *op) { return 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);
}
bool is_left_operanded_unary_operator(const char *op) {
return S_EQ(op, "++") || S_EQ(op, "--");
}
long arithmetic(struct compile_process *compiler, long left, long right,
const char *op, bool *success) {
*success = true;
int res = 0;
if (S_EQ(op, "+")) {
res = left + right;
} else if (S_EQ(op, "-")) {
res = left - right;
} else if (S_EQ(op, "*")) {
res = left * right;
} else if (S_EQ(op, "/")) {
if (right == 0) {
*success = false;
return 0;
}
res = left / right;
} else if (S_EQ(op, "%")) {
if (right == 0) {
*success = false;
return 0;
}
res = left % right;
} else if (S_EQ(op, "<<")) {
res = left << right;
} else if (S_EQ(op, ">>")) {
res = left >> right;
} else if (S_EQ(op, "&")) {
res = left & right;
} else if (S_EQ(op, "|")) {
res = left | right;
} else if (S_EQ(op, "^")) {
res = left ^ right;
} else if (S_EQ(op, "&&")) {
res = left && right;
} else if (S_EQ(op, "||")) {
res = left || right;
} else if (S_EQ(op, "==")) {
res = left == right;
} else if (S_EQ(op, "!=")) {
res = left != right;
} else if (S_EQ(op, "<")) {
res = left < right;
} else if (S_EQ(op, ">")) {
res = left > right;
} else if (S_EQ(op, "<=")) {
res = left <= right;
} else if (S_EQ(op, ">=")) {
res = left >= right;
} else {
*success = false;
}
return res;
}
bool file_exists(const char *filename) {
FILE *file = fopen(filename, "r");
if (file) {
fclose(file);
return true;
}
return false;
}