Class BodyCodegen
- java.lang.Object
-
- org.htmlunit.corejs.javascript.optimizer.BodyCodegen
-
class BodyCodegen extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classBodyCodegen.ExceptionManagerManages placement of exception handlers for non-generator functions.(package private) static classBodyCodegen.FinallyReturnPoint
-
Field Summary
Fields Modifier and Type Field Description private shortargsLocal(package private) ClassFileWritercfw(package private) Codegencodegen(package private) CompilerEnvironscompilerEnvprivate shortcontextLocalprivate static intECMAERROR_EXCEPTIONprivate intenterAreaStartLabelprivate intepilogueLabelprivate static intEVALUATOR_EXCEPTIONprivate static intEXCEPTION_MAXprivate BodyCodegen.ExceptionManagerexceptionManagerprivate static intFINALLY_EXCEPTIONprivate java.util.Map<Node,BodyCodegen.FinallyReturnPoint>finallysprivate shortfirstFreeLocalprivate OptFunctionNodefnCurrentprivate shortfunObjLocal(package private) static intGENERATOR_START(package private) static intGENERATOR_TERMINATE(package private) static intGENERATOR_YIELD_STARTprivate shortgeneratorStateLocalprivate intgeneratorSwitchprivate booleanhasVarsInRegsprivate booleaninDirectCallFunctionprivate booleaninLocalBlockprivate booleanisGeneratorprivate booleanitsForcedObjectParametersprivate intitsLineNumberprivate shortitsOneArgArrayprivate shortitsZeroArgArrayprivate static intJAVASCRIPT_EXCEPTIONprivate java.util.List<Node>literalsprivate int[]localsprivate shortlocalsMaxprivate static intMAX_LOCALSprivate intmaxLocalsprivate intmaxStackprivate shortoperationLocalprivate shortpopvLocalprivate intsavedCodeOffset(package private) ScriptNodescriptOrFnintscriptOrFnIndexprivate shortthisObjLocalprivate static intTHROWABLE_EXCEPTIONprivate intunnestedYieldCountprivate java.util.IdentityHashMap<Node,java.lang.String>unnestedYieldsprivate shortvariableObjectLocalprivate short[]varRegisters
-
Constructor Summary
Constructors Constructor Description BodyCodegen()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddDoubleWrap()private voidaddGoto(Node target, int jumpcode)private voidaddGotoWithReturn(Node target)private voidaddInstructionCount()Generate calls to ScriptRuntime.addInstructionCount to keep track of executed instructions and callobserveInstructionCount()if a threshold is exceeded.
Calculates the count from getCurrentCodeOffset - savedCodeOffsetprivate voidaddInstructionCount(int count)Generate calls to ScriptRuntime.addInstructionCount to keep track of executed instructions and callobserveInstructionCount()if a threshold is exceeded.
Takes the count as a parameter - used to add monitoring to loops and other blocks that don't have any ops - this allows for monitoring/killing of while(true) loops and such.private voidaddJumpedBooleanWrap(int trueLabel, int falseLabel)private voidaddLoadPropertyIds(java.lang.Object[] properties, int count)load array with property idsprivate voidaddLoadPropertyValues(Node node, Node child, int count)load array with property valuesprivate voidaddNewObjectArray(int size)private voidaddObjectToDouble()private voidaddObjectToNumeric()private voidaddOptRuntimeInvoke(java.lang.String methodName, java.lang.String methodSignature)private voidaddScriptRuntimeInvoke(java.lang.String methodName, java.lang.String methodSignature)private voiddcpLoadAsNumber(int dcp_register)private voiddcpLoadAsObject(int dcp_register)private voiddecReferenceWordLocal(short local)private static java.lang.StringexceptionTypeToName(int exceptionType)private NodefindNestedYield(Node node)private voidgenerateActivationExit()private voidgenerateArrayLiteralFactory(Node node, int count)(package private) voidgenerateBodyCode()private voidgenerateCallArgArray(Node node, Node argChild, boolean directCall)private voidgenerateCatchBlock(int exceptionType, short savedVariableObject, int catchLabel, int exceptionLocal, int handler)private voidgenerateCheckForThrowOrClose(int label, boolean hasLocals, int nextState)private voidgenerateEpilogue()private voidgenerateExpression(Node node, Node parent)private voidgenerateFunctionAndThisObj(Node node, Node parent)private voidgenerateGenerator()private voidgenerateGetGeneratorLocalsState()private voidgenerateGetGeneratorResumptionPoint()private voidgenerateGetGeneratorStackState()private voidgenerateIfJump(Node node, Node parent, int trueLabel, int falseLabel)private voidgenerateIntegerUnwrap()private voidgenerateIntegerWrap()private voidgenerateLocalYieldPoint(Node node, boolean exprContext)private voidgenerateNestedFunctionInits()private voidgenerateObjectLiteralFactory(Node node, int count)private voidgeneratePrologue()Generate the prologue for a function or script.private booleangenerateSaveLocals(Node node)private voidgenerateSetGeneratorResumptionPoint(int nextState)private voidgenerateSetGeneratorReturnValue()private voidgenerateStatement(Node node)private voidgenerateThrowJavaScriptException()private voidgenerateYieldPoint(Node node, boolean exprContext)private voidgenSimpleCompare(int type, int trueGOTO, int falseGOTO)private static NodegetFinallyAtTarget(Node node)Get a FINALLY node at a point in the IR.private static intgetLocalBlockRegister(Node node)private shortgetNewWordIntern(int count)private shortgetNewWordLocal()private shortgetNewWordLocal(boolean isConst)private shortgetNewWordPairLocal(boolean isConst)Const locals use an extra slot to hold the has-been-assigned-once flag at runtime.private intgetNextGeneratorState(Node node)private intgetTargetLabel(Node target)private voidincReferenceWordLocal(short local)private voidinitBodyGeneration()private voidinlineFinally(Node finallyTarget)private voidinlineFinally(Node finallyTarget, int finallyStart, int finallyEnd)Inline a FINALLY node into the method bytecode.private static booleanisArithmeticNode(Node node)private intnodeIsDirectCallParameter(Node node)private voidreleaseWordLocal(short local)private voidsaveCurrentCodeOffset()Save the current code offset.private voidupdateLineNumber(Node node)private booleanvarIsDirectCallParameter(int varIndex)private voidvisitArithmetic(Node node, int type, Node child, Node parent)private voidvisitArrayLiteral(Node node, Node child, boolean topLevel)private voidvisitBitNot(Node node, Node child)private voidvisitBitOp(Node node, int type, Node child)private voidvisitDotQuery(Node node, Node child)private voidvisitExponentiation(Node node, Node child, Node parent)private voidvisitFunction(OptFunctionNode ofn, int functionType)private voidvisitGetProp(Node node, Node child)private voidvisitGetVar(Node node)private voidvisitGoto(Jump node, int type, Node child)private voidvisitIfJumpEqOp(Node node, Node child, int trueGOTO, int falseGOTO)private voidvisitIfJumpRelOp(Node node, Node child, int trueGOTO, int falseGOTO)private voidvisitIncDec(Node node)private voidvisitObjectLiteral(Node node, Node child, boolean topLevel)private voidvisitOptimizedCall(Node node, OptFunctionNode target, int type, Node child)private voidvisitSetConst(Node node, Node child)private voidvisitSetConstVar(Node node, Node child, boolean needValue)private voidvisitSetElem(int type, Node node, Node child)private voidvisitSetName(Node node, Node child)private voidvisitSetProp(int type, Node node, Node child)private voidvisitSetVar(Node node, Node child, boolean needValue)private voidvisitSpecialCall(Node node, int type, int specialType, Node child)private voidvisitStandardCall(Node node, Node child)private voidvisitStandardNew(Node node, Node child)private voidvisitStrictSetName(Node node, Node child)private voidvisitSwitch(Jump switchNode, Node child)private voidvisitTemplateLiteral(Node node)private voidvisitTryCatchFinally(Jump node, Node child)private voidvisitTypeofname(Node node)
-
-
-
Field Detail
-
JAVASCRIPT_EXCEPTION
private static final int JAVASCRIPT_EXCEPTION
- See Also:
- Constant Field Values
-
EVALUATOR_EXCEPTION
private static final int EVALUATOR_EXCEPTION
- See Also:
- Constant Field Values
-
ECMAERROR_EXCEPTION
private static final int ECMAERROR_EXCEPTION
- See Also:
- Constant Field Values
-
THROWABLE_EXCEPTION
private static final int THROWABLE_EXCEPTION
- See Also:
- Constant Field Values
-
FINALLY_EXCEPTION
private static final int FINALLY_EXCEPTION
- See Also:
- Constant Field Values
-
EXCEPTION_MAX
private static final int EXCEPTION_MAX
- See Also:
- Constant Field Values
-
exceptionManager
private BodyCodegen.ExceptionManager exceptionManager
-
GENERATOR_TERMINATE
static final int GENERATOR_TERMINATE
- See Also:
- Constant Field Values
-
GENERATOR_START
static final int GENERATOR_START
- See Also:
- Constant Field Values
-
GENERATOR_YIELD_START
static final int GENERATOR_YIELD_START
- See Also:
- Constant Field Values
-
cfw
ClassFileWriter cfw
-
codegen
Codegen codegen
-
compilerEnv
CompilerEnvirons compilerEnv
-
scriptOrFn
ScriptNode scriptOrFn
-
scriptOrFnIndex
public int scriptOrFnIndex
-
savedCodeOffset
private int savedCodeOffset
-
fnCurrent
private OptFunctionNode fnCurrent
-
MAX_LOCALS
private static final int MAX_LOCALS
- See Also:
- Constant Field Values
-
locals
private int[] locals
-
firstFreeLocal
private short firstFreeLocal
-
localsMax
private short localsMax
-
itsLineNumber
private int itsLineNumber
-
hasVarsInRegs
private boolean hasVarsInRegs
-
varRegisters
private short[] varRegisters
-
inDirectCallFunction
private boolean inDirectCallFunction
-
itsForcedObjectParameters
private boolean itsForcedObjectParameters
-
enterAreaStartLabel
private int enterAreaStartLabel
-
epilogueLabel
private int epilogueLabel
-
inLocalBlock
private boolean inLocalBlock
-
variableObjectLocal
private short variableObjectLocal
-
popvLocal
private short popvLocal
-
contextLocal
private short contextLocal
-
argsLocal
private short argsLocal
-
operationLocal
private short operationLocal
-
thisObjLocal
private short thisObjLocal
-
funObjLocal
private short funObjLocal
-
itsZeroArgArray
private short itsZeroArgArray
-
itsOneArgArray
private short itsOneArgArray
-
generatorStateLocal
private short generatorStateLocal
-
isGenerator
private boolean isGenerator
-
generatorSwitch
private int generatorSwitch
-
maxLocals
private int maxLocals
-
maxStack
private int maxStack
-
finallys
private java.util.Map<Node,BodyCodegen.FinallyReturnPoint> finallys
-
literals
private java.util.List<Node> literals
-
unnestedYieldCount
private int unnestedYieldCount
-
unnestedYields
private java.util.IdentityHashMap<Node,java.lang.String> unnestedYields
-
-
Method Detail
-
generateBodyCode
void generateBodyCode()
-
generateGenerator
private void generateGenerator()
-
generateNestedFunctionInits
private void generateNestedFunctionInits()
-
initBodyGeneration
private void initBodyGeneration()
-
generatePrologue
private void generatePrologue()
Generate the prologue for a function or script.
-
generateGetGeneratorResumptionPoint
private void generateGetGeneratorResumptionPoint()
-
generateSetGeneratorResumptionPoint
private void generateSetGeneratorResumptionPoint(int nextState)
-
generateGetGeneratorStackState
private void generateGetGeneratorStackState()
-
generateEpilogue
private void generateEpilogue()
-
generateGetGeneratorLocalsState
private void generateGetGeneratorLocalsState()
-
generateSetGeneratorReturnValue
private void generateSetGeneratorReturnValue()
-
generateActivationExit
private void generateActivationExit()
-
generateStatement
private void generateStatement(Node node)
-
generateIntegerWrap
private void generateIntegerWrap()
-
generateIntegerUnwrap
private void generateIntegerUnwrap()
-
generateThrowJavaScriptException
private void generateThrowJavaScriptException()
-
getNextGeneratorState
private int getNextGeneratorState(Node node)
-
generateYieldPoint
private void generateYieldPoint(Node node, boolean exprContext)
-
generateLocalYieldPoint
private void generateLocalYieldPoint(Node node, boolean exprContext)
-
generateCheckForThrowOrClose
private void generateCheckForThrowOrClose(int label, boolean hasLocals, int nextState)
-
visitTemplateLiteral
private void visitTemplateLiteral(Node node)
-
visitFunction
private void visitFunction(OptFunctionNode ofn, int functionType)
-
getTargetLabel
private int getTargetLabel(Node target)
-
addGotoWithReturn
private void addGotoWithReturn(Node target)
-
generateArrayLiteralFactory
private void generateArrayLiteralFactory(Node node, int count)
-
generateObjectLiteralFactory
private void generateObjectLiteralFactory(Node node, int count)
-
addLoadPropertyIds
private void addLoadPropertyIds(java.lang.Object[] properties, int count)load array with property ids
-
addLoadPropertyValues
private void addLoadPropertyValues(Node node, Node child, int count)
load array with property values
-
visitOptimizedCall
private void visitOptimizedCall(Node node, OptFunctionNode target, int type, Node child)
-
generateCallArgArray
private void generateCallArgArray(Node node, Node argChild, boolean directCall)
-
updateLineNumber
private void updateLineNumber(Node node)
-
generateCatchBlock
private void generateCatchBlock(int exceptionType, short savedVariableObject, int catchLabel, int exceptionLocal, int handler)
-
exceptionTypeToName
private static java.lang.String exceptionTypeToName(int exceptionType)
-
inlineFinally
private void inlineFinally(Node finallyTarget, int finallyStart, int finallyEnd)
Inline a FINALLY node into the method bytecode.This method takes a label that points to the real start of the finally block as implemented in the bytecode. This is because in some cases, the finally block really starts before any of the code in the Node. For example, the catch-all-rethrow finally block has a few instructions prior to the finally block made by the user.
In addition, an end label that should be unmarked is given as a method parameter. It is the responsibility of any callers of this method to mark the label.
The start and end labels of the finally block are used to exclude the inlined block from the proper exception handler. For example, an inlined finally block should not be handled by a catch-all-rethrow.
- Parameters:
finallyTarget- a TARGET node directly preceding a FINALLY node or a FINALLY node itselffinallyStart- a pre-marked label that indicates the actual start of the finally block in the bytecode.finallyEnd- an unmarked label that will indicate the actual end of the finally block in the bytecode.
-
inlineFinally
private void inlineFinally(Node finallyTarget)
-
getFinallyAtTarget
private static Node getFinallyAtTarget(Node node)
Get a FINALLY node at a point in the IR.This is strongly dependent on the generated IR. If the node is a TARGET, it only check the next node to see if it is a FINALLY node.
-
generateSaveLocals
private boolean generateSaveLocals(Node node)
-
visitTypeofname
private void visitTypeofname(Node node)
-
saveCurrentCodeOffset
private void saveCurrentCodeOffset()
Save the current code offset. This saved code offset is used to compute instruction counts in subsequent calls toaddInstructionCount().
-
addInstructionCount
private void addInstructionCount()
Generate calls to ScriptRuntime.addInstructionCount to keep track of executed instructions and callobserveInstructionCount()if a threshold is exceeded.
Calculates the count from getCurrentCodeOffset - savedCodeOffset
-
addInstructionCount
private void addInstructionCount(int count)
Generate calls to ScriptRuntime.addInstructionCount to keep track of executed instructions and callobserveInstructionCount()if a threshold is exceeded.
Takes the count as a parameter - used to add monitoring to loops and other blocks that don't have any ops - this allows for monitoring/killing of while(true) loops and such.
-
visitIncDec
private void visitIncDec(Node node)
-
isArithmeticNode
private static boolean isArithmeticNode(Node node)
-
nodeIsDirectCallParameter
private int nodeIsDirectCallParameter(Node node)
-
varIsDirectCallParameter
private boolean varIsDirectCallParameter(int varIndex)
-
genSimpleCompare
private void genSimpleCompare(int type, int trueGOTO, int falseGOTO)
-
visitGetVar
private void visitGetVar(Node node)
-
getLocalBlockRegister
private static int getLocalBlockRegister(Node node)
-
dcpLoadAsNumber
private void dcpLoadAsNumber(int dcp_register)
-
dcpLoadAsObject
private void dcpLoadAsObject(int dcp_register)
-
addGoto
private void addGoto(Node target, int jumpcode)
-
addObjectToDouble
private void addObjectToDouble()
-
addObjectToNumeric
private void addObjectToNumeric()
-
addNewObjectArray
private void addNewObjectArray(int size)
-
addScriptRuntimeInvoke
private void addScriptRuntimeInvoke(java.lang.String methodName, java.lang.String methodSignature)
-
addOptRuntimeInvoke
private void addOptRuntimeInvoke(java.lang.String methodName, java.lang.String methodSignature)
-
addJumpedBooleanWrap
private void addJumpedBooleanWrap(int trueLabel, int falseLabel)
-
addDoubleWrap
private void addDoubleWrap()
-
getNewWordPairLocal
private short getNewWordPairLocal(boolean isConst)
Const locals use an extra slot to hold the has-been-assigned-once flag at runtime.- Parameters:
isConst- true iff the variable is const- Returns:
- the register for the word pair (double/long)
-
getNewWordLocal
private short getNewWordLocal(boolean isConst)
-
getNewWordLocal
private short getNewWordLocal()
-
getNewWordIntern
private short getNewWordIntern(int count)
-
incReferenceWordLocal
private void incReferenceWordLocal(short local)
-
decReferenceWordLocal
private void decReferenceWordLocal(short local)
-
releaseWordLocal
private void releaseWordLocal(short local)
-
-