Line data Source code
1 : /* Unit-safe concrete ranges of values (e.g. "bits 16-31", "bytes 0-1023").
2 : Copyright (C) 2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_ANALYZER_CONCRETE_RANGES_H
22 : #define GCC_ANALYZER_CONCRETE_RANGES_H
23 :
24 : #include "analyzer/integer-with-unit.h"
25 :
26 : namespace ana {
27 : namespace concrete {
28 :
29 : /* Concrete ranges of bits or bytes (or unitless). */
30 :
31 : template<typename ConcreteValueType>
32 52953823 : class range
33 : {
34 : public:
35 7036923 : range () = default;
36 :
37 12797829 : range (ConcreteValueType start_offset,
38 : ConcreteValueType size)
39 12797146 : : m_start_offset (start_offset),
40 12797829 : m_size (size)
41 : {
42 : }
43 :
44 : void
45 : dump () const;
46 :
47 : void
48 : dump_to_pp (pretty_printer *pp) const;
49 :
50 : void
51 : dump_to_pp_pretty (pretty_printer *pp) const;
52 :
53 : ConcreteValueType
54 23759359 : get_start_offset () const { return m_start_offset; };
55 :
56 : ConcreteValueType
57 17155146 : get_size () const { return m_size; }
58 :
59 : ConcreteValueType
60 26528 : get_last_offset () const
61 : {
62 26528 : return get_next_offset () - ConcreteValueType (1);
63 : }
64 :
65 : ConcreteValueType
66 8169214 : get_next_offset () const { return m_start_offset + m_size; }
67 :
68 : bool
69 153546 : contains_p (ConcreteValueType offset) const
70 : {
71 307092 : return (offset >= get_start_offset ()
72 457534 : && offset < get_next_offset ());
73 : }
74 :
75 : bool
76 : intersects_p (const range &other, range *out = nullptr) const;
77 :
78 : bool
79 : exceeds_p (ConcreteValueType offset,
80 : range<ConcreteValueType> *out) const;
81 :
82 : bool
83 : falls_short_of_p (ConcreteValueType offset,
84 : range<ConcreteValueType> *out) const;
85 :
86 : bool
87 2339 : operator== (const range<ConcreteValueType> &other) const
88 : {
89 2339 : return (m_start_offset == other.m_start_offset
90 2339 : && m_size == other.m_size);
91 : }
92 :
93 : static int
94 : cmp (const range<ConcreteValueType> &lhs,
95 : const range<ConcreteValueType> &rhs);
96 :
97 : bool
98 764055 : operator< (const range<ConcreteValueType> &other) const
99 : {
100 764055 : return cmp (*this, other) < 0;
101 : }
102 :
103 : std::unique_ptr<json::object>
104 : to_json () const;
105 :
106 :
107 : std::unique_ptr<diagnostics::markup::node>
108 : make_markup_node () const;
109 :
110 : private:
111 : ConcreteValueType m_start_offset;
112 : ConcreteValueType m_size;
113 : };
114 :
115 : } // namespace ana::concrete
116 :
117 : using concrete_bit_range = concrete::range<integer_with_unit<units::bit>>;
118 : using concrete_byte_range = concrete::range<integer_with_unit<units::byte>>;
119 :
120 : inline concrete_bit_range
121 683 : bits_from_bytes (concrete_byte_range bytes)
122 : {
123 2049 : return concrete_bit_range (bits_from_bytes (bytes.get_start_offset ()),
124 2049 : bits_from_bytes (bytes.get_size ()));
125 : }
126 :
127 : bool
128 : maybe_bytes_from_bits (concrete_bit_range bits,
129 : concrete_byte_range *out);
130 :
131 : bool
132 : concrete_bit_range_from_mask (unsigned HOST_WIDE_INT mask,
133 : concrete_bit_range *out);
134 :
135 : void
136 : get_smallest_enclosing_byte_range (const concrete_bit_range &in_bit_range,
137 : concrete_byte_range *out_byte_range,
138 : concrete_bit_offset *out_bit_offset);
139 :
140 : } // namespace ana
141 :
142 : #endif /* GCC_ANALYZER_CONCRETE_RANGES_H */
|