Created
September 21, 2020 15:29
-
-
Save cpurdy/64032e11f5cf001e5aeffe7014feff85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generate assembly code for a method. This is the entry point for the compilation of a method. | |
* | |
* @param code the code object to which the assembly is added | |
* @param errs the error listener to log to | |
* | |
* @return true if nothing occurred during the compilation that should stop further progress | |
*/ | |
public boolean compileMethod(Code code, ErrorListener errs) | |
{ | |
RootContext ctx = new RootContext(code.getMethodStructure()); | |
ErrorListener errsValidation = errs.branch(); | |
Statement that = this.validate(ctx.validatingContext(), errsValidation); | |
errsValidation.merge(); | |
if (that == null || errsValidation.hasSeriousErrors() || errsValidation.isAbortDesired()) | |
{ | |
return false; | |
} | |
boolean fCompletes = that.completes(ctx.emittingContext(code), true, code, errs); | |
if (fCompletes) | |
{ | |
if (code.getMethodStructure().getReturns().isEmpty()) | |
{ | |
// a void method has an implicit "return;" at the end of it | |
code.add(new Return_0()); | |
} | |
else | |
{ | |
errs.log(Severity.ERROR, Compiler.RETURN_REQUIRED, null, getSource(), | |
getEndPosition(), getEndPosition()); | |
} | |
} | |
else | |
{ | |
// it is is possible that there is a dangling label at the end that is unreachable, | |
// and it will not have been eliminated at this point, so "cap" the op code stream | |
// with a Nop that will get removed by "dead code elimination" | |
code.add(new Nop()); | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment