Skip to content

Commit

Permalink
Split cc1 from compiler driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 53e8103 commit f3d9613
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
3 changes: 3 additions & 0 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
Expand Down
55 changes: 53 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "chibicc.h"

static bool opt_cc1;
static bool opt_hash_hash_hash;
static char *opt_o;

static char *input_path;
Expand All @@ -11,6 +13,16 @@ static void usage(int status) {

static void parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-###")) {
opt_hash_hash_hash = true;
continue;
}

if (!strcmp(argv[i], "-cc1")) {
opt_cc1 = true;
continue;
}

if (!strcmp(argv[i], "--help"))
usage(0);

Expand Down Expand Up @@ -46,9 +58,37 @@ static FILE *open_file(char *path) {
return out;
}

int main(int argc, char **argv) {
parse_args(argc, argv);
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);
}

// Wait for the child process to finish.
int status;
while (wait(&status) > 0);
if (status != 0)
exit(1);
}

static void run_cc1(int argc, char **argv) {
char **args = calloc(argc + 10, sizeof(char *));
memcpy(args, argv, argc * sizeof(char *));
args[argc++] = "-cc1";
run_subprocess(args);
}

static void cc1(void) {
// Tokenize and parse.
Token *tok = tokenize_file(input_path);
Obj *prog = parse(tok);
Expand All @@ -57,5 +97,16 @@ int main(int argc, char **argv) {
FILE *out = open_file(opt_o);
fprintf(out, ".file 1 \"%s\"\n", input_path);
codegen(prog, out);
}

int main(int argc, char **argv) {
parse_args(argc, argv);

if (opt_cc1) {
cc1();
return 0;
}

run_cc1(argc, argv);
return 0;
}
10 changes: 10 additions & 0 deletions self.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
static void va_end(va_list ap) {}
long strtoul(char *nptr, char **endptr, int base);
void exit(int code);
char *basename(char *path);
char *strrchr(char *s, int c);
int unlink(char *pathname);
int mkstemp(char *template);
int close(int fd);
int fork(void);
int execvp(char *file, char **argv);
void _exit(int code);
int wait(int *wstatus);
int atexit(void (*)(void));
""")

for path in sys.argv[1:]:
Expand Down

0 comments on commit f3d9613

Please sign in to comment.