blob: 33c624a36cae71a752bb8f58df5f0fc2b1bbbdee [file] [log] [blame]
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +01001/*
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 Srbecky71ec1cc2018-05-18 15:57:25 +010020#include "base/allocator.h"
21#include "base/arena_bit_vector.h"
22#include "base/bit_table.h"
Calin Juravle6ae70962015-03-18 16:31:28 +000023#include "base/bit_vector-inl.h"
David Sehr1ce2b3b2018-04-05 11:02:03 -070024#include "base/memory_region.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010025#include "base/scoped_arena_containers.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070026#include "base/value_object.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010027#include "dex_register_location.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000028#include "nodes.h"
David Srbecky50fac062018-06-13 18:55:35 +010029#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010030
31namespace art {
32
33/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010034 * Collects and builds stack maps for a method. All the stack maps
35 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010036 */
Vladimir Markoced04832018-07-26 14:42:17 +010037class StackMapStream : public DeletableArenaObject<kArenaAllocStackMapStream> {
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010038 public:
Vladimir Marko174b2e22017-10-12 13:34:49 +010039 explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set)
David Srbeckye7a91942018-08-01 17:23:53 +010040 : allocator_(allocator),
41 instruction_set_(instruction_set),
David Srbecky71ec1cc2018-05-18 15:57:25 +010042 stack_maps_(allocator),
43 register_masks_(allocator),
44 stack_masks_(allocator),
David Srbecky697c47a2019-06-16 21:53:07 +010045 inline_infos_(allocator),
46 method_infos_(allocator),
David Srbecky71ec1cc2018-05-18 15:57:25 +010047 dex_register_masks_(allocator),
48 dex_register_maps_(allocator),
49 dex_register_catalog_(allocator),
David Srbecky71ec1cc2018-05-18 15:57:25 +010050 lazy_stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbeckyf325e282018-06-13 15:02:32 +010051 current_stack_map_(),
David Srbecky6eb4d5e2018-06-03 12:00:20 +010052 current_inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky71ec1cc2018-05-18 15:57:25 +010053 current_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky6de88332018-06-03 12:00:11 +010054 previous_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
55 dex_register_timestamp_(allocator->Adapter(kArenaAllocStackMapStream)),
Vladimir Markoced04832018-07-26 14:42:17 +010056 expected_num_dex_registers_(0u),
David Srbecky71ec1cc2018-05-18 15:57:25 +010057 temp_dex_register_mask_(allocator, 32, true, kArenaAllocStackMapStream),
58 temp_dex_register_map_(allocator->Adapter(kArenaAllocStackMapStream)) {
Vladimir Marko225b6462015-09-28 12:17:40 +010059 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010060
David Srbeckyf6ba5b32018-06-23 22:05:49 +010061 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);
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000065 void EndMethod();
David Srbeckyf6ba5b32018-06-23 22:05:49 +010066
Calin Juravle4f46ac52015-04-23 18:47:21 +010067 void BeginStackMapEntry(uint32_t dex_pc,
68 uint32_t native_pc_offset,
David Srbeckyf6ba5b32018-06-23 22:05:49 +010069 uint32_t register_mask = 0,
70 BitVector* sp_mask = nullptr,
David Srbecky50fac062018-06-13 18:55:35 +010071 StackMap::Kind kind = StackMap::Kind::Default);
Calin Juravle4f46ac52015-04-23 18:47:21 +010072 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010073
David Srbeckye1402122018-06-13 18:20:45 +010074 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
75 current_dex_registers_.push_back(DexRegisterLocation(kind, value));
76 }
Nicolas Geoffray004c2302015-03-20 10:06:38 +000077
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000078 void BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010079 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000080 uint32_t num_dex_registers,
81 const DexFile* outer_dex_file = nullptr);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010082 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010083
Vladimir Markobd8c7252015-06-12 10:06:32 +010084 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +010085 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +010086 }
87
David Srbeckyd02b23f2018-05-29 23:27:22 +010088 uint32_t GetStackMapNativePcOffset(size_t i);
89 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000090
David Srbeckye7a91942018-08-01 17:23:53 +010091 // Encode all stack map data.
92 // The returned vector is allocated using the allocator passed to the StackMapStream.
93 ScopedArenaVector<uint8_t> Encode();
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000094
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000095 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +010096 static constexpr uint32_t kNoValue = -1;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080097
David Srbecky71ec1cc2018-05-18 15:57:25 +010098 void CreateDexRegisterMap();
Calin Juravle6ae70962015-03-18 16:31:28 +000099
David Srbecky697c47a2019-06-16 21:53:07 +0100100 // Invokes the callback with pointer of each BitTableBuilder field.
101 template<typename Callback>
102 void ForEachBitTable(Callback callback) {
103 size_t index = 0;
104 callback(index++, &stack_maps_);
105 callback(index++, &register_masks_);
106 callback(index++, &stack_masks_);
107 callback(index++, &inline_infos_);
108 callback(index++, &method_infos_);
109 callback(index++, &dex_register_masks_);
110 callback(index++, &dex_register_maps_);
111 callback(index++, &dex_register_catalog_);
112 CHECK_EQ(index, CodeInfo::kNumBitTables);
113 }
114
David Srbeckye7a91942018-08-01 17:23:53 +0100115 ScopedArenaAllocator* allocator_;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800116 const InstructionSet instruction_set_;
David Srbecky3aaaa212018-07-30 16:46:53 +0100117 uint32_t packed_frame_size_ = 0;
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100118 uint32_t core_spill_mask_ = 0;
119 uint32_t fp_spill_mask_ = 0;
120 uint32_t num_dex_registers_ = 0;
David Srbeckycf7833e2018-06-14 16:45:22 +0100121 BitTableBuilder<StackMap> stack_maps_;
122 BitTableBuilder<RegisterMask> register_masks_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100123 BitmapTableBuilder stack_masks_;
David Srbecky697c47a2019-06-16 21:53:07 +0100124 BitTableBuilder<InlineInfo> inline_infos_;
125 BitTableBuilder<MethodInfo> method_infos_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100126 BitmapTableBuilder dex_register_masks_;
David Srbecky42deda82018-08-10 11:23:27 +0100127 BitTableBuilder<DexRegisterMapInfo> dex_register_maps_;
David Srbeckycf7833e2018-06-14 16:45:22 +0100128 BitTableBuilder<DexRegisterInfo> dex_register_catalog_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100129
David Srbecky71ec1cc2018-05-18 15:57:25 +0100130 ScopedArenaVector<BitVector*> lazy_stack_masks_;
131
132 // Variables which track the current state between Begin/End calls;
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100133 bool in_method_ = false;
134 bool in_stack_map_ = false;
135 bool in_inline_info_ = false;
David Srbeckycf7833e2018-06-14 16:45:22 +0100136 BitTableBuilder<StackMap>::Entry current_stack_map_;
137 ScopedArenaVector<BitTableBuilder<InlineInfo>::Entry> current_inline_infos_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100138 ScopedArenaVector<DexRegisterLocation> current_dex_registers_;
David Srbecky6de88332018-06-03 12:00:11 +0100139 ScopedArenaVector<DexRegisterLocation> previous_dex_registers_;
140 ScopedArenaVector<uint32_t> dex_register_timestamp_; // Stack map index of last change.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100141 size_t expected_num_dex_registers_;
142
143 // Temporary variables used in CreateDexRegisterMap.
144 // They are here so that we can reuse the reserved memory.
145 ArenaBitVector temp_dex_register_mask_;
David Srbeckycf7833e2018-06-14 16:45:22 +0100146 ScopedArenaVector<BitTableBuilder<DexRegisterMapInfo>::Entry> temp_dex_register_map_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100147
David Srbecky049d6812018-05-18 14:46:49 +0100148 // A set of lambda functions to be executed at the end to verify
149 // the encoded data. It is generally only used in debug builds.
150 std::vector<std::function<void(CodeInfo&)>> dchecks_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100151
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100152 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
153};
154
155} // namespace art
156
157#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_