chibicc/main.c

209 lines
4.4 KiB
C
Raw Normal View History

2020-10-07 19:11:16 +08:00
#include "chibicc.h"
2019-08-03 14:36:43 +08:00
2020-10-08 13:34:23 +08:00
static bool opt_S;
2020-08-15 21:30:28 +08:00
static bool opt_cc1;
static bool opt_hash_hash_hash;
2020-05-08 19:44:25 +08:00
static char *opt_o;
2020-08-18 08:16:57 +08:00
static char *base_file;
static char *output_file;
static StringArray input_paths;
2020-10-08 13:34:23 +08:00
static StringArray tmpfiles;
2020-05-08 19:44:25 +08:00
static void usage(int status) {
fprintf(stderr, "chibicc [ -o <path> ] <file>\n");
exit(status);
}
2020-08-18 08:16:57 +08:00
static bool take_arg(char *arg) {
return !strcmp(arg, "-o");
}
2020-05-08 19:44:25 +08:00
static void parse_args(int argc, char **argv) {
2020-08-18 08:16:57 +08:00
// Make sure that all command line options that take an argument
// have an argument.
for (int i = 1; i < argc; i++)
if (take_arg(argv[i]))
if (!argv[++i])
usage(1);
2020-05-08 19:44:25 +08:00
for (int i = 1; i < argc; i++) {
2020-08-15 21:30:28 +08:00
if (!strcmp(argv[i], "-###")) {
opt_hash_hash_hash = true;
continue;
}
if (!strcmp(argv[i], "-cc1")) {
opt_cc1 = true;
continue;
}
2020-05-08 19:44:25 +08:00
if (!strcmp(argv[i], "--help"))
usage(0);
if (!strcmp(argv[i], "-o")) {
2020-08-18 08:16:57 +08:00
opt_o = argv[++i];
2020-05-08 19:44:25 +08:00
continue;
}
if (!strncmp(argv[i], "-o", 2)) {
opt_o = argv[i] + 2;
continue;
}
2020-10-08 13:34:23 +08:00
if (!strcmp(argv[i], "-S")) {
opt_S = true;
continue;
}
2020-08-18 08:16:57 +08:00
if (!strcmp(argv[i], "-cc1-input")) {
base_file = argv[++i];
continue;
}
if (!strcmp(argv[i], "-cc1-output")) {
output_file = argv[++i];
continue;
}
2020-05-08 19:44:25 +08:00
if (argv[i][0] == '-' && argv[i][1] != '\0')
error("unknown argument: %s", argv[i]);
2020-08-18 08:16:57 +08:00
strarray_push(&input_paths, argv[i]);
2020-05-08 19:44:25 +08:00
}
2020-08-18 08:16:57 +08:00
if (input_paths.len == 0)
2020-05-08 19:44:25 +08:00
error("no input files");
}
static FILE *open_file(char *path) {
if (!path || strcmp(path, "-") == 0)
return stdout;
FILE *out = fopen(path, "w");
if (!out)
error("cannot open output file: %s: %s", path, strerror(errno));
return out;
}
2020-10-08 13:34:23 +08:00
// Replace file extension
static char *replace_extn(char *tmpl, char *extn) {
char *filename = basename(strdup(tmpl));
char *dot = strrchr(filename, '.');
if (dot)
*dot = '\0';
return format("%s%s", filename, extn);
}
static void cleanup(void) {
for (int i = 0; i < tmpfiles.len; i++)
unlink(tmpfiles.data[i]);
}
static char *create_tmpfile(void) {
char *path = strdup("/tmp/chibicc-XXXXXX");
int fd = mkstemp(path);
if (fd == -1)
error("mkstemp failed: %s", strerror(errno));
close(fd);
strarray_push(&tmpfiles, path);
return path;
}
2020-08-15 21:30:28 +08:00
static void run_subprocess(char **argv) {
// If -### is given, dump the subprocess's command line.
if (opt_hash_hash_hash) {
fprintf(stderr, "%s", argv[0]);
for (int i = 1; argv[i]; i++)
fprintf(stderr, " %s", argv[i]);
fprintf(stderr, "\n");
}
if (fork() == 0) {
// Child process. Run a new command.
execvp(argv[0], argv);
fprintf(stderr, "exec failed: %s: %s\n", argv[0], strerror(errno));
_exit(1);
}
2019-08-03 14:36:43 +08:00
2020-08-15 21:30:28 +08:00
// Wait for the child process to finish.
int status;
while (wait(&status) > 0);
if (status != 0)
exit(1);
}
2020-10-08 13:34:23 +08:00
static void run_cc1(int argc, char **argv, char *input, char *output) {
2020-08-15 21:30:28 +08:00
char **args = calloc(argc + 10, sizeof(char *));
memcpy(args, argv, argc * sizeof(char *));
args[argc++] = "-cc1";
2020-10-08 13:34:23 +08:00
2020-08-18 08:16:57 +08:00
if (input) {
args[argc++] = "-cc1-input";
2020-10-08 13:34:23 +08:00
args[argc++] = input;
2020-08-18 08:16:57 +08:00
}
2020-10-08 13:34:23 +08:00
if (output) {
2020-08-18 08:16:57 +08:00
args[argc++] = "-cc1-output";
2020-10-08 13:34:23 +08:00
args[argc++] = output;
}
2020-08-15 21:30:28 +08:00
run_subprocess(args);
}
static void cc1(void) {
// Tokenize and parse.
2020-08-18 08:16:57 +08:00
Token *tok = tokenize_file(base_file);
Obj *prog = parse(tok);
2020-10-07 19:12:19 +08:00
// Traverse the AST to emit assembly.
2020-08-18 08:16:57 +08:00
FILE *out = open_file(output_file);
fprintf(out, ".file 1 \"%s\"\n", base_file);
2020-05-08 19:44:25 +08:00
codegen(prog, out);
2020-08-15 21:30:28 +08:00
}
2020-10-08 13:34:23 +08:00
static void assemble(char *input, char *output) {
char *cmd[] = {"as", "-c", input, "-o", output, NULL};
run_subprocess(cmd);
}
2020-08-15 21:30:28 +08:00
int main(int argc, char **argv) {
2020-10-08 13:34:23 +08:00
atexit(cleanup);
2020-08-15 21:30:28 +08:00
parse_args(argc, argv);
if (opt_cc1) {
cc1();
return 0;
}
2020-08-18 08:16:57 +08:00
if (input_paths.len > 1 && opt_o)
error("cannot specify '-o' with multiple files");
for (int i = 0; i < input_paths.len; i++) {
char *input = input_paths.data[i];
char *output;
if (opt_o)
output = opt_o;
else if (opt_S)
output = replace_extn(input, ".s");
else
output = replace_extn(input, ".o");
// If -S is given, assembly text is the final output.
if (opt_S) {
run_cc1(argc, argv, input, output);
continue;
}
// Otherwise, run the assembler to assemble our output.
char *tmpfile = create_tmpfile();
run_cc1(argc, argv, input, tmpfile);
assemble(tmpfile, output);
2020-10-08 13:34:23 +08:00
}
return 0;
}