#ifndef JITDriver_h
#define JITDriver_h
#include <wtf/Platform.h>
#if ENABLE(JIT)
#include "BytecodeGenerator.h"
#include "DFGDriver.h"
#include "JIT.h"
#include "LLIntEntrypoints.h"
namespace JSC {
template<typename CodeBlockType>
inline bool jitCompileIfAppropriate(ExecState* exec, OwnPtr<CodeBlockType>& codeBlock, JITCode& jitCode, JITCode::JITType jitType, unsigned bytecodeIndex, JITCompilationEffort effort)
{
VM& vm = exec->vm();
if (jitType == codeBlock->getJITType())
return true;
if (!vm.canUseJIT())
return true;
codeBlock->unlinkIncomingCalls();
JITCode oldJITCode = jitCode;
bool dfgCompiled = false;
if (jitType == JITCode::DFGJIT)
dfgCompiled = DFG::tryCompile(exec, codeBlock.get(), jitCode, bytecodeIndex);
if (dfgCompiled) {
if (codeBlock->alternative())
codeBlock->alternative()->unlinkIncomingCalls();
} else {
if (codeBlock->alternative()) {
codeBlock = static_pointer_cast<CodeBlockType>(codeBlock->releaseAlternative());
jitCode = oldJITCode;
return false;
}
jitCode = JIT::compile(&vm, codeBlock.get(), effort);
if (!jitCode) {
jitCode = oldJITCode;
return false;
}
}
codeBlock->setJITCode(jitCode, MacroAssemblerCodePtr());
return true;
}
inline bool jitCompileFunctionIfAppropriate(ExecState* exec, OwnPtr<FunctionCodeBlock>& codeBlock, JITCode& jitCode, MacroAssemblerCodePtr& jitCodeWithArityCheck, JITCode::JITType jitType, unsigned bytecodeIndex, JITCompilationEffort effort)
{
VM& vm = exec->vm();
if (jitType == codeBlock->getJITType())
return true;
if (!vm.canUseJIT())
return true;
codeBlock->unlinkIncomingCalls();
JITCode oldJITCode = jitCode;
MacroAssemblerCodePtr oldJITCodeWithArityCheck = jitCodeWithArityCheck;
bool dfgCompiled = false;
if (jitType == JITCode::DFGJIT)
dfgCompiled = DFG::tryCompileFunction(exec, codeBlock.get(), jitCode, jitCodeWithArityCheck, bytecodeIndex);
if (dfgCompiled) {
if (codeBlock->alternative())
codeBlock->alternative()->unlinkIncomingCalls();
} else {
if (codeBlock->alternative()) {
codeBlock = static_pointer_cast<FunctionCodeBlock>(codeBlock->releaseAlternative());
jitCode = oldJITCode;
jitCodeWithArityCheck = oldJITCodeWithArityCheck;
return false;
}
jitCode = JIT::compile(&vm, codeBlock.get(), effort, &jitCodeWithArityCheck);
if (!jitCode) {
jitCode = oldJITCode;
jitCodeWithArityCheck = oldJITCodeWithArityCheck;
return false;
}
}
codeBlock->setJITCode(jitCode, jitCodeWithArityCheck);
return true;
}
}
#endif // ENABLE(JIT)
#endif // JITDriver_h