Skip to content

Commit

Permalink
Self Compile!!!!!!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
sukesan1984 committed Jun 15, 2020
1 parent 1047226 commit 45e491c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -13,6 +13,10 @@ self-test:
make build
docker run --rm sukesan1984/make_compiler bash -c 'cd 9cc; make clean && make self-test'

self2-test:
make build
docker run --rm sukesan1984/make_compiler bash -c 'cd 9cc; make clean && make self2-test'

dump: build
docker run --rm sukesan1984/make_compiler bash -c 'cd 9cc; make clean && make dump'

Expand Down
42 changes: 42 additions & 0 deletions workspace/9cc/Makefile
Expand Up @@ -7,6 +7,7 @@ INCLUDES=-Iinclude -I/usr/local/include -I/usr/include \
SRCS=$(wildcard *.c)
OBJS=$(SRCS:.c=.o)
SELF_OBJS=$(SRCS:.c=.self.o)
SELF2_OBJS=$(SRCS:.c=.self2.o)
TEST_FILE=test/test.c


Expand Down Expand Up @@ -50,6 +51,30 @@ self: 9cc
$(CC) $(CFLAGS) -c _self_codegen.s -o codegen.self.o
$(CC) -o self $(SELF_OBJS) $(CFLAGS)

2nd-self: self
./self main.c > _self_main2.s
$(CC) $(CFLAGS) -c _self_main2.s -o main.self2.o

./self preprocess.c > _self_preprocess2.s
$(CC) $(CFLAGS) -c _self_preprocess2.s -o preprocess.self2.o

./self parse.c > _self_parse2.s
$(CC) $(CFLAGS) -c _self_parse2.s -o parse.self2.o

./self tokenize.c > _self_tokenize2.s
$(CC) $(CFLAGS) -c _self_tokenize2.s -o tokenize.self2.o

./self util.c > _self_util2.s
$(CC) $(CFLAGS) -c _self_util2.s -o util.self2.o

./self sema.c > _self_sema2.s
$(CC) $(CFLAGS) -c _self_sema2.s -o sema.self2.o

./self codegen.c > _self_codegen2.s
$(CC) $(CFLAGS) -c _self_codegen2.s -o codegen.self2.o
$(CC) -o 2nd-self $(SELF2_OBJS) $(CFLAGS)


self-test: self
@./self $(INCLUDES) $(TEST_FILE) > tmp-test.s
echo 'int static_fn() { return 5; }' | \
Expand All @@ -60,6 +85,23 @@ self-test: self
@gcc -static -o tmp-test2 tmp-test2.s
@./tmp-test2

self2-test: 2nd-self
diff _self_main2.s _self_main.s
diff _self_preprocess2.s _self_preprocess.s
diff _self_parse2.s _self_parse.s
diff _self_tokenize2.s _self_tokenize.s
diff _self_util2.s _self_util.s
diff _self_sema2.s _self_sema.s
diff _self_codegen2.s _self_codegen.s
@./2nd-self $(INCLUDES) $(TEST_FILE) > tmp-test.s
echo 'int static_fn() { return 5; }' | \
gcc -xc -c -o tmp2.o -
@gcc -g -static -o tmp-test tmp-test.s test/external.c tmp2.o
@./tmp-test
@./2nd-self -I. test/comment.c > tmp-test2.s
@gcc -static -o tmp-test2 tmp-test2.s
@./tmp-test2

dump: 9cc
./dump.sh $(TEST_FILE)

Expand Down
2 changes: 1 addition & 1 deletion workspace/9cc/codegen.c
Expand Up @@ -163,7 +163,7 @@ void gen_expr(Node *node){
for (int i = 0; i < args_len; i++) {
printf("#先にND_NUMがここで処理されるのではない?\n");
printf("#gen_expr ND_CALL(引数処理): %d番目の引数\n", i);
gen_expr((Node *) node->args->data[i]); // スタックに引数を順に積む
gen_expr(node->args->data[i]); // スタックに引数を順に積む
}
for (int i = args_len - 1; i >= 0; i--) {
printf(" pop rax # スタックされた引数の評価値をスタックからraxに格納\n"); // 結果をraxに格納
Expand Down
3 changes: 1 addition & 2 deletions workspace/9cc/parse.c
Expand Up @@ -1057,7 +1057,7 @@ Initializer *gvar_init_string(char *p, int len) {
Initializer head = {};
Initializer *cur = &head;
for (int i = 0; i < len; i++)
cur = new_init_val(cur, 1, p[i]);
cur = new_init_val(cur, 1, (signed char) p[i]);
return head.next;
}

Expand Down Expand Up @@ -1361,7 +1361,6 @@ static Type *decl_specifiers() {
Type *ty = find_typedef(t->name);
if (ty)
pos++;
fprintf(stderr, "decl_specifiers: not defined %s\n", t->name);
return ty;
}
Type *ty = int_ty();
Expand Down
2 changes: 1 addition & 1 deletion workspace/9cc/sema.c
@@ -1,7 +1,7 @@
#include "9cc.h"

static int str_label;
static Map *vars;
Map *vars;
Vector *globals;
Vector *strings;
static int stacksize;
Expand Down

0 comments on commit 45e491c

Please sign in to comment.