1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_ARCH_CODE_OFFSET_H_
18 #define ART_RUNTIME_ARCH_CODE_OFFSET_H_
19 
20 #include <iosfwd>
21 
22 #include <android-base/logging.h>
23 
24 #include "base/bit_utils.h"
25 #include "base/macros.h"
26 #include "instruction_set.h"
27 
28 namespace art {
29 
30 // CodeOffset is a holder for compressed code offsets. Since some architectures have alignment
31 // requirements it is possible to compress code offsets to reduce stack map sizes.
32 class CodeOffset {
33  public:
34   ALWAYS_INLINE static CodeOffset FromOffset(uint32_t offset, InstructionSet isa = kRuntimeISA) {
35     return CodeOffset(offset / GetInstructionSetInstructionAlignment(isa));
36   }
37 
FromCompressedOffset(uint32_t offset)38   ALWAYS_INLINE static CodeOffset FromCompressedOffset(uint32_t offset) {
39     return CodeOffset(offset);
40   }
41 
42   ALWAYS_INLINE uint32_t Uint32Value(InstructionSet isa = kRuntimeISA) const {
43     uint32_t decoded = value_ * GetInstructionSetInstructionAlignment(isa);
44     DCHECK_GE(decoded, value_) << "Integer overflow";
45     return decoded;
46   }
47 
48   // Return compressed internal value.
CompressedValue()49   ALWAYS_INLINE uint32_t CompressedValue() const {
50     return value_;
51   }
52 
53   ALWAYS_INLINE CodeOffset() = default;
54   ALWAYS_INLINE CodeOffset(const CodeOffset&) = default;
55   ALWAYS_INLINE CodeOffset& operator=(const CodeOffset&) = default;
56   ALWAYS_INLINE CodeOffset& operator=(CodeOffset&&) = default;
57 
58  private:
CodeOffset(uint32_t value)59   ALWAYS_INLINE explicit CodeOffset(uint32_t value) : value_(value) {}
60 
61   uint32_t value_ = 0u;
62 };
63 
64 inline bool operator==(const CodeOffset& a, const CodeOffset& b) {
65   return a.CompressedValue() == b.CompressedValue();
66 }
67 
68 inline bool operator!=(const CodeOffset& a, const CodeOffset& b) {
69   return !(a == b);
70 }
71 
72 inline bool operator<(const CodeOffset& a, const CodeOffset& b) {
73   return a.CompressedValue() < b.CompressedValue();
74 }
75 
76 inline bool operator<=(const CodeOffset& a, const CodeOffset& b) {
77   return a.CompressedValue() <= b.CompressedValue();
78 }
79 
80 inline bool operator>(const CodeOffset& a, const CodeOffset& b) {
81   return a.CompressedValue() > b.CompressedValue();
82 }
83 
84 inline bool operator>=(const CodeOffset& a, const CodeOffset& b) {
85   return a.CompressedValue() >= b.CompressedValue();
86 }
87 
88 inline std::ostream& operator<<(std::ostream& os, const CodeOffset& offset) {
89   return os << offset.Uint32Value();
90 }
91 
92 }  // namespace art
93 
94 #endif  // ART_RUNTIME_ARCH_CODE_OFFSET_H_
95