blob: eb76d381ab0bf5f433422986ee69163ce969e4c3 [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,
43 bool is_eval) {
44 ASSERT(literal != NULL);
45
46 // Rewrite the AST by introducing .result assignments where needed.
47 if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) {
kasper.lund212ac232008-07-16 07:07:30 +000048 // Signal a stack overflow by returning a null handle. The stack
49 // overflow exception will be thrown by the caller.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050 return Handle<Code>::null();
51 }
52
53 // Compute top scope and allocate variables. For lazy compilation
54 // the top scope only contains the single lazily compiled function,
55 // so this doesn't re-allocate variables repeatedly.
56 Scope* top = literal->scope();
57 while (top->outer_scope() != NULL) top = top->outer_scope();
58 top->AllocateVariables();
59
60#ifdef DEBUG
61 if (Bootstrapper::IsActive() ?
62 FLAG_print_builtin_scopes :
63 FLAG_print_scopes) {
64 literal->scope()->Print();
65 }
66#endif
67
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000068 // Optimize the AST.
ager@chromium.org3bf7b912008-11-17 09:09:45 +000069 if (!Rewriter::Optimize(literal)) {
70 // Signal a stack overflow by returning a null handle. The stack
71 // overflow exception will be thrown by the caller.
72 return Handle<Code>::null();
73 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +000074
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075 // Generate code and return it.
76 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
77 return result;
78}
79
80
81static Handle<JSFunction> MakeFunction(bool is_global,
82 bool is_eval,
83 Handle<Script> script,
84 v8::Extension* extension,
85 ScriptDataImpl* pre_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000086 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087
88 // Make sure we have an initial stack limit.
89 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +000090 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000091
92 // Notify debugger
93 Debugger::OnBeforeCompile(script);
94
95 // Only allow non-global compiles for eval.
96 ASSERT(is_eval || is_global);
97
98 // Build AST.
99 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
100
kasper.lund212ac232008-07-16 07:07:30 +0000101 // Check for parse errors.
102 if (lit == NULL) {
103 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000104 return Handle<JSFunction>::null();
105 }
106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000107 // Measure how long it takes to do the compilation; only take the
108 // rest of the function into account to avoid overlap with the
109 // parsing statistics.
110 StatsRate* rate = is_eval
111 ? &Counters::compile_eval
112 : &Counters::compile;
113 StatsRateScope timer(rate);
114
115 // Compile the code.
kasper.lund212ac232008-07-16 07:07:30 +0000116 Handle<Code> code = MakeCode(lit, script, is_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
kasper.lund212ac232008-07-16 07:07:30 +0000118 // Check for stack-overflow exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000120 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 return Handle<JSFunction>::null();
122 }
123
124 if (script->name()->IsString()) {
125 SmartPointer<char> data =
126 String::cast(script->name())->ToCString(DISALLOW_NULLS);
127 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
128 } else {
129 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
130 }
131
132 // Allocate function.
133 Handle<JSFunction> fun =
134 Factory::NewFunctionBoilerplate(lit->name(),
135 lit->materialized_literal_count(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000136 lit->contains_array_literal(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 code);
138
139 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
ager@chromium.org236ad962008-09-25 09:45:57 +0000140 RelocInfo::kNoPosition,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 lit->start_position(), lit->end_position(),
142 lit->is_expression(), true, script);
143
144 // Hint to the runtime system used when allocating space for initial
145 // property space by setting the expected number of properties for
146 // the instances of the function.
147 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149 // Notify debugger
150 Debugger::OnAfterCompile(script, fun);
151
152 return fun;
153}
154
155
156static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
157
158
159Handle<JSFunction> Compiler::Compile(Handle<String> source,
mads.s.agercbaa0602008-08-14 13:41:48 +0000160 Handle<Object> script_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 int line_offset, int column_offset,
162 v8::Extension* extension,
163 ScriptDataImpl* input_pre_data) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000164 int source_length = source->length();
165 Counters::total_load_size.Increment(source_length);
166 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167
168 // The VM is in the COMPILER state until exiting this function.
169 VMState state(COMPILER);
170
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000171 // Do a lookup in the compilation cache but not for extensions.
172 Handle<JSFunction> result;
173 if (extension == NULL) {
174 result = CompilationCache::LookupScript(source,
175 script_name,
176 line_offset,
177 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 }
179
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000180 if (result.is_null()) {
181 // No cache entry found. Do pre-parsing and compile the script.
182 ScriptDataImpl* pre_data = input_pre_data;
ager@chromium.org870a0b62008-11-04 11:43:05 +0000183 if (pre_data == NULL && source_length >= FLAG_min_preparse_length) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000184 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
185 buf->Reset(source.location());
186 pre_data = PreParse(buf.value(), extension);
187 }
188
189 // Create a script object describing the script to be compiled.
190 Handle<Script> script = Factory::NewScript(source);
191 if (!script_name.is_null()) {
192 script->set_name(*script_name);
193 script->set_line_offset(Smi::FromInt(line_offset));
194 script->set_column_offset(Smi::FromInt(column_offset));
195 }
196
197 // Compile the function and add it to the cache.
198 result = MakeFunction(true, false, script, extension, pre_data);
199 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000200 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000201 }
202
203 // Get rid of the pre-parsing data (if necessary).
204 if (input_pre_data == NULL && pre_data != NULL) {
205 delete pre_data;
206 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207 }
208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209 return result;
210}
211
212
ager@chromium.org236ad962008-09-25 09:45:57 +0000213Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
214 int line_offset,
215 bool is_global) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000216 int source_length = source->length();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000217 Counters::total_eval_size.Increment(source_length);
218 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220 // The VM is in the COMPILER state until exiting this function.
221 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000222 CompilationCache::Entry entry = is_global
223 ? CompilationCache::EVAL_GLOBAL
224 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000226 // Do a lookup in the compilation cache; if the entry is not there,
227 // invoke the compiler and add the result to the cache.
228 Handle<JSFunction> result = CompilationCache::LookupEval(source, entry);
229 if (result.is_null()) {
230 // Create a script object describing the script to be compiled.
231 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000232 script->set_line_offset(Smi::FromInt(line_offset));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000233 result = MakeFunction(is_global, true, script, NULL, NULL);
234 if (!result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000235 CompilationCache::PutFunction(source, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000236 }
237 }
238 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000242bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared,
243 int loop_nesting) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000244 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246 // The VM is in the COMPILER state until exiting this function.
247 VMState state(COMPILER);
248
249 // Make sure we have an initial stack limit.
250 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000251 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252
253 // Compute name, source code and script data.
254 Handle<String> name(String::cast(shared->name()));
255 Handle<Script> script(Script::cast(shared->script()));
256
257 int start_position = shared->start_position();
258 int end_position = shared->end_position();
259 bool is_expression = shared->is_expression();
260 Counters::total_compile_size.Increment(end_position - start_position);
261
262 // Generate the AST for the lazily compiled function. The AST may be
263 // NULL in case of parser stack overflow.
264 FunctionLiteral* lit = MakeLazyAST(script, name,
265 start_position,
266 end_position,
267 is_expression);
268
kasper.lund212ac232008-07-16 07:07:30 +0000269 // Check for parse errors.
270 if (lit == NULL) {
271 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000272 return false;
273 }
274
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000275 // Update the loop nesting in the function literal.
276 lit->set_loop_nesting(loop_nesting);
277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 // Measure how long it takes to do the lazy compilation; only take
279 // the rest of the function into account to avoid overlap with the
280 // lazy parsing statistics.
281 StatsRateScope timer(&Counters::compile_lazy);
282
kasper.lund212ac232008-07-16 07:07:30 +0000283 // Compile the code.
284 Handle<Code> code = MakeCode(lit, script, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285
kasper.lund212ac232008-07-16 07:07:30 +0000286 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000288 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 return false;
290 }
291
292 // Generate the code, update the function info, and return the code.
293 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
294
295 // Update the shared function info with the compiled code.
296 shared->set_code(*code);
297
298 // Set the expected number of properties for instances.
299 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
300
301 // Check the function has compiled code.
302 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 return true;
304}
305
306
307} } // namespace v8::internal