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 | |
| 40 | namespace v8 { namespace internal { |
| 41 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | static Handle<Code> MakeCode(FunctionLiteral* literal, |
| 43 | Handle<Script> script, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 44 | Handle<Context> context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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)) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 50 | // Signal a stack overflow by returning a null handle. The stack |
| 51 | // overflow exception will be thrown by the caller. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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(); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 60 | top->AllocateVariables(context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 70 | // Optimize the AST. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 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 | } |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 76 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 86 | Handle<Context> context, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 113 | HistogramTimer* rate = is_eval |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | ? &Counters::compile_eval |
| 115 | : &Counters::compile; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 116 | HistogramTimerScope timer(rate); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | |
| 118 | // Compile the code. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 119 | Handle<Code> code = MakeCode(lit, script, context, 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 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 127 | #if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 128 | // Log the code generation for the script. Check explicit whether logging is |
| 129 | // to avoid allocating when not required. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 130 | if (Logger::is_enabled() || OProfileAgent::is_enabled()) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 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)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 135 | OProfileAgent::CreateNativeCodeRegion(*data, code->address(), |
| 136 | code->ExecutableSize()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 137 | } else { |
| 138 | LOG(CodeCreateEvent(is_eval ? "Eval" : "Script", *code, "")); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 139 | OProfileAgent::CreateNativeCodeRegion(is_eval ? "Eval" : "Script", |
| 140 | code->address(), code->ExecutableSize()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 141 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 142 | } |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 143 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | |
| 145 | // Allocate function. |
| 146 | Handle<JSFunction> fun = |
| 147 | Factory::NewFunctionBoilerplate(lit->name(), |
| 148 | lit->materialized_literal_count(), |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 149 | lit->contains_array_literal(), |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 150 | code); |
| 151 | |
| 152 | CodeGenerator::SetFunctionInfo(fun, lit->scope()->num_parameters(), |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 153 | RelocInfo::kNoPosition, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 154 | lit->start_position(), lit->end_position(), |
| 155 | lit->is_expression(), true, script); |
| 156 | |
| 157 | // Hint to the runtime system used when allocating space for initial |
| 158 | // property space by setting the expected number of properties for |
| 159 | // the instances of the function. |
| 160 | SetExpectedNofPropertiesFromEstimate(fun, lit->expected_property_count()); |
| 161 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | // Notify debugger |
| 163 | Debugger::OnAfterCompile(script, fun); |
| 164 | |
| 165 | return fun; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | static StaticResource<SafeStringInputBuffer> safe_string_input_buffer; |
| 170 | |
| 171 | |
| 172 | Handle<JSFunction> Compiler::Compile(Handle<String> source, |
mads.s.ager | cbaa060 | 2008-08-14 13:41:48 +0000 | [diff] [blame] | 173 | Handle<Object> script_name, |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | int line_offset, int column_offset, |
| 175 | v8::Extension* extension, |
| 176 | ScriptDataImpl* input_pre_data) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 177 | int source_length = source->length(); |
| 178 | Counters::total_load_size.Increment(source_length); |
| 179 | Counters::total_compile_size.Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | |
| 181 | // The VM is in the COMPILER state until exiting this function. |
| 182 | VMState state(COMPILER); |
| 183 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 184 | // Do a lookup in the compilation cache but not for extensions. |
| 185 | Handle<JSFunction> result; |
| 186 | if (extension == NULL) { |
| 187 | result = CompilationCache::LookupScript(source, |
| 188 | script_name, |
| 189 | line_offset, |
| 190 | column_offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 191 | } |
| 192 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 193 | if (result.is_null()) { |
| 194 | // No cache entry found. Do pre-parsing and compile the script. |
| 195 | ScriptDataImpl* pre_data = input_pre_data; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 196 | if (pre_data == NULL && source_length >= FLAG_min_preparse_length) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 197 | Access<SafeStringInputBuffer> buf(&safe_string_input_buffer); |
| 198 | buf->Reset(source.location()); |
| 199 | pre_data = PreParse(buf.value(), extension); |
| 200 | } |
| 201 | |
| 202 | // Create a script object describing the script to be compiled. |
| 203 | Handle<Script> script = Factory::NewScript(source); |
| 204 | if (!script_name.is_null()) { |
| 205 | script->set_name(*script_name); |
| 206 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 207 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 208 | } |
| 209 | |
| 210 | // Compile the function and add it to the cache. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 211 | result = MakeFunction(true, |
| 212 | false, |
| 213 | script, |
| 214 | Handle<Context>::null(), |
| 215 | extension, |
| 216 | pre_data); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 217 | if (extension == NULL && !result.is_null()) { |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 218 | CompilationCache::PutScript(source, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Get rid of the pre-parsing data (if necessary). |
| 222 | if (input_pre_data == NULL && pre_data != NULL) { |
| 223 | delete pre_data; |
| 224 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 225 | } |
| 226 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 227 | if (result.is_null()) Top::ReportPendingMessages(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 228 | return result; |
| 229 | } |
| 230 | |
| 231 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 232 | Handle<JSFunction> Compiler::CompileEval(Handle<String> source, |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 233 | Handle<Context> context, |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 234 | int line_offset, |
| 235 | bool is_global) { |
ager@chromium.org | c3e50d8 | 2008-11-05 11:53:10 +0000 | [diff] [blame] | 236 | int source_length = source->length(); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 237 | Counters::total_eval_size.Increment(source_length); |
| 238 | Counters::total_compile_size.Increment(source_length); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | |
| 240 | // The VM is in the COMPILER state until exiting this function. |
| 241 | VMState state(COMPILER); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 242 | CompilationCache::Entry entry = is_global |
| 243 | ? CompilationCache::EVAL_GLOBAL |
| 244 | : CompilationCache::EVAL_CONTEXTUAL; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 246 | // Do a lookup in the compilation cache; if the entry is not there, |
| 247 | // invoke the compiler and add the result to the cache. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 248 | Handle<JSFunction> result = |
| 249 | CompilationCache::LookupEval(source, context, entry); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 250 | if (result.is_null()) { |
| 251 | // Create a script object describing the script to be compiled. |
| 252 | Handle<Script> script = Factory::NewScript(source); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 253 | script->set_line_offset(Smi::FromInt(line_offset)); |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 254 | result = MakeFunction(is_global, true, script, context, NULL, NULL); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 255 | if (!result.is_null()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 256 | CompilationCache::PutEval(source, context, entry, result); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 259 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 260 | return result; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 264 | bool Compiler::CompileLazy(Handle<SharedFunctionInfo> shared, |
| 265 | int loop_nesting) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 266 | ZoneScope zone_scope(DELETE_ON_EXIT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 267 | |
| 268 | // The VM is in the COMPILER state until exiting this function. |
| 269 | VMState state(COMPILER); |
| 270 | |
| 271 | // Make sure we have an initial stack limit. |
| 272 | StackGuard guard; |
kasper.lund | 4451067 | 2008-07-25 07:37:58 +0000 | [diff] [blame] | 273 | PostponeInterruptsScope postpone; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | |
| 275 | // Compute name, source code and script data. |
| 276 | Handle<String> name(String::cast(shared->name())); |
| 277 | Handle<Script> script(Script::cast(shared->script())); |
| 278 | |
| 279 | int start_position = shared->start_position(); |
| 280 | int end_position = shared->end_position(); |
| 281 | bool is_expression = shared->is_expression(); |
| 282 | Counters::total_compile_size.Increment(end_position - start_position); |
| 283 | |
| 284 | // Generate the AST for the lazily compiled function. The AST may be |
| 285 | // NULL in case of parser stack overflow. |
| 286 | FunctionLiteral* lit = MakeLazyAST(script, name, |
| 287 | start_position, |
| 288 | end_position, |
| 289 | is_expression); |
| 290 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 291 | // Check for parse errors. |
| 292 | if (lit == NULL) { |
| 293 | ASSERT(Top::has_pending_exception()); |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 297 | // Update the loop nesting in the function literal. |
| 298 | lit->set_loop_nesting(loop_nesting); |
| 299 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | // Measure how long it takes to do the lazy compilation; only take |
| 301 | // the rest of the function into account to avoid overlap with the |
| 302 | // lazy parsing statistics. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 303 | HistogramTimerScope timer(&Counters::compile_lazy); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 304 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 305 | // Compile the code. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 306 | Handle<Code> code = MakeCode(lit, script, Handle<Context>::null(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 307 | |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 308 | // Check for stack-overflow exception. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 309 | if (code.is_null()) { |
kasper.lund | 212ac23 | 2008-07-16 07:07:30 +0000 | [diff] [blame] | 310 | Top::StackOverflow(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 314 | #if defined ENABLE_LOGGING_AND_PROFILING || defined ENABLE_OPROFILE_AGENT |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 315 | // Log the code generation. If source information is available include script |
| 316 | // name and line number. Check explicit whether logging is enabled as finding |
| 317 | // the line number is not for free. |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 318 | if (Logger::is_enabled() || OProfileAgent::is_enabled()) { |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 319 | if (script->name()->IsString()) { |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 320 | int line_num = GetScriptLineNumber(script, start_position); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 321 | if (line_num > 0) { |
| 322 | line_num += script->line_offset()->value() + 1; |
| 323 | } |
| 324 | LOG(CodeCreateEvent("LazyCompile", *code, *lit->name(), |
| 325 | String::cast(script->name()), line_num)); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 326 | OProfileAgent::CreateNativeCodeRegion(*lit->name(), |
| 327 | String::cast(script->name()), |
| 328 | line_num, code->address(), |
| 329 | code->ExecutableSize()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 330 | } else { |
| 331 | LOG(CodeCreateEvent("LazyCompile", *code, *lit->name())); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 332 | OProfileAgent::CreateNativeCodeRegion(*lit->name(), code->address(), |
| 333 | code->ExecutableSize()); |
iposva@chromium.org | 245aa85 | 2009-02-10 00:49:54 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | |
| 338 | // Update the shared function info with the compiled code. |
| 339 | shared->set_code(*code); |
| 340 | |
| 341 | // Set the expected number of properties for instances. |
| 342 | SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count()); |
| 343 | |
| 344 | // Check the function has compiled code. |
| 345 | ASSERT(shared->is_compiled()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
| 349 | |
| 350 | } } // namespace v8::internal |