Skip to content

Commit 312afad

Browse files
aardvark179rbri
authored andcommitted
Clean up the main loop now that all instructions have been extracted.
1 parent 70d2088 commit 312afad

1 file changed

Lines changed: 42 additions & 96 deletions

File tree

rhino/src/main/java/org/mozilla/javascript/Interpreter.java

Lines changed: 42 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,117 +1786,63 @@ private static Object interpretLoop(Context cx, CallFrame frame, Object throwabl
17861786
private static InterpreterResult interpretFunction(
17871787
Context cx,
17881788
CallFrame frame,
1789-
Object throwable,
1790-
GeneratorState generatorState,
1791-
int indexReg,
1789+
Object tble,
1790+
GeneratorState genState,
1791+
int iReg,
17921792
boolean instructionCounting) {
1793+
final InterpreterState state =
1794+
new InterpreterState(frame.savedStackTop, iReg, instructionCounting);
17931795

17941796
withoutExceptions:
17951797
try {
17961798

17971799
// Use local variables for constant values in frame
17981800
// for faster access
1799-
final Object[] stack = frame.stack;
1800-
final double[] sDbl = frame.sDbl;
1801-
final Object[] vars = frame.varSource.stack;
1802-
final double[] varDbls = frame.varSource.sDbl;
1803-
final byte[] varAttributes = frame.varSource.stackAttributes;
1804-
final InterpreterData iData = frame.idata;
18051801
final byte[] iCode = frame.idata.itsICode;
1806-
final String[] strings = frame.idata.itsStringTable;
1807-
final BigInteger[] bigInts = frame.idata.itsBigIntTable;
1808-
String stringReg = null;
1809-
BigInteger bigIntReg = null;
18101802

1811-
// Use local for stackTop as well. Since execption handlers
1812-
// can only exist at statement level where stack is empty,
1813-
// it is necessary to save/restore stackTop only across
1814-
// function calls and normal returns.
1815-
int stackTop = frame.savedStackTop;
1803+
state.generatorState = genState;
1804+
state.throwable = tble;
18161805

18171806
// Store new frame in cx which is used for error reporting etc.
18181807
cx.lastInterpreterFrame = frame;
18191808

18201809
Loop:
18211810
for (; ; ) {
18221811

1823-
// Exception handler assumes that PC is already incremented
1824-
// pass the instruction start when it searches the
1825-
// exception handler
1826-
int op = iCode[frame.pc++];
1827-
jumplessRun:
1828-
{
1829-
{ // Extra braces to reduce the formatting change.
1830-
// Back indent to ease implementation reading
1831-
switch (op) {
1832-
default:
1833-
{
1834-
NewState nextState;
1835-
1836-
var insn = instructionObjs[-MIN_ICODE + op];
1837-
if (insn == null) {
1838-
dumpICode(iData);
1839-
throw new RuntimeException(
1840-
"Unknown icode : "
1841-
+ op
1842-
+ " @ pc : "
1843-
+ (frame.pc - 1));
1844-
}
1845-
1846-
// The state will become
1847-
// persistent for an entire call
1848-
// once all instructions are moved
1849-
// to this new scheme.
1850-
InterpreterState state =
1851-
new InterpreterState(
1852-
stackTop, indexReg, instructionCounting);
1853-
state.stringReg = stringReg;
1854-
state.bigIntReg = bigIntReg;
1855-
state.throwable = throwable;
1856-
state.generatorState = generatorState;
1857-
1858-
nextState = insn.execute(cx, frame, state, op);
1859-
1860-
stackTop = state.stackTop;
1861-
indexReg = state.indexReg;
1862-
stringReg = state.stringReg;
1863-
bigIntReg = state.bigIntReg;
1864-
throwable = state.throwable;
1865-
generatorState = state.generatorState;
1866-
1867-
if (nextState == null) {
1868-
continue Loop;
1869-
} else if (nextState == BREAK_LOOP) {
1870-
break Loop;
1871-
} else if (nextState == BREAK_JUMPLESSRUN) {
1872-
break jumplessRun;
1873-
} else if (nextState == BREAK_WITHOUT_EXTENSION) {
1874-
break withoutExceptions;
1875-
} else {
1876-
return (InterpreterResult) nextState;
1877-
}
1878-
}
1879-
} // end of interpreter switch
1880-
} // end of extra braces.
1881-
} // end of jumplessRun label block
1812+
NewState nextState;
1813+
do {
1814+
// Exception handler assumes that PC is already incremented
1815+
// pass the instruction start when it searches the
1816+
// exception handler
1817+
int op = iCode[frame.pc++];
18821818

1883-
// This should be reachable only for jump implementation
1884-
// when pc points to encoded target offset
1885-
if (instructionCounting) {
1886-
addInstructionCount(cx, frame, 2);
1887-
}
1888-
int offset = getShort(iCode, frame.pc);
1889-
if (offset != 0) {
1890-
// -1 accounts for pc pointing to jump opcode + 1
1891-
frame.pc += offset - 1;
1819+
var insn = instructionObjs[-MIN_ICODE + op];
1820+
1821+
nextState = insn.execute(cx, frame, state, op);
1822+
} while (nextState == null);
1823+
1824+
if (nextState == BREAK_LOOP) {
1825+
break Loop;
1826+
} else if (nextState == BREAK_JUMPLESSRUN) {
1827+
if (instructionCounting) {
1828+
addInstructionCount(cx, frame, 2);
1829+
}
1830+
int offset = getShort(iCode, frame.pc);
1831+
if (offset != 0) {
1832+
// -1 accounts for pc pointing to jump opcode + 1
1833+
frame.pc += offset - 1;
1834+
} else {
1835+
frame.pc = frame.idata.longJumps.get(frame.pc);
1836+
}
1837+
if (instructionCounting) {
1838+
frame.pcPrevBranch = frame.pc;
1839+
}
1840+
} else if (nextState == BREAK_WITHOUT_EXTENSION) {
1841+
break withoutExceptions;
18921842
} else {
1893-
frame.pc = iData.longJumps.get(frame.pc);
1894-
}
1895-
if (instructionCounting) {
1896-
frame.pcPrevBranch = frame.pc;
1843+
return (InterpreterResult) nextState;
18971844
}
1898-
continue Loop;
1899-
} // end of Loop: for
1845+
}
19001846

19011847
exitFrame(cx, frame, null);
19021848
if (frame.parentFrame != null) {
@@ -1905,20 +1851,20 @@ private static InterpreterResult interpretFunction(
19051851
newFrame = newFrame.cloneFrozen();
19061852
}
19071853
setCallResult(newFrame, frame.result, frame.resultDbl);
1908-
return new StateContinueResult(newFrame, indexReg);
1854+
return new StateContinueResult(newFrame, state.indexReg);
19091855
}
19101856
return new StateBreakResult(frame);
19111857

19121858
} // end of interpreter withoutExceptions: try
19131859
catch (Throwable ex) {
1914-
if (throwable != null) {
1860+
if (state.throwable != null) {
19151861
// This is serious bug and it is better to track it ASAP
19161862
ex.printStackTrace(System.err);
19171863
throw new IllegalStateException();
19181864
}
1919-
throwable = ex;
1865+
state.throwable = ex;
19201866
}
1921-
return new ThrowableResult(frame, throwable);
1867+
return new ThrowableResult(frame, state.throwable);
19221868
}
19231869

19241870
private static class DoGenerator extends InstructionClass {

0 commit comments

Comments
 (0)