Skip to content

Commit

Permalink
feat: call func with args
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Oct 31, 2021
1 parent 5e574c1 commit 40a62b9
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 65 deletions.
5 changes: 4 additions & 1 deletion 50cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ typedef enum {
ND_FOR,
ND_FOR_LEFT,
ND_FOR_RIGHT,
ND_BLOCK
ND_BLOCK,
ND_FUNC
} NodeKind;

typedef struct Node Node;
Expand All @@ -77,6 +78,8 @@ struct Node {
Node *rhs;
Node *els;
Node **block;
char *funcname;
int len;
int val;
int offset;
};
Expand Down
10 changes: 10 additions & 0 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ void gen_lval(Node *node) {
}

int genId = 0;
char name[100] = {0};

void gen(Node *node) {
if (!node) return;
Expand Down Expand Up @@ -86,6 +87,15 @@ void gen(Node *node) {
printf(" pop rax\n");
}
return;
case ND_FUNC:
memcpy(name, node->funcname, node->len);
for (int i = 0; node->block[i]; i++) {
gen(node->block[i]);
}
printf(" pop rsi\n");
printf(" pop rdi\n");
printf(" call %s\n", name);
return;
}

gen(node->lhs);
Expand Down
Binary file modified codegen.o
Binary file not shown.
Binary file added func.o
Binary file not shown.
5 changes: 5 additions & 0 deletions func/func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int foo() { printf("%d\n", 10); }

int fooo(int x, int y) { printf("%d\n", x + y); }
Binary file added func/func.o
Binary file not shown.
Binary file modified main.o
Binary file not shown.
15 changes: 15 additions & 0 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ Node *primary() {

Token *tok = consume_ident();
if (tok) {
if (consume("(")) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_FUNC;
node->funcname = tok->str;
node->len = tok->len;
node->block = calloc(10, sizeof(Node));
for (int i = 0; !consume(")"); i++) {
node->block[i] = expr();
if (consume(")")) {
break;
}
expect(",");
}
return node;
}
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;

Expand Down
Binary file modified parser.o
Binary file not shown.
Binary file added test.o
Binary file not shown.
133 changes: 70 additions & 63 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ assert(){
input="$2"

./main "$input" > tmp.s
cc -o tmp tmp.s
cd func
cc -c func.c
cd ..
cc -o tmp tmp.s func/func.o
./tmp
actual="$?"

Expand All @@ -17,68 +20,72 @@ assert(){
fi
}

assert "0" "0;"
assert 42 "42;"
assert 21 "5+20-4;"
assert 2 " 5 - 3; "
assert 47 "5+6*7;"
assert 15 "5*(9-6);"
assert 4 "(3+5)/2;"
assert 10 "-10+20;"

assert 0 "0==1;"
assert 1 "42==42;"
assert 1 "0!=1;"
assert 0 "42!=42;"

assert 1 "0<1;"
assert 0 "1<1;"
assert 0 "2<1;"
assert 1 "0<=1;"
assert 1 "1<=1;"
assert 0 "2<=1;"

assert 1 "1>0;"
assert 0 "1>1;"
assert 0 "1>2;"
assert 1 "1>=0;"
assert 1 "1>=1;"
assert 0 "1>=2;"

assert 14 "a = 3;
b = 5 * 6 - 8;
a + b / 2;"

assert 6 "return 6;"

assert 6 "foo = 1;
bar = 2 + 3;
return foo + bar;"

assert 6 "if (1 != 1) return 1;
else return 6;
return 2;
"

assert 10 "i = 0;
while (i < 10) i = i + 1;
return i;"

assert 10 "a = 0;
for (i = 0; i < 10; i = i + 1) a = a + 1;
return a;"

assert 10 "a = 0;
for (;a < 10;) a = a + 1;
return a;"

assert 10 "a = 0;
for(;;){
a = a + 1;
if(a == 5) return 10;
}
return 1;
"
# assert "0" "0;"
# assert 42 "42;"
# assert 21 "5+20-4;"
# assert 2 " 5 - 3; "
# assert 47 "5+6*7;"
# assert 15 "5*(9-6);"
# assert 4 "(3+5)/2;"
# assert 10 "-10+20;"

# assert 0 "0==1;"
# assert 1 "42==42;"
# assert 1 "0!=1;"
# assert 0 "42!=42;"

# assert 1 "0<1;"
# assert 0 "1<1;"
# assert 0 "2<1;"
# assert 1 "0<=1;"
# assert 1 "1<=1;"
# assert 0 "2<=1;"

# assert 1 "1>0;"
# assert 0 "1>1;"
# assert 0 "1>2;"
# assert 1 "1>=0;"
# assert 1 "1>=1;"
# assert 0 "1>=2;"

# assert 14 "a = 3;
# b = 5 * 6 - 8;
# a + b / 2;"

# assert 6 "return 6;"

# assert 6 "foo = 1;
# bar = 2 + 3;
# return foo + bar;"

# assert 6 "if (1 != 1) return 1;
# else return 6;
# return 2;
# "

# assert 10 "i = 0;
# while (i < 10) i = i + 1;
# return i;"

# assert 10 "a = 0;
# for (i = 0; i < 10; i = i + 1) a = a + 1;
# return a;"

# assert 10 "a = 0;
# for (;a < 10;) a = a + 1;
# return a;"

# assert 10 "a = 0;
# for(;;){
# a = a + 1;
# if(a == 5) return 10;
# }
# return 1;
# "

# assert 10 "foo();"

assert 0 "fooo(4, 1);"


echo OK
2 changes: 1 addition & 1 deletion tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Token *tokenize() {
continue;
}

if (strchr("+-*/()<>=;{}", *p)) {
if (strchr("+-*/()<>=;{},", *p)) {
cur = new_token(TK_RESERVED, cur, p++, 1);
continue;
}
Expand Down
Binary file modified tokenize.o
Binary file not shown.

0 comments on commit 40a62b9

Please sign in to comment.