-
Notifications
You must be signed in to change notification settings - Fork 1
/
zncc.c
239 lines (216 loc) · 7.48 KB
/
zncc.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#define _CRT_SECURE_NO_WARNINGS
#ifdef _WIN32
#include <process.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
//#include "compile.h"
#define CCB_IMPLEMENTATION
#include "zncp.h"
#define CCBGENERIC_IMPLEMENTATION
#include "zncg.h"
#ifdef TOOL_CPP
#define CPP_IMPLEMENTATION
#include "cpp.h"
#endif
#ifdef TOOL_MK
#define MK_IMPLEMENTATION
#include "mk.h"
#endif
int sh_main(int argc, char** argv);
void ccb_compile_error_impl(ccb_t* ccb/*, const char* fmt, ...*/) {
fprintf(stderr, "^ Around line %d, column %d of input.\n", ccb->line, ccb->col);
/*#ifdef _ZCC
fprintf(stderr, "ERROR '%s'\n(TODO: Better formatting!)\n", fmt);
((char*)NULL)[0] = 0; // Trigger debugger
#else
va_list a;
va_start(a, fmt);
vfprintf(stderr, fmt, a);
fprintf(stderr, "\n");
va_end(a);
#endif*/
exit(EXIT_FAILURE);
}
void ccb_compile_warn_impl(ccb_t* ccb/*, const char* fmt, ...*/) {
fprintf(stderr, "^ Around line %d, column %d of input.\n", ccb->line, ccb->col);
/*#ifdef _ZCC
fprintf(stderr, "WARNING '%s'\n(TODO: Better formatting!)\n", fmt);
#else
va_list a;
va_start(a, fmt);
vfprintf(stderr, fmt, a);
fprintf(stderr, "\n");
va_end(a);
#endif*/
}
static int startcompile(ccb_t* ccb) {
ccb_util_init(ccb);
ccb_target_init(ccb);
ccb_ast_init(ccb);
ccb_list_t* block = ccb_parse_run(ccb);
if (!ccb->dump_ast) {
if (ccb->include_data) {
ccb_target_gen_data_section(ccb);
}
}
for (ccb_list_iterator_t* it = ccb_list_iterator(block); !ccb_list_iterator_end(it); ) {
if (!ccb->dump_ast) {
if (ccb->include_code) {
ccb_target_gen_function(ccb, ccb_list_iterator_next(it));
}
}
else {
printf("%s", ccb_ast_string(ccb, ccb_list_iterator_next(it)));
}
}
return true;
}
/* Original main program from LICE:
int main(int argc, char **argv) {
argc--;
argv++;
return startcompile(!!(argc && !strcmp(*argv, "--dump-ast")))
? EXIT_SUCCESS
: EXIT_FAILURE;
}
*/
static void usage(int argc, char** argv, int arge) {
//fprintf(stderr, "TODO: Usage!\n");
char* n = argv[0];
fprintf(stderr, "USAGE:\n\n");
fprintf(stderr, " %s [--silent] [--ast-only|--code-only|--data-only|--usage] [TODO --asmfmt fasm|gas] [TODO --binfmt elf|flat] [--input <fname>] [--output|--append <fname>]\n\n", n);
fprintf(stderr, "(This is a simple core compiler designed to be invoked from a more user-friendly frontend.\nArguments must be provided in the above order, defaults to using stdin/stdout.)");
}
int preprocessormain(int argc, char** argv); // Extra definition of the C preprocessor main program
//void mk_main(int argc, char** argv); // Extra definition of the "make" tool main program
/* Zak's new main program: */
int main(int argc, char** argv) {
//argc = 3;
//argv = (char*[]){"test",/*"--ast-only",*/"--input","C:\\Users\\Zak\\source\\repos\\ZCC\\Debug\\test2.c"};
ccb_t ccb;
ccb.dump_ast = false;
ccb.silent = false;
ccb.input = stdin;
ccb.output = stdout;
ccb.include_code = true;
ccb.include_data = true;
ccb.line = 1;
ccb.col = 1;
int argi = 1;
if (argc > argi && (!strcmp(argv[argi], "--silent") || !strcmp(argv[argi], "--usage"))) { // Also skip added notices if using --usage
if (!strcmp(argv[argi], "--silent")) {
argi++;
}
ccb.silent = true;
}
else {
fprintf(stderr, "CCb C Compiler backend (early version)\n");
fprintf(stderr, "NOTE: This program generally reads program code from standard input (until EOF) and writes assembly code to standard output.\n");
fprintf(stderr, " It would usually be used from a compiler frontend (preprocessing, assembling and linking must be done separately).\n");
fprintf(stderr, " Use --silent as the first argument to disable these notices or --usage to learn more.\n\n");
}
if (argc > argi && !strcmp(argv[argi], "--ast-only")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Will dump AST to output instead of assembly code.\n");
}
ccb.dump_ast = true;
argi++;
}
else if (argc > argi && !strcmp(argv[argi], "--data-only")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Will dump data section only.\n");
}
ccb.include_code = false;
ccb.include_data = true;
argi++;
}
else if (argc > argi && !strcmp(argv[argi], "--code-only")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Will dump code section only.\n");
}
ccb.include_code = true;
ccb.include_data = false;
argi++;
}
#ifdef TOOL_CPP
else if (argc > argi && !strcmp(argv[argi], "-P")) { /* Invoke the built-in preprocessor instead of the backend. */
if (!ccb.silent) {
fprintf(stderr, "NOTE: Invoking the experimental preprocessor.\n");
}
// NOTE: This will only work for argv == 1, and because cppmain ignores the -P flag!
return preprocessormain(argc, argv);
}
#endif
#ifdef TOOL_MK
else if (argc > argi && !strcmp(argv[argi], "-M")) { /* Invoke the built-in make tool instead of the backend. */
if (!ccb.silent) {
fprintf(stderr, "NOTE: Invoking the experimental make tool.\n");
}
// NOTE: This will only work for argv == 1, and because cppmain ignores the -P flag!
void* tmp = NULL;
mk_main(argc, argv, &tmp);
return -1; // TODO: mk_main returns void but also has some exit calls, this behaviour needs to be checked to return error codes properly
}
#endif
#ifdef TOOL_SH
else if (argc > argi && !strcmp(argv[argi], "-S")) { /* Invoke the built-in make tool instead of the backend. */
if (!ccb.silent) {
fprintf(stderr, "NOTE: Invoking the experimental shell tool.\n");
}
// NOTE: This will only work for argv == 1, and because cppmain ignores the -P flag!
void* tmp = NULL;
return sh_main(argc, argv);
}
#endif
if (argc > argi && !strcmp(argv[argi], "--usage")) {
usage(argc, argv, -1);
return EXIT_SUCCESS;
}
if (argc > argi + 1 && !strcmp(argv[argi], "--input")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Using input from '%s'.\n", argv[argi + 1]);
}
ccb.input = fopen(argv[argi + 1], "r");
argi += 2;
}
if (argc > argi + 1 && !strcmp(argv[argi], "--output")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Writing output to '%s'.\n", argv[argi + 1]);
}
ccb.output = fopen(argv[argi + 1], "w");
argi += 2;
}
else if (argc > argi + 1 && !strcmp(argv[argi], "--append")) {
if (!ccb.silent) {
fprintf(stderr, "NOTE: Appending output from '%s'.\n", argv[argi]);
}
ccb.output = fopen(argv[argi + 1], "a");
argi += 2;
}
if (ccb.input == NULL) {
fprintf(stderr, "ERROR: Failed to open input file.\n");
}
if (ccb.output == NULL) {
fprintf(stderr, "ERROR: Failed to open output file.\n");
}
if (argi != argc) {
usage(argc, argv, argi);
return EXIT_FAILURE;
}
int result = startcompile(&ccb);
if (ccb.input != stdin) {
fclose(ccb.input);
}
if (ccb.output != stdout) {
fclose(ccb.output);
}
if (result) {
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}