-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmm-type.c
278 lines (252 loc) · 7.06 KB
/
cmm-type.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
#include "cmm-type.h"
#include <stdlib.h> /* malloc */
#include <string.h> /* strlen */
#include <assert.h> /* assert */
const char *class_s[] = {
"int",
"float",
"char",
"array",
"struct",
"function",
"field",
"param",
"type"
};
Type _BASIC_INT = {
CMM_INT,
"int",
{ NULL },
{ NULL },
{ 0 },
{ 4 },
0
};
Type _BASIC_FLOAT = {
CMM_FLOAT,
"float",
{ NULL },
{ NULL },
{ 0 },
{ 4 },
0
};
Type _BASIC_CHAR = {
CMM_CHAR,
"char",
{ NULL },
{ NULL },
{ 0 },
{ 4 },
0
};
Type *BASIC_INT = &_BASIC_INT;
Type *BASIC_FLOAT = &_BASIC_FLOAT;
Type *BASIC_CHAR = &_BASIC_CHAR;
// Constructor
Type *new_type(CmmType class, const char *name, Type *type, Type *link)
{
Type *this = NEW(Type);
this->class = class;
this->name = name;
this->base = type; // Equivalent to this->meta, this->ret
this->link = link;
this->ref = 0;
this->lineno = -1;
return this;
}
bool typecmp(const Type *x, const Type *y)
{
// TODO allow void ?
if (x == y) { // Allow void
return true;
}
else if (x == NULL || y == NULL) { // void vs others
return true; // This is error, but NULL is used for undefined variables, return true allow not reporting error recursively.
}
LOG("Now compare %s and %s", x->name, y->name);
/* Totally different! */
if (x->class != y->class) {
LOG("%s and %s are totally different!", class_s[x->class], class_s[y->class]);
return false;
}
// The function only use name to differ
if (x->class == CMM_FUNC) {
LOG("Then we fall into func comparision");
if (strcmp(x->name, y->name) != 0) {
return false;
}
else {
const Type *param_x = x->param;
const Type *param_y = y->param;
while (param_x != NULL && param_y != NULL && typecmp(param_x->base, param_y->base)) {
param_x = param_x->link;
param_y = param_y->link;
}
// Compare to the end means all the same
if (param_x != NULL || param_y != NULL) {
return false;
}
else {
// Compare return type
// TODO: we don't allow void type now
assert(x->ret != NULL && y->ret != NULL);
return typecmp(x->ret, y->ret);
}
}
}
if (x->class == CMM_ARRAY) {
LOG("Then we fall into array comparision");
// The array differs from its dimension and basic type
int loopno = 1;
// End loop when reach the basic type
while (x->class == CMM_ARRAY && y->class == CMM_ARRAY) {
LOG("loop %d", loopno);
x = x->base;
y = y->base;
loopno++;
}
if (x->class == CMM_ARRAY || y->class == CMM_ARRAY) {
// The dimension differs
return false;
}
else {
// Compare the base type
return typecmp(x, y);
}
}
if (x->class == CMM_STRUCT) {
LOG("Then we fall into struct comparision");
#ifdef STRUCTURE
// Comparison based on structure similarity
// But don't compare their name
const Type *field_x = x->field;
const Type *field_y = y->field;
// Compare the field one by one
while (field_x != NULL && field_y != NULL && typecmp(field_x->base, field_y->base)) {
field_x = field_x->link;
field_y = field_y->link;
}
if (field_x == NULL && field_y == NULL) {
return true; /* The fields' numbers are the same, and the type is the same respectively */
}
else {
return false;
}
#else // just the name
return strcmp(STRUCT(x)->tag, STRUCT(y)->tag) == 0;
#endif // ifdef STRUCTURE
}
assert(x->class == CMM_INT || x->class == CMM_FLOAT);
return 1;
}
void _print_type(const Type *type, char *end)
{
const Type *base, *link;
if (type == NULL) {
printf("void%s", end);
return;
}
switch (type->class) {
case CMM_INT:
case CMM_FLOAT:
printf("%s", class_s[type->class]);
break;
case CMM_ARRAY:
// Find the base
base = type->base;
while (base->class == CMM_ARRAY) {
base = base->base;
}
// Print the base recursively
_print_type(base, "");
// Print dimension from left to right
while (type->class == CMM_ARRAY) {
printf("[%d]", type->size);
type = type->base;
}
break;
case CMM_STRUCT:
printf("%s %s{", class_s[type->class], type->name);
// Print field list
link = type->field;
while (link != NULL) {
_print_type(link->base, " ");
printf("%s;", link->name);
link = link->link;
}
printf("}");
break;
case CMM_FUNC:
// Print return type
_print_type(type->ret, " ");
// Print function name
printf("%s(", type->name);
// Print param list
link = type->param;
while (link != NULL && link->link != NULL) {
_print_type(link->base, ", ");
link = link->link;
}
// Tail param handling
if (link != NULL) {
_print_type(link->base, "");
}
printf(")");
break;
case CMM_TYPE:
_print_type(type->base, end);
return;
default:
assert(0);
}
printf("%s", end);
}
void print_type(const Type *type)
{
_print_type(type, "\n");
}
// Test
#ifdef TEST_TYPE
int main()
#else // ifndef TEST_TYPE
int test_cmm_type()
#endif // endif TEST_TYPE
{
Type *top = BASIC_INT;
Type *current = top;
Type *types[100];
Type *struc;
Type *foo;
for (int i = 0; i < 100; i++) {
int rd = rand();
int idx = (unsigned)rd % 3;
switch (idx) {
case 0:
current = new_type(CMM_ARRAY, NULL, current, NULL);
break;
case 1:
struc = new_type(CMM_STRUCT, "hello", NULL, NULL);
struc->field = new_type(CMM_FIELD, "a", top, NULL);
struc->field = new_type(CMM_FIELD, "b", top, struc->field);
current = struc;
break;
case 2:
foo = new_type(CMM_FUNC, "foo", current, NULL);
foo->param = new_type(CMM_PARAM, "boo", BASIC_INT, NULL);
foo->param = new_type(CMM_PARAM, "coo", BASIC_FLOAT, foo->param);
current = foo;
break;
default:
assert(0);
}
types[i] = current;
}
for (int k = 0; k < 100; k++) {
assert(typecmp(types[k], types[k]));
}
for (int k = 0; k < 100; k++) {
print_type(types[k]);
}
return 0;
}