blob: 62e838e6c9d60b3b976698a7afa137c2d8626e74 [file] [log] [blame]
ager@chromium.org71daaf62009-04-01 07:22:49 +00001// Copyright 2009 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
30#include "bootstrapper.h"
31#include "codegen-inl.h"
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000032#include "compilation-cache.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "compiler.h"
34#include "debug.h"
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000035#include "oprofile-agent.h"
ager@chromium.org71daaf62009-04-01 07:22:49 +000036#include "rewriter.h"
37#include "scopes.h"
38#include "usage-analyzer.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
40namespace v8 { namespace internal {
41
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042static Handle<Code> MakeCode(FunctionLiteral* literal,
43 Handle<Script> script,
ager@chromium.org381abbb2009-02-25 13:23:22 +000044 Handle<Context> context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000045 bool is_eval) {
46 ASSERT(literal != NULL);
47
48 // Rewrite the AST by introducing .result assignments where needed.
49 if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) {
kasper.lund212ac232008-07-16 07:07:30 +000050 // Signal a stack overflow by returning a null handle. The stack
51 // overflow exception will be thrown by the caller.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 return Handle<Code>::null();
53 }
54
55 // Compute top scope and allocate variables. For lazy compilation
56 // the top scope only contains the single lazily compiled function,
57 // so this doesn't re-allocate variables repeatedly.
58 Scope* top = literal->scope();
59 while (top->outer_scope() != NULL) top = top->outer_scope();
ager@chromium.org381abbb2009-02-25 13:23:22 +000060 top->AllocateVariables(context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000061
62#ifdef DEBUG
63 if (Bootstrapper::IsActive() ?
64 FLAG_print_builtin_scopes :
65 FLAG_print_scopes) {
66 literal->scope()->Print();
67 }
68#endif
69
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000070 // Optimize the AST.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000071 if (!Rewriter::Optimize(literal)) {
72 // Signal a stack overflow by returning a null handle. The stack
73 // overflow exception will be thrown by the caller.
74 return Handle<Code>::null();
75 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077 // Generate code and return it.
78 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
79 return result;
80}
81
82
83static Handle<JSFunction> MakeFunction(bool is_global,
84 bool is_eval,
85 Handle<Script> script,
ager@chromium.org381abbb2009-02-25 13:23:22 +000086 Handle<Context> context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 v8::Extension* extension,
88 ScriptDataImpl* pre_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000089 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
91 // Make sure we have an initial stack limit.
92 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +000093 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000094
ager@chromium.org65dad4b2009-04-23 08:48:43 +000095#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096 // Notify debugger
97 Debugger::OnBeforeCompile(script);
ager@chromium.org65dad4b2009-04-23 08:48:43 +000098#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099
100 // Only allow non-global compiles for eval.
101 ASSERT(is_eval || is_global);
102
103 // Build AST.
104 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
105
kasper.lund212ac232008-07-16 07:07:30 +0000106 // Check for parse errors.
107 if (lit == NULL) {
108 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000109 return Handle<JSFunction>::null();
110 }
111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 // Measure how long it takes to do the compilation; only take the
113 // rest of the function into account to avoid overlap with the
114 // parsing statistics.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000115 HistogramTimer* rate = is_eval
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116 ? &Counters::compile_eval
117 : &Counters::compile;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000118 HistogramTimerScope timer(rate);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
120 // Compile the code.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000121 Handle<Code> code = MakeCode(lit, script, context, is_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122
kasper.lund212ac232008-07-16 07:07:30 +0000123 // Check for stack-overflow exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000125 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 return Handle<JSFunction>::null();
127 }
128
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129#if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT
iposva@chromium.org245aa852009-02-10 00:49:54 +0000130 // Log the code generation for the script. Check explicit whether logging is
131 // to avoid allocating when not required.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000132 if (Logger::is_enabled() || OProfileAgent::is_enabled()) {
iposva@chromium.org245aa852009-02-10 00:49:54 +0000133 if (script->name()->IsString()) {
134 SmartPointer<char> data =
135 String::cast(script->name())->ToCString(DISALLOW_NULLS);
136 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137 OProfileAgent::CreateNativeCodeRegion(*data, code->address(),
138 code->ExecutableSize());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000139 } else {
140 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141 OProfileAgent::CreateNativeCodeRegion(is_eval ? "Eval" : "Script",
142 code->address(), code->ExecutableSize());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000143 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000145#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
147 // Allocate function.
148 Handle<JSFunction> fun =
149 Factory::NewFunctionBoilerplate(lit->name(),
150 lit->materialized_literal_count(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000151 lit->contains_array_literal(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000152 code);
153
154 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
ager@chromium.org236ad962008-09-25 09:45:57 +0000155 RelocInfo::kNoPosition,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156 lit->start_position(), lit->end_position(),
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000157 lit->is_expression(), true, script,
158 lit->inferred_name());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
160 // Hint to the runtime system used when allocating space for initial
161 // property space by setting the expected number of properties for
162 // the instances of the function.
163 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
164
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000165#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 // Notify debugger
167 Debugger::OnAfterCompile(script, fun);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000168#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169
170 return fun;
171}
172
173
174static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
175
176
177Handle<JSFunction> Compiler::Compile(Handle<String> source,
mads.s.agercbaa0602008-08-14 13:41:48 +0000178 Handle<Object> script_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 int line_offset, int column_offset,
180 v8::Extension* extension,
181 ScriptDataImpl* input_pre_data) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000182 int source_length = source->length();
183 Counters::total_load_size.Increment(source_length);
184 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
186 // The VM is in the COMPILER state until exiting this function.
187 VMState state(COMPILER);
188
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000189 // Do a lookup in the compilation cache but not for extensions.
190 Handle<JSFunction> result;
191 if (extension == NULL) {
192 result = CompilationCache::LookupScript(source,
193 script_name,
194 line_offset,
195 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 }
197
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000198 if (result.is_null()) {
199 // No cache entry found. Do pre-parsing and compile the script.
200 ScriptDataImpl* pre_data = input_pre_data;
ager@chromium.org870a0b62008-11-04 11:43:05 +0000201 if (pre_data == NULL && source_length >= FLAG_min_preparse_length) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000202 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
203 buf->Reset(source.location());
204 pre_data = PreParse(buf.value(), extension);
205 }
206
207 // Create a script object describing the script to be compiled.
208 Handle<Script> script = Factory::NewScript(source);
209 if (!script_name.is_null()) {
210 script->set_name(*script_name);
211 script->set_line_offset(Smi::FromInt(line_offset));
212 script->set_column_offset(Smi::FromInt(column_offset));
213 }
214
215 // Compile the function and add it to the cache.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000216 result = MakeFunction(true,
217 false,
218 script,
219 Handle<Context>::null(),
220 extension,
221 pre_data);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000222 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +0000223 CompilationCache::PutScript(source, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000224 }
225
226 // Get rid of the pre-parsing data (if necessary).
227 if (input_pre_data == NULL && pre_data != NULL) {
228 delete pre_data;
229 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230 }
231
ager@chromium.org8bb60582008-12-11 12:02:20 +0000232 if (result.is_null()) Top::ReportPendingMessages();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 return result;
234}
235
236
ager@chromium.org236ad962008-09-25 09:45:57 +0000237Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000238 Handle<Context> context,
ager@chromium.org236ad962008-09-25 09:45:57 +0000239 int line_offset,
240 bool is_global) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000241 int source_length = source->length();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000242 Counters::total_eval_size.Increment(source_length);
243 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244
245 // The VM is in the COMPILER state until exiting this function.
246 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000247 CompilationCache::Entry entry = is_global
248 ? CompilationCache::EVAL_GLOBAL
249 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000251 // Do a lookup in the compilation cache; if the entry is not there,
252 // invoke the compiler and add the result to the cache.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000253 Handle<JSFunction> result =
254 CompilationCache::LookupEval(source, context, entry);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000255 if (result.is_null()) {
256 // Create a script object describing the script to be compiled.
257 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000258 script->set_line_offset(Smi::FromInt(line_offset));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000259 result = MakeFunction(is_global, true, script, context, NULL, NULL);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000260 if (!result.is_null()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000261 CompilationCache::PutEval(source, context, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000262 }
263 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000264
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000265 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266}
267
268
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000269bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared,
270 int loop_nesting) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000271 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272
273 // The VM is in the COMPILER state until exiting this function.
274 VMState state(COMPILER);
275
276 // Make sure we have an initial stack limit.
277 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000278 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279
280 // Compute name, source code and script data.
281 Handle<String> name(String::cast(shared->name()));
282 Handle<Script> script(Script::cast(shared->script()));
283
284 int start_position = shared->start_position();
285 int end_position = shared->end_position();
286 bool is_expression = shared->is_expression();
287 Counters::total_compile_size.Increment(end_position - start_position);
288
289 // Generate the AST for the lazily compiled function. The AST may be
290 // NULL in case of parser stack overflow.
291 FunctionLiteral* lit = MakeLazyAST(script, name,
292 start_position,
293 end_position,
294 is_expression);
295
kasper.lund212ac232008-07-16 07:07:30 +0000296 // Check for parse errors.
297 if (lit == NULL) {
298 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000299 return false;
300 }
301
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000302 // Update the loop nesting in the function literal.
303 lit->set_loop_nesting(loop_nesting);
304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 // Measure how long it takes to do the lazy compilation; only take
306 // the rest of the function into account to avoid overlap with the
307 // lazy parsing statistics.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000308 HistogramTimerScope timer(&Counters::compile_lazy);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000309
kasper.lund212ac232008-07-16 07:07:30 +0000310 // Compile the code.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000311 Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312
kasper.lund212ac232008-07-16 07:07:30 +0000313 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000315 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 return false;
317 }
318
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319#if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT
iposva@chromium.org245aa852009-02-10 00:49:54 +0000320 // Log the code generation. If source information is available include script
321 // name and line number. Check explicit whether logging is enabled as finding
322 // the line number is not for free.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000323 if (Logger::is_enabled() || OProfileAgent::is_enabled()) {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000324 Handle<String> func_name(lit->name()->length() > 0 ?
325 *lit->name() : shared->inferred_name());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000326 if (script->name()->IsString()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000327 int line_num = GetScriptLineNumber(script, start_position);
iposva@chromium.org245aa852009-02-10 00:49:54 +0000328 if (line_num > 0) {
329 line_num += script->line_offset()->value() + 1;
330 }
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000331 LOG(CodeCreateEvent("LazyCompile", *code, *func_name,
iposva@chromium.org245aa852009-02-10 00:49:54 +0000332 String::cast(script->name()), line_num));
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000333 OProfileAgent::CreateNativeCodeRegion(*func_name,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000334 String::cast(script->name()),
335 line_num, code->address(),
336 code->ExecutableSize());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000337 } else {
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000338 LOG(CodeCreateEvent("LazyCompile", *code, *func_name));
339 OProfileAgent::CreateNativeCodeRegion(*func_name, code->address(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000340 code->ExecutableSize());
iposva@chromium.org245aa852009-02-10 00:49:54 +0000341 }
342 }
343#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000344
345 // Update the shared function info with the compiled code.
346 shared->set_code(*code);
347
348 // Set the expected number of properties for instances.
349 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
350
351 // Check the function has compiled code.
352 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000353 return true;
354}
355
356
357} } // namespace v8::internal