blob: 7b0b92152931dc3da1e9036cc585b631de054c5c [file] [log] [blame]
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +00001// Copyright 2012 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000030#include "compiler.h"
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "bootstrapper.h"
karlklose@chromium.org44bc7082011-04-11 12:33:05 +000033#include "codegen.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000034#include "compilation-cache.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "debug.h"
ulan@chromium.org6e196bf2013-03-13 09:38:22 +000036#include "deoptimizer.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000037#include "full-codegen.h"
erik.corry@gmail.com0511e242011-01-19 11:11:08 +000038#include "gdb-jit.h"
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +000039#include "typing.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000040#include "hydrogen.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000041#include "isolate-inl.h"
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000042#include "lithium.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000043#include "liveedit.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000044#include "parser.h"
ager@chromium.org71daaf62009-04-01 07:22:49 +000045#include "rewriter.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000046#include "runtime-profiler.h"
ricow@chromium.org55ee8072011-09-08 16:33:10 +000047#include "scanner-character-streams.h"
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +000048#include "scopeinfo.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000049#include "scopes.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000050#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
kasperl@chromium.org71affb52009-05-26 05:44:31 +000052namespace v8 {
53namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000055
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000056CompilationInfo::CompilationInfo(Handle<Script> script, Zone* zone)
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000057 : flags_(LanguageModeField::encode(CLASSIC_MODE)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000058 script_(script),
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000059 osr_ast_id_(BailoutId::None()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000060 Initialize(script->GetIsolate(), BASE, zone);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000061}
62
63
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000064CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info,
65 Zone* zone)
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000066 : flags_(LanguageModeField::encode(CLASSIC_MODE) | IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000067 shared_info_(shared_info),
68 script_(Handle<Script>(Script::cast(shared_info->script()))),
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000069 osr_ast_id_(BailoutId::None()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000070 Initialize(script_->GetIsolate(), BASE, zone);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000071}
72
73
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000074CompilationInfo::CompilationInfo(Handle<JSFunction> closure, Zone* zone)
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000075 : flags_(LanguageModeField::encode(CLASSIC_MODE) | IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000076 closure_(closure),
77 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
78 script_(Handle<Script>(Script::cast(shared_info_->script()))),
yangguo@chromium.org355cfd12012-08-29 15:32:24 +000079 context_(closure->context()),
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000080 osr_ast_id_(BailoutId::None()) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000081 Initialize(script_->GetIsolate(), BASE, zone);
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000082}
83
84
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000085CompilationInfo::CompilationInfo(HydrogenCodeStub* stub,
86 Isolate* isolate, Zone* zone)
87 : flags_(LanguageModeField::encode(CLASSIC_MODE) |
88 IsLazy::encode(true)),
89 osr_ast_id_(BailoutId::None()) {
90 Initialize(isolate, STUB, zone);
91 code_stub_ = stub;
92}
93
94
95void CompilationInfo::Initialize(Isolate* isolate, Mode mode, Zone* zone) {
96 isolate_ = isolate;
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +000097 function_ = NULL;
98 scope_ = NULL;
99 global_scope_ = NULL;
100 extension_ = NULL;
101 pre_parse_data_ = NULL;
102 zone_ = zone;
103 deferred_handles_ = NULL;
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000104 code_stub_ = NULL;
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000105 prologue_offset_ = kPrologueOffsetNotSet;
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000106 opt_count_ = shared_info().is_null() ? 0 : shared_info()->opt_count();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000107 no_frame_ranges_ = isolate->cpu_profiler()->is_profiling()
108 ? new List<OffsetRange>(2) : NULL;
danno@chromium.org41728482013-06-12 22:31:22 +0000109 for (int i = 0; i < DependentCode::kGroupCount; i++) {
110 dependent_maps_[i] = NULL;
111 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000112 if (mode == STUB) {
113 mode_ = STUB;
114 return;
115 }
116 mode_ = V8::UseCrankshaft() ? mode : NONOPT;
svenpanne@chromium.org83130cf2012-11-30 10:13:25 +0000117 if (script_->type()->value() == Script::TYPE_NATIVE) {
118 MarkAsNative();
119 }
120 if (!shared_info_.is_null()) {
121 ASSERT(language_mode() == CLASSIC_MODE);
122 SetLanguageMode(shared_info_->language_mode());
123 }
124 set_bailout_reason("unknown");
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000125}
126
127
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000128CompilationInfo::~CompilationInfo() {
129 delete deferred_handles_;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000130 delete no_frame_ranges_;
danno@chromium.org41728482013-06-12 22:31:22 +0000131#ifdef DEBUG
132 // Check that no dependent maps have been added or added dependent maps have
133 // been rolled back or committed.
134 for (int i = 0; i < DependentCode::kGroupCount; i++) {
135 ASSERT_EQ(NULL, dependent_maps_[i]);
136 }
137#endif // DEBUG
138}
139
140
141void CompilationInfo::CommitDependentMaps(Handle<Code> code) {
142 for (int i = 0; i < DependentCode::kGroupCount; i++) {
143 ZoneList<Handle<Map> >* group_maps = dependent_maps_[i];
144 if (group_maps == NULL) continue;
145 ASSERT(!object_wrapper_.is_null());
146 for (int j = 0; j < group_maps->length(); j++) {
147 group_maps->at(j)->dependent_code()->UpdateToFinishedCode(
148 static_cast<DependentCode::DependencyGroup>(i), this, *code);
149 }
150 dependent_maps_[i] = NULL; // Zone-allocated, no need to delete.
151 }
152}
153
154
155void CompilationInfo::RollbackDependentMaps() {
156 // Unregister from all dependent maps if not yet committed.
157 for (int i = 0; i < DependentCode::kGroupCount; i++) {
158 ZoneList<Handle<Map> >* group_maps = dependent_maps_[i];
159 if (group_maps == NULL) continue;
160 for (int j = 0; j < group_maps->length(); j++) {
161 group_maps->at(j)->dependent_code()->RemoveCompilationInfo(
162 static_cast<DependentCode::DependencyGroup>(i), this);
163 }
164 dependent_maps_[i] = NULL; // Zone-allocated, no need to delete.
165 }
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000166}
167
168
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000169int CompilationInfo::num_parameters() const {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000170 ASSERT(!IsStub());
171 return scope()->num_parameters();
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000172}
173
174
175int CompilationInfo::num_heap_slots() const {
176 if (IsStub()) {
177 return 0;
178 } else {
179 return scope()->num_heap_slots();
180 }
181}
182
183
184Code::Flags CompilationInfo::flags() const {
185 if (IsStub()) {
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000186 return Code::ComputeFlags(code_stub()->GetCodeKind(),
187 code_stub()->GetICState(),
188 code_stub()->GetExtraICState(),
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000189 code_stub()->GetStubType(),
190 code_stub()->GetStubFlags());
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000191 } else {
192 return Code::ComputeFlags(Code::OPTIMIZED_FUNCTION);
193 }
194}
195
196
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000197// Disable optimization for the rest of the compilation pipeline.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000198void CompilationInfo::DisableOptimization() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000199 bool is_optimizable_closure =
200 FLAG_optimize_closures &&
201 closure_.is_null() &&
202 !scope_->HasTrivialOuterContext() &&
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000203 !scope_->outer_scope_calls_non_strict_eval() &&
lrn@chromium.org1c092762011-05-09 09:42:16 +0000204 !scope_->inside_with();
205 SetMode(is_optimizable_closure ? BASE : NONOPT);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000206}
207
208
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000209// Primitive functions are unlikely to be picked up by the stack-walking
210// profiler, so they trigger their own optimization when they're called
211// for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time.
212bool CompilationInfo::ShouldSelfOptimize() {
213 return FLAG_self_optimization &&
214 FLAG_crankshaft &&
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000215 !function()->flags()->Contains(kDontSelfOptimize) &&
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000216 !function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000217 function()->scope()->AllowsLazyCompilation() &&
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000218 (shared_info().is_null() || !shared_info()->optimization_disabled());
219}
220
221
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000222void CompilationInfo::AbortOptimization() {
223 Handle<Code> code(shared_info()->code());
224 SetCode(code);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000225}
226
227
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000228// Determine whether to use the full compiler for all code. If the flag
229// --always-full-compiler is specified this is the case. For the virtual frame
230// based compiler the full compiler is also used if a debugger is connected, as
231// the code from the full compiler supports mode precise break points. For the
232// crankshaft adaptive compiler debugging the optimized code is not possible at
233// all. However crankshaft support recompilation of functions, so in this case
234// the full compiler need not be be used if a debugger is attached, but only if
235// break points has actually been set.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000236static bool IsDebuggerActive(Isolate* isolate) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000237#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org1c092762011-05-09 09:42:16 +0000238 return V8::UseCrankshaft() ?
239 isolate->debug()->has_break_points() :
240 isolate->debugger()->IsDebuggerActive();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000241#else
lrn@chromium.org1c092762011-05-09 09:42:16 +0000242 return false;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000243#endif
244}
245
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000246
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000247static bool AlwaysFullCompiler(Isolate* isolate) {
248 return FLAG_always_full_compiler || IsDebuggerActive(isolate);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000249}
250
251
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000252void OptimizingCompiler::RecordOptimizationStats() {
253 Handle<JSFunction> function = info()->closure();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000254 int opt_count = function->shared()->opt_count();
255 function->shared()->set_opt_count(opt_count + 1);
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000256 double ms_creategraph =
257 static_cast<double>(time_taken_to_create_graph_) / 1000;
258 double ms_optimize = static_cast<double>(time_taken_to_optimize_) / 1000;
259 double ms_codegen = static_cast<double>(time_taken_to_codegen_) / 1000;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000260 if (FLAG_trace_opt) {
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000261 PrintF("[optimizing ");
262 function->ShortPrint();
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000263 PrintF(" - took %0.3f, %0.3f, %0.3f ms]\n", ms_creategraph, ms_optimize,
264 ms_codegen);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000265 }
266 if (FLAG_trace_opt_stats) {
267 static double compilation_time = 0.0;
268 static int compiled_functions = 0;
269 static int code_size = 0;
270
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000271 compilation_time += (ms_creategraph + ms_optimize + ms_codegen);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000272 compiled_functions++;
273 code_size += function->shared()->SourceSize();
274 PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
275 compiled_functions,
276 code_size,
277 compilation_time);
278 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000279 if (FLAG_hydrogen_stats) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000280 isolate()->GetHStatistics()->IncrementSubtotals(time_taken_to_create_graph_,
281 time_taken_to_optimize_,
282 time_taken_to_codegen_);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000283 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000284}
285
286
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000287// A return value of true indicates the compilation pipeline is still
288// going, not necessarily that we optimized the code.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000289static bool MakeCrankshaftCode(CompilationInfo* info) {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000290 OptimizingCompiler compiler(info);
291 OptimizingCompiler::Status status = compiler.CreateGraph();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000292
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000293 if (status != OptimizingCompiler::SUCCEEDED) {
294 return status != OptimizingCompiler::FAILED;
295 }
296 status = compiler.OptimizeGraph();
297 if (status != OptimizingCompiler::SUCCEEDED) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000298 status = compiler.AbortOptimization();
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000299 return status != OptimizingCompiler::FAILED;
300 }
301 status = compiler.GenerateAndInstallCode();
302 return status != OptimizingCompiler::FAILED;
303}
304
305
306OptimizingCompiler::Status OptimizingCompiler::CreateGraph() {
307 ASSERT(V8::UseCrankshaft());
308 ASSERT(info()->IsOptimizing());
309 ASSERT(!info()->IsCompilingForDebugging());
310
311 // We should never arrive here if there is no code object on the
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312 // shared function object.
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000313 Handle<Code> code(info()->shared_info()->code());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000314 ASSERT(code->kind() == Code::FUNCTION);
315
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000316 // We should never arrive here if optimization has been disabled on the
317 // shared function info.
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000318 ASSERT(!info()->shared_info()->optimization_disabled());
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000319
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000320 // Fall back to using the full code generator if it's not possible
321 // to use the Hydrogen-based optimizing compiler. We already have
322 // generated code for this from the shared function object.
ulan@chromium.org750145a2013-03-07 15:14:13 +0000323 if (AlwaysFullCompiler(isolate())) {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000324 info()->SetCode(code);
325 return SetLastStatus(BAILED_OUT);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000326 }
327
328 // Limit the number of times we re-compile a functions with
329 // the optimizing compiler.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000330 const int kMaxOptCount =
verwaest@chromium.orgde64f722012-08-16 15:44:54 +0000331 FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000;
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000332 if (info()->opt_count() > kMaxOptCount) {
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000333 info()->set_bailout_reason("optimized too many times");
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000334 return AbortOptimization();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000335 }
336
337 // Due to an encoding limit on LUnallocated operands in the Lithium
338 // language, we cannot optimize functions with too many formal parameters
339 // or perform on-stack replacement for function with too many
340 // stack-allocated local variables.
341 //
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000342 // The encoding is as a signed value, with parameters and receiver using
343 // the negative indices and locals the non-negative ones.
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000344 const int parameter_limit = -LUnallocated::kMinFixedSlotIndex;
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000345 Scope* scope = info()->scope();
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000346 if ((scope->num_parameters() + 1) > parameter_limit) {
347 info()->set_bailout_reason("too many parameters");
348 return AbortOptimization();
349 }
350
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000351 const int locals_limit = LUnallocated::kMaxFixedSlotIndex;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000352 if (!info()->osr_ast_id().IsNone() &&
353 scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit) {
354 info()->set_bailout_reason("too many parameters/locals");
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000355 return AbortOptimization();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000356 }
357
358 // Take --hydrogen-filter into account.
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000359 if (!info()->closure()->PassesHydrogenFilter()) {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000360 info()->SetCode(code);
361 return SetLastStatus(BAILED_OUT);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000362 }
363
364 // Recompile the unoptimized version of the code if the current version
365 // doesn't have deoptimization support. Alternatively, we may decide to
366 // run the full code generator to get a baseline for the compile-time
367 // performance of the hydrogen-based compiler.
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000368 bool should_recompile = !info()->shared_info()->has_deoptimization_support();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000369 if (should_recompile || FLAG_hydrogen_stats) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000370 HPhase phase(HPhase::kFullCodeGen, isolate());
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000371 CompilationInfoWithZone unoptimized(info()->shared_info());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000372 // Note that we use the same AST that we will use for generating the
373 // optimized code.
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000374 unoptimized.SetFunction(info()->function());
375 unoptimized.SetScope(info()->scope());
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000376 unoptimized.SetContext(info()->context());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000377 if (should_recompile) unoptimized.EnableDeoptimizationSupport();
378 bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
379 if (should_recompile) {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000380 if (!succeeded) return SetLastStatus(FAILED);
381 Handle<SharedFunctionInfo> shared = info()->shared_info();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000382 shared->EnableDeoptimizationSupport(*unoptimized.code());
383 // The existing unoptimized code was replaced with the new one.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000384 Compiler::RecordFunctionCompilation(
385 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000386 }
387 }
388
389 // Check that the unoptimized, shared code is ready for
390 // optimizations. When using the always_opt flag we disregard the
391 // optimizable marker in the code object and optimize anyway. This
392 // is safe as long as the unoptimized code has deoptimization
393 // support.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000394 ASSERT(FLAG_always_opt || code->optimizable());
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000395 ASSERT(info()->shared_info()->has_deoptimization_support());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000396
397 if (FLAG_trace_hydrogen) {
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000398 Handle<String> name = info()->function()->debug_name();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000399 PrintF("-----------------------------------------------------------\n");
400 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
ulan@chromium.org750145a2013-03-07 15:14:13 +0000401 isolate()->GetHTracer()->TraceCompilation(info());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000402 }
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000403
404 // Type-check the function.
danno@chromium.org41728482013-06-12 22:31:22 +0000405 AstTyper::Run(info());
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +0000406
407 graph_builder_ = new(info()->zone()) HOptimizedGraphBuilder(info());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000408
409 Timer t(this, &time_taken_to_create_graph_);
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000410 graph_ = graph_builder_->CreateGraph();
411
ulan@chromium.org750145a2013-03-07 15:14:13 +0000412 if (isolate()->has_pending_exception()) {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000413 info()->SetCode(Handle<Code>::null());
414 return SetLastStatus(FAILED);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000415 }
416
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000417 // The function being compiled may have bailed out due to an inline
418 // candidate bailing out. In such a case, we don't disable
419 // optimization on the shared_info.
420 ASSERT(!graph_builder_->inline_bailout() || graph_ == NULL);
421 if (graph_ == NULL) {
422 if (graph_builder_->inline_bailout()) {
423 info_->AbortOptimization();
424 return SetLastStatus(BAILED_OUT);
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000425 } else {
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000426 return AbortOptimization();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000427 }
428 }
429
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000430 return SetLastStatus(SUCCEEDED);
431}
432
433OptimizingCompiler::Status OptimizingCompiler::OptimizeGraph() {
rossberg@chromium.org79e79022013-06-03 15:43:46 +0000434 DisallowHeapAllocation no_allocation;
435 DisallowHandleAllocation no_handles;
436 DisallowHandleDereference no_deref;
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000437
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000438 ASSERT(last_status() == SUCCEEDED);
439 Timer t(this, &time_taken_to_optimize_);
440 ASSERT(graph_ != NULL);
441 SmartArrayPointer<char> bailout_reason;
442 if (!graph_->Optimize(&bailout_reason)) {
443 if (!bailout_reason.is_empty()) graph_builder_->Bailout(*bailout_reason);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000444 return SetLastStatus(BAILED_OUT);
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000445 } else {
446 chunk_ = LChunk::NewChunk(graph_);
447 if (chunk_ == NULL) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000448 return SetLastStatus(BAILED_OUT);
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000449 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000450 }
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000451 return SetLastStatus(SUCCEEDED);
452}
453
454
455OptimizingCompiler::Status OptimizingCompiler::GenerateAndInstallCode() {
456 ASSERT(last_status() == SUCCEEDED);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000457 { // Scope for timer.
458 Timer timer(this, &time_taken_to_codegen_);
459 ASSERT(chunk_ != NULL);
460 ASSERT(graph_ != NULL);
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000461 // Deferred handles reference objects that were accessible during
462 // graph creation. To make sure that we don't encounter inconsistencies
463 // between graph creation and code generation, we disallow accessing
464 // objects through deferred handles during the latter, with exceptions.
yangguo@chromium.org20301242013-06-03 16:06:25 +0000465 DisallowDeferredHandleDereference no_deferred_handle_deref;
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000466 Handle<Code> optimized_code = chunk_->Codegen();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000467 if (optimized_code.is_null()) {
468 info()->set_bailout_reason("code generation failed");
469 return AbortOptimization();
470 }
471 info()->SetCode(optimized_code);
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000472 }
verwaest@chromium.org178fb152012-07-18 11:21:48 +0000473 RecordOptimizationStats();
474 return SetLastStatus(SUCCEEDED);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000475}
476
477
lrn@chromium.org1c092762011-05-09 09:42:16 +0000478static bool GenerateCode(CompilationInfo* info) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000479 bool is_optimizing = V8::UseCrankshaft() &&
480 !info->IsCompilingForDebugging() &&
481 info->IsOptimizing();
482 if (is_optimizing) {
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000483 Logger::TimerEventScope timer(
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000484 info->isolate(), Logger::TimerEventScope::v8_recompile_synchronous);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000485 return MakeCrankshaftCode(info);
486 } else {
487 if (info->IsOptimizing()) {
488 // Have the CompilationInfo decide if the compilation should be
489 // BASE or NONOPT.
490 info->DisableOptimization();
491 }
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000492 Logger::TimerEventScope timer(
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000493 info->isolate(), Logger::TimerEventScope::v8_compile_full_code);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000494 return FullCodeGenerator::MakeCode(info);
495 }
lrn@chromium.org1c092762011-05-09 09:42:16 +0000496}
497
498
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000499static bool MakeCode(CompilationInfo* info) {
500 // Precondition: code has been parsed. Postcondition: the code field in
501 // the compilation info is set if compilation succeeded.
502 ASSERT(info->function() != NULL);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000503 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000504}
505
506
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000507#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000508bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
509 // Precondition: code has been parsed. Postcondition: the code field in
510 // the compilation info is set if compilation succeeded.
511 bool succeeded = MakeCode(info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000512 if (!info->shared_info().is_null()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000513 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000514 info->zone());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000515 info->shared_info()->set_scope_info(*scope_info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000516 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000517 return succeeded;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000518}
519#endif
520
521
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000522static bool DebuggerWantsEagerCompilation(CompilationInfo* info,
523 bool allow_lazy_without_ctx = false) {
524 return LiveEditFunctionTracker::IsActive(info->isolate()) ||
525 (info->isolate()->DebuggerHasBreakPoints() && !allow_lazy_without_ctx);
526}
527
528
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000529static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000530 Isolate* isolate = info->isolate();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000531 ZoneScope zone_scope(info->zone(), DELETE_ON_EXIT);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 PostponeInterruptsScope postpone(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000534 ASSERT(!isolate->native_context().is_null());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000535 Handle<Script> script = info->script();
yangguo@chromium.orgeeb44b62012-11-13 13:56:09 +0000536 // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile?
537 FixedArray* array = isolate->native_context()->embedder_data();
538 script->set_context_data(array->get(0));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000539
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000540#ifdef ENABLE_DEBUGGER_SUPPORT
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000541 if (info->is_eval()) {
542 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000543 script->set_compilation_type(Smi::FromInt(compilation_type));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000544 // For eval scripts add information on the function from which eval was
545 // called.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000546 if (info->is_eval()) {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000547 StackTraceFrameIterator it(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000548 if (!it.done()) {
549 script->set_eval_from_shared(
550 JSFunction::cast(it.frame()->function())->shared());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000551 Code* code = it.frame()->LookupCode();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000552 int offset = static_cast<int>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553 it.frame()->pc() - code->instruction_start());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000554 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
555 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000556 }
557 }
558
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 isolate->debugger()->OnBeforeCompile(script);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000561#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000562
563 // Only allow non-global compiles for eval.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000564 ASSERT(info->is_eval() || info->is_global());
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000565 {
566 Parser parser(info);
567 if ((info->pre_parse_data() != NULL ||
568 String::cast(script->source())->length() > FLAG_min_preparse_length) &&
569 !DebuggerWantsEagerCompilation(info))
570 parser.set_allow_lazy(true);
571 if (!parser.Parse()) {
572 return Handle<SharedFunctionInfo>::null();
573 }
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000574 }
kasper.lund212ac232008-07-16 07:07:30 +0000575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 // Measure how long it takes to do the compilation; only take the
577 // rest of the function into account to avoid overlap with the
578 // parsing statistics.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000579 HistogramTimer* rate = info->is_eval()
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000580 ? info->isolate()->counters()->compile_eval()
581 : info->isolate()->counters()->compile();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000582 HistogramTimerScope timer(rate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583
584 // Compile the code.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000585 FunctionLiteral* lit = info->function();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000586 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000587 if (!MakeCode(info)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000588 if (!isolate->has_pending_exception()) isolate->StackOverflow();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000589 return Handle<SharedFunctionInfo>::null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 }
591
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000592 // Allocate function.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000593 ASSERT(!info->code().is_null());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000594 Handle<SharedFunctionInfo> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000595 isolate->factory()->NewSharedFunctionInfo(
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000596 lit->name(),
597 lit->materialized_literal_count(),
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000598 lit->is_generator(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000599 info->code(),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000600 ScopeInfo::Create(info->scope(), info->zone()));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000601
602 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
603 Compiler::SetFunctionInfo(result, lit, true, script);
604
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000605 if (script->name()->IsString()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000606 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000607 info->is_eval()
608 ? Logger::EVAL_TAG
609 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
610 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000611 *result,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000612 info,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000613 String::cast(script->name())));
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000614 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
615 script,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000616 info->code(),
617 info));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000618 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000619 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000620 info->is_eval()
621 ? Logger::EVAL_TAG
622 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
623 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000624 *result,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000625 info,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000626 isolate->heap()->empty_string()));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000627 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 }
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 // Hint to the runtime system used when allocating space for initial
631 // property space by setting the expected number of properties for
632 // the instances of the function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000633 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000635 script->set_compilation_state(
636 Smi::FromInt(Script::COMPILATION_STATE_COMPILED));
637
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000638#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000640 isolate->debugger()->OnAfterCompile(
641 script, Debugger::NO_AFTER_COMPILE_FLAGS);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000642#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000644 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000645
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000646 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647}
648
649
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000650Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
651 Handle<Object> script_name,
652 int line_offset,
653 int column_offset,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000654 Handle<Context> context,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000655 v8::Extension* extension,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000656 ScriptDataImpl* pre_data,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000657 Handle<Object> script_data,
658 NativesFlag natives) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000659 Isolate* isolate = source->GetIsolate();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000660 int source_length = source->length();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000661 isolate->counters()->total_load_size()->Increment(source_length);
662 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663
664 // The VM is in the COMPILER state until exiting this function.
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000665 VMState<COMPILER> state(isolate);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000666
667 CompilationCache* compilation_cache = isolate->compilation_cache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000669 // Do a lookup in the compilation cache but not for extensions.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000670 Handle<SharedFunctionInfo> result;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000671 if (extension == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000672 result = compilation_cache->LookupScript(source,
673 script_name,
674 line_offset,
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000675 column_offset,
676 context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677 }
678
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000679 if (result.is_null()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000680 // No cache entry found. Do pre-parsing, if it makes sense, and compile
681 // the script.
682 // Building preparse data that is only used immediately after is only a
683 // saving if we might skip building the AST for lazily compiled functions.
684 // I.e., preparse data isn't relevant when the lazy flag is off, and
685 // for small sources, odds are that there aren't many functions
686 // that would be compiled lazily anyway, so we skip the preparse step
687 // in that case too.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000688
689 // Create a script object describing the script to be compiled.
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000690 Handle<Script> script = isolate->factory()->NewScript(source);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000691 if (natives == NATIVES_CODE) {
692 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
693 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000694 if (!script_name.is_null()) {
695 script->set_name(*script_name);
696 script->set_line_offset(Smi::FromInt(line_offset));
697 script->set_column_offset(Smi::FromInt(column_offset));
698 }
699
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000700 script->set_data(script_data.is_null() ? HEAP->undefined_value()
ager@chromium.org5c838252010-02-19 08:53:10 +0000701 : *script_data);
702
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000703 // Compile the function and add it to the cache.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000704 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000705 info.MarkAsGlobal();
706 info.SetExtension(extension);
707 info.SetPreParseData(pre_data);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000708 info.SetContext(context);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000709 if (FLAG_use_strict) {
710 info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE);
711 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000712 result = MakeFunctionInfo(&info);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000713 if (extension == NULL && !result.is_null() && !result->dont_cache()) {
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000714 compilation_cache->PutScript(source, context, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000715 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000716 } else {
717 if (result->ic_age() != HEAP->global_ic_age()) {
718 result->ResetForNewContext(HEAP->global_ic_age());
719 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000720 }
721
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000722 if (result.is_null()) isolate->ReportPendingMessages();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 return result;
724}
725
726
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000727Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
728 Handle<Context> context,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000729 bool is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000730 LanguageMode language_mode,
svenpanne@chromium.org9faefa42013-03-08 13:13:16 +0000731 ParseRestriction restriction,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000732 int scope_position) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000733 Isolate* isolate = source->GetIsolate();
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000734 int source_length = source->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000735 isolate->counters()->total_eval_size()->Increment(source_length);
736 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000737
738 // The VM is in the COMPILER state until exiting this function.
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000739 VMState<COMPILER> state(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000741 // Do a lookup in the compilation cache; if the entry is not there, invoke
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000742 // the compiler and add the result to the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000743 Handle<SharedFunctionInfo> result;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000744 CompilationCache* compilation_cache = isolate->compilation_cache();
745 result = compilation_cache->LookupEval(source,
746 context,
747 is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000748 language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000749 scope_position);
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000750
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000751 if (result.is_null()) {
752 // Create a script object describing the script to be compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000753 Handle<Script> script = isolate->factory()->NewScript(source);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000754 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000755 info.MarkAsEval();
756 if (is_global) info.MarkAsGlobal();
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000757 info.SetLanguageMode(language_mode);
svenpanne@chromium.org9faefa42013-03-08 13:13:16 +0000758 info.SetParseRestriction(restriction);
yangguo@chromium.org355cfd12012-08-29 15:32:24 +0000759 info.SetContext(context);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000760 result = MakeFunctionInfo(&info);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000761 if (!result.is_null()) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000762 // Explicitly disable optimization for eval code. We're not yet prepared
763 // to handle eval-code in the optimizing compiler.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000764 result->DisableOptimization("eval");
yangguo@chromium.org56454712012-02-16 15:33:53 +0000765
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000766 // If caller is strict mode, the result must be in strict mode or
767 // extended mode as well, but not the other way around. Consider:
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000768 // eval("'use strict'; ...");
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000769 ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode());
770 // If caller is in extended mode, the result must also be in
771 // extended mode.
772 ASSERT(language_mode != EXTENDED_MODE ||
773 result->is_extended_mode());
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000774 if (!result->dont_cache()) {
775 compilation_cache->PutEval(
776 source, context, is_global, result, scope_position);
777 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000778 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000779 } else {
780 if (result->ic_age() != HEAP->global_ic_age()) {
781 result->ResetForNewContext(HEAP->global_ic_age());
782 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000783 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000784
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000785 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000786}
787
788
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000789static bool InstallFullCode(CompilationInfo* info) {
790 // Update the shared function info with the compiled code and the
791 // scope info. Please note, that the order of the shared function
792 // info initialization is important since set_scope_info might
793 // trigger a GC, causing the ASSERT below to be invalid if the code
794 // was flushed. By setting the code object last we avoid this.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000795 Handle<SharedFunctionInfo> shared = info->shared_info();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000796 Handle<Code> code = info->code();
797 Handle<JSFunction> function = info->closure();
798 Handle<ScopeInfo> scope_info =
799 ScopeInfo::Create(info->scope(), info->zone());
800 shared->set_scope_info(*scope_info);
yangguo@chromium.org9768bf12013-01-11 14:51:07 +0000801 shared->ReplaceCode(*code);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000802 if (!function.is_null()) {
803 function->ReplaceCode(*code);
804 ASSERT(!function->IsOptimized());
805 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000807 // Set the expected number of properties for instances.
808 FunctionLiteral* lit = info->function();
809 int expected = lit->expected_property_count();
810 SetExpectedNofPropertiesFromEstimate(shared, expected);
811
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000812 // Check the function has compiled code.
813 ASSERT(shared->is_compiled());
814 shared->set_code_age(0);
815 shared->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
816 shared->set_dont_inline(lit->flags()->Contains(kDontInline));
817 shared->set_ast_node_count(lit->ast_node_count());
818
jkummerow@chromium.org000f7fb2012-08-01 11:14:42 +0000819 if (V8::UseCrankshaft() &&
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000820 !function.is_null() &&
821 !shared->optimization_disabled()) {
822 // If we're asked to always optimize, we compile the optimized
823 // version of the function right away - unless the debugger is
824 // active as it makes no sense to compile optimized code then.
825 if (FLAG_always_opt &&
826 !Isolate::Current()->DebuggerHasBreakPoints()) {
827 CompilationInfoWithZone optimized(function);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000828 optimized.SetOptimizing(BailoutId::None());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000829 return Compiler::CompileLazy(&optimized);
830 }
831 }
832 return true;
833}
834
835
836static void InstallCodeCommon(CompilationInfo* info) {
837 Handle<SharedFunctionInfo> shared = info->shared_info();
838 Handle<Code> code = info->code();
839 ASSERT(!code.is_null());
840
841 // Set optimizable to false if this is disallowed by the shared
842 // function info, e.g., we might have flushed the code and must
843 // reset this bit when lazy compiling the code again.
844 if (shared->optimization_disabled()) code->set_optimizable(false);
845
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000846 if (shared->code() == *code) {
847 // Do not send compilation event for the same code twice.
848 return;
849 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000850 Compiler::RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
851}
852
853
854static void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) {
855 Handle<Code> code = info->code();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000856 if (FLAG_cache_optimized_code &&
857 info->osr_ast_id().IsNone() &&
858 code->kind() == Code::OPTIMIZED_FUNCTION) {
859 Handle<JSFunction> function = info->closure();
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000860 Handle<SharedFunctionInfo> shared(function->shared());
861 Handle<FixedArray> literals(function->literals());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000862 Handle<Context> native_context(function->context()->native_context());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000863 SharedFunctionInfo::AddToOptimizedCodeMap(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000864 shared, native_context, code, literals);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000865 }
866}
867
868
869static bool InstallCodeFromOptimizedCodeMap(CompilationInfo* info) {
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000870 if (FLAG_cache_optimized_code &&
871 info->osr_ast_id().IsNone() &&
872 info->IsOptimizing()) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000873 Handle<SharedFunctionInfo> shared = info->shared_info();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000874 Handle<JSFunction> function = info->closure();
875 ASSERT(!function.is_null());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000876 Handle<Context> native_context(function->context()->native_context());
877 int index = shared->SearchOptimizedCodeMap(*native_context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000878 if (index > 0) {
879 if (FLAG_trace_opt) {
ulan@chromium.org906e2fb2013-05-14 08:14:38 +0000880 PrintF("[found optimized code for ");
881 function->ShortPrint();
882 PrintF("]\n");
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000883 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000884 // Caching of optimized code enabled and optimized code found.
885 shared->InstallFromOptimizedCodeMap(*function, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000886 return true;
887 }
888 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000889 return false;
890}
891
892
893bool Compiler::CompileLazy(CompilationInfo* info) {
894 Isolate* isolate = info->isolate();
895
896 ZoneScope zone_scope(info->zone(), DELETE_ON_EXIT);
897
898 // The VM is in the COMPILER state until exiting this function.
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000899 VMState<COMPILER> state(isolate);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000900
901 PostponeInterruptsScope postpone(isolate);
902
903 Handle<SharedFunctionInfo> shared = info->shared_info();
904 int compiled_size = shared->end_position() - shared->start_position();
905 isolate->counters()->total_compile_size()->Increment(compiled_size);
906
907 if (InstallCodeFromOptimizedCodeMap(info)) return true;
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000908
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000909 // Generate the AST for the lazily compiled function.
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000910 if (Parser::Parse(info)) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000911 // Measure how long it takes to do the lazy compilation; only take the
912 // rest of the function into account to avoid overlap with the lazy
913 // parsing statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000914 HistogramTimerScope timer(isolate->counters()->compile_lazy());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000916 // After parsing we know the function's language mode. Remember it.
917 LanguageMode language_mode = info->function()->language_mode();
918 info->SetLanguageMode(language_mode);
919 shared->set_language_mode(language_mode);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000920
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000921 // Compile the code.
922 if (!MakeCode(info)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000923 if (!isolate->has_pending_exception()) {
924 isolate->StackOverflow();
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000925 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000926 } else {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000927 InstallCodeCommon(info);
kasper.lund212ac232008-07-16 07:07:30 +0000928
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000929 if (info->IsOptimizing()) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000930 Handle<Code> code = info->code();
hpayer@chromium.org8432c912013-02-28 15:55:26 +0000931 ASSERT(shared->scope_info() != ScopeInfo::Empty(isolate));
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000932 info->closure()->ReplaceCode(*code);
933 InsertCodeIntoOptimizedCodeMap(info);
934 return true;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000935 } else {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000936 return InstallFullCode(info);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000937 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000938 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 }
940
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000941 ASSERT(info->code().is_null());
942 return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000943}
944
945
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000946void Compiler::RecompileParallel(Handle<JSFunction> closure) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000947 ASSERT(closure->IsMarkedForParallelRecompilation());
948
949 Isolate* isolate = closure->GetIsolate();
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000950 // Here we prepare compile data for the parallel recompilation thread, but
951 // this still happens synchronously and interrupts execution.
952 Logger::TimerEventScope timer(
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000953 isolate, Logger::TimerEventScope::v8_recompile_synchronous);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000954
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000955 if (!isolate->optimizing_compiler_thread()->IsQueueAvailable()) {
956 if (FLAG_trace_parallel_recompilation) {
957 PrintF(" ** Compilation queue, will retry opting on next run.\n");
958 }
959 return;
960 }
961
962 SmartPointer<CompilationInfo> info(new CompilationInfoWithZone(closure));
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000963 VMState<COMPILER> state(isolate);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000964 PostponeInterruptsScope postpone(isolate);
965
966 Handle<SharedFunctionInfo> shared = info->shared_info();
967 int compiled_size = shared->end_position() - shared->start_position();
968 isolate->counters()->total_compile_size()->Increment(compiled_size);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000969 info->SetOptimizing(BailoutId::None());
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000970
971 {
972 CompilationHandleScope handle_scope(*info);
973
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000974 if (InstallCodeFromOptimizedCodeMap(*info)) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000975 return;
976 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000977
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000978 if (Parser::Parse(*info)) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000979 LanguageMode language_mode = info->function()->language_mode();
980 info->SetLanguageMode(language_mode);
981 shared->set_language_mode(language_mode);
982 info->SaveHandles();
983
984 if (Rewriter::Rewrite(*info) && Scope::Analyze(*info)) {
985 OptimizingCompiler* compiler =
986 new(info->zone()) OptimizingCompiler(*info);
987 OptimizingCompiler::Status status = compiler->CreateGraph();
988 if (status == OptimizingCompiler::SUCCEEDED) {
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000989 info.Detach();
990 shared->code()->set_profiler_ticks(0);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +0000991 isolate->optimizing_compiler_thread()->QueueForOptimization(compiler);
yangguo@chromium.org304cc332012-07-24 07:59:48 +0000992 } else if (status == OptimizingCompiler::BAILED_OUT) {
993 isolate->clear_pending_exception();
994 InstallFullCode(*info);
995 }
996 }
997 }
998 }
999
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001000 if (shared->code()->back_edges_patched_for_osr()) {
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001001 // At this point we either put the function on recompilation queue or
1002 // aborted optimization. In either case we want to continue executing
1003 // the unoptimized code without running into OSR. If the unoptimized
1004 // code has been patched for OSR, unpatch it.
1005 InterruptStub interrupt_stub;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001006 Handle<Code> interrupt_code = interrupt_stub.GetCode(isolate);
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001007 Handle<Code> replacement_code =
1008 isolate->builtins()->OnStackReplacement();
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00001009 Deoptimizer::RevertInterruptCode(shared->code(),
1010 *interrupt_code,
1011 *replacement_code);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001012 }
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001013
1014 if (isolate->has_pending_exception()) isolate->clear_pending_exception();
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001015}
1016
1017
1018void Compiler::InstallOptimizedCode(OptimizingCompiler* optimizing_compiler) {
1019 SmartPointer<CompilationInfo> info(optimizing_compiler->info());
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001020 // The function may have already been optimized by OSR. Simply continue.
1021 // Except when OSR already disabled optimization for some reason.
1022 if (info->shared_info()->optimization_disabled()) {
danno@chromium.org41728482013-06-12 22:31:22 +00001023 info->AbortOptimization();
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001024 InstallFullCode(*info);
1025 if (FLAG_trace_parallel_recompilation) {
1026 PrintF(" ** aborting optimization for ");
1027 info->closure()->PrintName();
1028 PrintF(" as it has been disabled.\n");
1029 }
1030 ASSERT(!info->closure()->IsMarkedForInstallingRecompiledCode());
1031 return;
1032 }
ulan@chromium.org750145a2013-03-07 15:14:13 +00001033
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001034 Isolate* isolate = info->isolate();
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001035 VMState<COMPILER> state(isolate);
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00001036 Logger::TimerEventScope timer(
danno@chromium.org1f34ad32012-11-26 14:53:56 +00001037 isolate, Logger::TimerEventScope::v8_recompile_synchronous);
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001038 // If crankshaft succeeded, install the optimized code else install
1039 // the unoptimized code.
1040 OptimizingCompiler::Status status = optimizing_compiler->last_status();
danno@chromium.org41728482013-06-12 22:31:22 +00001041 if (info->HasAbortedDueToDependentMap()) {
1042 info->set_bailout_reason("bailed out due to dependent map");
1043 status = optimizing_compiler->AbortOptimization();
1044 } else if (status != OptimizingCompiler::SUCCEEDED) {
1045 info->set_bailout_reason("failed/bailed out last time");
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001046 status = optimizing_compiler->AbortOptimization();
1047 } else {
1048 status = optimizing_compiler->GenerateAndInstallCode();
1049 ASSERT(status == OptimizingCompiler::SUCCEEDED ||
1050 status == OptimizingCompiler::BAILED_OUT);
1051 }
1052
1053 InstallCodeCommon(*info);
1054 if (status == OptimizingCompiler::SUCCEEDED) {
1055 Handle<Code> code = info->code();
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001056 ASSERT(info->shared_info()->scope_info() != ScopeInfo::Empty(isolate));
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001057 info->closure()->ReplaceCode(*code);
1058 if (info->shared_info()->SearchOptimizedCodeMap(
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001059 info->closure()->context()->native_context()) == -1) {
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001060 InsertCodeIntoOptimizedCodeMap(*info);
1061 }
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001062 if (FLAG_trace_parallel_recompilation) {
1063 PrintF(" ** Optimized code for ");
1064 info->closure()->PrintName();
1065 PrintF(" installed.\n");
1066 }
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001067 } else {
1068 info->SetCode(Handle<Code>(info->shared_info()->code()));
1069 InstallFullCode(*info);
1070 }
ulan@chromium.org6e196bf2013-03-13 09:38:22 +00001071 // Optimized code is finally replacing unoptimized code. Reset the latter's
1072 // profiler ticks to prevent too soon re-opt after a deopt.
1073 info->shared_info()->code()->set_profiler_ticks(0);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00001074 ASSERT(!info->closure()->IsMarkedForInstallingRecompiledCode());
yangguo@chromium.org304cc332012-07-24 07:59:48 +00001075}
1076
1077
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001078Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001079 Handle<Script> script) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001080 // Precondition: code has been parsed and scopes have been analyzed.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001081 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001082 info.SetFunction(literal);
1083 info.SetScope(literal->scope());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001084 info.SetLanguageMode(literal->scope()->language_mode());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001085
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001086 Isolate* isolate = info.isolate();
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001087 Factory* factory = isolate->factory();
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001088 LiveEditFunctionTracker live_edit_tracker(isolate, literal);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001089 // Determine if the function can be lazily compiled. This is necessary to
1090 // allow some of our builtin JS files to be lazily compiled. These
1091 // builtins cannot be handled lazily by the parser, since we have to know
1092 // if a function uses the special natives syntax, which is something the
1093 // parser records.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001094 // If the debugger requests compilation for break points, we cannot be
1095 // aggressive about lazy compilation, because it might trigger compilation
1096 // of functions without an outer context when setting a breakpoint through
jkummerow@chromium.org78502a92012-09-06 13:50:42 +00001097 // Debug::FindSharedFunctionInfoInScript.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001098 bool allow_lazy_without_ctx = literal->AllowsLazyCompilationWithoutContext();
ager@chromium.org5c838252010-02-19 08:53:10 +00001099 bool allow_lazy = literal->AllowsLazyCompilation() &&
yangguo@chromium.org46a2a512013-01-18 16:29:40 +00001100 !DebuggerWantsEagerCompilation(&info, allow_lazy_without_ctx);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001101
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001102 Handle<ScopeInfo> scope_info(ScopeInfo::Empty(isolate));
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001103
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001104 // Generate code
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00001105 if (FLAG_lazy && allow_lazy && !literal->is_parenthesized()) {
hpayer@chromium.org8432c912013-02-28 15:55:26 +00001106 Handle<Code> code = isolate->builtins()->LazyCompile();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001107 info.SetCode(code);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +00001108 } else if (GenerateCode(&info)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001109 ASSERT(!info.code().is_null());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001110 scope_info = ScopeInfo::Create(info.scope(), info.zone());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001111 } else {
1112 return Handle<SharedFunctionInfo>::null();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001113 }
1114
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001115 // Create a shared function info object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001116 Handle<SharedFunctionInfo> result =
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001117 factory->NewSharedFunctionInfo(literal->name(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001118 literal->materialized_literal_count(),
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001119 literal->is_generator(),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001120 info.code(),
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001121 scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001122 SetFunctionInfo(result, literal, false, script);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001123 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001124 result->set_allows_lazy_compilation(allow_lazy);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001125 result->set_allows_lazy_compilation_without_context(allow_lazy_without_ctx);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001126
1127 // Set the expected number of properties for instances and return
1128 // the resulting function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001129 SetExpectedNofPropertiesFromEstimate(result,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001130 literal->expected_property_count());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001131 live_edit_tracker.RecordFunctionInfo(result, literal, info.zone());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001132 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001133}
1134
1135
1136// Sets the function info on a function.
1137// The start_position points to the first '(' character after the function name
1138// in the full script source. When counting characters in the script source the
1139// the first character is number 0 (not 1).
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001140void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001141 FunctionLiteral* lit,
1142 bool is_toplevel,
1143 Handle<Script> script) {
danno@chromium.orgc612e022011-11-10 11:38:15 +00001144 function_info->set_length(lit->parameter_count());
1145 function_info->set_formal_parameter_count(lit->parameter_count());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001146 function_info->set_script(*script);
1147 function_info->set_function_token_position(lit->function_token_position());
1148 function_info->set_start_position(lit->start_position());
1149 function_info->set_end_position(lit->end_position());
1150 function_info->set_is_expression(lit->is_expression());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001151 function_info->set_is_anonymous(lit->is_anonymous());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001152 function_info->set_is_toplevel(is_toplevel);
1153 function_info->set_inferred_name(*lit->inferred_name());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00001154 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +00001155 function_info->set_allows_lazy_compilation_without_context(
1156 lit->AllowsLazyCompilationWithoutContext());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001157 function_info->set_language_mode(lit->language_mode());
whesse@chromium.org7b260152011-06-20 15:33:18 +00001158 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
1159 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +00001160 function_info->set_ast_node_count(lit->ast_node_count());
yangguo@chromium.org56454712012-02-16 15:33:53 +00001161 function_info->set_is_function(lit->is_function());
1162 function_info->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +00001163 function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
danno@chromium.org81cac2b2012-07-10 11:28:27 +00001164 function_info->set_dont_cache(lit->flags()->Contains(kDontCache));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001165 function_info->set_is_generator(lit->is_generator());
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001166}
1167
1168
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001169void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001170 CompilationInfo* info,
1171 Handle<SharedFunctionInfo> shared) {
1172 // SharedFunctionInfo is passed separately, because if CompilationInfo
1173 // was created using Script object, it will not have it.
1174
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001175 // Log the code generation. If source information is available include
1176 // script name and line number. Check explicitly whether logging is
1177 // enabled as finding the line number is not free.
yangguo@chromium.org355cfd12012-08-29 15:32:24 +00001178 if (info->isolate()->logger()->is_logging_code_events() ||
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001179 info->isolate()->cpu_profiler()->is_profiling()) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001180 Handle<Script> script = info->script();
1181 Handle<Code> code = info->code();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001182 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001183 return;
ager@chromium.org5c838252010-02-19 08:53:10 +00001184 if (script->name()->IsString()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001185 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001186 USE(line_num);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001187 PROFILE(info->isolate(),
1188 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001189 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001190 *shared,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001191 info,
danno@chromium.orgf95d4b92013-06-13 14:40:17 +00001192 String::cast(script->name()),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001193 line_num));
ager@chromium.org5c838252010-02-19 08:53:10 +00001194 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001195 PROFILE(info->isolate(),
1196 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00001197 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001198 *shared,
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +00001199 info,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001200 shared->DebugName()));
ager@chromium.org5c838252010-02-19 08:53:10 +00001201 }
1202 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001203
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001204 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001205 Handle<Script>(info->script()),
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001206 Handle<Code>(info->code()),
1207 info));
ager@chromium.org5c838252010-02-19 08:53:10 +00001208}
ager@chromium.org5c838252010-02-19 08:53:10 +00001209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210} } // namespace v8::internal