blob: 22118ee4d05edae1b104769dbc6f7adf9a9ece31 [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"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000036#include "full-codegen.h"
erik.corry@gmail.com0511e242011-01-19 11:11:08 +000037#include "gdb-jit.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000038#include "hydrogen.h"
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +000039#include "isolate-inl.h"
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000040#include "lithium.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000041#include "liveedit.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000042#include "parser.h"
ager@chromium.org71daaf62009-04-01 07:22:49 +000043#include "rewriter.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000044#include "runtime-profiler.h"
ricow@chromium.org55ee8072011-09-08 16:33:10 +000045#include "scanner-character-streams.h"
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +000046#include "scopeinfo.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000047#include "scopes.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000048#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000049
kasperl@chromium.org71affb52009-05-26 05:44:31 +000050namespace v8 {
51namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000053
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000054CompilationInfo::CompilationInfo(Handle<Script> script, Zone* zone)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 : isolate_(script->GetIsolate()),
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000056 flags_(LanguageModeField::encode(CLASSIC_MODE)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000057 function_(NULL),
58 scope_(NULL),
ricow@chromium.org27bf2882011-11-17 08:34:43 +000059 global_scope_(NULL),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000060 script_(script),
61 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000062 pre_parse_data_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000063 osr_ast_id_(AstNode::kNoNumber),
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000064 zone_(zone),
65 deferred_handles_(NULL) {
yangguo@chromium.org56454712012-02-16 15:33:53 +000066 Initialize(BASE);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000067}
68
69
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000070CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info,
71 Zone* zone)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000072 : isolate_(shared_info->GetIsolate()),
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000073 flags_(LanguageModeField::encode(CLASSIC_MODE) |
74 IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000075 function_(NULL),
76 scope_(NULL),
ricow@chromium.org27bf2882011-11-17 08:34:43 +000077 global_scope_(NULL),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000078 shared_info_(shared_info),
79 script_(Handle<Script>(Script::cast(shared_info->script()))),
80 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000081 pre_parse_data_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000082 osr_ast_id_(AstNode::kNoNumber),
yangguo@chromium.org99aa4902012-07-06 16:21:55 +000083 zone_(zone),
84 deferred_handles_(NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +000085 Initialize(BASE);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000086}
87
88
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000089CompilationInfo::CompilationInfo(Handle<JSFunction> closure, Zone* zone)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 : isolate_(closure->GetIsolate()),
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +000091 flags_(LanguageModeField::encode(CLASSIC_MODE) |
92 IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000093 function_(NULL),
94 scope_(NULL),
ricow@chromium.org27bf2882011-11-17 08:34:43 +000095 global_scope_(NULL),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000096 closure_(closure),
97 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
98 script_(Handle<Script>(Script::cast(shared_info_->script()))),
99 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000100 pre_parse_data_(NULL),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000101 osr_ast_id_(AstNode::kNoNumber),
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000102 zone_(zone),
103 deferred_handles_(NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000104 Initialize(BASE);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000105}
106
107
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000108CompilationInfo::~CompilationInfo() {
109 delete deferred_handles_;
110}
111
112
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000113// Disable optimization for the rest of the compilation pipeline.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000114void CompilationInfo::DisableOptimization() {
lrn@chromium.org1c092762011-05-09 09:42:16 +0000115 bool is_optimizable_closure =
116 FLAG_optimize_closures &&
117 closure_.is_null() &&
118 !scope_->HasTrivialOuterContext() &&
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000119 !scope_->outer_scope_calls_non_strict_eval() &&
lrn@chromium.org1c092762011-05-09 09:42:16 +0000120 !scope_->inside_with();
121 SetMode(is_optimizable_closure ? BASE : NONOPT);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000122}
123
124
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000125// Primitive functions are unlikely to be picked up by the stack-walking
126// profiler, so they trigger their own optimization when they're called
127// for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time.
128bool CompilationInfo::ShouldSelfOptimize() {
129 return FLAG_self_optimization &&
130 FLAG_crankshaft &&
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000131 !function()->flags()->Contains(kDontSelfOptimize) &&
yangguo@chromium.orga7d3df92012-02-27 11:46:55 +0000132 !function()->flags()->Contains(kDontOptimize) &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000133 function()->scope()->AllowsLazyCompilation() &&
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000134 (shared_info().is_null() || !shared_info()->optimization_disabled());
135}
136
137
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000138void CompilationInfo::AbortOptimization() {
139 Handle<Code> code(shared_info()->code());
140 SetCode(code);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000141}
142
143
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000144// Determine whether to use the full compiler for all code. If the flag
145// --always-full-compiler is specified this is the case. For the virtual frame
146// based compiler the full compiler is also used if a debugger is connected, as
147// the code from the full compiler supports mode precise break points. For the
148// crankshaft adaptive compiler debugging the optimized code is not possible at
149// all. However crankshaft support recompilation of functions, so in this case
150// the full compiler need not be be used if a debugger is attached, but only if
151// break points has actually been set.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000152static bool IsDebuggerActive(Isolate* isolate) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000153#ifdef ENABLE_DEBUGGER_SUPPORT
lrn@chromium.org1c092762011-05-09 09:42:16 +0000154 return V8::UseCrankshaft() ?
155 isolate->debug()->has_break_points() :
156 isolate->debugger()->IsDebuggerActive();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000157#else
lrn@chromium.org1c092762011-05-09 09:42:16 +0000158 return false;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000159#endif
160}
161
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000162
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000163static bool AlwaysFullCompiler(Isolate* isolate) {
164 return FLAG_always_full_compiler || IsDebuggerActive(isolate);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000165}
166
167
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000168static void FinishOptimization(Handle<JSFunction> function, int64_t start) {
169 int opt_count = function->shared()->opt_count();
170 function->shared()->set_opt_count(opt_count + 1);
lrn@chromium.org8541d772010-12-15 12:05:09 +0000171 double ms = static_cast<double>(OS::Ticks() - start) / 1000;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000172 if (FLAG_trace_opt) {
173 PrintF("[optimizing: ");
174 function->PrintName();
175 PrintF(" / %" V8PRIxPTR, reinterpret_cast<intptr_t>(*function));
176 PrintF(" - took %0.3f ms]\n", ms);
177 }
178 if (FLAG_trace_opt_stats) {
179 static double compilation_time = 0.0;
180 static int compiled_functions = 0;
181 static int code_size = 0;
182
183 compilation_time += ms;
184 compiled_functions++;
185 code_size += function->shared()->SourceSize();
186 PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
187 compiled_functions,
188 code_size,
189 compilation_time);
190 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000191}
192
193
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000194static bool MakeCrankshaftCode(CompilationInfo* info) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000195 ASSERT(V8::UseCrankshaft());
196 ASSERT(info->IsOptimizing());
197 ASSERT(!info->IsCompilingForDebugging());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000198
199 // We should never arrive here if there is not code object on the
200 // shared function object.
201 Handle<Code> code(info->shared_info()->code());
202 ASSERT(code->kind() == Code::FUNCTION);
203
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000204 // We should never arrive here if optimization has been disabled on the
205 // shared function info.
206 ASSERT(!info->shared_info()->optimization_disabled());
207
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000208 // Fall back to using the full code generator if it's not possible
209 // to use the Hydrogen-based optimizing compiler. We already have
210 // generated code for this from the shared function object.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000211 if (AlwaysFullCompiler(info->isolate())) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000212 info->SetCode(code);
213 return true;
214 }
215
216 // Limit the number of times we re-compile a functions with
217 // the optimizing compiler.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000218 const int kMaxOptCount =
219 FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000220 if (info->shared_info()->opt_count() > kMaxOptCount) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000221 info->AbortOptimization();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000222 info->shared_info()->DisableOptimization();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000223 // True indicates the compilation pipeline is still going, not
224 // necessarily that we optimized the code.
225 return true;
226 }
227
228 // Due to an encoding limit on LUnallocated operands in the Lithium
229 // language, we cannot optimize functions with too many formal parameters
230 // or perform on-stack replacement for function with too many
231 // stack-allocated local variables.
232 //
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000233 // The encoding is as a signed value, with parameters and receiver using
234 // the negative indices and locals the non-negative ones.
ager@chromium.org04921a82011-06-27 13:21:41 +0000235 const int parameter_limit = -LUnallocated::kMinFixedIndex;
236 const int locals_limit = LUnallocated::kMaxFixedIndex;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000237 Scope* scope = info->scope();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000238 if ((scope->num_parameters() + 1) > parameter_limit ||
ager@chromium.org04921a82011-06-27 13:21:41 +0000239 (info->osr_ast_id() != AstNode::kNoNumber &&
240 scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit)) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000241 info->AbortOptimization();
yangguo@chromium.org56454712012-02-16 15:33:53 +0000242 info->shared_info()->DisableOptimization();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000243 // True indicates the compilation pipeline is still going, not
244 // necessarily that we optimized the code.
245 return true;
246 }
247
248 // Take --hydrogen-filter into account.
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000249 Handle<String> name = info->function()->debug_name();
mstarzinger@chromium.org3233d2f2012-03-14 11:16:03 +0000250 if (*FLAG_hydrogen_filter != '\0') {
251 Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
252 if ((filter[0] == '-'
253 && name->IsEqualTo(filter.SubVector(1, filter.length())))
254 || (filter[0] != '-' && !name->IsEqualTo(filter))) {
255 info->SetCode(code);
256 return true;
257 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000258 }
259
260 // Recompile the unoptimized version of the code if the current version
261 // doesn't have deoptimization support. Alternatively, we may decide to
262 // run the full code generator to get a baseline for the compile-time
263 // performance of the hydrogen-based compiler.
264 int64_t start = OS::Ticks();
265 bool should_recompile = !info->shared_info()->has_deoptimization_support();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000266 if (should_recompile || FLAG_hydrogen_stats) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000267 HPhase phase(HPhase::kFullCodeGen);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000268 CompilationInfoWithZone unoptimized(info->shared_info());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000269 // Note that we use the same AST that we will use for generating the
270 // optimized code.
271 unoptimized.SetFunction(info->function());
272 unoptimized.SetScope(info->scope());
273 if (should_recompile) unoptimized.EnableDeoptimizationSupport();
274 bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
275 if (should_recompile) {
276 if (!succeeded) return false;
277 Handle<SharedFunctionInfo> shared = info->shared_info();
278 shared->EnableDeoptimizationSupport(*unoptimized.code());
279 // The existing unoptimized code was replaced with the new one.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000280 Compiler::RecordFunctionCompilation(
281 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000282 }
283 }
284
285 // Check that the unoptimized, shared code is ready for
286 // optimizations. When using the always_opt flag we disregard the
287 // optimizable marker in the code object and optimize anyway. This
288 // is safe as long as the unoptimized code has deoptimization
289 // support.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000290 ASSERT(FLAG_always_opt || code->optimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000291 ASSERT(info->shared_info()->has_deoptimization_support());
292
293 if (FLAG_trace_hydrogen) {
294 PrintF("-----------------------------------------------------------\n");
295 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
296 HTracer::Instance()->TraceCompilation(info->function());
297 }
298
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000299 Handle<Context> global_context(info->closure()->context()->global_context());
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000300 TypeFeedbackOracle oracle(code, global_context, info->isolate(),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000301 info->zone());
302 HGraphBuilder builder(info, &oracle);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303 HPhase phase(HPhase::kTotal);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000304 HGraph* graph = builder.CreateGraph();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000305 if (info->isolate()->has_pending_exception()) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000306 info->SetCode(Handle<Code>::null());
307 return false;
308 }
309
rossberg@chromium.org994edf62012-02-06 10:12:55 +0000310 if (graph != NULL) {
jkummerow@chromium.org28583c92012-07-16 11:31:55 +0000311 SmartArrayPointer<char> bailout_reason;
312 if (!graph->Optimize(&bailout_reason)) {
313 if (!bailout_reason.is_empty()) builder.Bailout(*bailout_reason);
314 } else {
315 LChunk* chunk = LChunk::NewChunk(graph);
316 if (chunk != NULL) {
317 Handle<Code> optimized_code = chunk->Codegen();
318 if (!optimized_code.is_null()) {
319 info->SetCode(optimized_code);
320 FinishOptimization(info->closure(), start);
321 return true;
322 }
323 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000324 }
325 }
326
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000327 // Keep using the shared code.
328 info->AbortOptimization();
329 if (!builder.inline_bailout()) {
330 // Mark the shared code as unoptimizable unless it was an inlined
331 // function that bailed out.
yangguo@chromium.org56454712012-02-16 15:33:53 +0000332 info->shared_info()->DisableOptimization();
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000333 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000334 // True indicates the compilation pipeline is still going, not necessarily
335 // that we optimized the code.
336 return true;
337}
338
339
lrn@chromium.org1c092762011-05-09 09:42:16 +0000340static bool GenerateCode(CompilationInfo* info) {
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000341 bool is_optimizing = V8::UseCrankshaft() &&
342 !info->IsCompilingForDebugging() &&
343 info->IsOptimizing();
344 if (is_optimizing) {
345 return MakeCrankshaftCode(info);
346 } else {
347 if (info->IsOptimizing()) {
348 // Have the CompilationInfo decide if the compilation should be
349 // BASE or NONOPT.
350 info->DisableOptimization();
351 }
352 return FullCodeGenerator::MakeCode(info);
353 }
lrn@chromium.org1c092762011-05-09 09:42:16 +0000354}
355
356
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000357static bool MakeCode(CompilationInfo* info) {
358 // Precondition: code has been parsed. Postcondition: the code field in
359 // the compilation info is set if compilation succeeded.
360 ASSERT(info->function() != NULL);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000361 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000362}
363
364
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000365#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000366bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
367 // Precondition: code has been parsed. Postcondition: the code field in
368 // the compilation info is set if compilation succeeded.
369 bool succeeded = MakeCode(info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000370 if (!info->shared_info().is_null()) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000371 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000372 info->zone());
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000373 info->shared_info()->set_scope_info(*scope_info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000374 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000375 return succeeded;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000376}
377#endif
378
379
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000380static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000381 Isolate* isolate = info->isolate();
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000382 ZoneScope zone_scope(info->zone(), DELETE_ON_EXIT);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000383 PostponeInterruptsScope postpone(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000385 ASSERT(!isolate->global_context().is_null());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000386 Handle<Script> script = info->script();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000387 script->set_context_data((*isolate->global_context())->data());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000388
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000389#ifdef ENABLE_DEBUGGER_SUPPORT
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000390 if (info->is_eval()) {
391 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000392 script->set_compilation_type(Smi::FromInt(compilation_type));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000393 // For eval scripts add information on the function from which eval was
394 // called.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000395 if (info->is_eval()) {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000396 StackTraceFrameIterator it(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000397 if (!it.done()) {
398 script->set_eval_from_shared(
399 JSFunction::cast(it.frame()->function())->shared());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000400 Code* code = it.frame()->LookupCode();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000401 int offset = static_cast<int>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000402 it.frame()->pc() - code->instruction_start());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000403 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
404 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000405 }
406 }
407
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000409 isolate->debugger()->OnBeforeCompile(script);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000410#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412 // Only allow non-global compiles for eval.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000413 ASSERT(info->is_eval() || info->is_global());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000414 ParsingFlags flags = kNoParsingFlags;
415 if (info->pre_parse_data() != NULL ||
416 String::cast(script->source())->length() > FLAG_min_preparse_length) {
417 flags = kAllowLazy;
418 }
419 if (!ParserApi::Parse(info, flags)) {
420 return Handle<SharedFunctionInfo>::null();
421 }
kasper.lund212ac232008-07-16 07:07:30 +0000422
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423 // Measure how long it takes to do the compilation; only take the
424 // rest of the function into account to avoid overlap with the
425 // parsing statistics.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000426 HistogramTimer* rate = info->is_eval()
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000427 ? info->isolate()->counters()->compile_eval()
428 : info->isolate()->counters()->compile();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000429 HistogramTimerScope timer(rate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000430
431 // Compile the code.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000432 FunctionLiteral* lit = info->function();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000433 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000434 if (!MakeCode(info)) {
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000435 if (!isolate->has_pending_exception()) isolate->StackOverflow();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000436 return Handle<SharedFunctionInfo>::null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 }
438
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000439 // Allocate function.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000440 ASSERT(!info->code().is_null());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000441 Handle<SharedFunctionInfo> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000442 isolate->factory()->NewSharedFunctionInfo(
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000443 lit->name(),
444 lit->materialized_literal_count(),
445 info->code(),
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000446 ScopeInfo::Create(info->scope(), info->zone()));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000447
448 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
449 Compiler::SetFunctionInfo(result, lit, true, script);
450
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000451 if (script->name()->IsString()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000452 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000453 info->is_eval()
454 ? Logger::EVAL_TAG
455 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
456 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000457 *result,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000458 String::cast(script->name())));
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000459 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
460 script,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000461 info->code(),
462 info));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000463 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000464 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000465 info->is_eval()
466 ? Logger::EVAL_TAG
467 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
468 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000469 *result,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000470 isolate->heap()->empty_string()));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000471 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 }
473
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 // Hint to the runtime system used when allocating space for initial
475 // property space by setting the expected number of properties for
476 // the instances of the function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000477 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000478
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000479 script->set_compilation_state(
480 Smi::FromInt(Script::COMPILATION_STATE_COMPILED));
481
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000482#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000483 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000484 isolate->debugger()->OnAfterCompile(
485 script, Debugger::NO_AFTER_COMPILE_FLAGS);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000486#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000488 live_edit_tracker.RecordFunctionInfo(result, lit, info->zone());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000489
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000490 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491}
492
493
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000494Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
495 Handle<Object> script_name,
496 int line_offset,
497 int column_offset,
498 v8::Extension* extension,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000499 ScriptDataImpl* pre_data,
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000500 Handle<Object> script_data,
501 NativesFlag natives) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000502 Isolate* isolate = source->GetIsolate();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000503 int source_length = source->length();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000504 isolate->counters()->total_load_size()->Increment(source_length);
505 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506
507 // The VM is in the COMPILER state until exiting this function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000508 VMState state(isolate, COMPILER);
509
510 CompilationCache* compilation_cache = isolate->compilation_cache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000512 // Do a lookup in the compilation cache but not for extensions.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000513 Handle<SharedFunctionInfo> result;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000514 if (extension == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515 result = compilation_cache->LookupScript(source,
516 script_name,
517 line_offset,
518 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519 }
520
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000521 if (result.is_null()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000522 // No cache entry found. Do pre-parsing, if it makes sense, and compile
523 // the script.
524 // Building preparse data that is only used immediately after is only a
525 // saving if we might skip building the AST for lazily compiled functions.
526 // I.e., preparse data isn't relevant when the lazy flag is off, and
527 // for small sources, odds are that there aren't many functions
528 // that would be compiled lazily anyway, so we skip the preparse step
529 // in that case too.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000530
531 // Create a script object describing the script to be compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 Handle<Script> script = FACTORY->NewScript(source);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000533 if (natives == NATIVES_CODE) {
534 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
535 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000536 if (!script_name.is_null()) {
537 script->set_name(*script_name);
538 script->set_line_offset(Smi::FromInt(line_offset));
539 script->set_column_offset(Smi::FromInt(column_offset));
540 }
541
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000542 script->set_data(script_data.is_null() ? HEAP->undefined_value()
ager@chromium.org5c838252010-02-19 08:53:10 +0000543 : *script_data);
544
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000545 // Compile the function and add it to the cache.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000546 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000547 info.MarkAsGlobal();
548 info.SetExtension(extension);
549 info.SetPreParseData(pre_data);
rossberg@chromium.org2c067b12012-03-19 11:01:52 +0000550 if (FLAG_use_strict) {
551 info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE);
552 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000553 result = MakeFunctionInfo(&info);
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000554 if (extension == NULL && !result.is_null() && !result->dont_cache()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000555 compilation_cache->PutScript(source, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000556 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000557 } else {
558 if (result->ic_age() != HEAP->global_ic_age()) {
559 result->ResetForNewContext(HEAP->global_ic_age());
560 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561 }
562
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563 if (result.is_null()) isolate->ReportPendingMessages();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 return result;
565}
566
567
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000568Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
569 Handle<Context> context,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000570 bool is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000571 LanguageMode language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000572 int scope_position) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573 Isolate* isolate = source->GetIsolate();
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000574 int source_length = source->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000575 isolate->counters()->total_eval_size()->Increment(source_length);
576 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577
578 // The VM is in the COMPILER state until exiting this function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000579 VMState state(isolate, COMPILER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000581 // Do a lookup in the compilation cache; if the entry is not there, invoke
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000582 // the compiler and add the result to the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000583 Handle<SharedFunctionInfo> result;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000584 CompilationCache* compilation_cache = isolate->compilation_cache();
585 result = compilation_cache->LookupEval(source,
586 context,
587 is_global,
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000588 language_mode,
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +0000589 scope_position);
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000590
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000591 if (result.is_null()) {
592 // Create a script object describing the script to be compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000593 Handle<Script> script = isolate->factory()->NewScript(source);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000594 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000595 info.MarkAsEval();
596 if (is_global) info.MarkAsGlobal();
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000597 info.SetLanguageMode(language_mode);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000598 info.SetCallingContext(context);
599 result = MakeFunctionInfo(&info);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000600 if (!result.is_null()) {
yangguo@chromium.org56454712012-02-16 15:33:53 +0000601 // Explicitly disable optimization for eval code. We're not yet prepared
602 // to handle eval-code in the optimizing compiler.
603 result->DisableOptimization();
604
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000605 // If caller is strict mode, the result must be in strict mode or
606 // extended mode as well, but not the other way around. Consider:
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000607 // eval("'use strict'; ...");
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000608 ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode());
609 // If caller is in extended mode, the result must also be in
610 // extended mode.
611 ASSERT(language_mode != EXTENDED_MODE ||
612 result->is_extended_mode());
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000613 if (!result->dont_cache()) {
614 compilation_cache->PutEval(
615 source, context, is_global, result, scope_position);
616 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000617 }
jkummerow@chromium.org1456e702012-03-30 08:38:13 +0000618 } else {
619 if (result->ic_age() != HEAP->global_ic_age()) {
620 result->ResetForNewContext(HEAP->global_ic_age());
621 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000622 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000623
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000624 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625}
626
627
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000628bool Compiler::CompileLazy(CompilationInfo* info) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000629 Isolate* isolate = info->isolate();
630
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000631 ZoneScope zone_scope(info->zone(), DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632
633 // The VM is in the COMPILER state until exiting this function.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000634 VMState state(isolate, COMPILER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000636 PostponeInterruptsScope postpone(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000638 Handle<SharedFunctionInfo> shared = info->shared_info();
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000639 int compiled_size = shared->end_position() - shared->start_position();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000640 isolate->counters()->total_compile_size()->Increment(compiled_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000642 if (FLAG_cache_optimized_code && info->IsOptimizing()) {
643 Handle<JSFunction> function = info->closure();
644 ASSERT(!function.is_null());
645 Handle<Context> global_context(function->context()->global_context());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000646 int index = shared->SearchOptimizedCodeMap(*global_context);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000647 if (index > 0) {
648 if (FLAG_trace_opt) {
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000649 PrintF("[found optimized code for: ");
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000650 function->PrintName();
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000651 PrintF(" / %" V8PRIxPTR "]\n", reinterpret_cast<intptr_t>(*function));
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000652 }
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000653 // Caching of optimized code enabled and optimized code found.
654 shared->InstallFromOptimizedCodeMap(*function, index);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000655 return true;
656 }
657 }
658
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000659 // Generate the AST for the lazily compiled function.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000660 if (ParserApi::Parse(info, kNoParsingFlags)) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000661 // Measure how long it takes to do the lazy compilation; only take the
662 // rest of the function into account to avoid overlap with the lazy
663 // parsing statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000664 HistogramTimerScope timer(isolate->counters()->compile_lazy());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000666 // After parsing we know the function's language mode. Remember it.
667 LanguageMode language_mode = info->function()->language_mode();
668 info->SetLanguageMode(language_mode);
669 shared->set_language_mode(language_mode);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000670
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000671 // Compile the code.
672 if (!MakeCode(info)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000673 if (!isolate->has_pending_exception()) {
674 isolate->StackOverflow();
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000675 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000676 } else {
677 ASSERT(!info->code().is_null());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000678 Handle<Code> code = info->code();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000679 // Set optimizable to false if this is disallowed by the shared
680 // function info, e.g., we might have flushed the code and must
681 // reset this bit when lazy compiling the code again.
682 if (shared->optimization_disabled()) code->set_optimizable(false);
683
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000684 Handle<JSFunction> function = info->closure();
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000685 RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
kasper.lund212ac232008-07-16 07:07:30 +0000686
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000687 if (info->IsOptimizing()) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000688 ASSERT(shared->scope_info() != ScopeInfo::Empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000689 function->ReplaceCode(*code);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000690 if (FLAG_cache_optimized_code &&
691 code->kind() == Code::OPTIMIZED_FUNCTION) {
692 Handle<SharedFunctionInfo> shared(function->shared());
ulan@chromium.orgd9e468a2012-06-25 09:47:40 +0000693 Handle<FixedArray> literals(function->literals());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000694 Handle<Context> global_context(function->context()->global_context());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000695 SharedFunctionInfo::AddToOptimizedCodeMap(
696 shared, global_context, code, literals);
697 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000698 } else {
699 // Update the shared function info with the compiled code and the
700 // scope info. Please note, that the order of the shared function
701 // info initialization is important since set_scope_info might
702 // trigger a GC, causing the ASSERT below to be invalid if the code
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000703 // was flushed. By setting the code object last we avoid this.
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000704 Handle<ScopeInfo> scope_info =
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000705 ScopeInfo::Create(info->scope(), info->zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000706 shared->set_scope_info(*scope_info);
707 shared->set_code(*code);
708 if (!function.is_null()) {
709 function->ReplaceCode(*code);
710 ASSERT(!function->IsOptimized());
711 }
712
713 // Set the expected number of properties for instances.
714 FunctionLiteral* lit = info->function();
715 int expected = lit->expected_property_count();
716 SetExpectedNofPropertiesFromEstimate(shared, expected);
717
718 // Set the optimization hints after performing lazy compilation, as
719 // these are not set when the function is set up as a lazily
720 // compiled function.
721 shared->SetThisPropertyAssignmentsInfo(
722 lit->has_only_simple_this_property_assignments(),
723 *lit->this_property_assignments());
724
725 // Check the function has compiled code.
726 ASSERT(shared->is_compiled());
727 shared->set_code_age(0);
yangguo@chromium.org56454712012-02-16 15:33:53 +0000728 shared->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000729 shared->set_dont_inline(lit->flags()->Contains(kDontInline));
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000730 shared->set_dont_cache(lit->flags()->Contains(kDontCache));
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +0000731 shared->set_ast_node_count(lit->ast_node_count());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000732
yangguo@chromium.org56454712012-02-16 15:33:53 +0000733 if (V8::UseCrankshaft()&&
734 !function.is_null() &&
735 !shared->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000736 // If we're asked to always optimize, we compile the optimized
737 // version of the function right away - unless the debugger is
738 // active as it makes no sense to compile optimized code then.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000739 if (FLAG_always_opt &&
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000740 !Isolate::Current()->DebuggerHasBreakPoints()) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000741 CompilationInfoWithZone optimized(function);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000742 optimized.SetOptimizing(AstNode::kNoNumber);
743 return CompileLazy(&optimized);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000744 }
745 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000746 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000748 return true;
749 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000750 }
751
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000752 ASSERT(info->code().is_null());
753 return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754}
755
756
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000757Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000758 Handle<Script> script) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000759 // Precondition: code has been parsed and scopes have been analyzed.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000760 CompilationInfoWithZone info(script);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000761 info.SetFunction(literal);
762 info.SetScope(literal->scope());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000763 info.SetLanguageMode(literal->scope()->language_mode());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000764
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000765 LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000766 // Determine if the function can be lazily compiled. This is necessary to
767 // allow some of our builtin JS files to be lazily compiled. These
768 // builtins cannot be handled lazily by the parser, since we have to know
769 // if a function uses the special natives syntax, which is something the
770 // parser records.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000771 // If the debugger requests compilation for break points, we cannot be
772 // aggressive about lazy compilation, because it might trigger compilation
773 // of functions without an outer context when setting a breakpoint through
774 // Runtime::FindSharedFunctionInfoInScript.
775 bool allow_lazy_without_ctx = literal->AllowsLazyCompilationWithoutContext();
ager@chromium.org5c838252010-02-19 08:53:10 +0000776 bool allow_lazy = literal->AllowsLazyCompilation() &&
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000777 !LiveEditFunctionTracker::IsActive(info.isolate()) &&
778 (!info.isolate()->DebuggerHasBreakPoints() || allow_lazy_without_ctx);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000779
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000780 Handle<ScopeInfo> scope_info(ScopeInfo::Empty());
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000781
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000782 // Generate code
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000783 if (FLAG_lazy && allow_lazy) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000784 Handle<Code> code = info.isolate()->builtins()->LazyCompile();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000785 info.SetCode(code);
yangguo@chromium.org99aa4902012-07-06 16:21:55 +0000786 } else if (GenerateCode(&info)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000787 ASSERT(!info.code().is_null());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000788 scope_info = ScopeInfo::Create(info.scope(), info.zone());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000789 } else {
790 return Handle<SharedFunctionInfo>::null();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000791 }
792
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000793 // Create a shared function info object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000794 Handle<SharedFunctionInfo> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000795 FACTORY->NewSharedFunctionInfo(literal->name(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000796 literal->materialized_literal_count(),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000797 info.code(),
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000798 scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000799 SetFunctionInfo(result, literal, false, script);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000800 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000801 result->set_allows_lazy_compilation(allow_lazy);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000802 result->set_allows_lazy_compilation_without_context(allow_lazy_without_ctx);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000803
804 // Set the expected number of properties for instances and return
805 // the resulting function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000806 SetExpectedNofPropertiesFromEstimate(result,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000807 literal->expected_property_count());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000808 live_edit_tracker.RecordFunctionInfo(result, literal, info.zone());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000809 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000810}
811
812
813// Sets the function info on a function.
814// The start_position points to the first '(' character after the function name
815// in the full script source. When counting characters in the script source the
816// the first character is number 0 (not 1).
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000817void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000818 FunctionLiteral* lit,
819 bool is_toplevel,
820 Handle<Script> script) {
danno@chromium.orgc612e022011-11-10 11:38:15 +0000821 function_info->set_length(lit->parameter_count());
822 function_info->set_formal_parameter_count(lit->parameter_count());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000823 function_info->set_script(*script);
824 function_info->set_function_token_position(lit->function_token_position());
825 function_info->set_start_position(lit->start_position());
826 function_info->set_end_position(lit->end_position());
827 function_info->set_is_expression(lit->is_expression());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000828 function_info->set_is_anonymous(lit->is_anonymous());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000829 function_info->set_is_toplevel(is_toplevel);
830 function_info->set_inferred_name(*lit->inferred_name());
831 function_info->SetThisPropertyAssignmentsInfo(
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000832 lit->has_only_simple_this_property_assignments(),
833 *lit->this_property_assignments());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000834 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000835 function_info->set_allows_lazy_compilation_without_context(
836 lit->AllowsLazyCompilationWithoutContext());
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000837 function_info->set_language_mode(lit->language_mode());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000838 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
839 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000840 function_info->set_ast_node_count(lit->ast_node_count());
yangguo@chromium.org56454712012-02-16 15:33:53 +0000841 function_info->set_is_function(lit->is_function());
842 function_info->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
svenpanne@chromium.orgb1df11d2012-02-08 10:26:21 +0000843 function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
danno@chromium.org81cac2b2012-07-10 11:28:27 +0000844 function_info->set_dont_cache(lit->flags()->Contains(kDontCache));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000845}
846
847
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000848void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000849 CompilationInfo* info,
850 Handle<SharedFunctionInfo> shared) {
851 // SharedFunctionInfo is passed separately, because if CompilationInfo
852 // was created using Script object, it will not have it.
853
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000854 // Log the code generation. If source information is available include
855 // script name and line number. Check explicitly whether logging is
856 // enabled as finding the line number is not free.
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000857 if (info->isolate()->logger()->is_logging() ||
858 CpuProfiler::is_profiling(info->isolate())) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000859 Handle<Script> script = info->script();
860 Handle<Code> code = info->code();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000861 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000862 return;
ager@chromium.org5c838252010-02-19 08:53:10 +0000863 if (script->name()->IsString()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000864 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000865 USE(line_num);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000866 PROFILE(info->isolate(),
867 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000868 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000869 *shared,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000870 String::cast(script->name()),
871 line_num));
ager@chromium.org5c838252010-02-19 08:53:10 +0000872 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000873 PROFILE(info->isolate(),
874 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000875 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000876 *shared,
877 shared->DebugName()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000878 }
879 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000880
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000881 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000882 Handle<Script>(info->script()),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000883 Handle<Code>(info->code()),
884 info));
ager@chromium.org5c838252010-02-19 08:53:10 +0000885}
ager@chromium.org5c838252010-02-19 08:53:10 +0000886
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887} } // namespace v8::internal