Skip to content

Instantly share code, notes, and snippets.

@cpurdy
Created September 21, 2020 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpurdy/64032e11f5cf001e5aeffe7014feff85 to your computer and use it in GitHub Desktop.
Save cpurdy/64032e11f5cf001e5aeffe7014feff85 to your computer and use it in GitHub Desktop.
/**
* 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