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
|
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "semantic-analysis.h"
#include "common.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERROR(msg) ("Error found in line %zu\n" msg "\n")
static CcmmcValueConst eval_const_expr(CcmmcAst *expr) {
if (expr->type_node == CCMMC_AST_NODE_CONST_VALUE)
return expr->value_const;
assert(expr->type_node == CCMMC_AST_NODE_EXPR);
assert(expr->value_expr.kind == CCMMC_KIND_EXPR_BINARY_OP);
CcmmcValueConst left = eval_const_expr(expr->child);
CcmmcValueConst right = eval_const_expr(expr->child->right_sibling);
if (left.kind == CCMMC_KIND_CONST_ERROR ||
right.kind == CCMMC_KIND_CONST_ERROR)
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_ERROR };
switch (expr->value_expr.op_binary) {
case CCMMC_KIND_OP_BINARY_ADD:
if (left.kind == CCMMC_KIND_CONST_INT) {
if (right.kind == CCMMC_KIND_CONST_INT) {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_INT,
.const_int = left.const_int + right.const_int };
} else {
float left_value = left.const_int;
float right_value = right.const_float;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value + right_value };
}
} else {
if (right.kind == CCMMC_KIND_CONST_INT) {
float left_value = left.const_float;
float right_value = right.const_int;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value + right_value };
} else {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left.const_float + right.const_float };
}
}
case CCMMC_KIND_OP_BINARY_SUB:
if (left.kind == CCMMC_KIND_CONST_INT) {
if (right.kind == CCMMC_KIND_CONST_INT) {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_INT,
.const_int = left.const_int - right.const_int };
} else {
float left_value = left.const_int;
float right_value = right.const_float;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value - right_value };
}
} else {
if (right.kind == CCMMC_KIND_CONST_INT) {
float left_value = left.const_float;
float right_value = right.const_int;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value - right_value };
} else {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left.const_float - right.const_float };
}
}
case CCMMC_KIND_OP_BINARY_MUL:
if (left.kind == CCMMC_KIND_CONST_INT) {
if (right.kind == CCMMC_KIND_CONST_INT) {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_INT,
.const_int = left.const_int * right.const_int };
} else {
float left_value = left.const_int;
float right_value = right.const_float;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value * right_value };
}
} else {
if (right.kind == CCMMC_KIND_CONST_INT) {
float left_value = left.const_float;
float right_value = right.const_int;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value * right_value };
} else {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left.const_float * right.const_float };
}
}
case CCMMC_KIND_OP_BINARY_DIV:
if (left.kind == CCMMC_KIND_CONST_INT) {
if (right.kind == CCMMC_KIND_CONST_INT) {
if (right.const_int == 0) {
fprintf(stderr, ERROR("Integer division by zero."),
expr->line_number);
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_ERROR };
}
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_INT,
.const_int = left.const_int / right.const_int };
} else {
float left_value = left.const_int;
float right_value = right.const_float;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value / right_value };
}
} else {
if (right.kind == CCMMC_KIND_CONST_INT) {
float left_value = left.const_float;
float right_value = right.const_int;
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left_value / right_value };
} else {
return (CcmmcValueConst){ .kind = CCMMC_KIND_CONST_FLOAT,
.const_float = left.const_float / right.const_float };
}
}
case CCMMC_KIND_OP_BINARY_EQ:
case CCMMC_KIND_OP_BINARY_GE:
case CCMMC_KIND_OP_BINARY_LE:
case CCMMC_KIND_OP_BINARY_NE:
case CCMMC_KIND_OP_BINARY_GT:
case CCMMC_KIND_OP_BINARY_LT:
case CCMMC_KIND_OP_BINARY_AND:
case CCMMC_KIND_OP_BINARY_OR:
default:
assert(false);
}
}
static size_t *get_array_size(CcmmcAst *id_array, size_t *array_dimension)
{
CcmmcAst *dim;
size_t dim_count = 0;
for (dim = id_array->child; dim != NULL; dim = dim->right_sibling, dim_count++);
assert(dim_count != 0);
size_t dim_index = 0;
size_t *array_size = malloc(sizeof(size_t) * dim_count);
ERR_FATAL_CHECK(array_size, malloc);
for (dim = id_array->child; dim != NULL; dim = dim->right_sibling, dim_index++) {
CcmmcValueConst value = eval_const_expr(dim);
if (value.kind == CCMMC_KIND_CONST_ERROR) {
free(array_size);
return NULL;
}
if (value.kind != CCMMC_KIND_CONST_INT) {
fprintf(stderr, ERROR("Array subscript is not an integer."),
dim->line_number);
free(array_size);
return NULL;
}
if (value.const_int <= 0) {
fprintf(stderr, ERROR("Array size must be positive."),
dim->line_number);
free(array_size);
return NULL;
}
array_size[dim_index] = value.const_int;
}
*array_dimension = dim_count;
return array_size;
}
static size_t *get_array_of_array_size(CcmmcAst *id_array, size_t *array_dimension,
size_t base_array_dimension, size_t *base_array_size)
{
size_t this_array_dimension;
size_t *this_array_size = get_array_size(id_array, &this_array_dimension);
if (this_array_size == NULL) {
free (this_array_size);
return NULL;
}
size_t dim_count = this_array_dimension + base_array_dimension;
size_t *array_size = realloc(this_array_size, sizeof(size_t) * dim_count);
ERR_FATAL_CHECK(array_size, realloc);
memcpy(array_size + this_array_dimension,
base_array_size, sizeof(size_t) * base_array_dimension);
*array_dimension = dim_count;
return array_size;
}
static bool process_typedef(CcmmcAst *type_decl, CcmmcSymbolTable *table)
{
bool any_error = false;
// The leftmost child is an existing type
assert(type_decl->child != NULL);
assert(type_decl->child->type_node == CCMMC_AST_NODE_ID);
assert(type_decl->child->value_id.kind == CCMMC_KIND_ID_NORMAL);
const char *source_str = type_decl->child->value_id.name;
CcmmcSymbol *source_sym = ccmmc_symbol_table_retrive(table, source_str);
// We don't support an existing array typedef
assert(ccmmc_symbol_is_scalar(source_sym));
if (source_sym == NULL) {
fprintf(stderr, ERROR("ID `%s' undeclared."),
type_decl->line_number, source_str);
return true;
}
if (source_sym->kind != CCMMC_SYMBOL_KIND_TYPE) {
fprintf(stderr, ERROR("ID `%s' is not a type."),
type_decl->line_number, source_str);
return true;
}
// Other children are newly declared types
for (CcmmcAst *id = type_decl->child->right_sibling; id != NULL;
id = id->right_sibling) {
assert(id->type_node == CCMMC_AST_NODE_ID);
const char *target_str = id->value_id.name;
if (ccmmc_symbol_scope_exist(table->current, target_str)) {
any_error = true;
fprintf (stderr, ERROR("ID `%s' redeclared."),
id->line_number, target_str);
continue;
}
switch (id->value_id.kind) {
case CCMMC_KIND_ID_NORMAL:
ccmmc_symbol_table_insert(table,
target_str, CCMMC_SYMBOL_KIND_TYPE, source_sym->type);
break;
case CCMMC_KIND_ID_ARRAY: {
size_t array_dimension;
size_t *array_size = get_array_size(id, &array_dimension);
if (array_size == NULL)
any_error = true;
CcmmcSymbolType type = {
.type_base = source_sym->type.type_base,
.array_dimension = array_dimension,
.array_size = array_size };
ccmmc_symbol_table_insert(table,
target_str, CCMMC_SYMBOL_KIND_TYPE, type);
} break;
case CCMMC_KIND_ID_WITH_INIT:
default:
assert(false);
}
}
return any_error;
}
static bool process_relop_expr(CcmmcAst *expr, CcmmcSymbolTable *table)
{
bool any_error = false;
return any_error;
}
static bool process_variable(CcmmcAst *var_decl, CcmmcSymbolTable *table)
{
bool any_error = false;
// The leftmost child is the type of the variable declaration list
assert(var_decl->child != NULL);
assert(var_decl->child->type_node == CCMMC_AST_NODE_ID);
assert(var_decl->child->value_id.kind == CCMMC_KIND_ID_NORMAL);
const char *type_str = var_decl->child->value_id.name;
CcmmcSymbol *type_sym = ccmmc_symbol_table_retrive(table, type_str);
if (type_sym == NULL) {
fprintf(stderr, ERROR("ID `%s' undeclared."),
var_decl->line_number, type_str);
return true;
}
if (type_sym->kind != CCMMC_SYMBOL_KIND_TYPE) {
fprintf(stderr, ERROR("ID `%s' is not a type."),
var_decl->line_number, type_str);
return true;
}
// Other children are newly declared variables
for (CcmmcAst *init_id = var_decl->child->right_sibling; init_id != NULL;
init_id = init_id->right_sibling) {
assert(init_id->type_node = CCMMC_AST_NODE_ID);
const char *var_str = init_id->value_id.name;
if (ccmmc_symbol_scope_exist(table->current, var_str)) {
any_error = true;
fprintf (stderr, ERROR("ID `%s' redeclared."),
init_id->line_number, var_str);
continue;
}
switch (init_id->value_id.kind) {
case CCMMC_KIND_ID_NORMAL:
ccmmc_symbol_table_insert(table,
var_str, CCMMC_SYMBOL_KIND_VARIABLE, type_sym->type);
break;
case CCMMC_KIND_ID_ARRAY: {
size_t array_dimension;
size_t *array_size;
if (ccmmc_symbol_is_array(type_sym))
array_size = get_array_of_array_size(
init_id, &array_dimension,
type_sym->type.array_dimension,
type_sym->type.array_size);
else
array_size = get_array_size (init_id, &array_dimension);
if (array_size == NULL) {
any_error = true;
continue;
}
CcmmcSymbolType type = {
.type_base = type_sym->type.type_base,
.array_dimension = array_dimension,
.array_size = array_size };
ccmmc_symbol_table_insert(table,
var_str, CCMMC_SYMBOL_KIND_VARIABLE, type);
} break;
case CCMMC_KIND_ID_WITH_INIT: {
assert(ccmmc_symbol_is_scalar(type_sym));
if (process_relop_expr(init_id, table)) {
any_error = true;
continue;
}
ccmmc_symbol_table_insert(table,
var_str, CCMMC_SYMBOL_KIND_VARIABLE, type_sym->type);
} break;
default:
assert(false);
}
}
return any_error;
}
static bool process_function(CcmmcAst *function_decl, CcmmcSymbolTable *table)
{
bool any_error = false;
return any_error;
}
static bool process_program(CcmmcAst *program, CcmmcSymbolTable *table)
{
bool any_error = false;
// Process the list of global declarations
for (CcmmcAst *global_decl = program->child; global_decl != NULL;
global_decl = global_decl->right_sibling) {
assert(global_decl->type_node == CCMMC_AST_NODE_DECL);
switch (global_decl->value_decl.kind) {
case CCMMC_KIND_DECL_TYPE:
any_error = process_typedef(global_decl, table) || any_error;
break;
case CCMMC_KIND_DECL_VARIABLE:
any_error = process_variable(global_decl, table) || any_error;
break;
case CCMMC_KIND_DECL_FUNCTION:
any_error = process_function(global_decl, table) || any_error;
break;
case CCMMC_KIND_DECL_FUNCTION_PARAMETER:
default:
assert(false);
}
}
return any_error;
}
bool ccmmc_semantic_check(CcmmcAst *root, CcmmcSymbolTable *table)
{
bool any_error = false;
// The symbol table must be empty
assert(table->all == NULL && table->all_last == NULL);
assert(table->current == NULL);
// Push the global scope
ccmmc_symbol_table_open_scope(table);
// Insert builtin types
ccmmc_symbol_table_insert(table, "int", CCMMC_SYMBOL_KIND_TYPE,
(CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_INT });
ccmmc_symbol_table_insert(table, "float", CCMMC_SYMBOL_KIND_TYPE,
(CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_FLOAT });
ccmmc_symbol_table_insert(table, "void", CCMMC_SYMBOL_KIND_TYPE,
(CcmmcSymbolType){ .type_base = CCMMC_AST_VALUE_VOID });
// Start processing from the program node
any_error = process_program(root, table) || any_error;
return !any_error;
}
// vim: set sw=4 ts=4 sts=4 et:
|