Line data Source code
1 : // Copyright 2016 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/compilation-info.h"
6 :
7 : #include "src/api.h"
8 : #include "src/ast/ast.h"
9 : #include "src/ast/scopes.h"
10 : #include "src/debug/debug.h"
11 : #include "src/isolate.h"
12 : #include "src/objects-inl.h"
13 : #include "src/parsing/parse-info.h"
14 : #include "src/source-position.h"
15 :
16 : namespace v8 {
17 : namespace internal {
18 :
19 : // TODO(mvstanton): the Code::OPTIMIZED_FUNCTION constant below is
20 : // bogus, it's just that I've eliminated Code::FUNCTION and there isn't
21 : // a "better" value to put in this place.
22 2477372 : CompilationInfo::CompilationInfo(Zone* zone, Isolate* isolate,
23 2477376 : ParseInfo* parse_info,
24 : FunctionLiteral* literal)
25 2477372 : : CompilationInfo({}, Code::OPTIMIZED_FUNCTION, BASE, isolate, zone) {
26 : // NOTE: The parse_info passed here represents the global information gathered
27 : // during parsing, but does not represent specific details of the actual
28 : // function literal being compiled for this CompilationInfo. As such,
29 : // parse_info->literal() might be different from literal, and only global
30 : // details of the script being parsed are relevant to this CompilationInfo.
31 : DCHECK_NOT_NULL(literal);
32 2477376 : literal_ = literal;
33 2477376 : source_range_map_ = parse_info->source_range_map();
34 :
35 2477376 : if (parse_info->is_eval()) MarkAsEval();
36 2477376 : if (parse_info->is_native()) MarkAsNative();
37 2477376 : if (parse_info->will_serialize()) MarkAsSerializing();
38 2477376 : if (parse_info->collect_type_profile()) MarkAsCollectTypeProfile();
39 2477376 : }
40 :
41 461375 : CompilationInfo::CompilationInfo(Zone* zone, Isolate* isolate,
42 : Handle<SharedFunctionInfo> shared,
43 : Handle<JSFunction> closure)
44 461375 : : CompilationInfo({}, Code::OPTIMIZED_FUNCTION, OPTIMIZE, isolate, zone) {
45 461376 : shared_info_ = shared;
46 461376 : closure_ = closure;
47 461376 : optimization_id_ = isolate->NextOptimizationId();
48 :
49 461376 : if (FLAG_function_context_specialization) MarkAsFunctionContextSpecializing();
50 461376 : if (FLAG_turbo_splitting) MarkAsSplittingEnabled();
51 :
52 : // Collect source positions for optimized code when profiling or if debugger
53 : // is active, to be able to get more precise source positions at the price of
54 : // more memory consumption.
55 461376 : if (isolate_->NeedsSourcePositionsForProfiling()) {
56 : MarkAsSourcePositionsEnabled();
57 : }
58 461376 : }
59 :
60 858275 : CompilationInfo::CompilationInfo(Vector<const char> debug_name,
61 : Isolate* isolate, Zone* zone,
62 : Code::Kind code_kind)
63 858275 : : CompilationInfo(debug_name, code_kind, STUB, isolate, zone) {}
64 :
65 3797019 : CompilationInfo::CompilationInfo(Vector<const char> debug_name,
66 : Code::Kind code_kind, Mode mode,
67 : Isolate* isolate, Zone* zone)
68 : : isolate_(isolate),
69 : literal_(nullptr),
70 : flags_(0),
71 : code_kind_(code_kind),
72 : mode_(mode),
73 : osr_offset_(BailoutId::None()),
74 : feedback_vector_spec_(zone),
75 : zone_(zone),
76 : deferred_handles_(nullptr),
77 : dependencies_(isolate, zone),
78 : bailout_reason_(kNoReason),
79 : parameter_count_(0),
80 : optimization_id_(-1),
81 : osr_expr_stack_height_(-1),
82 22782022 : debug_name_(debug_name) {}
83 :
84 3815010 : CompilationInfo::~CompilationInfo() {
85 3797043 : if (GetFlag(kDisableFutureOptimization) && has_shared_info()) {
86 17967 : shared_info()->DisableOptimization(bailout_reason());
87 : }
88 3797043 : dependencies()->Rollback();
89 3797043 : }
90 :
91 10663692 : DeclarationScope* CompilationInfo::scope() const {
92 : DCHECK_NOT_NULL(literal_);
93 12852221 : return literal_->scope();
94 : }
95 :
96 2131325 : int CompilationInfo::num_parameters() const {
97 2131325 : return !IsStub() ? scope()->num_parameters() : parameter_count_;
98 : }
99 :
100 2131325 : int CompilationInfo::num_parameters_including_this() const {
101 2131325 : return num_parameters() + (is_this_defined() ? 1 : 0);
102 : }
103 :
104 0 : bool CompilationInfo::is_this_defined() const { return !IsStub(); }
105 :
106 0 : void CompilationInfo::set_deferred_handles(
107 : std::shared_ptr<DeferredHandles> deferred_handles) {
108 : DCHECK_NULL(deferred_handles_);
109 : deferred_handles_.swap(deferred_handles);
110 0 : }
111 :
112 8605 : void CompilationInfo::set_deferred_handles(DeferredHandles* deferred_handles) {
113 : DCHECK_NULL(deferred_handles_);
114 8605 : deferred_handles_.reset(deferred_handles);
115 8605 : }
116 :
117 441831 : void CompilationInfo::ReopenHandlesInNewHandleScope() {
118 441831 : if (!shared_info_.is_null()) {
119 441833 : shared_info_ = Handle<SharedFunctionInfo>(*shared_info_);
120 : }
121 441832 : if (!closure_.is_null()) {
122 441834 : closure_ = Handle<JSFunction>(*closure_);
123 : }
124 441833 : }
125 :
126 57204 : bool CompilationInfo::has_simple_parameters() {
127 57204 : return scope()->has_simple_parameters();
128 : }
129 :
130 1319511 : std::unique_ptr<char[]> CompilationInfo::GetDebugName() const {
131 1319511 : if (literal()) {
132 : AllowHandleDereference allow_deref;
133 0 : return literal()->debug_name()->ToCString();
134 : }
135 1319511 : if (!shared_info().is_null()) {
136 461344 : return shared_info()->DebugName()->ToCString();
137 : }
138 858167 : Vector<const char> name_vec = debug_name_;
139 858167 : if (name_vec.is_empty()) name_vec = ArrayVector("unknown");
140 858167 : std::unique_ptr<char[]> name(new char[name_vec.length() + 1]);
141 858183 : memcpy(name.get(), name_vec.start(), name_vec.length());
142 858183 : name[name_vec.length()] = '\0';
143 : return name;
144 : }
145 :
146 226077 : StackFrame::Type CompilationInfo::GetOutputStackFrameType() const {
147 226077 : switch (code_kind()) {
148 : case Code::STUB:
149 : case Code::BYTECODE_HANDLER:
150 : case Code::BUILTIN:
151 : return StackFrame::STUB;
152 : case Code::WASM_FUNCTION:
153 153016 : return StackFrame::WASM_COMPILED;
154 : case Code::JS_TO_WASM_FUNCTION:
155 0 : return StackFrame::JS_TO_WASM;
156 : case Code::WASM_TO_JS_FUNCTION:
157 13492 : return StackFrame::WASM_TO_JS;
158 : case Code::WASM_INTERPRETER_ENTRY:
159 1125 : return StackFrame::WASM_INTERPRETER_ENTRY;
160 : default:
161 0 : UNIMPLEMENTED();
162 : return StackFrame::NONE;
163 : }
164 : }
165 :
166 88870 : int CompilationInfo::GetDeclareGlobalsFlags() const {
167 88870 : return DeclareGlobalsEvalFlag::encode(is_eval()) |
168 88870 : DeclareGlobalsNativeFlag::encode(is_native());
169 : }
170 :
171 : SourcePositionTableBuilder::RecordingMode
172 3432843 : CompilationInfo::SourcePositionRecordingMode() const {
173 : return is_native() ? SourcePositionTableBuilder::OMIT_SOURCE_POSITIONS
174 3432843 : : SourcePositionTableBuilder::RECORD_SOURCE_POSITIONS;
175 : }
176 :
177 0 : bool CompilationInfo::has_context() const { return !closure().is_null(); }
178 :
179 983197 : Context* CompilationInfo::context() const {
180 1966394 : return has_context() ? closure()->context() : nullptr;
181 : }
182 :
183 2216906 : bool CompilationInfo::has_native_context() const {
184 4433812 : return !closure().is_null() && (closure()->native_context() != nullptr);
185 : }
186 :
187 2216905 : Context* CompilationInfo::native_context() const {
188 4433813 : return has_native_context() ? closure()->native_context() : nullptr;
189 : }
190 :
191 0 : bool CompilationInfo::has_global_object() const { return has_native_context(); }
192 :
193 0 : JSGlobalObject* CompilationInfo::global_object() const {
194 0 : return has_global_object() ? native_context()->global_object() : nullptr;
195 : }
196 :
197 61528 : int CompilationInfo::AddInlinedFunction(
198 : Handle<SharedFunctionInfo> inlined_function, SourcePosition pos) {
199 123056 : int id = static_cast<int>(inlined_functions_.size());
200 123056 : inlined_functions_.push_back(InlinedFunctionHolder(inlined_function, pos));
201 61528 : return id;
202 : }
203 :
204 : } // namespace internal
205 : } // namespace v8
|