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 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | static Handle<Code> MakeCode(FunctionLiteral* literal, |
| 52 | Handle<Script> script, |
| 53 | bool is_eval) { |
| 54 | ASSERT(literal != NULL); |
| 55 | |
| 56 | // Rewrite the AST by introducing .result assignments where needed. |
| 57 | if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 58 | // Signal a stack overflow by returning a null handle. The stack |
| 59 | // overflow exception will be thrown by the caller. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 60 | return Handle<Code>::null(); |
| 61 | } |
| 62 | |
| 63 | // Compute top scope and allocate variables. For lazy compilation |
| 64 | // the top scope only contains the single lazily compiled function, |
| 65 | // so this doesn't re-allocate variables repeatedly. |
| 66 | Scope* top = literal->scope(); |
| 67 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 68 | top->AllocateVariables(); |
| 69 | |
| 70 | #ifdef DEBUG |
| 71 | if (Bootstrapper::IsActive() ? |
| 72 | FLAG_print_builtin_scopes : |
| 73 | FLAG_print_scopes) { |
| 74 | literal->scope()->Print(); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | // Generate code and return it. |
| 79 | Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval); |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static Handle<JSFunction> MakeFunction(bool is_global, |
| 85 | bool is_eval, |
| 86 | Handle<Script> script, |
| 87 | v8::Extension* extension, |
| 88 | ScriptDataImpl* pre_data) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 89 | ZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | |
| 91 | // Make sure we have an initial stack limit. |
| 92 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 93 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 104 | // Check for parse errors. |
| 105 | if (lit == NULL) { |
| 106 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 107 | return Handle<JSFunction>::null(); |
| 108 | } |
| 109 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | StatsRate* rate = is_eval |
| 114 | ? &Counters::compile_eval |
| 115 | : &Counters::compile; |
| 116 | StatsRateScope timer(rate); |
| 117 | |
| 118 | // Compile the code. |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 119 | Handle<Code> code = MakeCode(lit, script, is_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 121 | // Check for stack-overflow exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 123 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | return Handle<JSFunction>::null(); |
| 125 | } |
| 126 | |
| 127 | if (script->name()->IsString()) { |
| 128 | SmartPointer<char> data = |
| 129 | String::cast(script->name())->ToCString(DISALLOW_NULLS); |
| 130 | LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, *data)); |
| 131 | } else { |
| 132 | LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, "")); |
| 133 | } |
| 134 | |
| 135 | // Allocate function. |
| 136 | Handle<JSFunction> fun = |
| 137 | Factory::NewFunctionBoilerplate(lit->name(), |
| 138 | lit->materialized_literal_count(), |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 139 | lit->contains_array_literal(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 140 | code); |
| 141 | |
| 142 | CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
| 143 | kNoPosition, |
| 144 | lit->start_position(), lit->end_position(), |
| 145 | lit->is_expression(), true, script); |
| 146 | |
| 147 | // Hint to the runtime system used when allocating space for initial |
| 148 | // property space by setting the expected number of properties for |
| 149 | // the instances of the function. |
| 150 | SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count()); |
| 151 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | // Notify debugger |
| 153 | Debugger::OnAfterCompile(script, fun); |
| 154 | |
| 155 | return fun; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static StaticResource<SafeStringInputBuffer> safe_string_input_buffer; |
| 160 | |
| 161 | |
| 162 | Handle<JSFunction> Compiler::Compile(Handle<String> source, |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 163 | Handle<Object> script_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | int line_offset, int column_offset, |
| 165 | v8::Extension* extension, |
| 166 | ScriptDataImpl* input_pre_data) { |
| 167 | Counters::total_load_size.Increment(source->length()); |
| 168 | Counters::total_compile_size.Increment(source->length()); |
| 169 | |
| 170 | // The VM is in the COMPILER state until exiting this function. |
| 171 | VMState state(COMPILER); |
| 172 | |
| 173 | ScriptDataImpl* pre_data = input_pre_data; |
| 174 | if (pre_data == NULL && source->length() >= FLAG_min_preparse_length) { |
| 175 | Access<SafeStringInputBuffer> buf(&safe_string_input_buffer); |
| 176 | buf->Reset(source.location()); |
| 177 | pre_data = PreParse(buf.value(), extension); |
| 178 | } |
| 179 | |
| 180 | // Create a script object describing the script to be compiled. |
| 181 | Handle<Script> script = Factory::NewScript(source); |
| 182 | if (!script_name.is_null()) { |
| 183 | script->set_name(*script_name); |
| 184 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 185 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 186 | } |
| 187 | |
| 188 | Handle<JSFunction> result = |
| 189 | MakeFunction(true, false, script, extension, pre_data); |
| 190 | |
| 191 | if (input_pre_data == NULL && pre_data != NULL) |
| 192 | delete pre_data; |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | Handle<JSFunction> Compiler::CompileEval(bool is_global, |
| 199 | Handle<String> source) { |
| 200 | Counters::total_eval_size.Increment(source->length()); |
| 201 | Counters::total_compile_size.Increment(source->length()); |
| 202 | |
| 203 | // The VM is in the COMPILER state until exiting this function. |
| 204 | VMState state(COMPILER); |
| 205 | |
| 206 | // Create a script object describing the script to be compiled. |
| 207 | Handle<Script> script = Factory::NewScript(source); |
| 208 | return MakeFunction(is_global, true, script, NULL, NULL); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 213 | ZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | |
| 215 | // The VM is in the COMPILER state until exiting this function. |
| 216 | VMState state(COMPILER); |
| 217 | |
| 218 | // Make sure we have an initial stack limit. |
| 219 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 220 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 221 | |
| 222 | // Compute name, source code and script data. |
| 223 | Handle<String> name(String::cast(shared->name())); |
| 224 | Handle<Script> script(Script::cast(shared->script())); |
| 225 | |
| 226 | int start_position = shared->start_position(); |
| 227 | int end_position = shared->end_position(); |
| 228 | bool is_expression = shared->is_expression(); |
| 229 | Counters::total_compile_size.Increment(end_position - start_position); |
| 230 | |
| 231 | // Generate the AST for the lazily compiled function. The AST may be |
| 232 | // NULL in case of parser stack overflow. |
| 233 | FunctionLiteral* lit = MakeLazyAST(script, name, |
| 234 | start_position, |
| 235 | end_position, |
| 236 | is_expression); |
| 237 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 238 | // Check for parse errors. |
| 239 | if (lit == NULL) { |
| 240 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | // Measure how long it takes to do the lazy compilation; only take |
| 245 | // the rest of the function into account to avoid overlap with the |
| 246 | // lazy parsing statistics. |
| 247 | StatsRateScope timer(&Counters::compile_lazy); |
| 248 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 249 | // Compile the code. |
| 250 | Handle<Code> code = MakeCode(lit, script, false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 251 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 252 | // Check for stack-overflow exception. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 253 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 254 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | // Generate the code, update the function info, and return the code. |
| 259 | LOG(CodeCreateEvent("LazyCompile", *code, *lit->name())); |
| 260 | |
| 261 | // Update the shared function info with the compiled code. |
| 262 | shared->set_code(*code); |
| 263 | |
| 264 | // Set the expected number of properties for instances. |
| 265 | SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count()); |
| 266 | |
| 267 | // Check the function has compiled code. |
| 268 | ASSERT(shared->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | } } // namespace v8::internal |