Skip to content

Commit

Permalink
feat: impl define function with no args
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Nov 1, 2021
1 parent b6ee2c2 commit e44f3bf
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 23 deletions.
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"cstdlib": "c",
"mutex": "c",
"compare": "c",
"functional": "c",
"type_traits": "c"
}
}
4 changes: 3 additions & 1 deletion 50cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ typedef enum {
ND_FOR_LEFT,
ND_FOR_RIGHT,
ND_BLOCK,
ND_FUNC
ND_FUNC_CALL,
ND_FUNC_DEF,
} NodeKind;

typedef struct Node Node;
Expand All @@ -87,6 +88,7 @@ struct Node {
extern Node *code[];

void program();
Node *func();
Node *stmt();
Node *assign();
Node *expr();
Expand Down
21 changes: 15 additions & 6 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ void gen_lval(Node *node) {

int genId = 0;
char *argRegs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
char name[100] = {0};

void gen(Node *node) {
if (!node) return;
genId += 1;
int id = genId;
int argCount = 0;
switch (node->kind) {
case ND_NUM:
printf(" push %d\n", node->val);
Expand Down Expand Up @@ -88,9 +88,7 @@ void gen(Node *node) {
printf(" pop rax\n");
}
return;
case ND_FUNC:
memcpy(name, node->funcname, node->len);
int argCount = 0;
case ND_FUNC_CALL:
for (int i = 0; node->block[i]; i++) {
gen(node->block[i]);
argCount++;
Expand All @@ -102,14 +100,25 @@ void gen(Node *node) {
printf(" and rax, 15\n");
printf(" jnz .L.call.%d\n", id);
printf(" mov rax, 0\n");
printf(" call %s\n", name);
printf(" call %s\n", node->funcname);
printf(" jmp .L.end.%d\n", id);
printf(".L.call.%d:\n", id);
printf(" sub rsp, 8\n");
printf("mov rax, 0\n");
printf(" call %s\n", name);
printf(" call %s\n", node->funcname);
printf(" add rsp, 8\n");
printf(".L.end.%d:\n", id);
printf(" push rax\n");
return;
case ND_FUNC_DEF:
printf("%s:\n", node->funcname);
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, 208\n");
gen(node->lhs);
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
return;
}

Expand Down
Binary file modified codegen.o
Binary file not shown.
9 changes: 0 additions & 9 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,10 @@ int main(int argc, char **argv) {

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

printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, 208\n");

for (int i = 0; code[i]; i++) {
gen(code[i]);
printf(" pop rax\n");
}

printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
return 0;
}
Binary file modified main.o
Binary file not shown.
26 changes: 22 additions & 4 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,30 @@ LVar *find_lvar(Token *tok) {
return NULL;
}

// program = func*
void program() {
int i = 0;
while (!at_eof()) code[i++] = stmt();
while (!at_eof()) code[i++] = func();
code[i] = NULL;
}

// func = ident "(" ")" stmt
Node *func() {
Node *node;
Token *tok = consume_ident();
if (tok == NULL) {
error("invalid function");
}
node = calloc(1, sizeof(Node));
node->kind = ND_FUNC_DEF;
node->funcname = calloc(100, sizeof(char));
memcpy(node->funcname, tok->str, tok->len);
expect("(");
expect(")");
node->lhs = stmt();
return node;
}

Node *stmt() {
Node *node;
if (consume("{")) {
Expand Down Expand Up @@ -259,9 +277,9 @@ Node *primary() {
if (tok) {
if (consume("(")) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_FUNC;
node->funcname = tok->str;
node->len = tok->len;
node->kind = ND_FUNC_CALL;
node->funcname = calloc(100, sizeof(char));
memcpy(node->funcname, tok->str, tok->len);
node->block = calloc(10, sizeof(Node));
for (int i = 0; !consume(")"); i++) {
node->block[i] = expr();
Expand Down
Binary file modified parser.o
Binary file not shown.
6 changes: 3 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ assert(){
fi
}

# assert "0" "0;"
assert 2 "main () { return 2;}"
# assert 42 "42;"
# assert 21 "5+20-4;"
# assert 2 " 5 - 3; "
Expand Down Expand Up @@ -85,8 +85,8 @@ assert(){

# assert 10 "foo();"

assert 0 "fooo(4, 1);"
assert 0 "foooo(1, 1, 1);"
# assert 0 "fooo(4, 1);"
# assert 0 "foooo(1, 1, 1);"


echo OK
Binary file modified tokenize.o
Binary file not shown.

0 comments on commit e44f3bf

Please sign in to comment.