-
Notifications
You must be signed in to change notification settings - Fork 2
/
analysis.c
305 lines (291 loc) · 10 KB
/
analysis.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
// analysis.c
// SPDX-License-Identifier: Apache-2.0
/*
Copyright 2020 hiromi-mi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "main.h"
#include "selfhost.h"
#include <string.h>
extern Env *env;
extern int lang;
extern Map *typedb;
extern Map *struct_typedb;
extern Map *current_local_typedb;
extern int neg_float_generate;
extern int neg_double_generate;
Node *new_long_num_node(long num_val);
Node *implicit_althemic_type_conversion(Node *node);
void update_rsp_offset(int size) {
env->rsp_offset += size;
if (*env->rsp_offset_max < env->rsp_offset) {
*env->rsp_offset_max = env->rsp_offset;
}
}
Node *analyzing(Node *node) {
int j;
Env *prev_env = env;
if (node->ty == ND_BLOCK || node->ty == ND_FDEF) {
env = node->env;
LocalVariable *local_variable = NULL;
// generate all size of nodes
// preview all variables and setup offsets.
if (prev_env) {
// This is to update rsp_offset based on (it's based system).
// Previously, this is called by new_env() but no longer supported.
env->rsp_offset += prev_env->rsp_offset;
}
for (j = 0; j < env->idents->keys->len; j++) {
local_variable = (LocalVariable *)env->idents->vals->data[j];
if (local_variable->lvar_offset != 0) {
// For Example: STRUCT Passed Argumenets (use rsp+24 or so on)
continue;
}
int size = cnt_size(local_variable->type);
// should aligned as x86_64
if (size % 8 != 0) {
size += (8 - size % 8);
}
update_rsp_offset(size);
local_variable->lvar_offset = env->rsp_offset;
}
}
if (node->lhs) {
node->lhs = analyzing(node->lhs);
}
if (node->rhs) {
node->rhs = analyzing(node->rhs);
}
if (node->is_omiited) {
node->is_omiited = analyzing(node->is_omiited);
}
for (j = 0; j < 3; j++) {
if (node->conds[j]) {
node->conds[j] = analyzing(node->conds[j]);
}
}
if (node->argc > 0) {
for (j = 0; j < node->argc; j++) {
if (node->args[j]) {
node->args[j] = analyzing(node->args[j]);
}
}
}
if (node->code) {
for (j = 0; j < node->code->len && node->code->data[j]; j++) {
node->code->data[j] = (Token *)analyzing((Node *)node->code->data[j]);
}
}
switch (node->ty) {
case ND_RETURN:
if (env->ret == NULL) {
error("try to Return to NULL: Unexpected behaviour\n");
}
// treat TY_AUTO
if (env->ret->ty == TY_AUTO) {
copy_type(node->lhs->type, env->ret);
}
if (node->lhs) {
node->type = node->lhs->type;
} // if return to VOID, there is no lhs
break;
case ND_SIZEOF:
if (node->conds[0]) {
// evaluate node->conds[0] and return its type
node = new_long_num_node(cnt_size(node->conds[0]->type));
} else {
// ND_SIZEOF should generate its type information.
// This is because: on parse, no (structured) type size are
// avaliable
node = new_long_num_node(cnt_size(node->sizeof_type));
}
break;
case ND_ADD:
case ND_SUB:
if (node->lhs->type->ty == TY_PTR || node->lhs->type->ty == TY_ARRAY) {
node->rhs = new_node(ND_MULTIPLY_IMMUTABLE_VALUE, node->rhs, NULL);
node->rhs->type = node->rhs->lhs->type;
node->rhs->num_val = cnt_size(node->lhs->type->ptrof);
node->type = node->lhs->type;
} else if (node->rhs->type->ty == TY_PTR ||
node->rhs->type->ty == TY_ARRAY) {
node->lhs = new_node(ND_MULTIPLY_IMMUTABLE_VALUE, node->lhs, NULL);
node->lhs->type = node->rhs->lhs->type;
node->lhs->num_val = cnt_size(node->rhs->type->ptrof);
node->type = node->rhs->type;
} else {
node = implicit_althemic_type_conversion(node);
node->type = node->lhs->type;
break;
}
if (node->rhs->type->ty == TY_PTR || node->rhs->type->ty == TY_ARRAY) {
node->type = node->rhs->type;
// TY_PTR no matter when node->lhs->node->lhs is INT or PTR
} else if (node->lhs->type->ty == TY_PTR ||
node->lhs->type->ty == TY_ARRAY) {
node->type = node->lhs->type;
// TY_PTR no matter when node->lhs->node->lhs is INT or PTR
}
break;
case ND_MUL:
node = implicit_althemic_type_conversion(node);
node->type = node->lhs->type;
break;
case ND_ISEQ:
case ND_ISNOTEQ:
case ND_ISLESSEQ:
case ND_ISMOREEQ:
case ND_LESS:
case ND_GREATER:
node->type = find_typed_db("int", typedb);
node = implicit_althemic_type_conversion(node);
break;
case ND_FPLUSPLUS:
case ND_FSUBSUB:
// Moved to analyzing process.
if (node->lhs->type->ty == TY_PTR || node->lhs->type->ty == TY_ARRAY) {
node->num_val = type2size(node->lhs->type->ptrof);
}
node->type = node->lhs->type;
break;
case ND_ADDRESS:
node->type = new_type();
node->type->ty = TY_PTR;
node->type->ptrof = node->lhs->type;
break;
case ND_DEREF:
node->type = node->lhs->type->ptrof;
if (!node->type) {
error("Error: Dereference on NOT pointered.");
}
break;
case ND_ASSIGN:
if (node->lhs->type->is_const) {
error("Error: Assign to constant value.\n");
}
if (node->lhs->type->ty != node->rhs->type->ty) {
node->rhs = new_node(ND_CAST, node->rhs, NULL);
node->rhs->type = node->lhs->type;
}
node->type = node->lhs->type;
break;
case ND_DOT:
if (node->lhs->type->ty != TY_STRUCT) {
error("Error: dot operator to NOT struct\n");
}
node->type = (Type *)map_get(node->lhs->type->structure, node->name);
if (!node->type) {
error("Error: structure not found: %s\n", node->name);
}
// Member Accesss Control p.231
if ((lang & 1) && (node->type->memaccess == HIDED)) {
error("Error: access to private item: %s\n", node->name);
}
if ((lang & 1) && ((node->type->memaccess == PRIVATE) ||
(node->type->memaccess == PROTECTED))) {
if (!env->current_class || !env->current_class->var_name ||
(strcmp(env->current_class->var_name, "this"))) {
// TODO
//(strcmp(env->current_class->type_name,
// node->type->type_name)))) {
error("Error: access to private item: %s\n", node->name);
}
}
break;
case ND_NEG:
node->type = node->lhs->type;
if (node->type->ty == TY_DOUBLE) {
neg_double_generate = 1;
}
if (node->type->ty == TY_FLOAT) {
neg_float_generate = 1;
}
break;
case ND_MULTIPLY_IMMUTABLE_VALUE:
node->type = node->lhs->type;
break;
case ND_COMMA:
node->type = node->rhs->type;
break;
case ND_FUNC:
if (node->funcdef) {
// there are new typedefs. {
node->type = node->funcdef->type->ret;
} else {
node->type = find_typed_db("int", typedb);
}
break;
case ND_VASTART:
node->num_val = env->current_func->argc;
node->type = node->lhs->type;
break;
case '?':
/* arithmetic, arithmetic -> arithmetic
* structure, structure -> structure
* void, void -> void
* pointer, pointer/NULL -> pointer
* (differently qualified) compatible types
* null or not -> other
* coid -> pointer to qualified void
*/
/* TODO: Support NULL pointer & void */
node->type = node->lhs->type;
if (node->type->ty != TY_STRUCT || node->type->ty != TY_PTR ||
node->type->ty != TY_ARRAY || node->type->ty != TY_VOID) {
node = implicit_althemic_type_conversion(node);
}
break;
default:
if (node->type == NULL && node->lhs && node->lhs->type) {
node->type = node->lhs->type;
}
break;
}
env = prev_env;
return node;
}
Node *implicit_althemic_type_conversion(Node *node) {
// See Section 6.3.1.8
if (node->lhs->type->ty == node->rhs->type->ty) {
return node;
}
if (node->lhs->type->ty == TY_DOUBLE) {
node->rhs = new_node(ND_CAST, node->rhs, NULL);
node->rhs->type = node->lhs->type;
return node;
}
if (node->rhs->type->ty == TY_DOUBLE) {
node->lhs = new_node(ND_CAST, node->lhs, NULL);
node->lhs->type = node->rhs->type;
return node;
}
if (node->lhs->type->ty == TY_FLOAT) {
node->rhs = new_node(ND_CAST, node->rhs, NULL);
node->rhs->type = node->lhs->type;
return node;
}
if (node->rhs->type->ty == TY_FLOAT) {
node->lhs = new_node(ND_CAST, node->lhs, NULL);
node->lhs->type = node->rhs->type;
return node;
}
if (type2size(node->lhs->type) < type2size(node->rhs->type)) {
node->lhs = new_node(ND_CAST, node->lhs, NULL);
node->lhs->type = node->rhs->type;
return node;
}
if (type2size(node->lhs->type) > type2size(node->rhs->type)) {
node->rhs = new_node(ND_CAST, node->rhs, NULL);
node->rhs->type = node->lhs->type;
return node;
}
return node;
}