source-position.h 5.98 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_CODEGEN_SOURCE_POSITION_H_
#define V8_CODEGEN_SOURCE_POSITION_H_
7 8 9

#include <ostream>

10
#include "src/base/bit-field.h"
11 12
#include "src/common/globals.h"
#include "src/flags/flags.h"
13
#include "src/handles/handles.h"
14 15 16 17

namespace v8 {
namespace internal {

18
class Code;
19
class OptimizedCompilationInfo;
20 21 22 23 24
class Script;
class SharedFunctionInfo;
struct SourcePositionInfo;

// SourcePosition stores
25 26 27 28 29 30 31 32 33 34
// - is_external (1 bit true/false)
//
// - if is_external is true:
// - external_line (20 bits, non-negative int)
// - external_file_id (10 bits, non-negative int)
//
// - if is_external is false:
// - script_offset (30 bit non-negative int or kNoSourcePosition)
//
// - In both cases, there is an inlining_id.
35 36
// - inlining_id (16 bit non-negative int or kNotInlined).
//
37 38 39 40 41
// An "external" SourcePosition is one given by a file_id and a line,
// suitable for embedding references to .cc or .tq files.
// Otherwise, a SourcePosition contains an offset into a JavaScript
// file.
//
42
// A defined inlining_id refers to positions in
43
// OptimizedCompilationInfo::inlined_functions or
44
// DeoptimizationData::InliningPositions, depending on the compilation stage.
45
class SourcePosition final {
46
 public:
47 48
  explicit SourcePosition(int script_offset, int inlining_id = kNotInlined)
      : value_(0) {
49
    SetIsExternal(false);
50 51
    SetScriptOffset(script_offset);
    SetInliningId(inlining_id);
52 53
  }

54 55 56 57 58 59
  // External SourcePositions should use the following method to construct
  // SourcePositions to avoid confusion.
  static SourcePosition External(int line, int file_id) {
    return SourcePosition(line, file_id, kNotInlined);
  }

60 61
  static SourcePosition Unknown() { return SourcePosition(kNoSourcePosition); }
  bool IsKnown() const {
62
    if (IsExternal()) return true;
63
    return ScriptOffset() != kNoSourcePosition || InliningId() != kNotInlined;
64
  }
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  bool isInlined() const {
    if (IsExternal()) return false;
    return InliningId() != kNotInlined;
  }

  bool IsExternal() const { return IsExternalField::decode(value_); }
  bool IsJavaScript() const { return !IsExternal(); }

  int ExternalLine() const {
    DCHECK(IsExternal());
    return ExternalLineField::decode(value_);
  }

  int ExternalFileId() const {
    DCHECK(IsExternal());
    return ExternalFileIdField::decode(value_);
  }
82

83
  // Assumes that the code object is optimized
84
  std::vector<SourcePositionInfo> InliningStack(Handle<Code> code) const;
85 86
  std::vector<SourcePositionInfo> InliningStack(
      OptimizedCompilationInfo* cinfo) const;
87

88
  void Print(std::ostream& out, Code code) const;
89
  void PrintJson(std::ostream& out) const;
90

91 92 93 94
  int ScriptOffset() const {
    DCHECK(IsJavaScript());
    return ScriptOffsetField::decode(value_) - 1;
  }
95
  int InliningId() const { return InliningIdField::decode(value_) - 1; }
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110
  void SetIsExternal(bool external) {
    value_ = IsExternalField::update(value_, external);
  }
  void SetExternalLine(int line) {
    DCHECK(IsExternal());
    DCHECK(line <= ExternalLineField::kMax - 1);
    value_ = ExternalLineField::update(value_, line);
  }
  void SetExternalFileId(int file_id) {
    DCHECK(IsExternal());
    DCHECK(file_id <= ExternalFileIdField::kMax - 1);
    value_ = ExternalFileIdField::update(value_, file_id);
  }

111
  void SetScriptOffset(int script_offset) {
112
    DCHECK(IsJavaScript());
113
    DCHECK(script_offset <= ScriptOffsetField::kMax - 2);
114
    DCHECK_GE(script_offset, kNoSourcePosition);
115 116 117 118
    value_ = ScriptOffsetField::update(value_, script_offset + 1);
  }
  void SetInliningId(int inlining_id) {
    DCHECK(inlining_id <= InliningIdField::kMax - 2);
119
    DCHECK_GE(inlining_id, kNotInlined);
120 121
    value_ = InliningIdField::update(value_, inlining_id + 1);
  }
122

123 124
  static const int kNotInlined = -1;
  STATIC_ASSERT(kNoSourcePosition == -1);
125

126 127
  int64_t raw() const { return static_cast<int64_t>(value_); }
  static SourcePosition FromRaw(int64_t raw) {
128
    SourcePosition position = Unknown();
129 130
    DCHECK_GE(raw, 0);
    position.value_ = static_cast<uint64_t>(raw);
131 132 133
    return position;
  }

134
 private:
135 136 137 138 139 140 141 142
  // Used by SourcePosition::External(line, file_id).
  SourcePosition(int line, int file_id, int inlining_id) : value_(0) {
    SetIsExternal(true);
    SetExternalLine(line);
    SetExternalFileId(file_id);
    SetInliningId(inlining_id);
  }

143
  void Print(std::ostream& out, SharedFunctionInfo function) const;
144

145
  using IsExternalField = base::BitField64<bool, 0, 1>;
146 147

  // The two below are only used if IsExternal() is true.
148 149
  using ExternalLineField = base::BitField64<int, 1, 20>;
  using ExternalFileIdField = base::BitField64<int, 21, 10>;
150 151

  // ScriptOffsetField is only used if IsExternal() is false.
152
  using ScriptOffsetField = base::BitField64<int, 1, 30>;
153

154 155
  // InliningId is in the high bits for better compression in
  // SourcePositionTable.
156
  using InliningIdField = base::BitField64<int, 31, 16>;
157

158 159
  // Leaving the highest bit untouched to allow for signed conversion.
  uint64_t value_;
160 161
};

162 163 164 165 166 167
inline bool operator==(const SourcePosition& lhs, const SourcePosition& rhs) {
  return lhs.raw() == rhs.raw();
}

inline bool operator!=(const SourcePosition& lhs, const SourcePosition& rhs) {
  return !(lhs == rhs);
168 169
}

170 171 172 173
struct InliningPosition {
  // position of the inlined call
  SourcePosition position = SourcePosition::Unknown();

174
  // references position in DeoptimizationData::literals()
175 176 177 178
  int inlined_function_id;
};

struct SourcePositionInfo {
179
  SourcePositionInfo(SourcePosition pos, Handle<SharedFunctionInfo> f);
180 181

  SourcePosition position;
182
  Handle<SharedFunctionInfo> shared;
183
  Handle<Script> script;
184 185 186 187 188 189 190 191 192 193
  int line = -1;
  int column = -1;
};

std::ostream& operator<<(std::ostream& out, const SourcePosition& pos);

std::ostream& operator<<(std::ostream& out, const SourcePositionInfo& pos);
std::ostream& operator<<(std::ostream& out,
                         const std::vector<SourcePositionInfo>& stack);

194 195 196
}  // namespace internal
}  // namespace v8

197
#endif  // V8_CODEGEN_SOURCE_POSITION_H_