a822079ae880901ead6b06779d93a20c2cd4c994
[WebKit-https.git] / Source / JavaScriptCore / dfg / DFGDriver.cpp
1 /*
2  * Copyright (C) 2011, 2012, 2013, 2014 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 "DFGDriver.h"
28
29 #include "JSObject.h"
30 #include "JSString.h"
31
32 #include "CodeBlock.h"
33 #include "DFGJITCode.h"
34 #include "DFGPlan.h"
35 #include "DFGThunks.h"
36 #include "DFGWorklist.h"
37 #include "JITCode.h"
38 #include "JSCInlines.h"
39 #include "Options.h"
40 #include "SamplingTool.h"
41 #include "TypeProfilerLog.h"
42 #include <wtf/Atomics.h>
43
44 #if ENABLE(FTL_JIT)
45 #include "FTLThunks.h"
46 #endif
47
48 namespace JSC { namespace DFG {
49
50 static unsigned numCompilations;
51
52 unsigned getNumCompilations()
53 {
54     return numCompilations;
55 }
56
57 #if ENABLE(DFG_JIT)
58 static CompilationResult compileImpl(
59     VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode,
60     unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues,
61     PassRefPtr<DeferredCompilationCallback> callback)
62 {
63     SamplingRegion samplingRegion("DFG Compilation (Driver)");
64     
65     numCompilations++;
66     
67     ASSERT(codeBlock);
68     ASSERT(codeBlock->alternative());
69     ASSERT(codeBlock->alternative()->jitType() == JITCode::BaselineJIT);
70     ASSERT(!profiledDFGCodeBlock || profiledDFGCodeBlock->jitType() == JITCode::DFGJIT);
71     
72     if (logCompilationChanges(mode))
73         dataLog("DFG(Driver) compiling ", *codeBlock, " with ", mode, ", number of instructions = ", codeBlock->instructionCount(), "\n");
74     
75     // Make sure that any stubs that the DFG is going to use are initialized. We want to
76     // make sure that all JIT code generation does finalization on the main thread.
77     vm.getCTIStub(osrExitGenerationThunkGenerator);
78     vm.getCTIStub(throwExceptionFromCallSlowPathGenerator);
79     if (mode == DFGMode) {
80         vm.getCTIStub(linkCallThunkGenerator);
81         vm.getCTIStub(linkConstructThunkGenerator);
82         vm.getCTIStub(linkPolymorphicCallThunkGenerator);
83         vm.getCTIStub(virtualCallThunkGenerator);
84         vm.getCTIStub(virtualConstructThunkGenerator);
85     } else {
86         vm.getCTIStub(linkCallThatPreservesRegsThunkGenerator);
87         vm.getCTIStub(linkConstructThatPreservesRegsThunkGenerator);
88         vm.getCTIStub(linkPolymorphicCallThatPreservesRegsThunkGenerator);
89         vm.getCTIStub(virtualCallThatPreservesRegsThunkGenerator);
90         vm.getCTIStub(virtualConstructThatPreservesRegsThunkGenerator);
91     }
92     
93     if (vm.typeProfiler())
94         vm.typeProfilerLog()->processLogEntries(ASCIILiteral("Preparing for DFG compilation."));
95     
96     RefPtr<Plan> plan = adoptRef(
97         new Plan(codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues));
98     
99     if (Options::enableConcurrentJIT()) {
100         Worklist* worklist = ensureGlobalWorklistFor(mode);
101         plan->callback = callback;
102         if (logCompilationChanges(mode))
103             dataLog("Deferring DFG compilation of ", *codeBlock, " with queue length ", worklist->queueLength(), ".\n");
104         worklist->enqueue(plan);
105         return CompilationDeferred;
106     }
107     
108     plan->compileInThread(*vm.dfgState, 0);
109     return plan->finalizeWithoutNotifyingCallback();
110 }
111 #else // ENABLE(DFG_JIT)
112 static CompilationResult compileImpl(
113     VM&, CodeBlock*, CodeBlock*, CompilationMode, unsigned, const Operands<JSValue>&,
114     PassRefPtr<DeferredCompilationCallback>)
115 {
116     return CompilationFailed;
117 }
118 #endif // ENABLE(DFG_JIT)
119
120 CompilationResult compile(
121     VM& vm, CodeBlock* codeBlock, CodeBlock* profiledDFGCodeBlock, CompilationMode mode,
122     unsigned osrEntryBytecodeIndex, const Operands<JSValue>& mustHandleValues,
123     PassRefPtr<DeferredCompilationCallback> passedCallback)
124 {
125     RefPtr<DeferredCompilationCallback> callback = passedCallback;
126     CompilationResult result = compileImpl(
127         vm, codeBlock, profiledDFGCodeBlock, mode, osrEntryBytecodeIndex, mustHandleValues,
128         callback);
129     if (result != CompilationDeferred)
130         callback->compilationDidComplete(codeBlock, result);
131     return result;
132 }
133
134 } } // namespace JSC::DFG