blob: 11ac1457e9ce9f98e8e63f3a26389cc1713580db [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
68 // Generate code and return it.
69 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
70 return result;
71}
72
73
74static Handle<JSFunction> MakeFunction(bool is_global,
75 bool is_eval,
76 Handle<Script> script,
77 v8::Extension* extension,
78 ScriptDataImpl* pre_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080
81 // Make sure we have an initial stack limit.
82 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +000083 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084
85 // Notify debugger
86 Debugger::OnBeforeCompile(script);
87
88 // Only allow non-global compiles for eval.
89 ASSERT(is_eval || is_global);
90
91 // Build AST.
92 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
93
kasper.lund212ac232008-07-16 07:07:30 +000094 // Check for parse errors.
95 if (lit == NULL) {
96 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +000097 return Handle<JSFunction>::null();
98 }
99
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100 // Measure how long it takes to do the compilation; only take the
101 // rest of the function into account to avoid overlap with the
102 // parsing statistics.
103 StatsRate* rate = is_eval
104 ? &Counters::compile_eval
105 : &Counters::compile;
106 StatsRateScope timer(rate);
107
108 // Compile the code.
kasper.lund212ac232008-07-16 07:07:30 +0000109 Handle<Code> code = MakeCode(lit, script, is_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110
kasper.lund212ac232008-07-16 07:07:30 +0000111 // Check for stack-overflow exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000113 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000114 return Handle<JSFunction>::null();
115 }
116
117 if (script->name()->IsString()) {
118 SmartPointer<char> data =
119 String::cast(script->name())->ToCString(DISALLOW_NULLS);
120 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
121 } else {
122 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
123 }
124
125 // Allocate function.
126 Handle<JSFunction> fun =
127 Factory::NewFunctionBoilerplate(lit->name(),
128 lit->materialized_literal_count(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000129 lit->contains_array_literal(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 code);
131
132 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
ager@chromium.org236ad962008-09-25 09:45:57 +0000133 RelocInfo::kNoPosition,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 lit->start_position(), lit->end_position(),
135 lit->is_expression(), true, script);
136
137 // Hint to the runtime system used when allocating space for initial
138 // property space by setting the expected number of properties for
139 // the instances of the function.
140 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 // Notify debugger
143 Debugger::OnAfterCompile(script, fun);
144
145 return fun;
146}
147
148
149static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
150
151
152Handle<JSFunction> Compiler::Compile(Handle<String> source,
mads.s.agercbaa0602008-08-14 13:41:48 +0000153 Handle<Object> script_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000154 int line_offset, int column_offset,
155 v8::Extension* extension,
156 ScriptDataImpl* input_pre_data) {
157 Counters::total_load_size.Increment(source->length());
158 Counters::total_compile_size.Increment(source->length());
159
160 // The VM is in the COMPILER state until exiting this function.
161 VMState state(COMPILER);
162
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000163 // Do a lookup in the compilation cache but not for extensions.
164 Handle<JSFunction> result;
165 if (extension == NULL) {
166 result = CompilationCache::LookupScript(source,
167 script_name,
168 line_offset,
169 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 }
171
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000172 if (result.is_null()) {
173 // No cache entry found. Do pre-parsing and compile the script.
174 ScriptDataImpl* pre_data = input_pre_data;
175 if (pre_data == NULL && source->length() >= FLAG_min_preparse_length) {
176 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
177 buf->Reset(source.location());
178 pre_data = PreParse(buf.value(), extension);
179 }
180
181 // Create a script object describing the script to be compiled.
182 Handle<Script> script = Factory::NewScript(source);
183 if (!script_name.is_null()) {
184 script->set_name(*script_name);
185 script->set_line_offset(Smi::FromInt(line_offset));
186 script->set_column_offset(Smi::FromInt(column_offset));
187 }
188
189 // Compile the function and add it to the cache.
190 result = MakeFunction(true, false, script, extension, pre_data);
191 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000192 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000193 }
194
195 // Get rid of the pre-parsing data (if necessary).
196 if (input_pre_data == NULL && pre_data != NULL) {
197 delete pre_data;
198 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 }
200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 return result;
202}
203
204
ager@chromium.org236ad962008-09-25 09:45:57 +0000205Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
206 int line_offset,
207 bool is_global) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 Counters::total_eval_size.Increment(source->length());
209 Counters::total_compile_size.Increment(source->length());
210
211 // The VM is in the COMPILER state until exiting this function.
212 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000213 CompilationCache::Entry entry = is_global
214 ? CompilationCache::EVAL_GLOBAL
215 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000217 // Do a lookup in the compilation cache; if the entry is not there,
218 // invoke the compiler and add the result to the cache.
219 Handle<JSFunction> result = CompilationCache::LookupEval(source, entry);
220 if (result.is_null()) {
221 // Create a script object describing the script to be compiled.
222 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000223 script->set_line_offset(Smi::FromInt(line_offset));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000224 result = MakeFunction(is_global, true, script, NULL, NULL);
225 if (!result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000226 CompilationCache::PutFunction(source, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000227 }
228 }
229 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230}
231
232
233bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000234 ZoneScope zone_scope(DELETE_ON_EXIT);
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);
238
239 // Make sure we have an initial stack limit.
240 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000241 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242
243 // Compute name, source code and script data.
244 Handle<String> name(String::cast(shared->name()));
245 Handle<Script> script(Script::cast(shared->script()));
246
247 int start_position = shared->start_position();
248 int end_position = shared->end_position();
249 bool is_expression = shared->is_expression();
250 Counters::total_compile_size.Increment(end_position - start_position);
251
252 // Generate the AST for the lazily compiled function. The AST may be
253 // NULL in case of parser stack overflow.
254 FunctionLiteral* lit = MakeLazyAST(script, name,
255 start_position,
256 end_position,
257 is_expression);
258
kasper.lund212ac232008-07-16 07:07:30 +0000259 // Check for parse errors.
260 if (lit == NULL) {
261 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000262 return false;
263 }
264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265 // Measure how long it takes to do the lazy compilation; only take
266 // the rest of the function into account to avoid overlap with the
267 // lazy parsing statistics.
268 StatsRateScope timer(&Counters::compile_lazy);
269
kasper.lund212ac232008-07-16 07:07:30 +0000270 // Compile the code.
271 Handle<Code> code = MakeCode(lit, script, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272
kasper.lund212ac232008-07-16 07:07:30 +0000273 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000274 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000275 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 return false;
277 }
278
279 // Generate the code, update the function info, and return the code.
280 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
281
282 // Update the shared function info with the compiled code.
283 shared->set_code(*code);
284
285 // Set the expected number of properties for instances.
286 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
287
288 // Check the function has compiled code.
289 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000290 return true;
291}
292
293
294} } // namespace v8::internal