• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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_OAT_QUICK_METHOD_HEADER_H_
18 #define ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
19 
20 #include "arch/instruction_set.h"
21 #include "base/macros.h"
22 #include "base/utils.h"
23 #include "quick/quick_method_frame_info.h"
24 #include "stack_map.h"
25 
26 namespace art {
27 
28 class ArtMethod;
29 
30 // OatQuickMethodHeader precedes the raw code chunk generated by the compiler.
31 class PACKED(4) OatQuickMethodHeader {
32  public:
33   OatQuickMethodHeader() = default;
OatQuickMethodHeader(uint32_t vmap_table_offset,uint32_t code_size)34   OatQuickMethodHeader(uint32_t vmap_table_offset,
35                        uint32_t code_size)
36       : vmap_table_offset_(vmap_table_offset),
37         code_size_(code_size) {
38   }
39 
FromCodePointer(const void * code_ptr)40   static OatQuickMethodHeader* FromCodePointer(const void* code_ptr) {
41     uintptr_t code = reinterpret_cast<uintptr_t>(code_ptr);
42     uintptr_t header = code - OFFSETOF_MEMBER(OatQuickMethodHeader, code_);
43     DCHECK(IsAlignedParam(code, GetInstructionSetAlignment(kRuntimeISA)) ||
44            IsAlignedParam(header, GetInstructionSetAlignment(kRuntimeISA)))
45         << std::hex << code << " " << std::hex << header;
46     return reinterpret_cast<OatQuickMethodHeader*>(header);
47   }
48 
FromEntryPoint(const void * entry_point)49   static OatQuickMethodHeader* FromEntryPoint(const void* entry_point) {
50     return FromCodePointer(EntryPointToCodePointer(entry_point));
51   }
52 
53   OatQuickMethodHeader(const OatQuickMethodHeader&) = default;
54   OatQuickMethodHeader& operator=(const OatQuickMethodHeader&) = default;
55 
NativeQuickPcOffset(const uintptr_t pc)56   uintptr_t NativeQuickPcOffset(const uintptr_t pc) const {
57     return pc - reinterpret_cast<uintptr_t>(GetEntryPoint());
58   }
59 
IsOptimized()60   bool IsOptimized() const {
61     return GetCodeSize() != 0 && vmap_table_offset_ != 0;
62   }
63 
GetOptimizedCodeInfoPtr()64   const uint8_t* GetOptimizedCodeInfoPtr() const {
65     DCHECK(IsOptimized());
66     return code_ - vmap_table_offset_;
67   }
68 
GetOptimizedCodeInfoPtr()69   uint8_t* GetOptimizedCodeInfoPtr() {
70     DCHECK(IsOptimized());
71     return code_ - vmap_table_offset_;
72   }
73 
GetCode()74   const uint8_t* GetCode() const {
75     return code_;
76   }
77 
GetCodeSize()78   uint32_t GetCodeSize() const {
79     return code_size_ & kCodeSizeMask;
80   }
81 
GetCodeSizeAddr()82   const uint32_t* GetCodeSizeAddr() const {
83     return &code_size_;
84   }
85 
GetVmapTableOffset()86   uint32_t GetVmapTableOffset() const {
87     return vmap_table_offset_;
88   }
89 
SetVmapTableOffset(uint32_t offset)90   void SetVmapTableOffset(uint32_t offset) {
91     vmap_table_offset_ = offset;
92   }
93 
GetVmapTableOffsetAddr()94   const uint32_t* GetVmapTableOffsetAddr() const {
95     return &vmap_table_offset_;
96   }
97 
GetVmapTable()98   const uint8_t* GetVmapTable() const {
99     CHECK(!IsOptimized()) << "Unimplemented vmap table for optimizing compiler";
100     return (vmap_table_offset_ == 0) ? nullptr : code_ - vmap_table_offset_;
101   }
102 
Contains(uintptr_t pc)103   bool Contains(uintptr_t pc) const {
104     uintptr_t code_start = reinterpret_cast<uintptr_t>(code_);
105     static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
106     if (kRuntimeISA == InstructionSet::kArm) {
107       // On Thumb-2, the pc is offset by one.
108       code_start++;
109     }
110     return code_start <= pc && pc <= (code_start + GetCodeSize());
111   }
112 
GetEntryPoint()113   const uint8_t* GetEntryPoint() const {
114     // When the runtime architecture is ARM, `kRuntimeISA` is set to `kArm`
115     // (not `kThumb2`), *but* we always generate code for the Thumb-2
116     // instruction set anyway. Thumb-2 requires the entrypoint to be of
117     // offset 1.
118     static_assert(kRuntimeISA != InstructionSet::kThumb2, "kThumb2 cannot be a runtime ISA");
119     return (kRuntimeISA == InstructionSet::kArm)
120         ? reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(code_) | 1)
121         : code_;
122   }
123 
124   template <bool kCheckFrameSize = true>
GetFrameSizeInBytes()125   uint32_t GetFrameSizeInBytes() const {
126     uint32_t result = GetFrameInfo().FrameSizeInBytes();
127     if (kCheckFrameSize) {
128       DCHECK_ALIGNED(result, kStackAlignment);
129     }
130     return result;
131   }
132 
GetFrameInfo()133   QuickMethodFrameInfo GetFrameInfo() const {
134     DCHECK(IsOptimized());
135     return CodeInfo::DecodeFrameInfo(GetOptimizedCodeInfoPtr());
136   }
137 
138   uintptr_t ToNativeQuickPc(ArtMethod* method,
139                             const uint32_t dex_pc,
140                             bool is_for_catch_handler,
141                             bool abort_on_failure = true) const;
142 
143   uint32_t ToDexPc(ArtMethod* method, const uintptr_t pc, bool abort_on_failure = true) const;
144 
SetHasShouldDeoptimizeFlag()145   void SetHasShouldDeoptimizeFlag() {
146     DCHECK_EQ(code_size_ & kShouldDeoptimizeMask, 0u);
147     code_size_ |= kShouldDeoptimizeMask;
148   }
149 
HasShouldDeoptimizeFlag()150   bool HasShouldDeoptimizeFlag() const {
151     return (code_size_ & kShouldDeoptimizeMask) != 0;
152   }
153 
154  private:
155   static constexpr uint32_t kShouldDeoptimizeMask = 0x80000000;
156   static constexpr uint32_t kCodeSizeMask = ~kShouldDeoptimizeMask;
157 
158   // The offset in bytes from the start of the vmap table to the end of the header.
159   uint32_t vmap_table_offset_ = 0u;
160   // The code size in bytes. The highest bit is used to signify if the compiled
161   // code with the method header has should_deoptimize flag.
162   uint32_t code_size_ = 0u;
163   // The actual code.
164   uint8_t code_[0];
165 };
166 
167 }  // namespace art
168 
169 #endif  // ART_RUNTIME_OAT_QUICK_METHOD_HEADER_H_
170