blob: 896e9d4faeb881ef4a0c8497d6dee4b3b03026ec [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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"
35#include "scopes.h"
36#include "rewriter.h"
37#include "usage-analyzer.h"
38
39namespace v8 { namespace internal {
40
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041static Handle<Code> MakeCode(FunctionLiteral* literal,
42 Handle<Script> script,
ager@chromium.org381abbb2009-02-25 13:23:22 +000043 Handle<Context> context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044 bool is_eval) {
45 ASSERT(literal != NULL);
46
47 // Rewrite the AST by introducing .result assignments where needed.
48 if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) {
kasper.lund212ac232008-07-16 07:07:30 +000049 // Signal a stack overflow by returning a null handle. The stack
50 // overflow exception will be thrown by the caller.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051 return Handle<Code>::null();
52 }
53
54 // Compute top scope and allocate variables. For lazy compilation
55 // the top scope only contains the single lazily compiled function,
56 // so this doesn't re-allocate variables repeatedly.
57 Scope* top = literal->scope();
58 while (top->outer_scope() != NULL) top = top->outer_scope();
ager@chromium.org381abbb2009-02-25 13:23:22 +000059 top->AllocateVariables(context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060
61#ifdef DEBUG
62 if (Bootstrapper::IsActive() ?
63 FLAG_print_builtin_scopes :
64 FLAG_print_scopes) {
65 literal->scope()->Print();
66 }
67#endif
68
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000069 // Optimize the AST.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000070 if (!Rewriter::Optimize(literal)) {
71 // Signal a stack overflow by returning a null handle. The stack
72 // overflow exception will be thrown by the caller.
73 return Handle<Code>::null();
74 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000076 // Generate code and return it.
77 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
78 return result;
79}
80
81
82static Handle<JSFunction> MakeFunction(bool is_global,
83 bool is_eval,
84 Handle<Script> script,
ager@chromium.org381abbb2009-02-25 13:23:22 +000085 Handle<Context> context,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086 v8::Extension* extension,
87 ScriptDataImpl* pre_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000088 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089
90 // Make sure we have an initial stack limit.
91 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +000092 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093
94 // Notify debugger
95 Debugger::OnBeforeCompile(script);
96
97 // Only allow non-global compiles for eval.
98 ASSERT(is_eval || is_global);
99
100 // Build AST.
101 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
102
kasper.lund212ac232008-07-16 07:07:30 +0000103 // Check for parse errors.
104 if (lit == NULL) {
105 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000106 return Handle<JSFunction>::null();
107 }
108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109 // Measure how long it takes to do the compilation; only take the
110 // rest of the function into account to avoid overlap with the
111 // parsing statistics.
112 StatsRate* rate = is_eval
113 ? &Counters::compile_eval
114 : &Counters::compile;
115 StatsRateScope timer(rate);
116
117 // Compile the code.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000118 Handle<Code> code = MakeCode(lit, script, context, is_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119
kasper.lund212ac232008-07-16 07:07:30 +0000120 // Check for stack-overflow exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000122 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 return Handle<JSFunction>::null();
124 }
125
iposva@chromium.org245aa852009-02-10 00:49:54 +0000126#ifdef ENABLE_LOGGING_AND_PROFILING
127 // Log the code generation for the script. Check explicit whether logging is
128 // to avoid allocating when not required.
129 if (Logger::is_enabled()) {
130 if (script->name()->IsString()) {
131 SmartPointer<char> data =
132 String::cast(script->name())->ToCString(DISALLOW_NULLS);
133 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
134 } else {
135 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
136 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 }
iposva@chromium.org245aa852009-02-10 00:49:54 +0000138#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139
140 // Allocate function.
141 Handle<JSFunction> fun =
142 Factory::NewFunctionBoilerplate(lit->name(),
143 lit->materialized_literal_count(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000144 lit->contains_array_literal(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 code);
146
147 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
ager@chromium.org236ad962008-09-25 09:45:57 +0000148 RelocInfo::kNoPosition,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 lit->start_position(), lit->end_position(),
150 lit->is_expression(), true, script);
151
152 // Hint to the runtime system used when allocating space for initial
153 // property space by setting the expected number of properties for
154 // the instances of the function.
155 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 // Notify debugger
158 Debugger::OnAfterCompile(script, fun);
159
160 return fun;
161}
162
163
164static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
165
166
167Handle<JSFunction> Compiler::Compile(Handle<String> source,
mads.s.agercbaa0602008-08-14 13:41:48 +0000168 Handle<Object> script_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 int line_offset, int column_offset,
170 v8::Extension* extension,
171 ScriptDataImpl* input_pre_data) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000172 int source_length = source->length();
173 Counters::total_load_size.Increment(source_length);
174 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175
176 // The VM is in the COMPILER state until exiting this function.
177 VMState state(COMPILER);
178
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000179 // Do a lookup in the compilation cache but not for extensions.
180 Handle<JSFunction> result;
181 if (extension == NULL) {
182 result = CompilationCache::LookupScript(source,
183 script_name,
184 line_offset,
185 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186 }
187
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000188 if (result.is_null()) {
189 // No cache entry found. Do pre-parsing and compile the script.
190 ScriptDataImpl* pre_data = input_pre_data;
ager@chromium.org870a0b62008-11-04 11:43:05 +0000191 if (pre_data == NULL && source_length >= FLAG_min_preparse_length) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000192 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
193 buf->Reset(source.location());
194 pre_data = PreParse(buf.value(), extension);
195 }
196
197 // Create a script object describing the script to be compiled.
198 Handle<Script> script = Factory::NewScript(source);
199 if (!script_name.is_null()) {
200 script->set_name(*script_name);
201 script->set_line_offset(Smi::FromInt(line_offset));
202 script->set_column_offset(Smi::FromInt(column_offset));
203 }
204
205 // Compile the function and add it to the cache.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000206 result = MakeFunction(true,
207 false,
208 script,
209 Handle<Context>::null(),
210 extension,
211 pre_data);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000212 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000213 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000214 }
215
216 // Get rid of the pre-parsing data (if necessary).
217 if (input_pre_data == NULL && pre_data != NULL) {
218 delete pre_data;
219 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000220 }
221
ager@chromium.org8bb60582008-12-11 12:02:20 +0000222 if (result.is_null()) Top::ReportPendingMessages();
223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 return result;
225}
226
227
ager@chromium.org236ad962008-09-25 09:45:57 +0000228Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
ager@chromium.org381abbb2009-02-25 13:23:22 +0000229 Handle<Context> context,
ager@chromium.org236ad962008-09-25 09:45:57 +0000230 int line_offset,
231 bool is_global) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000232 int source_length = source->length();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000233 Counters::total_eval_size.Increment(source_length);
234 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235
236 // The VM is in the COMPILER state until exiting this function.
237 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000238 CompilationCache::Entry entry = is_global
239 ? CompilationCache::EVAL_GLOBAL
240 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000242 // Do a lookup in the compilation cache; if the entry is not there,
243 // invoke the compiler and add the result to the cache.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000244 Handle<JSFunction> result =
245 CompilationCache::LookupEval(source, context, entry);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000246 if (result.is_null()) {
247 // Create a script object describing the script to be compiled.
248 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000249 script->set_line_offset(Smi::FromInt(line_offset));
ager@chromium.org381abbb2009-02-25 13:23:22 +0000250 result = MakeFunction(is_global, true, script, context, NULL, NULL);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000251 if (!result.is_null()) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000252 CompilationCache::PutEvalFunction(source, context, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000253 }
254 }
ager@chromium.org8bb60582008-12-11 12:02:20 +0000255
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000256 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257}
258
259
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000260bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared,
261 int loop_nesting) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000262 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263
264 // The VM is in the COMPILER state until exiting this function.
265 VMState state(COMPILER);
266
267 // Make sure we have an initial stack limit.
268 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000269 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270
271 // Compute name, source code and script data.
272 Handle<String> name(String::cast(shared->name()));
273 Handle<Script> script(Script::cast(shared->script()));
274
275 int start_position = shared->start_position();
276 int end_position = shared->end_position();
277 bool is_expression = shared->is_expression();
278 Counters::total_compile_size.Increment(end_position - start_position);
279
280 // Generate the AST for the lazily compiled function. The AST may be
281 // NULL in case of parser stack overflow.
282 FunctionLiteral* lit = MakeLazyAST(script, name,
283 start_position,
284 end_position,
285 is_expression);
286
kasper.lund212ac232008-07-16 07:07:30 +0000287 // Check for parse errors.
288 if (lit == NULL) {
289 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000290 return false;
291 }
292
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000293 // Update the loop nesting in the function literal.
294 lit->set_loop_nesting(loop_nesting);
295
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 // Measure how long it takes to do the lazy compilation; only take
297 // the rest of the function into account to avoid overlap with the
298 // lazy parsing statistics.
299 StatsRateScope timer(&Counters::compile_lazy);
300
kasper.lund212ac232008-07-16 07:07:30 +0000301 // Compile the code.
ager@chromium.org381abbb2009-02-25 13:23:22 +0000302 Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303
kasper.lund212ac232008-07-16 07:07:30 +0000304 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000306 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000307 return false;
308 }
309
iposva@chromium.org245aa852009-02-10 00:49:54 +0000310#ifdef ENABLE_LOGGING_AND_PROFILING
311 // Log the code generation. If source information is available include script
312 // name and line number. Check explicit whether logging is enabled as finding
313 // the line number is not for free.
314 if (Logger::is_enabled()) {
315 if (script->name()->IsString()) {
316 int line_num = script->GetLineNumber(start_position);
317 if (line_num > 0) {
318 line_num += script->line_offset()->value() + 1;
319 }
320 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name(),
321 String::cast(script->name()), line_num));
322 } else {
323 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
324 }
325 }
326#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 // Update the shared function info with the compiled code.
329 shared->set_code(*code);
330
331 // Set the expected number of properties for instances.
332 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
333
334 // Check the function has compiled code.
335 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 return true;
337}
338
339
340} } // namespace v8::internal