Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #ifndef ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |
| 19 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 20 | #include "base/allocator.h" |
| 21 | #include "base/arena_bit_vector.h" |
| 22 | #include "base/bit_table.h" |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 23 | #include "base/bit_vector-inl.h" |
David Sehr | 1ce2b3b | 2018-04-05 11:02:03 -0700 | [diff] [blame] | 24 | #include "base/memory_region.h" |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 25 | #include "base/scoped_arena_containers.h" |
Ian Rogers | 0279ebb | 2014-10-08 17:27:48 -0700 | [diff] [blame] | 26 | #include "base/value_object.h" |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 27 | #include "dex_register_location.h" |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 28 | #include "method_info.h" |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 29 | #include "nodes.h" |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
David Srbecky | 049d681 | 2018-05-18 14:46:49 +0100 | [diff] [blame] | 33 | class CodeInfo; |
Roland Levillain | a552e1c | 2015-03-26 15:01:03 +0000 | [diff] [blame] | 34 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 35 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 36 | * Collects and builds stack maps for a method. All the stack maps |
| 37 | * for a method are placed in a CodeInfo object. |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 38 | */ |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 39 | class StackMapStream : public ValueObject { |
| 40 | public: |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 41 | explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set) |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 42 | : instruction_set_(instruction_set), |
| 43 | stack_maps_(allocator), |
| 44 | register_masks_(allocator), |
| 45 | stack_masks_(allocator), |
| 46 | invoke_infos_(allocator), |
| 47 | inline_infos_(allocator), |
| 48 | dex_register_masks_(allocator), |
| 49 | dex_register_maps_(allocator), |
| 50 | dex_register_catalog_(allocator), |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 51 | out_(allocator->Adapter(kArenaAllocStackMapStream)), |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 52 | method_infos_(allocator), |
| 53 | lazy_stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)), |
| 54 | in_stack_map_(false), |
| 55 | in_inline_info_(false), |
David Srbecky | 6eb4d5e | 2018-06-03 12:00:20 +0100 | [diff] [blame^] | 56 | current_inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)), |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 57 | current_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)), |
| 58 | temp_dex_register_mask_(allocator, 32, true, kArenaAllocStackMapStream), |
| 59 | temp_dex_register_map_(allocator->Adapter(kArenaAllocStackMapStream)) { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 60 | } |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 61 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 62 | void BeginStackMapEntry(uint32_t dex_pc, |
| 63 | uint32_t native_pc_offset, |
| 64 | uint32_t register_mask, |
| 65 | BitVector* sp_mask, |
| 66 | uint32_t num_dex_registers, |
| 67 | uint8_t inlining_depth); |
| 68 | void EndStackMapEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 69 | |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 70 | void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value); |
Nicolas Geoffray | 004c230 | 2015-03-20 10:06:38 +0000 | [diff] [blame] | 71 | |
Mathieu Chartier | d776ff0 | 2017-01-17 09:32:18 -0800 | [diff] [blame] | 72 | void AddInvoke(InvokeType type, uint32_t dex_method_index); |
| 73 | |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 74 | void BeginInlineInfoEntry(ArtMethod* method, |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 75 | uint32_t dex_pc, |
Nicolas Geoffray | 5d37c15 | 2017-01-12 13:25:19 +0000 | [diff] [blame] | 76 | uint32_t num_dex_registers, |
| 77 | const DexFile* outer_dex_file = nullptr); |
Nicolas Geoffray | b1d0f3f | 2015-05-14 12:41:51 +0100 | [diff] [blame] | 78 | void EndInlineInfoEntry(); |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 79 | |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 80 | size_t GetNumberOfStackMaps() const { |
Vladimir Marko | 225b646 | 2015-09-28 12:17:40 +0100 | [diff] [blame] | 81 | return stack_maps_.size(); |
Vladimir Marko | bd8c725 | 2015-06-12 10:06:32 +0100 | [diff] [blame] | 82 | } |
| 83 | |
David Srbecky | d02b23f | 2018-05-29 23:27:22 +0100 | [diff] [blame] | 84 | uint32_t GetStackMapNativePcOffset(size_t i); |
| 85 | void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset); |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 86 | |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 87 | // Prepares the stream to fill in a memory region. Must be called before FillIn. |
| 88 | // Returns the size (in bytes) needed to store this stream. |
| 89 | size_t PrepareForFillIn(); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 90 | void FillInCodeInfo(MemoryRegion region); |
| 91 | void FillInMethodInfo(MemoryRegion region); |
| 92 | |
| 93 | size_t ComputeMethodInfoSize() const; |
Nicolas Geoffray | fead4e4 | 2015-03-13 14:39:40 +0000 | [diff] [blame] | 94 | |
Nicolas Geoffray | eeefa12 | 2015-03-13 18:52:59 +0000 | [diff] [blame] | 95 | private: |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 96 | static constexpr uint32_t kNoValue = -1; |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 97 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 98 | // The fields must be uint32_t and mirror the StackMap accessor in stack_map.h! |
| 99 | struct StackMapEntry { |
| 100 | uint32_t packed_native_pc; |
| 101 | uint32_t dex_pc; |
| 102 | uint32_t register_mask_index; |
| 103 | uint32_t stack_mask_index; |
| 104 | uint32_t inline_info_index; |
| 105 | uint32_t dex_register_mask_index; |
| 106 | uint32_t dex_register_map_index; |
| 107 | }; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 108 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 109 | // The fields must be uint32_t and mirror the InlineInfo accessor in stack_map.h! |
| 110 | struct InlineInfoEntry { |
| 111 | uint32_t is_last; |
| 112 | uint32_t dex_pc; |
| 113 | uint32_t method_info_index; |
| 114 | uint32_t art_method_hi; |
| 115 | uint32_t art_method_lo; |
| 116 | uint32_t dex_register_mask_index; |
| 117 | uint32_t dex_register_map_index; |
| 118 | }; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 119 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 120 | // The fields must be uint32_t and mirror the InvokeInfo accessor in stack_map.h! |
| 121 | struct InvokeInfoEntry { |
| 122 | uint32_t packed_native_pc; |
| 123 | uint32_t invoke_type; |
| 124 | uint32_t method_info_index; |
| 125 | }; |
Mathieu Chartier | 3228908 | 2017-02-09 15:57:37 -0800 | [diff] [blame] | 126 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 127 | // The fields must be uint32_t and mirror the DexRegisterInfo accessor in stack_map.h! |
| 128 | struct DexRegisterEntry { |
| 129 | uint32_t kind; |
| 130 | uint32_t packed_value; |
| 131 | }; |
| 132 | |
| 133 | // The fields must be uint32_t and mirror the RegisterMask accessor in stack_map.h! |
| 134 | struct RegisterMaskEntry { |
| 135 | uint32_t value; |
| 136 | uint32_t shift; |
| 137 | }; |
| 138 | |
| 139 | void CreateDexRegisterMap(); |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 140 | |
Mathieu Chartier | a2f526f | 2017-01-19 14:48:48 -0800 | [diff] [blame] | 141 | const InstructionSet instruction_set_; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 142 | BitTableBuilder<StackMapEntry> stack_maps_; |
| 143 | BitTableBuilder<RegisterMaskEntry> register_masks_; |
| 144 | BitmapTableBuilder stack_masks_; |
| 145 | BitTableBuilder<InvokeInfoEntry> invoke_infos_; |
| 146 | BitTableBuilder<InlineInfoEntry> inline_infos_; |
| 147 | BitmapTableBuilder dex_register_masks_; |
| 148 | BitTableBuilder<uint32_t> dex_register_maps_; |
| 149 | BitTableBuilder<DexRegisterEntry> dex_register_catalog_; |
David Srbecky | 052f8ca | 2018-04-26 15:42:54 +0100 | [diff] [blame] | 150 | ScopedArenaVector<uint8_t> out_; |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 151 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 152 | BitTableBuilder<uint32_t> method_infos_; |
Calin Juravle | 6ae7096 | 2015-03-18 16:31:28 +0000 | [diff] [blame] | 153 | |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 154 | ScopedArenaVector<BitVector*> lazy_stack_masks_; |
| 155 | |
| 156 | // Variables which track the current state between Begin/End calls; |
| 157 | bool in_stack_map_; |
| 158 | bool in_inline_info_; |
| 159 | StackMapEntry current_stack_map_; |
David Srbecky | 6eb4d5e | 2018-06-03 12:00:20 +0100 | [diff] [blame^] | 160 | ScopedArenaVector<InlineInfoEntry> current_inline_infos_; |
David Srbecky | 71ec1cc | 2018-05-18 15:57:25 +0100 | [diff] [blame] | 161 | ScopedArenaVector<DexRegisterLocation> current_dex_registers_; |
| 162 | size_t expected_num_dex_registers_; |
| 163 | |
| 164 | // Temporary variables used in CreateDexRegisterMap. |
| 165 | // They are here so that we can reuse the reserved memory. |
| 166 | ArenaBitVector temp_dex_register_mask_; |
| 167 | ScopedArenaVector<uint32_t> temp_dex_register_map_; |
| 168 | |
David Srbecky | 049d681 | 2018-05-18 14:46:49 +0100 | [diff] [blame] | 169 | // A set of lambda functions to be executed at the end to verify |
| 170 | // the encoded data. It is generally only used in debug builds. |
| 171 | std::vector<std::function<void(CodeInfo&)>> dchecks_; |
Calin Juravle | 4f46ac5 | 2015-04-23 18:47:21 +0100 | [diff] [blame] | 172 | |
Nicolas Geoffray | 99ea58c | 2014-07-02 15:08:17 +0100 | [diff] [blame] | 173 | DISALLOW_COPY_AND_ASSIGN(StackMapStream); |
| 174 | }; |
| 175 | |
| 176 | } // namespace art |
| 177 | |
| 178 | #endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_ |