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