Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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" |
| 32 | #include "compilation-cache.h" |
| 33 | #include "compiler.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 34 | #include "data-flow.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "debug.h" |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 36 | #include "full-codegen.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 37 | #include "liveedit.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | #include "oprofile-agent.h" |
| 39 | #include "rewriter.h" |
| 40 | #include "scopes.h" |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 41 | #include "scopeinfo.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
| 43 | namespace v8 { |
| 44 | namespace internal { |
| 45 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 46 | // For normal operation the syntax checker is used to determine whether to |
| 47 | // use the full compiler for top level code or not. However if the flag |
| 48 | // --always-full-compiler is specified or debugging is active the full |
| 49 | // compiler will be used for all code. |
| 50 | static bool AlwaysFullCompiler() { |
| 51 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 52 | return FLAG_always_full_compiler || Debugger::IsDebuggerActive(); |
| 53 | #else |
| 54 | return FLAG_always_full_compiler; |
| 55 | #endif |
| 56 | } |
| 57 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 58 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 59 | static Handle<Code> MakeCode(Handle<Context> context, CompilationInfo* info) { |
| 60 | FunctionLiteral* function = info->function(); |
| 61 | ASSERT(function != NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 62 | // Rewrite the AST by introducing .result assignments where needed. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 63 | if (!Rewriter::Process(function)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | // Signal a stack overflow by returning a null handle. The stack |
| 65 | // overflow exception will be thrown by the caller. |
| 66 | return Handle<Code>::null(); |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | // Compute top scope and allocate variables. For lazy compilation |
| 71 | // the top scope only contains the single lazily compiled function, |
| 72 | // so this doesn't re-allocate variables repeatedly. |
| 73 | HistogramTimerScope timer(&Counters::variable_allocation); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 74 | Scope* top = info->scope(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 75 | while (top->outer_scope() != NULL) top = top->outer_scope(); |
| 76 | top->AllocateVariables(context); |
| 77 | } |
| 78 | |
| 79 | #ifdef DEBUG |
| 80 | if (Bootstrapper::IsActive() ? |
| 81 | FLAG_print_builtin_scopes : |
| 82 | FLAG_print_scopes) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 83 | info->scope()->Print(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | } |
| 85 | #endif |
| 86 | |
| 87 | // Optimize the AST. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 88 | if (!Rewriter::Optimize(function)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | // Signal a stack overflow by returning a null handle. The stack |
| 90 | // overflow exception will be thrown by the caller. |
| 91 | return Handle<Code>::null(); |
| 92 | } |
| 93 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 94 | // Generate code and return it. Code generator selection is governed by |
| 95 | // which backends are enabled and whether the function is considered |
| 96 | // run-once code or not: |
| 97 | // |
| 98 | // --full-compiler enables the dedicated backend for code we expect to be |
| 99 | // run once |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 100 | // |
| 101 | // The normal choice of backend can be overridden with the flags |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 102 | // --always-full-compiler. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 103 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 104 | bool is_run_once = (shared.is_null()) |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 105 | ? info->scope()->is_global_scope() |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 106 | : (shared->is_toplevel() || shared->try_full_codegen()); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 107 | bool use_full = FLAG_full_compiler && !function->contains_loops(); |
| 108 | if (AlwaysFullCompiler() || (use_full && is_run_once)) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 109 | return FullCodeGenerator::MakeCode(info); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 110 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 111 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 112 | AssignedVariablesAnalyzer ava(function); |
| 113 | if (!ava.Analyze()) return Handle<Code>::null(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 114 | return CodeGenerator::MakeCode(info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 118 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 119 | Handle<Code> MakeCodeForLiveEdit(CompilationInfo* info) { |
| 120 | Handle<Context> context = Handle<Context>::null(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 121 | Handle<Code> code = MakeCode(context, info); |
| 122 | if (!info->shared_info().is_null()) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 123 | Handle<SerializedScopeInfo> scope_info = |
| 124 | SerializedScopeInfo::Create(info->scope()); |
| 125 | info->shared_info()->set_scope_info(*scope_info); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 126 | } |
| 127 | return code; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 128 | } |
| 129 | #endif |
| 130 | |
| 131 | |
| 132 | static Handle<SharedFunctionInfo> MakeFunctionInfo(bool is_global, |
| 133 | bool is_eval, |
| 134 | Compiler::ValidationState validate, |
| 135 | Handle<Script> script, |
| 136 | Handle<Context> context, |
| 137 | v8::Extension* extension, |
| 138 | ScriptDataImpl* pre_data) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | CompilationZoneScope zone_scope(DELETE_ON_EXIT); |
| 140 | |
| 141 | PostponeInterruptsScope postpone; |
| 142 | |
| 143 | ASSERT(!i::Top::global_context().is_null()); |
| 144 | script->set_context_data((*i::Top::global_context())->data()); |
| 145 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 146 | bool is_json = (validate == Compiler::VALIDATE_JSON); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 147 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 148 | if (is_eval || is_json) { |
| 149 | script->set_compilation_type( |
| 150 | is_json ? Smi::FromInt(Script::COMPILATION_TYPE_JSON) : |
| 151 | Smi::FromInt(Script::COMPILATION_TYPE_EVAL)); |
| 152 | // For eval scripts add information on the function from which eval was |
| 153 | // called. |
| 154 | if (is_eval) { |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 155 | StackTraceFrameIterator it; |
| 156 | if (!it.done()) { |
| 157 | script->set_eval_from_shared( |
| 158 | JSFunction::cast(it.frame()->function())->shared()); |
| 159 | int offset = static_cast<int>( |
| 160 | it.frame()->pc() - it.frame()->code()->instruction_start()); |
| 161 | script->set_eval_from_instructions_offset(Smi::FromInt(offset)); |
| 162 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | // Notify debugger |
| 167 | Debugger::OnBeforeCompile(script); |
| 168 | #endif |
| 169 | |
| 170 | // Only allow non-global compiles for eval. |
| 171 | ASSERT(is_eval || is_global); |
| 172 | |
| 173 | // Build AST. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 174 | FunctionLiteral* lit = |
| 175 | MakeAST(is_global, script, extension, pre_data, is_json); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 177 | LiveEditFunctionTracker live_edit_tracker(lit); |
| 178 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 179 | // Check for parse errors. |
| 180 | if (lit == NULL) { |
| 181 | ASSERT(Top::has_pending_exception()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 182 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | // Measure how long it takes to do the compilation; only take the |
| 186 | // rest of the function into account to avoid overlap with the |
| 187 | // parsing statistics. |
| 188 | HistogramTimer* rate = is_eval |
| 189 | ? &Counters::compile_eval |
| 190 | : &Counters::compile; |
| 191 | HistogramTimerScope timer(rate); |
| 192 | |
| 193 | // Compile the code. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 194 | CompilationInfo info(lit, script, is_eval); |
| 195 | Handle<Code> code = MakeCode(context, &info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | |
| 197 | // Check for stack-overflow exceptions. |
| 198 | if (code.is_null()) { |
| 199 | Top::StackOverflow(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 200 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 203 | if (script->name()->IsString()) { |
| 204 | PROFILE(CodeCreateEvent( |
| 205 | is_eval ? Logger::EVAL_TAG : |
| 206 | Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script), |
| 207 | *code, String::cast(script->name()))); |
| 208 | OPROFILE(CreateNativeCodeRegion(String::cast(script->name()), |
| 209 | code->instruction_start(), |
| 210 | code->instruction_size())); |
| 211 | } else { |
| 212 | PROFILE(CodeCreateEvent( |
| 213 | is_eval ? Logger::EVAL_TAG : |
| 214 | Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script), |
| 215 | *code, "")); |
| 216 | OPROFILE(CreateNativeCodeRegion(is_eval ? "Eval" : "Script", |
| 217 | code->instruction_start(), |
| 218 | code->instruction_size())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 219 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 220 | |
| 221 | // Allocate function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 222 | Handle<SharedFunctionInfo> result = |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 223 | Factory::NewSharedFunctionInfo( |
| 224 | lit->name(), |
| 225 | lit->materialized_literal_count(), |
| 226 | code, |
| 227 | SerializedScopeInfo::Create(info.scope())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 228 | |
| 229 | ASSERT_EQ(RelocInfo::kNoPosition, lit->function_token_position()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 230 | Compiler::SetFunctionInfo(result, lit, true, script); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | |
| 232 | // Hint to the runtime system used when allocating space for initial |
| 233 | // property space by setting the expected number of properties for |
| 234 | // the instances of the function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 235 | SetExpectedNofPropertiesFromEstimate(result, lit->expected_property_count()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | |
| 237 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 238 | // Notify debugger |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 239 | Debugger::OnAfterCompile(script, Debugger::NO_AFTER_COMPILE_FLAGS); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 240 | #endif |
| 241 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 242 | live_edit_tracker.RecordFunctionInfo(result, lit); |
| 243 | |
| 244 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 248 | Handle<SharedFunctionInfo> Compiler::Compile(Handle<String> source, |
| 249 | Handle<Object> script_name, |
| 250 | int line_offset, |
| 251 | int column_offset, |
| 252 | v8::Extension* extension, |
| 253 | ScriptDataImpl* input_pre_data, |
| 254 | Handle<Object> script_data, |
| 255 | NativesFlag natives) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 256 | int source_length = source->length(); |
| 257 | Counters::total_load_size.Increment(source_length); |
| 258 | Counters::total_compile_size.Increment(source_length); |
| 259 | |
| 260 | // The VM is in the COMPILER state until exiting this function. |
| 261 | VMState state(COMPILER); |
| 262 | |
| 263 | // Do a lookup in the compilation cache but not for extensions. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 264 | Handle<SharedFunctionInfo> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | if (extension == NULL) { |
| 266 | result = CompilationCache::LookupScript(source, |
| 267 | script_name, |
| 268 | line_offset, |
| 269 | column_offset); |
| 270 | } |
| 271 | |
| 272 | if (result.is_null()) { |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 273 | // No cache entry found. Do pre-parsing, if it makes sense, and compile |
| 274 | // the script. |
| 275 | // Building preparse data that is only used immediately after is only a |
| 276 | // saving if we might skip building the AST for lazily compiled functions. |
| 277 | // I.e., preparse data isn't relevant when the lazy flag is off, and |
| 278 | // for small sources, odds are that there aren't many functions |
| 279 | // that would be compiled lazily anyway, so we skip the preparse step |
| 280 | // in that case too. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 281 | ScriptDataImpl* pre_data = input_pre_data; |
Steve Block | 5915150 | 2010-09-22 15:07:15 +0100 | [diff] [blame] | 282 | if (pre_data == NULL |
| 283 | && FLAG_lazy |
| 284 | && source_length >= FLAG_min_preparse_length) { |
| 285 | pre_data = PartialPreParse(source, NULL, extension); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // Create a script object describing the script to be compiled. |
| 289 | Handle<Script> script = Factory::NewScript(source); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 290 | if (natives == NATIVES_CODE) { |
| 291 | script->set_type(Smi::FromInt(Script::TYPE_NATIVE)); |
| 292 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | if (!script_name.is_null()) { |
| 294 | script->set_name(*script_name); |
| 295 | script->set_line_offset(Smi::FromInt(line_offset)); |
| 296 | script->set_column_offset(Smi::FromInt(column_offset)); |
| 297 | } |
| 298 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 299 | script->set_data(script_data.is_null() ? Heap::undefined_value() |
| 300 | : *script_data); |
| 301 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | // Compile the function and add it to the cache. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 303 | result = MakeFunctionInfo(true, |
| 304 | false, |
| 305 | DONT_VALIDATE_JSON, |
| 306 | script, |
| 307 | Handle<Context>::null(), |
| 308 | extension, |
| 309 | pre_data); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | if (extension == NULL && !result.is_null()) { |
| 311 | CompilationCache::PutScript(source, result); |
| 312 | } |
| 313 | |
| 314 | // Get rid of the pre-parsing data (if necessary). |
| 315 | if (input_pre_data == NULL && pre_data != NULL) { |
| 316 | delete pre_data; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (result.is_null()) Top::ReportPendingMessages(); |
| 321 | return result; |
| 322 | } |
| 323 | |
| 324 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 325 | Handle<SharedFunctionInfo> Compiler::CompileEval(Handle<String> source, |
| 326 | Handle<Context> context, |
| 327 | bool is_global, |
| 328 | ValidationState validate) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | // Note that if validation is required then no path through this |
| 330 | // function is allowed to return a value without validating that |
| 331 | // the input is legal json. |
| 332 | |
| 333 | int source_length = source->length(); |
| 334 | Counters::total_eval_size.Increment(source_length); |
| 335 | Counters::total_compile_size.Increment(source_length); |
| 336 | |
| 337 | // The VM is in the COMPILER state until exiting this function. |
| 338 | VMState state(COMPILER); |
| 339 | |
| 340 | // Do a lookup in the compilation cache; if the entry is not there, |
| 341 | // invoke the compiler and add the result to the cache. If we're |
| 342 | // evaluating json we bypass the cache since we can't be sure a |
| 343 | // potential value in the cache has been validated. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 344 | Handle<SharedFunctionInfo> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | if (validate == DONT_VALIDATE_JSON) |
| 346 | result = CompilationCache::LookupEval(source, context, is_global); |
| 347 | |
| 348 | if (result.is_null()) { |
| 349 | // Create a script object describing the script to be compiled. |
| 350 | Handle<Script> script = Factory::NewScript(source); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 351 | result = MakeFunctionInfo(is_global, |
| 352 | true, |
| 353 | validate, |
| 354 | script, |
| 355 | context, |
| 356 | NULL, |
| 357 | NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | if (!result.is_null() && validate != VALIDATE_JSON) { |
| 359 | // For json it's unlikely that we'll ever see exactly the same |
| 360 | // string again so we don't use the compilation cache. |
| 361 | CompilationCache::PutEval(source, context, is_global, result); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return result; |
| 366 | } |
| 367 | |
| 368 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 369 | bool Compiler::CompileLazy(CompilationInfo* info) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | CompilationZoneScope zone_scope(DELETE_ON_EXIT); |
| 371 | |
| 372 | // The VM is in the COMPILER state until exiting this function. |
| 373 | VMState state(COMPILER); |
| 374 | |
| 375 | PostponeInterruptsScope postpone; |
| 376 | |
| 377 | // Compute name, source code and script data. |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 378 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 379 | Handle<String> name(String::cast(shared->name())); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | |
| 381 | int start_position = shared->start_position(); |
| 382 | int end_position = shared->end_position(); |
| 383 | bool is_expression = shared->is_expression(); |
| 384 | Counters::total_compile_size.Increment(end_position - start_position); |
| 385 | |
| 386 | // Generate the AST for the lazily compiled function. The AST may be |
| 387 | // NULL in case of parser stack overflow. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 388 | FunctionLiteral* lit = MakeLazyAST(info->script(), |
| 389 | name, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 390 | start_position, |
| 391 | end_position, |
| 392 | is_expression); |
| 393 | |
| 394 | // Check for parse errors. |
| 395 | if (lit == NULL) { |
| 396 | ASSERT(Top::has_pending_exception()); |
| 397 | return false; |
| 398 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 399 | info->set_function(lit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 400 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | // Measure how long it takes to do the lazy compilation; only take |
| 402 | // the rest of the function into account to avoid overlap with the |
| 403 | // lazy parsing statistics. |
| 404 | HistogramTimerScope timer(&Counters::compile_lazy); |
| 405 | |
| 406 | // Compile the code. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 407 | Handle<Code> code = MakeCode(Handle<Context>::null(), info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 408 | |
| 409 | // Check for stack-overflow exception. |
| 410 | if (code.is_null()) { |
| 411 | Top::StackOverflow(); |
| 412 | return false; |
| 413 | } |
| 414 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 415 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, |
| 416 | name, |
| 417 | Handle<String>(shared->inferred_name()), |
| 418 | start_position, |
| 419 | info->script(), |
| 420 | code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 422 | // Update the shared function info with the compiled code and the scope info. |
| 423 | // Please note, that the order of the sharedfunction initialization is |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 424 | // important since SerializedScopeInfo::Create might trigger a GC, causing |
| 425 | // the ASSERT below to be invalid if the code was flushed. By setting the code |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 426 | // object last we avoid this. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 427 | Handle<SerializedScopeInfo> scope_info = |
| 428 | SerializedScopeInfo::Create(info->scope()); |
| 429 | shared->set_scope_info(*scope_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 430 | shared->set_code(*code); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 431 | if (!info->closure().is_null()) { |
| 432 | info->closure()->set_code(*code); |
| 433 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | |
| 435 | // Set the expected number of properties for instances. |
| 436 | SetExpectedNofPropertiesFromEstimate(shared, lit->expected_property_count()); |
| 437 | |
| 438 | // Set the optimication hints after performing lazy compilation, as these are |
| 439 | // not set when the function is set up as a lazily compiled function. |
| 440 | shared->SetThisPropertyAssignmentsInfo( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 441 | lit->has_only_simple_this_property_assignments(), |
| 442 | *lit->this_property_assignments()); |
| 443 | |
| 444 | // Check the function has compiled code. |
| 445 | ASSERT(shared->is_compiled()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 446 | shared->set_code_age(0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | |
| 450 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 451 | Handle<SharedFunctionInfo> Compiler::BuildFunctionInfo(FunctionLiteral* literal, |
| 452 | Handle<Script> script, |
| 453 | AstVisitor* caller) { |
| 454 | LiveEditFunctionTracker live_edit_tracker(literal); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 455 | #ifdef DEBUG |
| 456 | // We should not try to compile the same function literal more than |
| 457 | // once. |
| 458 | literal->mark_as_compiled(); |
| 459 | #endif |
| 460 | |
| 461 | // Determine if the function can be lazily compiled. This is |
| 462 | // necessary to allow some of our builtin JS files to be lazily |
| 463 | // compiled. These builtins cannot be handled lazily by the parser, |
| 464 | // since we have to know if a function uses the special natives |
| 465 | // syntax, which is something the parser records. |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 466 | bool allow_lazy = literal->AllowsLazyCompilation() && |
| 467 | !LiveEditFunctionTracker::IsActive(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 468 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 469 | Handle<SerializedScopeInfo> scope_info(SerializedScopeInfo::Empty()); |
| 470 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 471 | // Generate code |
| 472 | Handle<Code> code; |
| 473 | if (FLAG_lazy && allow_lazy) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 474 | code = Handle<Code>(Builtins::builtin(Builtins::LazyCompile)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 475 | } else { |
| 476 | // The bodies of function literals have not yet been visited by |
| 477 | // the AST optimizer/analyzer. |
| 478 | if (!Rewriter::Optimize(literal)) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 479 | return Handle<SharedFunctionInfo>::null(); |
| 480 | } |
| 481 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 482 | // Generate code and return it. The way that the compilation mode |
| 483 | // is controlled by the command-line flags is described in |
| 484 | // the static helper function MakeCode. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 485 | CompilationInfo info(literal, script, false); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 486 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 487 | bool is_run_once = literal->try_full_codegen(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 488 | bool use_full = FLAG_full_compiler && !literal->contains_loops(); |
| 489 | if (AlwaysFullCompiler() || (use_full && is_run_once)) { |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 490 | code = FullCodeGenerator::MakeCode(&info); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 491 | } else { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 492 | // We fall back to the classic V8 code generator. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 493 | AssignedVariablesAnalyzer ava(literal); |
| 494 | if (!ava.Analyze()) return Handle<SharedFunctionInfo>::null(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 495 | code = CodeGenerator::MakeCode(&info); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Check for stack-overflow exception. |
| 499 | if (code.is_null()) { |
| 500 | caller->SetStackOverflow(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 501 | return Handle<SharedFunctionInfo>::null(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | // Function compilation complete. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 505 | RecordFunctionCompilation(Logger::FUNCTION_TAG, |
| 506 | literal->name(), |
| 507 | literal->inferred_name(), |
| 508 | literal->start_position(), |
| 509 | script, |
| 510 | code); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 511 | scope_info = SerializedScopeInfo::Create(info.scope()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 514 | // Create a shared function info object. |
| 515 | Handle<SharedFunctionInfo> result = |
| 516 | Factory::NewSharedFunctionInfo(literal->name(), |
| 517 | literal->materialized_literal_count(), |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 518 | code, |
| 519 | scope_info); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 520 | SetFunctionInfo(result, literal, false, script); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 521 | |
| 522 | // Set the expected number of properties for instances and return |
| 523 | // the resulting function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 524 | SetExpectedNofPropertiesFromEstimate(result, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 525 | literal->expected_property_count()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 526 | live_edit_tracker.RecordFunctionInfo(result, literal); |
| 527 | return result; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
| 531 | // Sets the function info on a function. |
| 532 | // The start_position points to the first '(' character after the function name |
| 533 | // in the full script source. When counting characters in the script source the |
| 534 | // the first character is number 0 (not 1). |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 535 | void Compiler::SetFunctionInfo(Handle<SharedFunctionInfo> function_info, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 536 | FunctionLiteral* lit, |
| 537 | bool is_toplevel, |
| 538 | Handle<Script> script) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 539 | function_info->set_length(lit->num_parameters()); |
| 540 | function_info->set_formal_parameter_count(lit->num_parameters()); |
| 541 | function_info->set_script(*script); |
| 542 | function_info->set_function_token_position(lit->function_token_position()); |
| 543 | function_info->set_start_position(lit->start_position()); |
| 544 | function_info->set_end_position(lit->end_position()); |
| 545 | function_info->set_is_expression(lit->is_expression()); |
| 546 | function_info->set_is_toplevel(is_toplevel); |
| 547 | function_info->set_inferred_name(*lit->inferred_name()); |
| 548 | function_info->SetThisPropertyAssignmentsInfo( |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 549 | lit->has_only_simple_this_property_assignments(), |
| 550 | *lit->this_property_assignments()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 551 | function_info->set_try_full_codegen(lit->try_full_codegen()); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 552 | function_info->set_allows_lazy_compilation(lit->AllowsLazyCompilation()); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 556 | void Compiler::RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
| 557 | Handle<String> name, |
| 558 | Handle<String> inferred_name, |
| 559 | int start_position, |
| 560 | Handle<Script> script, |
| 561 | Handle<Code> code) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 562 | // Log the code generation. If source information is available |
| 563 | // include script name and line number. Check explicitly whether |
| 564 | // logging is enabled as finding the line number is not free. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 565 | if (Logger::is_logging() |
| 566 | || OProfileAgent::is_enabled() |
| 567 | || CpuProfiler::is_profiling()) { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 568 | Handle<String> func_name(name->length() > 0 ? *name : *inferred_name); |
| 569 | if (script->name()->IsString()) { |
| 570 | int line_num = GetScriptLineNumber(script, start_position) + 1; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 571 | USE(line_num); |
| 572 | PROFILE(CodeCreateEvent(Logger::ToNativeByScript(tag, *script), |
| 573 | *code, *func_name, |
| 574 | String::cast(script->name()), line_num)); |
| 575 | OPROFILE(CreateNativeCodeRegion(*func_name, |
| 576 | String::cast(script->name()), |
| 577 | line_num, |
| 578 | code->instruction_start(), |
| 579 | code->instruction_size())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 580 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 581 | PROFILE(CodeCreateEvent(Logger::ToNativeByScript(tag, *script), |
| 582 | *code, *func_name)); |
| 583 | OPROFILE(CreateNativeCodeRegion(*func_name, |
| 584 | code->instruction_start(), |
| 585 | code->instruction_size())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 589 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 590 | } } // namespace v8::internal |