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,
     26                            InstructionSet instruction_set,
     27                            const ArrayRef<const uint8_t>& quick_code)
     28     : compiler_driver_(compiler_driver),
     29       quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)),
     30       packed_fields_(InstructionSetField::Encode(instruction_set)) {
     31 }
     32 
     33 CompiledCode::~CompiledCode() {
     34   compiler_driver_->GetCompiledMethodStorage()->ReleaseCode(quick_code_);
     35 }
     36 
     37 bool CompiledCode::operator==(const CompiledCode& rhs) const {
     38   if (quick_code_ != nullptr) {
     39     if (rhs.quick_code_ == nullptr) {
     40       return false;
     41     } else if (quick_code_->size() != rhs.quick_code_->size()) {
     42       return false;
     43     } else {
     44       return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin());
     45     }
     46   }
     47   return (rhs.quick_code_ == nullptr);
     48 }
     49 
     50 size_t CompiledCode::AlignCode(size_t offset) const {
     51   return AlignCode(offset, GetInstructionSet());
     52 }
     53 
     54 size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
     55   return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
     56 }
     57 
     58 size_t CompiledCode::CodeDelta() const {
     59   return CodeDelta(GetInstructionSet());
     60 }
     61 
     62 size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
     63   switch (instruction_set) {
     64     case InstructionSet::kArm:
     65     case InstructionSet::kArm64:
     66     case InstructionSet::kMips:
     67     case InstructionSet::kMips64:
     68     case InstructionSet::kX86:
     69     case InstructionSet::kX86_64:
     70       return 0;
     71     case InstructionSet::kThumb2: {
     72       // +1 to set the low-order bit so a BLX will switch to Thumb mode
     73       return 1;
     74     }
     75     default:
     76       LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
     77       return 0;
     78   }
     79 }
     80 
     81 const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
     82   switch (instruction_set) {
     83     case InstructionSet::kArm:
     84     case InstructionSet::kArm64:
     85     case InstructionSet::kMips:
     86     case InstructionSet::kMips64:
     87     case InstructionSet::kX86:
     88     case InstructionSet::kX86_64:
     89       return code_pointer;
     90     case InstructionSet::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 uint8_t>& method_info,
    109                                const ArrayRef<const uint8_t>& vmap_table,
    110                                const ArrayRef<const uint8_t>& cfi_info,
    111                                const ArrayRef<const linker::LinkerPatch>& patches)
    112     : CompiledCode(driver, instruction_set, quick_code),
    113       frame_size_in_bytes_(frame_size_in_bytes),
    114       core_spill_mask_(core_spill_mask),
    115       fp_spill_mask_(fp_spill_mask),
    116       method_info_(driver->GetCompiledMethodStorage()->DeduplicateMethodInfo(method_info)),
    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 uint8_t>& method_info,
    130     const ArrayRef<const uint8_t>& vmap_table,
    131     const ArrayRef<const uint8_t>& cfi_info,
    132     const ArrayRef<const linker::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                   method_info,
    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->ReleaseMethodInfo(method_info_);
    160 }
    161 
    162 }  // namespace art
    163