V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
optimized-compilation-info.cc
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/optimized-compilation-info.h"
6 
7 #include "src/api.h"
8 #include "src/debug/debug.h"
9 #include "src/isolate.h"
10 #include "src/objects-inl.h"
11 #include "src/objects/shared-function-info.h"
12 #include "src/source-position.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 OptimizedCompilationInfo::OptimizedCompilationInfo(
18  Zone* zone, Isolate* isolate, Handle<SharedFunctionInfo> shared,
19  Handle<JSFunction> closure)
20  : OptimizedCompilationInfo(Code::OPTIMIZED_FUNCTION, zone) {
21  DCHECK(shared->is_compiled());
22  bytecode_array_ = handle(shared->GetBytecodeArray(), isolate);
23  shared_info_ = shared;
24  closure_ = closure;
25  optimization_id_ = isolate->NextOptimizationId();
26 
27  // Collect source positions for optimized code when profiling or if debugger
28  // is active, to be able to get more precise source positions at the price of
29  // more memory consumption.
30  if (isolate->NeedsDetailedOptimizedCodeLineInfo()) {
31  MarkAsSourcePositionsEnabled();
32  }
33 
34  SetTracingFlags(shared->PassesFilter(FLAG_trace_turbo_filter));
35 }
36 
37 OptimizedCompilationInfo::OptimizedCompilationInfo(
38  Vector<const char> debug_name, Zone* zone, Code::Kind code_kind)
39  : OptimizedCompilationInfo(code_kind, zone) {
40  debug_name_ = debug_name;
41 
42  SetTracingFlags(
43  PassesFilter(debug_name, CStrVector(FLAG_trace_turbo_filter)));
44 }
45 
46 OptimizedCompilationInfo::OptimizedCompilationInfo(Code::Kind code_kind,
47  Zone* zone)
48  : code_kind_(code_kind), zone_(zone) {
49  ConfigureFlags();
50 }
51 
52 void OptimizedCompilationInfo::ConfigureFlags() {
53  if (FLAG_untrusted_code_mitigations) SetFlag(kUntrustedCodeMitigations);
54 
55  switch (code_kind_) {
56  case Code::OPTIMIZED_FUNCTION:
57  SetFlag(kCalledWithCodeStartRegister);
58  SetFlag(kSwitchJumpTableEnabled);
59  if (FLAG_function_context_specialization) {
60  MarkAsFunctionContextSpecializing();
61  }
62  if (FLAG_turbo_splitting) {
63  MarkAsSplittingEnabled();
64  }
65  if (FLAG_untrusted_code_mitigations) {
66  MarkAsPoisoningRegisterArguments();
67  }
68  if (FLAG_analyze_environment_liveness) {
69  // TODO(yangguo): Disable this in case of debugging for crbug.com/826613
70  MarkAsAnalyzeEnvironmentLiveness();
71  }
72  break;
73  case Code::BYTECODE_HANDLER:
74  SetFlag(kCalledWithCodeStartRegister);
75  break;
76  case Code::BUILTIN:
77  case Code::STUB:
78 #if ENABLE_GDB_JIT_INTERFACE && DEBUG
79  MarkAsSourcePositionsEnabled();
80 #endif // ENABLE_GDB_JIT_INTERFACE && DEBUG
81  break;
82  case Code::WASM_FUNCTION:
83  SetFlag(kSwitchJumpTableEnabled);
84  break;
85  default:
86  break;
87  }
88 }
89 
90 OptimizedCompilationInfo::~OptimizedCompilationInfo() {
91  if (GetFlag(kDisableFutureOptimization) && has_shared_info()) {
92  shared_info()->DisableOptimization(bailout_reason());
93  }
94 }
95 
96 void OptimizedCompilationInfo::set_deferred_handles(
97  std::shared_ptr<DeferredHandles> deferred_handles) {
98  DCHECK_NULL(deferred_handles_);
99  deferred_handles_.swap(deferred_handles);
100 }
101 
102 void OptimizedCompilationInfo::set_deferred_handles(
103  DeferredHandles* deferred_handles) {
104  DCHECK_NULL(deferred_handles_);
105  deferred_handles_.reset(deferred_handles);
106 }
107 
108 void OptimizedCompilationInfo::ReopenHandlesInNewHandleScope(Isolate* isolate) {
109  if (!shared_info_.is_null()) {
110  shared_info_ = Handle<SharedFunctionInfo>(*shared_info_, isolate);
111  }
112  if (!bytecode_array_.is_null()) {
113  bytecode_array_ = Handle<BytecodeArray>(*bytecode_array_, isolate);
114  }
115  if (!closure_.is_null()) {
116  closure_ = Handle<JSFunction>(*closure_, isolate);
117  }
118 }
119 
120 std::unique_ptr<char[]> OptimizedCompilationInfo::GetDebugName() const {
121  if (!shared_info().is_null()) {
122  return shared_info()->DebugName()->ToCString();
123  }
124  Vector<const char> name_vec = debug_name_;
125  if (name_vec.is_empty()) name_vec = ArrayVector("unknown");
126  std::unique_ptr<char[]> name(new char[name_vec.length() + 1]);
127  memcpy(name.get(), name_vec.start(), name_vec.length());
128  name[name_vec.length()] = '\0';
129  return name;
130 }
131 
132 StackFrame::Type OptimizedCompilationInfo::GetOutputStackFrameType() const {
133  switch (code_kind()) {
134  case Code::STUB:
135  case Code::BYTECODE_HANDLER:
136  case Code::BUILTIN:
137  return StackFrame::STUB;
138  case Code::WASM_FUNCTION:
139  return StackFrame::WASM_COMPILED;
140  case Code::JS_TO_WASM_FUNCTION:
141  return StackFrame::JS_TO_WASM;
142  case Code::WASM_TO_JS_FUNCTION:
143  return StackFrame::WASM_TO_JS;
144  case Code::WASM_INTERPRETER_ENTRY:
145  return StackFrame::WASM_INTERPRETER_ENTRY;
146  default:
147  UNIMPLEMENTED();
148  return StackFrame::NONE;
149  }
150 }
151 
152 bool OptimizedCompilationInfo::has_context() const {
153  return !closure().is_null();
154 }
155 
156 Context OptimizedCompilationInfo::context() const {
157  DCHECK(has_context());
158  return closure()->context();
159 }
160 
161 bool OptimizedCompilationInfo::has_native_context() const {
162  return !closure().is_null() && !closure()->native_context().is_null();
163 }
164 
165 Context OptimizedCompilationInfo::native_context() const {
166  DCHECK(has_native_context());
167  return closure()->native_context();
168 }
169 
170 bool OptimizedCompilationInfo::has_global_object() const {
171  return has_native_context();
172 }
173 
174 JSGlobalObject* OptimizedCompilationInfo::global_object() const {
175  DCHECK(has_global_object());
176  return native_context()->global_object();
177 }
178 
179 int OptimizedCompilationInfo::AddInlinedFunction(
180  Handle<SharedFunctionInfo> inlined_function,
181  Handle<BytecodeArray> inlined_bytecode, SourcePosition pos) {
182  int id = static_cast<int>(inlined_functions_.size());
183  inlined_functions_.push_back(
184  InlinedFunctionHolder(inlined_function, inlined_bytecode, pos));
185  return id;
186 }
187 
188 void OptimizedCompilationInfo::SetTracingFlags(bool passes_filter) {
189  if (!passes_filter) return;
190  if (FLAG_trace_turbo) SetFlag(kTraceTurboJson);
191  if (FLAG_trace_turbo_graph) SetFlag(kTraceTurboGraph);
192  if (FLAG_trace_turbo_scheduled) SetFlag(kTraceTurboScheduled);
193 }
194 
195 } // namespace internal
196 } // namespace v8
Definition: libplatform.h:13