Home | History | Annotate | Download | only in compiler
      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 "driver/compiler_driver.h"
     21 #include "utils/swap_space.h"
     22 
     23 namespace art {
     24 
     25 CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
     26                            const ArrayRef<const uint8_t>& quick_code)
     27     : compiler_driver_(compiler_driver),
     28       instruction_set_(instruction_set),
     29       quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)) {
     30 }
     31 
     32 CompiledCode::~CompiledCode() {
     33   compiler_driver_->GetCompiledMethodStorage()->ReleaseCode(quick_code_);
     34 }
     35 
     36 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 
     49 size_t CompiledCode::AlignCode(size_t offset) const {
     50   return AlignCode(offset, instruction_set_);
     51 }
     52 
     53 size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
     54   return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
     55 }
     56 
     57 size_t CompiledCode::CodeDelta() const {
     58   return CodeDelta(instruction_set_);
     59 }
     60 
     61 size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
     62   switch (instruction_set) {
     63     case kArm:
     64     case kArm64:
     65     case kMips:
     66     case kMips64:
     67     case kX86:
     68     case kX86_64:
     69       return 0;
     70     case 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       return 0;
     77   }
     78 }
     79 
     80 const void* CompiledCode::CodePointer(const void* code_pointer,
     81                                       InstructionSet instruction_set) {
     82   switch (instruction_set) {
     83     case kArm:
     84     case kArm64:
     85     case kMips:
     86     case kMips64:
     87     case kX86:
     88     case kX86_64:
     89       return code_pointer;
     90     case kThumb2: {
     91       uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
     92       // Set the low-order bit so a BLX will switch to Thumb mode
     93       address |= 0x1;
     94       return reinterpret_cast<const void*>(address);
     95     }
     96     default:
     97       LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
     98       return nullptr;
     99   }
    100 }
    101 
    102 CompiledMethod::CompiledMethod(CompilerDriver* driver,
    103                                InstructionSet instruction_set,
    104                                const ArrayRef<const uint8_t>& quick_code,
    105                                const size_t frame_size_in_bytes,
    106                                const uint32_t core_spill_mask,
    107                                const uint32_t fp_spill_mask,
    108                                const ArrayRef<const SrcMapElem>& src_mapping_table,
    109                                const ArrayRef<const uint8_t>& vmap_table,
    110                                const ArrayRef<const uint8_t>& cfi_info,
    111                                const ArrayRef<const LinkerPatch>& patches)
    112     : CompiledCode(driver, instruction_set, quick_code),
    113       frame_size_in_bytes_(frame_size_in_bytes), core_spill_mask_(core_spill_mask),
    114       fp_spill_mask_(fp_spill_mask),
    115       src_mapping_table_(
    116           driver->GetCompiledMethodStorage()->DeduplicateSrcMappingTable(src_mapping_table)),
    117       vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)),
    118       cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)),
    119       patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) {
    120 }
    121 
    122 CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
    123     CompilerDriver* driver,
    124     InstructionSet instruction_set,
    125     const ArrayRef<const uint8_t>& quick_code,
    126     const size_t frame_size_in_bytes,
    127     const uint32_t core_spill_mask,
    128     const uint32_t fp_spill_mask,
    129     const ArrayRef<const SrcMapElem>& src_mapping_table,
    130     const ArrayRef<const uint8_t>& vmap_table,
    131     const ArrayRef<const uint8_t>& cfi_info,
    132     const ArrayRef<const LinkerPatch>& patches) {
    133   SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
    134   CompiledMethod* ret = alloc.allocate(1);
    135   alloc.construct(ret,
    136                   driver,
    137                   instruction_set,
    138                   quick_code,
    139                   frame_size_in_bytes,
    140                   core_spill_mask,
    141                   fp_spill_mask,
    142                   src_mapping_table,
    143                   vmap_table,
    144                   cfi_info, patches);
    145   return ret;
    146 }
    147 
    148 void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
    149   SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
    150   alloc.destroy(m);
    151   alloc.deallocate(m, 1);
    152 }
    153 
    154 CompiledMethod::~CompiledMethod() {
    155   CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage();
    156   storage->ReleaseLinkerPatches(patches_);
    157   storage->ReleaseCFIInfo(cfi_info_);
    158   storage->ReleaseVMapTable(vmap_table_);
    159   storage->ReleaseSrcMappingTable(src_mapping_table_);
    160 }
    161 
    162 }  // namespace art
    163