Package org.mozilla.javascript.optimizer
Class Codegen
java.lang.Object
org.mozilla.javascript.optimizer.Codegen
- All Implemented Interfaces:
Evaluator
This class generates code for a given IR tree.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate CompilerEnvirons
(package private) static final String
private ObjArray
(package private) static final String
(package private) static final String
private static final Object
private static int
(package private) static final String
private double[]
private int
(package private) String
(package private) String
private String
(package private) static final String
(package private) static final String
private ObjToIntMap
(package private) ScriptNode[]
private static final String
(package private) static final String
(package private) static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
(package private) static RuntimeException
badTree()
void
Capture stack information from the given exception.(package private) String
Gets a Java-compatible "informative" name for the the ScriptOrFnNodeprivate static void
compile
(CompilerEnvirons compilerEnv, ScriptNode tree, String encodedSource, boolean returnFunction) Compile the script or function from intermediate representation tree into an executable form.byte[]
compileToClassFile
(CompilerEnvirons compilerEnv, String mainClassName, ScriptNode scriptOrFn, String encodedSource, boolean returnFunction) createFunctionObject
(Context cx, Scriptable scope, Object bytecode, Object staticSecurityDomain) Create a function object.createScriptObject
(Object bytecode, Object staticSecurityDomain) Create a script object.private Class<?>
defineClass
(Object bytecode, Object staticSecurityDomain) private void
private void
private void
private void
Overview:private void
generateCallMethod
(ClassFileWriter cfw, boolean isStrictMode) private byte[]
generateCode
(String encodedSource) private static void
private void
private void
private void
private void
generateNativeFunctionOverrides
(ClassFileWriter cfw, String encodedSource) private void
private static void
(package private) String
(package private) String
(package private) String
getCompiledRegexpName
(ScriptNode n, int regexpIndex) (package private) String
(package private) String
(package private) int
getPatchedStack
(RhinoException ex, String nativeStackTrace) Given a native stack trace, patch it with script-specific source and line informationGet the script stack for the given exceptiongetSourcePositionFromStack
(Context cx, int[] linep) Get the source position information by examining the stack.private static String
getStaticConstantWrapperType
(double num) (package private) String
private static void
initOptFunctions_r
(ScriptNode scriptOrFn) private void
initScriptNodesData
(ScriptNode scriptOrFn) (package private) static boolean
isGenerator
(ScriptNode node) (package private) void
pushNumberAsObject
(ClassFileWriter cfw, double num) (package private) static void
void
setEvalScriptFlag
(Script script) Mark the given script to indicate it was created by a call to eval() or to a Function constructor.void
setMainMethodClass
(String className) private void
transform
(ScriptNode tree)
-
Field Details
-
DEFAULT_MAIN_METHOD_CLASS
- See Also:
-
SUPER_CLASS_NAME
- See Also:
-
ID_FIELD_NAME
- See Also:
-
REGEXP_INIT_METHOD_NAME
- See Also:
-
REGEXP_INIT_METHOD_SIGNATURE
- See Also:
-
TEMPLATE_LITERAL_INIT_METHOD_NAME
- See Also:
-
TEMPLATE_LITERAL_INIT_METHOD_SIGNATURE
- See Also:
-
FUNCTION_INIT_SIGNATURE
- See Also:
-
FUNCTION_CONSTRUCTOR_SIGNATURE
- See Also:
-
globalLock
-
globalSerialClassCounter
private static int globalSerialClassCounter -
compilerEnv
-
directCallTargets
-
scriptOrFnNodes
ScriptNode[] scriptOrFnNodes -
scriptOrFnIndexes
-
mainMethodClass
-
mainClassName
String mainClassName -
mainClassSignature
String mainClassSignature -
itsConstantList
private double[] itsConstantList -
itsConstantListSize
private int itsConstantListSize
-
-
Constructor Details
-
Codegen
public Codegen()
-
-
Method Details
-
captureStackInfo
Description copied from interface:Evaluator
Capture stack information from the given exception.- Specified by:
captureStackInfo
in interfaceEvaluator
- Parameters:
ex
- an exception thrown during execution
-
getSourcePositionFromStack
Description copied from interface:Evaluator
Get the source position information by examining the stack.- Specified by:
getSourcePositionFromStack
in interfaceEvaluator
- Parameters:
cx
- Contextlinep
- Array object of length >= 1; getSourcePositionFromStack will assign the line number to linep[0].- Returns:
- the name of the file or other source container
-
getPatchedStack
Description copied from interface:Evaluator
Given a native stack trace, patch it with script-specific source and line information- Specified by:
getPatchedStack
in interfaceEvaluator
- Parameters:
ex
- exceptionnativeStackTrace
- the native stack trace- Returns:
- patched stack trace
-
getScriptStack
Description copied from interface:Evaluator
Get the script stack for the given exception- Specified by:
getScriptStack
in interfaceEvaluator
- Parameters:
ex
- exception from execution- Returns:
- list of strings for the stack trace
-
setEvalScriptFlag
Description copied from interface:Evaluator
Mark the given script to indicate it was created by a call to eval() or to a Function constructor.- Specified by:
setEvalScriptFlag
in interfaceEvaluator
- Parameters:
script
- script to mark as from eval
-
compile
public Object compile(CompilerEnvirons compilerEnv, ScriptNode tree, String encodedSource, boolean returnFunction) Description copied from interface:Evaluator
Compile the script or function from intermediate representation tree into an executable form.- Specified by:
compile
in interfaceEvaluator
- Parameters:
compilerEnv
- Compiler environmenttree
- parse treeencodedSource
- encoding of the source code for decompilationreturnFunction
- if true, compiling a function- Returns:
- an opaque object that can be passed to either createFunctionObject or createScriptObject, depending on the value of returnFunction
-
createScriptObject
Description copied from interface:Evaluator
Create a script object.- Specified by:
createScriptObject
in interfaceEvaluator
- Parameters:
bytecode
- opaque object returned by compilestaticSecurityDomain
- security domain- Returns:
- Script object that can be evaluated
-
createFunctionObject
public Function createFunctionObject(Context cx, Scriptable scope, Object bytecode, Object staticSecurityDomain) Description copied from interface:Evaluator
Create a function object.- Specified by:
createFunctionObject
in interfaceEvaluator
- Parameters:
cx
- Current contextscope
- scope of the functionbytecode
- opaque object returned by compilestaticSecurityDomain
- security domain- Returns:
- Function object that can be called
-
defineClass
-
compileToClassFile
public byte[] compileToClassFile(CompilerEnvirons compilerEnv, String mainClassName, ScriptNode scriptOrFn, String encodedSource, boolean returnFunction) -
transform
-
initOptFunctions_r
-
initScriptNodesData
-
collectScriptNodes_r
-
generateCode
-
emitDirectConstructor
-
isGenerator
-
generateResumeGenerator
-
generateCallMethod
-
generateMain
-
generateExecute
-
generateScriptCtor
-
generateFunctionConstructor
-
generateFunctionInit
-
generateNativeFunctionOverrides
-
emitRegExpInit
-
emitTemplateLiteralInit
Overview:for each fn in functions(script) do let field = [] for each templateLiteral in templateLiterals(fn) do let values = concat([[cooked(s), raw(s)] | s <- strings(templateLiteral)]) field.push(values) end class[getTemplateLiteralName(fn)] = field end
-
emitConstantDudeInitializers
-
pushNumberAsObject
-
addDoubleWrap
-
getStaticConstantWrapperType
-
pushUndefined
-
getIndex
-
getDirectCtorName
-
getBodyMethodName
-
cleanName
Gets a Java-compatible "informative" name for the the ScriptOrFnNode -
getBodyMethodSignature
-
getFunctionInitMethodName
-
getCompiledRegexpName
-
getTemplateLiteralName
-
badTree
-
setMainMethodClass
-