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