v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
source-position.h
Go to the documentation of this file.
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#ifndef V8_CODEGEN_SOURCE_POSITION_H_
6#define V8_CODEGEN_SOURCE_POSITION_H_
7
8#include <iosfwd>
9
10#include "src/base/bit-field.h"
11#include "src/common/globals.h"
12#include "src/flags/flags.h"
13#include "src/handles/handles.h"
14
15namespace v8 {
16namespace internal {
17
18class InstructionStream;
19class OptimizedCompilationInfo;
20class Script;
21class SharedFunctionInfo;
22struct SourcePositionInfo;
23
24// SourcePosition stores
25// - is_external (1 bit true/false)
26//
27// - if is_external is true:
28// - external_line (20 bits, non-negative int)
29// - external_file_id (10 bits, non-negative int)
30//
31// - if is_external is false:
32// - script_offset (30 bit non-negative int or kNoSourcePosition)
33//
34// - In both cases, there is an inlining_id.
35// - inlining_id (16 bit non-negative int or kNotInlined).
36//
37// An "external" SourcePosition is one given by a file_id and a line,
38// suitable for embedding references to .cc or .tq files.
39// Otherwise, a SourcePosition contains an offset into a JavaScript
40// file.
41//
42// A defined inlining_id refers to positions in
43// OptimizedCompilationInfo::inlined_functions or
44// DeoptimizationData::InliningPositions, depending on the compilation stage.
45class SourcePosition final {
46 public:
47 explicit SourcePosition(int script_offset = kNoSourcePosition,
48 int inlining_id = kNotInlined)
49 : value_(0) {
50 SetIsExternal(false);
51 SetScriptOffset(script_offset);
52 SetInliningId(inlining_id);
53 }
54
55 // External SourcePositions should use the following method to construct
56 // SourcePositions to avoid confusion.
57 static SourcePosition External(int line, int file_id) {
58 return SourcePosition(line, file_id, kNotInlined);
59 }
60
61 static SourcePosition Unknown() { return SourcePosition(); }
62 bool IsKnown() const { return raw() != SourcePosition::Unknown().raw(); }
63 bool isInlined() const {
64 if (IsExternal()) return false;
65 return InliningId() != kNotInlined;
66 }
67
68 bool IsExternal() const { return IsExternalField::decode(value_); }
69 bool IsJavaScript() const { return !IsExternal(); }
70
71 int ExternalLine() const {
74 }
75
76 int ExternalFileId() const {
79 }
80
81 // Assumes that the code object is optimized.
82 std::vector<SourcePositionInfo> InliningStack(Isolate* isolate,
83 Tagged<Code> code) const;
84 std::vector<SourcePositionInfo> InliningStack(
85 Isolate* isolate, OptimizedCompilationInfo* cinfo) const;
87
88 void Print(std::ostream& out, Tagged<Code> code) const;
89 void PrintJson(std::ostream& out) const;
90
91 int ScriptOffset() const {
94 }
95 int InliningId() const { return InliningIdField::decode(value_) - 1; }
96
97 void SetIsExternal(bool external) {
99 }
100 void SetExternalLine(int line) {
103 }
104 void SetExternalFileId(int file_id) {
107 }
108
109 void SetScriptOffset(int script_offset) {
111 DCHECK_GE(script_offset, kNoSourcePosition);
112 value_ = ScriptOffsetField::update(value_, script_offset + 1);
113 }
114 void SetInliningId(int inlining_id) {
115 DCHECK_GE(inlining_id, kNotInlined);
116 value_ = InliningIdField::update(value_, inlining_id + 1);
117 }
118
119 static const int kNotInlined = -1;
120 static_assert(kNoSourcePosition == -1);
121
122 int64_t raw() const { return static_cast<int64_t>(value_); }
123 static SourcePosition FromRaw(int64_t raw) {
125 DCHECK_GE(raw, 0);
126 position.value_ = static_cast<uint64_t>(raw);
127 return position;
128 }
129
130 private:
131 // Used by SourcePosition::External(line, file_id).
132 SourcePosition(int line, int file_id, int inlining_id) : value_(0) {
133 SetIsExternal(true);
134 SetExternalLine(line);
135 SetExternalFileId(file_id);
136 SetInliningId(inlining_id);
137 }
138
139 void Print(std::ostream& out, Tagged<SharedFunctionInfo> function) const;
140
142
143 // The two below are only used if IsExternal() is true.
146
147 // ScriptOffsetField is only used if IsExternal() is false.
149
150 // InliningId is in the high bits for better compression in
151 // SourcePositionTable.
153
154 // Leaving the highest bit untouched to allow for signed conversion.
155 uint64_t value_;
156};
157
158inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) {
159 return lhs.raw() == rhs.raw();
160}
161
162inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) {
163 return !(lhs == rhs);
164}
165
167 // position of the inlined call
169
170 // references position in DeoptimizationData::literals()
172};
173
175 // Non-canonicalized (module-specific) index of the inlined function.
177 // Whether the call was a tail call.
179 // Source location of the caller.
181};
182
193
194std::ostream& operator<<(std::ostream& out, const SourcePosition& pos);
195
196std::ostream& operator<<(std::ostream& out, const SourcePositionInfo& pos);
197std::ostream& operator<<(std::ostream& out,
198 const std::vector<SourcePositionInfo>& stack);
199
200} // namespace internal
201} // namespace v8
202
203#endif // V8_CODEGEN_SOURCE_POSITION_H_
SourcePosition pos
static constexpr T decode(U value)
Definition bit-field.h:66
static V8_NODISCARD constexpr U update(U previous, T value)
Definition bit-field.h:61
static SourcePosition FromRaw(int64_t raw)
std::vector< SourcePositionInfo > InliningStack(Isolate *isolate, Tagged< Code > code) const
void Print(std::ostream &out, Tagged< Code > code) const
void SetScriptOffset(int script_offset)
static SourcePosition Unknown()
void SetInliningId(int inlining_id)
static SourcePosition External(int line, int file_id)
SourcePosition(int script_offset=kNoSourcePosition, int inlining_id=kNotInlined)
void SetExternalFileId(int file_id)
SourcePositionInfo FirstInfo(Isolate *isolate, Tagged< Code > code) const
void SetIsExternal(bool external)
SourcePosition(int line, int file_id, int inlining_id)
void PrintJson(std::ostream &out) const
int position
Definition liveedit.cc:290
bool operator!=(ExternalReference lhs, ExternalReference rhs)
constexpr int kNoSourcePosition
Definition globals.h:850
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
bool operator==(ExternalReference lhs, ExternalReference rhs)
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
IndirectHandle< Script > script
IndirectHandle< SharedFunctionInfo > shared
SourcePositionInfo(Isolate *isolate, SourcePosition pos, DirectHandle< SharedFunctionInfo > f)