This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
194 lines (164 loc) · 5.87 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
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include "codegen.h"
static bool is_file_name_valid(const char* file_name0) {
const char suffix[] = ".kt";
const i32 suffix_len = sizeof(suffix) - 1;
const i32 len = strlen(file_name0);
return (len > suffix_len) &&
memcmp(&file_name0[len - suffix_len], suffix, suffix_len) == 0;
}
static char* stdlib_obj_path() {
struct stat st = {0};
if (stat("mkt_stdlib.o", &st) == 0) return "mkt_stdlib.o";
if (stat("/usr/local/lib/mkt_stdlib.o", &st) == 0)
return "/usr/local/lib/mkt_stdlib.o";
return NULL;
}
static void base_source_file_name(const char* file_name0,
char* base_file_name0) {
CHECK(is_file_name_valid(file_name0), ==, true, "%d");
const i32 len = strlen(file_name0);
base_file_name0[len - 3] = 0;
base_file_name0[len - 2] = 0;
base_file_name0[len - 1] = 0;
}
static i32 run(const char* file_name0) {
CHECK((void*)file_name0, !=, NULL, "%p");
static char base_file_name0[MAXPATHLEN + 1] = "";
static char argv0[3 * MAXPATHLEN] = "";
mkt_res_t res = RES_NONE;
if (!is_file_name_valid(file_name0)) {
res = RES_INVALID_SOURCE_FILE_NAME;
fprintf(stderr, "Invalid source file name %s, must end with `.kt`\n",
file_name0);
return res;
}
struct stat st = {0};
if (stat(file_name0, &st) != 0) {
fprintf(stderr, "Failed to `stat(2)` the source file %s: %s\n",
file_name0, strerror(errno));
return errno;
}
const i32 file_size = st.st_size;
char* const source = malloc(file_size);
if (source == NULL) return ENOMEM;
i32 file = open(file_name0, O_RDONLY);
if (file == -1) {
fprintf(stderr, "Failed to `open(2)` the source file %s: %s\n",
file_name0, strerror(errno));
return errno;
}
const ssize_t read_bytes = read(file, source, file_size);
if (read_bytes == -1) {
fprintf(stderr, "Failed to `read(2)` the source file %s: %s\n",
file_name0, strerror(errno));
return errno;
}
if (read_bytes != file_size) {
fprintf(stderr,
"Failed to fully `read(2)` the source file %s: bytes to "
"read=%d, bytes read=%zd errno=%d err=%s\n",
file_name0, file_size, read_bytes, errno, strerror(errno));
return errno ? errno : EIO;
}
close(file);
parser_t parser = {0};
if ((res = parser_init(file_name0, source, file_size, &parser)) != RES_OK)
return res;
if ((res = parser_parse(&parser)) != RES_OK) return res;
for (i32 i = 0; i < (i32)buf_size(parser.par_class_decls); i++)
node_dump(&parser, parser.par_class_decls[i], 0);
const i32 file_name_len = strlen(file_name0);
char asm_file_name0[MAXPATHLEN + 1] = "";
snprintf(asm_file_name0, MAXPATHLEN, "%.*s.s", (i32)(file_name_len - 3),
file_name0);
FILE* asm_file = fopen(asm_file_name0, "w");
if (asm_file == NULL) {
res = RES_ASM_FILE_READ_FAILED;
fprintf(stderr, "Failed to read asm file %s: %s\n", asm_file_name0,
strerror(errno));
return res;
}
log_debug("writing asm output to `%s`", asm_file_name0);
emit(&parser, asm_file);
// Make sure everything has been written to the file and not simply buffered
fflush(asm_file);
// as
memset(base_file_name0, 0, sizeof(base_file_name0));
memcpy(base_file_name0, file_name0, (size_t)file_name_len);
base_source_file_name(file_name0, base_file_name0);
{
memset(argv0, 0, sizeof(argv0));
snprintf(argv0, sizeof(argv0), AS " %s -o %s.o", asm_file_name0,
base_file_name0);
fflush(stdout);
fflush(stderr);
FILE* as_process = popen(argv0, "r");
if (as_process == NULL) {
res = RES_FAILED_AS;
fprintf(stderr, "Failed to assemble `%s`: %s\n", argv0,
strerror(errno));
return res;
}
if (pclose(as_process) != 0) {
res = RES_FAILED_AS;
fprintf(stderr, "Failed to assemble `%s`: %s\n", argv0,
strerror(errno));
return res;
}
fflush(stdout);
fflush(stderr);
}
// ld
{
const char asan_opts[] =
" "
#if WITH_ASAN == 1 && defined(__APPLE__)
"-lclang_rt.asan_osx_dynamic -L " ASAN_DIR " -rpath " ASAN_DIR " "
#elif WITH_ASAN == 1
"-lclang_rt.asan-x86_64 -L " ASAN_DIR " -rpath " ASAN_DIR " "
#endif
;
const char link_opts[] = "-fPIE ";
const char* const stdlib = stdlib_obj_path();
CHECK((void*)stdlib, !=, NULL, "%p");
memset(argv0, 0, sizeof(argv0));
snprintf(argv0, sizeof(argv0), LD " %s.o %s -o %s.exe %s %s",
base_file_name0, stdlib, base_file_name0, asan_opts,
link_opts);
log_debug("%s", argv0);
fflush(stdout);
fflush(stderr);
FILE* ld_process = popen(argv0, "r");
if (ld_process == NULL) {
res = RES_FAILED_LD;
fprintf(stderr, "Failed to link `%s`: %s\n", argv0,
strerror(errno));
return res;
}
if (pclose(ld_process) != 0) {
res = RES_FAILED_LD;
fprintf(stderr, "Failed to link `%s`: %s\n", argv0,
strerror(errno));
return res;
}
fflush(stdout);
fflush(stderr);
}
log_debug("created executable `%s.exe`", base_file_name0);
return RES_OK;
}
i32 main(i32 argc, char* argv[]) {
if (argc != 2) {
printf("microkt: Tiny Kotlin compiler\nUsage: %s <file>\n", argv[0]);
return 0;
};
is_tty = isatty(2);
i32 err = 0;
if ((err = run(argv[1])) != RES_OK) return err;
}