Fix broken non-JIT build.
[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     UNUSED_PARAM(bytecodeIndex);
203 #endif
204     JSObject* exception = 0;
205     JSGlobalData* globalData = &exec->globalData();
206     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
207     
208     if (!!m_evalCodeBlock) {
209         OwnPtr<EvalCodeBlock> newCodeBlock = adoptPtr(new EvalCodeBlock(CodeBlock::CopyParsedBlock, *m_evalCodeBlock));
210         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_evalCodeBlock.release()));
211         m_evalCodeBlock = newCodeBlock.release();
212     } else {
213         if (!lexicalGlobalObject->evalEnabled())
214             return throwError(exec, createEvalError(exec, "Eval is disabled"));
215         RefPtr<EvalNode> evalNode = parse<EvalNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, EvalNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
216         if (!evalNode) {
217             ASSERT(exception);
218             return exception;
219         }
220         recordParse(evalNode->features(), evalNode->hasCapturedVariables(), evalNode->lineNo(), evalNode->lastLine());
221         
222         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
223         
224         OwnPtr<CodeBlock> previousCodeBlock = m_evalCodeBlock.release();
225         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
226         m_evalCodeBlock = adoptPtr(new EvalCodeBlock(this, globalObject, source().provider(), scopeChainNode->localDepth(), previousCodeBlock.release()));
227         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(evalNode.get(), scopeChainNode, m_evalCodeBlock->symbolTable(), m_evalCodeBlock.get(), !!m_evalCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
228         if ((exception = generator->generate())) {
229             m_evalCodeBlock = static_pointer_cast<EvalCodeBlock>(m_evalCodeBlock->releaseAlternative());
230             evalNode->destroyData();
231             return exception;
232         }
233         
234         evalNode->destroyData();
235         m_evalCodeBlock->copyPostParseDataFromAlternative();
236     }
237
238 #if ENABLE(JIT)
239     if (!prepareForExecution(exec, m_evalCodeBlock, m_jitCodeForCall, jitType, bytecodeIndex))
240         return 0;
241 #endif
242
243 #if ENABLE(JIT)
244 #if ENABLE(CLASSIC_INTERPRETER)
245     if (!m_jitCodeForCall)
246         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
247     else
248 #endif
249     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock) + m_jitCodeForCall.size());
250 #else
251     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_evalCodeBlock));
252 #endif
253
254     return 0;
255 }
256
257 #if ENABLE(JIT)
258 void EvalExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
259 {
260     jettisonCodeBlock(globalData, m_evalCodeBlock);
261     m_jitCodeForCall = m_evalCodeBlock->getJITCode();
262     ASSERT(!m_jitCodeForCallWithArityCheck);
263 }
264 #endif
265
266 void EvalExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
267 {
268     EvalExecutable* thisObject = jsCast<EvalExecutable*>(cell);
269     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
270     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
271     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
272     ScriptExecutable::visitChildren(thisObject, visitor);
273     if (thisObject->m_evalCodeBlock)
274         thisObject->m_evalCodeBlock->visitAggregate(visitor);
275 }
276
277 void EvalExecutable::unlinkCalls()
278 {
279 #if ENABLE(JIT)
280     if (!m_jitCodeForCall)
281         return;
282     ASSERT(m_evalCodeBlock);
283     m_evalCodeBlock->unlinkCalls();
284 #endif
285 }
286
287 void EvalExecutable::clearCode()
288 {
289     m_evalCodeBlock.clear();
290     Base::clearCode();
291 }
292
293 JSObject* ProgramExecutable::checkSyntax(ExecState* exec)
294 {
295     JSObject* exception = 0;
296     JSGlobalData* globalData = &exec->globalData();
297     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
298     RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
299     if (programNode)
300         return 0;
301     ASSERT(exception);
302     return exception;
303 }
304
305 JSObject* ProgramExecutable::compileOptimized(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
306 {
307     ASSERT(exec->globalData().dynamicGlobalObject);
308     ASSERT(!!m_programCodeBlock);
309     JSObject* error = 0;
310     if (m_programCodeBlock->getJITType() != JITCode::topTierJIT())
311         error = compileInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_programCodeBlock->getJITType()), bytecodeIndex);
312     ASSERT(!!m_programCodeBlock);
313     return error;
314 }
315
316 #if ENABLE(JIT)
317 bool ProgramExecutable::jitCompile(ExecState* exec)
318 {
319     return jitCompileIfAppropriate(exec, m_programCodeBlock, m_jitCodeForCall, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
320 }
321 #endif
322
323 JSObject* ProgramExecutable::compileInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
324 {
325     SamplingRegion samplingRegion(samplingDescription(jitType));
326     
327 #if !ENABLE(JIT)
328     UNUSED_PARAM(jitType);
329     UNUSED_PARAM(bytecodeIndex);
330 #endif
331     JSObject* exception = 0;
332     JSGlobalData* globalData = &exec->globalData();
333     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
334     
335     if (!!m_programCodeBlock) {
336         OwnPtr<ProgramCodeBlock> newCodeBlock = adoptPtr(new ProgramCodeBlock(CodeBlock::CopyParsedBlock, *m_programCodeBlock));
337         newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_programCodeBlock.release()));
338         m_programCodeBlock = newCodeBlock.release();
339     } else {
340         RefPtr<ProgramNode> programNode = parse<ProgramNode>(globalData, lexicalGlobalObject, m_source, 0, isStrictMode() ? JSParseStrict : JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, lexicalGlobalObject->debugger(), exec, &exception);
341         if (!programNode) {
342             ASSERT(exception);
343             return exception;
344         }
345         recordParse(programNode->features(), programNode->hasCapturedVariables(), programNode->lineNo(), programNode->lastLine());
346
347         JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
348     
349         OwnPtr<CodeBlock> previousCodeBlock = m_programCodeBlock.release();
350         ASSERT((jitType == JITCode::bottomTierJIT()) == !previousCodeBlock);
351         m_programCodeBlock = adoptPtr(new ProgramCodeBlock(this, GlobalCode, globalObject, source().provider(), previousCodeBlock.release()));
352         OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(programNode.get(), scopeChainNode, &globalObject->symbolTable(), m_programCodeBlock.get(), !!m_programCodeBlock->alternative() ? OptimizingCompilation : FirstCompilation)));
353         if ((exception = generator->generate())) {
354             m_programCodeBlock = static_pointer_cast<ProgramCodeBlock>(m_programCodeBlock->releaseAlternative());
355             programNode->destroyData();
356             return exception;
357         }
358
359         programNode->destroyData();
360         m_programCodeBlock->copyPostParseDataFromAlternative();
361     }
362
363 #if ENABLE(JIT)
364     if (!prepareForExecution(exec, m_programCodeBlock, m_jitCodeForCall, jitType, bytecodeIndex))
365         return 0;
366 #endif
367
368 #if ENABLE(JIT)
369 #if ENABLE(CLASSIC_INTERPRETER)
370     if (!m_jitCodeForCall)
371         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
372     else
373 #endif
374         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock) + m_jitCodeForCall.size());
375 #else
376     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_programCodeBlock));
377 #endif
378
379     return 0;
380 }
381
382 #if ENABLE(JIT)
383 void ProgramExecutable::jettisonOptimizedCode(JSGlobalData& globalData)
384 {
385     jettisonCodeBlock(globalData, m_programCodeBlock);
386     m_jitCodeForCall = m_programCodeBlock->getJITCode();
387     ASSERT(!m_jitCodeForCallWithArityCheck);
388 }
389 #endif
390
391 void ProgramExecutable::unlinkCalls()
392 {
393 #if ENABLE(JIT)
394     if (!m_jitCodeForCall)
395         return;
396     ASSERT(m_programCodeBlock);
397     m_programCodeBlock->unlinkCalls();
398 #endif
399 }
400
401 void ProgramExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
402 {
403     ProgramExecutable* thisObject = jsCast<ProgramExecutable*>(cell);
404     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
405     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
406     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
407     ScriptExecutable::visitChildren(thisObject, visitor);
408     if (thisObject->m_programCodeBlock)
409         thisObject->m_programCodeBlock->visitAggregate(visitor);
410 }
411
412 void ProgramExecutable::clearCode()
413 {
414     m_programCodeBlock.clear();
415     Base::clearCode();
416 }
417
418 FunctionCodeBlock* FunctionExecutable::baselineCodeBlockFor(CodeSpecializationKind kind)
419 {
420     FunctionCodeBlock* result;
421     if (kind == CodeForCall)
422         result = m_codeBlockForCall.get();
423     else {
424         ASSERT(kind == CodeForConstruct);
425         result = m_codeBlockForConstruct.get();
426     }
427     if (!result)
428         return 0;
429     while (result->alternative())
430         result = static_cast<FunctionCodeBlock*>(result->alternative());
431     ASSERT(result);
432     ASSERT(JITCode::isBaselineCode(result->getJITType()));
433     return result;
434 }
435
436 JSObject* FunctionExecutable::compileOptimizedForCall(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
437 {
438     ASSERT(exec->globalData().dynamicGlobalObject);
439     ASSERT(!!m_codeBlockForCall);
440     JSObject* error = 0;
441     if (m_codeBlockForCall->getJITType() != JITCode::topTierJIT())
442         error = compileForCallInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForCall->getJITType()), bytecodeIndex);
443     ASSERT(!!m_codeBlockForCall);
444     return error;
445 }
446
447 JSObject* FunctionExecutable::compileOptimizedForConstruct(ExecState* exec, ScopeChainNode* scopeChainNode, unsigned bytecodeIndex)
448 {
449     ASSERT(exec->globalData().dynamicGlobalObject);
450     ASSERT(!!m_codeBlockForConstruct);
451     JSObject* error = 0;
452     if (m_codeBlockForConstruct->getJITType() != JITCode::topTierJIT())
453         error = compileForConstructInternal(exec, scopeChainNode, JITCode::nextTierJIT(m_codeBlockForConstruct->getJITType()), bytecodeIndex);
454     ASSERT(!!m_codeBlockForConstruct);
455     return error;
456 }
457
458 #if ENABLE(JIT)
459 bool FunctionExecutable::jitCompileForCall(ExecState* exec)
460 {
461     return jitCompileFunctionIfAppropriate(exec, m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
462 }
463
464 bool FunctionExecutable::jitCompileForConstruct(ExecState* exec)
465 {
466     return jitCompileFunctionIfAppropriate(exec, m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, JITCode::bottomTierJIT(), UINT_MAX, JITCompilationCanFail);
467 }
468 #endif
469
470 FunctionCodeBlock* FunctionExecutable::codeBlockWithBytecodeFor(CodeSpecializationKind kind)
471 {
472     return baselineCodeBlockFor(kind);
473 }
474
475 PassOwnPtr<FunctionCodeBlock> FunctionExecutable::produceCodeBlockFor(ScopeChainNode* scopeChainNode, CompilationKind compilationKind, CodeSpecializationKind specializationKind, JSObject*& exception)
476 {
477     if (!!codeBlockFor(specializationKind))
478         return adoptPtr(new FunctionCodeBlock(CodeBlock::CopyParsedBlock, *codeBlockFor(specializationKind)));
479     
480     exception = 0;
481     JSGlobalData* globalData = scopeChainNode->globalData;
482     JSGlobalObject* globalObject = scopeChainNode->globalObject.get();
483     RefPtr<FunctionBodyNode> body = parse<FunctionBodyNode>(globalData, globalObject, m_source, m_parameters.get(), isStrictMode() ? JSParseStrict : JSParseNormal, FunctionBodyNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, 0, 0, &exception);
484
485     if (!body) {
486         ASSERT(exception);
487         return nullptr;
488     }
489     if (m_forceUsesArguments)
490         body->setUsesArguments();
491     body->finishParsing(m_parameters, m_name);
492     recordParse(body->features(), body->hasCapturedVariables(), body->lineNo(), body->lastLine());
493
494     OwnPtr<FunctionCodeBlock> result;
495     ASSERT((compilationKind == FirstCompilation) == !codeBlockFor(specializationKind));
496     result = adoptPtr(new FunctionCodeBlock(this, FunctionCode, globalObject, source().provider(), source().startOffset(), specializationKind == CodeForConstruct));
497     OwnPtr<BytecodeGenerator> generator(adoptPtr(new BytecodeGenerator(body.get(), scopeChainNode, result->symbolTable(), result.get(), compilationKind)));
498     exception = generator->generate();
499     body->destroyData();
500     if (exception)
501         return nullptr;
502
503     result->copyPostParseDataFrom(codeBlockFor(specializationKind).get());
504     return result.release();
505 }
506
507 JSObject* FunctionExecutable::compileForCallInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
508 {
509     SamplingRegion samplingRegion(samplingDescription(jitType));
510     
511 #if !ENABLE(JIT)
512     UNUSED_PARAM(exec);
513     UNUSED_PARAM(jitType);
514     UNUSED_PARAM(exec);
515     UNUSED_PARAM(bytecodeIndex);
516 #endif
517     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForCall);
518     JSObject* exception;
519     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForCall ? OptimizingCompilation : FirstCompilation, CodeForCall, exception);
520     if (!newCodeBlock)
521         return exception;
522
523     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForCall.release()));
524     m_codeBlockForCall = newCodeBlock.release();
525     
526     m_numParametersForCall = m_codeBlockForCall->numParameters();
527     ASSERT(m_numParametersForCall);
528     m_numCapturedVariables = m_codeBlockForCall->m_numCapturedVars;
529     m_symbolTable = m_codeBlockForCall->sharedSymbolTable();
530
531 #if ENABLE(JIT)
532     if (!prepareFunctionForExecution(exec, m_codeBlockForCall, m_jitCodeForCall, m_jitCodeForCallWithArityCheck, m_symbolTable, jitType, bytecodeIndex, CodeForCall))
533         return 0;
534 #endif
535
536 #if ENABLE(JIT)
537 #if ENABLE(CLASSIC_INTERPRETER)
538     if (!m_jitCodeForCall)
539         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
540     else
541 #endif
542         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall) + m_jitCodeForCall.size());
543 #else
544     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForCall));
545 #endif
546
547     return 0;
548 }
549
550 JSObject* FunctionExecutable::compileForConstructInternal(ExecState* exec, ScopeChainNode* scopeChainNode, JITCode::JITType jitType, unsigned bytecodeIndex)
551 {
552     SamplingRegion samplingRegion(samplingDescription(jitType));
553     
554 #if !ENABLE(JIT)
555     UNUSED_PARAM(jitType);
556     UNUSED_PARAM(exec);
557     UNUSED_PARAM(bytecodeIndex);
558 #endif
559     
560     ASSERT((jitType == JITCode::bottomTierJIT()) == !m_codeBlockForConstruct);
561     JSObject* exception;
562     OwnPtr<FunctionCodeBlock> newCodeBlock = produceCodeBlockFor(scopeChainNode, !!m_codeBlockForConstruct ? OptimizingCompilation : FirstCompilation, CodeForConstruct, exception);
563     if (!newCodeBlock)
564         return exception;
565
566     newCodeBlock->setAlternative(static_pointer_cast<CodeBlock>(m_codeBlockForConstruct.release()));
567     m_codeBlockForConstruct = newCodeBlock.release();
568     
569     m_numParametersForConstruct = m_codeBlockForConstruct->numParameters();
570     ASSERT(m_numParametersForConstruct);
571     m_numCapturedVariables = m_codeBlockForConstruct->m_numCapturedVars;
572     m_symbolTable = m_codeBlockForConstruct->sharedSymbolTable();
573
574 #if ENABLE(JIT)
575     if (!prepareFunctionForExecution(exec, m_codeBlockForConstruct, m_jitCodeForConstruct, m_jitCodeForConstructWithArityCheck, m_symbolTable, jitType, bytecodeIndex, CodeForConstruct))
576         return 0;
577 #endif
578
579 #if ENABLE(JIT)
580 #if ENABLE(CLASSIC_INTERPRETER)
581     if (!m_jitCodeForConstruct)
582         Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
583     else
584 #endif
585     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct) + m_jitCodeForConstruct.size());
586 #else
587     Heap::heap(this)->reportExtraMemoryCost(sizeof(*m_codeBlockForConstruct));
588 #endif
589
590     return 0;
591 }
592
593 #if ENABLE(JIT)
594 void FunctionExecutable::jettisonOptimizedCodeForCall(JSGlobalData& globalData)
595 {
596     jettisonCodeBlock(globalData, m_codeBlockForCall);
597     m_jitCodeForCall = m_codeBlockForCall->getJITCode();
598     m_jitCodeForCallWithArityCheck = m_codeBlockForCall->getJITCodeWithArityCheck();
599 }
600
601 void FunctionExecutable::jettisonOptimizedCodeForConstruct(JSGlobalData& globalData)
602 {
603     jettisonCodeBlock(globalData, m_codeBlockForConstruct);
604     m_jitCodeForConstruct = m_codeBlockForConstruct->getJITCode();
605     m_jitCodeForConstructWithArityCheck = m_codeBlockForConstruct->getJITCodeWithArityCheck();
606 }
607 #endif
608
609 void FunctionExecutable::visitChildren(JSCell* cell, SlotVisitor& visitor)
610 {
611     FunctionExecutable* thisObject = jsCast<FunctionExecutable*>(cell);
612     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
613     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
614     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
615     ScriptExecutable::visitChildren(thisObject, visitor);
616     if (thisObject->m_nameValue)
617         visitor.append(&thisObject->m_nameValue);
618     if (thisObject->m_codeBlockForCall)
619         thisObject->m_codeBlockForCall->visitAggregate(visitor);
620     if (thisObject->m_codeBlockForConstruct)
621         thisObject->m_codeBlockForConstruct->visitAggregate(visitor);
622 }
623
624 void FunctionExecutable::clearCodeIfNotCompiling()
625 {
626     if (isCompiling())
627         return;
628     clearCode();
629 }
630
631 void FunctionExecutable::clearCode()
632 {
633     m_codeBlockForCall.clear();
634     m_codeBlockForConstruct.clear();
635     Base::clearCode();
636 }
637
638 void FunctionExecutable::unlinkCalls()
639 {
640 #if ENABLE(JIT)
641     if (!!m_jitCodeForCall) {
642         ASSERT(m_codeBlockForCall);
643         m_codeBlockForCall->unlinkCalls();
644     }
645     if (!!m_jitCodeForConstruct) {
646         ASSERT(m_codeBlockForConstruct);
647         m_codeBlockForConstruct->unlinkCalls();
648     }
649 #endif
650 }
651
652 FunctionExecutable* FunctionExecutable::fromGlobalCode(const Identifier& functionName, ExecState* exec, Debugger* debugger, const SourceCode& source, JSObject** exception)
653 {
654     JSGlobalObject* lexicalGlobalObject = exec->lexicalGlobalObject();
655     RefPtr<ProgramNode> program = parse<ProgramNode>(&exec->globalData(), lexicalGlobalObject, source, 0, JSParseNormal, ProgramNode::isFunctionNode ? JSParseFunctionCode : JSParseProgramCode, debugger, exec, exception);
656     if (!program) {
657         ASSERT(*exception);
658         return 0;
659     }
660
661     // Uses of this function that would not result in a single function expression are invalid.
662     StatementNode* exprStatement = program->singleStatement();
663     ASSERT(exprStatement);
664     ASSERT(exprStatement->isExprStatement());
665     ExpressionNode* funcExpr = static_cast<ExprStatementNode*>(exprStatement)->expr();
666     ASSERT(funcExpr);
667     ASSERT(funcExpr->isFuncExprNode());
668     FunctionBodyNode* body = static_cast<FuncExprNode*>(funcExpr)->body();
669     ASSERT(body);
670
671     return FunctionExecutable::create(exec->globalData(), functionName, functionName, body->source(), body->usesArguments(), body->parameters(), body->isStrictMode(), body->lineNo(), body->lastLine());
672 }
673
674 UString FunctionExecutable::paramString() const
675 {
676     FunctionParameters& parameters = *m_parameters;
677     UStringBuilder builder;
678     for (size_t pos = 0; pos < parameters.size(); ++pos) {
679         if (!builder.isEmpty())
680             builder.append(", ");
681         builder.append(parameters[pos].ustring());
682     }
683     return builder.toUString();
684 }
685
686 }