Executable.cpp 27 KB
Newer Older
1
/*
2
 * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

#include "config.h"
#include "Executable.h"

#include "BytecodeGenerator.h"
#include "CodeBlock.h"
31
#include "DFGDriver.h"
32
#include "ExecutionHarness.h"
33
#include "JIT.h"
34
#include "JITDriver.h"
35
#include "Parser.h"
36
#include "UStringBuilder.h"
37
#include "Vector.h"
38 39 40

namespace JSC {

41
const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
42

oliver@apple.com's avatar
oliver@apple.com committed
43
#if ENABLE(JIT)
44 45 46 47
void ExecutableBase::destroy(JSCell* cell)
{
    jsCast<ExecutableBase*>(cell)->ExecutableBase::~ExecutableBase();
}
oliver@apple.com's avatar
oliver@apple.com committed
48
#endif
49

50
inline void ExecutableBase::clearCode()
51 52 53 54 55 56
{
#if ENABLE(JIT)
    m_jitCodeForCall.clear();
    m_jitCodeForConstruct.clear();
    m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
    m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
57
#endif
58 59 60
    m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
    m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
}
61

62
#if ENABLE(DFG_JIT)
63
Intrinsic ExecutableBase::intrinsic() const
64
{
65 66
    if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
        return nativeExecutable->intrinsic();
67
    return NoIntrinsic;
68 69 70
}
#endif

71
const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(NativeExecutable) };
72

oliver@apple.com's avatar
oliver@apple.com committed
73
#if ENABLE(JIT)
74
void NativeExecutable::destroy(JSCell* cell)
75
{
76
    jsCast<NativeExecutable*>(cell)->NativeExecutable::~NativeExecutable();
77
}
oliver@apple.com's avatar
oliver@apple.com committed
78
#endif
79

80
#if ENABLE(DFG_JIT)
81
Intrinsic NativeExecutable::intrinsic() const
82 83 84 85 86
{
    return m_intrinsic;
}
#endif

fpizlo@apple.com's avatar
fpizlo@apple.com committed
87
#if ENABLE(JIT)
88 89 90 91
// Utility method used for jettisoning code blocks.
template<typename T>
static void jettisonCodeBlock(JSGlobalData& globalData, OwnPtr<T>& codeBlock)
{
92
    ASSERT(JITCode::isOptimizingJIT(codeBlock->getJITType()));
93 94 95 96
    ASSERT(codeBlock->alternative());
    OwnPtr<T> codeBlockToJettison = codeBlock.release();
    codeBlock = static_pointer_cast<T>(codeBlockToJettison->releaseAlternative());
    codeBlockToJettison->unlinkIncomingCalls();
97
    globalData.heap.jettisonDFGCodeBlock(static_pointer_cast<CodeBlock>(codeBlockToJettison.release()));
98
}
fpizlo@apple.com's avatar
fpizlo@apple.com committed
99
#endif
100

101 102 103 104 105
void NativeExecutable::finalize(JSCell* cell)
{
    jsCast<NativeExecutable*>(cell)->clearCode();
}

106
const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
107

oliver@apple.com's avatar
oliver@apple.com committed
108
#if ENABLE(JIT)
109 110 111 112
void ScriptExecutable::destroy(JSCell* cell)
{
    jsCast<ScriptExecutable*>(cell)->ScriptExecutable::~ScriptExecutable();
}
oliver@apple.com's avatar
oliver@apple.com committed
113
#endif
114

115
const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(EvalExecutable) };
116

117
EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
118
    : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
119 120 121
{
}

122
void EvalExecutable::destroy(JSCell* cell)
123
{
124
    jsCast<EvalExecutable*>(cell)->EvalExecutable::~EvalExecutable();
125 126
}

127
const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
128

129
ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
130
    : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
131
{
132 133
}

134
void ProgramExecutable::destroy(JSCell* cell)
135
{
136
    jsCast<ProgramExecutable*>(cell)->ProgramExecutable::~ProgramExecutable();
137 138
}

139
const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
140

141
FunctionExecutable::FunctionExecutable(JSGlobalData& globalData, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
142
    : ScriptExecutable(globalData.functionExecutableStructure.get(), globalData, source, inStrictContext)
143
    , m_numCapturedVariables(0)
144 145 146
    , m_forceUsesArguments(forceUsesArguments)
    , m_parameters(parameters)
    , m_name(name)
oliver@apple.com's avatar
oliver@apple.com committed
147
    , m_inferredName(inferredName.isNull() ? globalData.propertyNames->emptyIdentifier : inferredName)
148 149 150 151
    , m_symbolTable(0)
{
}

152
FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
153
    : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext)
154
    , m_numCapturedVariables(0)
155 156 157
    , m_forceUsesArguments(forceUsesArguments)
    , m_parameters(parameters)
    , m_name(name)
158
    , m_inferredName(inferredName.isNull() ? exec->globalData().propertyNames->emptyIdentifier : inferredName)
159 160
    , m_symbolTable(0)
{
161 162
}

163
void FunctionExecutable::destroy(JSCell* cell)
164
{
165
    jsCast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
166 167
}

168 169 170 171 172 173 174 175 176 177
JSObject* EvalExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
{
    ASSERT(exec->globalData().dynamicGlobalObject);
    ASSERT(!!m_evalCodeBlock);
    JSObject* error = 0;
    if (m_evalCodeBlock->getJITType() != JITCode::topTierJIT())
        error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_evalCodeBlock->getJITType()));
    ASSERT(!!m_evalCodeBlock);
    return error;
}
178

179
#if ENABLE(JIT)
180
bool EvalExecutable::jitCompile(JSGlobalData& globalData)
181
{
182
    return jitCompileIfAppropriate(globalData, m_evalCodeBlock, m_jitCodeForCall, JITCode::bottomTierJIT(), JITCompilationCanFail);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
}
#endif

inline const char* samplingDescription(JITCode::JITType jitType)
{
    switch (jitType) {
    case JITCode::InterpreterThunk:
        return "Interpreter Compilation (TOTAL)";
    case JITCode::BaselineJIT:
        return "Baseline Compilation (TOTAL)";
    case JITCode::DFGJIT:
        return "DFG Compilation (TOTAL)";
    default:
        ASSERT_NOT_REACHED();
        return 0;
    }
}

201
JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
202
{
203
    SamplingRegion samplingRegion(samplingDescription(jitType));
204
    
205 206 207
#if !ENABLE(JIT)
    UNUSED_PARAM(jitType);
#endif
208
    JSObject* exception = 0;
209 210
    JSGlobalData* globalData = &exec->globalData();
    JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
211
    
212
    if (!!m_evalCodeBlock) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        OwnPtr<EvalCodeBlock> newCodeBlock = adoptPtr(new EvalCodeBlock(CodeBlock::CopyParsedBlock, *m_evalCodeBlock));
        newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_evalCodeBlock.release()));
        m_evalCodeBlock = newCodeBlock.release();
    } else {
        if (!lexicalGlobalObject->evalEnabled())
            return throwError(exec, createEvalError(exec, "Eval is disabled"));
        RefPtr<EvalNode> evalNode = parse<EvalNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, EvalNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
        if (!evalNode) {
            ASSERT(exception);
            return exception;
        }
        recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
        
        JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
        
        OwnPtr<CodeBlock> previousCodeBlock = m_evalCodeBlock.release();
        ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
        m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth(), previousCodeBlock.release()));
        OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get(), !!m_evalCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
        if ((exception = generator->generate())) {
            m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
            evalNode->destroyData();
            return exception;
        }
        
238
        evalNode->destroyData();
239
        m_evalCodeBlock->copyPostParseDataFromAlternative();
240 241
    }

242
#if ENABLE(JIT)
243
    if (!prepareForExecution(*globalData, m_evalCodeBlock, m_jitCodeForCall, jitType))
244
        return 0;
245 246
#endif

247
#if ENABLE(JIT)
248
#if ENABLE(CLASSIC_INTERPRETER)
249 250 251 252
    if (!m_jitCodeForCall)
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
    else
#endif
253 254 255 256 257
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
#else
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
#endif

258 259 260
    return 0;
}

fpizlo@apple.com's avatar
fpizlo@apple.com committed
261
#if ENABLE(JIT)
262 263 264 265 266 267
void EvalExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
{
    jettisonCodeBlock(globalData, m_evalCodeBlock);
    m_jitCodeForCall = m_evalCodeBlock->getJITCode();
    ASSERT(!m_jitCodeForCallWithArityCheck);
}
fpizlo@apple.com's avatar
fpizlo@apple.com committed
268
#endif
269

270 271
void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
272
    EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
273
    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
274
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
275 276 277 278
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
    ScriptExecutable::visitChildren(thisObject, visitor);
    if (thisObject->m_evalCodeBlock)
        thisObject->m_evalCodeBlock->visitAggregate(visitor);
279 280
}

281 282 283 284 285 286 287 288 289 290
void EvalExecutable::unlinkCalls()
{
#if ENABLE(JIT)
    if (!m_jitCodeForCall)
        return;
    ASSERT(m_evalCodeBlock);
    m_evalCodeBlock->unlinkCalls();
#endif
}

291 292 293 294 295 296
void EvalExecutable::finalize(JSCell* cell)
{
    jsCast<EvalExecutable*>(cell)->clearCode();
}

inline void EvalExecutable::clearCode()
297 298 299 300 301
{
    if (m_evalCodeBlock) {
        m_evalCodeBlock->clearEvalCache();
        m_evalCodeBlock.clear();
    }
302
    Base::clearCode();
303 304
}

305 306
JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
{
307
    JSObject* exception = 0;
308 309
    JSGlobalData* globalData = &exec->globalData();
    JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
310
    RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
311 312 313 314
    if (programNode)
        return 0;
    ASSERT(exception);
    return exception;
315 316
}

317
JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode)
318
{
319 320 321 322 323 324 325 326
    ASSERT(exec->globalData().dynamicGlobalObject);
    ASSERT(!!m_programCodeBlock);
    JSObject* error = 0;
    if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
        error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()));
    ASSERT(!!m_programCodeBlock);
    return error;
}
327

328
#if ENABLE(JIT)
329
bool ProgramExecutable::jitCompile(JSGlobalData& globalData)
330
{
331
    return jitCompileIfAppropriate(globalData, m_programCodeBlock, m_jitCodeForCall, JITCode::bottomTierJIT(), JITCompilationCanFail);
332 333 334
}
#endif

335 336
JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
{
337
    SamplingRegion samplingRegion(samplingDescription(jitType));
338
    
339 340 341
#if !ENABLE(JIT)
    UNUSED_PARAM(jitType);
#endif
342
    JSObject* exception = 0;
343 344
    JSGlobalData* globalData = &exec->globalData();
    JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
345
    
346
    if (!!m_programCodeBlock) {
347 348 349 350 351 352 353 354 355 356
        OwnPtr<ProgramCodeBlock> newCodeBlock = adoptPtr(new ProgramCodeBlock(CodeBlock::CopyParsedBlock, *m_programCodeBlock));
        newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_programCodeBlock.release()));
        m_programCodeBlock = newCodeBlock.release();
    } else {
        RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
        if (!programNode) {
            ASSERT(exception);
            return exception;
        }
        recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
357

358
        JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
359
    
360 361 362 363 364 365 366 367 368 369
        OwnPtr<CodeBlock> previousCodeBlock = m_programCodeBlock.release();
        ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
        m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider(), previousCodeBlock.release()));
        OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get(), !!m_programCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
        if ((exception = generator->generate())) {
            m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
            programNode->destroyData();
            return exception;
        }

370
        programNode->destroyData();
371
        m_programCodeBlock->copyPostParseDataFromAlternative();
372
    }
373

374
#if ENABLE(JIT)
375
    if (!prepareForExecution(*globalData, m_programCodeBlock, m_jitCodeForCall, jitType))
376
        return 0;
377 378
#endif

379
#if ENABLE(JIT)
380
#if ENABLE(CLASSIC_INTERPRETER)
381 382 383 384 385
    if (!m_jitCodeForCall)
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
    else
#endif
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
386 387 388 389 390
#else
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
#endif

    return 0;
391 392
}

fpizlo@apple.com's avatar
fpizlo@apple.com committed
393
#if ENABLE(JIT)
394 395 396 397 398 399
void ProgramExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
{
    jettisonCodeBlock(globalData, m_programCodeBlock);
    m_jitCodeForCall = m_programCodeBlock->getJITCode();
    ASSERT(!m_jitCodeForCallWithArityCheck);
}
fpizlo@apple.com's avatar
fpizlo@apple.com committed
400
#endif
401

402 403 404 405 406 407 408 409 410 411
void ProgramExecutable::unlinkCalls()
{
#if ENABLE(JIT)
    if (!m_jitCodeForCall)
        return;
    ASSERT(m_programCodeBlock);
    m_programCodeBlock->unlinkCalls();
#endif
}

412 413
void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
414
    ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
415
    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
416
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
417 418 419 420
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
    ScriptExecutable::visitChildren(thisObject, visitor);
    if (thisObject->m_programCodeBlock)
        thisObject->m_programCodeBlock->visitAggregate(visitor);
421 422
}

423 424 425 426 427 428
void ProgramExecutable::finalize(JSCell* cell)
{
    jsCast<ProgramExecutable*>(cell)->clearCode();
}

inline void ProgramExecutable::clearCode()
429 430 431 432 433
{
    if (m_programCodeBlock) {
        m_programCodeBlock->clearEvalCache();
        m_programCodeBlock.clear();
    }
434
    Base::clearCode();
435 436
}

437
FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
fpizlo@apple.com's avatar
fpizlo@apple.com committed
438
{
439
    FunctionCodeBlock* result;
fpizlo@apple.com's avatar
fpizlo@apple.com committed
440 441 442 443 444 445 446 447 448
    if (kind == CodeForCall)
        result = m_codeBlockForCall.get();
    else {
        ASSERT(kind == CodeForConstruct);
        result = m_codeBlockForConstruct.get();
    }
    if (!result)
        return 0;
    while (result->alternative())
449
        result = static_cast<FunctionCodeBlock*>(result->alternative());
fpizlo@apple.com's avatar
fpizlo@apple.com committed
450
    ASSERT(result);
451
    ASSERT(JITCode::isBaselineCode(result->getJITType()));
fpizlo@apple.com's avatar
fpizlo@apple.com committed
452 453 454
    return result;
}

455
JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode)
456 457 458 459 460
{
    ASSERT(exec->globalData().dynamicGlobalObject);
    ASSERT(!!m_codeBlockForCall);
    JSObject* error = 0;
    if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
461
        error = compileForCallInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()));
462 463 464 465
    ASSERT(!!m_codeBlockForCall);
    return error;
}

466
JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode)
467 468 469 470 471
{
    ASSERT(exec->globalData().dynamicGlobalObject);
    ASSERT(!!m_codeBlockForConstruct);
    JSObject* error = 0;
    if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
472
        error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()));
473 474 475 476
    ASSERT(!!m_codeBlockForConstruct);
    return error;
}

477
#if ENABLE(JIT)
478
bool FunctionExecutable::jitCompileForCall(JSGlobalData& globalData)
479
{
480
    return jitCompileFunctionIfAppropriate(globalData, m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), JITCompilationCanFail);
481 482
}

483
bool FunctionExecutable::jitCompileForConstruct(JSGlobalData& globalData)
484
{
485
    return jitCompileFunctionIfAppropriate(globalData, m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), JITCompilationCanFail);
486 487 488
}
#endif

489 490
FunctionCodeBlock* FunctionExecutable::codeBlockWithBytecodeFor(CodeSpecializationKind kind)
{
491
    return baselineCodeBlockFor(kind);
492 493
}

fpizlo@apple.com's avatar
fpizlo@apple.com committed
494
PassOwnPtr<FunctionCodeBlock> FunctionExecutable::produceCodeBlockFor(ScopeChainNode* scopeChainNode, CompilationKind compilationKind, CodeSpecializationKind specializationKind, JSObject*& exception)
495
{
496
    if (!!codeBlockFor(specializationKind))
497 498
        return adoptPtr(new FunctionCodeBlock(CodeBlock::CopyParsedBlock, *codeBlockFor(specializationKind)));
    
499
    exception = 0;
500
    JSGlobalData* globalData = scopeChainNode->globalData;
fpizlo@apple.com's avatar
fpizlo@apple.com committed
501
    JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
502 503
    RefPtr<FunctionBodyNode> body = parse<FunctionBodyNode>(globalData, globalObject, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, FunctionBodyNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, 0, 0, &exception);

504 505
    if (!body) {
        ASSERT(exception);
506
        return nullptr;
507
    }
508 509
    if (m_forceUsesArguments)
        body->setUsesArguments();
510
    body->finishParsing(m_parameters, m_name);
511
    recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
512

513 514 515 516 517 518 519 520 521
    OwnPtr<FunctionCodeBlock> result;
    ASSERT((compilationKind == FirstCompilation) == !codeBlockFor(specializationKind));
    result = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), specializationKind == CodeForConstruct));
    OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, result->symbolTable(), result.get(), compilationKind)));
    exception = generator->generate();
    body->destroyData();
    if (exception)
        return nullptr;

522
    result->copyPostParseDataFrom(codeBlockFor(specializationKind).get());
523 524 525
    return result.release();
}

526
JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
527
{
528
    SamplingRegion samplingRegion(samplingDescription(jitType));
529
    
530
#if !ENABLE(JIT)
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
531
    UNUSED_PARAM(exec);
532
    UNUSED_PARAM(jitType);
533
    UNUSED_PARAM(exec);
534 535
#endif
    ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForCall);
536
    JSObject* exception;
fpizlo@apple.com's avatar
fpizlo@apple.com committed
537
    OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForCall ? OptimizingCompilation : FirstCompilation, CodeForCall, exception);
538
    if (!newCodeBlock)
539 540
        return exception;

541 542 543
    newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForCall.release()));
    m_codeBlockForCall = newCodeBlock.release();
    
544
    m_numParametersForCall = m_codeBlockForCall->numParameters();
545
    ASSERT(m_numParametersForCall);
546
    m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
547 548
    m_symbolTable = m_codeBlockForCall->sharedSymbolTable();

549
#if ENABLE(JIT)
550
    if (!prepareFunctionForExecution(exec->globalData(), m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, jitType, CodeForCall))
551
        return 0;
552 553
#endif

554
#if ENABLE(JIT)
555
#if ENABLE(CLASSIC_INTERPRETER)
556 557 558 559 560
    if (!m_jitCodeForCall)
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
    else
#endif
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
561 562 563 564
#else
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
#endif

565
    return 0;
566 567
}

568
JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType)
569
{
570
    SamplingRegion samplingRegion(samplingDescription(jitType));
571
    
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
572
#if !ENABLE(JIT)
573
    UNUSED_PARAM(jitType);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
574 575
    UNUSED_PARAM(exec);
#endif
576
    
577
    ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForConstruct);
578
    JSObject* exception;
fpizlo@apple.com's avatar
fpizlo@apple.com committed
579
    OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForConstruct ? OptimizingCompilation : FirstCompilation, CodeForConstruct, exception);
580
    if (!newCodeBlock)
581 582
        return exception;

583 584 585
    newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForConstruct.release()));
    m_codeBlockForConstruct = newCodeBlock.release();
    
586
    m_numParametersForConstruct = m_codeBlockForConstruct->numParameters();
587
    ASSERT(m_numParametersForConstruct);
588
    m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
589
    m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
590 591

#if ENABLE(JIT)
592
    if (!prepareFunctionForExecution(exec->globalData(), m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, jitType, CodeForConstruct))
593
        return 0;
594
#endif
595

596
#if ENABLE(JIT)
597
#if ENABLE(CLASSIC_INTERPRETER)
598 599 600 601
    if (!m_jitCodeForConstruct)
        Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
    else
#endif
602 603 604 605 606
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
#else
    Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
#endif

607
    return 0;
608 609
}

fpizlo@apple.com's avatar
fpizlo@apple.com committed
610
#if ENABLE(JIT)
611 612 613 614 615 616 617 618 619 620 621 622 623
void FunctionExecutable::jettisonOptimizedCodeForCall(JSGlobalData& globalData)
{
    jettisonCodeBlock(globalData, m_codeBlockForCall);
    m_jitCodeForCall = m_codeBlockForCall->getJITCode();
    m_jitCodeForCallWithArityCheck = m_codeBlockForCall->getJITCodeWithArityCheck();
}

void FunctionExecutable::jettisonOptimizedCodeForConstruct(JSGlobalData& globalData)
{
    jettisonCodeBlock(globalData, m_codeBlockForConstruct);
    m_jitCodeForConstruct = m_codeBlockForConstruct->getJITCode();
    m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->getJITCodeWithArityCheck();
}
fpizlo@apple.com's avatar
fpizlo@apple.com committed
624
#endif
625

626
void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
627
{
628
    FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
629
    ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
630
    COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
631 632 633 634 635 636 637 638
    ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
    ScriptExecutable::visitChildren(thisObject, visitor);
    if (thisObject->m_nameValue)
        visitor.append(&thisObject->m_nameValue);
    if (thisObject->m_codeBlockForCall)
        thisObject->m_codeBlockForCall->visitAggregate(visitor);
    if (thisObject->m_codeBlockForConstruct)
        thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
639 640
}

641
void FunctionExecutable::discardCode()
642
{
643 644 645 646 647 648 649 650 651
#if ENABLE(JIT)
    // These first two checks are to handle the rare case where
    // we are trying to evict code for a function during its
    // codegen.
    if (!m_jitCodeForCall && m_codeBlockForCall)
        return;
    if (!m_jitCodeForConstruct && m_codeBlockForConstruct)
        return;
#endif
652 653 654 655 656 657
    clearCode();
}

void FunctionExecutable::finalize(JSCell* cell)
{
    jsCast<FunctionExecutable*>(cell)->clearCode();
658 659
}

660
inline void FunctionExecutable::clearCode()
661 662
{
    if (m_codeBlockForCall) {
663
        m_codeBlockForCall->clearEvalCache();
664 665 666
        m_codeBlockForCall.clear();
    }
    if (m_codeBlockForConstruct) {
667
        m_codeBlockForConstruct->clearEvalCache();
668 669
        m_codeBlockForConstruct.clear();
    }
670
    Base::clearCode();
671 672 673 674
}

void FunctionExecutable::unlinkCalls()
{
675
#if ENABLE(JIT)
676 677 678 679 680 681 682 683
    if (!!m_jitCodeForCall) {
        ASSERT(m_codeBlockForCall);
        m_codeBlockForCall->unlinkCalls();
    }
    if (!!m_jitCodeForConstruct) {
        ASSERT(m_codeBlockForConstruct);
        m_codeBlockForConstruct->unlinkCalls();
    }
684
#endif
685 686
}

687
FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
688
{
689
    JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
690
    RefPtr<ProgramNode> program = parse<ProgramNode>(&exec->globalData(), lexicalGlobalObject, source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, debugger, exec, exception);
691 692
    if (!program) {
        ASSERT(*exception);
693
        return 0;
694
    }
695

696
    // Uses of this function that would not result in a single function expression are invalid.
697 698 699 700 701 702 703 704
    StatementNode* exprStatement = program->singleStatement();
    ASSERT(exprStatement);
    ASSERT(exprStatement->isExprStatement());
    ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
    ASSERT(funcExpr);
    ASSERT(funcExpr->isFuncExprNode());
    FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
    ASSERT(body);
705

706
    return FunctionExecutable::create(exec->globalData(), functionName, functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
707 708 709 710
}

UString FunctionExecutable::paramString() const
{
711
    FunctionParameters& parameters = *m_parameters;
712
    UStringBuilder builder;
713
    for (size_t pos = 0; pos < parameters.size(); ++pos) {
714 715 716
        if (!builder.isEmpty())
            builder.append(", ");
        builder.append(parameters[pos].ustring());
717
    }
718
    return builder.toUString();
719 720
}

721
}