| Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | // TODO: Dex helpers have ART specific APIs, we may want to refactor these for use in dexdump. |
| 18 | |
| 19 | #ifndef ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |
| 20 | #define ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |
| 21 | |
| 22 | #include "base/mutex.h" |
| 23 | #include "cdex/compact_dex_file.h" |
| 24 | #include "dex_file.h" |
| 25 | #include "dex_instruction_iterator.h" |
| 26 | #include "standard_dex_file.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class ArtMethod; |
| 31 | |
| 32 | // Abstracts accesses to the instruction fields of code items for CompactDexFile and |
| 33 | // StandardDexFile. |
| 34 | class CodeItemInstructionAccessor { |
| 35 | public: |
| 36 | ALWAYS_INLINE CodeItemInstructionAccessor(const DexFile* dex_file, |
| 37 | const DexFile::CodeItem* code_item); |
| 38 | |
| 39 | ALWAYS_INLINE explicit CodeItemInstructionAccessor(ArtMethod* method); |
| 40 | |
| 41 | ALWAYS_INLINE DexInstructionIterator begin() const; |
| 42 | |
| 43 | ALWAYS_INLINE DexInstructionIterator end() const; |
| 44 | |
| 45 | uint32_t InsnsSizeInCodeUnits() const { |
| 46 | return insns_size_in_code_units_; |
| 47 | } |
| 48 | |
| 49 | const uint16_t* Insns() const { |
| 50 | return insns_; |
| 51 | } |
| 52 | |
| 53 | // Return true if the accessor has a code item. |
| 54 | bool HasCodeItem() const { |
| 55 | return Insns() != nullptr; |
| 56 | } |
| 57 | |
| 58 | // CreateNullable allows ArtMethods that have a null code item. |
| 59 | ALWAYS_INLINE static CodeItemInstructionAccessor CreateNullable(ArtMethod* method) |
| 60 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 61 | |
| 62 | protected: |
| 63 | CodeItemInstructionAccessor() = default; |
| 64 | |
| 65 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 66 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
| 67 | ALWAYS_INLINE void Init(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 68 | |
| 69 | private: |
| 70 | // size of the insns array, in 2 byte code units. 0 if there is no code item. |
| 71 | uint32_t insns_size_in_code_units_ = 0; |
| 72 | |
| 73 | // Pointer to the instructions, null if there is no code item. |
| 74 | const uint16_t* insns_ = 0; |
| 75 | }; |
| 76 | |
| 77 | // Abstracts accesses to code item fields other than debug info for CompactDexFile and |
| 78 | // StandardDexFile. |
| 79 | class CodeItemDataAccessor : public CodeItemInstructionAccessor { |
| 80 | public: |
| 81 | ALWAYS_INLINE CodeItemDataAccessor(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 82 | |
| 83 | ALWAYS_INLINE explicit CodeItemDataAccessor(ArtMethod* method); |
| 84 | |
| 85 | uint16_t RegistersSize() const { |
| 86 | return registers_size_; |
| 87 | } |
| 88 | |
| 89 | uint16_t InsSize() const { |
| 90 | return ins_size_; |
| 91 | } |
| 92 | |
| 93 | uint16_t OutsSize() const { |
| 94 | return outs_size_; |
| 95 | } |
| 96 | |
| 97 | uint16_t TriesSize() const { |
| 98 | return tries_size_; |
| 99 | } |
| 100 | |
| 101 | // CreateNullable allows ArtMethods that have a null code item. |
| 102 | ALWAYS_INLINE static CodeItemDataAccessor CreateNullable(ArtMethod* method) |
| 103 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 104 | |
| 105 | protected: |
| 106 | CodeItemDataAccessor() = default; |
| 107 | |
| 108 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 109 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
| 110 | ALWAYS_INLINE void Init(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 111 | |
| 112 | private: |
| 113 | // Fields mirrored from the dex/cdex code item. |
| 114 | uint16_t registers_size_; |
| 115 | uint16_t ins_size_; |
| 116 | uint16_t outs_size_; |
| 117 | uint16_t tries_size_; |
| 118 | }; |
| 119 | |
| 120 | } // namespace art |
| 121 | |
| 122 | #endif // ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |