-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
222 lines (184 loc) · 5.82 KB
/
main.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
#include "mimicc.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Types Types;
Globals globals;
// Allocate memory and return it with entirely 0 cleared. If allocating memory
// fails, exit program immediately.
void *safeAlloc(size_t size) {
void *p = malloc(size);
if (!p)
error("Allocating memory failed.");
memset(p, 0, size);
return p;
}
// Print error message on stderr and exit.
void error(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
_Noreturn void errorAt(Token *loc, const char *fmt, ...) {
char *head = NULL; // Head of error line
char *tail = NULL; // Tail of error line
int indent = 0;
va_list ap;
if (!loc) {
fputs("Error while processing internal node: ", stderr);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
va_start(ap, fmt);
head = (char *)(loc->str - loc->column);
tail = loc->str;
while (*tail != '\n')
tail++;
indent = fprintf(stderr, "%s:%d: ", loc->file->display, loc->line);
fprintf(stderr, "%.*s\n", (int)(tail - head), head);
if (loc->column + indent) {
fprintf(stderr, "%*s", loc->column + indent, " ");
}
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
// Like putchar(), but dump character into output file.
int dumpc(int c) {
if (!globals.destFile)
errorUnreachable();
return fputc(c, globals.destFile);
}
// Like puts(), but dump string into output file.
int dumps(const char *s) {
if (!globals.destFile)
errorUnreachable();
return fprintf(globals.destFile, "%s\n", s);
}
// Like printf(), but dump string into output file.
int dumpf(const char *fmt, ...) {
va_list ap;
int retval;
if (!globals.destFile)
errorUnreachable();
va_start(ap, fmt);
retval = vfprintf(globals.destFile, fmt, ap);
va_end(ap);
return retval;
}
FilePath *analyzeFilepath(const char *path, const char *display) {
FilePath *obj = (FilePath *)safeAlloc(sizeof(FilePath));
int pathSize = 0;
int displaySize = 0;
int dirnameLen = 0;
displaySize = strlen(display) + 1;
obj->display = (char *)safeAlloc(displaySize);
memcpy(obj->display, display, displaySize);
pathSize = strlen(path) + 1;
obj->path = (char *)safeAlloc(pathSize);
memcpy(obj->path, path, pathSize);
for (dirnameLen = pathSize; dirnameLen > 0; --dirnameLen) {
if (path[dirnameLen - 1] == '/')
break;
}
obj->dirname = (char *)safeAlloc(dirnameLen + 1);
memcpy(obj->dirname, path, dirnameLen);
obj->dirname[dirnameLen] = '\0';
obj->basename = (char *)safeAlloc(pathSize - dirnameLen);
memcpy(obj->basename, &path[dirnameLen], pathSize - dirnameLen);
return obj;
}
char *readFile(const char *path) {
FILE *fp = fopen(path, "r");
char *buf = NULL;
size_t size;
if (!fp) {
error("File open failed: %s: %s\n", path, strerror(errno));
}
if (fseek(fp, 0, SEEK_END) == -1) {
fclose(fp);
error("%s: fseek: %s\n", path, strerror(errno));
}
size = ftell(fp);
if (fseek(fp, 0, SEEK_SET) == -1) {
fclose(fp);
error("%s: fseek: %s\n", path, strerror(errno));
}
buf = (char *)safeAlloc((size + 2) * sizeof(char));
fread(buf, size, 1, fp);
fclose(fp);
if (size == 0 || buf[size - 1] != '\n')
buf[size++] = '\n';
buf[size] = '\0';
return buf;
}
_Noreturn void cmdlineArgsError(int argc, char *argv[], int at, const char *msg) {
int len = 0;
for (int i = 0; i < argc; ++i) {
fprintf(stderr, "%s ", argv[i]);
if (i <= at)
len += strlen(argv[i]) + 1;
}
fprintf(stderr, "\n%*s %s\n", len, "^", msg);
exit(1);
}
int main(int argc, char *argv[]) {
char *inFile = NULL;
char *outFile = NULL;
char *source = NULL;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-o") == 0) {
if ((++i) == argc)
cmdlineArgsError(argc, argv, i, "File name must follow after \"-o\"");
outFile = argv[i];
} else if (strcmp(argv[i], "-S") == 0) {
// Just ignore
} else if (!inFile) {
inFile = argv[i];
} else {
cmdlineArgsError(argc, argv, i - 1, "Invalid argument");
}
}
if (!inFile)
cmdlineArgsError(argc, argv, argc, "No input file is specified");
else if (!outFile)
cmdlineArgsError(argc, argv, argc, "No output file is specified");
#define PrimitiveType(type) \
(TypeInfo) { NULL, type }
Types.None = PrimitiveType(TypeNone);
Types.Void = PrimitiveType(TypeVoid);
Types.Int = PrimitiveType(TypeInt);
Types.Char = PrimitiveType(TypeChar);
Types.Number = PrimitiveType(TypeNumber);
#undef PrimitiveType
memset(&globals, 0, sizeof(globals));
globals.currentEnv = &globals.globalEnv;
globals.ccFile = analyzeFilepath(argv[0], argv[0]);
globals.includePath = (char *)safeAlloc(strlen(globals.ccFile->dirname) + 9);
sprintf(globals.includePath, "%sinclude/", globals.ccFile->dirname);
source = readFile(inFile);
globals.token = tokenize(source, analyzeFilepath(inFile, inFile));
preprocess(globals.token);
removeAllNewLineToken(globals.token);
program();
verifyType(globals.code);
verifyFlow(globals.code);
globals.destFile = fopen(outFile, "wb");
if (!globals.destFile)
error("Failed to open file: %s", outFile);
dumps(".intel_syntax noprefix");
genCode(globals.code);
genCodeGlobals();
fclose(globals.destFile);
return 0;
}