christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 Google Inc. 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 "compiler.h" |
| 33 | #include "debug.h" |
| 34 | #include "scopes.h" |
| 35 | #include "rewriter.h" |
| 36 | #include "usage-analyzer.h" |
| 37 | |
| 38 | namespace v8 { namespace internal { |
| 39 | |
| 40 | DEFINE_bool(strict, false, "strict error checking"); |
| 41 | DEFINE_int(min_preparse_length, 1024, |
| 42 | "Minimum length for automatic enable preparsing"); |
| 43 | DECLARE_bool(debug_info); |
| 44 | |
| 45 | #ifdef DEBUG |
| 46 | DEFINE_bool(print_builtin_scopes, false, "print scopes for builtins"); |
| 47 | DEFINE_bool(print_scopes, false, "print scopes"); |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | // Helper class to keep track of compilation nesting and to do proper |
| 52 | // cleanups of generated ASTs. |
| 53 | class CompilationTracker BASE_EMBEDDED { |
| 54 | public: |
| 55 | CompilationTracker() { |
| 56 | ++nesting_; |
| 57 | } |
| 58 | |
| 59 | ~CompilationTracker() { |
| 60 | // If we're leaving the top-level compilation, we must make sure |
| 61 | // to get rid of all generated ASTs. |
| 62 | if (--nesting_ == 0) Zone::DeleteAll(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | static int nesting_; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | int CompilationTracker::nesting_ = 0; |
| 71 | |
| 72 | |
| 73 | static Handle<Code> MakeCode(FunctionLiteral* literal, |
| 74 | Handle<Script> script, |
| 75 | bool is_eval) { |
| 76 | ASSERT(literal != NULL); |
| 77 | |
| 78 | // Rewrite the AST by introducing .result assignments where needed. |
| 79 | if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 80 | // Signal a stack overflow by returning a null handle. The stack |
| 81 | // overflow exception will be thrown by the caller. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | return Handle<Code>::null(); |
| 83 | } |
| 84 | |
| 85 | // Compute top scope and allocate variables. For lazy compilation |
| 86 | // the top scope only contains the single lazily compiled function, |
| 87 | // so this doesn't re-allocate variables repeatedly. |
| 88 | Scope* top = literal->scope(); |
| 89 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 90 | top->AllocateVariables(); |
| 91 | |
| 92 | #ifdef DEBUG |
| 93 | if (Bootstrapper::IsActive() ? |
| 94 | FLAG_print_builtin_scopes : |
| 95 | FLAG_print_scopes) { |
| 96 | literal->scope()->Print(); |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | // Generate code and return it. |
| 101 | Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval); |
| 102 | return result; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static Handle<JSFunction> MakeFunction(bool is_global, |
| 107 | bool is_eval, |
| 108 | Handle<Script> script, |
| 109 | v8::Extension* extension, |
| 110 | ScriptDataImpl* pre_data) { |
| 111 | CompilationTracker tracker; |
| 112 | |
| 113 | // Make sure we have an initial stack limit. |
| 114 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 115 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
| 117 | // Notify debugger |
| 118 | Debugger::OnBeforeCompile(script); |
| 119 | |
| 120 | // Only allow non-global compiles for eval. |
| 121 | ASSERT(is_eval || is_global); |
| 122 | |
| 123 | // Build AST. |
| 124 | FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data); |
| 125 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 126 | // Check for parse errors. |
| 127 | if (lit == NULL) { |
| 128 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 129 | return Handle<JSFunction>::null(); |
| 130 | } |
| 131 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | // Measure how long it takes to do the compilation; only take the |
| 133 | // rest of the function into account to avoid overlap with the |
| 134 | // parsing statistics. |
| 135 | StatsRate* rate = is_eval |
| 136 | ? &Counters::compile_eval |
| 137 | : &Counters::compile; |
| 138 | StatsRateScope timer(rate); |
| 139 | |
| 140 | // Compile the code. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 141 | Handle<Code> code = MakeCode(lit, script, is_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 143 | // Check for stack-overflow exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 145 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | return Handle<JSFunction>::null(); |
| 147 | } |
| 148 | |
| 149 | if (script->name()->IsString()) { |
| 150 | SmartPointer<char> data = |
| 151 | String::cast(script->name())->ToCString(DISALLOW_NULLS); |
| 152 | LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data)); |
| 153 | } else { |
| 154 | LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, "")); |
| 155 | } |
| 156 | |
| 157 | // Allocate function. |
| 158 | Handle<JSFunction> fun = |
| 159 | Factory::NewFunctionBoilerplate(lit->name(), |
| 160 | lit->materialized_literal_count(), |
| 161 | code); |
| 162 | |
| 163 | CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
| 164 | kNoPosition, |
| 165 | lit->start_position(), lit->end_position(), |
| 166 | lit->is_expression(), true, script); |
| 167 | |
| 168 | // Hint to the runtime system used when allocating space for initial |
| 169 | // property space by setting the expected number of properties for |
| 170 | // the instances of the function. |
| 171 | SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count()); |
| 172 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 173 | // Notify debugger |
| 174 | Debugger::OnAfterCompile(script, fun); |
| 175 | |
| 176 | return fun; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static StaticResource<SafeStringInputBuffer> safe_string_input_buffer; |
| 181 | |
| 182 | |
| 183 | Handle<JSFunction> Compiler::Compile(Handle<String> source, |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame^] | 184 | Handle<Object> script_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | int line_offset, int column_offset, |
| 186 | v8::Extension* extension, |
| 187 | ScriptDataImpl* input_pre_data) { |
| 188 | Counters::total_load_size.Increment(source->length()); |
| 189 | Counters::total_compile_size.Increment(source->length()); |
| 190 | |
| 191 | // The VM is in the COMPILER state until exiting this function. |
| 192 | VMState state(COMPILER); |
| 193 | |
| 194 | ScriptDataImpl* pre_data = input_pre_data; |
| 195 | if (pre_data == NULL && source->length() >= FLAG_min_preparse_length) { |
| 196 | Access<SafeStringInputBuffer> buf(&safe_string_input_buffer); |
| 197 | buf->Reset(source.location()); |
| 198 | pre_data = PreParse(buf.value(), extension); |
| 199 | } |
| 200 | |
| 201 | // Create a script object describing the script to be compiled. |
| 202 | Handle<Script> script = Factory::NewScript(source); |
| 203 | if (!script_name.is_null()) { |
| 204 | script->set_name(*script_name); |
| 205 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 206 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 207 | } |
| 208 | |
| 209 | Handle<JSFunction> result = |
| 210 | MakeFunction(true, false, script, extension, pre_data); |
| 211 | |
| 212 | if (input_pre_data == NULL && pre_data != NULL) |
| 213 | delete pre_data; |
| 214 | |
| 215 | return result; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | Handle<JSFunction> Compiler::CompileEval(bool is_global, |
| 220 | Handle<String> source) { |
| 221 | Counters::total_eval_size.Increment(source->length()); |
| 222 | Counters::total_compile_size.Increment(source->length()); |
| 223 | |
| 224 | // The VM is in the COMPILER state until exiting this function. |
| 225 | VMState state(COMPILER); |
| 226 | |
| 227 | // Create a script object describing the script to be compiled. |
| 228 | Handle<Script> script = Factory::NewScript(source); |
| 229 | return MakeFunction(is_global, true, script, NULL, NULL); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) { |
| 234 | CompilationTracker tracker; |
| 235 | |
| 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.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 241 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
| 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 259 | // Check for parse errors. |
| 260 | if (lit == NULL) { |
| 261 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 262 | return false; |
| 263 | } |
| 264 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | // 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.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 270 | // Compile the code. |
| 271 | Handle<Code> code = MakeCode(lit, script, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 272 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 273 | // Check for stack-overflow exception. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 275 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 276 | 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.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | } } // namespace v8::internal |