ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 1 | // Copyright 2009 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" |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 35 | #include "oprofile-agent.h" |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 36 | #include "rewriter.h" |
| 37 | #include "scopes.h" |
| 38 | #include "usage-analyzer.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 43 | static Handle<Code> MakeCode(FunctionLiteral* literal, |
| 44 | Handle<Script> script, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 45 | Handle<Context> context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | bool is_eval) { |
| 47 | ASSERT(literal != NULL); |
| 48 | |
| 49 | // Rewrite the AST by introducing .result assignments where needed. |
| 50 | if (!Rewriter::Process(literal) || !AnalyzeVariableUsage(literal)) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 51 | // Signal a stack overflow by returning a null handle. The stack |
| 52 | // overflow exception will be thrown by the caller. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | return Handle<Code>::null(); |
| 54 | } |
| 55 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 56 | { |
| 57 | // Compute top scope and allocate variables. For lazy compilation |
| 58 | // the top scope only contains the single lazily compiled function, |
| 59 | // so this doesn't re-allocate variables repeatedly. |
| 60 | HistogramTimerScope timer(&Counters::variable_allocation); |
| 61 | Scope* top = literal->scope(); |
| 62 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 63 | top->AllocateVariables(context); |
| 64 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | |
| 66 | #ifdef DEBUG |
| 67 | if (Bootstrapper::IsActive() ? |
| 68 | FLAG_print_builtin_scopes : |
| 69 | FLAG_print_scopes) { |
| 70 | literal->scope()->Print(); |
| 71 | } |
| 72 | #endif |
| 73 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 74 | // Optimize the AST. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 75 | if (!Rewriter::Optimize(literal)) { |
| 76 | // Signal a stack overflow by returning a null handle. The stack |
| 77 | // overflow exception will be thrown by the caller. |
| 78 | return Handle<Code>::null(); |
| 79 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 80 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 81 | // Generate code and return it. |
| 82 | Handle<Code> result = CodeGenerator::MakeCode(literal, script, is_eval); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 87 | static bool IsValidJSON(FunctionLiteral* lit) { |
| 88 | if (!lit->body()->length() == 1) |
| 89 | return false; |
| 90 | Statement* stmt = lit->body()->at(0); |
| 91 | if (stmt->AsExpressionStatement() == NULL) |
| 92 | return false; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 93 | Expression* expr = stmt->AsExpressionStatement()->expression(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 94 | return expr->IsValidJSON(); |
| 95 | } |
| 96 | |
| 97 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | static Handle<JSFunction> MakeFunction(bool is_global, |
| 99 | bool is_eval, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 100 | bool is_json, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 101 | Handle<Script> script, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 102 | Handle<Context> context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 103 | v8::Extension* extension, |
| 104 | ScriptDataImpl* pre_data) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 105 | CompilationZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 106 | |
| 107 | // Make sure we have an initial stack limit. |
| 108 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 109 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 111 | ASSERT(!i::Top::global_context().is_null()); |
| 112 | script->set_context_data((*i::Top::global_context())->data()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 113 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 114 | #ifdef ENABLE_DEBUGGER_SUPPORT |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 115 | if (is_eval || is_json) { |
| 116 | script->set_compilation_type( |
| 117 | is_json ? Smi::FromInt(Script::COMPILATION_TYPE_JSON) : |
| 118 | Smi::FromInt(Script::COMPILATION_TYPE_EVAL)); |
| 119 | // For eval scripts add information on the function from which eval was |
| 120 | // called. |
| 121 | if (is_eval) { |
| 122 | JavaScriptFrameIterator it; |
| 123 | script->set_eval_from_function(it.frame()->function()); |
| 124 | int offset = it.frame()->pc() - it.frame()->code()->instruction_start(); |
| 125 | script->set_eval_from_instructions_offset(Smi::FromInt(offset)); |
| 126 | } |
| 127 | } |
| 128 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 129 | // Notify debugger |
| 130 | Debugger::OnBeforeCompile(script); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 131 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 132 | |
| 133 | // Only allow non-global compiles for eval. |
| 134 | ASSERT(is_eval || is_global); |
| 135 | |
| 136 | // Build AST. |
| 137 | FunctionLiteral* lit = MakeAST(is_global, script, extension, pre_data); |
| 138 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 139 | // Check for parse errors. |
| 140 | if (lit == NULL) { |
| 141 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 142 | return Handle<JSFunction>::null(); |
| 143 | } |
| 144 | |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 145 | // When parsing JSON we do an ordinary parse and then afterwards |
| 146 | // check the AST to ensure it was well-formed. If not we give a |
| 147 | // syntax error. |
| 148 | if (is_json && !IsValidJSON(lit)) { |
| 149 | HandleScope scope; |
| 150 | Handle<JSArray> args = Factory::NewJSArray(1); |
| 151 | Handle<Object> source(script->source()); |
| 152 | SetElement(args, 0, source); |
| 153 | Handle<Object> result = Factory::NewSyntaxError("invalid_json", args); |
| 154 | Top::Throw(*result, NULL); |
| 155 | return Handle<JSFunction>::null(); |
| 156 | } |
| 157 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 158 | // Measure how long it takes to do the compilation; only take the |
| 159 | // rest of the function into account to avoid overlap with the |
| 160 | // parsing statistics. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 161 | HistogramTimer* rate = is_eval |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | ? &Counters::compile_eval |
| 163 | : &Counters::compile; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 164 | HistogramTimerScope timer(rate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | |
| 166 | // Compile the code. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 167 | Handle<Code> code = MakeCode(lit, script, context, is_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 168 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 169 | // Check for stack-overflow exceptions. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 170 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 171 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 172 | return Handle<JSFunction>::null(); |
| 173 | } |
| 174 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 175 | #if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 176 | // Log the code generation for the script. Check explicit whether logging is |
| 177 | // to avoid allocating when not required. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 178 | if (Logger::IsEnabled() || OProfileAgent::is_enabled()) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 179 | if (script->name()->IsString()) { |
| 180 | SmartPointer<char> data = |
| 181 | String::cast(script->name())->ToCString(DISALLOW_NULLS); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 182 | LOG(CodeCreateEvent(is_eval ? Logger::EVAL_TAG : Logger::SCRIPT_TAG, |
| 183 | *code, *data)); |
| 184 | OProfileAgent::CreateNativeCodeRegion(*data, |
| 185 | code->instruction_start(), |
| 186 | code->instruction_size()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 187 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 188 | LOG(CodeCreateEvent(is_eval ? Logger::EVAL_TAG : Logger::SCRIPT_TAG, |
| 189 | *code, "")); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 190 | OProfileAgent::CreateNativeCodeRegion(is_eval ? "Eval" : "Script", |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 191 | code->instruction_start(), |
| 192 | code->instruction_size()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 193 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 194 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 195 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | |
| 197 | // Allocate function. |
| 198 | Handle<JSFunction> fun = |
| 199 | Factory::NewFunctionBoilerplate(lit->name(), |
| 200 | lit->materialized_literal_count(), |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 201 | lit->contains_array_literal(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | code); |
| 203 | |
| 204 | CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 205 | RelocInfo::kNoPosition, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 206 | lit->start_position(), lit->end_position(), |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 207 | lit->is_expression(), true, script, |
| 208 | lit->inferred_name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | |
| 210 | // Hint to the runtime system used when allocating space for initial |
| 211 | // property space by setting the expected number of properties for |
| 212 | // the instances of the function. |
| 213 | SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count()); |
| 214 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 215 | #ifdef ENABLE_DEBUGGER_SUPPORT |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | // Notify debugger |
| 217 | Debugger::OnAfterCompile(script, fun); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 218 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 219 | |
| 220 | return fun; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | static StaticResource<SafeStringInputBuffer> safe_string_input_buffer; |
| 225 | |
| 226 | |
| 227 | Handle<JSFunction> Compiler::Compile(Handle<String> source, |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 228 | Handle<Object> script_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 229 | int line_offset, int column_offset, |
| 230 | v8::Extension* extension, |
| 231 | ScriptDataImpl* input_pre_data) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 232 | int source_length = source->length(); |
| 233 | Counters::total_load_size.Increment(source_length); |
| 234 | Counters::total_compile_size.Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | |
| 236 | // The VM is in the COMPILER state until exiting this function. |
| 237 | VMState state(COMPILER); |
| 238 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 239 | // Do a lookup in the compilation cache but not for extensions. |
| 240 | Handle<JSFunction> result; |
| 241 | if (extension == NULL) { |
| 242 | result = CompilationCache::LookupScript(source, |
| 243 | script_name, |
| 244 | line_offset, |
| 245 | column_offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 246 | } |
| 247 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 248 | if (result.is_null()) { |
| 249 | // No cache entry found. Do pre-parsing and compile the script. |
| 250 | ScriptDataImpl* pre_data = input_pre_data; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 251 | if (pre_data == NULL && source_length >= FLAG_min_preparse_length) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 252 | Access<SafeStringInputBuffer> buf(&safe_string_input_buffer); |
| 253 | buf->Reset(source.location()); |
| 254 | pre_data = PreParse(buf.value(), extension); |
| 255 | } |
| 256 | |
| 257 | // Create a script object describing the script to be compiled. |
| 258 | Handle<Script> script = Factory::NewScript(source); |
| 259 | if (!script_name.is_null()) { |
| 260 | script->set_name(*script_name); |
| 261 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 262 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 263 | } |
| 264 | |
| 265 | // Compile the function and add it to the cache. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 266 | result = MakeFunction(true, |
| 267 | false, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 268 | false, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 269 | script, |
| 270 | Handle<Context>::null(), |
| 271 | extension, |
| 272 | pre_data); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 273 | if (extension == NULL && !result.is_null()) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 274 | CompilationCache::PutScript(source, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | // Get rid of the pre-parsing data (if necessary). |
| 278 | if (input_pre_data == NULL && pre_data != NULL) { |
| 279 | delete pre_data; |
| 280 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 281 | } |
| 282 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 283 | if (result.is_null()) Top::ReportPendingMessages(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 284 | return result; |
| 285 | } |
| 286 | |
| 287 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 288 | Handle<JSFunction> Compiler::CompileEval(Handle<String> source, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 289 | Handle<Context> context, |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 290 | bool is_global, |
| 291 | bool is_json) { |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 292 | int source_length = source->length(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 293 | Counters::total_eval_size.Increment(source_length); |
| 294 | Counters::total_compile_size.Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | |
| 296 | // The VM is in the COMPILER state until exiting this function. |
| 297 | VMState state(COMPILER); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 298 | CompilationCache::Entry entry = is_global |
| 299 | ? CompilationCache::EVAL_GLOBAL |
| 300 | : CompilationCache::EVAL_CONTEXTUAL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 301 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 302 | // Do a lookup in the compilation cache; if the entry is not there, |
| 303 | // invoke the compiler and add the result to the cache. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 304 | Handle<JSFunction> result = |
| 305 | CompilationCache::LookupEval(source, context, entry); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 306 | if (result.is_null()) { |
| 307 | // Create a script object describing the script to be compiled. |
| 308 | Handle<Script> script = Factory::NewScript(source); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 309 | result = MakeFunction(is_global, |
| 310 | true, |
| 311 | is_json, |
| 312 | script, |
| 313 | context, |
| 314 | NULL, |
| 315 | NULL); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 316 | if (!result.is_null()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 317 | CompilationCache::PutEval(source, context, entry, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 320 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 321 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 325 | bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared, |
| 326 | int loop_nesting) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 327 | CompilationZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 328 | |
| 329 | // The VM is in the COMPILER state until exiting this function. |
| 330 | VMState state(COMPILER); |
| 331 | |
| 332 | // Make sure we have an initial stack limit. |
| 333 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 334 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | |
| 336 | // Compute name, source code and script data. |
| 337 | Handle<String> name(String::cast(shared->name())); |
| 338 | Handle<Script> script(Script::cast(shared->script())); |
| 339 | |
| 340 | int start_position = shared->start_position(); |
| 341 | int end_position = shared->end_position(); |
| 342 | bool is_expression = shared->is_expression(); |
| 343 | Counters::total_compile_size.Increment(end_position - start_position); |
| 344 | |
| 345 | // Generate the AST for the lazily compiled function. The AST may be |
| 346 | // NULL in case of parser stack overflow. |
| 347 | FunctionLiteral* lit = MakeLazyAST(script, name, |
| 348 | start_position, |
| 349 | end_position, |
| 350 | is_expression); |
| 351 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 352 | // Check for parse errors. |
| 353 | if (lit == NULL) { |
| 354 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 358 | // Update the loop nesting in the function literal. |
| 359 | lit->set_loop_nesting(loop_nesting); |
| 360 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 361 | // Measure how long it takes to do the lazy compilation; only take |
| 362 | // the rest of the function into account to avoid overlap with the |
| 363 | // lazy parsing statistics. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 364 | HistogramTimerScope timer(&Counters::compile_lazy); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 365 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 366 | // Compile the code. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 367 | Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 369 | // Check for stack-overflow exception. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 371 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 372 | return false; |
| 373 | } |
| 374 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 375 | #if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 376 | // Log the code generation. If source information is available include script |
| 377 | // name and line number. Check explicit whether logging is enabled as finding |
| 378 | // the line number is not for free. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 379 | if (Logger::IsEnabled() || OProfileAgent::is_enabled()) { |
| 380 | Handle<String> func_name(name->length() > 0 ? |
| 381 | *name : shared->inferred_name()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 382 | if (script->name()->IsString()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 383 | int line_num = GetScriptLineNumber(script, start_position); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 384 | if (line_num > 0) { |
| 385 | line_num += script->line_offset()->value() + 1; |
| 386 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 387 | LOG(CodeCreateEvent(Logger::LAZY_COMPILE_TAG, *code, *func_name, |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 388 | String::cast(script->name()), line_num)); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 389 | OProfileAgent::CreateNativeCodeRegion(*func_name, |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 390 | String::cast(script->name()), |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 391 | line_num, |
| 392 | code->instruction_start(), |
| 393 | code->instruction_size()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 394 | } else { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 395 | LOG(CodeCreateEvent(Logger::LAZY_COMPILE_TAG, *code, *func_name)); |
| 396 | OProfileAgent::CreateNativeCodeRegion(*func_name, |
| 397 | code->instruction_start(), |
| 398 | code->instruction_size()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 402 | |
| 403 | // Update the shared function info with the compiled code. |
| 404 | shared->set_code(*code); |
| 405 | |
| 406 | // Set the expected number of properties for instances. |
| 407 | SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count()); |
| 408 | |
| 409 | // Check the function has compiled code. |
| 410 | ASSERT(shared->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | } } // namespace v8::internal |