blob: 06868476bc4f1a06b8dc0c1e97a05a2a9fc2ece4 [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"
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070028#include "method_info.h"
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000029#include "nodes.h"
David Srbecky50fac062018-06-13 18:55:35 +010030#include "stack_map.h"
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010031
32namespace art {
33
34/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010035 * Collects and builds stack maps for a method. All the stack maps
36 * for a method are placed in a CodeInfo object.
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010037 */
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010038class StackMapStream : public ValueObject {
39 public:
Vladimir Marko174b2e22017-10-12 13:34:49 +010040 explicit StackMapStream(ScopedArenaAllocator* allocator, InstructionSet instruction_set)
David Srbecky71ec1cc2018-05-18 15:57:25 +010041 : instruction_set_(instruction_set),
42 stack_maps_(allocator),
43 register_masks_(allocator),
44 stack_masks_(allocator),
45 invoke_infos_(allocator),
46 inline_infos_(allocator),
47 dex_register_masks_(allocator),
48 dex_register_maps_(allocator),
49 dex_register_catalog_(allocator),
David Srbecky052f8ca2018-04-26 15:42:54 +010050 out_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky71ec1cc2018-05-18 15:57:25 +010051 method_infos_(allocator),
52 lazy_stack_masks_(allocator->Adapter(kArenaAllocStackMapStream)),
53 in_stack_map_(false),
54 in_inline_info_(false),
David Srbecky6eb4d5e2018-06-03 12:00:20 +010055 current_inline_infos_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky71ec1cc2018-05-18 15:57:25 +010056 current_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky6de88332018-06-03 12:00:11 +010057 previous_dex_registers_(allocator->Adapter(kArenaAllocStackMapStream)),
58 dex_register_timestamp_(allocator->Adapter(kArenaAllocStackMapStream)),
David Srbecky71ec1cc2018-05-18 15:57:25 +010059 temp_dex_register_mask_(allocator, 32, true, kArenaAllocStackMapStream),
60 temp_dex_register_map_(allocator->Adapter(kArenaAllocStackMapStream)) {
Vladimir Marko225b6462015-09-28 12:17:40 +010061 }
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010062
Calin Juravle4f46ac52015-04-23 18:47:21 +010063 void BeginStackMapEntry(uint32_t dex_pc,
64 uint32_t native_pc_offset,
65 uint32_t register_mask,
66 BitVector* sp_mask,
67 uint32_t num_dex_registers,
David Srbecky50fac062018-06-13 18:55:35 +010068 uint8_t inlining_depth,
69 StackMap::Kind kind = StackMap::Kind::Default);
Calin Juravle4f46ac52015-04-23 18:47:21 +010070 void EndStackMapEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010071
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010072 void AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value);
Nicolas Geoffray004c2302015-03-20 10:06:38 +000073
Mathieu Chartierd776ff02017-01-17 09:32:18 -080074 void AddInvoke(InvokeType type, uint32_t dex_method_index);
75
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000076 void BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010077 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000078 uint32_t num_dex_registers,
79 const DexFile* outer_dex_file = nullptr);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +010080 void EndInlineInfoEntry();
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +010081
Vladimir Markobd8c7252015-06-12 10:06:32 +010082 size_t GetNumberOfStackMaps() const {
Vladimir Marko225b6462015-09-28 12:17:40 +010083 return stack_maps_.size();
Vladimir Markobd8c7252015-06-12 10:06:32 +010084 }
85
David Srbeckyd02b23f2018-05-29 23:27:22 +010086 uint32_t GetStackMapNativePcOffset(size_t i);
87 void SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset);
Vladimir Markocf93a5c2015-06-16 11:33:24 +000088
Calin Juravle4f46ac52015-04-23 18:47:21 +010089 // Prepares the stream to fill in a memory region. Must be called before FillIn.
90 // Returns the size (in bytes) needed to store this stream.
91 size_t PrepareForFillIn();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -070092 void FillInCodeInfo(MemoryRegion region);
93 void FillInMethodInfo(MemoryRegion region);
94
95 size_t ComputeMethodInfoSize() const;
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +000096
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +000097 private:
David Srbecky71ec1cc2018-05-18 15:57:25 +010098 static constexpr uint32_t kNoValue = -1;
Mathieu Chartiera2f526f2017-01-19 14:48:48 -080099
David Srbecky71ec1cc2018-05-18 15:57:25 +0100100 // The fields must be uint32_t and mirror the StackMap accessor in stack_map.h!
101 struct StackMapEntry {
David Srbecky50fac062018-06-13 18:55:35 +0100102 uint32_t kind;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100103 uint32_t packed_native_pc;
104 uint32_t dex_pc;
105 uint32_t register_mask_index;
106 uint32_t stack_mask_index;
107 uint32_t inline_info_index;
108 uint32_t dex_register_mask_index;
109 uint32_t dex_register_map_index;
110 };
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700111
David Srbecky71ec1cc2018-05-18 15:57:25 +0100112 // The fields must be uint32_t and mirror the InlineInfo accessor in stack_map.h!
113 struct InlineInfoEntry {
114 uint32_t is_last;
115 uint32_t dex_pc;
116 uint32_t method_info_index;
117 uint32_t art_method_hi;
118 uint32_t art_method_lo;
David Srbecky6de88332018-06-03 12:00:11 +0100119 uint32_t num_dex_registers;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100120 };
Mathieu Chartier32289082017-02-09 15:57:37 -0800121
David Srbecky71ec1cc2018-05-18 15:57:25 +0100122 // The fields must be uint32_t and mirror the InvokeInfo accessor in stack_map.h!
123 struct InvokeInfoEntry {
124 uint32_t packed_native_pc;
125 uint32_t invoke_type;
126 uint32_t method_info_index;
127 };
Mathieu Chartier32289082017-02-09 15:57:37 -0800128
David Srbecky71ec1cc2018-05-18 15:57:25 +0100129 // The fields must be uint32_t and mirror the DexRegisterInfo accessor in stack_map.h!
130 struct DexRegisterEntry {
131 uint32_t kind;
132 uint32_t packed_value;
133 };
134
135 // The fields must be uint32_t and mirror the RegisterMask accessor in stack_map.h!
136 struct RegisterMaskEntry {
137 uint32_t value;
138 uint32_t shift;
139 };
140
141 void CreateDexRegisterMap();
Calin Juravle6ae70962015-03-18 16:31:28 +0000142
Mathieu Chartiera2f526f2017-01-19 14:48:48 -0800143 const InstructionSet instruction_set_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100144 BitTableBuilder<StackMapEntry> stack_maps_;
145 BitTableBuilder<RegisterMaskEntry> register_masks_;
146 BitmapTableBuilder stack_masks_;
147 BitTableBuilder<InvokeInfoEntry> invoke_infos_;
148 BitTableBuilder<InlineInfoEntry> inline_infos_;
149 BitmapTableBuilder dex_register_masks_;
150 BitTableBuilder<uint32_t> dex_register_maps_;
151 BitTableBuilder<DexRegisterEntry> dex_register_catalog_;
David Srbecky6de88332018-06-03 12:00:11 +0100152 uint32_t num_dex_registers_ = 0; // TODO: Make this const and get the value in constructor.
David Srbecky052f8ca2018-04-26 15:42:54 +0100153 ScopedArenaVector<uint8_t> out_;
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100154
David Srbecky71ec1cc2018-05-18 15:57:25 +0100155 BitTableBuilder<uint32_t> method_infos_;
Calin Juravle6ae70962015-03-18 16:31:28 +0000156
David Srbecky71ec1cc2018-05-18 15:57:25 +0100157 ScopedArenaVector<BitVector*> lazy_stack_masks_;
158
159 // Variables which track the current state between Begin/End calls;
160 bool in_stack_map_;
161 bool in_inline_info_;
162 StackMapEntry current_stack_map_;
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100163 ScopedArenaVector<InlineInfoEntry> current_inline_infos_;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100164 ScopedArenaVector<DexRegisterLocation> current_dex_registers_;
David Srbecky6de88332018-06-03 12:00:11 +0100165 ScopedArenaVector<DexRegisterLocation> previous_dex_registers_;
166 ScopedArenaVector<uint32_t> dex_register_timestamp_; // Stack map index of last change.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100167 size_t expected_num_dex_registers_;
168
169 // Temporary variables used in CreateDexRegisterMap.
170 // They are here so that we can reuse the reserved memory.
171 ArenaBitVector temp_dex_register_mask_;
172 ScopedArenaVector<uint32_t> temp_dex_register_map_;
173
David Srbecky049d6812018-05-18 14:46:49 +0100174 // A set of lambda functions to be executed at the end to verify
175 // the encoded data. It is generally only used in debug builds.
176 std::vector<std::function<void(CodeInfo&)>> dchecks_;
Calin Juravle4f46ac52015-04-23 18:47:21 +0100177
Nicolas Geoffray99ea58c2014-07-02 15:08:17 +0100178 DISALLOW_COPY_AND_ASSIGN(StackMapStream);
179};
180
181} // namespace art
182
183#endif // ART_COMPILER_OPTIMIZING_STACK_MAP_STREAM_H_