Add return that doesn't take any value

This commit is contained in:
Rui Ueyama 2019-08-24 08:26:37 +09:00
parent a554dff9d1
commit 3eaf84bf16
3 changed files with 14 additions and 3 deletions

View File

@ -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:

View File

@ -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;

6
tests
View File

@ -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;
}