Make the main function return 0 if execution reaches the closing brace of its body.

This is required by C99 and later. It’s not required by C89, but it’s allowed and produces more predictable behavior.
This commit is contained in:
Stephen Heumann 2016-12-26 18:17:02 -06:00
parent 90c291808a
commit a75d18a45c
1 changed files with 16 additions and 0 deletions

View File

@ -291,6 +291,22 @@ stPtr := statementList; {pop the statement record}
statementList := stPtr^.next;
doingFunction := statementList <> nil; {see if we're done with the function}
if not doingFunction then begin {if so, finish it off}
if doingMain then begin {executing to the end of main returns 0}
if fType^.kind = scalarType then begin
if fType^.baseType in [cgByte,cgUByte,cgWord,cgUWord] then begin
Gen1t(pc_ldc, 0, fType^.baseType);
Gen2t(pc_str, 0, 0, fType^.baseType);
end {if}
else if fType^.baseType in [cgLong,cgULong] then begin
GenLdcLong(0);
Gen2t(pc_str, 0, 0, fType^.baseType);
end; {else if}
end {if}
else if fType^.kind = enumType then begin
Gen1t(pc_ldc, 0, cgWord);
Gen2t(pc_str, 0, 0, cgWord);
end; {else if}
end; {if}
Gen1(dc_lab, returnLabel);
with fType^ do {generate the pc_ret instruction}
case kind of