source-position.h 3.69 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class Code;
class CompilationInfo;
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
// CompilationInfo::inlined_functions or
// DeoptimizationInputData::InliningPositions, depending on the compilation
// stage.
class SourcePosition final {
33
 public:
34 35 36 37
  explicit SourcePosition(int script_offset, int inlining_id = kNotInlined)
      : value_(0) {
    SetScriptOffset(script_offset);
    SetInliningId(inlining_id);
38 39
  }

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

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

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

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

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

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

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

77 78 79 80 81 82 83 84 85
 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_;
86 87
};

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

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

  // references position in DeoptimizationInputData::literals()
  int inlined_function_id;
};

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

  SourcePosition position;
108
  Handle<SharedFunctionInfo> function;
109 110 111 112 113 114 115 116 117 118
  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);

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

#endif  // V8_SOURCE_POSITION_H_