Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/compiler.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 6 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
| 9 | #include "src/ast/ast-numbering.h" |
| 10 | #include "src/ast/prettyprinter.h" |
| 11 | #include "src/ast/scopeinfo.h" |
| 12 | #include "src/ast/scopes.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 13 | #include "src/bootstrapper.h" |
| 14 | #include "src/codegen.h" |
| 15 | #include "src/compilation-cache.h" |
| 16 | #include "src/compiler/pipeline.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | #include "src/crankshaft/hydrogen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | #include "src/debug/debug.h" |
| 19 | #include "src/debug/liveedit.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | #include "src/deoptimizer.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 21 | #include "src/frames-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | #include "src/full-codegen/full-codegen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 23 | #include "src/interpreter/interpreter.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | #include "src/isolate-inl.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | #include "src/log-inl.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 26 | #include "src/messages.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | #include "src/parsing/parser.h" |
| 28 | #include "src/parsing/rewriter.h" |
| 29 | #include "src/parsing/scanner-character-streams.h" |
| 30 | #include "src/profiler/cpu-profiler.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | #include "src/runtime-profiler.h" |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 32 | #include "src/snapshot/code-serializer.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 33 | #include "src/typing-asm.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 34 | #include "src/vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | |
| 36 | namespace v8 { |
| 37 | namespace internal { |
| 38 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 39 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | #define PARSE_INFO_GETTER(type, name) \ |
| 41 | type CompilationInfo::name() const { \ |
| 42 | CHECK(parse_info()); \ |
| 43 | return parse_info()->name(); \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 44 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | #define PARSE_INFO_GETTER_WITH_DEFAULT(type, name, def) \ |
| 48 | type CompilationInfo::name() const { \ |
| 49 | return parse_info() ? parse_info()->name() : def; \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 50 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 51 | |
| 52 | |
| 53 | PARSE_INFO_GETTER(Handle<Script>, script) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 54 | PARSE_INFO_GETTER(FunctionLiteral*, literal) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 55 | PARSE_INFO_GETTER_WITH_DEFAULT(Scope*, scope, nullptr) |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 56 | PARSE_INFO_GETTER_WITH_DEFAULT(Handle<Context>, context, |
| 57 | Handle<Context>::null()) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | PARSE_INFO_GETTER(Handle<SharedFunctionInfo>, shared_info) |
| 59 | |
| 60 | #undef PARSE_INFO_GETTER |
| 61 | #undef PARSE_INFO_GETTER_WITH_DEFAULT |
| 62 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 63 | // A wrapper around a CompilationInfo that detaches the Handles from |
| 64 | // the underlying DeferredHandleScope and stores them in info_ on |
| 65 | // destruction. |
| 66 | class CompilationHandleScope BASE_EMBEDDED { |
| 67 | public: |
| 68 | explicit CompilationHandleScope(CompilationInfo* info) |
| 69 | : deferred_(info->isolate()), info_(info) {} |
| 70 | ~CompilationHandleScope() { info_->set_deferred_handles(deferred_.Detach()); } |
| 71 | |
| 72 | private: |
| 73 | DeferredHandleScope deferred_; |
| 74 | CompilationInfo* info_; |
| 75 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 77 | // Helper that times a scoped region and records the elapsed time. |
| 78 | struct ScopedTimer { |
| 79 | explicit ScopedTimer(base::TimeDelta* location) : location_(location) { |
| 80 | DCHECK(location_ != NULL); |
| 81 | timer_.Start(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 84 | ~ScopedTimer() { *location_ += timer_.Elapsed(); } |
| 85 | |
| 86 | base::ElapsedTimer timer_; |
| 87 | base::TimeDelta* location_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 90 | // ---------------------------------------------------------------------------- |
| 91 | // Implementation of CompilationInfo |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | |
| 93 | bool CompilationInfo::has_shared_info() const { |
| 94 | return parse_info_ && !parse_info_->shared_info().is_null(); |
| 95 | } |
| 96 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 97 | CompilationInfo::CompilationInfo(ParseInfo* parse_info, |
| 98 | Handle<JSFunction> closure) |
| 99 | : CompilationInfo(parse_info, {}, Code::ComputeFlags(Code::FUNCTION), BASE, |
| 100 | parse_info->isolate(), parse_info->zone()) { |
| 101 | closure_ = closure; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 103 | // Compiling for the snapshot typically results in different code than |
| 104 | // compiling later on. This means that code recompiled with deoptimization |
| 105 | // support won't be "equivalent" (as defined by SharedFunctionInfo:: |
| 106 | // EnableDeoptimizationSupport), so it will replace the old code and all |
| 107 | // its type feedback. To avoid this, always compile functions in the snapshot |
| 108 | // with deoptimization support. |
| 109 | if (isolate_->serializer_enabled()) EnableDeoptimizationSupport(); |
| 110 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | if (FLAG_function_context_specialization) MarkAsFunctionContextSpecializing(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 112 | if (FLAG_turbo_inlining) MarkAsInliningEnabled(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | if (FLAG_turbo_source_positions) MarkAsSourcePositionsEnabled(); |
| 114 | if (FLAG_turbo_splitting) MarkAsSplittingEnabled(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 117 | CompilationInfo::CompilationInfo(Vector<const char> debug_name, |
| 118 | Isolate* isolate, Zone* zone, |
| 119 | Code::Flags code_flags) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 120 | : CompilationInfo(nullptr, debug_name, code_flags, STUB, isolate, zone) {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 122 | CompilationInfo::CompilationInfo(ParseInfo* parse_info, |
| 123 | Vector<const char> debug_name, |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 124 | Code::Flags code_flags, Mode mode, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | Isolate* isolate, Zone* zone) |
| 126 | : parse_info_(parse_info), |
| 127 | isolate_(isolate), |
| 128 | flags_(0), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 129 | code_flags_(code_flags), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | mode_(mode), |
| 131 | osr_ast_id_(BailoutId::None()), |
| 132 | zone_(zone), |
| 133 | deferred_handles_(nullptr), |
| 134 | dependencies_(isolate, zone), |
| 135 | bailout_reason_(kNoReason), |
| 136 | prologue_offset_(Code::kPrologueOffsetNotSet), |
| 137 | track_positions_(FLAG_hydrogen_track_positions || |
| 138 | isolate->cpu_profiler()->is_profiling()), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | parameter_count_(0), |
| 140 | optimization_id_(-1), |
| 141 | osr_expr_stack_height_(0), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 142 | debug_name_(debug_name) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 143 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 144 | CompilationInfo::~CompilationInfo() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | DisableFutureOptimization(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 146 | dependencies()->Rollback(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | delete deferred_handles_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | int CompilationInfo::num_parameters() const { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 152 | return !IsStub() ? scope()->num_parameters() : parameter_count_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | int CompilationInfo::num_parameters_including_this() const { |
| 157 | return num_parameters() + (is_this_defined() ? 1 : 0); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | bool CompilationInfo::is_this_defined() const { return !IsStub(); } |
| 162 | |
| 163 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 164 | // Primitive functions are unlikely to be picked up by the stack-walking |
| 165 | // profiler, so they trigger their own optimization when they're called |
| 166 | // for the SharedFunctionInfo::kCallsUntilPrimitiveOptimization-th time. |
| 167 | bool CompilationInfo::ShouldSelfOptimize() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 168 | return FLAG_crankshaft && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 169 | !(literal()->flags() & AstProperties::kDontSelfOptimize) && |
| 170 | !literal()->dont_optimize() && |
| 171 | literal()->scope()->AllowsLazyCompilation() && |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 172 | !shared_info()->optimization_disabled(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 173 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 174 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 175 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 176 | bool CompilationInfo::has_simple_parameters() { |
| 177 | return scope()->has_simple_parameters(); |
| 178 | } |
| 179 | |
| 180 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | base::SmartArrayPointer<char> CompilationInfo::GetDebugName() const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 182 | if (parse_info() && parse_info()->literal()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 183 | AllowHandleDereference allow_deref; |
| 184 | return parse_info()->literal()->debug_name()->ToCString(); |
| 185 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 186 | if (parse_info() && !parse_info()->shared_info().is_null()) { |
| 187 | return parse_info()->shared_info()->DebugName()->ToCString(); |
| 188 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 189 | Vector<const char> name_vec = debug_name_; |
| 190 | if (name_vec.is_empty()) name_vec = ArrayVector("unknown"); |
| 191 | base::SmartArrayPointer<char> name(new char[name_vec.length() + 1]); |
| 192 | memcpy(name.get(), name_vec.start(), name_vec.length()); |
| 193 | name[name_vec.length()] = '\0'; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 194 | return name; |
| 195 | } |
| 196 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 197 | StackFrame::Type CompilationInfo::GetOutputStackFrameType() const { |
| 198 | switch (output_code_kind()) { |
| 199 | case Code::STUB: |
| 200 | case Code::BYTECODE_HANDLER: |
| 201 | case Code::HANDLER: |
| 202 | case Code::BUILTIN: |
| 203 | return StackFrame::STUB; |
| 204 | case Code::WASM_FUNCTION: |
| 205 | return StackFrame::WASM; |
| 206 | case Code::JS_TO_WASM_FUNCTION: |
| 207 | return StackFrame::JS_TO_WASM; |
| 208 | case Code::WASM_TO_JS_FUNCTION: |
| 209 | return StackFrame::WASM_TO_JS; |
| 210 | default: |
| 211 | UNIMPLEMENTED(); |
| 212 | return StackFrame::NONE; |
| 213 | } |
| 214 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 215 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 216 | int CompilationInfo::GetDeclareGlobalsFlags() const { |
| 217 | DCHECK(DeclareGlobalsLanguageMode::is_valid(parse_info()->language_mode())); |
| 218 | return DeclareGlobalsEvalFlag::encode(parse_info()->is_eval()) | |
| 219 | DeclareGlobalsNativeFlag::encode(parse_info()->is_native()) | |
| 220 | DeclareGlobalsLanguageMode::encode(parse_info()->language_mode()); |
| 221 | } |
| 222 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 223 | bool CompilationInfo::ExpectsJSReceiverAsReceiver() { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 224 | return is_sloppy(parse_info()->language_mode()) && !parse_info()->is_native(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 227 | #if DEBUG |
| 228 | void CompilationInfo::PrintAstForTesting() { |
| 229 | PrintF("--- Source from AST ---\n%s\n", |
| 230 | PrettyPrinter(isolate()).PrintProgram(literal())); |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | // ---------------------------------------------------------------------------- |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 235 | // Implementation of CompilationJob |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 236 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 237 | CompilationJob::Status CompilationJob::CreateGraph() { |
| 238 | DisallowJavascriptExecution no_js(isolate()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 239 | DCHECK(info()->IsOptimizing()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 240 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 241 | if (FLAG_trace_opt) { |
| 242 | OFStream os(stdout); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 243 | os << "[compiling method " << Brief(*info()->closure()) << " using " |
| 244 | << compiler_name_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 245 | if (info()->is_osr()) os << " OSR"; |
| 246 | os << "]" << std::endl; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 249 | // Delegate to the underlying implementation. |
| 250 | DCHECK_EQ(SUCCEEDED, last_status()); |
| 251 | ScopedTimer t(&time_taken_to_create_graph_); |
| 252 | return SetLastStatus(CreateGraphImpl()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 255 | CompilationJob::Status CompilationJob::OptimizeGraph() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | DisallowHeapAllocation no_allocation; |
| 257 | DisallowHandleAllocation no_handles; |
| 258 | DisallowHandleDereference no_deref; |
| 259 | DisallowCodeDependencyChange no_dependency_change; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 260 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 261 | // Delegate to the underlying implementation. |
| 262 | DCHECK_EQ(SUCCEEDED, last_status()); |
| 263 | ScopedTimer t(&time_taken_to_optimize_); |
| 264 | return SetLastStatus(OptimizeGraphImpl()); |
| 265 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 266 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 267 | CompilationJob::Status CompilationJob::GenerateCode() { |
| 268 | DisallowCodeDependencyChange no_dependency_change; |
| 269 | DisallowJavascriptExecution no_js(isolate()); |
| 270 | DCHECK(!info()->dependencies()->HasAborted()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 271 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 272 | // Delegate to the underlying implementation. |
| 273 | DCHECK_EQ(SUCCEEDED, last_status()); |
| 274 | ScopedTimer t(&time_taken_to_codegen_); |
| 275 | return SetLastStatus(GenerateCodeImpl()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 276 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 277 | |
| 278 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 279 | namespace { |
| 280 | |
| 281 | void AddWeakObjectToCodeDependency(Isolate* isolate, Handle<HeapObject> object, |
| 282 | Handle<Code> code) { |
| 283 | Handle<WeakCell> cell = Code::WeakCellFor(code); |
| 284 | Heap* heap = isolate->heap(); |
| 285 | Handle<DependentCode> dep(heap->LookupWeakObjectToCodeDependency(object)); |
| 286 | dep = DependentCode::InsertWeakCode(dep, DependentCode::kWeakCodeGroup, cell); |
| 287 | heap->AddWeakObjectToCodeDependency(object, dep); |
| 288 | } |
| 289 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 290 | } // namespace |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 292 | void CompilationJob::RegisterWeakObjectsInOptimizedCode(Handle<Code> code) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 293 | // TODO(turbofan): Move this to pipeline.cc once Crankshaft dies. |
| 294 | Isolate* const isolate = code->GetIsolate(); |
| 295 | DCHECK(code->is_optimized_code()); |
| 296 | std::vector<Handle<Map>> maps; |
| 297 | std::vector<Handle<HeapObject>> objects; |
| 298 | { |
| 299 | DisallowHeapAllocation no_gc; |
| 300 | int const mode_mask = RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | |
| 301 | RelocInfo::ModeMask(RelocInfo::CELL); |
| 302 | for (RelocIterator it(*code, mode_mask); !it.done(); it.next()) { |
| 303 | RelocInfo::Mode mode = it.rinfo()->rmode(); |
| 304 | if (mode == RelocInfo::CELL && |
| 305 | code->IsWeakObjectInOptimizedCode(it.rinfo()->target_cell())) { |
| 306 | objects.push_back(handle(it.rinfo()->target_cell(), isolate)); |
| 307 | } else if (mode == RelocInfo::EMBEDDED_OBJECT && |
| 308 | code->IsWeakObjectInOptimizedCode( |
| 309 | it.rinfo()->target_object())) { |
| 310 | Handle<HeapObject> object(HeapObject::cast(it.rinfo()->target_object()), |
| 311 | isolate); |
| 312 | if (object->IsMap()) { |
| 313 | maps.push_back(Handle<Map>::cast(object)); |
| 314 | } else { |
| 315 | objects.push_back(object); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | for (Handle<Map> map : maps) { |
| 321 | if (map->dependent_code()->IsEmpty(DependentCode::kWeakCodeGroup)) { |
| 322 | isolate->heap()->AddRetainedMap(map); |
| 323 | } |
| 324 | Map::AddDependentCode(map, DependentCode::kWeakCodeGroup, code); |
| 325 | } |
| 326 | for (Handle<HeapObject> object : objects) { |
| 327 | AddWeakObjectToCodeDependency(isolate, object, code); |
| 328 | } |
| 329 | code->set_can_have_weak_objects(true); |
| 330 | } |
| 331 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 332 | void CompilationJob::RecordOptimizationStats() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | Handle<JSFunction> function = info()->closure(); |
| 334 | if (!function->IsOptimized()) { |
| 335 | // Concurrent recompilation and OSR may race. Increment only once. |
| 336 | int opt_count = function->shared()->opt_count(); |
| 337 | function->shared()->set_opt_count(opt_count + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 339 | double ms_creategraph = time_taken_to_create_graph_.InMillisecondsF(); |
| 340 | double ms_optimize = time_taken_to_optimize_.InMillisecondsF(); |
| 341 | double ms_codegen = time_taken_to_codegen_.InMillisecondsF(); |
| 342 | if (FLAG_trace_opt) { |
| 343 | PrintF("[optimizing "); |
| 344 | function->ShortPrint(); |
| 345 | PrintF(" - took %0.3f, %0.3f, %0.3f ms]\n", ms_creategraph, ms_optimize, |
| 346 | ms_codegen); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 347 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | if (FLAG_trace_opt_stats) { |
| 349 | static double compilation_time = 0.0; |
| 350 | static int compiled_functions = 0; |
| 351 | static int code_size = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 352 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 353 | compilation_time += (ms_creategraph + ms_optimize + ms_codegen); |
| 354 | compiled_functions++; |
| 355 | code_size += function->shared()->SourceSize(); |
| 356 | PrintF("Compiled: %d functions with %d byte source size in %fms.\n", |
| 357 | compiled_functions, |
| 358 | code_size, |
| 359 | compilation_time); |
| 360 | } |
| 361 | if (FLAG_hydrogen_stats) { |
| 362 | isolate()->GetHStatistics()->IncrementSubtotals(time_taken_to_create_graph_, |
| 363 | time_taken_to_optimize_, |
| 364 | time_taken_to_codegen_); |
| 365 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 368 | // ---------------------------------------------------------------------------- |
| 369 | // Local helper methods that make up the compilation pipeline. |
| 370 | |
| 371 | namespace { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 372 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 373 | bool IsEvalToplevel(Handle<SharedFunctionInfo> shared) { |
| 374 | return shared->is_toplevel() && shared->script()->IsScript() && |
| 375 | Script::cast(shared->script())->compilation_type() == |
| 376 | Script::COMPILATION_TYPE_EVAL; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 379 | void RecordFunctionCompilation(Logger::LogEventsAndTags tag, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 380 | CompilationInfo* info) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 381 | // Log the code generation. If source information is available include |
| 382 | // script name and line number. Check explicitly whether logging is |
| 383 | // enabled as finding the line number is not free. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 384 | if (info->isolate()->logger()->is_logging_code_events() || |
| 385 | info->isolate()->cpu_profiler()->is_profiling()) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 386 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 387 | Handle<Script> script = info->parse_info()->script(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 388 | Handle<AbstractCode> abstract_code = |
| 389 | info->has_bytecode_array() |
| 390 | ? Handle<AbstractCode>::cast(info->bytecode_array()) |
| 391 | : Handle<AbstractCode>::cast(info->code()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 392 | if (abstract_code.is_identical_to( |
| 393 | info->isolate()->builtins()->CompileLazy())) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 394 | return; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 395 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | int line_num = Script::GetLineNumber(script, shared->start_position()) + 1; |
| 397 | int column_num = |
| 398 | Script::GetColumnNumber(script, shared->start_position()) + 1; |
| 399 | String* script_name = script->name()->IsString() |
| 400 | ? String::cast(script->name()) |
| 401 | : info->isolate()->heap()->empty_string(); |
| 402 | Logger::LogEventsAndTags log_tag = Logger::ToNativeByScript(tag, *script); |
| 403 | PROFILE(info->isolate(), |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 404 | CodeCreateEvent(log_tag, *abstract_code, *shared, script_name, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 405 | line_num, column_num)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 408 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 409 | void EnsureFeedbackVector(CompilationInfo* info) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 410 | DCHECK(info->has_shared_info()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 411 | |
| 412 | // If no type feedback vector exists, we create one now. At this point the |
| 413 | // AstNumbering pass has already run. Note the snapshot can contain outdated |
| 414 | // vectors for a different configuration, hence we also recreate a new vector |
| 415 | // when the function is not compiled (i.e. no code was serialized). |
| 416 | if (info->shared_info()->feedback_vector()->is_empty() || |
| 417 | !info->shared_info()->is_compiled()) { |
| 418 | Handle<TypeFeedbackMetadata> feedback_metadata = TypeFeedbackMetadata::New( |
| 419 | info->isolate(), info->literal()->feedback_vector_spec()); |
| 420 | Handle<TypeFeedbackVector> feedback_vector = |
| 421 | TypeFeedbackVector::New(info->isolate(), feedback_metadata); |
| 422 | info->shared_info()->set_feedback_vector(*feedback_vector); |
| 423 | } |
| 424 | |
| 425 | // It's very important that recompiles do not alter the structure of the type |
| 426 | // feedback vector. Verify that the structure fits the function literal. |
| 427 | CHECK(!info->shared_info()->feedback_vector()->metadata()->SpecDiffersFrom( |
| 428 | info->literal()->feedback_vector_spec())); |
| 429 | } |
| 430 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 431 | bool UseIgnition(CompilationInfo* info) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 432 | if (info->is_debug()) return false; |
| 433 | if (info->shared_info()->is_resumable() && !FLAG_ignition_generators) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 434 | return false; |
| 435 | } |
| 436 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 437 | // Checks whether top level functions should be passed by the filter. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 438 | if (info->shared_info()->is_toplevel()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 439 | Vector<const char> filter = CStrVector(FLAG_ignition_filter); |
| 440 | return (filter.length() == 0) || (filter.length() == 1 && filter[0] == '*'); |
| 441 | } |
| 442 | |
| 443 | // Finally respect the filter. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 444 | return info->shared_info()->PassesFilter(FLAG_ignition_filter); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 447 | int CodeAndMetadataSize(CompilationInfo* info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 448 | int size = 0; |
| 449 | if (info->has_bytecode_array()) { |
| 450 | Handle<BytecodeArray> bytecode_array = info->bytecode_array(); |
| 451 | size += bytecode_array->BytecodeArraySize(); |
| 452 | size += bytecode_array->constant_pool()->Size(); |
| 453 | size += bytecode_array->handler_table()->Size(); |
| 454 | size += bytecode_array->source_position_table()->Size(); |
| 455 | } else { |
| 456 | Handle<Code> code = info->code(); |
| 457 | size += code->CodeSize(); |
| 458 | size += code->relocation_info()->Size(); |
| 459 | size += code->deoptimization_data()->Size(); |
| 460 | size += code->handler_table()->Size(); |
| 461 | } |
| 462 | return size; |
| 463 | } |
| 464 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 465 | bool GenerateUnoptimizedCode(CompilationInfo* info) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 466 | bool success; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 467 | EnsureFeedbackVector(info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 468 | if (FLAG_validate_asm && info->scope()->asm_module()) { |
| 469 | AsmTyper typer(info->isolate(), info->zone(), *(info->script()), |
| 470 | info->literal()); |
| 471 | if (FLAG_enable_simd_asmjs) { |
| 472 | typer.set_allow_simd(true); |
| 473 | } |
| 474 | if (!typer.Validate()) { |
| 475 | DCHECK(!info->isolate()->has_pending_exception()); |
| 476 | PrintF("Validation of asm.js module failed: %s", typer.error_message()); |
| 477 | } |
| 478 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 479 | if (FLAG_ignition && UseIgnition(info)) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 480 | success = interpreter::Interpreter::MakeBytecode(info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 481 | } else { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 482 | success = FullCodeGenerator::MakeCode(info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 483 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 484 | if (success) { |
| 485 | Isolate* isolate = info->isolate(); |
| 486 | Counters* counters = isolate->counters(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 487 | // TODO(4280): Rename counters from "baseline" to "unoptimized" eventually. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 488 | counters->total_baseline_code_size()->Increment(CodeAndMetadataSize(info)); |
| 489 | counters->total_baseline_compile_count()->Increment(1); |
| 490 | } |
| 491 | return success; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 494 | bool CompileUnoptimizedCode(CompilationInfo* info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 495 | DCHECK(AllowCompilation::IsAllowed(info->isolate())); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 496 | if (!Compiler::Analyze(info->parse_info()) || |
| 497 | !GenerateUnoptimizedCode(info)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 498 | Isolate* isolate = info->isolate(); |
| 499 | if (!isolate->has_pending_exception()) isolate->StackOverflow(); |
| 500 | return false; |
| 501 | } |
| 502 | return true; |
| 503 | } |
| 504 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 505 | void InstallSharedScopeInfo(CompilationInfo* info, |
| 506 | Handle<SharedFunctionInfo> shared) { |
| 507 | Handle<ScopeInfo> scope_info = |
| 508 | ScopeInfo::Create(info->isolate(), info->zone(), info->scope()); |
| 509 | shared->set_scope_info(*scope_info); |
| 510 | } |
| 511 | |
| 512 | void InstallSharedCompilationResult(CompilationInfo* info, |
| 513 | Handle<SharedFunctionInfo> shared) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 514 | // Assert that we are not overwriting (possibly patched) debug code. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 515 | DCHECK(!shared->HasDebugInfo()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 516 | DCHECK(!info->code().is_null()); |
| 517 | shared->ReplaceCode(*info->code()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 518 | if (info->has_bytecode_array()) { |
| 519 | DCHECK(!shared->HasBytecodeArray()); // Only compiled once. |
| 520 | shared->set_bytecode_array(*info->bytecode_array()); |
| 521 | } |
| 522 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 524 | MUST_USE_RESULT MaybeHandle<Code> GetUnoptimizedCode(CompilationInfo* info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 525 | VMState<COMPILER> state(info->isolate()); |
| 526 | PostponeInterruptsScope postpone(info->isolate()); |
| 527 | |
| 528 | // Parse and update CompilationInfo with the results. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 529 | if (!Parser::ParseStatic(info->parse_info())) return MaybeHandle<Code>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 530 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 531 | DCHECK_EQ(shared->language_mode(), info->literal()->language_mode()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 532 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 533 | // Compile either unoptimized code or bytecode for the interpreter. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 534 | if (!CompileUnoptimizedCode(info)) return MaybeHandle<Code>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 535 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 536 | // Update the shared function info with the scope info. |
| 537 | InstallSharedScopeInfo(info, shared); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 538 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 539 | // Install compilation result on the shared function info |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 540 | InstallSharedCompilationResult(info, shared); |
| 541 | |
| 542 | // Record the function compilation event. |
| 543 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 544 | |
| 545 | return info->code(); |
| 546 | } |
| 547 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 548 | MUST_USE_RESULT MaybeHandle<Code> GetCodeFromOptimizedCodeMap( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 549 | Handle<JSFunction> function, BailoutId osr_ast_id) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 550 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 551 | DisallowHeapAllocation no_gc; |
| 552 | CodeAndLiterals cached = shared->SearchOptimizedCodeMap( |
| 553 | function->context()->native_context(), osr_ast_id); |
| 554 | if (cached.code != nullptr) { |
| 555 | // Caching of optimized code enabled and optimized code found. |
| 556 | if (cached.literals != nullptr) function->set_literals(cached.literals); |
| 557 | DCHECK(!cached.code->marked_for_deoptimization()); |
| 558 | DCHECK(function->shared()->is_compiled()); |
| 559 | return Handle<Code>(cached.code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 560 | } |
| 561 | return MaybeHandle<Code>(); |
| 562 | } |
| 563 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 564 | void InsertCodeIntoOptimizedCodeMap(CompilationInfo* info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 565 | Handle<Code> code = info->code(); |
| 566 | if (code->kind() != Code::OPTIMIZED_FUNCTION) return; // Nothing to do. |
| 567 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 568 | // Function context specialization folds-in the function context, |
| 569 | // so no sharing can occur. |
| 570 | if (info->is_function_context_specializing()) return; |
| 571 | // Frame specialization implies function context specialization. |
| 572 | DCHECK(!info->is_frame_specializing()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 573 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 574 | // Cache optimized context-specific code. |
| 575 | Handle<JSFunction> function = info->closure(); |
| 576 | Handle<SharedFunctionInfo> shared(function->shared()); |
| 577 | Handle<LiteralsArray> literals(function->literals()); |
| 578 | Handle<Context> native_context(function->context()->native_context()); |
| 579 | SharedFunctionInfo::AddToOptimizedCodeMap(shared, native_context, code, |
| 580 | literals, info->osr_ast_id()); |
| 581 | |
| 582 | // Do not cache (native) context-independent code compiled for OSR. |
| 583 | if (code->is_turbofanned() && info->is_osr()) return; |
| 584 | |
| 585 | // Cache optimized (native) context-independent code. |
| 586 | if (FLAG_turbo_cache_shared_code && code->is_turbofanned() && |
| 587 | !info->is_native_context_specializing()) { |
| 588 | DCHECK(!info->is_function_context_specializing()); |
| 589 | DCHECK(info->osr_ast_id().IsNone()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 590 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 591 | SharedFunctionInfo::AddSharedCodeToOptimizedCodeMap(shared, code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 595 | bool Renumber(ParseInfo* parse_info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 596 | if (!AstNumbering::Renumber(parse_info->isolate(), parse_info->zone(), |
| 597 | parse_info->literal())) { |
| 598 | return false; |
| 599 | } |
| 600 | Handle<SharedFunctionInfo> shared_info = parse_info->shared_info(); |
| 601 | if (!shared_info.is_null()) { |
| 602 | FunctionLiteral* lit = parse_info->literal(); |
| 603 | shared_info->set_ast_node_count(lit->ast_node_count()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 604 | if (lit->dont_optimize_reason() != kNoReason) { |
| 605 | shared_info->DisableOptimization(lit->dont_optimize_reason()); |
| 606 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 607 | shared_info->set_dont_crankshaft( |
| 608 | shared_info->dont_crankshaft() || |
| 609 | (lit->flags() & AstProperties::kDontCrankshaft)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 610 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 611 | return true; |
| 612 | } |
| 613 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 614 | bool UseTurboFan(Handle<SharedFunctionInfo> shared) { |
| 615 | bool optimization_disabled = shared->optimization_disabled(); |
| 616 | bool dont_crankshaft = shared->dont_crankshaft(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 617 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 618 | // Check the enabling conditions for Turbofan. |
| 619 | // 1. "use asm" code. |
| 620 | bool is_turbofanable_asm = |
| 621 | FLAG_turbo_asm && shared->asm_function() && !optimization_disabled; |
| 622 | |
| 623 | // 2. Fallback for features unsupported by Crankshaft. |
| 624 | bool is_unsupported_by_crankshaft_but_turbofanable = |
| 625 | dont_crankshaft && strcmp(FLAG_turbo_filter, "~~") == 0 && |
| 626 | !optimization_disabled; |
| 627 | |
| 628 | // 3. Explicitly enabled by the command-line filter. |
| 629 | bool passes_turbo_filter = shared->PassesFilter(FLAG_turbo_filter); |
| 630 | |
| 631 | return is_turbofanable_asm || is_unsupported_by_crankshaft_but_turbofanable || |
| 632 | passes_turbo_filter; |
| 633 | } |
| 634 | |
| 635 | bool GetOptimizedCodeNow(CompilationJob* job) { |
| 636 | CompilationInfo* info = job->info(); |
| 637 | Isolate* isolate = info->isolate(); |
| 638 | |
| 639 | // Parsing is not required when optimizing from existing bytecode. |
| 640 | if (!info->is_optimizing_from_bytecode()) { |
| 641 | if (!Compiler::ParseAndAnalyze(info->parse_info())) return false; |
| 642 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 643 | |
| 644 | TimerEventScope<TimerEventRecompileSynchronous> timer(isolate); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 645 | TRACE_EVENT0("v8", "V8.RecompileSynchronous"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 646 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 647 | if (job->CreateGraph() != CompilationJob::SUCCEEDED || |
| 648 | job->OptimizeGraph() != CompilationJob::SUCCEEDED || |
| 649 | job->GenerateCode() != CompilationJob::SUCCEEDED) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 650 | if (FLAG_trace_opt) { |
| 651 | PrintF("[aborted optimizing "); |
| 652 | info->closure()->ShortPrint(); |
| 653 | PrintF(" because: %s]\n", GetBailoutReason(info->bailout_reason())); |
| 654 | } |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | // Success! |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 659 | job->RecordOptimizationStats(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 660 | DCHECK(!isolate->has_pending_exception()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 661 | InsertCodeIntoOptimizedCodeMap(info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 662 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 666 | bool GetOptimizedCodeLater(CompilationJob* job) { |
| 667 | CompilationInfo* info = job->info(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 668 | Isolate* isolate = info->isolate(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 669 | |
| 670 | if (!isolate->optimizing_compile_dispatcher()->IsQueueAvailable()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 671 | if (FLAG_trace_concurrent_recompilation) { |
| 672 | PrintF(" ** Compilation queue full, will retry optimizing "); |
| 673 | info->closure()->ShortPrint(); |
| 674 | PrintF(" later.\n"); |
| 675 | } |
| 676 | return false; |
| 677 | } |
| 678 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 679 | // All handles below this point will be allocated in a deferred handle scope |
| 680 | // that is detached and handed off to the background thread when we return. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 681 | CompilationHandleScope handle_scope(info); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 682 | |
| 683 | // Parsing is not required when optimizing from existing bytecode. |
| 684 | if (!info->is_optimizing_from_bytecode()) { |
| 685 | if (!Compiler::ParseAndAnalyze(info->parse_info())) return false; |
| 686 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 687 | |
| 688 | // Reopen handles in the new CompilationHandleScope. |
| 689 | info->ReopenHandlesInNewHandleScope(); |
| 690 | info->parse_info()->ReopenHandlesInNewHandleScope(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 691 | |
| 692 | TimerEventScope<TimerEventRecompileSynchronous> timer(info->isolate()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 693 | TRACE_EVENT0("v8", "V8.RecompileSynchronous"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 694 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 695 | if (job->CreateGraph() != CompilationJob::SUCCEEDED) return false; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 696 | isolate->optimizing_compile_dispatcher()->QueueForOptimization(job); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 697 | |
| 698 | if (FLAG_trace_concurrent_recompilation) { |
| 699 | PrintF(" ** Queued "); |
| 700 | info->closure()->ShortPrint(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 701 | PrintF(" for concurrent optimization.\n"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 702 | } |
| 703 | return true; |
| 704 | } |
| 705 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 706 | MaybeHandle<Code> GetOptimizedCode(Handle<JSFunction> function, |
| 707 | Compiler::ConcurrencyMode mode, |
| 708 | BailoutId osr_ast_id = BailoutId::None(), |
| 709 | JavaScriptFrame* osr_frame = nullptr) { |
| 710 | Isolate* isolate = function->GetIsolate(); |
| 711 | Handle<SharedFunctionInfo> shared(function->shared(), isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 712 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 713 | Handle<Code> cached_code; |
| 714 | if (GetCodeFromOptimizedCodeMap(function, osr_ast_id) |
| 715 | .ToHandle(&cached_code)) { |
| 716 | if (FLAG_trace_opt) { |
| 717 | PrintF("[found optimized code for "); |
| 718 | function->ShortPrint(); |
| 719 | if (!osr_ast_id.IsNone()) { |
| 720 | PrintF(" at OSR AST id %d", osr_ast_id.ToInt()); |
| 721 | } |
| 722 | PrintF("]\n"); |
| 723 | } |
| 724 | return cached_code; |
| 725 | } |
| 726 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 727 | // Reset profiler ticks, function is no longer considered hot. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 728 | if (shared->is_compiled()) { |
| 729 | shared->code()->set_profiler_ticks(0); |
| 730 | } |
| 731 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 732 | VMState<COMPILER> state(isolate); |
| 733 | DCHECK(!isolate->has_pending_exception()); |
| 734 | PostponeInterruptsScope postpone(isolate); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 735 | bool use_turbofan = UseTurboFan(shared); |
| 736 | base::SmartPointer<CompilationJob> job( |
| 737 | use_turbofan ? compiler::Pipeline::NewCompilationJob(function) |
| 738 | : new HCompilationJob(function)); |
| 739 | CompilationInfo* info = job->info(); |
| 740 | ParseInfo* parse_info = info->parse_info(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 741 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 742 | info->SetOptimizingForOsr(osr_ast_id, osr_frame); |
| 743 | |
| 744 | // Do not use Crankshaft/TurboFan if we need to be able to set break points. |
| 745 | if (info->shared_info()->HasDebugInfo()) { |
| 746 | info->AbortOptimization(kFunctionBeingDebugged); |
| 747 | return MaybeHandle<Code>(); |
| 748 | } |
| 749 | |
| 750 | // Limit the number of times we try to optimize functions. |
| 751 | const int kMaxOptCount = |
| 752 | FLAG_deopt_every_n_times == 0 ? FLAG_max_opt_count : 1000; |
| 753 | if (info->shared_info()->opt_count() > kMaxOptCount) { |
| 754 | info->AbortOptimization(kOptimizedTooManyTimes); |
| 755 | return MaybeHandle<Code>(); |
| 756 | } |
| 757 | |
| 758 | CanonicalHandleScope canonical(isolate); |
| 759 | TimerEventScope<TimerEventOptimizeCode> optimize_code_timer(isolate); |
| 760 | TRACE_EVENT0("v8", "V8.OptimizeCode"); |
| 761 | |
| 762 | // TurboFan can optimize directly from existing bytecode. |
| 763 | if (FLAG_turbo_from_bytecode && use_turbofan && |
| 764 | info->shared_info()->HasBytecodeArray()) { |
| 765 | info->MarkAsOptimizeFromBytecode(); |
| 766 | } |
| 767 | |
| 768 | if (IsEvalToplevel(shared)) { |
| 769 | parse_info->set_eval(); |
| 770 | if (function->context()->IsNativeContext()) parse_info->set_global(); |
| 771 | parse_info->set_toplevel(); |
| 772 | parse_info->set_allow_lazy_parsing(false); |
| 773 | parse_info->set_lazy(false); |
| 774 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 775 | |
| 776 | if (mode == Compiler::CONCURRENT) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 777 | if (GetOptimizedCodeLater(job.get())) { |
| 778 | job.Detach(); // The background recompile job owns this now. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 779 | return isolate->builtins()->InOptimizationQueue(); |
| 780 | } |
| 781 | } else { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 782 | if (GetOptimizedCodeNow(job.get())) return info->code(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | if (isolate->has_pending_exception()) isolate->clear_pending_exception(); |
| 786 | return MaybeHandle<Code>(); |
| 787 | } |
| 788 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 789 | class InterpreterActivationsFinder : public ThreadVisitor, |
| 790 | public OptimizedFunctionVisitor { |
| 791 | public: |
| 792 | SharedFunctionInfo* shared_; |
| 793 | bool has_activations_; |
| 794 | |
| 795 | explicit InterpreterActivationsFinder(SharedFunctionInfo* shared) |
| 796 | : shared_(shared), has_activations_(false) {} |
| 797 | |
| 798 | void VisitThread(Isolate* isolate, ThreadLocalTop* top) { |
| 799 | JavaScriptFrameIterator it(isolate, top); |
| 800 | for (; !it.done() && !has_activations_; it.Advance()) { |
| 801 | JavaScriptFrame* frame = it.frame(); |
| 802 | if (!frame->is_interpreted()) continue; |
| 803 | if (frame->function()->shared() == shared_) has_activations_ = true; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | void VisitFunction(JSFunction* function) { |
| 808 | if (function->Inlines(shared_)) has_activations_ = true; |
| 809 | } |
| 810 | |
| 811 | void EnterContext(Context* context) {} |
| 812 | void LeaveContext(Context* context) {} |
| 813 | }; |
| 814 | |
| 815 | bool HasInterpreterActivations(Isolate* isolate, SharedFunctionInfo* shared) { |
| 816 | InterpreterActivationsFinder activations_finder(shared); |
| 817 | activations_finder.VisitThread(isolate, isolate->thread_local_top()); |
| 818 | isolate->thread_manager()->IterateArchivedThreads(&activations_finder); |
| 819 | if (FLAG_turbo_from_bytecode) { |
| 820 | // If we are able to optimize functions directly from bytecode, then there |
| 821 | // might be optimized functions that rely on bytecode being around. We need |
| 822 | // to prevent switching the given function to baseline code in those cases. |
| 823 | Deoptimizer::VisitAllOptimizedFunctions(isolate, &activations_finder); |
| 824 | } |
| 825 | return activations_finder.has_activations_; |
| 826 | } |
| 827 | |
| 828 | MaybeHandle<Code> GetBaselineCode(Handle<JSFunction> function) { |
| 829 | Isolate* isolate = function->GetIsolate(); |
| 830 | VMState<COMPILER> state(isolate); |
| 831 | PostponeInterruptsScope postpone(isolate); |
| 832 | Zone zone(isolate->allocator()); |
| 833 | ParseInfo parse_info(&zone, function); |
| 834 | CompilationInfo info(&parse_info, function); |
| 835 | |
| 836 | // Reset profiler ticks, function is no longer considered hot. |
| 837 | if (function->shared()->HasBytecodeArray()) { |
| 838 | function->shared()->set_profiler_ticks(0); |
| 839 | } |
| 840 | |
| 841 | // Nothing left to do if the function already has baseline code. |
| 842 | if (function->shared()->code()->kind() == Code::FUNCTION) { |
| 843 | return Handle<Code>(function->shared()->code()); |
| 844 | } |
| 845 | |
| 846 | // We do not switch to baseline code when the debugger might have created a |
| 847 | // copy of the bytecode with break slots to be able to set break points. |
| 848 | if (function->shared()->HasDebugInfo()) { |
| 849 | return MaybeHandle<Code>(); |
| 850 | } |
| 851 | |
| 852 | // TODO(4280): For now we do not switch generators to baseline code because |
| 853 | // there might be suspended activations stored in generator objects on the |
| 854 | // heap. We could eventually go directly to TurboFan in this case. |
| 855 | if (function->shared()->is_generator()) { |
| 856 | return MaybeHandle<Code>(); |
| 857 | } |
| 858 | |
| 859 | // TODO(4280): For now we disable switching to baseline code in the presence |
| 860 | // of interpreter activations of the given function. The reasons are: |
| 861 | // 1) The debugger assumes each function is either full-code or bytecode. |
| 862 | // 2) The underlying bytecode is cleared below, breaking stack unwinding. |
| 863 | if (HasInterpreterActivations(isolate, function->shared())) { |
| 864 | if (FLAG_trace_opt) { |
| 865 | OFStream os(stdout); |
| 866 | os << "[unable to switch " << Brief(*function) << " due to activations]" |
| 867 | << std::endl; |
| 868 | } |
| 869 | return MaybeHandle<Code>(); |
| 870 | } |
| 871 | |
| 872 | if (FLAG_trace_opt) { |
| 873 | OFStream os(stdout); |
| 874 | os << "[switching method " << Brief(*function) << " to baseline code]" |
| 875 | << std::endl; |
| 876 | } |
| 877 | |
| 878 | // Parse and update CompilationInfo with the results. |
| 879 | if (!Parser::ParseStatic(info.parse_info())) return MaybeHandle<Code>(); |
| 880 | Handle<SharedFunctionInfo> shared = info.shared_info(); |
| 881 | DCHECK_EQ(shared->language_mode(), info.literal()->language_mode()); |
| 882 | |
| 883 | // Compile baseline code using the full code generator. |
| 884 | if (!Compiler::Analyze(info.parse_info()) || |
| 885 | !FullCodeGenerator::MakeCode(&info)) { |
| 886 | if (!isolate->has_pending_exception()) isolate->StackOverflow(); |
| 887 | return MaybeHandle<Code>(); |
| 888 | } |
| 889 | |
| 890 | // TODO(4280): For now we play it safe and remove the bytecode array when we |
| 891 | // switch to baseline code. We might consider keeping around the bytecode so |
| 892 | // that it can be used as the "source of truth" eventually. |
| 893 | shared->ClearBytecodeArray(); |
| 894 | |
| 895 | // Update the shared function info with the scope info. |
| 896 | InstallSharedScopeInfo(&info, shared); |
| 897 | |
| 898 | // Install compilation result on the shared function info |
| 899 | InstallSharedCompilationResult(&info, shared); |
| 900 | |
| 901 | // Record the function compilation event. |
| 902 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, &info); |
| 903 | |
| 904 | return info.code(); |
| 905 | } |
| 906 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 907 | MaybeHandle<Code> GetLazyCode(Handle<JSFunction> function) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 908 | Isolate* isolate = function->GetIsolate(); |
| 909 | DCHECK(!isolate->has_pending_exception()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 910 | DCHECK(!function->is_compiled()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 911 | TimerEventScope<TimerEventCompileCode> compile_timer(isolate); |
| 912 | TRACE_EVENT0("v8", "V8.CompileCode"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 913 | AggregatedHistogramTimerScope timer(isolate->counters()->compile_lazy()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 914 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 915 | if (FLAG_turbo_cache_shared_code) { |
| 916 | Handle<Code> cached_code; |
| 917 | if (GetCodeFromOptimizedCodeMap(function, BailoutId::None()) |
| 918 | .ToHandle(&cached_code)) { |
| 919 | if (FLAG_trace_opt) { |
| 920 | PrintF("[found optimized code for "); |
| 921 | function->ShortPrint(); |
| 922 | PrintF(" during unoptimized compile]\n"); |
| 923 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 924 | DCHECK(function->shared()->is_compiled()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 925 | return cached_code; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 926 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | if (function->shared()->is_compiled()) { |
| 930 | return Handle<Code>(function->shared()->code()); |
| 931 | } |
| 932 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 933 | Zone zone(isolate->allocator()); |
| 934 | ParseInfo parse_info(&zone, function); |
| 935 | CompilationInfo info(&parse_info, function); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 936 | Handle<Code> result; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 937 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, GetUnoptimizedCode(&info), Code); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 938 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 939 | if (FLAG_always_opt) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 940 | Handle<Code> opt_code; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 941 | if (GetOptimizedCode(function, Compiler::NOT_CONCURRENT) |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 942 | .ToHandle(&opt_code)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 943 | result = opt_code; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return result; |
| 948 | } |
| 949 | |
| 950 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 951 | Handle<SharedFunctionInfo> NewSharedFunctionInfoForLiteral( |
| 952 | Isolate* isolate, FunctionLiteral* literal, Handle<Script> script) { |
| 953 | Handle<Code> code = isolate->builtins()->CompileLazy(); |
| 954 | Handle<ScopeInfo> scope_info = handle(ScopeInfo::Empty(isolate)); |
| 955 | Handle<SharedFunctionInfo> result = isolate->factory()->NewSharedFunctionInfo( |
| 956 | literal->name(), literal->materialized_literal_count(), literal->kind(), |
| 957 | code, scope_info); |
| 958 | SharedFunctionInfo::InitFromFunctionLiteral(result, literal); |
| 959 | SharedFunctionInfo::SetScript(result, script); |
| 960 | return result; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 963 | Handle<SharedFunctionInfo> CompileToplevel(CompilationInfo* info) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 964 | Isolate* isolate = info->isolate(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 965 | TimerEventScope<TimerEventCompileCode> timer(isolate); |
| 966 | TRACE_EVENT0("v8", "V8.CompileCode"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 967 | PostponeInterruptsScope postpone(isolate); |
| 968 | DCHECK(!isolate->native_context().is_null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 969 | ParseInfo* parse_info = info->parse_info(); |
| 970 | Handle<Script> script = parse_info->script(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 971 | |
| 972 | // TODO(svenpanne) Obscure place for this, perhaps move to OnBeforeCompile? |
| 973 | FixedArray* array = isolate->native_context()->embedder_data(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 974 | script->set_context_data(array->get(v8::Context::kDebugIdIndex)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 975 | |
| 976 | isolate->debug()->OnBeforeCompile(script); |
| 977 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 978 | DCHECK(parse_info->is_eval() || parse_info->is_global() || |
| 979 | parse_info->is_module()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 980 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 981 | parse_info->set_toplevel(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 982 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 983 | Handle<SharedFunctionInfo> result; |
| 984 | |
| 985 | { VMState<COMPILER> state(info->isolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 986 | if (parse_info->literal() == NULL) { |
| 987 | // Parse the script if needed (if it's already parsed, literal() is |
| 988 | // non-NULL). If compiling for debugging, we may eagerly compile inner |
| 989 | // functions, so do not parse lazily in that case. |
| 990 | ScriptCompiler::CompileOptions options = parse_info->compile_options(); |
| 991 | bool parse_allow_lazy = (options == ScriptCompiler::kConsumeParserCache || |
| 992 | String::cast(script->source())->length() > |
| 993 | FLAG_min_preparse_length) && |
| 994 | !info->is_debug(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 995 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 996 | // Consider parsing eagerly when targeting the code cache. |
| 997 | parse_allow_lazy &= !(FLAG_serialize_eager && info->will_serialize()); |
| 998 | |
| 999 | // Consider parsing eagerly when targeting Ignition. |
| 1000 | parse_allow_lazy &= !(FLAG_ignition && FLAG_ignition_eager && |
| 1001 | !isolate->serializer_enabled()); |
| 1002 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1003 | parse_info->set_allow_lazy_parsing(parse_allow_lazy); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1004 | if (!parse_allow_lazy && |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1005 | (options == ScriptCompiler::kProduceParserCache || |
| 1006 | options == ScriptCompiler::kConsumeParserCache)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1007 | // We are going to parse eagerly, but we either 1) have cached data |
| 1008 | // produced by lazy parsing or 2) are asked to generate cached data. |
| 1009 | // Eager parsing cannot benefit from cached data, and producing cached |
| 1010 | // data while parsing eagerly is not implemented. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1011 | parse_info->set_cached_data(nullptr); |
| 1012 | parse_info->set_compile_options(ScriptCompiler::kNoCompileOptions); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1013 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1014 | if (!Parser::ParseStatic(parse_info)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1015 | return Handle<SharedFunctionInfo>::null(); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1019 | DCHECK(!info->is_debug() || !parse_info->allow_lazy_parsing()); |
| 1020 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1021 | FunctionLiteral* lit = parse_info->literal(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Measure how long it takes to do the compilation; only take the |
| 1024 | // rest of the function into account to avoid overlap with the |
| 1025 | // parsing statistics. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1026 | RuntimeCallTimerScope runtimeTimer( |
| 1027 | isolate, parse_info->is_eval() ? &RuntimeCallStats::CompileEval |
| 1028 | : &RuntimeCallStats::Compile); |
| 1029 | HistogramTimer* rate = parse_info->is_eval() |
| 1030 | ? info->isolate()->counters()->compile_eval() |
| 1031 | : info->isolate()->counters()->compile(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1032 | HistogramTimerScope timer(rate); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1033 | TRACE_EVENT0("v8", parse_info->is_eval() ? "V8.CompileEval" : "V8.Compile"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1034 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1035 | // Allocate a shared function info object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1036 | DCHECK_EQ(RelocInfo::kNoPosition, lit->function_token_position()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1037 | result = NewSharedFunctionInfoForLiteral(isolate, lit, script); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1038 | result->set_is_toplevel(true); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1039 | if (parse_info->is_eval()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1040 | // Eval scripts cannot be (re-)compiled without context. |
| 1041 | result->set_allows_lazy_compilation_without_context(false); |
| 1042 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1043 | parse_info->set_shared_info(result); |
| 1044 | |
| 1045 | // Compile the code. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1046 | if (!CompileUnoptimizedCode(info)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1047 | return Handle<SharedFunctionInfo>::null(); |
| 1048 | } |
| 1049 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1050 | // Update the shared function info with the scope info. |
| 1051 | InstallSharedScopeInfo(info, result); |
| 1052 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1053 | // Install compilation result on the shared function info |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1054 | InstallSharedCompilationResult(info, result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1055 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1056 | Handle<String> script_name = |
| 1057 | script->name()->IsString() |
| 1058 | ? Handle<String>(String::cast(script->name())) |
| 1059 | : isolate->factory()->empty_string(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1060 | Logger::LogEventsAndTags log_tag = |
| 1061 | parse_info->is_eval() |
| 1062 | ? Logger::EVAL_TAG |
| 1063 | : Logger::ToNativeByScript(Logger::SCRIPT_TAG, *script); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1064 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1065 | PROFILE(isolate, CodeCreateEvent(log_tag, result->abstract_code(), *result, |
| 1066 | *script_name)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1067 | |
| 1068 | if (!script.is_null()) |
| 1069 | script->set_compilation_state(Script::COMPILATION_STATE_COMPILED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1072 | return result; |
| 1073 | } |
| 1074 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1075 | } // namespace |
| 1076 | |
| 1077 | // ---------------------------------------------------------------------------- |
| 1078 | // Implementation of Compiler |
| 1079 | |
| 1080 | bool Compiler::Analyze(ParseInfo* info) { |
| 1081 | DCHECK_NOT_NULL(info->literal()); |
| 1082 | if (!Rewriter::Rewrite(info)) return false; |
| 1083 | if (!Scope::Analyze(info)) return false; |
| 1084 | if (!Renumber(info)) return false; |
| 1085 | DCHECK_NOT_NULL(info->scope()); |
| 1086 | return true; |
| 1087 | } |
| 1088 | |
| 1089 | bool Compiler::ParseAndAnalyze(ParseInfo* info) { |
| 1090 | if (!Parser::ParseStatic(info)) return false; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1091 | if (!Compiler::Analyze(info)) return false; |
| 1092 | DCHECK_NOT_NULL(info->literal()); |
| 1093 | DCHECK_NOT_NULL(info->scope()); |
| 1094 | return true; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | bool Compiler::Compile(Handle<JSFunction> function, ClearExceptionFlag flag) { |
| 1098 | if (function->is_compiled()) return true; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1099 | Isolate* isolate = function->GetIsolate(); |
| 1100 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1101 | |
| 1102 | // Start a compilation. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1103 | Handle<Code> code; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1104 | if (!GetLazyCode(function).ToHandle(&code)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1105 | if (flag == CLEAR_EXCEPTION) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1106 | isolate->clear_pending_exception(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1107 | } |
| 1108 | return false; |
| 1109 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1110 | |
| 1111 | // Install code on closure. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1112 | function->ReplaceCode(*code); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1113 | |
| 1114 | // Check postconditions on success. |
| 1115 | DCHECK(!isolate->has_pending_exception()); |
| 1116 | DCHECK(function->shared()->is_compiled()); |
| 1117 | DCHECK(function->is_compiled()); |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
| 1121 | bool Compiler::CompileBaseline(Handle<JSFunction> function) { |
| 1122 | Isolate* isolate = function->GetIsolate(); |
| 1123 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1124 | |
| 1125 | // Start a compilation. |
| 1126 | Handle<Code> code; |
| 1127 | if (!GetBaselineCode(function).ToHandle(&code)) { |
| 1128 | // Baseline generation failed, get unoptimized code. |
| 1129 | DCHECK(function->shared()->is_compiled()); |
| 1130 | code = handle(function->shared()->code()); |
| 1131 | isolate->clear_pending_exception(); |
| 1132 | } |
| 1133 | |
| 1134 | // Install code on closure. |
| 1135 | function->ReplaceCode(*code); |
| 1136 | |
| 1137 | // Check postconditions on success. |
| 1138 | DCHECK(!isolate->has_pending_exception()); |
| 1139 | DCHECK(function->shared()->is_compiled()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1140 | DCHECK(function->is_compiled()); |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | bool Compiler::CompileOptimized(Handle<JSFunction> function, |
| 1145 | ConcurrencyMode mode) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1146 | if (function->IsOptimized()) return true; |
| 1147 | Isolate* isolate = function->GetIsolate(); |
| 1148 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1149 | |
| 1150 | // Start a compilation. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1151 | Handle<Code> code; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1152 | if (!GetOptimizedCode(function, mode).ToHandle(&code)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1153 | // Optimization failed, get unoptimized code. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1154 | DCHECK(!isolate->has_pending_exception()); |
| 1155 | if (function->shared()->is_compiled()) { |
| 1156 | code = handle(function->shared()->code(), isolate); |
| 1157 | } else { |
| 1158 | Zone zone(isolate->allocator()); |
| 1159 | ParseInfo parse_info(&zone, function); |
| 1160 | CompilationInfo info(&parse_info, function); |
| 1161 | if (!GetUnoptimizedCode(&info).ToHandle(&code)) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1162 | return false; |
| 1163 | } |
| 1164 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1165 | } |
| 1166 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1167 | // Install code on closure. |
| 1168 | function->ReplaceCode(*code); |
| 1169 | |
| 1170 | // Check postconditions on success. |
| 1171 | DCHECK(!isolate->has_pending_exception()); |
| 1172 | DCHECK(function->shared()->is_compiled()); |
| 1173 | DCHECK(function->is_compiled()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1174 | return true; |
| 1175 | } |
| 1176 | |
| 1177 | bool Compiler::CompileDebugCode(Handle<JSFunction> function) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1178 | Isolate* isolate = function->GetIsolate(); |
| 1179 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1180 | |
| 1181 | // Start a compilation. |
| 1182 | Zone zone(isolate->allocator()); |
| 1183 | ParseInfo parse_info(&zone, function); |
| 1184 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
| 1185 | if (IsEvalToplevel(handle(function->shared()))) { |
| 1186 | parse_info.set_eval(); |
| 1187 | if (function->context()->IsNativeContext()) parse_info.set_global(); |
| 1188 | parse_info.set_toplevel(); |
| 1189 | parse_info.set_allow_lazy_parsing(false); |
| 1190 | parse_info.set_lazy(false); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1191 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1192 | info.MarkAsDebug(); |
| 1193 | if (GetUnoptimizedCode(&info).is_null()) { |
| 1194 | isolate->clear_pending_exception(); |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | // Check postconditions on success. |
| 1199 | DCHECK(!isolate->has_pending_exception()); |
| 1200 | DCHECK(function->shared()->is_compiled()); |
| 1201 | DCHECK(function->shared()->HasDebugCode()); |
| 1202 | return true; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | bool Compiler::CompileDebugCode(Handle<SharedFunctionInfo> shared) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1206 | Isolate* isolate = shared->GetIsolate(); |
| 1207 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1208 | |
| 1209 | // Start a compilation. |
| 1210 | Zone zone(isolate->allocator()); |
| 1211 | ParseInfo parse_info(&zone, shared); |
| 1212 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1213 | DCHECK(shared->allows_lazy_compilation_without_context()); |
| 1214 | DCHECK(!IsEvalToplevel(shared)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1215 | info.MarkAsDebug(); |
| 1216 | if (GetUnoptimizedCode(&info).is_null()) { |
| 1217 | isolate->clear_pending_exception(); |
| 1218 | return false; |
| 1219 | } |
| 1220 | |
| 1221 | // Check postconditions on success. |
| 1222 | DCHECK(!isolate->has_pending_exception()); |
| 1223 | DCHECK(shared->is_compiled()); |
| 1224 | DCHECK(shared->HasDebugCode()); |
| 1225 | return true; |
| 1226 | } |
| 1227 | |
| 1228 | MaybeHandle<JSArray> Compiler::CompileForLiveEdit(Handle<Script> script) { |
| 1229 | Isolate* isolate = script->GetIsolate(); |
| 1230 | DCHECK(AllowCompilation::IsAllowed(isolate)); |
| 1231 | |
| 1232 | // In order to ensure that live edit function info collection finds the newly |
| 1233 | // generated shared function infos, clear the script's list temporarily |
| 1234 | // and restore it at the end of this method. |
| 1235 | Handle<Object> old_function_infos(script->shared_function_infos(), isolate); |
| 1236 | script->set_shared_function_infos(Smi::FromInt(0)); |
| 1237 | |
| 1238 | // Start a compilation. |
| 1239 | Zone zone(isolate->allocator()); |
| 1240 | ParseInfo parse_info(&zone, script); |
| 1241 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
| 1242 | parse_info.set_global(); |
| 1243 | info.MarkAsDebug(); |
| 1244 | |
| 1245 | // TODO(635): support extensions. |
| 1246 | const bool compilation_succeeded = !CompileToplevel(&info).is_null(); |
| 1247 | Handle<JSArray> infos; |
| 1248 | if (compilation_succeeded) { |
| 1249 | // Check postconditions on success. |
| 1250 | DCHECK(!isolate->has_pending_exception()); |
| 1251 | infos = LiveEditFunctionTracker::Collect(parse_info.literal(), script, |
| 1252 | &zone, isolate); |
| 1253 | } |
| 1254 | |
| 1255 | // Restore the original function info list in order to remain side-effect |
| 1256 | // free as much as possible, since some code expects the old shared function |
| 1257 | // infos to stick around. |
| 1258 | script->set_shared_function_infos(*old_function_infos); |
| 1259 | |
| 1260 | return infos; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | // TODO(turbofan): In the future, unoptimized code with deopt support could |
| 1264 | // be generated lazily once deopt is triggered. |
| 1265 | bool Compiler::EnsureDeoptimizationSupport(CompilationInfo* info) { |
| 1266 | DCHECK_NOT_NULL(info->literal()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1267 | DCHECK_NOT_NULL(info->scope()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1268 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 1269 | if (!shared->has_deoptimization_support()) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1270 | Zone zone(info->isolate()->allocator()); |
| 1271 | CompilationInfo unoptimized(info->parse_info(), info->closure()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1272 | unoptimized.EnableDeoptimizationSupport(); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1273 | |
| 1274 | // TODO(4280): For now we do not switch generators to baseline code because |
| 1275 | // there might be suspended activations stored in generator objects on the |
| 1276 | // heap. We could eventually go directly to TurboFan in this case. |
| 1277 | if (shared->is_generator()) return false; |
| 1278 | |
| 1279 | // TODO(4280): For now we disable switching to baseline code in the presence |
| 1280 | // of interpreter activations of the given function. The reasons are: |
| 1281 | // 1) The debugger assumes each function is either full-code or bytecode. |
| 1282 | // 2) The underlying bytecode is cleared below, breaking stack unwinding. |
| 1283 | // The expensive check for activations only needs to be done when the given |
| 1284 | // function has bytecode, otherwise we can be sure there are no activations. |
| 1285 | if (shared->HasBytecodeArray() && |
| 1286 | HasInterpreterActivations(info->isolate(), *shared)) { |
| 1287 | return false; |
| 1288 | } |
| 1289 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1290 | // If the current code has reloc info for serialization, also include |
| 1291 | // reloc info for serialization for the new code, so that deopt support |
| 1292 | // can be added without losing IC state. |
| 1293 | if (shared->code()->kind() == Code::FUNCTION && |
| 1294 | shared->code()->has_reloc_info_for_serialization()) { |
| 1295 | unoptimized.PrepareForSerializing(); |
| 1296 | } |
| 1297 | EnsureFeedbackVector(&unoptimized); |
| 1298 | if (!FullCodeGenerator::MakeCode(&unoptimized)) return false; |
| 1299 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1300 | // TODO(4280): For now we play it safe and remove the bytecode array when we |
| 1301 | // switch to baseline code. We might consider keeping around the bytecode so |
| 1302 | // that it can be used as the "source of truth" eventually. |
| 1303 | shared->ClearBytecodeArray(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1304 | |
| 1305 | // The scope info might not have been set if a lazily compiled |
| 1306 | // function is inlined before being called for the first time. |
| 1307 | if (shared->scope_info() == ScopeInfo::Empty(info->isolate())) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1308 | InstallSharedScopeInfo(info, shared); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1309 | } |
| 1310 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1311 | // Install compilation result on the shared function info |
| 1312 | shared->EnableDeoptimizationSupport(*unoptimized.code()); |
| 1313 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1314 | // The existing unoptimized code was replaced with the new one. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1315 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, &unoptimized); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1316 | } |
| 1317 | return true; |
| 1318 | } |
| 1319 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1320 | MaybeHandle<JSFunction> Compiler::GetFunctionFromEval( |
| 1321 | Handle<String> source, Handle<SharedFunctionInfo> outer_info, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1322 | Handle<Context> context, LanguageMode language_mode, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1323 | ParseRestriction restriction, int eval_scope_position, int eval_position, |
| 1324 | int line_offset, int column_offset, Handle<Object> script_name, |
| 1325 | ScriptOriginOptions options) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1326 | Isolate* isolate = source->GetIsolate(); |
| 1327 | int source_length = source->length(); |
| 1328 | isolate->counters()->total_eval_size()->Increment(source_length); |
| 1329 | isolate->counters()->total_compile_size()->Increment(source_length); |
| 1330 | |
| 1331 | CompilationCache* compilation_cache = isolate->compilation_cache(); |
| 1332 | MaybeHandle<SharedFunctionInfo> maybe_shared_info = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1333 | compilation_cache->LookupEval(source, outer_info, context, language_mode, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1334 | eval_scope_position); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1335 | Handle<SharedFunctionInfo> shared_info; |
| 1336 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1337 | Handle<Script> script; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1338 | if (!maybe_shared_info.ToHandle(&shared_info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1339 | script = isolate->factory()->NewScript(source); |
| 1340 | if (!script_name.is_null()) { |
| 1341 | script->set_name(*script_name); |
| 1342 | script->set_line_offset(line_offset); |
| 1343 | script->set_column_offset(column_offset); |
| 1344 | } |
| 1345 | script->set_origin_options(options); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1346 | script->set_compilation_type(Script::COMPILATION_TYPE_EVAL); |
| 1347 | Script::SetEvalOrigin(script, outer_info, eval_position); |
| 1348 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1349 | Zone zone(isolate->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1350 | ParseInfo parse_info(&zone, script); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1351 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1352 | parse_info.set_eval(); |
| 1353 | if (context->IsNativeContext()) parse_info.set_global(); |
| 1354 | parse_info.set_language_mode(language_mode); |
| 1355 | parse_info.set_parse_restriction(restriction); |
| 1356 | parse_info.set_context(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1357 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1358 | shared_info = CompileToplevel(&info); |
| 1359 | |
| 1360 | if (shared_info.is_null()) { |
| 1361 | return MaybeHandle<JSFunction>(); |
| 1362 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1363 | // If caller is strict mode, the result must be in strict mode as well. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1364 | DCHECK(is_sloppy(language_mode) || |
| 1365 | is_strict(shared_info->language_mode())); |
| 1366 | compilation_cache->PutEval(source, outer_info, context, shared_info, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1367 | eval_scope_position); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1368 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1371 | Handle<JSFunction> result = |
| 1372 | isolate->factory()->NewFunctionFromSharedFunctionInfo( |
| 1373 | shared_info, context, NOT_TENURED); |
| 1374 | |
| 1375 | // OnAfterCompile has to be called after we create the JSFunction, which we |
| 1376 | // may require to recompile the eval for debugging, if we find a function |
| 1377 | // that contains break points in the eval script. |
| 1378 | isolate->debug()->OnAfterCompile(script); |
| 1379 | |
| 1380 | return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1383 | Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1384 | Handle<String> source, Handle<Object> script_name, int line_offset, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1385 | int column_offset, ScriptOriginOptions resource_options, |
| 1386 | Handle<Object> source_map_url, Handle<Context> context, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1387 | v8::Extension* extension, ScriptData** cached_data, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1388 | ScriptCompiler::CompileOptions compile_options, NativesFlag natives, |
| 1389 | bool is_module) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1390 | Isolate* isolate = source->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1391 | if (compile_options == ScriptCompiler::kNoCompileOptions) { |
| 1392 | cached_data = NULL; |
| 1393 | } else if (compile_options == ScriptCompiler::kProduceParserCache || |
| 1394 | compile_options == ScriptCompiler::kProduceCodeCache) { |
| 1395 | DCHECK(cached_data && !*cached_data); |
| 1396 | DCHECK(extension == NULL); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1397 | DCHECK(!isolate->debug()->is_loaded()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1398 | } else { |
| 1399 | DCHECK(compile_options == ScriptCompiler::kConsumeParserCache || |
| 1400 | compile_options == ScriptCompiler::kConsumeCodeCache); |
| 1401 | DCHECK(cached_data && *cached_data); |
| 1402 | DCHECK(extension == NULL); |
| 1403 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1404 | int source_length = source->length(); |
| 1405 | isolate->counters()->total_load_size()->Increment(source_length); |
| 1406 | isolate->counters()->total_compile_size()->Increment(source_length); |
| 1407 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1408 | LanguageMode language_mode = construct_language_mode(FLAG_use_strict); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1409 | CompilationCache* compilation_cache = isolate->compilation_cache(); |
| 1410 | |
| 1411 | // Do a lookup in the compilation cache but not for extensions. |
| 1412 | MaybeHandle<SharedFunctionInfo> maybe_result; |
| 1413 | Handle<SharedFunctionInfo> result; |
| 1414 | if (extension == NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1415 | // First check per-isolate compilation cache. |
| 1416 | maybe_result = compilation_cache->LookupScript( |
| 1417 | source, script_name, line_offset, column_offset, resource_options, |
| 1418 | context, language_mode); |
| 1419 | if (maybe_result.is_null() && FLAG_serialize_toplevel && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1420 | compile_options == ScriptCompiler::kConsumeCodeCache && |
| 1421 | !isolate->debug()->is_loaded()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1422 | // Then check cached code provided by embedder. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1423 | HistogramTimerScope timer(isolate->counters()->compile_deserialize()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1424 | TRACE_EVENT0("v8", "V8.CompileDeserialize"); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1425 | Handle<SharedFunctionInfo> result; |
| 1426 | if (CodeSerializer::Deserialize(isolate, *cached_data, source) |
| 1427 | .ToHandle(&result)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1428 | // Promote to per-isolate compilation cache. |
| 1429 | compilation_cache->PutScript(source, context, language_mode, result); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1430 | return result; |
| 1431 | } |
| 1432 | // Deserializer failed. Fall through to compile. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | base::ElapsedTimer timer; |
| 1437 | if (FLAG_profile_deserialization && FLAG_serialize_toplevel && |
| 1438 | compile_options == ScriptCompiler::kProduceCodeCache) { |
| 1439 | timer.Start(); |
| 1440 | } |
| 1441 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1442 | if (!maybe_result.ToHandle(&result) || |
| 1443 | (FLAG_serialize_toplevel && |
| 1444 | compile_options == ScriptCompiler::kProduceCodeCache)) { |
| 1445 | // No cache entry found, or embedder wants a code cache. Compile the script. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1446 | |
| 1447 | // Create a script object describing the script to be compiled. |
| 1448 | Handle<Script> script = isolate->factory()->NewScript(source); |
| 1449 | if (natives == NATIVES_CODE) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1450 | script->set_type(Script::TYPE_NATIVE); |
| 1451 | script->set_hide_source(true); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1452 | } else if (natives == EXTENSION_CODE) { |
| 1453 | script->set_type(Script::TYPE_EXTENSION); |
| 1454 | script->set_hide_source(true); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1455 | } |
| 1456 | if (!script_name.is_null()) { |
| 1457 | script->set_name(*script_name); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1458 | script->set_line_offset(line_offset); |
| 1459 | script->set_column_offset(column_offset); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1460 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1461 | script->set_origin_options(resource_options); |
| 1462 | if (!source_map_url.is_null()) { |
| 1463 | script->set_source_mapping_url(*source_map_url); |
| 1464 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1465 | |
| 1466 | // Compile the function and add it to the cache. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1467 | Zone zone(isolate->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1468 | ParseInfo parse_info(&zone, script); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1469 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1470 | if (is_module) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1471 | parse_info.set_module(); |
| 1472 | } else { |
| 1473 | parse_info.set_global(); |
| 1474 | } |
| 1475 | if (compile_options != ScriptCompiler::kNoCompileOptions) { |
| 1476 | parse_info.set_cached_data(cached_data); |
| 1477 | } |
| 1478 | parse_info.set_compile_options(compile_options); |
| 1479 | parse_info.set_extension(extension); |
| 1480 | parse_info.set_context(context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1481 | if (FLAG_serialize_toplevel && |
| 1482 | compile_options == ScriptCompiler::kProduceCodeCache) { |
| 1483 | info.PrepareForSerializing(); |
| 1484 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1485 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1486 | parse_info.set_language_mode( |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1487 | static_cast<LanguageMode>(parse_info.language_mode() | language_mode)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1488 | result = CompileToplevel(&info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1489 | if (extension == NULL && !result.is_null()) { |
| 1490 | compilation_cache->PutScript(source, context, language_mode, result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1491 | if (FLAG_serialize_toplevel && |
| 1492 | compile_options == ScriptCompiler::kProduceCodeCache) { |
| 1493 | HistogramTimerScope histogram_timer( |
| 1494 | isolate->counters()->compile_serialize()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1495 | TRACE_EVENT0("v8", "V8.CompileSerialize"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1496 | *cached_data = CodeSerializer::Serialize(isolate, result, source); |
| 1497 | if (FLAG_profile_deserialization) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1498 | PrintF("[Compiling and serializing took %0.3f ms]\n", |
| 1499 | timer.Elapsed().InMillisecondsF()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1500 | } |
| 1501 | } |
| 1502 | } |
| 1503 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1504 | if (result.is_null()) { |
| 1505 | isolate->ReportPendingMessages(); |
| 1506 | } else { |
| 1507 | isolate->debug()->OnAfterCompile(script); |
| 1508 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1509 | } else if (result->ic_age() != isolate->heap()->global_ic_age()) { |
| 1510 | result->ResetForNewContext(isolate->heap()->global_ic_age()); |
| 1511 | } |
| 1512 | return result; |
| 1513 | } |
| 1514 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1515 | Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForStreamedScript( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1516 | Handle<Script> script, ParseInfo* parse_info, int source_length) { |
| 1517 | Isolate* isolate = script->GetIsolate(); |
| 1518 | // TODO(titzer): increment the counters in caller. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1519 | isolate->counters()->total_load_size()->Increment(source_length); |
| 1520 | isolate->counters()->total_compile_size()->Increment(source_length); |
| 1521 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1522 | LanguageMode language_mode = construct_language_mode(FLAG_use_strict); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1523 | parse_info->set_language_mode( |
| 1524 | static_cast<LanguageMode>(parse_info->language_mode() | language_mode)); |
| 1525 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1526 | CompilationInfo compile_info(parse_info, Handle<JSFunction>::null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1527 | |
| 1528 | // The source was parsed lazily, so compiling for debugging is not possible. |
| 1529 | DCHECK(!compile_info.is_debug()); |
| 1530 | |
| 1531 | Handle<SharedFunctionInfo> result = CompileToplevel(&compile_info); |
| 1532 | if (!result.is_null()) isolate->debug()->OnAfterCompile(script); |
| 1533 | return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1537 | Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfo( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1538 | FunctionLiteral* literal, Handle<Script> script, |
| 1539 | CompilationInfo* outer_info) { |
| 1540 | // Precondition: code has been parsed and scopes have been analyzed. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1541 | Isolate* isolate = outer_info->isolate(); |
| 1542 | MaybeHandle<SharedFunctionInfo> maybe_existing; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1543 | |
| 1544 | // Find any previously allocated shared function info for the given literal. |
| 1545 | if (outer_info->shared_info()->never_compiled()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1546 | // On the first compile, there are no existing shared function info for |
| 1547 | // inner functions yet, so do not try to find them. All bets are off for |
| 1548 | // live edit though. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1549 | SLOW_DCHECK(script->FindSharedFunctionInfo(literal).is_null() || |
| 1550 | isolate->debug()->live_edit_enabled()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1551 | } else { |
| 1552 | maybe_existing = script->FindSharedFunctionInfo(literal); |
| 1553 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1554 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1555 | // We found an existing shared function info. If it's already compiled, |
| 1556 | // don't worry about compiling it, and simply return it. If it's not yet |
| 1557 | // compiled, continue to decide whether to eagerly compile. |
| 1558 | // Carry on if we are compiling eager to obtain code for debugging, |
| 1559 | // unless we already have code with debut break slots. |
| 1560 | Handle<SharedFunctionInfo> existing; |
| 1561 | if (maybe_existing.ToHandle(&existing) && existing->is_compiled()) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1562 | DCHECK(!existing->is_toplevel()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1563 | if (!outer_info->is_debug() || existing->HasDebugCode()) { |
| 1564 | return existing; |
| 1565 | } |
| 1566 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1567 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1568 | // Allocate a shared function info object. |
| 1569 | Handle<SharedFunctionInfo> result; |
| 1570 | if (!maybe_existing.ToHandle(&result)) { |
| 1571 | result = NewSharedFunctionInfoForLiteral(isolate, literal, script); |
| 1572 | result->set_is_toplevel(false); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1573 | |
| 1574 | // If the outer function has been compiled before, we cannot be sure that |
| 1575 | // shared function info for this function literal has been created for the |
| 1576 | // first time. It may have already been compiled previously. |
| 1577 | result->set_never_compiled(outer_info->shared_info()->never_compiled()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | Zone zone(isolate->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1581 | ParseInfo parse_info(&zone, script); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1582 | CompilationInfo info(&parse_info, Handle<JSFunction>::null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1583 | parse_info.set_literal(literal); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1584 | parse_info.set_shared_info(result); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1585 | parse_info.set_scope(literal->scope()); |
| 1586 | parse_info.set_language_mode(literal->scope()->language_mode()); |
| 1587 | if (outer_info->will_serialize()) info.PrepareForSerializing(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1588 | if (outer_info->is_debug()) info.MarkAsDebug(); |
| 1589 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1590 | // Determine if the function can be lazily compiled. This is necessary to |
| 1591 | // allow some of our builtin JS files to be lazily compiled. These |
| 1592 | // builtins cannot be handled lazily by the parser, since we have to know |
| 1593 | // if a function uses the special natives syntax, which is something the |
| 1594 | // parser records. |
| 1595 | // If the debugger requests compilation for break points, we cannot be |
| 1596 | // aggressive about lazy compilation, because it might trigger compilation |
| 1597 | // of functions without an outer context when setting a breakpoint through |
| 1598 | // Debug::FindSharedFunctionInfoInScript. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1599 | bool allow_lazy = literal->AllowsLazyCompilation() && !info.is_debug(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1600 | bool lazy = FLAG_lazy && allow_lazy && !literal->should_eager_compile(); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1601 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1602 | // Consider compiling eagerly when targeting the code cache. |
| 1603 | lazy &= !(FLAG_serialize_eager && info.will_serialize()); |
| 1604 | |
| 1605 | // Consider compiling eagerly when compiling bytecode for Ignition. |
| 1606 | lazy &= |
| 1607 | !(FLAG_ignition && FLAG_ignition_eager && !isolate->serializer_enabled()); |
| 1608 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1609 | // Generate code |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1610 | TimerEventScope<TimerEventCompileCode> timer(isolate); |
| 1611 | TRACE_EVENT0("v8", "V8.CompileCode"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1612 | if (lazy) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1613 | info.SetCode(isolate->builtins()->CompileLazy()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1614 | } else if (Renumber(info.parse_info()) && GenerateUnoptimizedCode(&info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1615 | // Code generation will ensure that the feedback vector is present and |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 1616 | // appropriately sized. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1617 | DCHECK(!info.code().is_null()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1618 | if (literal->should_eager_compile() && |
| 1619 | literal->should_be_used_once_hint()) { |
| 1620 | info.code()->MarkToBeExecutedOnce(isolate); |
| 1621 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1622 | // Update the shared function info with the scope info. |
| 1623 | InstallSharedScopeInfo(&info, result); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1624 | // Install compilation result on the shared function info. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1625 | InstallSharedCompilationResult(&info, result); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1626 | } else { |
| 1627 | return Handle<SharedFunctionInfo>::null(); |
| 1628 | } |
| 1629 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1630 | if (maybe_existing.is_null()) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1631 | RecordFunctionCompilation(Logger::FUNCTION_TAG, &info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1632 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1633 | |
| 1634 | return result; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1637 | Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForNative( |
| 1638 | v8::Extension* extension, Handle<String> name) { |
| 1639 | Isolate* isolate = name->GetIsolate(); |
| 1640 | v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
| 1641 | |
| 1642 | // Compute the function template for the native function. |
| 1643 | v8::Local<v8::FunctionTemplate> fun_template = |
| 1644 | extension->GetNativeFunctionTemplate(v8_isolate, |
| 1645 | v8::Utils::ToLocal(name)); |
| 1646 | DCHECK(!fun_template.IsEmpty()); |
| 1647 | |
| 1648 | // Instantiate the function and create a shared function info from it. |
| 1649 | Handle<JSFunction> fun = Handle<JSFunction>::cast(Utils::OpenHandle( |
| 1650 | *fun_template->GetFunction(v8_isolate->GetCurrentContext()) |
| 1651 | .ToLocalChecked())); |
| 1652 | const int literals = fun->NumberOfLiterals(); |
| 1653 | Handle<Code> code = Handle<Code>(fun->shared()->code()); |
| 1654 | Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
| 1655 | Handle<SharedFunctionInfo> shared = isolate->factory()->NewSharedFunctionInfo( |
| 1656 | name, literals, FunctionKind::kNormalFunction, code, |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1657 | Handle<ScopeInfo>(fun->shared()->scope_info())); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1658 | shared->set_construct_stub(*construct_stub); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1659 | shared->set_feedback_vector(fun->shared()->feedback_vector()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1660 | |
| 1661 | // Copy the function data to the shared function info. |
| 1662 | shared->set_function_data(fun->shared()->function_data()); |
| 1663 | int parameters = fun->shared()->internal_formal_parameter_count(); |
| 1664 | shared->set_internal_formal_parameter_count(parameters); |
| 1665 | |
| 1666 | return shared; |
| 1667 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1668 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1669 | MaybeHandle<Code> Compiler::GetOptimizedCodeForOSR(Handle<JSFunction> function, |
| 1670 | BailoutId osr_ast_id, |
| 1671 | JavaScriptFrame* osr_frame) { |
| 1672 | DCHECK(!osr_ast_id.IsNone()); |
| 1673 | DCHECK_NOT_NULL(osr_frame); |
| 1674 | return GetOptimizedCode(function, NOT_CONCURRENT, osr_ast_id, osr_frame); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1677 | void Compiler::FinalizeCompilationJob(CompilationJob* raw_job) { |
| 1678 | // Take ownership of compilation job. Deleting job also tears down the zone. |
| 1679 | base::SmartPointer<CompilationJob> job(raw_job); |
| 1680 | CompilationInfo* info = job->info(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1681 | Isolate* isolate = info->isolate(); |
| 1682 | |
| 1683 | VMState<COMPILER> state(isolate); |
| 1684 | TimerEventScope<TimerEventRecompileSynchronous> timer(info->isolate()); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1685 | TRACE_EVENT0("v8", "V8.RecompileSynchronous"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1686 | |
| 1687 | Handle<SharedFunctionInfo> shared = info->shared_info(); |
| 1688 | shared->code()->set_profiler_ticks(0); |
| 1689 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1690 | DCHECK(!shared->HasDebugInfo()); |
| 1691 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1692 | // 1) Optimization on the concurrent thread may have failed. |
| 1693 | // 2) The function may have already been optimized by OSR. Simply continue. |
| 1694 | // Except when OSR already disabled optimization for some reason. |
| 1695 | // 3) The code may have already been invalidated due to dependency change. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1696 | // 4) Code generation may have failed. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1697 | if (job->last_status() == CompilationJob::SUCCEEDED) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1698 | if (shared->optimization_disabled()) { |
| 1699 | job->RetryOptimization(kOptimizationDisabled); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1700 | } else if (info->dependencies()->HasAborted()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1701 | job->RetryOptimization(kBailedOutDueToDependencyChange); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1702 | } else if (job->GenerateCode() == CompilationJob::SUCCEEDED) { |
| 1703 | job->RecordOptimizationStats(); |
| 1704 | RecordFunctionCompilation(Logger::LAZY_COMPILE_TAG, info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1705 | if (shared->SearchOptimizedCodeMap(info->context()->native_context(), |
| 1706 | info->osr_ast_id()).code == nullptr) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1707 | InsertCodeIntoOptimizedCodeMap(info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1708 | } |
| 1709 | if (FLAG_trace_opt) { |
| 1710 | PrintF("[completed optimizing "); |
| 1711 | info->closure()->ShortPrint(); |
| 1712 | PrintF("]\n"); |
| 1713 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1714 | info->closure()->ReplaceCode(*info->code()); |
| 1715 | return; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 1719 | DCHECK(job->last_status() != CompilationJob::SUCCEEDED); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1720 | if (FLAG_trace_opt) { |
| 1721 | PrintF("[aborted optimizing "); |
| 1722 | info->closure()->ShortPrint(); |
| 1723 | PrintF(" because: %s]\n", GetBailoutReason(info->bailout_reason())); |
| 1724 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1725 | info->closure()->ReplaceCode(shared->code()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1728 | void Compiler::PostInstantiation(Handle<JSFunction> function, |
| 1729 | PretenureFlag pretenure) { |
| 1730 | Handle<SharedFunctionInfo> shared(function->shared()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1731 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 1732 | if (FLAG_always_opt && shared->allows_lazy_compilation()) { |
| 1733 | function->MarkForOptimization(); |
| 1734 | } |
| 1735 | |
| 1736 | CodeAndLiterals cached = shared->SearchOptimizedCodeMap( |
| 1737 | function->context()->native_context(), BailoutId::None()); |
| 1738 | if (cached.code != nullptr) { |
| 1739 | // Caching of optimized code enabled and optimized code found. |
| 1740 | DCHECK(!cached.code->marked_for_deoptimization()); |
| 1741 | DCHECK(function->shared()->is_compiled()); |
| 1742 | function->ReplaceCode(cached.code); |
| 1743 | } |
| 1744 | |
| 1745 | if (cached.literals != nullptr) { |
| 1746 | function->set_literals(cached.literals); |
| 1747 | } else { |
| 1748 | Isolate* isolate = function->GetIsolate(); |
| 1749 | int number_of_literals = shared->num_literals(); |
| 1750 | Handle<LiteralsArray> literals = |
| 1751 | LiteralsArray::New(isolate, handle(shared->feedback_vector()), |
| 1752 | number_of_literals, pretenure); |
| 1753 | function->set_literals(*literals); |
| 1754 | |
| 1755 | // Cache context-specific literals. |
| 1756 | MaybeHandle<Code> code; |
| 1757 | if (cached.code != nullptr) code = handle(cached.code); |
| 1758 | Handle<Context> native_context(function->context()->native_context()); |
| 1759 | SharedFunctionInfo::AddToOptimizedCodeMap(shared, native_context, code, |
| 1760 | literals, BailoutId::None()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1764 | } // namespace internal |
| 1765 | } // namespace v8 |