blob: 771620e0dd858d28255a70e3597a76e9767bc4fc [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.
69 Rewriter::Optimize(literal);
70
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071 // Generate code and return it.
72 Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval);
73 return result;
74}
75
76
77static Handle<JSFunction> MakeFunction(bool is_global,
78 bool is_eval,
79 Handle<Script> script,
80 v8::Extension* extension,
81 ScriptDataImpl* pre_data) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000082 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083
84 // Make sure we have an initial stack limit.
85 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +000086 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087
88 // Notify debugger
89 Debugger::OnBeforeCompile(script);
90
91 // Only allow non-global compiles for eval.
92 ASSERT(is_eval || is_global);
93
94 // Build AST.
95 FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data);
96
kasper.lund212ac232008-07-16 07:07:30 +000097 // Check for parse errors.
98 if (lit == NULL) {
99 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000100 return Handle<JSFunction>::null();
101 }
102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 // Measure how long it takes to do the compilation; only take the
104 // rest of the function into account to avoid overlap with the
105 // parsing statistics.
106 StatsRate* rate = is_eval
107 ? &Counters::compile_eval
108 : &Counters::compile;
109 StatsRateScope timer(rate);
110
111 // Compile the code.
kasper.lund212ac232008-07-16 07:07:30 +0000112 Handle<Code> code = MakeCode(lit, script, is_eval);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113
kasper.lund212ac232008-07-16 07:07:30 +0000114 // Check for stack-overflow exceptions.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000115 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000116 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 return Handle<JSFunction>::null();
118 }
119
120 if (script->name()->IsString()) {
121 SmartPointer<char> data =
122 String::cast(script->name())->ToCString(DISALLOW_NULLS);
123 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data));
124 } else {
125 LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, ""));
126 }
127
128 // Allocate function.
129 Handle<JSFunction> fun =
130 Factory::NewFunctionBoilerplate(lit->name(),
131 lit->materialized_literal_count(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000132 lit->contains_array_literal(),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 code);
134
135 CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(),
ager@chromium.org236ad962008-09-25 09:45:57 +0000136 RelocInfo::kNoPosition,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 lit->start_position(), lit->end_position(),
138 lit->is_expression(), true, script);
139
140 // Hint to the runtime system used when allocating space for initial
141 // property space by setting the expected number of properties for
142 // the instances of the function.
143 SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count());
144
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 // Notify debugger
146 Debugger::OnAfterCompile(script, fun);
147
148 return fun;
149}
150
151
152static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
153
154
155Handle<JSFunction> Compiler::Compile(Handle<String> source,
mads.s.agercbaa0602008-08-14 13:41:48 +0000156 Handle<Object> script_name,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 int line_offset, int column_offset,
158 v8::Extension* extension,
159 ScriptDataImpl* input_pre_data) {
160 Counters::total_load_size.Increment(source->length());
161 Counters::total_compile_size.Increment(source->length());
162
163 // The VM is in the COMPILER state until exiting this function.
164 VMState state(COMPILER);
165
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000166 // Do a lookup in the compilation cache but not for extensions.
167 Handle<JSFunction> result;
168 if (extension == NULL) {
169 result = CompilationCache::LookupScript(source,
170 script_name,
171 line_offset,
172 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 }
174
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000175 if (result.is_null()) {
176 // No cache entry found. Do pre-parsing and compile the script.
177 ScriptDataImpl* pre_data = input_pre_data;
178 if (pre_data == NULL && source->length() >= FLAG_min_preparse_length) {
179 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
180 buf->Reset(source.location());
181 pre_data = PreParse(buf.value(), extension);
182 }
183
184 // Create a script object describing the script to be compiled.
185 Handle<Script> script = Factory::NewScript(source);
186 if (!script_name.is_null()) {
187 script->set_name(*script_name);
188 script->set_line_offset(Smi::FromInt(line_offset));
189 script->set_column_offset(Smi::FromInt(column_offset));
190 }
191
192 // Compile the function and add it to the cache.
193 result = MakeFunction(true, false, script, extension, pre_data);
194 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000195 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000196 }
197
198 // Get rid of the pre-parsing data (if necessary).
199 if (input_pre_data == NULL && pre_data != NULL) {
200 delete pre_data;
201 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 }
203
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204 return result;
205}
206
207
ager@chromium.org236ad962008-09-25 09:45:57 +0000208Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
209 int line_offset,
210 bool is_global) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 Counters::total_eval_size.Increment(source->length());
212 Counters::total_compile_size.Increment(source->length());
213
214 // The VM is in the COMPILER state until exiting this function.
215 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000216 CompilationCache::Entry entry = is_global
217 ? CompilationCache::EVAL_GLOBAL
218 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000220 // Do a lookup in the compilation cache; if the entry is not there,
221 // invoke the compiler and add the result to the cache.
222 Handle<JSFunction> result = CompilationCache::LookupEval(source, entry);
223 if (result.is_null()) {
224 // Create a script object describing the script to be compiled.
225 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000226 script->set_line_offset(Smi::FromInt(line_offset));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000227 result = MakeFunction(is_global, true, script, NULL, NULL);
228 if (!result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000229 CompilationCache::PutFunction(source, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000230 }
231 }
232 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233}
234
235
236bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000237 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238
239 // The VM is in the COMPILER state until exiting this function.
240 VMState state(COMPILER);
241
242 // Make sure we have an initial stack limit.
243 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000244 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000245
246 // Compute name, source code and script data.
247 Handle<String> name(String::cast(shared->name()));
248 Handle<Script> script(Script::cast(shared->script()));
249
250 int start_position = shared->start_position();
251 int end_position = shared->end_position();
252 bool is_expression = shared->is_expression();
253 Counters::total_compile_size.Increment(end_position - start_position);
254
255 // Generate the AST for the lazily compiled function. The AST may be
256 // NULL in case of parser stack overflow.
257 FunctionLiteral* lit = MakeLazyAST(script, name,
258 start_position,
259 end_position,
260 is_expression);
261
kasper.lund212ac232008-07-16 07:07:30 +0000262 // Check for parse errors.
263 if (lit == NULL) {
264 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000265 return false;
266 }
267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 // Measure how long it takes to do the lazy compilation; only take
269 // the rest of the function into account to avoid overlap with the
270 // lazy parsing statistics.
271 StatsRateScope timer(&Counters::compile_lazy);
272
kasper.lund212ac232008-07-16 07:07:30 +0000273 // Compile the code.
274 Handle<Code> code = MakeCode(lit, script, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275
kasper.lund212ac232008-07-16 07:07:30 +0000276 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000278 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 return false;
280 }
281
282 // Generate the code, update the function info, and return the code.
283 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
284
285 // Update the shared function info with the compiled code.
286 shared->set_code(*code);
287
288 // Set the expected number of properties for instances.
289 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
290
291 // Check the function has compiled code.
292 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 return true;
294}
295
296
297} } // namespace v8::internal