diff --git a/codegen.c b/codegen.c index 50c125a4e70cabba4bd7cdc4bc3485e7e343a233..48e70c13bb31add2cf935dd109c7d75184aa2538 100644 --- a/codegen.c +++ b/codegen.c @@ -506,8 +506,10 @@ static void gen(Node *node) { return; } case ND_RETURN: - gen(node->lhs); - printf(" pop rax\n"); + if (node->lhs) { + gen(node->lhs); + printf(" pop rax\n"); + } printf(" jmp .L.return.%s\n", funcname); return; case ND_CAST: diff --git a/parse.c b/parse.c index 17cd563f694ccb8c6acd3d4372ebcccd79913190..6ec767f58e17f5dc7cc925c244bcbec663274d1e 100644 --- a/parse.c +++ b/parse.c @@ -1121,7 +1121,7 @@ static Node *stmt(void) { return node; } -// stmt2 = "return" expr ";" +// stmt2 = "return" expr? ";" // | "if" "(" expr ")" stmt ("else" stmt)? // | "switch" "(" expr ")" stmt // | "case" const-expr ":" stmt @@ -1139,6 +1139,9 @@ static Node *stmt(void) { static Node *stmt2(void) { Token *tok; if (tok = consume("return")) { + if (consume(";")) + return new_node(ND_RETURN, tok); + Node *node = new_unary(ND_RETURN, expr(), tok); expect(";"); return node; diff --git a/tests b/tests index 597286b4fa961b0c99ebd9a8873dc463d18bb8a5..f2552377d899453dd3b1f9f903d99b5c8bad53d2 100644 --- a/tests +++ b/tests @@ -117,6 +117,10 @@ int fib(int x) { return fib(x-1) + fib(x-2); } +void ret_none() { + return; +} + static int static_fn() { return 3; } int param_decay(int x[]) { return x[0]; } @@ -678,6 +682,8 @@ int main() { assert(3, tree->lhs->lhs->val, "tree->lhs->lhs->val"); assert(4, tree->lhs->rhs->val, "tree->lhs->rhs->val"); + ret_none(); + printf("OK\n"); return 0; }