Skip to content

Commit

Permalink
JIT: don't include a final return statement if we're sure the functio…
Browse files Browse the repository at this point in the history
…n already returned
  • Loading branch information
gfwilliams committed Jan 24, 2023
1 parent 401101b commit 344422a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/jsjit.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,11 +828,16 @@ void jsjExpression() {
}
}

void jsjBlockNoBrackets() {
// Returns true if isFunctionRoot and we had a RETURN statement (so we definitely already returned;
bool jsjBlockNoBrackets() {
// if isFunctionRoot we're at the root scope
bool hadReturn = false;
while (lex->tk && lex->tk!='}' && JSJ_PARSING) {
if (lex->tk == LEX_R_RETURN)
hadReturn = true;
jsjStatement();
}
return;
return hadReturn;
}

void jsjBlock() {
Expand Down Expand Up @@ -1061,12 +1066,14 @@ JsVar *jsjParseFunction() {
if (JSJ_PARSING) { // if no error, re-parse and create code
jslSeekTo(codeStartPosition);
jit.phase = JSJP_EMIT; DEBUG_JIT("; ============ EMIT PHASE\n");
jsjBlockNoBrackets();
// optimisation: if the last statement was a return, no need for this. Could check if last instruction was 'POP {r4,r5,r6,r7,pc}'
// Return 'undefined' from function if no other return statement
DEBUG_JIT_EMIT("; END of function - return undefined\n");
jsjcLiteral32(0, 0);
jsjFunctionReturn(false/*isReturnStatement*/);
bool hadReturnStatement = jsjBlockNoBrackets(true);
// if this block had a return in it (eg not behind 'if'/etc), hadReturnStatement=true
// if so, we can skip adding a return statement
if (!hadReturnStatement) {
DEBUG_JIT_EMIT("; END of function - return undefined\n");
jsjcLiteral32(0, 0);
jsjFunctionReturn(false/*isReturnStatement*/);
}
}
JsVar *v = jsjcStop();
JsVar *exception = jspGetException();
Expand Down
10 changes: 7 additions & 3 deletions src/jsjitc.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ const char *jsjcGetTypeName(JsjValueType t) {

void jsjcDebugPrintf(const char *fmt, ...) {
if (jsFlags & JSF_JIT_DEBUG) {
if (!jit.blockCount) jsiConsolePrintf("%6x: ", jsjcGetByteCount());
else jsiConsolePrintf(" : ");
if (fmt[0]==';') { // just a comment - don't add address
jsiConsolePrintf(" ");
} else {
if (!jit.blockCount) jsiConsolePrintf("%6x: ", jsjcGetByteCount());
else jsiConsolePrintf(" : ");
}
va_list argp;
va_start(argp, fmt);
vcbprintf((vcbprintf_callback)jsiConsolePrint,0, fmt, argp);
Expand All @@ -71,7 +75,7 @@ void jsjcStart() {
}

JsVar *jsjcStop() {
jsjcDebugPrintf("VARS: %j\n", jit.vars);
jsjcDebugPrintf("; VARS: %j\n", jit.vars);
jsvUnLock(jit.vars);
jit.vars = 0;
assert(jit.stackDepth == 0);
Expand Down

0 comments on commit 344422a

Please sign in to comment.