blob: 5e1c4a9789dddd01935d7865c8b3611e39e13392 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 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"
Steve Block1e0659c2011-05-24 12:43:12 +010039#include "lithium.h"
Steve Block6ded16b2010-05-10 14:33:55 +010040#include "liveedit.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010041#include "parser.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000042#include "rewriter.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010043#include "runtime-profiler.h"
Ben Murdoch589d6972011-11-30 16:04:58 +000044#include "scanner-character-streams.h"
Ben Murdoch3bec4d22010-07-22 14:51:16 +010045#include "scopeinfo.h"
Ben Murdochf87a2032010-10-22 12:50:53 +010046#include "scopes.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010047#include "vm-state-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000048
49namespace v8 {
50namespace internal {
51
Ben Murdochf87a2032010-10-22 12:50:53 +010052
53CompilationInfo::CompilationInfo(Handle<Script> script)
Steve Block44f0eee2011-05-26 01:26:41 +010054 : isolate_(script->GetIsolate()),
55 flags_(0),
Ben Murdochf87a2032010-10-22 12:50:53 +010056 function_(NULL),
57 scope_(NULL),
58 script_(script),
59 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010060 pre_parse_data_(NULL),
61 supports_deoptimization_(false),
62 osr_ast_id_(AstNode::kNoNumber) {
63 Initialize(NONOPT);
Ben Murdochf87a2032010-10-22 12:50:53 +010064}
65
66
67CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info)
Steve Block44f0eee2011-05-26 01:26:41 +010068 : isolate_(shared_info->GetIsolate()),
69 flags_(IsLazy::encode(true)),
Ben Murdochf87a2032010-10-22 12:50:53 +010070 function_(NULL),
71 scope_(NULL),
72 shared_info_(shared_info),
73 script_(Handle<Script>(Script::cast(shared_info->script()))),
74 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010075 pre_parse_data_(NULL),
76 supports_deoptimization_(false),
77 osr_ast_id_(AstNode::kNoNumber) {
78 Initialize(BASE);
Ben Murdochf87a2032010-10-22 12:50:53 +010079}
80
81
82CompilationInfo::CompilationInfo(Handle<JSFunction> closure)
Steve Block44f0eee2011-05-26 01:26:41 +010083 : isolate_(closure->GetIsolate()),
84 flags_(IsLazy::encode(true)),
Ben Murdochf87a2032010-10-22 12:50:53 +010085 function_(NULL),
86 scope_(NULL),
87 closure_(closure),
88 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
89 script_(Handle<Script>(Script::cast(shared_info_->script()))),
90 extension_(NULL),
Ben Murdochb0fe1622011-05-05 13:52:32 +010091 pre_parse_data_(NULL),
92 supports_deoptimization_(false),
93 osr_ast_id_(AstNode::kNoNumber) {
94 Initialize(BASE);
Ben Murdochf87a2032010-10-22 12:50:53 +010095}
96
97
Ben Murdoch257744e2011-11-30 15:57:28 +000098// Disable optimization for the rest of the compilation pipeline.
Ben Murdochb8e0da22011-05-16 14:20:40 +010099void CompilationInfo::DisableOptimization() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000100 bool is_optimizable_closure =
101 FLAG_optimize_closures &&
102 closure_.is_null() &&
103 !scope_->HasTrivialOuterContext() &&
104 !scope_->outer_scope_calls_non_strict_eval() &&
105 !scope_->inside_with();
106 SetMode(is_optimizable_closure ? BASE : NONOPT);
107}
Ben Murdochb8e0da22011-05-16 14:20:40 +0100108
Ben Murdoch257744e2011-11-30 15:57:28 +0000109
110void CompilationInfo::AbortOptimization() {
111 Handle<Code> code(shared_info()->code());
112 SetCode(code);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100113}
114
115
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116// Determine whether to use the full compiler for all code. If the flag
117// --always-full-compiler is specified this is the case. For the virtual frame
118// based compiler the full compiler is also used if a debugger is connected, as
119// the code from the full compiler supports mode precise break points. For the
120// crankshaft adaptive compiler debugging the optimized code is not possible at
121// all. However crankshaft support recompilation of functions, so in this case
122// the full compiler need not be be used if a debugger is attached, but only if
123// break points has actually been set.
Ben Murdoch257744e2011-11-30 15:57:28 +0000124static bool is_debugging_active() {
Leon Clarkef7060e22010-06-03 12:02:55 +0100125#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100126 Isolate* isolate = Isolate::Current();
Ben Murdoch257744e2011-11-30 15:57:28 +0000127 return V8::UseCrankshaft() ?
128 isolate->debug()->has_break_points() :
129 isolate->debugger()->IsDebuggerActive();
Leon Clarkef7060e22010-06-03 12:02:55 +0100130#else
Ben Murdoch257744e2011-11-30 15:57:28 +0000131 return false;
Leon Clarkef7060e22010-06-03 12:02:55 +0100132#endif
133}
134
Steve Block3ce2e202009-11-05 08:53:23 +0000135
Ben Murdoch257744e2011-11-30 15:57:28 +0000136static bool AlwaysFullCompiler() {
137 return FLAG_always_full_compiler || is_debugging_active();
138}
139
140
Ben Murdochb0fe1622011-05-05 13:52:32 +0100141static void FinishOptimization(Handle<JSFunction> function, int64_t start) {
142 int opt_count = function->shared()->opt_count();
143 function->shared()->set_opt_count(opt_count + 1);
144 double ms = static_cast<double>(OS::Ticks() - start) / 1000;
145 if (FLAG_trace_opt) {
146 PrintF("[optimizing: ");
147 function->PrintName();
148 PrintF(" / %" V8PRIxPTR, reinterpret_cast<intptr_t>(*function));
149 PrintF(" - took %0.3f ms]\n", ms);
150 }
151 if (FLAG_trace_opt_stats) {
152 static double compilation_time = 0.0;
153 static int compiled_functions = 0;
154 static int code_size = 0;
155
156 compilation_time += ms;
157 compiled_functions++;
158 code_size += function->shared()->SourceSize();
159 PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
160 compiled_functions,
161 code_size,
162 compilation_time);
163 }
164}
165
166
Ben Murdochb0fe1622011-05-05 13:52:32 +0100167static bool MakeCrankshaftCode(CompilationInfo* info) {
168 // Test if we can optimize this function when asked to. We can only
169 // do this after the scopes are computed.
170 if (!info->AllowOptimize()) info->DisableOptimization();
171
172 // In case we are not optimizing simply return the code from
173 // the full code generator.
174 if (!info->IsOptimizing()) {
175 return FullCodeGenerator::MakeCode(info);
176 }
177
178 // We should never arrive here if there is not code object on the
179 // shared function object.
180 Handle<Code> code(info->shared_info()->code());
181 ASSERT(code->kind() == Code::FUNCTION);
182
Steve Block44f0eee2011-05-26 01:26:41 +0100183 // We should never arrive here if optimization has been disabled on the
184 // shared function info.
185 ASSERT(!info->shared_info()->optimization_disabled());
186
Ben Murdochb0fe1622011-05-05 13:52:32 +0100187 // Fall back to using the full code generator if it's not possible
188 // to use the Hydrogen-based optimizing compiler. We already have
189 // generated code for this from the shared function object.
190 if (AlwaysFullCompiler() || !FLAG_use_hydrogen) {
191 info->SetCode(code);
192 return true;
193 }
194
195 // Limit the number of times we re-compile a functions with
196 // the optimizing compiler.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100197 const int kMaxOptCount =
198 FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100199 if (info->shared_info()->opt_count() > kMaxOptCount) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000200 info->AbortOptimization();
201 Handle<JSFunction> closure = info->closure();
202 info->shared_info()->DisableOptimization(*closure);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100203 // True indicates the compilation pipeline is still going, not
204 // necessarily that we optimized the code.
205 return true;
206 }
207
208 // Due to an encoding limit on LUnallocated operands in the Lithium
209 // language, we cannot optimize functions with too many formal parameters
210 // or perform on-stack replacement for function with too many
211 // stack-allocated local variables.
212 //
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100213 // The encoding is as a signed value, with parameters and receiver using
214 // the negative indices and locals the non-negative ones.
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100215 const int parameter_limit = -LUnallocated::kMinFixedIndex;
216 const int locals_limit = LUnallocated::kMaxFixedIndex;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100217 Scope* scope = info->scope();
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +0100218 if ((scope->num_parameters() + 1) > parameter_limit ||
219 (info->osr_ast_id() != AstNode::kNoNumber &&
220 scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000221 info->AbortOptimization();
222 Handle<JSFunction> closure = info->closure();
223 info->shared_info()->DisableOptimization(*closure);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100224 // True indicates the compilation pipeline is still going, not
225 // necessarily that we optimized the code.
226 return true;
227 }
228
229 // Take --hydrogen-filter into account.
230 Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
231 Handle<String> name = info->function()->debug_name();
232 bool match = filter.is_empty() || name->IsEqualTo(filter);
233 if (!match) {
234 info->SetCode(code);
235 return true;
236 }
237
238 // Recompile the unoptimized version of the code if the current version
239 // doesn't have deoptimization support. Alternatively, we may decide to
240 // run the full code generator to get a baseline for the compile-time
241 // performance of the hydrogen-based compiler.
242 int64_t start = OS::Ticks();
243 bool should_recompile = !info->shared_info()->has_deoptimization_support();
Steve Block44f0eee2011-05-26 01:26:41 +0100244 if (should_recompile || FLAG_hydrogen_stats) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100245 HPhase phase(HPhase::kFullCodeGen);
246 CompilationInfo unoptimized(info->shared_info());
247 // Note that we use the same AST that we will use for generating the
248 // optimized code.
249 unoptimized.SetFunction(info->function());
250 unoptimized.SetScope(info->scope());
251 if (should_recompile) unoptimized.EnableDeoptimizationSupport();
252 bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
253 if (should_recompile) {
254 if (!succeeded) return false;
255 Handle<SharedFunctionInfo> shared = info->shared_info();
256 shared->EnableDeoptimizationSupport(*unoptimized.code());
257 // The existing unoptimized code was replaced with the new one.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100258 Compiler::RecordFunctionCompilation(
259 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100260 }
261 }
262
263 // Check that the unoptimized, shared code is ready for
264 // optimizations. When using the always_opt flag we disregard the
265 // optimizable marker in the code object and optimize anyway. This
266 // is safe as long as the unoptimized code has deoptimization
267 // support.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100268 ASSERT(FLAG_always_opt || code->optimizable());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100269 ASSERT(info->shared_info()->has_deoptimization_support());
270
271 if (FLAG_trace_hydrogen) {
272 PrintF("-----------------------------------------------------------\n");
273 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
274 HTracer::Instance()->TraceCompilation(info->function());
275 }
276
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100277 Handle<Context> global_context(info->closure()->context()->global_context());
278 TypeFeedbackOracle oracle(code, global_context);
279 HGraphBuilder builder(info, &oracle);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100280 HPhase phase(HPhase::kTotal);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100281 HGraph* graph = builder.CreateGraph();
Steve Block44f0eee2011-05-26 01:26:41 +0100282 if (info->isolate()->has_pending_exception()) {
Steve Block1e0659c2011-05-24 12:43:12 +0100283 info->SetCode(Handle<Code>::null());
284 return false;
285 }
286
Ben Murdochb0fe1622011-05-05 13:52:32 +0100287 if (graph != NULL && FLAG_build_lithium) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100288 Handle<Code> optimized_code = graph->Compile(info);
289 if (!optimized_code.is_null()) {
290 info->SetCode(optimized_code);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100291 FinishOptimization(info->closure(), start);
292 return true;
293 }
294 }
295
Ben Murdoch257744e2011-11-30 15:57:28 +0000296 // Keep using the shared code.
297 info->AbortOptimization();
298 if (!builder.inline_bailout()) {
299 // Mark the shared code as unoptimizable unless it was an inlined
300 // function that bailed out.
301 Handle<JSFunction> closure = info->closure();
302 info->shared_info()->DisableOptimization(*closure);
303 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100304 // True indicates the compilation pipeline is still going, not necessarily
305 // that we optimized the code.
306 return true;
307}
308
309
Ben Murdoch257744e2011-11-30 15:57:28 +0000310static bool GenerateCode(CompilationInfo* info) {
311 return V8::UseCrankshaft() ?
312 MakeCrankshaftCode(info) :
313 FullCodeGenerator::MakeCode(info);
314}
315
316
Ben Murdochf87a2032010-10-22 12:50:53 +0100317static bool MakeCode(CompilationInfo* info) {
318 // Precondition: code has been parsed. Postcondition: the code field in
319 // the compilation info is set if compilation succeeded.
320 ASSERT(info->function() != NULL);
Ben Murdoch257744e2011-11-30 15:57:28 +0000321 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000322}
323
324
Steve Block6ded16b2010-05-10 14:33:55 +0100325#ifdef ENABLE_DEBUGGER_SUPPORT
Ben Murdochf87a2032010-10-22 12:50:53 +0100326bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
327 // Precondition: code has been parsed. Postcondition: the code field in
328 // the compilation info is set if compilation succeeded.
329 bool succeeded = MakeCode(info);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100330 if (!info->shared_info().is_null()) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100331 Handle<SerializedScopeInfo> scope_info =
332 SerializedScopeInfo::Create(info->scope());
333 info->shared_info()->set_scope_info(*scope_info);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100334 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100335 return succeeded;
Steve Block6ded16b2010-05-10 14:33:55 +0100336}
337#endif
338
339
Ben Murdochf87a2032010-10-22 12:50:53 +0100340static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
Steve Block44f0eee2011-05-26 01:26:41 +0100341 Isolate* isolate = info->isolate();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000342 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
Steve Block44f0eee2011-05-26 01:26:41 +0100343 PostponeInterruptsScope postpone(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000344
Steve Block44f0eee2011-05-26 01:26:41 +0100345 ASSERT(!isolate->global_context().is_null());
Ben Murdochf87a2032010-10-22 12:50:53 +0100346 Handle<Script> script = info->script();
Steve Block44f0eee2011-05-26 01:26:41 +0100347 script->set_context_data((*isolate->global_context())->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
Leon Clarke4515c472010-02-03 11:58:03 +0000349#ifdef ENABLE_DEBUGGER_SUPPORT
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800350 if (info->is_eval()) {
351 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
Ben Murdochf87a2032010-10-22 12:50:53 +0100352 script->set_compilation_type(Smi::FromInt(compilation_type));
Steve Blocka7e24c12009-10-30 11:49:00 +0000353 // For eval scripts add information on the function from which eval was
354 // called.
Ben Murdochf87a2032010-10-22 12:50:53 +0100355 if (info->is_eval()) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100356 StackTraceFrameIterator it(isolate);
Leon Clarke4515c472010-02-03 11:58:03 +0000357 if (!it.done()) {
358 script->set_eval_from_shared(
359 JSFunction::cast(it.frame()->function())->shared());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100360 Code* code = it.frame()->LookupCode();
Leon Clarke4515c472010-02-03 11:58:03 +0000361 int offset = static_cast<int>(
Steve Block44f0eee2011-05-26 01:26:41 +0100362 it.frame()->pc() - code->instruction_start());
Leon Clarke4515c472010-02-03 11:58:03 +0000363 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
364 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 }
366 }
367
368 // Notify debugger
Steve Block44f0eee2011-05-26 01:26:41 +0100369 isolate->debugger()->OnBeforeCompile(script);
Steve Blocka7e24c12009-10-30 11:49:00 +0000370#endif
371
372 // Only allow non-global compiles for eval.
Ben Murdochf87a2032010-10-22 12:50:53 +0100373 ASSERT(info->is_eval() || info->is_global());
Steve Blocka7e24c12009-10-30 11:49:00 +0000374
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800375 if (!ParserApi::Parse(info)) return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000376
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 // Measure how long it takes to do the compilation; only take the
378 // rest of the function into account to avoid overlap with the
379 // parsing statistics.
Ben Murdochf87a2032010-10-22 12:50:53 +0100380 HistogramTimer* rate = info->is_eval()
Steve Block44f0eee2011-05-26 01:26:41 +0100381 ? info->isolate()->counters()->compile_eval()
382 : info->isolate()->counters()->compile();
Steve Blocka7e24c12009-10-30 11:49:00 +0000383 HistogramTimerScope timer(rate);
384
385 // Compile the code.
Ben Murdochf87a2032010-10-22 12:50:53 +0100386 FunctionLiteral* lit = info->function();
Steve Block44f0eee2011-05-26 01:26:41 +0100387 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
Ben Murdochf87a2032010-10-22 12:50:53 +0100388 if (!MakeCode(info)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100389 isolate->StackOverflow();
Steve Block6ded16b2010-05-10 14:33:55 +0100390 return Handle<SharedFunctionInfo>::null();
Steve Blocka7e24c12009-10-30 11:49:00 +0000391 }
392
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100393 // Allocate function.
Ben Murdochf87a2032010-10-22 12:50:53 +0100394 ASSERT(!info->code().is_null());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100395 Handle<SharedFunctionInfo> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100396 isolate->factory()->NewSharedFunctionInfo(
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100397 lit->name(),
398 lit->materialized_literal_count(),
399 info->code(),
400 SerializedScopeInfo::Create(info->scope()));
401
402 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
403 Compiler::SetFunctionInfo(result, lit, true, script);
404
Steve Block6ded16b2010-05-10 14:33:55 +0100405 if (script->name()->IsString()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100406 PROFILE(isolate, CodeCreateEvent(
Ben Murdochf87a2032010-10-22 12:50:53 +0100407 info->is_eval()
408 ? Logger::EVAL_TAG
409 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
410 *info->code(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100411 *result,
Ben Murdochf87a2032010-10-22 12:50:53 +0100412 String::cast(script->name())));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100413 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
414 script,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000415 info->code(),
416 info));
Steve Block6ded16b2010-05-10 14:33:55 +0100417 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100418 PROFILE(isolate, CodeCreateEvent(
Ben Murdochf87a2032010-10-22 12:50:53 +0100419 info->is_eval()
420 ? Logger::EVAL_TAG
421 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
422 *info->code(),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100423 *result,
Steve Block44f0eee2011-05-26 01:26:41 +0100424 isolate->heap()->empty_string()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000425 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
Steve Blocka7e24c12009-10-30 11:49:00 +0000426 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000427
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 // Hint to the runtime system used when allocating space for initial
429 // property space by setting the expected number of properties for
430 // the instances of the function.
Steve Block6ded16b2010-05-10 14:33:55 +0100431 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
Steve Blocka7e24c12009-10-30 11:49:00 +0000432
433#ifdef ENABLE_DEBUGGER_SUPPORT
434 // Notify debugger
Steve Block44f0eee2011-05-26 01:26:41 +0100435 isolate->debugger()->OnAfterCompile(
436 script, Debugger::NO_AFTER_COMPILE_FLAGS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000437#endif
438
Steve Block6ded16b2010-05-10 14:33:55 +0100439 live_edit_tracker.RecordFunctionInfo(result, lit);
440
441 return result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000442}
443
444
Steve Block6ded16b2010-05-10 14:33:55 +0100445Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
446 Handle<Object> script_name,
447 int line_offset,
448 int column_offset,
449 v8::Extension* extension,
450 ScriptDataImpl* input_pre_data,
451 Handle<Object> script_data,
452 NativesFlag natives) {
Steve Block44f0eee2011-05-26 01:26:41 +0100453 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 int source_length = source->length();
Steve Block44f0eee2011-05-26 01:26:41 +0100455 isolate->counters()->total_load_size()->Increment(source_length);
456 isolate->counters()->total_compile_size()->Increment(source_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000457
458 // The VM is in the COMPILER state until exiting this function.
Steve Block44f0eee2011-05-26 01:26:41 +0100459 VMState state(isolate, COMPILER);
460
461 CompilationCache* compilation_cache = isolate->compilation_cache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000462
463 // Do a lookup in the compilation cache but not for extensions.
Steve Block6ded16b2010-05-10 14:33:55 +0100464 Handle<SharedFunctionInfo> result;
Steve Blocka7e24c12009-10-30 11:49:00 +0000465 if (extension == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +0100466 result = compilation_cache->LookupScript(source,
467 script_name,
468 line_offset,
469 column_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 }
471
472 if (result.is_null()) {
Steve Block59151502010-09-22 15:07:15 +0100473 // No cache entry found. Do pre-parsing, if it makes sense, and compile
474 // the script.
475 // Building preparse data that is only used immediately after is only a
476 // saving if we might skip building the AST for lazily compiled functions.
477 // I.e., preparse data isn't relevant when the lazy flag is off, and
478 // for small sources, odds are that there aren't many functions
479 // that would be compiled lazily anyway, so we skip the preparse step
480 // in that case too.
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 ScriptDataImpl* pre_data = input_pre_data;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000482 bool harmony_block_scoping = natives != NATIVES_CODE &&
483 FLAG_harmony_block_scoping;
Steve Block59151502010-09-22 15:07:15 +0100484 if (pre_data == NULL
Steve Block59151502010-09-22 15:07:15 +0100485 && source_length >= FLAG_min_preparse_length) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100486 if (source->IsExternalTwoByteString()) {
487 ExternalTwoByteStringUC16CharacterStream stream(
488 Handle<ExternalTwoByteString>::cast(source), 0, source->length());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000489 pre_data = ParserApi::PartialPreParse(&stream,
490 extension,
491 harmony_block_scoping);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100492 } else {
493 GenericStringUC16CharacterStream stream(source, 0, source->length());
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000494 pre_data = ParserApi::PartialPreParse(&stream,
495 extension,
496 harmony_block_scoping);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100497 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 }
499
500 // Create a script object describing the script to be compiled.
Steve Block44f0eee2011-05-26 01:26:41 +0100501 Handle<Script> script = FACTORY->NewScript(source);
Andrei Popescu31002712010-02-23 13:46:05 +0000502 if (natives == NATIVES_CODE) {
503 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
504 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000505 if (!script_name.is_null()) {
506 script->set_name(*script_name);
507 script->set_line_offset(Smi::FromInt(line_offset));
508 script->set_column_offset(Smi::FromInt(column_offset));
509 }
510
Steve Block44f0eee2011-05-26 01:26:41 +0100511 script->set_data(script_data.is_null() ? HEAP->undefined_value()
Andrei Popescu402d9372010-02-26 13:31:12 +0000512 : *script_data);
513
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 // Compile the function and add it to the cache.
Ben Murdochf87a2032010-10-22 12:50:53 +0100515 CompilationInfo info(script);
516 info.MarkAsGlobal();
517 info.SetExtension(extension);
518 info.SetPreParseData(pre_data);
519 result = MakeFunctionInfo(&info);
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 if (extension == NULL && !result.is_null()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100521 compilation_cache->PutScript(source, result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 }
523
524 // Get rid of the pre-parsing data (if necessary).
525 if (input_pre_data == NULL && pre_data != NULL) {
526 delete pre_data;
527 }
528 }
529
Steve Block44f0eee2011-05-26 01:26:41 +0100530 if (result.is_null()) isolate->ReportPendingMessages();
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 return result;
532}
533
534
Steve Block6ded16b2010-05-10 14:33:55 +0100535Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
536 Handle<Context> context,
Steve Block1e0659c2011-05-24 12:43:12 +0100537 bool is_global,
538 StrictModeFlag strict_mode) {
Steve Block44f0eee2011-05-26 01:26:41 +0100539 Isolate* isolate = source->GetIsolate();
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 int source_length = source->length();
Steve Block44f0eee2011-05-26 01:26:41 +0100541 isolate->counters()->total_eval_size()->Increment(source_length);
542 isolate->counters()->total_compile_size()->Increment(source_length);
Steve Blocka7e24c12009-10-30 11:49:00 +0000543
544 // The VM is in the COMPILER state until exiting this function.
Steve Block44f0eee2011-05-26 01:26:41 +0100545 VMState state(isolate, COMPILER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
Ben Murdochf87a2032010-10-22 12:50:53 +0100547 // Do a lookup in the compilation cache; if the entry is not there, invoke
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800548 // the compiler and add the result to the cache.
Steve Block6ded16b2010-05-10 14:33:55 +0100549 Handle<SharedFunctionInfo> result;
Steve Block44f0eee2011-05-26 01:26:41 +0100550 CompilationCache* compilation_cache = isolate->compilation_cache();
551 result = compilation_cache->LookupEval(source,
552 context,
553 is_global,
554 strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000555
556 if (result.is_null()) {
557 // Create a script object describing the script to be compiled.
Steve Block44f0eee2011-05-26 01:26:41 +0100558 Handle<Script> script = isolate->factory()->NewScript(source);
Ben Murdochf87a2032010-10-22 12:50:53 +0100559 CompilationInfo info(script);
560 info.MarkAsEval();
561 if (is_global) info.MarkAsGlobal();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100562 if (strict_mode == kStrictMode) info.MarkAsStrictMode();
Ben Murdochf87a2032010-10-22 12:50:53 +0100563 info.SetCallingContext(context);
564 result = MakeFunctionInfo(&info);
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800565 if (!result.is_null()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100566 CompilationCache* compilation_cache = isolate->compilation_cache();
Steve Block1e0659c2011-05-24 12:43:12 +0100567 // If caller is strict mode, the result must be strict as well,
568 // but not the other way around. Consider:
569 // eval("'use strict'; ...");
570 ASSERT(strict_mode == kNonStrictMode || result->strict_mode());
Steve Block44f0eee2011-05-26 01:26:41 +0100571 compilation_cache->PutEval(source, context, is_global, result);
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 }
573 }
574
575 return result;
576}
577
578
Leon Clarke4515c472010-02-03 11:58:03 +0000579bool Compiler::CompileLazy(CompilationInfo* info) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000580 Isolate* isolate = info->isolate();
581
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000582 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000583
584 // The VM is in the COMPILER state until exiting this function.
Ben Murdoch257744e2011-11-30 15:57:28 +0000585 VMState state(isolate, COMPILER);
Steve Blocka7e24c12009-10-30 11:49:00 +0000586
Steve Block44f0eee2011-05-26 01:26:41 +0100587 PostponeInterruptsScope postpone(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000588
Leon Clarke4515c472010-02-03 11:58:03 +0000589 Handle<SharedFunctionInfo> shared = info->shared_info();
Ben Murdochf87a2032010-10-22 12:50:53 +0100590 int compiled_size = shared->end_position() - shared->start_position();
Steve Block44f0eee2011-05-26 01:26:41 +0100591 isolate->counters()->total_compile_size()->Increment(compiled_size);
Steve Blocka7e24c12009-10-30 11:49:00 +0000592
Ben Murdochf87a2032010-10-22 12:50:53 +0100593 // Generate the AST for the lazily compiled function.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800594 if (ParserApi::Parse(info)) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100595 // Measure how long it takes to do the lazy compilation; only take the
596 // rest of the function into account to avoid overlap with the lazy
597 // parsing statistics.
Steve Block44f0eee2011-05-26 01:26:41 +0100598 HistogramTimerScope timer(isolate->counters()->compile_lazy());
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
Ben Murdoch8b112d22011-06-08 16:22:53 +0100600 // After parsing we know function's strict mode. Remember it.
601 if (info->function()->strict_mode()) {
602 shared->set_strict_mode(true);
603 info->MarkAsStrictMode();
604 }
605
Ben Murdochf87a2032010-10-22 12:50:53 +0100606 // Compile the code.
607 if (!MakeCode(info)) {
Steve Block44f0eee2011-05-26 01:26:41 +0100608 if (!isolate->has_pending_exception()) {
609 isolate->StackOverflow();
Steve Block1e0659c2011-05-24 12:43:12 +0100610 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100611 } else {
612 ASSERT(!info->code().is_null());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100613 Handle<Code> code = info->code();
Steve Block44f0eee2011-05-26 01:26:41 +0100614 // Set optimizable to false if this is disallowed by the shared
615 // function info, e.g., we might have flushed the code and must
616 // reset this bit when lazy compiling the code again.
617 if (shared->optimization_disabled()) code->set_optimizable(false);
618
Ben Murdochb0fe1622011-05-05 13:52:32 +0100619 Handle<JSFunction> function = info->closure();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100620 RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
Steve Blocka7e24c12009-10-30 11:49:00 +0000621
Ben Murdochb0fe1622011-05-05 13:52:32 +0100622 if (info->IsOptimizing()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000623 ASSERT(shared->scope_info() != SerializedScopeInfo::Empty());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 function->ReplaceCode(*code);
625 } else {
626 // Update the shared function info with the compiled code and the
627 // scope info. Please note, that the order of the shared function
628 // info initialization is important since set_scope_info might
629 // trigger a GC, causing the ASSERT below to be invalid if the code
630 // was flushed. By settting the code object last we avoid this.
631 Handle<SerializedScopeInfo> scope_info =
632 SerializedScopeInfo::Create(info->scope());
633 shared->set_scope_info(*scope_info);
634 shared->set_code(*code);
635 if (!function.is_null()) {
636 function->ReplaceCode(*code);
637 ASSERT(!function->IsOptimized());
638 }
639
640 // Set the expected number of properties for instances.
641 FunctionLiteral* lit = info->function();
642 int expected = lit->expected_property_count();
643 SetExpectedNofPropertiesFromEstimate(shared, expected);
644
645 // Set the optimization hints after performing lazy compilation, as
646 // these are not set when the function is set up as a lazily
647 // compiled function.
648 shared->SetThisPropertyAssignmentsInfo(
649 lit->has_only_simple_this_property_assignments(),
650 *lit->this_property_assignments());
651
652 // Check the function has compiled code.
653 ASSERT(shared->is_compiled());
654 shared->set_code_age(0);
655
Steve Block44f0eee2011-05-26 01:26:41 +0100656 if (info->AllowOptimize() && !shared->optimization_disabled()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100657 // If we're asked to always optimize, we compile the optimized
658 // version of the function right away - unless the debugger is
659 // active as it makes no sense to compile optimized code then.
Steve Block44f0eee2011-05-26 01:26:41 +0100660 if (FLAG_always_opt &&
Ben Murdoch257744e2011-11-30 15:57:28 +0000661 !Isolate::Current()->DebuggerHasBreakPoints()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100662 CompilationInfo optimized(function);
663 optimized.SetOptimizing(AstNode::kNoNumber);
664 return CompileLazy(&optimized);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100665 }
666 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100667 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000668
Ben Murdochf87a2032010-10-22 12:50:53 +0100669 return true;
670 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000671 }
672
Ben Murdochf87a2032010-10-22 12:50:53 +0100673 ASSERT(info->code().is_null());
674 return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000675}
676
677
Steve Block6ded16b2010-05-10 14:33:55 +0100678Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
Ben Murdochf87a2032010-10-22 12:50:53 +0100679 Handle<Script> script) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100680 // Precondition: code has been parsed and scopes have been analyzed.
681 CompilationInfo info(script);
682 info.SetFunction(literal);
683 info.SetScope(literal->scope());
Ben Murdoch257744e2011-11-30 15:57:28 +0000684 if (literal->scope()->is_strict_mode()) info.MarkAsStrictMode();
Ben Murdochf87a2032010-10-22 12:50:53 +0100685
Steve Block44f0eee2011-05-26 01:26:41 +0100686 LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
Ben Murdochf87a2032010-10-22 12:50:53 +0100687 // Determine if the function can be lazily compiled. This is necessary to
688 // allow some of our builtin JS files to be lazily compiled. These
689 // builtins cannot be handled lazily by the parser, since we have to know
690 // if a function uses the special natives syntax, which is something the
691 // parser records.
Andrei Popescu402d9372010-02-26 13:31:12 +0000692 bool allow_lazy = literal->AllowsLazyCompilation() &&
Steve Block44f0eee2011-05-26 01:26:41 +0100693 !LiveEditFunctionTracker::IsActive(info.isolate());
Steve Blockd0582a62009-12-15 09:54:21 +0000694
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100695 Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty());
696
Steve Blockd0582a62009-12-15 09:54:21 +0000697 // Generate code
Steve Blockd0582a62009-12-15 09:54:21 +0000698 if (FLAG_lazy && allow_lazy) {
Steve Block44f0eee2011-05-26 01:26:41 +0100699 Handle<Code> code = info.isolate()->builtins()->LazyCompile();
Ben Murdochf87a2032010-10-22 12:50:53 +0100700 info.SetCode(code);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100701 } else if ((V8::UseCrankshaft() && MakeCrankshaftCode(&info)) ||
702 (!V8::UseCrankshaft() && FullCodeGenerator::MakeCode(&info))) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100703 ASSERT(!info.code().is_null());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100704 scope_info = SerializedScopeInfo::Create(info.scope());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100705 } else {
706 return Handle<SharedFunctionInfo>::null();
Steve Blockd0582a62009-12-15 09:54:21 +0000707 }
708
Steve Block6ded16b2010-05-10 14:33:55 +0100709 // Create a shared function info object.
710 Handle<SharedFunctionInfo> result =
Steve Block44f0eee2011-05-26 01:26:41 +0100711 FACTORY->NewSharedFunctionInfo(literal->name(),
Steve Block6ded16b2010-05-10 14:33:55 +0100712 literal->materialized_literal_count(),
Ben Murdochf87a2032010-10-22 12:50:53 +0100713 info.code(),
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100714 scope_info);
Steve Block6ded16b2010-05-10 14:33:55 +0100715 SetFunctionInfo(result, literal, false, script);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100716 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100717 result->set_allows_lazy_compilation(allow_lazy);
Steve Blockd0582a62009-12-15 09:54:21 +0000718
719 // Set the expected number of properties for instances and return
720 // the resulting function.
Steve Block6ded16b2010-05-10 14:33:55 +0100721 SetExpectedNofPropertiesFromEstimate(result,
Steve Blockd0582a62009-12-15 09:54:21 +0000722 literal->expected_property_count());
Steve Block6ded16b2010-05-10 14:33:55 +0100723 live_edit_tracker.RecordFunctionInfo(result, literal);
724 return result;
Steve Blockd0582a62009-12-15 09:54:21 +0000725}
726
727
728// Sets the function info on a function.
729// The start_position points to the first '(' character after the function name
730// in the full script source. When counting characters in the script source the
731// the first character is number 0 (not 1).
Steve Block6ded16b2010-05-10 14:33:55 +0100732void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
Steve Blockd0582a62009-12-15 09:54:21 +0000733 FunctionLiteral* lit,
734 bool is_toplevel,
735 Handle<Script> script) {
Steve Block6ded16b2010-05-10 14:33:55 +0100736 function_info->set_length(lit->num_parameters());
737 function_info->set_formal_parameter_count(lit->num_parameters());
738 function_info->set_script(*script);
739 function_info->set_function_token_position(lit->function_token_position());
740 function_info->set_start_position(lit->start_position());
741 function_info->set_end_position(lit->end_position());
742 function_info->set_is_expression(lit->is_expression());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000743 function_info->set_is_anonymous(lit->is_anonymous());
Steve Block6ded16b2010-05-10 14:33:55 +0100744 function_info->set_is_toplevel(is_toplevel);
745 function_info->set_inferred_name(*lit->inferred_name());
746 function_info->SetThisPropertyAssignmentsInfo(
Steve Blockd0582a62009-12-15 09:54:21 +0000747 lit->has_only_simple_this_property_assignments(),
748 *lit->this_property_assignments());
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100749 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
Steve Block1e0659c2011-05-24 12:43:12 +0100750 function_info->set_strict_mode(lit->strict_mode());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000751 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
752 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
Steve Blockd0582a62009-12-15 09:54:21 +0000753}
754
755
Steve Block6ded16b2010-05-10 14:33:55 +0100756void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100757 CompilationInfo* info,
758 Handle<SharedFunctionInfo> shared) {
759 // SharedFunctionInfo is passed separately, because if CompilationInfo
760 // was created using Script object, it will not have it.
761
Ben Murdochf87a2032010-10-22 12:50:53 +0100762 // Log the code generation. If source information is available include
763 // script name and line number. Check explicitly whether logging is
764 // enabled as finding the line number is not free.
Ben Murdoch257744e2011-11-30 15:57:28 +0000765 if (info->isolate()->logger()->is_logging() ||
766 CpuProfiler::is_profiling(info->isolate())) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100767 Handle<Script> script = info->script();
768 Handle<Code> code = info->code();
Steve Block44f0eee2011-05-26 01:26:41 +0100769 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
770 return;
Andrei Popescu31002712010-02-23 13:46:05 +0000771 if (script->name()->IsString()) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100772 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
Steve Block6ded16b2010-05-10 14:33:55 +0100773 USE(line_num);
Steve Block44f0eee2011-05-26 01:26:41 +0100774 PROFILE(info->isolate(),
775 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
Ben Murdochf87a2032010-10-22 12:50:53 +0100776 *code,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100777 *shared,
Ben Murdochf87a2032010-10-22 12:50:53 +0100778 String::cast(script->name()),
779 line_num));
Andrei Popescu31002712010-02-23 13:46:05 +0000780 } else {
Steve Block44f0eee2011-05-26 01:26:41 +0100781 PROFILE(info->isolate(),
782 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
Ben Murdochf87a2032010-10-22 12:50:53 +0100783 *code,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100784 *shared,
785 shared->DebugName()));
Andrei Popescu31002712010-02-23 13:46:05 +0000786 }
787 }
Ben Murdochb8e0da22011-05-16 14:20:40 +0100788
Ben Murdoch8b112d22011-06-08 16:22:53 +0100789 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
Ben Murdochb8e0da22011-05-16 14:20:40 +0100790 Handle<Script>(info->script()),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000791 Handle<Code>(info->code()),
792 info));
Andrei Popescu31002712010-02-23 13:46:05 +0000793}
Andrei Popescu31002712010-02-23 13:46:05 +0000794
Steve Blocka7e24c12009-10-30 11:49:00 +0000795} } // namespace v8::internal