Repository navigation
Expand file tree
/
Copy pathCompiler.c
More file actions
399 lines (349 loc) · 7.71 KB
/
Copy pathCompiler.c
File metadata and controls
399 lines (349 loc) · 7.71 KB
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
/*
*********************************************
* 314 Principles of Programming Languages *
* Fall 2015 *
* Author: Ulrich Kremer *
* Student Version *
*********************************************
*/
/* -------------------------------------------------
CFG for tinyL LANGUAGE
PROGRAM ::= STMTLIST .
STMTLIST ::= STMT MORESTMTS
MORESTMTS ::= ; STMTLIST | epsilon
STMT ::= ASSIGN | READ | PRINT
ASSIGN ::= VARIABLE = EXPR
PRINT ::= ! VARIABLE
EXPR ::= + EXPR EXPR |
- EXPR EXPR |
* EXPR EXPR |
/ EXPR EXPR |
VARIABLE |
DIGIT
VARIABLE ::= a | b | c | d | e | f | g | h | i | j | k | l | m | n
DIGIT ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
NOTE: tokens are exactly a single character long
Example expressions:
+12.
+1b.
+*34-78.
-*+1+2a58.
Example programs;
a=2;b=5;c=+3*ab;d=+c1;!d.
b=-*+1/2a58;!b.
---------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "Instr.h"
#include "InstrUtils.h"
#include "Utils.h"
#define MAX_BUFFER_SIZE 500
#define EMPTY_FIELD 0xFFFFF
#define token *buffer
/* GLOBALS */
static char *buffer = NULL; /* read buffer */
static int regnum = 1; /* for next free virtual register number */
static FILE *outfile = NULL; /* output of code generation */
/* Utilities */
static void CodeGen(OpCode opcode, int field1, int field2, int field3);
static inline void next_token();
static inline int next_register();
static inline int is_digit(char c);
static inline int to_digit(char c);
static inline int is_identifier(char c);
static char *read_input(FILE * f);
/* Routines for recursive descending parser LL(1) */
static void program();
static void stmtlist();
static void morestmts();
static void stmt();
static void assign();
static void print();
static int expr();
static int variable();
static int digit();
/*************************************************************************/
/* Definitions for recursive descending parser LL(1) */
/*************************************************************************/
static int digit()
{
int reg;
if (!is_digit(token)) {
ERROR("Expected digit\n");
exit(EXIT_FAILURE);
}
reg = next_register();
CodeGen(LOADI, to_digit(token), reg, EMPTY_FIELD);
next_token();
return reg;
}
static int variable()
{
int reg;
if (!is_identifier(token)) {
ERROR("Expected identifier\n");
exit(EXIT_FAILURE);
}
reg = next_register();
CodeGen(LOADAI, 0, (token-'a')*4, reg); /* token - 'a' is offset of varible, *4 for byte address */
next_token();
return reg;
}
static int expr()
{
int reg, left_reg, right_reg;
switch (token) {
case '+':
next_token();
left_reg = expr();
right_reg = expr();
reg = next_register();
CodeGen(ADD, left_reg, right_reg, reg);
return reg;
case '-':
next_token();
left_reg = expr();
right_reg = expr();
reg = next_register();
CodeGen(SUB, left_reg, right_reg, reg);
return reg;
case '*':
next_token();
left_reg = expr();
right_reg = expr();
reg = next_register();
CodeGen(MUL, left_reg, right_reg, reg);
return reg;
case '/':
next_token();
left_reg = expr();
right_reg = expr();
reg = next_register();
CodeGen(DIV, left_reg, right_reg, reg);
return reg;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
return variable();
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return digit();
default:
ERROR("Symbol %c unknown\n", token);
exit(EXIT_FAILURE);
}
}
static void assign()
{
char var;
int reg;
if(is_identifier(token)){
var = token;
next_token();
if(token != '='){
ERROR("Expected '=' symbol \n");
exit(EXIT_FAILURE);
}
next_token();
reg = expr();
CodeGen(STOREAI, reg, 0, (var-'a')*4);
}
}
static void print()
{
next_token();
if(!is_identifier(token)){
ERROR("An identifier is expected");
exit(EXIT_FAILURE);
}
CodeGen(OUTPUTAI, 0, (token-'a')*4, EMPTY_FIELD);
next_token();
}
static void stmt()
{
switch(token){
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
assign();
break;
case '!':
print();
break;
case '+':
case '-':
case '*':
case '/':
expr();
break;
}
}
static void morestmts()
{
if(token == ';'){
next_token();
stmtlist();
}
}
static void stmtlist()
{
stmt();
morestmts();
}
static void program()
{
stmtlist();
if (token != '.') {
ERROR("Program error. Current input symbol is %c\n", token);
exit(EXIT_FAILURE);
};
}
/*************************************************************************/
/* Utility definitions */
/*************************************************************************/
static void CodeGen(OpCode opcode, int field1, int field2, int field3)
{
Instruction instr;
if (!outfile) {
ERROR("File error\n");
exit(EXIT_FAILURE);
}
instr.opcode = opcode;
instr.field1 = field1;
instr.field2 = field2;
instr.field3 = field3;
PrintInstruction(outfile, &instr);
}
static inline void next_token()
{
if (*buffer == '\0') {
ERROR("End of program input\n");
exit(EXIT_FAILURE);
}
printf("%c ", *buffer);
if (*buffer == ';')
printf("\n");
buffer++;
if (*buffer == '\0') {
ERROR("End of program input\n");
exit(EXIT_FAILURE);
}
if (*buffer == '.')
printf(".\n");
}
static inline int next_register()
{
return regnum++;
}
static inline int is_digit(char c)
{
if (c >= '0' && c <= '9')
return 1;
return 0;
}
static inline int to_digit(char c)
{
if (is_digit(c))
return c - '0';
WARNING("Non-digit passed to %s, returning zero\n", __func__);
return 0;
}
static inline int is_identifier(char c)
{
if (c >= 'a' && c <= 'n')
return 1;
return 0;
}
static char *read_input(FILE * f)
{
size_t size, i;
char *b;
int c;
for (b = NULL, size = 0, i = 0;;) {
if (i >= size) {
size = (size == 0) ? MAX_BUFFER_SIZE : size * 2;
b = (char *)realloc(b, size * sizeof(char));
if (!b) {
ERROR("Realloc failed\n");
exit(EXIT_FAILURE);
}
}
c = fgetc(f);
if (EOF == c) {
b[i] = '\0';
break;
}
if (isspace(c))
continue;
b[i] = c;
i++;
}
return b;
}
/*************************************************************************/
/* Main function */
/*************************************************************************/
int main(int argc, char *argv[])
{
const char *outfilename = "tinyL.out";
char *input;
FILE *infile;
printf("------------------------------------------------\n");
printf(" Compiler for tinyL\n Fall 2015\n");
printf("------------------------------------------------\n");
if (argc != 2) {
ERROR("Use of command:\n compile <tinyL file>\n");
exit(EXIT_FAILURE);
}
infile = fopen(argv[1], "r");
if (!infile) {
ERROR("Cannot open input file \"%s\"\n", argv[1]);
exit(EXIT_FAILURE);
}
outfile = fopen(outfilename, "w");
if (!outfile) {
ERROR("Cannot open output file \"%s\"\n", outfilename);
exit(EXIT_FAILURE);
}
input = read_input(infile);
buffer = input;
CodeGen(LOADI, 1024, 0, EMPTY_FIELD); /* set base register to 1024 */
program();
printf("\nCode written to file \"%s\".\n\n", outfilename);
free(input);
fclose(infile);
fclose(outfile);
return EXIT_SUCCESS;
}