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 #include "compiled_method.h"
18 
19 #include "driver/compiled_method_storage.h"
20 #include "utils/swap_space.h"
21 
22 namespace art {
23 
CompiledCode(CompiledMethodStorage * storage,InstructionSet instruction_set,const ArrayRef<const uint8_t> & quick_code)24 CompiledCode::CompiledCode(CompiledMethodStorage* storage,
25                            InstructionSet instruction_set,
26                            const ArrayRef<const uint8_t>& quick_code)
27     : storage_(storage),
28       quick_code_(storage->DeduplicateCode(quick_code)),
29       packed_fields_(InstructionSetField::Encode(instruction_set)) {
30 }
31 
~CompiledCode()32 CompiledCode::~CompiledCode() {
33   GetStorage()->ReleaseCode(quick_code_);
34 }
35 
operator ==(const CompiledCode & rhs) const36 bool CompiledCode::operator==(const CompiledCode& rhs) const {
37   if (quick_code_ != nullptr) {
38     if (rhs.quick_code_ == nullptr) {
39       return false;
40     } else if (quick_code_->size() != rhs.quick_code_->size()) {
41       return false;
42     } else {
43       return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin());
44     }
45   }
46   return (rhs.quick_code_ == nullptr);
47 }
48 
AlignCode(size_t offset) const49 size_t CompiledCode::AlignCode(size_t offset) const {
50   return AlignCode(offset, GetInstructionSet());
51 }
52 
AlignCode(size_t offset,InstructionSet instruction_set)53 size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
54   return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
55 }
56 
CodeDelta() const57 size_t CompiledCode::CodeDelta() const {
58   return CodeDelta(GetInstructionSet());
59 }
60 
CodeDelta(InstructionSet instruction_set)61 size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
62   switch (instruction_set) {
63     case InstructionSet::kArm:
64     case InstructionSet::kArm64:
65     case InstructionSet::kX86:
66     case InstructionSet::kX86_64:
67       return 0;
68     case InstructionSet::kThumb2: {
69       // +1 to set the low-order bit so a BLX will switch to Thumb mode
70       return 1;
71     }
72     default:
73       LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
74       UNREACHABLE();
75   }
76 }
77 
CodePointer(const void * code_pointer,InstructionSet instruction_set)78 const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
79   switch (instruction_set) {
80     case InstructionSet::kArm:
81     case InstructionSet::kArm64:
82     case InstructionSet::kX86:
83     case InstructionSet::kX86_64:
84       return code_pointer;
85     case InstructionSet::kThumb2: {
86       uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
87       // Set the low-order bit so a BLX will switch to Thumb mode
88       address |= 0x1;
89       return reinterpret_cast<const void*>(address);
90     }
91     default:
92       LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
93       UNREACHABLE();
94   }
95 }
96 
CompiledMethod(CompiledMethodStorage * storage,InstructionSet instruction_set,const ArrayRef<const uint8_t> & quick_code,const ArrayRef<const uint8_t> & vmap_table,const ArrayRef<const uint8_t> & cfi_info,const ArrayRef<const linker::LinkerPatch> & patches)97 CompiledMethod::CompiledMethod(CompiledMethodStorage* storage,
98                                InstructionSet instruction_set,
99                                const ArrayRef<const uint8_t>& quick_code,
100                                const ArrayRef<const uint8_t>& vmap_table,
101                                const ArrayRef<const uint8_t>& cfi_info,
102                                const ArrayRef<const linker::LinkerPatch>& patches)
103     : CompiledCode(storage, instruction_set, quick_code),
104       vmap_table_(storage->DeduplicateVMapTable(vmap_table)),
105       cfi_info_(storage->DeduplicateCFIInfo(cfi_info)),
106       patches_(storage->DeduplicateLinkerPatches(patches)) {
107 }
108 
SwapAllocCompiledMethod(CompiledMethodStorage * storage,InstructionSet instruction_set,const ArrayRef<const uint8_t> & quick_code,const ArrayRef<const uint8_t> & vmap_table,const ArrayRef<const uint8_t> & cfi_info,const ArrayRef<const linker::LinkerPatch> & patches)109 CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
110     CompiledMethodStorage* storage,
111     InstructionSet instruction_set,
112     const ArrayRef<const uint8_t>& quick_code,
113     const ArrayRef<const uint8_t>& vmap_table,
114     const ArrayRef<const uint8_t>& cfi_info,
115     const ArrayRef<const linker::LinkerPatch>& patches) {
116   SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
117   CompiledMethod* ret = alloc.allocate(1);
118   alloc.construct(ret,
119                   storage,
120                   instruction_set,
121                   quick_code,
122                   vmap_table,
123                   cfi_info, patches);
124   return ret;
125 }
126 
ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage * storage,CompiledMethod * m)127 void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage,
128                                                         CompiledMethod* m) {
129   SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
130   alloc.destroy(m);
131   alloc.deallocate(m, 1);
132 }
133 
~CompiledMethod()134 CompiledMethod::~CompiledMethod() {
135   CompiledMethodStorage* storage = GetStorage();
136   storage->ReleaseLinkerPatches(patches_);
137   storage->ReleaseCFIInfo(cfi_info_);
138   storage->ReleaseVMapTable(vmap_table_);
139 }
140 
141 }  // namespace art
142