source-position.h 3.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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.

#ifndef V8_SOURCE_POSITION_H_
#define V8_SOURCE_POSITION_H_

#include <ostream>

#include "src/flags.h"
yangguo's avatar
yangguo committed
11
#include "src/globals.h"
12
#include "src/handles.h"
13 14 15 16 17
#include "src/utils.h"

namespace v8 {
namespace internal {

18
class Code;
19
class OptimizedCompilationInfo;
20 21 22 23 24 25 26 27 28
class Script;
class SharedFunctionInfo;
struct SourcePositionInfo;

// SourcePosition stores
// - script_offset (31 bit non-negative int or kNoSourcePosition)
// - inlining_id (16 bit non-negative int or kNotInlined).
//
// A defined inlining_id refers to positions in
29
// OptimizedCompilationInfo::inlined_functions or
30
// DeoptimizationData::InliningPositions, depending on the compilation stage.
31
class SourcePosition final {
32
 public:
33 34 35 36
  explicit SourcePosition(int script_offset, int inlining_id = kNotInlined)
      : value_(0) {
    SetScriptOffset(script_offset);
    SetInliningId(inlining_id);
37 38
  }

39 40 41
  static SourcePosition Unknown() { return SourcePosition(kNoSourcePosition); }
  bool IsKnown() const {
    return ScriptOffset() != kNoSourcePosition || InliningId() != kNotInlined;
42
  }
43
  bool isInlined() const { return InliningId() != kNotInlined; }
44

45
  // Assumes that the code object is optimized
46
  std::vector<SourcePositionInfo> InliningStack(Handle<Code> code) const;
47 48
  std::vector<SourcePositionInfo> InliningStack(
      OptimizedCompilationInfo* cinfo) const;
49

50
  void Print(std::ostream& out, Code* code) const;
51
  void PrintJson(std::ostream& out) const;
52

53 54
  int ScriptOffset() const { return ScriptOffsetField::decode(value_) - 1; }
  int InliningId() const { return InliningIdField::decode(value_) - 1; }
55

56 57
  void SetScriptOffset(int script_offset) {
    DCHECK(script_offset <= ScriptOffsetField::kMax - 2);
58
    DCHECK_GE(script_offset, kNoSourcePosition);
59 60 61 62
    value_ = ScriptOffsetField::update(value_, script_offset + 1);
  }
  void SetInliningId(int inlining_id) {
    DCHECK(inlining_id <= InliningIdField::kMax - 2);
63
    DCHECK_GE(inlining_id, kNotInlined);
64 65
    value_ = InliningIdField::update(value_, inlining_id + 1);
  }
66

67 68
  static const int kNotInlined = -1;
  STATIC_ASSERT(kNoSourcePosition == -1);
69

70 71
  int64_t raw() const { return static_cast<int64_t>(value_); }
  static SourcePosition FromRaw(int64_t raw) {
72
    SourcePosition position = Unknown();
73 74
    DCHECK_GE(raw, 0);
    position.value_ = static_cast<uint64_t>(raw);
75 76 77
    return position;
  }

78 79 80 81 82 83 84 85 86
 private:
  void Print(std::ostream& out, SharedFunctionInfo* function) const;

  // InliningId is in the high bits for better compression in
  // SourcePositionTable.
  typedef BitField64<int, 0, 31> ScriptOffsetField;
  typedef BitField64<int, 31, 16> InliningIdField;
  // Leaving the highest bit untouched to allow for signed conversion.
  uint64_t value_;
87 88
};

89 90 91 92 93 94
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);
95 96
}

97 98 99 100
struct InliningPosition {
  // position of the inlined call
  SourcePosition position = SourcePosition::Unknown();

101
  // references position in DeoptimizationData::literals()
102 103 104 105
  int inlined_function_id;
};

struct SourcePositionInfo {
106
  SourcePositionInfo(SourcePosition pos, Handle<SharedFunctionInfo> f);
107 108

  SourcePosition position;
109
  Handle<Script> script;
110 111 112 113 114 115 116 117 118 119
  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);

120 121 122 123
}  // namespace internal
}  // namespace v8

#endif  // V8_SOURCE_POSITION_H_