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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "sema/sema.h"
static bool token_is(struct Token token, const char* text)
{
size_t length = strlen(text);
return token.length == length && memcmp(token.start, text, length) == 0;
}
static bool names_equal(struct Token a, struct Token b)
{
return a.length == b.length && memcmp(a.start, b.start, a.length) == 0;
}
static struct ProcDecl* find_proc(struct Program* program, struct Token name)
{
for (size_t i = 0; i < program->proc_count; i += 1)
if (names_equal(program->procs[i].name, name))
return &program->procs[i];
return NULL;
}
static bool check_duplicate_names(struct Source source, struct Program* program)
{
size_t count = program->const_count + program->data_count
+ program->enum_count + program->struct_count + program->proc_count;
if (count == 0)
return true;
struct Token* names = malloc(count * sizeof(struct Token));
size_t n = 0;
for (size_t i = 0; i < program->const_count; i += 1)
{
names[n] = program->consts[i].name;
n += 1;
}
for (size_t i = 0; i < program->data_count; i += 1)
{
names[n] = program->data_decls[i].name;
n += 1;
}
for (size_t i = 0; i < program->enum_count; i += 1)
{
names[n] = program->enums[i].name;
n += 1;
}
for (size_t i = 0; i < program->struct_count; i += 1)
{
names[n] = program->structs[i].name;
n += 1;
}
for (size_t i = 0; i < program->proc_count; i += 1)
{
names[n] = program->procs[i].name;
n += 1;
}
bool ok = true;
for (size_t i = 0; i < count; i += 1)
for (size_t j = 0; j < i; j += 1)
if (names_equal(names[i], names[j]))
{
char message[128];
snprintf(message, sizeof(message), "'%.*s' is already defined",
(int)names[i].length, names[i].start);
report_error(source, names[i], message);
ok = false;
}
free(names);
return ok;
}
static bool check_entry_point(struct Source source, struct Program* program)
{
if (!program->config.has_entry)
return true;
if (find_proc(program, program->config.entry) != NULL)
return true;
struct Token entry = program->config.entry;
char message[128];
snprintf(message, sizeof(message), "entry point '%.*s' is not defined",
(int)entry.length, entry.start);
report_error(source, entry, message);
return false;
}
static bool is_program_const(struct Program* program, struct Token name)
{
for (size_t i = 0; i < program->const_count; i += 1)
if (names_equal(program->consts[i].name, name))
return true;
return false;
}
static struct Token first_token(struct Expr* expr)
{
switch (expr->kind)
{
case EXPR_BINARY: return first_token(expr->binary.left);
case EXPR_MEMBER: return first_token(expr->member.object);
case EXPR_DEREF: return first_token(expr->deref.address);
default: return expr->primary.token;
}
}
static bool check_const_value(struct Source source, struct Program* program, struct Expr* expr)
{
if (expr->kind == EXPR_BINARY)
{
bool left = check_const_value(source, program, expr->binary.left);
bool right = check_const_value(source, program, expr->binary.right);
return left && right;
}
if (expr->kind == EXPR_PRIMARY)
{
struct Token token = expr->primary.token;
if (token.type == TOKEN_INTEGER || token.type == TOKEN_CHAR)
return true;
if (token.type == TOKEN_IDENTIFIER && is_program_const(program, token))
return true;
char message[128];
snprintf(message, sizeof(message), "'%.*s' is not a constant",
(int)token.length, token.start);
report_error(source, token, message);
return false;
}
report_error(source, first_token(expr), "constant must be an integer expression");
return false;
}
static bool check_const_values(struct Source source, struct Program* program)
{
bool ok = true;
for (size_t i = 0; i < program->const_count; i += 1)
if (!check_const_value(source, program, program->consts[i].value))
ok = false;
return ok;
}
struct RefCheck
{
struct Source source;
struct Program* program;
struct ProcDecl* proc;
bool ok;
};
static void ref_error(struct RefCheck* check, struct Token token, const char* format, ...)
{
char message[256];
va_list args;
va_start(args, format);
vsnprintf(message, sizeof(message), format, args);
va_end(args);
report_error(check->source, token, message);
check->ok = false;
}
static bool is_arch_register(struct Token token)
{
static const char* names[] = {
"rax", "eax", "ax", "al", "ah",
"rbx", "ebx", "bx", "bl", "bh",
"rcx", "ecx", "cx", "cl", "ch",
"rdx", "edx", "dx", "dl", "dh",
"rsi", "esi", "si", "sil",
"rdi", "edi", "di", "dil",
"rbp", "ebp", "bp", "bpl",
"rsp", "esp", "sp", "spl",
"r8", "r8d", "r8w", "r8b",
"r9", "r9d", "r9w", "r9b",
"r10", "r10d", "r10w", "r10b",
"r11", "r11d", "r11w", "r11b",
"r12", "r12d", "r12w", "r12b",
"r13", "r13d", "r13w", "r13b",
"r14", "r14d", "r14w", "r14b",
"r15", "r15d", "r15w", "r15b",
"rip",
};
for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); i += 1)
if (token_is(token, names[i]))
return true;
return false;
}
static bool is_logical_register(struct Token token)
{
if (token.length < 2 || token.start[0] != 'r')
return false;
uint32_t index = 0;
for (size_t i = 1; i < token.length; i += 1)
{
char digit = token.start[i];
if (digit < '0' || digit > '9')
return false;
index = index * 10 + (uint32_t)(digit - '0');
}
return index >= 1 && index <= 14;
}
static bool is_register(struct RefCheck* check, struct Token token)
{
if (is_arch_register(token))
return true;
return check->program->config.logical_registers && is_logical_register(token);
}
static bool is_param(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->proc->param_count; i += 1)
if (names_equal(check->proc->params[i].name, token))
return true;
return false;
}
static bool is_const(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->program->const_count; i += 1)
if (names_equal(check->program->consts[i].name, token))
return true;
return false;
}
static bool is_data(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->program->data_count; i += 1)
if (names_equal(check->program->data_decls[i].name, token))
return true;
return false;
}
static struct EnumDecl* find_enum(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->program->enum_count; i += 1)
if (names_equal(check->program->enums[i].name, token))
return &check->program->enums[i];
return NULL;
}
static struct StructDecl* find_struct(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->program->struct_count; i += 1)
if (names_equal(check->program->structs[i].name, token))
return &check->program->structs[i];
return NULL;
}
static bool is_stack_buffer(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->proc->body_count; i += 1)
{
struct Statement* statement = &check->proc->body[i];
if (statement->kind == STATEMENT_STACK && names_equal(statement->stack.name, token))
return true;
}
return false;
}
static bool is_label(struct RefCheck* check, struct Token token)
{
for (size_t i = 0; i < check->proc->body_count; i += 1)
{
struct Statement* statement = &check->proc->body[i];
if (statement->kind == STATEMENT_LABEL && names_equal(statement->label.name, token))
return true;
}
return false;
}
static void check_value_name(struct RefCheck* check, struct Token name)
{
if (is_register(check, name) || is_param(check, name) || is_const(check, name)
|| is_data(check, name) || is_stack_buffer(check, name))
return;
ref_error(check, name, "undefined name '%.*s'", (int)name.length, name.start);
}
static void check_expr(struct RefCheck* check, struct Expr* expr)
{
switch (expr->kind)
{
case EXPR_PRIMARY:
if (expr->primary.token.type == TOKEN_IDENTIFIER)
check_value_name(check, expr->primary.token);
break;
case EXPR_BINARY:
check_expr(check, expr->binary.left);
check_expr(check, expr->binary.right);
break;
case EXPR_DEREF:
{
struct Expr* address = expr->deref.address;
if (address->kind == EXPR_PRIMARY
&& (is_register(check, address->primary.token) || is_param(check, address->primary.token)))
break;
if (address->kind == EXPR_PRIMARY)
ref_error(check, address->primary.token, "dereference address must be a register");
else
check_expr(check, address);
break;
}
case EXPR_MEMBER:
{
struct Expr* object = expr->member.object;
struct Token member = expr->member.member;
if (member.type == TOKEN_INTEGER)
{
if (object->kind != EXPR_PRIMARY || !is_register(check, object->primary.token))
ref_error(check, member, "size suffix requires a register");
break;
}
if (object->kind == EXPR_PRIMARY)
{
struct EnumDecl* enumeration = find_enum(check, object->primary.token);
if (enumeration != NULL)
{
bool found = false;
for (size_t i = 0; i < enumeration->member_count; i += 1)
if (names_equal(enumeration->members[i], member))
found = true;
if (!found)
ref_error(check, member, "enum '%.*s' has no member '%.*s'",
(int)object->primary.token.length, object->primary.token.start,
(int)member.length, member.start);
break;
}
struct StructDecl* layout = find_struct(check, object->primary.token);
if (layout != NULL)
{
bool found = token_is(member, "size");
for (size_t i = 0; i < layout->field_count; i += 1)
if (names_equal(layout->fields[i].name, member))
found = true;
if (!found)
ref_error(check, member, "struct '%.*s' has no field '%.*s'",
(int)object->primary.token.length, object->primary.token.start,
(int)member.length, member.start);
break;
}
if (is_data(check, object->primary.token))
{
if (!token_is(member, "len"))
ref_error(check, member, "unknown member '%.*s'", (int)member.length, member.start);
break;
}
}
check_expr(check, object);
break;
}
}
}
static void check_target(struct RefCheck* check, struct Token target)
{
if (is_register(check, target) || is_param(check, target))
return;
ref_error(check, target, "cannot assign to '%.*s': not a register", (int)target.length, target.start);
}
static void check_statement(struct RefCheck* check, struct Statement* statement)
{
switch (statement->kind)
{
case STATEMENT_ASSIGN:
check_target(check, statement->assign.target);
check_expr(check, statement->assign.value);
break;
case STATEMENT_GOTO:
if (!is_label(check, statement->jump.label))
ref_error(check, statement->jump.label, "undefined label '%.*s'",
(int)statement->jump.label.length, statement->jump.label.start);
break;
case STATEMENT_IF:
check_expr(check, statement->branch.left);
check_expr(check, statement->branch.right);
check_statement(check, statement->branch.body);
break;
case STATEMENT_CALL:
{
struct CallStatement* call = &statement->call;
struct ProcDecl* callee = find_proc(check->program, call->name);
if (callee == NULL)
ref_error(check, call->name, "undefined procedure '%.*s'",
(int)call->name.length, call->name.start);
else if (callee->param_count != call->arg_count)
ref_error(check, call->name, "'%.*s' expects %zu argument(s), got %zu",
(int)call->name.length, call->name.start, callee->param_count, call->arg_count);
for (size_t i = 0; i < call->arg_count; i += 1)
check_expr(check, call->args[i]);
break;
}
case STATEMENT_LABEL:
case STATEMENT_SYSCALL:
case STATEMENT_STACK:
break;
}
}
static bool check_references(struct Source source, struct Program* program)
{
bool ok = true;
for (size_t i = 0; i < program->proc_count; i += 1)
{
struct RefCheck check = { source, program, &program->procs[i], true };
for (size_t j = 0; j < program->procs[i].body_count; j += 1)
check_statement(&check, &program->procs[i].body[j]);
if (!check.ok)
ok = false;
}
return ok;
}
bool analyze_program(struct Source source, struct Program* program)
{
bool ok = true;
if (!check_duplicate_names(source, program))
ok = false;
if (!check_entry_point(source, program))
ok = false;
if (!check_const_values(source, program))
ok = false;
if (!check_references(source, program))
ok = false;
return ok;
}
|