Skip to content

Commit

Permalink
feat: impl define function
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Nov 1, 2021
1 parent e44f3bf commit 802265a
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 27 deletions.
5 changes: 4 additions & 1 deletion 50cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct Node {
Node *els;
Node **block;
char *funcname;
Node **args;
int len;
int val;
int offset;
Expand All @@ -98,6 +99,7 @@ Node *add();
Node *mul();
Node *unary();
Node *primary();
Node *variable();

Node *new_node(NodeKind kind, Node *lhs, Node *rhs);
Node *new_node_num(int val);
Expand All @@ -114,4 +116,5 @@ struct LVar {
int offset;
};

extern LVar *locals;
extern LVar *locals[];
extern int cur_func;
13 changes: 11 additions & 2 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ void gen(Node *node) {
case ND_BLOCK:
for (int i = 0; node->block[i]; i++) {
gen(node->block[i]);
printf(" pop rax\n");
}
return;
case ND_FUNC_CALL:
Expand Down Expand Up @@ -114,7 +113,17 @@ void gen(Node *node) {
printf("%s:\n", node->funcname);
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, 208\n");
for (int i = 0; node->args[i]; i++) {
printf("push %s\n", argRegs[i]);
argCount++;
}

if (locals[cur_func]) {
int offset = locals[cur_func][0].offset;
offset -= argCount * 8;
printf(" sub rsp, %d\n", offset);
}

gen(node->lhs);
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
Expand Down
Binary file modified codegen.o
Binary file not shown.
3 changes: 2 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ int main(int argc, char **argv) {
return 1;
}

locals = NULL;
user_input = argv[1];
token = tokenize();
program();

printf(".intel_syntax noprefix\n");
printf(".global main\n");

cur_func = 0;
for (int i = 0; code[i]; i++) {
cur_func++;
gen(code[i]);
}

Expand Down
Binary file modified main.o
Binary file not shown.
62 changes: 39 additions & 23 deletions parser.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "50cc.h"

Node *code[100];
LVar *locals;
LVar *locals[100];
int cur_func = 0;

Node *new_node(NodeKind kind, Node *lhs, Node *rhs) {
Node *node = calloc(1, sizeof(Node));
Expand Down Expand Up @@ -83,7 +84,7 @@ int expect_number() {

LVar *find_lvar(Token *tok) {
int i = 0;
for (LVar *var = locals; var; var = var->next) {
for (LVar *var = locals[cur_func]; var; var = var->next) {
i++;
if (var->len == tok->len && !memcmp(tok->str, var->name, var->len))
return var;
Expand All @@ -100,6 +101,7 @@ void program() {

// func = ident "(" ")" stmt
Node *func() {
cur_func++;
Node *node;
Token *tok = consume_ident();
if (tok == NULL) {
Expand All @@ -108,9 +110,20 @@ Node *func() {
node = calloc(1, sizeof(Node));
node->kind = ND_FUNC_DEF;
node->funcname = calloc(100, sizeof(char));
node->args = calloc(10, sizeof(Node *));
memcpy(node->funcname, tok->str, tok->len);
expect("(");
expect(")");
int i = 0;
for (int i = 0; !consume(")"); i++) {
Token *tok = consume_ident();
if (tok != NULL) {
node->args[i] = variable(tok);
}
if (consume(")")) {
break;
}
expect(",");
}
node->lhs = stmt();
return node;
}
Expand Down Expand Up @@ -290,28 +303,31 @@ Node *primary() {
}
return node;
}
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;
return variable(tok);
}

return new_node_num(expect_number());
}

LVar *lvar = find_lvar(tok);
Node *variable(Token *tok) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_LVAR;

if (lvar) {
node->offset = lvar->offset;
LVar *lvar = find_lvar(tok);
if (lvar) {
node->offset = lvar->offset;
} else {
lvar = calloc(1, sizeof(LVar));
lvar->next = locals[cur_func];
lvar->name = tok->str;
lvar->len = tok->len;
if (locals[cur_func] == NULL) {
lvar->offset = 8;
} else {
lvar = calloc(1, sizeof(LVar));
lvar->next = locals;
lvar->name = tok->str;
lvar->len = tok->len;
if (locals == NULL) {
lvar->offset = 8;
} else {
lvar->offset = locals->offset + 8;
}
node->offset = lvar->offset;
locals = lvar;
lvar->offset = locals[cur_func]->offset + 8;
}
return node;
node->offset = lvar->offset;
locals[cur_func] = lvar;
}

return new_node_num(expect_number());
}
return node;
}
Binary file modified parser.o
Binary file not shown.
20 changes: 20 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ assert(){
}

assert 2 "main () { return 2;}"
assert 3 "
main() return func(1, 2);
func(a, b) { return a + b; }
"

assert 4 "
main() return func(1, 2, 3);
func(a, b, c) { return a + c; }
"

assert 3 "main() {
a = 4;
return fib(a);
}
fib(n){
if(n == 0) return 0;
if(n == 1) return 1;
return fib(n - 1)+ fib(n -2);
}
"
# assert 42 "42;"
# assert 21 "5+20-4;"
# assert 2 " 5 - 3; "
Expand Down
Binary file modified tokenize.o
Binary file not shown.

0 comments on commit 802265a

Please sign in to comment.