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::kMips:
66 case InstructionSet::kMips64:
67 case InstructionSet::kX86:
68 case InstructionSet::kX86_64:
69 return 0;
70 case InstructionSet::kThumb2: {
71 // +1 to set the low-order bit so a BLX will switch to Thumb mode
72 return 1;
73 }
74 default:
75 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
76 UNREACHABLE();
77 }
78 }
79
CodePointer(const void * code_pointer,InstructionSet instruction_set)80 const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
81 switch (instruction_set) {
82 case InstructionSet::kArm:
83 case InstructionSet::kArm64:
84 case InstructionSet::kMips:
85 case InstructionSet::kMips64:
86 case InstructionSet::kX86:
87 case InstructionSet::kX86_64:
88 return code_pointer;
89 case InstructionSet::kThumb2: {
90 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
91 // Set the low-order bit so a BLX will switch to Thumb mode
92 address |= 0x1;
93 return reinterpret_cast<const void*>(address);
94 }
95 default:
96 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
97 UNREACHABLE();
98 }
99 }
100
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)101 CompiledMethod::CompiledMethod(CompiledMethodStorage* storage,
102 InstructionSet instruction_set,
103 const ArrayRef<const uint8_t>& quick_code,
104 const ArrayRef<const uint8_t>& vmap_table,
105 const ArrayRef<const uint8_t>& cfi_info,
106 const ArrayRef<const linker::LinkerPatch>& patches)
107 : CompiledCode(storage, instruction_set, quick_code),
108 vmap_table_(storage->DeduplicateVMapTable(vmap_table)),
109 cfi_info_(storage->DeduplicateCFIInfo(cfi_info)),
110 patches_(storage->DeduplicateLinkerPatches(patches)) {
111 }
112
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)113 CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
114 CompiledMethodStorage* storage,
115 InstructionSet instruction_set,
116 const ArrayRef<const uint8_t>& quick_code,
117 const ArrayRef<const uint8_t>& vmap_table,
118 const ArrayRef<const uint8_t>& cfi_info,
119 const ArrayRef<const linker::LinkerPatch>& patches) {
120 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
121 CompiledMethod* ret = alloc.allocate(1);
122 alloc.construct(ret,
123 storage,
124 instruction_set,
125 quick_code,
126 vmap_table,
127 cfi_info, patches);
128 return ret;
129 }
130
ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage * storage,CompiledMethod * m)131 void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage,
132 CompiledMethod* m) {
133 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
134 alloc.destroy(m);
135 alloc.deallocate(m, 1);
136 }
137
~CompiledMethod()138 CompiledMethod::~CompiledMethod() {
139 CompiledMethodStorage* storage = GetStorage();
140 storage->ReleaseLinkerPatches(patches_);
141 storage->ReleaseCFIInfo(cfi_info_);
142 storage->ReleaseVMapTable(vmap_table_);
143 }
144
145 } // namespace art
146