Make "main" to implicitly return 0

This commit is contained in:
Rui Ueyama 2020-05-11 20:21:17 +09:00
parent 5257ee0f20
commit 9c36dd727c
2 changed files with 7 additions and 1 deletions

View File

@ -1284,6 +1284,13 @@ static void emit_text(Obj *prog) {
gen_stmt(fn->body);
assert(depth == 0);
// [https://www.sigbus.info/n1570#5.1.2.2.3p1] The C spec defines
// a special rule for the main function. Reaching the end of the
// main function is equivalent to returning 0, even though the
// behavior is undefined for the other functions.
if (strcmp(fn->name, "main") == 0)
println(" mov $0, %%rax");
// Epilogue
println(".L.return.%s:", fn->name);
println(" mov %%rbp, %%rsp");

View File

@ -366,5 +366,4 @@ int main() {
ASSERT(5, (***add2)(2,3));
printf("OK\n");
return 0;
}