blob: 2a2bb87269b75e29a152eb410c42fb0ecffc166d [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) {
ager@chromium.org870a0b62008-11-04 11:43:05 +0000160 int source_length = source->length();
161 Counters::total_load_size.Increment(source_length);
162 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163
164 // The VM is in the COMPILER state until exiting this function.
165 VMState state(COMPILER);
166
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000167 // Do a lookup in the compilation cache but not for extensions.
168 Handle<JSFunction> result;
169 if (extension == NULL) {
170 result = CompilationCache::LookupScript(source,
171 script_name,
172 line_offset,
173 column_offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 }
175
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000176 if (result.is_null()) {
177 // No cache entry found. Do pre-parsing and compile the script.
178 ScriptDataImpl* pre_data = input_pre_data;
ager@chromium.org870a0b62008-11-04 11:43:05 +0000179 if (pre_data == NULL && source_length >= FLAG_min_preparse_length) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000180 Access<SafeStringInputBuffer> buf(&safe_string_input_buffer);
181 buf->Reset(source.location());
182 pre_data = PreParse(buf.value(), extension);
183 }
184
185 // Create a script object describing the script to be compiled.
186 Handle<Script> script = Factory::NewScript(source);
187 if (!script_name.is_null()) {
188 script->set_name(*script_name);
189 script->set_line_offset(Smi::FromInt(line_offset));
190 script->set_column_offset(Smi::FromInt(column_offset));
191 }
192
193 // Compile the function and add it to the cache.
194 result = MakeFunction(true, false, script, extension, pre_data);
195 if (extension == NULL && !result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000196 CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000197 }
198
199 // Get rid of the pre-parsing data (if necessary).
200 if (input_pre_data == NULL && pre_data != NULL) {
201 delete pre_data;
202 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 }
204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 return result;
206}
207
208
ager@chromium.org236ad962008-09-25 09:45:57 +0000209Handle<JSFunction> Compiler::CompileEval(Handle<String> source,
210 int line_offset,
211 bool is_global) {
ager@chromium.orgc3e50d82008-11-05 11:53:10 +0000212 int source_length = source->length();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000213 Counters::total_eval_size.Increment(source_length);
214 Counters::total_compile_size.Increment(source_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215
216 // The VM is in the COMPILER state until exiting this function.
217 VMState state(COMPILER);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000218 CompilationCache::Entry entry = is_global
219 ? CompilationCache::EVAL_GLOBAL
220 : CompilationCache::EVAL_CONTEXTUAL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000222 // Do a lookup in the compilation cache; if the entry is not there,
223 // invoke the compiler and add the result to the cache.
224 Handle<JSFunction> result = CompilationCache::LookupEval(source, entry);
225 if (result.is_null()) {
226 // Create a script object describing the script to be compiled.
227 Handle<Script> script = Factory::NewScript(source);
ager@chromium.org236ad962008-09-25 09:45:57 +0000228 script->set_line_offset(Smi::FromInt(line_offset));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000229 result = MakeFunction(is_global, true, script, NULL, NULL);
230 if (!result.is_null()) {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000231 CompilationCache::PutFunction(source, entry, result);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000232 }
233 }
234 return result;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000235}
236
237
238bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000239 ZoneScope zone_scope(DELETE_ON_EXIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240
241 // The VM is in the COMPILER state until exiting this function.
242 VMState state(COMPILER);
243
244 // Make sure we have an initial stack limit.
245 StackGuard guard;
kasper.lund44510672008-07-25 07:37:58 +0000246 PostponeInterruptsScope postpone;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247
248 // Compute name, source code and script data.
249 Handle<String> name(String::cast(shared->name()));
250 Handle<Script> script(Script::cast(shared->script()));
251
252 int start_position = shared->start_position();
253 int end_position = shared->end_position();
254 bool is_expression = shared->is_expression();
255 Counters::total_compile_size.Increment(end_position - start_position);
256
257 // Generate the AST for the lazily compiled function. The AST may be
258 // NULL in case of parser stack overflow.
259 FunctionLiteral* lit = MakeLazyAST(script, name,
260 start_position,
261 end_position,
262 is_expression);
263
kasper.lund212ac232008-07-16 07:07:30 +0000264 // Check for parse errors.
265 if (lit == NULL) {
266 ASSERT(Top::has_pending_exception());
kasper.lund212ac232008-07-16 07:07:30 +0000267 return false;
268 }
269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000270 // Measure how long it takes to do the lazy compilation; only take
271 // the rest of the function into account to avoid overlap with the
272 // lazy parsing statistics.
273 StatsRateScope timer(&Counters::compile_lazy);
274
kasper.lund212ac232008-07-16 07:07:30 +0000275 // Compile the code.
276 Handle<Code> code = MakeCode(lit, script, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277
kasper.lund212ac232008-07-16 07:07:30 +0000278 // Check for stack-overflow exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 if (code.is_null()) {
kasper.lund212ac232008-07-16 07:07:30 +0000280 Top::StackOverflow();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 return false;
282 }
283
284 // Generate the code, update the function info, and return the code.
285 LOG(CodeCreateEvent("LazyCompile", *code, *lit->name()));
286
287 // Update the shared function info with the compiled code.
288 shared->set_code(*code);
289
290 // Set the expected number of properties for instances.
291 SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count());
292
293 // Check the function has compiled code.
294 ASSERT(shared->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295 return true;
296}
297
298
299} } // namespace v8::internal