Skip to content

Commit

Permalink
step 14: function call without arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyoro committed May 17, 2019
1 parent eaddef4 commit 1eddf11
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions 9cc.h
Expand Up @@ -72,6 +72,7 @@ enum {
ND_WHILE, // while
ND_FOR, // for
ND_BLOCK, // block
ND_CALL, // function call
ND_EQ, // ==
ND_NE, // !=
ND_LE, // <=
Expand Down
14 changes: 14 additions & 0 deletions code_gen.c
Expand Up @@ -140,6 +140,15 @@ static void gen_block(Node *node) {
}
}

static void gen_call(Node *node) {
// push argument count
printf(" mov rax, 0\n");
// call
printf(" call %s\n", node->name);
// push result to stack
printf(" push rax\n");
}

static bool is_binop(Node *node) {
return strchr("+-*/<", node->ty) || node->ty == ND_EQ ||
node->ty == ND_NE || node->ty == ND_LE;
Expand Down Expand Up @@ -244,6 +253,11 @@ static void gen(Node *node) {
return;
}

if (node->ty == ND_CALL) {
gen_call(node);
return;
}

if (node->ty == '=') {
gen_lval(node->lhs);
gen(node->rhs);
Expand Down
2 changes: 1 addition & 1 deletion dr
@@ -1,3 +1,3 @@
#!/bin/bash

docker run -it -v $(pwd)/:/app -w /app buildpack-deps:jessie "$@"
docker run --rm -it -v $(pwd)/:/app -w /app buildpack-deps:jessie "$@"
30 changes: 23 additions & 7 deletions parse.c
Expand Up @@ -8,6 +8,7 @@ const char *NODE_STRING[] = {
STRING(ND_WHILE),
STRING(ND_FOR),
STRING(ND_BLOCK),
STRING(ND_CALL),
STRING(ND_EQ),
STRING(ND_NE),
STRING(ND_LE),
Expand Down Expand Up @@ -102,7 +103,8 @@ static Node *new_node_cond(int ty, Node *cond) {
add = mul ("+" mul | "-" mul)*
mul = unary ("*" unary | "/" unary)*
unary = ("+" | "-")? term
term = num | "(" expr ")"
term = num | call | (" expr ")"
call = ident [ "()" ]
*/

Node *code[100];
Expand All @@ -122,10 +124,6 @@ static Token *current_token() {
return (Token *)tokens->data[pos];
}

static Token *next_token() {
return (Token *)tokens->data[pos++];
}

void trace_parse(char *name) {
if (debug) {
Token *t = current_token();
Expand Down Expand Up @@ -355,6 +353,18 @@ static Node *mul() {
}
}

Node *parse_num(Token *t) {
return new_node_num(t->val);
}

Node *parse_call(Token *t) {
Node *node = new_node(ND_CALL);
node->name = t->name;
expect(')');

return node;
}

static Node *term() {
trace_parse("term");

Expand All @@ -371,12 +381,18 @@ static Node *term() {

// number
if (t->ty == TK_NUM) {
return new_node_num(next_token()->val);
Node *node = parse_num(t);
pos++;
return node;
}

// identifier
if (t->ty == TK_IDENT) {
return new_node_ident(next_token()->name);
pos++;
if (consume('(')) {
return parse_call(t);
}
return new_node_ident(t->name);
}

error("term : invalid token: %s", t->input);
Expand Down
14 changes: 13 additions & 1 deletion test.sh
Expand Up @@ -3,6 +3,7 @@
try(){
expected="$1"
input="$2"
obj="$3"

echo -n "$input => "
./9cc -debug "$input" > tmp.s
Expand All @@ -12,7 +13,12 @@ try(){
exit $status
fi

gcc -o tmp tmp.s
if [ -n "$obj" ]; then
gcc -static -o tmp tmp.s "$obj"
else
gcc -o tmp tmp.s
fi

./tmp
actual="$?"

Expand Down Expand Up @@ -192,5 +198,11 @@ EOF
)

try 110 "$code"

# step 14 : function call

echo 'int foo() { return 99; }' | gcc -xc -c -o tmp-foo.o -
try 99 "return foo();" tmp-foo.o

# end test
echo OK

0 comments on commit 1eddf11

Please sign in to comment.