Home | History | Annotate | Download | only in arch
      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 "base/bit_utils.h"
     23 #include "base/logging.h"
     24 #include "instruction_set.h"
     25 
     26 namespace art {
     27 
     28 // CodeOffset is a holder for compressed code offsets. Since some architectures have alignment
     29 // requirements it is possible to compress code offsets to reduce stack map sizes.
     30 class CodeOffset {
     31  public:
     32   ALWAYS_INLINE static CodeOffset FromOffset(uint32_t offset, InstructionSet isa = kRuntimeISA) {
     33     return CodeOffset(offset / GetInstructionSetInstructionAlignment(isa));
     34   }
     35 
     36   ALWAYS_INLINE static CodeOffset FromCompressedOffset(uint32_t offset) {
     37     return CodeOffset(offset);
     38   }
     39 
     40   ALWAYS_INLINE uint32_t Uint32Value(InstructionSet isa = kRuntimeISA) const {
     41     uint32_t decoded = value_ * GetInstructionSetInstructionAlignment(isa);
     42     DCHECK_GE(decoded, value_) << "Integer overflow";
     43     return decoded;
     44   }
     45 
     46   // Return compressed internal value.
     47   ALWAYS_INLINE uint32_t CompressedValue() const {
     48     return value_;
     49   }
     50 
     51   ALWAYS_INLINE CodeOffset() = default;
     52   ALWAYS_INLINE CodeOffset(const CodeOffset&) = default;
     53   ALWAYS_INLINE CodeOffset& operator=(const CodeOffset&) = default;
     54   ALWAYS_INLINE CodeOffset& operator=(CodeOffset&&) = default;
     55 
     56  private:
     57   ALWAYS_INLINE explicit CodeOffset(uint32_t value) : value_(value) {}
     58 
     59   uint32_t value_ = 0u;
     60 };
     61 
     62 inline bool operator==(const CodeOffset& a, const CodeOffset& b) {
     63   return a.CompressedValue() == b.CompressedValue();
     64 }
     65 
     66 inline bool operator!=(const CodeOffset& a, const CodeOffset& b) {
     67   return !(a == b);
     68 }
     69 
     70 inline bool operator<(const CodeOffset& a, const CodeOffset& b) {
     71   return a.CompressedValue() < b.CompressedValue();
     72 }
     73 
     74 inline bool operator<=(const CodeOffset& a, const CodeOffset& b) {
     75   return a.CompressedValue() <= b.CompressedValue();
     76 }
     77 
     78 inline bool operator>(const CodeOffset& a, const CodeOffset& b) {
     79   return a.CompressedValue() > b.CompressedValue();
     80 }
     81 
     82 inline bool operator>=(const CodeOffset& a, const CodeOffset& b) {
     83   return a.CompressedValue() >= b.CompressedValue();
     84 }
     85 
     86 inline std::ostream& operator<<(std::ostream& os, const CodeOffset& offset) {
     87   return os << offset.Uint32Value();
     88 }
     89 
     90 }  // namespace art
     91 
     92 #endif  // ART_RUNTIME_ARCH_CODE_OFFSET_H_
     93