ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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" |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 32 | #include "compilation-cache.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 33 | #include "compiler.h" |
| 34 | #include "debug.h" |
| 35 | #include "scopes.h" |
| 36 | #include "rewriter.h" |
| 37 | #include "usage-analyzer.h" |
| 38 | |
| 39 | namespace v8 { namespace internal { |
| 40 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | static 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 48 | // Signal a stack overflow by returning a null handle. The stack |
| 49 | // overflow exception will be thrown by the caller. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | 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.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 68 | // Optimize the AST. |
| 69 | Rewriter::Optimize(literal); |
| 70 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | // Generate code and return it. |
| 72 | Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 82 | ZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
| 84 | // Make sure we have an initial stack limit. |
| 85 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 86 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | |
| 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 97 | // Check for parse errors. |
| 98 | if (lit == NULL) { |
| 99 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 100 | return Handle<JSFunction>::null(); |
| 101 | } |
| 102 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | // 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 112 | Handle<Code> code = MakeCode(lit, script, is_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 113 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 114 | // Check for stack-overflow exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 115 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 116 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | 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.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 132 | lit->contains_array_literal(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | code); |
| 134 | |
| 135 | CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 136 | RelocInfo::kNoPosition, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 137 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | // Notify debugger |
| 146 | Debugger::OnAfterCompile(script, fun); |
| 147 | |
| 148 | return fun; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static StaticResource<SafeStringInputBuffer> safe_string_input_buffer; |
| 153 | |
| 154 | |
| 155 | Handle<JSFunction> Compiler::Compile(Handle<String> source, |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 156 | Handle<Object> script_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 157 | 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 166 | // 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | } |
| 174 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 175 | 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.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 195 | CompilationCache::PutFunction(source, CompilationCache::SCRIPT, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 196 | } |
| 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | } |
| 203 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | return result; |
| 205 | } |
| 206 | |
| 207 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 208 | Handle<JSFunction> Compiler::CompileEval(Handle<String> source, |
| 209 | int line_offset, |
| 210 | bool is_global) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 211 | 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.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 216 | CompilationCache::Entry entry = is_global |
| 217 | ? CompilationCache::EVAL_GLOBAL |
| 218 | : CompilationCache::EVAL_CONTEXTUAL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 220 | // 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.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 226 | script->set_line_offset(Smi::FromInt(line_offset)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 227 | result = MakeFunction(is_global, true, script, NULL, NULL); |
| 228 | if (!result.is_null()) { |
kasperl@chromium.org | 9fe21c6 | 2008-10-28 08:53:51 +0000 | [diff] [blame] | 229 | CompilationCache::PutFunction(source, entry, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | |
| 236 | bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 237 | ZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 238 | |
| 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.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 244 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
| 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 262 | // Check for parse errors. |
| 263 | if (lit == NULL) { |
| 264 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 265 | return false; |
| 266 | } |
| 267 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 268 | // 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 273 | // Compile the code. |
| 274 | Handle<Code> code = MakeCode(lit, script, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 275 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 276 | // Check for stack-overflow exception. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 277 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 278 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | } } // namespace v8::internal |