blob: c7e78067cfebce5161baaaf18ecc4520e33cfa92 [file] [log] [blame]
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001// Copyright 2011 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"
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +000039#include "lithium.h"
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000040#include "liveedit.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000041#include "parser.h"
ager@chromium.org71daaf62009-04-01 07:22:49 +000042#include "rewriter.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000043#include "runtime-profiler.h"
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +000044#include "scopeinfo.h"
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +000045#include "scopes.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000046#include "vm-state-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048namespace v8 {
49namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000051
52CompilationInfo::CompilationInfo(Handle<Script> script)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000053 : isolate_(script->GetIsolate()),
54 flags_(0),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000055 function_(NULL),
56 scope_(NULL),
57 script_(script),
58 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000059 pre_parse_data_(NULL),
60 supports_deoptimization_(false),
61 osr_ast_id_(AstNode::kNoNumber) {
62 Initialize(NONOPT);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000063}
64
65
66CompilationInfo::CompilationInfo(Handle<SharedFunctionInfo> shared_info)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000067 : isolate_(shared_info->GetIsolate()),
68 flags_(IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000069 function_(NULL),
70 scope_(NULL),
71 shared_info_(shared_info),
72 script_(Handle<Script>(Script::cast(shared_info->script()))),
73 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000074 pre_parse_data_(NULL),
75 supports_deoptimization_(false),
76 osr_ast_id_(AstNode::kNoNumber) {
77 Initialize(BASE);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000078}
79
80
81CompilationInfo::CompilationInfo(Handle<JSFunction> closure)
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 : isolate_(closure->GetIsolate()),
83 flags_(IsLazy::encode(true)),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000084 function_(NULL),
85 scope_(NULL),
86 closure_(closure),
87 shared_info_(Handle<SharedFunctionInfo>(closure->shared())),
88 script_(Handle<Script>(Script::cast(shared_info_->script()))),
89 extension_(NULL),
kasperl@chromium.orga5551262010-12-07 12:49:48 +000090 pre_parse_data_(NULL),
91 supports_deoptimization_(false),
92 osr_ast_id_(AstNode::kNoNumber) {
93 Initialize(BASE);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +000094}
95
96
ager@chromium.orgea91cc52011-05-23 06:06:11 +000097// Disable optimization for the rest of the compilation pipeline.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000098void CompilationInfo::DisableOptimization() {
lrn@chromium.org1c092762011-05-09 09:42:16 +000099 bool is_optimizable_closure =
100 FLAG_optimize_closures &&
101 closure_.is_null() &&
102 !scope_->HasTrivialOuterContext() &&
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000103 !scope_->outer_scope_calls_non_strict_eval() &&
lrn@chromium.org1c092762011-05-09 09:42:16 +0000104 !scope_->inside_with();
105 SetMode(is_optimizable_closure ? BASE : NONOPT);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000106}
107
108
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000109void CompilationInfo::AbortOptimization() {
110 Handle<Code> code(shared_info()->code());
111 SetCode(code);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000112}
113
114
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000115// Determine whether to use the full compiler for all code. If the flag
116// --always-full-compiler is specified this is the case. For the virtual frame
117// based compiler the full compiler is also used if a debugger is connected, as
118// the code from the full compiler supports mode precise break points. For the
119// crankshaft adaptive compiler debugging the optimized code is not possible at
120// all. However crankshaft support recompilation of functions, so in this case
121// the full compiler need not be be used if a debugger is attached, but only if
122// break points has actually been set.
lrn@chromium.org1c092762011-05-09 09:42:16 +0000123static bool is_debugging_active() {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000124#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 Isolate* isolate = Isolate::Current();
lrn@chromium.org1c092762011-05-09 09:42:16 +0000126 return V8::UseCrankshaft() ?
127 isolate->debug()->has_break_points() :
128 isolate->debugger()->IsDebuggerActive();
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000129#else
lrn@chromium.org1c092762011-05-09 09:42:16 +0000130 return false;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000131#endif
132}
133
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000134
lrn@chromium.org1c092762011-05-09 09:42:16 +0000135static bool AlwaysFullCompiler() {
136 return FLAG_always_full_compiler || is_debugging_active();
137}
138
139
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000140static void FinishOptimization(Handle<JSFunction> function, int64_t start) {
141 int opt_count = function->shared()->opt_count();
142 function->shared()->set_opt_count(opt_count + 1);
lrn@chromium.org8541d772010-12-15 12:05:09 +0000143 double ms = static_cast<double>(OS::Ticks() - start) / 1000;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000144 if (FLAG_trace_opt) {
145 PrintF("[optimizing: ");
146 function->PrintName();
147 PrintF(" / %" V8PRIxPTR, reinterpret_cast<intptr_t>(*function));
148 PrintF(" - took %0.3f ms]\n", ms);
149 }
150 if (FLAG_trace_opt_stats) {
151 static double compilation_time = 0.0;
152 static int compiled_functions = 0;
153 static int code_size = 0;
154
155 compilation_time += ms;
156 compiled_functions++;
157 code_size += function->shared()->SourceSize();
158 PrintF("Compiled: %d functions with %d byte source size in %fms.\n",
159 compiled_functions,
160 code_size,
161 compilation_time);
162 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000163}
164
165
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000166static bool MakeCrankshaftCode(CompilationInfo* info) {
167 // Test if we can optimize this function when asked to. We can only
168 // do this after the scopes are computed.
169 if (!info->AllowOptimize()) info->DisableOptimization();
170
171 // In case we are not optimizing simply return the code from
172 // the full code generator.
173 if (!info->IsOptimizing()) {
174 return FullCodeGenerator::MakeCode(info);
175 }
176
177 // We should never arrive here if there is not code object on the
178 // shared function object.
179 Handle<Code> code(info->shared_info()->code());
180 ASSERT(code->kind() == Code::FUNCTION);
181
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000182 // We should never arrive here if optimization has been disabled on the
183 // shared function info.
184 ASSERT(!info->shared_info()->optimization_disabled());
185
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000186 // Fall back to using the full code generator if it's not possible
187 // to use the Hydrogen-based optimizing compiler. We already have
188 // generated code for this from the shared function object.
189 if (AlwaysFullCompiler() || !FLAG_use_hydrogen) {
190 info->SetCode(code);
191 return true;
192 }
193
194 // Limit the number of times we re-compile a functions with
195 // the optimizing compiler.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000196 const int kMaxOptCount =
197 FLAG_deopt_every_n_times == 0 ? Compiler::kDefaultMaxOptCount : 1000;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000198 if (info->shared_info()->opt_count() > kMaxOptCount) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000199 info->AbortOptimization();
200 Handle<JSFunction> closure = info->closure();
201 info->shared_info()->DisableOptimization(*closure);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000202 // True indicates the compilation pipeline is still going, not
203 // necessarily that we optimized the code.
204 return true;
205 }
206
207 // Due to an encoding limit on LUnallocated operands in the Lithium
208 // language, we cannot optimize functions with too many formal parameters
209 // or perform on-stack replacement for function with too many
210 // stack-allocated local variables.
211 //
ager@chromium.org9ee27ae2011-03-02 13:43:26 +0000212 // The encoding is as a signed value, with parameters and receiver using
213 // the negative indices and locals the non-negative ones.
ager@chromium.org04921a82011-06-27 13:21:41 +0000214 const int parameter_limit = -LUnallocated::kMinFixedIndex;
215 const int locals_limit = LUnallocated::kMaxFixedIndex;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000216 Scope* scope = info->scope();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000217 if ((scope->num_parameters() + 1) > parameter_limit ||
ager@chromium.org04921a82011-06-27 13:21:41 +0000218 (info->osr_ast_id() != AstNode::kNoNumber &&
219 scope->num_parameters() + 1 + scope->num_stack_slots() > locals_limit)) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000220 info->AbortOptimization();
221 Handle<JSFunction> closure = info->closure();
222 info->shared_info()->DisableOptimization(*closure);
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 // Take --hydrogen-filter into account.
229 Vector<const char> filter = CStrVector(FLAG_hydrogen_filter);
230 Handle<String> name = info->function()->debug_name();
231 bool match = filter.is_empty() || name->IsEqualTo(filter);
232 if (!match) {
233 info->SetCode(code);
234 return true;
235 }
236
237 // Recompile the unoptimized version of the code if the current version
238 // doesn't have deoptimization support. Alternatively, we may decide to
239 // run the full code generator to get a baseline for the compile-time
240 // performance of the hydrogen-based compiler.
241 int64_t start = OS::Ticks();
242 bool should_recompile = !info->shared_info()->has_deoptimization_support();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000243 if (should_recompile || FLAG_hydrogen_stats) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000244 HPhase phase(HPhase::kFullCodeGen);
245 CompilationInfo unoptimized(info->shared_info());
246 // Note that we use the same AST that we will use for generating the
247 // optimized code.
248 unoptimized.SetFunction(info->function());
249 unoptimized.SetScope(info->scope());
250 if (should_recompile) unoptimized.EnableDeoptimizationSupport();
251 bool succeeded = FullCodeGenerator::MakeCode(&unoptimized);
252 if (should_recompile) {
253 if (!succeeded) return false;
254 Handle<SharedFunctionInfo> shared = info->shared_info();
255 shared->EnableDeoptimizationSupport(*unoptimized.code());
256 // The existing unoptimized code was replaced with the new one.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000257 Compiler::RecordFunctionCompilation(
258 Logger::LAZY_COMPILE_TAG, &unoptimized, shared);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000259 }
260 }
261
262 // Check that the unoptimized, shared code is ready for
263 // optimizations. When using the always_opt flag we disregard the
264 // optimizable marker in the code object and optimize anyway. This
265 // is safe as long as the unoptimized code has deoptimization
266 // support.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000267 ASSERT(FLAG_always_opt || code->optimizable());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000268 ASSERT(info->shared_info()->has_deoptimization_support());
269
270 if (FLAG_trace_hydrogen) {
271 PrintF("-----------------------------------------------------------\n");
272 PrintF("Compiling method %s using hydrogen\n", *name->ToCString());
273 HTracer::Instance()->TraceCompilation(info->function());
274 }
275
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000276 Handle<Context> global_context(info->closure()->context()->global_context());
277 TypeFeedbackOracle oracle(code, global_context);
278 HGraphBuilder builder(info, &oracle);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000279 HPhase phase(HPhase::kTotal);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000280 HGraph* graph = builder.CreateGraph();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000281 if (info->isolate()->has_pending_exception()) {
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000282 info->SetCode(Handle<Code>::null());
283 return false;
284 }
285
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000286 if (graph != NULL && FLAG_build_lithium) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000287 Handle<Code> optimized_code = graph->Compile(info);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000288 if (!optimized_code.is_null()) {
289 info->SetCode(optimized_code);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000290 FinishOptimization(info->closure(), start);
291 return true;
292 }
293 }
294
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000295 // Keep using the shared code.
296 info->AbortOptimization();
297 if (!builder.inline_bailout()) {
298 // Mark the shared code as unoptimizable unless it was an inlined
299 // function that bailed out.
300 Handle<JSFunction> closure = info->closure();
301 info->shared_info()->DisableOptimization(*closure);
302 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000303 // True indicates the compilation pipeline is still going, not necessarily
304 // that we optimized the code.
305 return true;
306}
307
308
lrn@chromium.org1c092762011-05-09 09:42:16 +0000309static bool GenerateCode(CompilationInfo* info) {
310 return V8::UseCrankshaft() ?
311 MakeCrankshaftCode(info) :
312 FullCodeGenerator::MakeCode(info);
313}
314
315
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000316static bool MakeCode(CompilationInfo* info) {
317 // Precondition: code has been parsed. Postcondition: the code field in
318 // the compilation info is set if compilation succeeded.
319 ASSERT(info->function() != NULL);
lrn@chromium.org1c092762011-05-09 09:42:16 +0000320 return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +0000321}
322
323
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000324#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000325bool Compiler::MakeCodeForLiveEdit(CompilationInfo* info) {
326 // Precondition: code has been parsed. Postcondition: the code field in
327 // the compilation info is set if compilation succeeded.
328 bool succeeded = MakeCode(info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000329 if (!info->shared_info().is_null()) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000330 Handle<SerializedScopeInfo> scope_info =
331 SerializedScopeInfo::Create(info->scope());
332 info->shared_info()->set_scope_info(*scope_info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000333 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000334 return succeeded;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000335}
336#endif
337
338
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000339static Handle<SharedFunctionInfo> MakeFunctionInfo(CompilationInfo* info) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000340 Isolate* isolate = info->isolate();
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000341 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000342 PostponeInterruptsScope postpone(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000343
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000344 ASSERT(!isolate->global_context().is_null());
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000345 Handle<Script> script = info->script();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000346 script->set_context_data((*isolate->global_context())->data());
ager@chromium.orge2902be2009-06-08 12:21:35 +0000347
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000348#ifdef ENABLE_DEBUGGER_SUPPORT
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000349 if (info->is_eval()) {
350 Script::CompilationType compilation_type = Script::COMPILATION_TYPE_EVAL;
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000351 script->set_compilation_type(Smi::FromInt(compilation_type));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000352 // For eval scripts add information on the function from which eval was
353 // called.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000354 if (info->is_eval()) {
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000355 StackTraceFrameIterator it(isolate);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000356 if (!it.done()) {
357 script->set_eval_from_shared(
358 JSFunction::cast(it.frame()->function())->shared());
vegorov@chromium.org74f333b2011-04-06 11:17:46 +0000359 Code* code = it.frame()->LookupCode();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360 int offset = static_cast<int>(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000361 it.frame()->pc() - code->instruction_start());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000362 script->set_eval_from_instructions_offset(Smi::FromInt(offset));
363 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000364 }
365 }
366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000367 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000368 isolate->debugger()->OnBeforeCompile(script);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000369#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000370
371 // Only allow non-global compiles for eval.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000372 ASSERT(info->is_eval() || info->is_global());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000373
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000374 if (!ParserApi::Parse(info)) return Handle<SharedFunctionInfo>::null();
kasper.lund212ac232008-07-16 07:07:30 +0000375
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000376 // Measure how long it takes to do the compilation; only take the
377 // rest of the function into account to avoid overlap with the
378 // parsing statistics.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000379 HistogramTimer* rate = info->is_eval()
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000380 ? info->isolate()->counters()->compile_eval()
381 : info->isolate()->counters()->compile();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000382 HistogramTimerScope timer(rate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383
384 // Compile the code.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000385 FunctionLiteral* lit = info->function();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000386 LiveEditFunctionTracker live_edit_tracker(isolate, lit);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000387 if (!MakeCode(info)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000388 isolate->StackOverflow();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000389 return Handle<SharedFunctionInfo>::null();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390 }
391
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000392 // Allocate function.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000393 ASSERT(!info->code().is_null());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000394 Handle<SharedFunctionInfo> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000395 isolate->factory()->NewSharedFunctionInfo(
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000396 lit->name(),
397 lit->materialized_literal_count(),
398 info->code(),
399 SerializedScopeInfo::Create(info->scope()));
400
401 ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position());
402 Compiler::SetFunctionInfo(result, lit, true, script);
403
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000404 if (script->name()->IsString()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000405 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000406 info->is_eval()
407 ? Logger::EVAL_TAG
408 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
409 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000410 *result,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000411 String::cast(script->name())));
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000412 GDBJIT(AddCode(Handle<String>(String::cast(script->name())),
413 script,
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000414 info->code(),
415 info));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000416 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000417 PROFILE(isolate, CodeCreateEvent(
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000418 info->is_eval()
419 ? Logger::EVAL_TAG
420 : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script),
421 *info->code(),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000422 *result,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000423 isolate->heap()->empty_string()));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000424 GDBJIT(AddCode(Handle<String>(), script, info->code(), info));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425 }
426
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427 // Hint to the runtime system used when allocating space for initial
428 // property space by setting the expected number of properties for
429 // the instances of the function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000430 SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000431
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000432#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433 // Notify debugger
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000434 isolate->debugger()->OnAfterCompile(
435 script, Debugger::NO_AFTER_COMPILE_FLAGS);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000436#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000438 live_edit_tracker.RecordFunctionInfo(result, lit);
439
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000440 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441}
442
443
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000444Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source,
445 Handle<Object> script_name,
446 int line_offset,
447 int column_offset,
448 v8::Extension* extension,
449 ScriptDataImpl* input_pre_data,
450 Handle<Object> script_data,
451 NativesFlag natives) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000452 Isolate* isolate = source->GetIsolate();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000453 int source_length = source->length();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000454 isolate->counters()->total_load_size()->Increment(source_length);
455 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456
457 // The VM is in the COMPILER state until exiting this function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000458 VMState state(isolate, COMPILER);
459
460 CompilationCache* compilation_cache = isolate->compilation_cache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000462 // Do a lookup in the compilation cache but not for extensions.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000463 Handle<SharedFunctionInfo> result;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000464 if (extension == NULL) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000465 result = compilation_cache->LookupScript(source,
466 script_name,
467 line_offset,
468 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469 }
470
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000471 if (result.is_null()) {
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000472 // No cache entry found. Do pre-parsing, if it makes sense, and compile
473 // the script.
474 // Building preparse data that is only used immediately after is only a
475 // saving if we might skip building the AST for lazily compiled functions.
476 // I.e., preparse data isn't relevant when the lazy flag is off, and
477 // for small sources, odds are that there aren't many functions
478 // that would be compiled lazily anyway, so we skip the preparse step
479 // in that case too.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000480 ScriptDataImpl* pre_data = input_pre_data;
danno@chromium.orgb6451162011-08-17 14:33:23 +0000481 bool harmony_block_scoping = natives != NATIVES_CODE &&
482 FLAG_harmony_block_scoping;
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000483 if (pre_data == NULL
sgjesse@chromium.org2ec107f2010-09-13 09:19:46 +0000484 && source_length >= FLAG_min_preparse_length) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000485 if (source->IsExternalTwoByteString()) {
486 ExternalTwoByteStringUC16CharacterStream stream(
487 Handle<ExternalTwoByteString>::cast(source), 0, source->length());
danno@chromium.orgb6451162011-08-17 14:33:23 +0000488 pre_data = ParserApi::PartialPreParse(&stream,
489 extension,
490 harmony_block_scoping);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000491 } else {
492 GenericStringUC16CharacterStream stream(source, 0, source->length());
danno@chromium.orgb6451162011-08-17 14:33:23 +0000493 pre_data = ParserApi::PartialPreParse(&stream,
494 extension,
495 harmony_block_scoping);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000496 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000497 }
498
499 // Create a script object describing the script to be compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000500 Handle<Script> script = FACTORY->NewScript(source);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000501 if (natives == NATIVES_CODE) {
502 script->set_type(Smi::FromInt(Script::TYPE_NATIVE));
503 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000504 if (!script_name.is_null()) {
505 script->set_name(*script_name);
506 script->set_line_offset(Smi::FromInt(line_offset));
507 script->set_column_offset(Smi::FromInt(column_offset));
508 }
509
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000510 script->set_data(script_data.is_null() ? HEAP->undefined_value()
ager@chromium.org5c838252010-02-19 08:53:10 +0000511 : *script_data);
512
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000513 // Compile the function and add it to the cache.
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000514 CompilationInfo info(script);
515 info.MarkAsGlobal();
516 info.SetExtension(extension);
517 info.SetPreParseData(pre_data);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000518 if (natives == NATIVES_CODE) {
519 info.MarkAsAllowingNativesSyntax();
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000520 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000521 result = MakeFunctionInfo(&info);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000522 if (extension == NULL && !result.is_null()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000523 compilation_cache->PutScript(source, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000524 }
525
526 // Get rid of the pre-parsing data (if necessary).
527 if (input_pre_data == NULL && pre_data != NULL) {
528 delete pre_data;
529 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 }
531
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000532 if (result.is_null()) isolate->ReportPendingMessages();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 return result;
534}
535
536
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000537Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source,
538 Handle<Context> context,
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000539 bool is_global,
540 StrictModeFlag strict_mode) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000541 Isolate* isolate = source->GetIsolate();
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000542 int source_length = source->length();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000543 isolate->counters()->total_eval_size()->Increment(source_length);
544 isolate->counters()->total_compile_size()->Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545
546 // The VM is in the COMPILER state until exiting this function.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000547 VMState state(isolate, COMPILER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000549 // Do a lookup in the compilation cache; if the entry is not there, invoke
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000550 // the compiler and add the result to the cache.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000551 Handle<SharedFunctionInfo> result;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000552 CompilationCache* compilation_cache = isolate->compilation_cache();
553 result = compilation_cache->LookupEval(source,
554 context,
555 is_global,
556 strict_mode);
ager@chromium.orgadd848f2009-08-13 12:44:13 +0000557
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000558 if (result.is_null()) {
559 // Create a script object describing the script to be compiled.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000560 Handle<Script> script = isolate->factory()->NewScript(source);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000561 CompilationInfo info(script);
562 info.MarkAsEval();
563 if (is_global) info.MarkAsGlobal();
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000564 if (strict_mode == kStrictMode) info.MarkAsStrictMode();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000565 info.SetCallingContext(context);
566 result = MakeFunctionInfo(&info);
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000567 if (!result.is_null()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000568 CompilationCache* compilation_cache = isolate->compilation_cache();
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000569 // If caller is strict mode, the result must be strict as well,
570 // but not the other way around. Consider:
571 // eval("'use strict'; ...");
572 ASSERT(strict_mode == kNonStrictMode || result->strict_mode());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000573 compilation_cache->PutEval(source, context, is_global, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000574 }
575 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000576
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000577 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578}
579
580
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000581bool Compiler::CompileLazy(CompilationInfo* info) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000582 Isolate* isolate = info->isolate();
583
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000584 ZoneScope zone_scope(isolate, DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585
586 // The VM is in the COMPILER state until exiting this function.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000587 VMState state(isolate, COMPILER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000589 PostponeInterruptsScope postpone(isolate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000591 Handle<SharedFunctionInfo> shared = info->shared_info();
kmillikin@chromium.orgf05f2912010-09-30 10:07:24 +0000592 int compiled_size = shared->end_position() - shared->start_position();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000593 isolate->counters()->total_compile_size()->Increment(compiled_size);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000595 // Generate the AST for the lazily compiled function.
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000596 if (ParserApi::Parse(info)) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000597 // Measure how long it takes to do the lazy compilation; only take the
598 // rest of the function into account to avoid overlap with the lazy
599 // parsing statistics.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000600 HistogramTimerScope timer(isolate->counters()->compile_lazy());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000602 // After parsing we know function's strict mode. Remember it.
603 if (info->function()->strict_mode()) {
604 shared->set_strict_mode(true);
605 info->MarkAsStrictMode();
606 }
607
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000608 // Compile the code.
609 if (!MakeCode(info)) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000610 if (!isolate->has_pending_exception()) {
611 isolate->StackOverflow();
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000612 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000613 } else {
614 ASSERT(!info->code().is_null());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000615 Handle<Code> code = info->code();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000616 // Set optimizable to false if this is disallowed by the shared
617 // function info, e.g., we might have flushed the code and must
618 // reset this bit when lazy compiling the code again.
619 if (shared->optimization_disabled()) code->set_optimizable(false);
620
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000621 Handle<JSFunction> function = info->closure();
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000622 RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info, shared);
kasper.lund212ac232008-07-16 07:07:30 +0000623
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000624 if (info->IsOptimizing()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000625 ASSERT(shared->scope_info() != SerializedScopeInfo::Empty());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000626 function->ReplaceCode(*code);
627 } else {
628 // Update the shared function info with the compiled code and the
629 // scope info. Please note, that the order of the shared function
630 // info initialization is important since set_scope_info might
631 // trigger a GC, causing the ASSERT below to be invalid if the code
632 // was flushed. By settting the code object last we avoid this.
633 Handle<SerializedScopeInfo> scope_info =
634 SerializedScopeInfo::Create(info->scope());
635 shared->set_scope_info(*scope_info);
636 shared->set_code(*code);
637 if (!function.is_null()) {
638 function->ReplaceCode(*code);
639 ASSERT(!function->IsOptimized());
640 }
641
642 // Set the expected number of properties for instances.
643 FunctionLiteral* lit = info->function();
644 int expected = lit->expected_property_count();
645 SetExpectedNofPropertiesFromEstimate(shared, expected);
646
647 // Set the optimization hints after performing lazy compilation, as
648 // these are not set when the function is set up as a lazily
649 // compiled function.
650 shared->SetThisPropertyAssignmentsInfo(
651 lit->has_only_simple_this_property_assignments(),
652 *lit->this_property_assignments());
653
654 // Check the function has compiled code.
655 ASSERT(shared->is_compiled());
656 shared->set_code_age(0);
657
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000658 if (info->AllowOptimize() && !shared->optimization_disabled()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000659 // If we're asked to always optimize, we compile the optimized
660 // version of the function right away - unless the debugger is
661 // active as it makes no sense to compile optimized code then.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000662 if (FLAG_always_opt &&
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000663 !Isolate::Current()->DebuggerHasBreakPoints()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000664 CompilationInfo optimized(function);
665 optimized.SetOptimizing(AstNode::kNoNumber);
666 return CompileLazy(&optimized);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000667 }
668 }
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000669 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000671 return true;
672 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 }
674
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000675 ASSERT(info->code().is_null());
676 return false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677}
678
679
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000680Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000681 Handle<Script> script) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000682 // Precondition: code has been parsed and scopes have been analyzed.
683 CompilationInfo info(script);
684 info.SetFunction(literal);
685 info.SetScope(literal->scope());
danno@chromium.org40cb8782011-05-25 07:58:50 +0000686 if (literal->scope()->is_strict_mode()) info.MarkAsStrictMode();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000687
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000688 LiveEditFunctionTracker live_edit_tracker(info.isolate(), literal);
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000689 // Determine if the function can be lazily compiled. This is necessary to
690 // allow some of our builtin JS files to be lazily compiled. These
691 // builtins cannot be handled lazily by the parser, since we have to know
692 // if a function uses the special natives syntax, which is something the
693 // parser records.
ager@chromium.org5c838252010-02-19 08:53:10 +0000694 bool allow_lazy = literal->AllowsLazyCompilation() &&
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000695 !LiveEditFunctionTracker::IsActive(info.isolate());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000696
ager@chromium.orgb5737492010-07-15 09:29:43 +0000697 Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty());
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000698
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000699 // Generate code
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000700 if (FLAG_lazy && allow_lazy) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000701 Handle<Code> code = info.isolate()->builtins()->LazyCompile();
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000702 info.SetCode(code);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000703 } else if ((V8::UseCrankshaft() && MakeCrankshaftCode(&info)) ||
704 (!V8::UseCrankshaft() && FullCodeGenerator::MakeCode(&info))) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000705 ASSERT(!info.code().is_null());
ager@chromium.orgb5737492010-07-15 09:29:43 +0000706 scope_info = SerializedScopeInfo::Create(info.scope());
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000707 } else {
708 return Handle<SharedFunctionInfo>::null();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000709 }
710
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000711 // Create a shared function info object.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000712 Handle<SharedFunctionInfo> result =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000713 FACTORY->NewSharedFunctionInfo(literal->name(),
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000714 literal->materialized_literal_count(),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000715 info.code(),
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000716 scope_info);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000717 SetFunctionInfo(result, literal, false, script);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000718 RecordFunctionCompilation(Logger::FUNCTION_TAG, &info, result);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000719 result->set_allows_lazy_compilation(allow_lazy);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000720
721 // Set the expected number of properties for instances and return
722 // the resulting function.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000723 SetExpectedNofPropertiesFromEstimate(result,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000724 literal->expected_property_count());
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000725 live_edit_tracker.RecordFunctionInfo(result, literal);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000726 return result;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000727}
728
729
730// Sets the function info on a function.
731// The start_position points to the first '(' character after the function name
732// in the full script source. When counting characters in the script source the
733// the first character is number 0 (not 1).
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000734void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info,
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000735 FunctionLiteral* lit,
736 bool is_toplevel,
737 Handle<Script> script) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000738 function_info->set_length(lit->num_parameters());
739 function_info->set_formal_parameter_count(lit->num_parameters());
740 function_info->set_script(*script);
741 function_info->set_function_token_position(lit->function_token_position());
742 function_info->set_start_position(lit->start_position());
743 function_info->set_end_position(lit->end_position());
744 function_info->set_is_expression(lit->is_expression());
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000745 function_info->set_is_anonymous(lit->is_anonymous());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000746 function_info->set_is_toplevel(is_toplevel);
747 function_info->set_inferred_name(*lit->inferred_name());
748 function_info->SetThisPropertyAssignmentsInfo(
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000749 lit->has_only_simple_this_property_assignments(),
750 *lit->this_property_assignments());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000751 function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation());
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000752 function_info->set_strict_mode(lit->strict_mode());
whesse@chromium.org7b260152011-06-20 15:33:18 +0000753 function_info->set_uses_arguments(lit->scope()->arguments() != NULL);
754 function_info->set_has_duplicate_parameters(lit->has_duplicate_parameters());
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000755}
756
757
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000758void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000759 CompilationInfo* info,
760 Handle<SharedFunctionInfo> shared) {
761 // SharedFunctionInfo is passed separately, because if CompilationInfo
762 // was created using Script object, it will not have it.
763
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000764 // Log the code generation. If source information is available include
765 // script name and line number. Check explicitly whether logging is
766 // enabled as finding the line number is not free.
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000767 if (info->isolate()->logger()->is_logging() ||
768 CpuProfiler::is_profiling(info->isolate())) {
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000769 Handle<Script> script = info->script();
770 Handle<Code> code = info->code();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000771 if (*code == info->isolate()->builtins()->builtin(Builtins::kLazyCompile))
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000772 return;
ager@chromium.org5c838252010-02-19 08:53:10 +0000773 if (script->name()->IsString()) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000774 int line_num = GetScriptLineNumber(script, shared->start_position()) + 1;
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000775 USE(line_num);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000776 PROFILE(info->isolate(),
777 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000778 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000779 *shared,
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000780 String::cast(script->name()),
781 line_num));
ager@chromium.org5c838252010-02-19 08:53:10 +0000782 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000783 PROFILE(info->isolate(),
784 CodeCreateEvent(Logger::ToNativeByScript(tag, *script),
ager@chromium.orgb61a0d12010-10-13 08:35:23 +0000785 *code,
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000786 *shared,
787 shared->DebugName()));
ager@chromium.org5c838252010-02-19 08:53:10 +0000788 }
789 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000790
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000791 GDBJIT(AddCode(Handle<String>(shared->DebugName()),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000792 Handle<Script>(info->script()),
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000793 Handle<Code>(info->code()),
794 info));
ager@chromium.org5c838252010-02-19 08:53:10 +0000795}
ager@chromium.org5c838252010-02-19 08:53:10 +0000796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797} } // namespace v8::internal