blob: 227233773931ac0418fe7338f99a326617b8c5f9 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Ben Murdochf87a2032010-10-22 12:50:53 +010030#include "compiler.h"
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "bootstrapper.h"
Ben Murdoch8b112d22011-06-08 16:22:53 +010033#include "codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "compilation-cache.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#include "debug.h"
Leon Clarked91b9f72010-01-27 17:25:45 +000036#include "full-codegen.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +010037#include "gdb-jit.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010038#include "hydrogen.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039#include "isolate-inl.h"
Steve Block1e0659c2011-05-24 12:43:12 +010040#include "lithium.h"
Steve Block6ded16b2010-05-10 14:33:55 +010041#include "liveedit.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010042#include "parser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000043#include "rewriter.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010044#include "runtime-profiler.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000045#include "scanner-character-streams.h"
Ben Murdoch3bec4d22010-07-22 14:51:16 +010046#include "scopeinfo.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010047#include "scopes.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010048#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000049
50namespace v8 {
51namespace internal {
52
Ben Murdochf87a2032010-10-22 12:50:53 +010053
54CompilationInfo::CompilationInfo(Handle<Script> script)
Steve Block44f0eee2011-05-26 01:26:41 +010055 : isolate_(script->GetIsolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056 flags_(LanguageModeField::encode(CLASSIC_MODE)),
Ben Murdochf87a2032010-10-22 12:50:53 +010057 function_(NULL),
58 scope_(NULL),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010059 global_scope_(NULL),
Ben Murdochf87a2032010-10-22 12:50:53 +010060 script_(script),
61 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010062 pre_parse_data_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010063 osr_ast_id_(AstNode::kNoNumber) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +010064 Initialize(BASE);
Ben Murdochf87a2032010-10-22 12:50:53 +010065}
66
67
68CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info)
Steve Block44f0eee2011-05-26 01:26:41 +010069 : isolate_(shared_info->GetIsolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010070 flags_(LanguageModeField::encode(CLASSIC_MODE) |
71 IsLazy::encode(true)),
Ben Murdochf87a2032010-10-22 12:50:53 +010072 function_(NULL),
73 scope_(NULL),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010074 global_scope_(NULL),
Ben Murdochf87a2032010-10-22 12:50:53 +010075 shared_info_(shared_info),
76 script_(Handle<Script>(Script::cast(shared_info->script()))),
77 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010078 pre_parse_data_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010079 osr_ast_id_(AstNode::kNoNumber) {
80 Initialize(BASE);
Ben Murdochf87a2032010-10-22 12:50:53 +010081}
82
83
84CompilationInfo::CompilationInfo(Handle<JSFunction> closure)
Steve Block44f0eee2011-05-26 01:26:41 +010085 : isolate_(closure->GetIsolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010086 flags_(LanguageModeField::encode(CLASSIC_MODE) |
87 IsLazy::encode(true)),
Ben Murdochf87a2032010-10-22 12:50:53 +010088 function_(NULL),
89 scope_(NULL),
Ben Murdoch3ef787d2012-04-12 10:51:47 +010090 global_scope_(NULL),
Ben Murdochf87a2032010-10-22 12:50:53 +010091 closure_(closure),
92 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
93 script_(Handle<Script>(Script::cast(shared_info_->script()))),
94 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010095 pre_parse_data_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010096 osr_ast_id_(AstNode::kNoNumber) {
97 Initialize(BASE);
Ben Murdochf87a2032010-10-22 12:50:53 +010098}
99
100
Ben Murdoch257744e2011-11-30 15:57:28 +0000101// Disable optimization for the rest of the compilation pipeline.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100102void CompilationInfo::DisableOptimization() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000103 bool is_optimizable_closure =
104 FLAG_optimize_closures &&
105 closure_.is_null() &&
106 !scope_->HasTrivialOuterContext() &&
107 !scope_->outer_scope_calls_non_strict_eval() &&
108 !scope_->inside_with();
109 SetMode(is_optimizable_closure ? BASE : NONOPT);
110}
Ben Murdochb8e0da22011-05-16 14:20:40 +0100111
Ben Murdoch257744e2011-11-30 15:57:28 +0000112
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100113// Primitive functions are unlikely to be picked up by the stack-walking
114// profiler, so they trigger their own optimization when they're called
115// for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time.
116bool CompilationInfo::ShouldSelfOptimize() {
117 return FLAG_self_optimization &&
118 FLAG_crankshaft &&
119 !function()->flags()->Contains(kDontSelfOptimize) &&
120 !function()->flags()->Contains(kDontOptimize) &&
121 function()->scope()->allows_lazy_recompilation() &&
122 (shared_info().is_null() || !shared_info()->optimization_disabled());
123}
124
125
Ben Murdoch257744e2011-11-30 15:57:28 +0000126void CompilationInfo::AbortOptimization() {
127 Handle<Code> code(shared_info()->code());
128 SetCode(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100129}
130
131
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132// Determine whether to use the full compiler for all code. If the flag
133// --always-full-compiler is specified this is the case. For the virtual frame
134// based compiler the full compiler is also used if a debugger is connected, as
135// the code from the full compiler supports mode precise break points. For the
136// crankshaft adaptive compiler debugging the optimized code is not possible at
137// all. However crankshaft support recompilation of functions, so in this case
138// the full compiler need not be be used if a debugger is attached, but only if
139// break points has actually been set.
Ben Murdoch257744e2011-11-30 15:57:28 +0000140static bool is_debugging_active() {
Leon Clarkef7060e22010-06-03 12:02:55 +0100141#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100142 Isolate* isolate = Isolate::Current();
Ben Murdoch257744e2011-11-30 15:57:28 +0000143 return V8::UseCrankshaft() ?
144 isolate->debug()->has_break_points() :
145 isolate->debugger()->IsDebuggerActive();
Leon Clarkef7060e22010-06-03 12:02:55 +0100146#else
Ben Murdoch257744e2011-11-30 15:57:28 +0000147 return false;
Leon Clarkef7060e22010-06-03 12:02:55 +0100148#endif
149}
150
Steve Block3ce2e202009-11-05 08:53:23 +0000151
Ben Murdoch257744e2011-11-30 15:57:28 +0000152static bool AlwaysFullCompiler() {
153 return FLAG_always_full_compiler || is_debugging_active();
154}
155
156
Ben Murdochb0fe1622011-05-05 13:52:32 +0100157static void FinishOptimization(Handle<JSFunction> function, int64_t start) {
158 int opt_count = function->shared()->opt_count();
159 function->shared()->set_opt_count(opt_count + 1);
160 double ms = static_cast<double>(OS::Ticks() - start) / 1000;
161 if (FLAG_trace_opt) {
162 PrintF("[optimizing: ");
163 function->PrintName();
164 PrintF(" / %" V8PRIxPTR, reinterpret_cast<intptr_t>(*function));
165 PrintF(" - took %0.3f ms]\n", ms);
166 }
167 if (FLAG_trace_opt_stats) {
168 static double compilation_time = 0.0;
169 static int compiled_functions = 0;
170 static int code_size = 0;
171
172 compilation_time += ms;
173 compiled_functions++;
174 code_size += function->shared()->SourceSize();
175 PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
176 compiled_functions,
177 code_size,
178 compilation_time);
179 }
180}
181
182
Ben Murdochb0fe1622011-05-05 13:52:32 +0100183static bool MakeCrankshaftCode(CompilationInfo* info) {
184 // Test if we can optimize this function when asked to. We can only
185 // do this after the scopes are computed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100186 if (!V8::UseCrankshaft()) {
187 info->DisableOptimization();
188 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100189
190 // In case we are not optimizing simply return the code from
191 // the full code generator.
192 if (!info->IsOptimizing()) {
193 return FullCodeGenerator::MakeCode(info);
194 }
195
196 // We should never arrive here if there is not code object on the
197 // shared function object.
198 Handle<Code> code(info->shared_info()->code());
199 ASSERT(code->kind() == Code::FUNCTION);
200
Steve Block44f0eee2011-05-26 01:26:41 +0100201 // We should never arrive here if optimization has been disabled on the
202 // shared function info.
203 ASSERT(!info->shared_info()->optimization_disabled());
204
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205 // Fall back to using the full code generator if it's not possible
206 // to use the Hydrogen-based optimizing compiler. We already have
207 // generated code for this from the shared function object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100208 if (AlwaysFullCompiler()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100209 info->SetCode(code);
210 return true;
211 }
212
213 // Limit the number of times we re-compile a functions with
214 // the optimizing compiler.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100215 const int kMaxOptCount =
216 FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100217 if (info->shared_info()->opt_count() > kMaxOptCount) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000218 info->AbortOptimization();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100219 info->shared_info()->DisableOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100220 // True indicates the compilation pipeline is still going, not
221 // necessarily that we optimized the code.
222 return true;
223 }
224
225 // Due to an encoding limit on LUnallocated operands in the Lithium
226 // language, we cannot optimize functions with too many formal parameters
227 // or perform on-stack replacement for function with too many
228 // stack-allocated local variables.
229 //
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100230 // The encoding is as a signed value, with parameters and receiver using
231 // the negative indices and locals the non-negative ones.
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100232 const int parameter_limit = -LUnallocated::kMinFixedIndex;
233 const int locals_limit = LUnallocated::kMaxFixedIndex;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100234 Scope* scope = info->scope();
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100235 if ((scope->num_parameters() + 1) > parameter_limit ||
236 (info->osr_ast_id() != AstNode::kNoNumber &&
237 scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000238 info->AbortOptimization();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 info->shared_info()->DisableOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100240 // True indicates the compilation pipeline is still going, not
241 // necessarily that we optimized the code.
242 return true;
243 }
244
245 // Take --hydrogen-filter into account.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100246 Handle<String> name = info->function()->debug_name();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100247 if (*FLAG_hydrogen_filter != '\0') {
248 Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
249 if ((filter[0] == '-'
250 && name->IsEqualTo(filter.SubVector(1, filter.length())))
251 || (filter[0] != '-' && !name->IsEqualTo(filter))) {
252 info->SetCode(code);
253 return true;
254 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100255 }
256
257 // Recompile the unoptimized version of the code if the current version
258 // doesn't have deoptimization support. Alternatively, we may decide to
259 // run the full code generator to get a baseline for the compile-time
260 // performance of the hydrogen-based compiler.
261 int64_t start = OS::Ticks();
262 bool should_recompile = !info->shared_info()->has_deoptimization_support();
Steve Block44f0eee2011-05-26 01:26:41 +0100263 if (should_recompile || FLAG_hydrogen_stats) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100264 HPhase phase(HPhase::kFullCodeGen);
265 CompilationInfo unoptimized(info->shared_info());
266 // Note that we use the same AST that we will use for generating the
267 // optimized code.
268 unoptimized.SetFunction(info->function());
269 unoptimized.SetScope(info->scope());
270 if (should_recompile) unoptimized.EnableDeoptimizationSupport();
271 bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
272 if (should_recompile) {
273 if (!succeeded) return false;
274 Handle<SharedFunctionInfo> shared = info->shared_info();
275 shared->EnableDeoptimizationSupport(*unoptimized.code());
276 // The existing unoptimized code was replaced with the new one.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100277 Compiler::RecordFunctionCompilation(
278 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100279 }
280 }
281
282 // Check that the unoptimized, shared code is ready for
283 // optimizations. When using the always_opt flag we disregard the
284 // optimizable marker in the code object and optimize anyway. This
285 // is safe as long as the unoptimized code has deoptimization
286 // support.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100287 ASSERT(FLAG_always_opt || code->optimizable());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100288 ASSERT(info->shared_info()->has_deoptimization_support());
289
290 if (FLAG_trace_hydrogen) {
291 PrintF("-----------------------------------------------------------\n");
292 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
293 HTracer::Instance()->TraceCompilation(info->function());
294 }
295
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100296 Handle<Context> global_context(info->closure()->context()->global_context());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100297 TypeFeedbackOracle oracle(code, global_context, info->isolate());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100298 HGraphBuilder builder(info, &oracle);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100299 HPhase phase(HPhase::kTotal);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100300 HGraph* graph = builder.CreateGraph();
Steve Block44f0eee2011-05-26 01:26:41 +0100301 if (info->isolate()->has_pending_exception()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100302 info->SetCode(Handle<Code>::null());
303 return false;
304 }
305
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100306 if (graph != NULL) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100307 Handle<Code> optimized_code = graph->Compile(info);
308 if (!optimized_code.is_null()) {
309 info->SetCode(optimized_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100310 FinishOptimization(info->closure(), start);
311 return true;
312 }
313 }
314
Ben Murdoch257744e2011-11-30 15:57:28 +0000315 // Keep using the shared code.
316 info->AbortOptimization();
317 if (!builder.inline_bailout()) {
318 // Mark the shared code as unoptimizable unless it was an inlined
319 // function that bailed out.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100320 info->shared_info()->DisableOptimization();
Ben Murdoch257744e2011-11-30 15:57:28 +0000321 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100322 // True indicates the compilation pipeline is still going, not necessarily
323 // that we optimized the code.
324 return true;
325}
326
327
Ben Murdoch257744e2011-11-30 15:57:28 +0000328static bool GenerateCode(CompilationInfo* info) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100329 return info->IsCompilingForDebugging() || !V8::UseCrankshaft() ?
330 FullCodeGenerator::MakeCode(info) :
331 MakeCrankshaftCode(info);
Ben Murdoch257744e2011-11-30 15:57:28 +0000332}
333
334
Ben Murdochf87a2032010-10-22 12:50:53 +0100335static bool MakeCode(CompilationInfo* info) {
336 // Precondition: code has been parsed. Postcondition: the code field in
337 // the compilation info is set if compilation succeeded.
338 ASSERT(info->function() != NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +0000339 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000340}
341
342
Steve Block6ded16b2010-05-10 14:33:55 +0100343#ifdef ENABLE_DEBUGGER_SUPPORT
Ben Murdochf87a2032010-10-22 12:50:53 +0100344bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
345 // Precondition: code has been parsed. Postcondition: the code field in
346 // the compilation info is set if compilation succeeded.
347 bool succeeded = MakeCode(info);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100348 if (!info->shared_info().is_null()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100349 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100350 info->shared_info()->set_scope_info(*scope_info);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100351 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100352 return succeeded;
Steve Block6ded16b2010-05-10 14:33:55 +0100353}
354#endif
355
356
Ben Murdochf87a2032010-10-22 12:50:53 +0100357static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100358 Isolate* isolate = info->isolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000359 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
Steve Block44f0eee2011-05-26 01:26:41 +0100360 PostponeInterruptsScope postpone(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
Steve Block44f0eee2011-05-26 01:26:41 +0100362 ASSERT(!isolate->global_context().is_null());
Ben Murdochf87a2032010-10-22 12:50:53 +0100363 Handle<Script> script = info->script();
Steve Block44f0eee2011-05-26 01:26:41 +0100364 script->set_context_data((*isolate->global_context())->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
Leon Clarke4515c472010-02-03 11:58:03 +0000366#ifdef ENABLE_DEBUGGER_SUPPORT
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800367 if (info->is_eval()) {
368 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
Ben Murdochf87a2032010-10-22 12:50:53 +0100369 script->set_compilation_type(Smi::FromInt(compilation_type));
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 // For eval scripts add information on the function from which eval was
371 // called.
Ben Murdochf87a2032010-10-22 12:50:53 +0100372 if (info->is_eval()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100373 StackTraceFrameIterator it(isolate);
Leon Clarke4515c472010-02-03 11:58:03 +0000374 if (!it.done()) {
375 script->set_eval_from_shared(
376 JSFunction::cast(it.frame()->function())->shared());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100377 Code* code = it.frame()->LookupCode();
Leon Clarke4515c472010-02-03 11:58:03 +0000378 int offset = static_cast<int>(
Steve Block44f0eee2011-05-26 01:26:41 +0100379 it.frame()->pc() - code->instruction_start());
Leon Clarke4515c472010-02-03 11:58:03 +0000380 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
381 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 }
383 }
384
385 // Notify debugger
Steve Block44f0eee2011-05-26 01:26:41 +0100386 isolate->debugger()->OnBeforeCompile(script);
Steve Blocka7e24c12009-10-30 11:49:00 +0000387#endif
388
389 // Only allow non-global compiles for eval.
Ben Murdochf87a2032010-10-22 12:50:53 +0100390 ASSERT(info->is_eval() || info->is_global());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100391 ParsingFlags flags = kNoParsingFlags;
392 if (info->pre_parse_data() != NULL ||
393 String::cast(script->source())->length() > FLAG_min_preparse_length) {
394 flags = kAllowLazy;
395 }
396 if (!ParserApi::Parse(info, flags)) {
397 return Handle<SharedFunctionInfo>::null();
398 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000399
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 // Measure how long it takes to do the compilation; only take the
401 // rest of the function into account to avoid overlap with the
402 // parsing statistics.
Ben Murdochf87a2032010-10-22 12:50:53 +0100403 HistogramTimer* rate = info->is_eval()
Steve Block44f0eee2011-05-26 01:26:41 +0100404 ? info->isolate()->counters()->compile_eval()
405 : info->isolate()->counters()->compile();
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 HistogramTimerScope timer(rate);
407
408 // Compile the code.
Ben Murdochf87a2032010-10-22 12:50:53 +0100409 FunctionLiteral* lit = info->function();
Steve Block44f0eee2011-05-26 01:26:41 +0100410 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
Ben Murdochf87a2032010-10-22 12:50:53 +0100411 if (!MakeCode(info)) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100412 if (!isolate->has_pending_exception()) isolate->StackOverflow();
Steve Block6ded16b2010-05-10 14:33:55 +0100413 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000414 }
415
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100416 // Allocate function.
Ben Murdochf87a2032010-10-22 12:50:53 +0100417 ASSERT(!info->code().is_null());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100418 Handle<SharedFunctionInfo> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100419 isolate->factory()->NewSharedFunctionInfo(
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100420 lit->name(),
421 lit->materialized_literal_count(),
422 info->code(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100423 ScopeInfo::Create(info->scope()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100424
425 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
426 Compiler::SetFunctionInfo(result, lit, true, script);
427
Steve Block6ded16b2010-05-10 14:33:55 +0100428 if (script->name()->IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100429 PROFILE(isolate, CodeCreateEvent(
Ben Murdochf87a2032010-10-22 12:50:53 +0100430 info->is_eval()
431 ? Logger::EVAL_TAG
432 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
433 *info->code(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100434 *result,
Ben Murdochf87a2032010-10-22 12:50:53 +0100435 String::cast(script->name())));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100436 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
437 script,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000438 info->code(),
439 info));
Steve Block6ded16b2010-05-10 14:33:55 +0100440 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100441 PROFILE(isolate, CodeCreateEvent(
Ben Murdochf87a2032010-10-22 12:50:53 +0100442 info->is_eval()
443 ? Logger::EVAL_TAG
444 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
445 *info->code(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100446 *result,
Steve Block44f0eee2011-05-26 01:26:41 +0100447 isolate->heap()->empty_string()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000448 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
Steve Blocka7e24c12009-10-30 11:49:00 +0000449 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 // Hint to the runtime system used when allocating space for initial
452 // property space by setting the expected number of properties for
453 // the instances of the function.
Steve Block6ded16b2010-05-10 14:33:55 +0100454 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100456 script->set_compilation_state(
457 Smi::FromInt(Script::COMPILATION_STATE_COMPILED));
458
Steve Blocka7e24c12009-10-30 11:49:00 +0000459#ifdef ENABLE_DEBUGGER_SUPPORT
460 // Notify debugger
Steve Block44f0eee2011-05-26 01:26:41 +0100461 isolate->debugger()->OnAfterCompile(
462 script, Debugger::NO_AFTER_COMPILE_FLAGS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000463#endif
464
Steve Block6ded16b2010-05-10 14:33:55 +0100465 live_edit_tracker.RecordFunctionInfo(result, lit);
466
467 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000468}
469
470
Steve Block6ded16b2010-05-10 14:33:55 +0100471Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
472 Handle<Object> script_name,
473 int line_offset,
474 int column_offset,
475 v8::Extension* extension,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100476 ScriptDataImpl* pre_data,
Steve Block6ded16b2010-05-10 14:33:55 +0100477 Handle<Object> script_data,
478 NativesFlag natives) {
Steve Block44f0eee2011-05-26 01:26:41 +0100479 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000480 int source_length = source->length();
Steve Block44f0eee2011-05-26 01:26:41 +0100481 isolate->counters()->total_load_size()->Increment(source_length);
482 isolate->counters()->total_compile_size()->Increment(source_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000483
484 // The VM is in the COMPILER state until exiting this function.
Steve Block44f0eee2011-05-26 01:26:41 +0100485 VMState state(isolate, COMPILER);
486
487 CompilationCache* compilation_cache = isolate->compilation_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000488
489 // Do a lookup in the compilation cache but not for extensions.
Steve Block6ded16b2010-05-10 14:33:55 +0100490 Handle<SharedFunctionInfo> result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 if (extension == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100492 result = compilation_cache->LookupScript(source,
493 script_name,
494 line_offset,
495 column_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000496 }
497
498 if (result.is_null()) {
Steve Block59151502010-09-22 15:07:15 +0100499 // No cache entry found. Do pre-parsing, if it makes sense, and compile
500 // the script.
501 // Building preparse data that is only used immediately after is only a
502 // saving if we might skip building the AST for lazily compiled functions.
503 // I.e., preparse data isn't relevant when the lazy flag is off, and
504 // for small sources, odds are that there aren't many functions
505 // that would be compiled lazily anyway, so we skip the preparse step
506 // in that case too.
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
508 // Create a script object describing the script to be compiled.
Steve Block44f0eee2011-05-26 01:26:41 +0100509 Handle<Script> script = FACTORY->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000510 if (natives == NATIVES_CODE) {
511 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
512 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 if (!script_name.is_null()) {
514 script->set_name(*script_name);
515 script->set_line_offset(Smi::FromInt(line_offset));
516 script->set_column_offset(Smi::FromInt(column_offset));
517 }
518
Steve Block44f0eee2011-05-26 01:26:41 +0100519 script->set_data(script_data.is_null() ? HEAP->undefined_value()
Andrei Popescu402d9372010-02-26 13:31:12 +0000520 : *script_data);
521
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 // Compile the function and add it to the cache.
Ben Murdochf87a2032010-10-22 12:50:53 +0100523 CompilationInfo info(script);
524 info.MarkAsGlobal();
525 info.SetExtension(extension);
526 info.SetPreParseData(pre_data);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100527 if (FLAG_use_strict) {
528 info.SetLanguageMode(FLAG_harmony_scoping ? EXTENDED_MODE : STRICT_MODE);
529 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100530 result = MakeFunctionInfo(&info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 if (extension == NULL && !result.is_null()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100532 compilation_cache->PutScript(source, result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000534 }
535
Steve Block44f0eee2011-05-26 01:26:41 +0100536 if (result.is_null()) isolate->ReportPendingMessages();
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 return result;
538}
539
540
Steve Block6ded16b2010-05-10 14:33:55 +0100541Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
542 Handle<Context> context,
Steve Block1e0659c2011-05-24 12:43:12 +0100543 bool is_global,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100544 LanguageMode language_mode,
545 int scope_position) {
Steve Block44f0eee2011-05-26 01:26:41 +0100546 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000547 int source_length = source->length();
Steve Block44f0eee2011-05-26 01:26:41 +0100548 isolate->counters()->total_eval_size()->Increment(source_length);
549 isolate->counters()->total_compile_size()->Increment(source_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000550
551 // The VM is in the COMPILER state until exiting this function.
Steve Block44f0eee2011-05-26 01:26:41 +0100552 VMState state(isolate, COMPILER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000553
Ben Murdochf87a2032010-10-22 12:50:53 +0100554 // Do a lookup in the compilation cache; if the entry is not there, invoke
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800555 // the compiler and add the result to the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100556 Handle<SharedFunctionInfo> result;
Steve Block44f0eee2011-05-26 01:26:41 +0100557 CompilationCache* compilation_cache = isolate->compilation_cache();
558 result = compilation_cache->LookupEval(source,
559 context,
560 is_global,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100561 language_mode,
562 scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000563
564 if (result.is_null()) {
565 // Create a script object describing the script to be compiled.
Steve Block44f0eee2011-05-26 01:26:41 +0100566 Handle<Script> script = isolate->factory()->NewScript(source);
Ben Murdochf87a2032010-10-22 12:50:53 +0100567 CompilationInfo info(script);
568 info.MarkAsEval();
569 if (is_global) info.MarkAsGlobal();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100570 info.SetLanguageMode(language_mode);
Ben Murdochf87a2032010-10-22 12:50:53 +0100571 info.SetCallingContext(context);
572 result = MakeFunctionInfo(&info);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800573 if (!result.is_null()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100574 // Explicitly disable optimization for eval code. We're not yet prepared
575 // to handle eval-code in the optimizing compiler.
576 result->DisableOptimization();
577
578 // If caller is strict mode, the result must be in strict mode or
579 // extended mode as well, but not the other way around. Consider:
Steve Block1e0659c2011-05-24 12:43:12 +0100580 // eval("'use strict'; ...");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100581 ASSERT(language_mode != STRICT_MODE || !result->is_classic_mode());
582 // If caller is in extended mode, the result must also be in
583 // extended mode.
584 ASSERT(language_mode != EXTENDED_MODE ||
585 result->is_extended_mode());
586 compilation_cache->PutEval(
587 source, context, is_global, result, scope_position);
Steve Blocka7e24c12009-10-30 11:49:00 +0000588 }
589 }
590
591 return result;
592}
593
594
Leon Clarke4515c472010-02-03 11:58:03 +0000595bool Compiler::CompileLazy(CompilationInfo* info) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000596 Isolate* isolate = info->isolate();
597
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000598 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
600 // The VM is in the COMPILER state until exiting this function.
Ben Murdoch257744e2011-11-30 15:57:28 +0000601 VMState state(isolate, COMPILER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000602
Steve Block44f0eee2011-05-26 01:26:41 +0100603 PostponeInterruptsScope postpone(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
Leon Clarke4515c472010-02-03 11:58:03 +0000605 Handle<SharedFunctionInfo> shared = info->shared_info();
Ben Murdochf87a2032010-10-22 12:50:53 +0100606 int compiled_size = shared->end_position() - shared->start_position();
Steve Block44f0eee2011-05-26 01:26:41 +0100607 isolate->counters()->total_compile_size()->Increment(compiled_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000608
Ben Murdochf87a2032010-10-22 12:50:53 +0100609 // Generate the AST for the lazily compiled function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100610 if (ParserApi::Parse(info, kNoParsingFlags)) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100611 // Measure how long it takes to do the lazy compilation; only take the
612 // rest of the function into account to avoid overlap with the lazy
613 // parsing statistics.
Steve Block44f0eee2011-05-26 01:26:41 +0100614 HistogramTimerScope timer(isolate->counters()->compile_lazy());
Steve Blocka7e24c12009-10-30 11:49:00 +0000615
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100616 // After parsing we know the function's language mode. Remember it.
617 LanguageMode language_mode = info->function()->language_mode();
618 info->SetLanguageMode(language_mode);
619 shared->set_language_mode(language_mode);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100620
Ben Murdochf87a2032010-10-22 12:50:53 +0100621 // Compile the code.
622 if (!MakeCode(info)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100623 if (!isolate->has_pending_exception()) {
624 isolate->StackOverflow();
Steve Block1e0659c2011-05-24 12:43:12 +0100625 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100626 } else {
627 ASSERT(!info->code().is_null());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100628 Handle<Code> code = info->code();
Steve Block44f0eee2011-05-26 01:26:41 +0100629 // Set optimizable to false if this is disallowed by the shared
630 // function info, e.g., we might have flushed the code and must
631 // reset this bit when lazy compiling the code again.
632 if (shared->optimization_disabled()) code->set_optimizable(false);
633
Ben Murdochb0fe1622011-05-05 13:52:32 +0100634 Handle<JSFunction> function = info->closure();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100635 RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000636
Ben Murdochb0fe1622011-05-05 13:52:32 +0100637 if (info->IsOptimizing()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100638 ASSERT(shared->scope_info() != ScopeInfo::Empty());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100639 function->ReplaceCode(*code);
640 } else {
641 // Update the shared function info with the compiled code and the
642 // scope info. Please note, that the order of the shared function
643 // info initialization is important since set_scope_info might
644 // trigger a GC, causing the ASSERT below to be invalid if the code
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100645 // was flushed. By setting the code object last we avoid this.
646 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100647 shared->set_scope_info(*scope_info);
648 shared->set_code(*code);
649 if (!function.is_null()) {
650 function->ReplaceCode(*code);
651 ASSERT(!function->IsOptimized());
652 }
653
654 // Set the expected number of properties for instances.
655 FunctionLiteral* lit = info->function();
656 int expected = lit->expected_property_count();
657 SetExpectedNofPropertiesFromEstimate(shared, expected);
658
659 // Set the optimization hints after performing lazy compilation, as
660 // these are not set when the function is set up as a lazily
661 // compiled function.
662 shared->SetThisPropertyAssignmentsInfo(
663 lit->has_only_simple_this_property_assignments(),
664 *lit->this_property_assignments());
665
666 // Check the function has compiled code.
667 ASSERT(shared->is_compiled());
668 shared->set_code_age(0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100669 shared->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
670 shared->set_dont_inline(lit->flags()->Contains(kDontInline));
671 shared->set_ast_node_count(lit->ast_node_count());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100672
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100673 if (V8::UseCrankshaft()&&
674 !function.is_null() &&
675 !shared->optimization_disabled()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100676 // If we're asked to always optimize, we compile the optimized
677 // version of the function right away - unless the debugger is
678 // active as it makes no sense to compile optimized code then.
Steve Block44f0eee2011-05-26 01:26:41 +0100679 if (FLAG_always_opt &&
Ben Murdoch257744e2011-11-30 15:57:28 +0000680 !Isolate::Current()->DebuggerHasBreakPoints()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100681 CompilationInfo optimized(function);
682 optimized.SetOptimizing(AstNode::kNoNumber);
683 return CompileLazy(&optimized);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100684 }
685 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100686 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000687
Ben Murdochf87a2032010-10-22 12:50:53 +0100688 return true;
689 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000690 }
691
Ben Murdochf87a2032010-10-22 12:50:53 +0100692 ASSERT(info->code().is_null());
693 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000694}
695
696
Steve Block6ded16b2010-05-10 14:33:55 +0100697Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
Ben Murdochf87a2032010-10-22 12:50:53 +0100698 Handle<Script> script) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100699 // Precondition: code has been parsed and scopes have been analyzed.
700 CompilationInfo info(script);
701 info.SetFunction(literal);
702 info.SetScope(literal->scope());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100703 info.SetLanguageMode(literal->scope()->language_mode());
Ben Murdochf87a2032010-10-22 12:50:53 +0100704
Steve Block44f0eee2011-05-26 01:26:41 +0100705 LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
Ben Murdochf87a2032010-10-22 12:50:53 +0100706 // Determine if the function can be lazily compiled. This is necessary to
707 // allow some of our builtin JS files to be lazily compiled. These
708 // builtins cannot be handled lazily by the parser, since we have to know
709 // if a function uses the special natives syntax, which is something the
710 // parser records.
Andrei Popescu402d9372010-02-26 13:31:12 +0000711 bool allow_lazy = literal->AllowsLazyCompilation() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100712 !LiveEditFunctionTracker::IsActive(info.isolate());
Steve Blockd0582a62009-12-15 09:54:21 +0000713
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100714 Handle<ScopeInfo> scope_info(ScopeInfo::Empty());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100715
Steve Blockd0582a62009-12-15 09:54:21 +0000716 // Generate code
Steve Blockd0582a62009-12-15 09:54:21 +0000717 if (FLAG_lazy && allow_lazy) {
Steve Block44f0eee2011-05-26 01:26:41 +0100718 Handle<Code> code = info.isolate()->builtins()->LazyCompile();
Ben Murdochf87a2032010-10-22 12:50:53 +0100719 info.SetCode(code);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100720 } else if ((V8::UseCrankshaft() && MakeCrankshaftCode(&info)) ||
721 (!V8::UseCrankshaft() && FullCodeGenerator::MakeCode(&info))) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100722 ASSERT(!info.code().is_null());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100723 scope_info = ScopeInfo::Create(info.scope());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100724 } else {
725 return Handle<SharedFunctionInfo>::null();
Steve Blockd0582a62009-12-15 09:54:21 +0000726 }
727
Steve Block6ded16b2010-05-10 14:33:55 +0100728 // Create a shared function info object.
729 Handle<SharedFunctionInfo> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100730 FACTORY->NewSharedFunctionInfo(literal->name(),
Steve Block6ded16b2010-05-10 14:33:55 +0100731 literal->materialized_literal_count(),
Ben Murdochf87a2032010-10-22 12:50:53 +0100732 info.code(),
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100733 scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100734 SetFunctionInfo(result, literal, false, script);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100735 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100736 result->set_allows_lazy_compilation(allow_lazy);
Steve Blockd0582a62009-12-15 09:54:21 +0000737
738 // Set the expected number of properties for instances and return
739 // the resulting function.
Steve Block6ded16b2010-05-10 14:33:55 +0100740 SetExpectedNofPropertiesFromEstimate(result,
Steve Blockd0582a62009-12-15 09:54:21 +0000741 literal->expected_property_count());
Steve Block6ded16b2010-05-10 14:33:55 +0100742 live_edit_tracker.RecordFunctionInfo(result, literal);
743 return result;
Steve Blockd0582a62009-12-15 09:54:21 +0000744}
745
746
747// Sets the function info on a function.
748// The start_position points to the first '(' character after the function name
749// in the full script source. When counting characters in the script source the
750// the first character is number 0 (not 1).
Steve Block6ded16b2010-05-10 14:33:55 +0100751void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
Steve Blockd0582a62009-12-15 09:54:21 +0000752 FunctionLiteral* lit,
753 bool is_toplevel,
754 Handle<Script> script) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100755 function_info->set_length(lit->parameter_count());
756 function_info->set_formal_parameter_count(lit->parameter_count());
Steve Block6ded16b2010-05-10 14:33:55 +0100757 function_info->set_script(*script);
758 function_info->set_function_token_position(lit->function_token_position());
759 function_info->set_start_position(lit->start_position());
760 function_info->set_end_position(lit->end_position());
761 function_info->set_is_expression(lit->is_expression());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000762 function_info->set_is_anonymous(lit->is_anonymous());
Steve Block6ded16b2010-05-10 14:33:55 +0100763 function_info->set_is_toplevel(is_toplevel);
764 function_info->set_inferred_name(*lit->inferred_name());
765 function_info->SetThisPropertyAssignmentsInfo(
Steve Blockd0582a62009-12-15 09:54:21 +0000766 lit->has_only_simple_this_property_assignments(),
767 *lit->this_property_assignments());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100768 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100769 function_info->set_language_mode(lit->language_mode());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000770 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
771 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100772 function_info->set_ast_node_count(lit->ast_node_count());
773 function_info->set_is_function(lit->is_function());
774 function_info->set_dont_optimize(lit->flags()->Contains(kDontOptimize));
775 function_info->set_dont_inline(lit->flags()->Contains(kDontInline));
Steve Blockd0582a62009-12-15 09:54:21 +0000776}
777
778
Steve Block6ded16b2010-05-10 14:33:55 +0100779void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100780 CompilationInfo* info,
781 Handle<SharedFunctionInfo> shared) {
782 // SharedFunctionInfo is passed separately, because if CompilationInfo
783 // was created using Script object, it will not have it.
784
Ben Murdochf87a2032010-10-22 12:50:53 +0100785 // Log the code generation. If source information is available include
786 // script name and line number. Check explicitly whether logging is
787 // enabled as finding the line number is not free.
Ben Murdoch257744e2011-11-30 15:57:28 +0000788 if (info->isolate()->logger()->is_logging() ||
789 CpuProfiler::is_profiling(info->isolate())) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100790 Handle<Script> script = info->script();
791 Handle<Code> code = info->code();
Steve Block44f0eee2011-05-26 01:26:41 +0100792 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
793 return;
Andrei Popescu31002712010-02-23 13:46:05 +0000794 if (script->name()->IsString()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100795 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
Steve Block6ded16b2010-05-10 14:33:55 +0100796 USE(line_num);
Steve Block44f0eee2011-05-26 01:26:41 +0100797 PROFILE(info->isolate(),
798 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
Ben Murdochf87a2032010-10-22 12:50:53 +0100799 *code,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100800 *shared,
Ben Murdochf87a2032010-10-22 12:50:53 +0100801 String::cast(script->name()),
802 line_num));
Andrei Popescu31002712010-02-23 13:46:05 +0000803 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100804 PROFILE(info->isolate(),
805 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
Ben Murdochf87a2032010-10-22 12:50:53 +0100806 *code,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100807 *shared,
808 shared->DebugName()));
Andrei Popescu31002712010-02-23 13:46:05 +0000809 }
810 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100811
Ben Murdoch8b112d22011-06-08 16:22:53 +0100812 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
Ben Murdochb8e0da22011-05-16 14:20:40 +0100813 Handle<Script>(info->script()),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000814 Handle<Code>(info->code()),
815 info));
Andrei Popescu31002712010-02-23 13:46:05 +0000816}
Andrei Popescu31002712010-02-23 13:46:05 +0000817
Steve Blocka7e24c12009-10-30 11:49:00 +0000818} } // namespace v8::internal