Debian Codegraph @

js/src/frontend/BytecodeOffset.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef frontend_BytecodeOffset_h
8 #define frontend_BytecodeOffset_h
9
10 #include "mozilla/Assertions.h" // MOZ_ASSERT
11 #include "mozilla/CheckedInt.h" // mozilla::CheckedInt
12
13 #include <stddef.h> // ptrdiff_t
14
15 namespace js {
16 namespace frontend {
17
18 class BytecodeOffsetDiff;
19
20 // The offset inside bytecode.
21 class BytecodeOffset {
22 private:
23 static const ptrdiff_t INVALID_OFFSET = -1;
24
25 ptrdiff_t value_ = 0;
26
27 struct Invalid {};
28 explicit constexpr BytecodeOffset(Invalid) : value_(INVALID_OFFSET) {}
29
30 public:
31 constexpr BytecodeOffset() = default;
32
33 explicit BytecodeOffset(ptrdiff_t value) : value_(value) {
34 MOZ_ASSERT(value >= 0);
35 }
36
37 static constexpr BytecodeOffset invalidOffset() {
38 return BytecodeOffset(Invalid());
39 }
40
41 bool operator==(const BytecodeOffset& rhs) const {
42 return value_ == rhs.value_;
43 }
44
45 bool operator!=(const BytecodeOffset& rhs) const { return !(*this == rhs); }
46
47 inline BytecodeOffsetDiff operator-(const BytecodeOffset& rhs) const;
48 inline BytecodeOffset operator+(const BytecodeOffsetDiff& diff) const;
49
50 inline BytecodeOffset& operator+=(const BytecodeOffsetDiff& diff);
51 inline BytecodeOffset& operator-=(const BytecodeOffsetDiff& diff);
52
53 bool operator<(const BytecodeOffset& rhs) const {
54 MOZ_ASSERT(valid());
55 MOZ_ASSERT(rhs.valid());
56 return value_ < rhs.value_;
57 }
58 bool operator<=(const BytecodeOffset& rhs) const {
59 MOZ_ASSERT(valid());
60 MOZ_ASSERT(rhs.valid());
61 return value_ <= rhs.value_;
62 }
63 bool operator>(const BytecodeOffset& rhs) const {
64 MOZ_ASSERT(valid());
65 MOZ_ASSERT(rhs.valid());
66 return value_ > rhs.value_;
67 }
68 bool operator>=(const BytecodeOffset& rhs) const {
69 MOZ_ASSERT(valid());
70 MOZ_ASSERT(rhs.valid());
71 return value_ >= rhs.value_;
72 }
73
74 ptrdiff_t value() const { return value_; }
75 uint32_t toUint32() const {
76 MOZ_ASSERT(size_t(uint32_t(value_)) == size_t(value_));
77 return uint32_t(value_);
78 }
79
80 bool valid() const { return value_ != INVALID_OFFSET; }
81 };
82
83 class BytecodeOffsetDiff {
84 private:
85 friend class BytecodeOffset;
86
87 ptrdiff_t value_ = 0;
88
89 public:
90 constexpr BytecodeOffsetDiff() = default;
91
92 explicit constexpr BytecodeOffsetDiff(ptrdiff_t value_) : value_(value_) {}
93
94 bool operator==(const BytecodeOffsetDiff& rhs) const {
95 return value_ == rhs.value_;
96 }
97
98 bool operator!=(const BytecodeOffsetDiff& rhs) const {
99 return !(*this == rhs);
100 }
101
102 BytecodeOffsetDiff operator+(const BytecodeOffsetDiff& rhs) const {
103 mozilla::CheckedInt<ptrdiff_t> result = value_;
104 result += rhs.value_;
105 return BytecodeOffsetDiff(result.value());
106 }
107
108 ptrdiff_t value() const { return value_; }
109 uint32_t toUint32() const {
110 MOZ_ASSERT(size_t(uint32_t(value_)) == size_t(value_));
111 return uint32_t(value_);
112 }
113 };
114
115 inline BytecodeOffsetDiff BytecodeOffset::operator-(
116 const BytecodeOffset& rhs) const {
117 MOZ_ASSERT(valid());
118 MOZ_ASSERT(rhs.valid());
119 mozilla::CheckedInt<ptrdiff_t> result = value_;
120 result -= rhs.value_;
121 return BytecodeOffsetDiff(result.value());
122 }
123
124 inline BytecodeOffset BytecodeOffset::operator+(
125 const BytecodeOffsetDiff& diff) const {
126 MOZ_ASSERT(valid());
127 mozilla::CheckedInt<ptrdiff_t> result = value_;
128 result += diff.value_;
129 return BytecodeOffset(result.value());
130 }
131
132 inline BytecodeOffset& BytecodeOffset::operator+=(
133 const BytecodeOffsetDiff& diff) {
134 MOZ_ASSERT(valid());
135 mozilla::CheckedInt<ptrdiff_t> result = value_;
136 result += diff.value_;
137 value_ = result.value();
138 return *this;
139 }
140
141 inline BytecodeOffset& BytecodeOffset::operator-=(
142 const BytecodeOffsetDiff& diff) {
143 MOZ_ASSERT(valid());
144 mozilla::CheckedInt<ptrdiff_t> result = value_;
145 result -= diff.value_;
146 value_ = result.value();
147 return *this;
148 }
149
150 } /* namespace frontend */
151 } /* namespace js */
152
153 #endif /* frontend_BytecodeOffset_h */