diff options
author | Michael Forney <mforney@mforney.org> | 2022-03-10 22:52:04 -0800 |
---|---|---|
committer | Michael Forney <mforney@mforney.org> | 2022-03-10 23:08:58 -0800 |
commit | 892cadc8e27a94c6eb794a4d706e0b0c5bd210d0 (patch) | |
tree | 95bc3c1b23131c6d55a8ff8f820989d409ef0146 | |
parent | 995d5b48b15779b9561aef59e035e34ef5c56839 (diff) |
qbe: Only return 0 from main if it has type int
Though C11 5.1.2.2.1 says that main must have a return type of int,
we could still encounter a program which declares it as something
else. This is undefined behavior, but we should not produce invalid
QBE IL in this case.
Also, 5.1.2.2.3 specifies that the implicit return 0 should only
apply when main's return type is compatible with int.
-rw-r--r-- | qbe.c | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -1197,9 +1197,15 @@ emitfunc(struct func *f, bool global) struct block *b; struct inst **inst, **instend; struct param *p; + struct value *v; - if (f->end->jump.kind == JUMP_NONE) - funcret(f, strcmp(f->name, "main") == 0 ? mkintconst(0) : NULL); + if (f->end->jump.kind == JUMP_NONE) { + v = NULL; + /* implicitly return 0 from main if we reach the end of the function */ + if (strcmp(f->name, "main") == 0 && f->type->base == &typeint) + v = mkintconst(0); + funcret(f, v); + } if (global) puts("export"); fputs("function ", stdout); |