1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "compiled_method.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "driver/compiled_method_storage.h"
20*795d594fSAndroid Build Coastguard Worker #include "utils/swap_space.h"
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker namespace art {
23*795d594fSAndroid Build Coastguard Worker
CompiledCode(CompiledMethodStorage * storage,InstructionSet instruction_set,const ArrayRef<const uint8_t> & quick_code)24*795d594fSAndroid Build Coastguard Worker CompiledCode::CompiledCode(CompiledMethodStorage* storage,
25*795d594fSAndroid Build Coastguard Worker InstructionSet instruction_set,
26*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& quick_code)
27*795d594fSAndroid Build Coastguard Worker : storage_(storage),
28*795d594fSAndroid Build Coastguard Worker quick_code_(storage->DeduplicateCode(quick_code)),
29*795d594fSAndroid Build Coastguard Worker packed_fields_(InstructionSetField::Encode(instruction_set)) {
30*795d594fSAndroid Build Coastguard Worker }
31*795d594fSAndroid Build Coastguard Worker
~CompiledCode()32*795d594fSAndroid Build Coastguard Worker CompiledCode::~CompiledCode() {
33*795d594fSAndroid Build Coastguard Worker GetStorage()->ReleaseCode(quick_code_);
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker
operator ==(const CompiledCode & rhs) const36*795d594fSAndroid Build Coastguard Worker bool CompiledCode::operator==(const CompiledCode& rhs) const {
37*795d594fSAndroid Build Coastguard Worker if (quick_code_ != nullptr) {
38*795d594fSAndroid Build Coastguard Worker if (rhs.quick_code_ == nullptr) {
39*795d594fSAndroid Build Coastguard Worker return false;
40*795d594fSAndroid Build Coastguard Worker } else if (quick_code_->size() != rhs.quick_code_->size()) {
41*795d594fSAndroid Build Coastguard Worker return false;
42*795d594fSAndroid Build Coastguard Worker } else {
43*795d594fSAndroid Build Coastguard Worker return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin());
44*795d594fSAndroid Build Coastguard Worker }
45*795d594fSAndroid Build Coastguard Worker }
46*795d594fSAndroid Build Coastguard Worker return (rhs.quick_code_ == nullptr);
47*795d594fSAndroid Build Coastguard Worker }
48*795d594fSAndroid Build Coastguard Worker
AlignCode(size_t offset) const49*795d594fSAndroid Build Coastguard Worker size_t CompiledCode::AlignCode(size_t offset) const {
50*795d594fSAndroid Build Coastguard Worker return AlignCode(offset, GetInstructionSet());
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker
AlignCode(size_t offset,InstructionSet instruction_set)53*795d594fSAndroid Build Coastguard Worker size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
54*795d594fSAndroid Build Coastguard Worker return RoundUp(offset, GetInstructionSetCodeAlignment(instruction_set));
55*795d594fSAndroid Build Coastguard Worker }
56*795d594fSAndroid Build Coastguard Worker
GetEntryPointAdjustment() const57*795d594fSAndroid Build Coastguard Worker size_t CompiledCode::GetEntryPointAdjustment() const {
58*795d594fSAndroid Build Coastguard Worker return GetInstructionSetEntryPointAdjustment(GetInstructionSet());
59*795d594fSAndroid Build Coastguard Worker }
60*795d594fSAndroid Build Coastguard Worker
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)61*795d594fSAndroid Build Coastguard Worker CompiledMethod::CompiledMethod(CompiledMethodStorage* storage,
62*795d594fSAndroid Build Coastguard Worker InstructionSet instruction_set,
63*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& quick_code,
64*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& vmap_table,
65*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& cfi_info,
66*795d594fSAndroid Build Coastguard Worker const ArrayRef<const linker::LinkerPatch>& patches)
67*795d594fSAndroid Build Coastguard Worker : CompiledCode(storage, instruction_set, quick_code),
68*795d594fSAndroid Build Coastguard Worker vmap_table_(storage->DeduplicateVMapTable(vmap_table)),
69*795d594fSAndroid Build Coastguard Worker cfi_info_(storage->DeduplicateCFIInfo(cfi_info)),
70*795d594fSAndroid Build Coastguard Worker patches_(storage->DeduplicateLinkerPatches(patches)) {
71*795d594fSAndroid Build Coastguard Worker }
72*795d594fSAndroid Build Coastguard Worker
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)73*795d594fSAndroid Build Coastguard Worker CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
74*795d594fSAndroid Build Coastguard Worker CompiledMethodStorage* storage,
75*795d594fSAndroid Build Coastguard Worker InstructionSet instruction_set,
76*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& quick_code,
77*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& vmap_table,
78*795d594fSAndroid Build Coastguard Worker const ArrayRef<const uint8_t>& cfi_info,
79*795d594fSAndroid Build Coastguard Worker const ArrayRef<const linker::LinkerPatch>& patches) {
80*795d594fSAndroid Build Coastguard Worker SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
81*795d594fSAndroid Build Coastguard Worker CompiledMethod* ret = alloc.allocate(1);
82*795d594fSAndroid Build Coastguard Worker alloc.construct(ret,
83*795d594fSAndroid Build Coastguard Worker storage,
84*795d594fSAndroid Build Coastguard Worker instruction_set,
85*795d594fSAndroid Build Coastguard Worker quick_code,
86*795d594fSAndroid Build Coastguard Worker vmap_table,
87*795d594fSAndroid Build Coastguard Worker cfi_info, patches);
88*795d594fSAndroid Build Coastguard Worker return ret;
89*795d594fSAndroid Build Coastguard Worker }
90*795d594fSAndroid Build Coastguard Worker
ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage * storage,CompiledMethod * m)91*795d594fSAndroid Build Coastguard Worker void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage,
92*795d594fSAndroid Build Coastguard Worker CompiledMethod* m) {
93*795d594fSAndroid Build Coastguard Worker SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
94*795d594fSAndroid Build Coastguard Worker alloc.destroy(m);
95*795d594fSAndroid Build Coastguard Worker alloc.deallocate(m, 1);
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker
~CompiledMethod()98*795d594fSAndroid Build Coastguard Worker CompiledMethod::~CompiledMethod() {
99*795d594fSAndroid Build Coastguard Worker CompiledMethodStorage* storage = GetStorage();
100*795d594fSAndroid Build Coastguard Worker storage->ReleaseLinkerPatches(patches_);
101*795d594fSAndroid Build Coastguard Worker storage->ReleaseCFIInfo(cfi_info_);
102*795d594fSAndroid Build Coastguard Worker storage->ReleaseVMapTable(vmap_table_);
103*795d594fSAndroid Build Coastguard Worker }
104*795d594fSAndroid Build Coastguard Worker
105*795d594fSAndroid Build Coastguard Worker } // namespace art
106