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 
20 #include "base/allocator.h"
21 #include "base/arena_bit_vector.h"
22 #include "base/bit_table.h"
23 #include "base/bit_vector-inl.h"
24 #include "base/memory_region.h"
25 #include "base/scoped_arena_containers.h"
26 #include "base/value_object.h"
27 #include "dex_register_location.h"
28 #include "nodes.h"
29 #include "stack_map.h"
30 
31 namespace art {
32 
33 /**
34  * Collects and builds stack maps for a method. All the stack maps
35  * for a method are placed in a CodeInfo object.
36  */
37 class StackMapStream : public DeletableArenaObject<kArenaAllocStackMapStream> {
38  public:
StackMapStream(ScopedArenaAllocator * allocator,InstructionSet instruction_set)39   explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set)
40       : allocator_(allocator),
41         instruction_set_(instruction_set),
42         stack_maps_(allocator),
43         register_masks_(allocator),
44         stack_masks_(allocator),
45         inline_infos_(allocator),
46         method_infos_(allocator),
47         dex_register_masks_(allocator),
48         dex_register_maps_(allocator),
49         dex_register_catalog_(allocator),
50         lazy_stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)),
51         current_stack_map_(),
52         current_inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)),
53         current_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
54         previous_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
55         dex_register_timestamp_(allocator->Adapter(kArenaAllocStackMapStream)),
56         expected_num_dex_registers_(0u),
57         temp_dex_register_mask_(allocator, 32, true, kArenaAllocStackMapStream),
58         temp_dex_register_map_(allocator->Adapter(kArenaAllocStackMapStream)) {
59   }
60 
61   void BeginMethod(size_t frame_size_in_bytes,
62                    size_t core_spill_mask,
63                    size_t fp_spill_mask,
64                    uint32_t num_dex_registers,
65                    bool baseline = false);
66   void EndMethod(size_t code_size);
67 
68   void BeginStackMapEntry(uint32_t dex_pc,
69                           uint32_t native_pc_offset,
70                           uint32_t register_mask = 0,
71                           BitVector* sp_mask = nullptr,
72                           StackMap::Kind kind = StackMap::Kind::Default,
73                           bool needs_vreg_info = true);
74   void EndStackMapEntry();
75 
AddDexRegisterEntry(DexRegisterLocation::Kind kind,int32_t value)76   void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
77     current_dex_registers_.push_back(DexRegisterLocation(kind, value));
78   }
79 
80   void BeginInlineInfoEntry(ArtMethod* method,
81                             uint32_t dex_pc,
82                             uint32_t num_dex_registers,
83                             const DexFile* outer_dex_file = nullptr);
84   void EndInlineInfoEntry();
85 
GetNumberOfStackMaps()86   size_t GetNumberOfStackMaps() const {
87     return stack_maps_.size();
88   }
89 
90   uint32_t GetStackMapNativePcOffset(size_t i);
91   void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset);
92 
93   // Encode all stack map data.
94   // The returned vector is allocated using the allocator passed to the StackMapStream.
95   ScopedArenaVector<uint8_t> Encode();
96 
97  private:
98   static constexpr uint32_t kNoValue = -1;
99 
100   void CreateDexRegisterMap();
101 
102   // Invokes the callback with pointer of each BitTableBuilder field.
103   template<typename Callback>
ForEachBitTable(Callback callback)104   void ForEachBitTable(Callback callback) {
105     size_t index = 0;
106     callback(index++, &stack_maps_);
107     callback(index++, &register_masks_);
108     callback(index++, &stack_masks_);
109     callback(index++, &inline_infos_);
110     callback(index++, &method_infos_);
111     callback(index++, &dex_register_masks_);
112     callback(index++, &dex_register_maps_);
113     callback(index++, &dex_register_catalog_);
114     CHECK_EQ(index, CodeInfo::kNumBitTables);
115   }
116 
117   ScopedArenaAllocator* allocator_;
118   const InstructionSet instruction_set_;
119   uint32_t code_size_ = 0;
120   uint32_t packed_frame_size_ = 0;
121   uint32_t core_spill_mask_ = 0;
122   uint32_t fp_spill_mask_ = 0;
123   uint32_t num_dex_registers_ = 0;
124   bool baseline_;
125   BitTableBuilder<StackMap> stack_maps_;
126   BitTableBuilder<RegisterMask> register_masks_;
127   BitmapTableBuilder stack_masks_;
128   BitTableBuilder<InlineInfo> inline_infos_;
129   BitTableBuilder<MethodInfo> method_infos_;
130   BitmapTableBuilder dex_register_masks_;
131   BitTableBuilder<DexRegisterMapInfo> dex_register_maps_;
132   BitTableBuilder<DexRegisterInfo> dex_register_catalog_;
133 
134   ScopedArenaVector<BitVector*> lazy_stack_masks_;
135 
136   // Variables which track the current state between Begin/End calls;
137   bool in_method_ = false;
138   bool in_stack_map_ = false;
139   bool in_inline_info_ = false;
140   BitTableBuilder<StackMap>::Entry current_stack_map_;
141   ScopedArenaVector<BitTableBuilder<InlineInfo>::Entry> current_inline_infos_;
142   ScopedArenaVector<DexRegisterLocation> current_dex_registers_;
143   ScopedArenaVector<DexRegisterLocation> previous_dex_registers_;
144   ScopedArenaVector<uint32_t> dex_register_timestamp_;  // Stack map index of last change.
145   size_t expected_num_dex_registers_;
146 
147   // Temporary variables used in CreateDexRegisterMap.
148   // They are here so that we can reuse the reserved memory.
149   ArenaBitVector temp_dex_register_mask_;
150   ScopedArenaVector<BitTableBuilder<DexRegisterMapInfo>::Entry> temp_dex_register_map_;
151 
152   // A set of lambda functions to be executed at the end to verify
153   // the encoded data. It is generally only used in debug builds.
154   std::vector<std::function<void(CodeInfo&)>> dchecks_;
155 
156   DISALLOW_COPY_AND_ASSIGN(StackMapStream);
157 };
158 
159 }  // namespace art
160 
161 #endif  // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_
162