b54e9b0532809919753dfc23cd14098b356f40d0
[WebKit-https.git] / Source / JavaScriptCore / runtime / Executable.cpp
1 /*
2  * Copyright (C) 2009, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "Executable.h"
28
29 #include "BytecodeGenerator.h"
30 #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 <wtf/Vector.h>
38
39 namespace JSC {
40
41 const ClassInfo ExecutableBase::s_info = { "Executable", 0, 0, 0, CREATE_METHOD_TABLE(ExecutableBase) };
42
43 #if ENABLE(JIT)
44 void ExecutableBase::destroy(JSCell* cell)
45 {
46     static_cast<ExecutableBase*>(cell)->ExecutableBase::~ExecutableBase();
47 }
48 #endif
49
50 void ExecutableBase::clearCode()
51 {
52 #if ENABLE(JIT)
53     m_jitCodeForCall.clear();
54     m_jitCodeForConstruct.clear();
55     m_jitCodeForCallWithArityCheck = MacroAssemblerCodePtr();
56     m_jitCodeForConstructWithArityCheck = MacroAssemblerCodePtr();
57 #endif
58     m_numParametersForCall = NUM_PARAMETERS_NOT_COMPILED;
59     m_numParametersForConstruct = NUM_PARAMETERS_NOT_COMPILED;
60 }
61
62 #if ENABLE(DFG_JIT)
63 Intrinsic ExecutableBase::intrinsic() const
64 {
65     if (const NativeExecutable* nativeExecutable = jsDynamicCast<const NativeExecutable*>(this))
66         return nativeExecutable->intrinsic();
67     return NoIntrinsic;
68 }
69 #endif
70
71 const ClassInfo NativeExecutable::s_info = { "NativeExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(NativeExecutable) };
72
73 #if ENABLE(JIT)
74 void NativeExecutable::destroy(JSCell* cell)
75 {
76     static_cast<NativeExecutable*>(cell)->NativeExecutable::~NativeExecutable();
77 }
78 #endif
79
80 #if ENABLE(DFG_JIT)
81 Intrinsic NativeExecutable::intrinsic() const
82 {
83     return m_intrinsic;
84 }
85 #endif
86
87 #if ENABLE(JIT)
88 // Utility method used for jettisoning code blocks.
89 template<typename T>
90 static void jettisonCodeBlock(JSGlobalData& globalData, OwnPtr<T>& codeBlock)
91 {
92     ASSERT(JITCode::isOptimizingJIT(codeBlock->getJITType()));
93     ASSERT(codeBlock->alternative());
94     OwnPtr<T> codeBlockToJettison = codeBlock.release();
95     codeBlock = static_pointer_cast<T>(codeBlockToJettison->releaseAlternative());
96     codeBlockToJettison->unlinkIncomingCalls();
97     globalData.heap.jettisonDFGCodeBlock(static_pointer_cast<CodeBlock>(codeBlockToJettison.release()));
98 }
99 #endif
100
101 const ClassInfo ScriptExecutable::s_info = { "ScriptExecutable", &ExecutableBase::s_info, 0, 0, CREATE_METHOD_TABLE(ScriptExecutable) };
102
103 #if ENABLE(JIT)
104 void ScriptExecutable::destroy(JSCell* cell)
105 {
106     static_cast<ScriptExecutable*>(cell)->ScriptExecutable::~ScriptExecutable();
107 }
108 #endif
109
110 const ClassInfo EvalExecutable::s_info = { "EvalExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(EvalExecutable) };
111
112 EvalExecutable::EvalExecutable(ExecState* exec, const SourceCode& source, bool inStrictContext)
113     : ScriptExecutable(exec->globalData().evalExecutableStructure.get(), exec, source, inStrictContext)
114 {
115 }
116
117 void EvalExecutable::destroy(JSCell* cell)
118 {
119     static_cast<EvalExecutable*>(cell)->EvalExecutable::~EvalExecutable();
120 }
121
122 const ClassInfo ProgramExecutable::s_info = { "ProgramExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(ProgramExecutable) };
123
124 ProgramExecutable::ProgramExecutable(ExecState* exec, const SourceCode& source)
125     : ScriptExecutable(exec->globalData().programExecutableStructure.get(), exec, source, false)
126 {
127 }
128
129 void ProgramExecutable::destroy(JSCell* cell)
130 {
131     static_cast<ProgramExecutable*>(cell)->ProgramExecutable::~ProgramExecutable();
132 }
133
134 const ClassInfo FunctionExecutable::s_info = { "FunctionExecutable", &ScriptExecutable::s_info, 0, 0, CREATE_METHOD_TABLE(FunctionExecutable) };
135
136 FunctionExecutable::FunctionExecutable(JSGlobalData& globalData, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
137     : ScriptExecutable(globalData.functionExecutableStructure.get(), globalData, source, inStrictContext)
138     , m_numCapturedVariables(0)
139     , m_forceUsesArguments(forceUsesArguments)
140     , m_parameters(parameters)
141     , m_name(name)
142     , m_inferredName(inferredName.isNull() ? globalData.propertyNames->emptyIdentifier : inferredName)
143     , m_symbolTable(0)
144 {
145 }
146
147 FunctionExecutable::FunctionExecutable(ExecState* exec, const Identifier& name, const Identifier& inferredName, const SourceCode& source, bool forceUsesArguments, FunctionParameters* parameters, bool inStrictContext)
148     : ScriptExecutable(exec->globalData().functionExecutableStructure.get(), exec, source, inStrictContext)
149     , m_numCapturedVariables(0)
150     , m_forceUsesArguments(forceUsesArguments)
151     , m_parameters(parameters)
152     , m_name(name)
153     , m_inferredName(inferredName.isNull() ? exec->globalData().propertyNames->emptyIdentifier : inferredName)
154     , m_symbolTable(0)
155 {
156 }
157
158 void FunctionExecutable::destroy(JSCell* cell)
159 {
160     static_cast<FunctionExecutable*>(cell)->FunctionExecutable::~FunctionExecutable();
161 }
162
163 JSObject* EvalExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
164 {
165     ASSERT(exec->globalData().dynamicGlobalObject);
166     ASSERT(!!m_evalCodeBlock);
167     JSObject* error = 0;
168     if (m_evalCodeBlock->getJITType() != JITCode::topTierJIT())
169         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_evalCodeBlock->getJITType()), bytecodeIndex);
170     ASSERT(!!m_evalCodeBlock);
171     return error;
172 }
173
174 #if ENABLE(JIT)
175 bool EvalExecutable::jitCompile(ExecState* exec)
176 {
177     return jitCompileIfAppropriate(exec, m_evalCodeBlock, m_jitCodeForCall, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
178 }
179 #endif
180
181 inline const char* samplingDescription(JITCode::JITType jitType)
182 {
183     switch (jitType) {
184     case JITCode::InterpreterThunk:
185         return "Interpreter Compilation (TOTAL)";
186     case JITCode::BaselineJIT:
187         return "Baseline Compilation (TOTAL)";
188     case JITCode::DFGJIT:
189         return "DFG Compilation (TOTAL)";
190     default:
191         ASSERT_NOT_REACHED();
192         return 0;
193     }
194 }
195
196 JSObject* EvalExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
197 {
198     SamplingRegion samplingRegion(samplingDescription(jitType));
199     
200 #if !ENABLE(JIT)
201     UNUSED_PARAM(jitType);
202 #endif
203     JSObject* exception = 0;
204     JSGlobalData* globalData = &exec->globalData();
205     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
206     
207     if (!!m_evalCodeBlock) {
208         OwnPtr<EvalCodeBlock> newCodeBlock = adoptPtr(new EvalCodeBlock(CodeBlock::CopyParsedBlock, *m_evalCodeBlock));
209         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_evalCodeBlock.release()));
210         m_evalCodeBlock = newCodeBlock.release();
211     } else {
212         if (!lexicalGlobalObject->evalEnabled())
213             return throwError(exec, createEvalError(exec, "Eval is disabled"));
214         RefPtr<EvalNode> evalNode = parse<EvalNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, EvalNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
215         if (!evalNode) {
216             ASSERT(exception);
217             return exception;
218         }
219         recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
220         
221         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
222         
223         OwnPtr<CodeBlock> previousCodeBlock = m_evalCodeBlock.release();
224         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
225         m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth(), previousCodeBlock.release()));
226         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get(), !!m_evalCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
227         if ((exception = generator->generate())) {
228             m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
229             evalNode->destroyData();
230             return exception;
231         }
232         
233         evalNode->destroyData();
234         m_evalCodeBlock->copyPostParseDataFromAlternative();
235     }
236
237 #if ENABLE(JIT)
238     if (!prepareForExecution(exec, m_evalCodeBlock, m_jitCodeForCall, jitType, bytecodeIndex))
239         return 0;
240 #endif
241
242 #if ENABLE(JIT)
243 #if ENABLE(CLASSIC_INTERPRETER)
244     if (!m_jitCodeForCall)
245         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
246     else
247 #endif
248     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
249 #else
250     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
251 #endif
252
253     return 0;
254 }
255
256 #if ENABLE(JIT)
257 void EvalExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
258 {
259     jettisonCodeBlock(globalData, m_evalCodeBlock);
260     m_jitCodeForCall = m_evalCodeBlock->getJITCode();
261     ASSERT(!m_jitCodeForCallWithArityCheck);
262 }
263 #endif
264
265 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
266 {
267     EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
268     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
269     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
270     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
271     ScriptExecutable::visitChildren(thisObject, visitor);
272     if (thisObject->m_evalCodeBlock)
273         thisObject->m_evalCodeBlock->visitAggregate(visitor);
274 }
275
276 void EvalExecutable::unlinkCalls()
277 {
278 #if ENABLE(JIT)
279     if (!m_jitCodeForCall)
280         return;
281     ASSERT(m_evalCodeBlock);
282     m_evalCodeBlock->unlinkCalls();
283 #endif
284 }
285
286 void EvalExecutable::clearCode()
287 {
288     m_evalCodeBlock.clear();
289     Base::clearCode();
290 }
291
292 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
293 {
294     JSObject* exception = 0;
295     JSGlobalData* globalData = &exec->globalData();
296     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
297     RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
298     if (programNode)
299         return 0;
300     ASSERT(exception);
301     return exception;
302 }
303
304 JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
305 {
306     ASSERT(exec->globalData().dynamicGlobalObject);
307     ASSERT(!!m_programCodeBlock);
308     JSObject* error = 0;
309     if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
310         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()), bytecodeIndex);
311     ASSERT(!!m_programCodeBlock);
312     return error;
313 }
314
315 #if ENABLE(JIT)
316 bool ProgramExecutable::jitCompile(ExecState* exec)
317 {
318     return jitCompileIfAppropriate(exec, m_programCodeBlock, m_jitCodeForCall, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
319 }
320 #endif
321
322 JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
323 {
324     SamplingRegion samplingRegion(samplingDescription(jitType));
325     
326 #if !ENABLE(JIT)
327     UNUSED_PARAM(jitType);
328 #endif
329     JSObject* exception = 0;
330     JSGlobalData* globalData = &exec->globalData();
331     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
332     
333     if (!!m_programCodeBlock) {
334         OwnPtr<ProgramCodeBlock> newCodeBlock = adoptPtr(new ProgramCodeBlock(CodeBlock::CopyParsedBlock, *m_programCodeBlock));
335         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_programCodeBlock.release()));
336         m_programCodeBlock = newCodeBlock.release();
337     } else {
338         RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
339         if (!programNode) {
340             ASSERT(exception);
341             return exception;
342         }
343         recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
344
345         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
346     
347         OwnPtr<CodeBlock> previousCodeBlock = m_programCodeBlock.release();
348         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
349         m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider(), previousCodeBlock.release()));
350         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get(), !!m_programCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
351         if ((exception = generator->generate())) {
352             m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
353             programNode->destroyData();
354             return exception;
355         }
356
357         programNode->destroyData();
358         m_programCodeBlock->copyPostParseDataFromAlternative();
359     }
360
361 #if ENABLE(JIT)
362     if (!prepareForExecution(exec, m_programCodeBlock, m_jitCodeForCall, jitType, bytecodeIndex))
363         return 0;
364 #endif
365
366 #if ENABLE(JIT)
367 #if ENABLE(CLASSIC_INTERPRETER)
368     if (!m_jitCodeForCall)
369         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
370     else
371 #endif
372         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
373 #else
374     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
375 #endif
376
377     return 0;
378 }
379
380 #if ENABLE(JIT)
381 void ProgramExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
382 {
383     jettisonCodeBlock(globalData, m_programCodeBlock);
384     m_jitCodeForCall = m_programCodeBlock->getJITCode();
385     ASSERT(!m_jitCodeForCallWithArityCheck);
386 }
387 #endif
388
389 void ProgramExecutable::unlinkCalls()
390 {
391 #if ENABLE(JIT)
392     if (!m_jitCodeForCall)
393         return;
394     ASSERT(m_programCodeBlock);
395     m_programCodeBlock->unlinkCalls();
396 #endif
397 }
398
399 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
400 {
401     ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
402     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
403     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
404     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
405     ScriptExecutable::visitChildren(thisObject, visitor);
406     if (thisObject->m_programCodeBlock)
407         thisObject->m_programCodeBlock->visitAggregate(visitor);
408 }
409
410 void ProgramExecutable::clearCode()
411 {
412     m_programCodeBlock.clear();
413     Base::clearCode();
414 }
415
416 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
417 {
418     FunctionCodeBlock* result;
419     if (kind == CodeForCall)
420         result = m_codeBlockForCall.get();
421     else {
422         ASSERT(kind == CodeForConstruct);
423         result = m_codeBlockForConstruct.get();
424     }
425     if (!result)
426         return 0;
427     while (result->alternative())
428         result = static_cast<FunctionCodeBlock*>(result->alternative());
429     ASSERT(result);
430     ASSERT(JITCode::isBaselineCode(result->getJITType()));
431     return result;
432 }
433
434 JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
435 {
436     ASSERT(exec->globalData().dynamicGlobalObject);
437     ASSERT(!!m_codeBlockForCall);
438     JSObject* error = 0;
439     if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
440         error = compileForCallInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()), bytecodeIndex);
441     ASSERT(!!m_codeBlockForCall);
442     return error;
443 }
444
445 JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
446 {
447     ASSERT(exec->globalData().dynamicGlobalObject);
448     ASSERT(!!m_codeBlockForConstruct);
449     JSObject* error = 0;
450     if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
451         error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()), bytecodeIndex);
452     ASSERT(!!m_codeBlockForConstruct);
453     return error;
454 }
455
456 #if ENABLE(JIT)
457 bool FunctionExecutable::jitCompileForCall(ExecState* exec)
458 {
459     return jitCompileFunctionIfAppropriate(exec, m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
460 }
461
462 bool FunctionExecutable::jitCompileForConstruct(ExecState* exec)
463 {
464     return jitCompileFunctionIfAppropriate(exec, m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
465 }
466 #endif
467
468 FunctionCodeBlock* FunctionExecutable::codeBlockWithBytecodeFor(CodeSpecializationKind kind)
469 {
470     return baselineCodeBlockFor(kind);
471 }
472
473 PassOwnPtr<FunctionCodeBlock> FunctionExecutable::produceCodeBlockFor(ScopeChainNode* scopeChainNode, CompilationKind compilationKind, CodeSpecializationKind specializationKind, JSObject*& exception)
474 {
475     if (!!codeBlockFor(specializationKind))
476         return adoptPtr(new FunctionCodeBlock(CodeBlock::CopyParsedBlock, *codeBlockFor(specializationKind)));
477     
478     exception = 0;
479     JSGlobalData* globalData = scopeChainNode->globalData;
480     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
481     RefPtr<FunctionBodyNode> body = parse<FunctionBodyNode>(globalData, globalObject, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, FunctionBodyNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, 0, 0, &exception);
482
483     if (!body) {
484         ASSERT(exception);
485         return nullptr;
486     }
487     if (m_forceUsesArguments)
488         body->setUsesArguments();
489     body->finishParsing(m_parameters, m_name);
490     recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
491
492     OwnPtr<FunctionCodeBlock> result;
493     ASSERT((compilationKind == FirstCompilation) == !codeBlockFor(specializationKind));
494     result = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), specializationKind == CodeForConstruct));
495     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, result->symbolTable(), result.get(), compilationKind)));
496     exception = generator->generate();
497     body->destroyData();
498     if (exception)
499         return nullptr;
500
501     result->copyPostParseDataFrom(codeBlockFor(specializationKind).get());
502     return result.release();
503 }
504
505 JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
506 {
507     SamplingRegion samplingRegion(samplingDescription(jitType));
508     
509 #if !ENABLE(JIT)
510     UNUSED_PARAM(exec);
511     UNUSED_PARAM(jitType);
512     UNUSED_PARAM(exec);
513 #endif
514     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForCall);
515     JSObject* exception;
516     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForCall ? OptimizingCompilation : FirstCompilation, CodeForCall, exception);
517     if (!newCodeBlock)
518         return exception;
519
520     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForCall.release()));
521     m_codeBlockForCall = newCodeBlock.release();
522     
523     m_numParametersForCall = m_codeBlockForCall->numParameters();
524     ASSERT(m_numParametersForCall);
525     m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
526     m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
527
528 #if ENABLE(JIT)
529     if (!prepareFunctionForExecution(exec, m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, jitType, bytecodeIndex, CodeForCall))
530         return 0;
531 #endif
532
533 #if ENABLE(JIT)
534 #if ENABLE(CLASSIC_INTERPRETER)
535     if (!m_jitCodeForCall)
536         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
537     else
538 #endif
539         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
540 #else
541     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
542 #endif
543
544     return 0;
545 }
546
547 JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
548 {
549     SamplingRegion samplingRegion(samplingDescription(jitType));
550     
551 #if !ENABLE(JIT)
552     UNUSED_PARAM(jitType);
553     UNUSED_PARAM(exec);
554 #endif
555     
556     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForConstruct);
557     JSObject* exception;
558     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForConstruct ? OptimizingCompilation : FirstCompilation, CodeForConstruct, exception);
559     if (!newCodeBlock)
560         return exception;
561
562     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForConstruct.release()));
563     m_codeBlockForConstruct = newCodeBlock.release();
564     
565     m_numParametersForConstruct = m_codeBlockForConstruct->numParameters();
566     ASSERT(m_numParametersForConstruct);
567     m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
568     m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
569
570 #if ENABLE(JIT)
571     if (!prepareFunctionForExecution(exec, m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, jitType, bytecodeIndex, CodeForConstruct))
572         return 0;
573 #endif
574
575 #if ENABLE(JIT)
576 #if ENABLE(CLASSIC_INTERPRETER)
577     if (!m_jitCodeForConstruct)
578         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
579     else
580 #endif
581     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
582 #else
583     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
584 #endif
585
586     return 0;
587 }
588
589 #if ENABLE(JIT)
590 void FunctionExecutable::jettisonOptimizedCodeForCall(JSGlobalData& globalData)
591 {
592     jettisonCodeBlock(globalData, m_codeBlockForCall);
593     m_jitCodeForCall = m_codeBlockForCall->getJITCode();
594     m_jitCodeForCallWithArityCheck = m_codeBlockForCall->getJITCodeWithArityCheck();
595 }
596
597 void FunctionExecutable::jettisonOptimizedCodeForConstruct(JSGlobalData& globalData)
598 {
599     jettisonCodeBlock(globalData, m_codeBlockForConstruct);
600     m_jitCodeForConstruct = m_codeBlockForConstruct->getJITCode();
601     m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->getJITCodeWithArityCheck();
602 }
603 #endif
604
605 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
606 {
607     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
608     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
609     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
610     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
611     ScriptExecutable::visitChildren(thisObject, visitor);
612     if (thisObject->m_nameValue)
613         visitor.append(&thisObject->m_nameValue);
614     if (thisObject->m_codeBlockForCall)
615         thisObject->m_codeBlockForCall->visitAggregate(visitor);
616     if (thisObject->m_codeBlockForConstruct)
617         thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
618 }
619
620 void FunctionExecutable::clearCodeIfNotCompiling()
621 {
622     if (isCompiling())
623         return;
624     clearCode();
625 }
626
627 void FunctionExecutable::clearCode()
628 {
629     m_codeBlockForCall.clear();
630     m_codeBlockForConstruct.clear();
631     Base::clearCode();
632 }
633
634 void FunctionExecutable::unlinkCalls()
635 {
636 #if ENABLE(JIT)
637     if (!!m_jitCodeForCall) {
638         ASSERT(m_codeBlockForCall);
639         m_codeBlockForCall->unlinkCalls();
640     }
641     if (!!m_jitCodeForConstruct) {
642         ASSERT(m_codeBlockForConstruct);
643         m_codeBlockForConstruct->unlinkCalls();
644     }
645 #endif
646 }
647
648 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
649 {
650     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
651     RefPtr<ProgramNode> program = parse<ProgramNode>(&exec->globalData(), lexicalGlobalObject, source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, debugger, exec, exception);
652     if (!program) {
653         ASSERT(*exception);
654         return 0;
655     }
656
657     // Uses of this function that would not result in a single function expression are invalid.
658     StatementNode* exprStatement = program->singleStatement();
659     ASSERT(exprStatement);
660     ASSERT(exprStatement->isExprStatement());
661     ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
662     ASSERT(funcExpr);
663     ASSERT(funcExpr->isFuncExprNode());
664     FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
665     ASSERT(body);
666
667     return FunctionExecutable::create(exec->globalData(), functionName, functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
668 }
669
670 UString FunctionExecutable::paramString() const
671 {
672     FunctionParameters& parameters = *m_parameters;
673     UStringBuilder builder;
674     for (size_t pos = 0; pos < parameters.size(); ++pos) {
675         if (!builder.isEmpty())
676             builder.append(", ");
677         builder.append(parameters[pos].ustring());
678     }
679     return builder.toUString();
680 }
681
682 }