The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / compiler.cc @ 40c0f755
History | View | Annotate | Download (13 KB)
1 |
// Copyright 2009 the V8 project authors. All rights reserved.
|
---|---|
2 |
// 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" |
32 |
#include "compilation-cache.h" |
33 |
#include "compiler.h" |
34 |
#include "debug.h" |
35 |
#include "oprofile-agent.h" |
36 |
#include "rewriter.h" |
37 |
#include "scopes.h" |
38 |
#include "usage-analyzer.h" |
39 |
|
40 |
namespace v8 { namespace internal { |
41 |
|
42 |
static Handle<Code> MakeCode(FunctionLiteral* literal,
|
43 |
Handle<Script> script, |
44 |
Handle<Context> context, |
45 |
bool is_eval) {
|
46 |
ASSERT(literal != NULL);
|
47 |
|
48 |
// Rewrite the AST by introducing .result assignments where needed.
|
49 |
if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) {
|
50 |
// Signal a stack overflow by returning a null handle. The stack
|
51 |
// overflow exception will be thrown by the caller.
|
52 |
return Handle<Code>::null();
|
53 |
} |
54 |
|
55 |
// Compute top scope and allocate variables. For lazy compilation
|
56 |
// the top scope only contains the single lazily compiled function,
|
57 |
// so this doesn't re-allocate variables repeatedly.
|
58 |
Scope* top = literal->scope(); |
59 |
while (top->outer_scope() != NULL) top = top->outer_scope(); |
60 |
top->AllocateVariables(context); |
61 |
|
62 |
#ifdef DEBUG
|
63 |
if (Bootstrapper::IsActive() ?
|
64 |
FLAG_print_builtin_scopes : |
65 |
FLAG_print_scopes) { |
66 |
literal->scope()->Print(); |
67 |
} |
68 |
#endif
|
69 |
|
70 |
// Optimize the AST.
|
71 |
if (!Rewriter::Optimize(literal)) {
|
72 |
// Signal a stack overflow by returning a null handle. The stack
|
73 |
// overflow exception will be thrown by the caller.
|
74 |
return Handle<Code>::null();
|
75 |
} |
76 |
|
77 |
// Generate code and return it.
|
78 |
Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval); |
79 |
return result;
|
80 |
} |
81 |
|
82 |
|
83 |
static Handle<JSFunction> MakeFunction(bool is_global, |
84 |
bool is_eval,
|
85 |
Handle<Script> script, |
86 |
Handle<Context> context, |
87 |
v8::Extension* extension, |
88 |
ScriptDataImpl* pre_data) { |
89 |
ZoneScope zone_scope(DELETE_ON_EXIT); |
90 |
|
91 |
// Make sure we have an initial stack limit.
|
92 |
StackGuard guard; |
93 |
PostponeInterruptsScope postpone; |
94 |
|
95 |
// Notify debugger
|
96 |
Debugger::OnBeforeCompile(script); |
97 |
|
98 |
// Only allow non-global compiles for eval.
|
99 |
ASSERT(is_eval || is_global); |
100 |
|
101 |
// Build AST.
|
102 |
FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data); |
103 |
|
104 |
// Check for parse errors.
|
105 |
if (lit == NULL) { |
106 |
ASSERT(Top::has_pending_exception()); |
107 |
return Handle<JSFunction>::null();
|
108 |
} |
109 |
|
110 |
// Measure how long it takes to do the compilation; only take the
|
111 |
// rest of the function into account to avoid overlap with the
|
112 |
// parsing statistics.
|
113 |
HistogramTimer* rate = is_eval |
114 |
? &Counters::compile_eval |
115 |
: &Counters::compile; |
116 |
HistogramTimerScope timer(rate); |
117 |
|
118 |
// Compile the code.
|
119 |
Handle<Code> code = MakeCode(lit, script, context, is_eval); |
120 |
|
121 |
// Check for stack-overflow exceptions.
|
122 |
if (code.is_null()) {
|
123 |
Top::StackOverflow(); |
124 |
return Handle<JSFunction>::null();
|
125 |
} |
126 |
|
127 |
#if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT
|
128 |
// Log the code generation for the script. Check explicit whether logging is
|
129 |
// to avoid allocating when not required.
|
130 |
if (Logger::is_enabled() || OProfileAgent::is_enabled()) {
|
131 |
if (script->name()->IsString()) {
|
132 |
SmartPointer<char> data =
|
133 |
String::cast(script->name())->ToCString(DISALLOW_NULLS); |
134 |
LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data)); |
135 |
OProfileAgent::CreateNativeCodeRegion(*data, code->address(), |
136 |
code->ExecutableSize()); |
137 |
} else {
|
138 |
LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, "")); |
139 |
OProfileAgent::CreateNativeCodeRegion(is_eval ? "Eval" : "Script", |
140 |
code->address(), code->ExecutableSize()); |
141 |
} |
142 |
} |
143 |
#endif
|
144 |
|
145 |
// Allocate function.
|
146 |
Handle<JSFunction> fun = |
147 |
Factory::NewFunctionBoilerplate(lit->name(), |
148 |
lit->materialized_literal_count(), |
149 |
lit->contains_array_literal(), |
150 |
code); |
151 |
|
152 |
CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
153 |
RelocInfo::kNoPosition, |
154 |
lit->start_position(), lit->end_position(), |
155 |
lit->is_expression(), true, script,
|
156 |
lit->inferred_name()); |
157 |
|
158 |
// Hint to the runtime system used when allocating space for initial
|
159 |
// property space by setting the expected number of properties for
|
160 |
// the instances of the function.
|
161 |
SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count()); |
162 |
|
163 |
// Notify debugger
|
164 |
Debugger::OnAfterCompile(script, fun); |
165 |
|
166 |
return fun;
|
167 |
} |
168 |
|
169 |
|
170 |
static StaticResource<SafeStringInputBuffer> safe_string_input_buffer;
|
171 |
|
172 |
|
173 |
Handle<JSFunction> Compiler::Compile(Handle<String> source, |
174 |
Handle<Object> script_name, |
175 |
int line_offset, int column_offset, |
176 |
v8::Extension* extension, |
177 |
ScriptDataImpl* input_pre_data) { |
178 |
int source_length = source->length();
|
179 |
Counters::total_load_size.Increment(source_length); |
180 |
Counters::total_compile_size.Increment(source_length); |
181 |
|
182 |
// The VM is in the COMPILER state until exiting this function.
|
183 |
VMState state(COMPILER); |
184 |
|
185 |
// Do a lookup in the compilation cache but not for extensions.
|
186 |
Handle<JSFunction> result; |
187 |
if (extension == NULL) { |
188 |
result = CompilationCache::LookupScript(source, |
189 |
script_name, |
190 |
line_offset, |
191 |
column_offset); |
192 |
} |
193 |
|
194 |
if (result.is_null()) {
|
195 |
// No cache entry found. Do pre-parsing and compile the script.
|
196 |
ScriptDataImpl* pre_data = input_pre_data; |
197 |
if (pre_data == NULL && source_length >= FLAG_min_preparse_length) { |
198 |
Access<SafeStringInputBuffer> buf(&safe_string_input_buffer); |
199 |
buf->Reset(source.location()); |
200 |
pre_data = PreParse(buf.value(), extension); |
201 |
} |
202 |
|
203 |
// Create a script object describing the script to be compiled.
|
204 |
Handle<Script> script = Factory::NewScript(source); |
205 |
if (!script_name.is_null()) {
|
206 |
script->set_name(*script_name); |
207 |
script->set_line_offset(Smi::FromInt(line_offset)); |
208 |
script->set_column_offset(Smi::FromInt(column_offset)); |
209 |
} |
210 |
|
211 |
// Compile the function and add it to the cache.
|
212 |
result = MakeFunction(true,
|
213 |
false,
|
214 |
script, |
215 |
Handle<Context>::null(), |
216 |
extension, |
217 |
pre_data); |
218 |
if (extension == NULL && !result.is_null()) { |
219 |
CompilationCache::PutScript(source, result); |
220 |
} |
221 |
|
222 |
// Get rid of the pre-parsing data (if necessary).
|
223 |
if (input_pre_data == NULL && pre_data != NULL) { |
224 |
delete pre_data;
|
225 |
} |
226 |
} |
227 |
|
228 |
if (result.is_null()) Top::ReportPendingMessages();
|
229 |
return result;
|
230 |
} |
231 |
|
232 |
|
233 |
Handle<JSFunction> Compiler::CompileEval(Handle<String> source, |
234 |
Handle<Context> context, |
235 |
int line_offset,
|
236 |
bool is_global) {
|
237 |
int source_length = source->length();
|
238 |
Counters::total_eval_size.Increment(source_length); |
239 |
Counters::total_compile_size.Increment(source_length); |
240 |
|
241 |
// The VM is in the COMPILER state until exiting this function.
|
242 |
VMState state(COMPILER); |
243 |
CompilationCache::Entry entry = is_global |
244 |
? CompilationCache::EVAL_GLOBAL |
245 |
: CompilationCache::EVAL_CONTEXTUAL; |
246 |
|
247 |
// Do a lookup in the compilation cache; if the entry is not there,
|
248 |
// invoke the compiler and add the result to the cache.
|
249 |
Handle<JSFunction> result = |
250 |
CompilationCache::LookupEval(source, context, entry); |
251 |
if (result.is_null()) {
|
252 |
// Create a script object describing the script to be compiled.
|
253 |
Handle<Script> script = Factory::NewScript(source); |
254 |
script->set_line_offset(Smi::FromInt(line_offset)); |
255 |
result = MakeFunction(is_global, true, script, context, NULL, NULL); |
256 |
if (!result.is_null()) {
|
257 |
CompilationCache::PutEval(source, context, entry, result); |
258 |
} |
259 |
} |
260 |
|
261 |
return result;
|
262 |
} |
263 |
|
264 |
|
265 |
bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared,
|
266 |
int loop_nesting) {
|
267 |
ZoneScope zone_scope(DELETE_ON_EXIT); |
268 |
|
269 |
// The VM is in the COMPILER state until exiting this function.
|
270 |
VMState state(COMPILER); |
271 |
|
272 |
// Make sure we have an initial stack limit.
|
273 |
StackGuard guard; |
274 |
PostponeInterruptsScope postpone; |
275 |
|
276 |
// Compute name, source code and script data.
|
277 |
Handle<String> name(String::cast(shared->name())); |
278 |
Handle<Script> script(Script::cast(shared->script())); |
279 |
|
280 |
int start_position = shared->start_position();
|
281 |
int end_position = shared->end_position();
|
282 |
bool is_expression = shared->is_expression();
|
283 |
Counters::total_compile_size.Increment(end_position - start_position); |
284 |
|
285 |
// Generate the AST for the lazily compiled function. The AST may be
|
286 |
// NULL in case of parser stack overflow.
|
287 |
FunctionLiteral* lit = MakeLazyAST(script, name, |
288 |
start_position, |
289 |
end_position, |
290 |
is_expression); |
291 |
|
292 |
// Check for parse errors.
|
293 |
if (lit == NULL) { |
294 |
ASSERT(Top::has_pending_exception()); |
295 |
return false; |
296 |
} |
297 |
|
298 |
// Update the loop nesting in the function literal.
|
299 |
lit->set_loop_nesting(loop_nesting); |
300 |
|
301 |
// Measure how long it takes to do the lazy compilation; only take
|
302 |
// the rest of the function into account to avoid overlap with the
|
303 |
// lazy parsing statistics.
|
304 |
HistogramTimerScope timer(&Counters::compile_lazy); |
305 |
|
306 |
// Compile the code.
|
307 |
Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false);
|
308 |
|
309 |
// Check for stack-overflow exception.
|
310 |
if (code.is_null()) {
|
311 |
Top::StackOverflow(); |
312 |
return false; |
313 |
} |
314 |
|
315 |
#if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT
|
316 |
// Log the code generation. If source information is available include script
|
317 |
// name and line number. Check explicit whether logging is enabled as finding
|
318 |
// the line number is not for free.
|
319 |
if (Logger::is_enabled() || OProfileAgent::is_enabled()) {
|
320 |
Handle<String> func_name(lit->name()->length() > 0 ?
|
321 |
*lit->name() : shared->inferred_name()); |
322 |
if (script->name()->IsString()) {
|
323 |
int line_num = GetScriptLineNumber(script, start_position);
|
324 |
if (line_num > 0) { |
325 |
line_num += script->line_offset()->value() + 1;
|
326 |
} |
327 |
LOG(CodeCreateEvent("LazyCompile", *code, *func_name,
|
328 |
String::cast(script->name()), line_num)); |
329 |
OProfileAgent::CreateNativeCodeRegion(*func_name, |
330 |
String::cast(script->name()), |
331 |
line_num, code->address(), |
332 |
code->ExecutableSize()); |
333 |
} else {
|
334 |
LOG(CodeCreateEvent("LazyCompile", *code, *func_name));
|
335 |
OProfileAgent::CreateNativeCodeRegion(*func_name, code->address(), |
336 |
code->ExecutableSize()); |
337 |
} |
338 |
} |
339 |
#endif
|
340 |
|
341 |
// Update the shared function info with the compiled code.
|
342 |
shared->set_code(*code); |
343 |
|
344 |
// Set the expected number of properties for instances.
|
345 |
SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count()); |
346 |
|
347 |
// Check the function has compiled code.
|
348 |
ASSERT(shared->is_compiled()); |
349 |
return true; |
350 |
} |
351 |
|
352 |
|
353 |
} } // namespace v8::internal
|