1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/MathExtras.h>
29#include <wtf/PrintStream.h>
30
31namespace JSC { namespace DOMJIT {
32
33class HeapRange {
34public:
35 constexpr HeapRange()
36 : m_begin(UINT16_MAX)
37 , m_end(UINT16_MAX)
38 {
39 }
40
41 HeapRange(uint16_t begin, uint16_t end)
42 : m_begin(begin)
43 , m_end(end)
44 {
45 ASSERT_WITH_MESSAGE(begin <= end, "begin <= end is the invariant of this HeapRange.");
46 }
47
48 enum ConstExprTag { ConstExpr };
49 constexpr HeapRange(ConstExprTag, uint16_t begin, uint16_t end)
50 : m_begin(begin)
51 , m_end(end)
52 {
53 }
54
55 enum RawRepresentationTag { RawRepresentation };
56 explicit constexpr HeapRange(RawRepresentationTag, uint32_t value)
57 : m_raw(value)
58 {
59 }
60
61 constexpr static HeapRange fromRaw(uint32_t value)
62 {
63 return HeapRange(RawRepresentation, value);
64 }
65
66 uint16_t begin() const { return m_begin; }
67 uint16_t end() const { return m_end; }
68 uint32_t rawRepresentation() { return m_raw; }
69
70 constexpr explicit operator bool() const
71 {
72 return m_begin != m_end;
73 }
74
75 constexpr bool operator==(const HeapRange& other) const
76 {
77 return m_begin == other.m_begin && m_end == other.m_end;
78 }
79
80 constexpr bool operator!=(const HeapRange& other) const
81 {
82 return !operator==(other);
83 }
84
85 template<uint16_t begin, uint16_t end>
86 static constexpr HeapRange fromConstant()
87 {
88 static_assert(begin < end || (begin == UINT16_MAX && end == UINT16_MAX), "begin < end or the both are UINT16_MAX is the invariant of this HeapRange.");
89 return HeapRange(ConstExpr, begin, end);
90 }
91
92 static constexpr HeapRange top() { return fromConstant<0, UINT16_MAX>(); }
93 static constexpr HeapRange none() { return fromConstant<UINT16_MAX, UINT16_MAX>(); } // Empty range.
94
95 bool isStrictSubtypeOf(const HeapRange& other) const
96 {
97 if (!*this || !other)
98 return false;
99 if (*this == other)
100 return false;
101 return other.m_begin <= m_begin && m_end <= other.m_end;
102 }
103
104 bool isSubtypeOf(const HeapRange& other) const
105 {
106 if (!*this || !other)
107 return false;
108 if (*this == other)
109 return true;
110 return isStrictSubtypeOf(other);
111 }
112
113 bool overlaps(const HeapRange& other) const
114 {
115 return WTF::rangesOverlap(m_begin, m_end, other.m_begin, other.m_end);
116 }
117
118 JS_EXPORT_PRIVATE void dump(PrintStream&) const;
119
120private:
121 union {
122 struct {
123 uint16_t m_begin;
124 uint16_t m_end;
125 };
126 uint32_t m_raw;
127 };
128};
129
130} }
131