blob: dd6d1a29590a68af87d06a8e204ad91d0e9aa5ce [file] [log] [blame]
Calin Juravlec416d332015-04-23 16:01:43 +01001/*
2 * Copyright (C) 2015 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 */
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000016
Calin Juravlec416d332015-04-23 16:01:43 +010017#include "stack_map_stream.h"
18
David Srbecky049d6812018-05-18 14:46:49 +010019#include <memory>
20
Andreas Gampe90b936d2017-01-31 08:58:55 -080021#include "art_method-inl.h"
David Srbecky45aa5982016-03-18 02:15:09 +000022#include "base/stl_util.h"
David Sehr9e734c72018-01-04 17:56:19 -080023#include "dex/dex_file_types.h"
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +000024#include "optimizing/optimizing_compiler.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000025#include "runtime.h"
26#include "scoped_thread_state_change-inl.h"
David Srbecky71ec1cc2018-05-18 15:57:25 +010027#include "stack_map.h"
Nicolas Geoffray5d37c152017-01-12 13:25:19 +000028
Vladimir Marko0a516052019-10-14 13:00:44 +000029namespace art {
Calin Juravlec416d332015-04-23 16:01:43 +010030
David Srbecky049d6812018-05-18 14:46:49 +010031constexpr static bool kVerifyStackMaps = kIsDebugBuild;
32
David Srbeckyd02b23f2018-05-29 23:27:22 +010033uint32_t StackMapStream::GetStackMapNativePcOffset(size_t i) {
David Srbeckyf325e282018-06-13 15:02:32 +010034 return StackMap::UnpackNativePc(stack_maps_[i][StackMap::kPackedNativePc], instruction_set_);
David Srbeckyd02b23f2018-05-29 23:27:22 +010035}
36
37void StackMapStream::SetStackMapNativePcOffset(size_t i, uint32_t native_pc_offset) {
David Srbeckyf325e282018-06-13 15:02:32 +010038 stack_maps_[i][StackMap::kPackedNativePc] =
39 StackMap::PackNativePc(native_pc_offset, instruction_set_);
David Srbeckyd02b23f2018-05-29 23:27:22 +010040}
41
David Srbeckyf6ba5b32018-06-23 22:05:49 +010042void StackMapStream::BeginMethod(size_t frame_size_in_bytes,
43 size_t core_spill_mask,
44 size_t fp_spill_mask,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +000045 uint32_t num_dex_registers,
46 bool baseline) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010047 DCHECK(!in_method_) << "Mismatched Begin/End calls";
48 in_method_ = true;
David Srbecky3aaaa212018-07-30 16:46:53 +010049 DCHECK_EQ(packed_frame_size_, 0u) << "BeginMethod was already called";
David Srbeckyf6ba5b32018-06-23 22:05:49 +010050
David Srbecky3aaaa212018-07-30 16:46:53 +010051 DCHECK_ALIGNED(frame_size_in_bytes, kStackAlignment);
52 packed_frame_size_ = frame_size_in_bytes / kStackAlignment;
David Srbeckyf6ba5b32018-06-23 22:05:49 +010053 core_spill_mask_ = core_spill_mask;
54 fp_spill_mask_ = fp_spill_mask;
55 num_dex_registers_ = num_dex_registers;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +000056 baseline_ = baseline;
David Srbecky67ba8722019-05-23 15:32:18 +010057
58 if (kVerifyStackMaps) {
59 dchecks_.emplace_back([=](const CodeInfo& code_info) {
60 DCHECK_EQ(code_info.packed_frame_size_, frame_size_in_bytes / kStackAlignment);
61 DCHECK_EQ(code_info.core_spill_mask_, core_spill_mask);
62 DCHECK_EQ(code_info.fp_spill_mask_, fp_spill_mask);
63 DCHECK_EQ(code_info.number_of_dex_registers_, num_dex_registers);
64 });
65 }
David Srbeckyf6ba5b32018-06-23 22:05:49 +010066}
67
Nicolas Geoffraybf5f0f32019-03-05 15:41:50 +000068void StackMapStream::EndMethod() {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010069 DCHECK(in_method_) << "Mismatched Begin/End calls";
70 in_method_ = false;
David Srbeckye7a91942018-08-01 17:23:53 +010071
72 // Read the stack masks now. The compiler might have updated them.
73 for (size_t i = 0; i < lazy_stack_masks_.size(); i++) {
74 BitVector* stack_mask = lazy_stack_masks_[i];
75 if (stack_mask != nullptr && stack_mask->GetNumberOfBits() != 0) {
76 stack_maps_[i][StackMap::kStackMaskIndex] =
77 stack_masks_.Dedup(stack_mask->GetRawStorage(), stack_mask->GetNumberOfBits());
78 }
79 }
David Srbeckyf6ba5b32018-06-23 22:05:49 +010080}
81
Calin Juravle4f46ac52015-04-23 18:47:21 +010082void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
83 uint32_t native_pc_offset,
84 uint32_t register_mask,
David Srbecky71ec1cc2018-05-18 15:57:25 +010085 BitVector* stack_mask,
Artem Serov2808be82018-12-20 19:15:11 +000086 StackMap::Kind kind,
87 bool needs_vreg_info) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +010088 DCHECK(in_method_) << "Call BeginMethod first";
David Srbecky71ec1cc2018-05-18 15:57:25 +010089 DCHECK(!in_stack_map_) << "Mismatched Begin/End calls";
90 in_stack_map_ = true;
91
David Srbeckycf7833e2018-06-14 16:45:22 +010092 current_stack_map_ = BitTableBuilder<StackMap>::Entry();
David Srbeckyf325e282018-06-13 15:02:32 +010093 current_stack_map_[StackMap::kKind] = static_cast<uint32_t>(kind);
94 current_stack_map_[StackMap::kPackedNativePc] =
95 StackMap::PackNativePc(native_pc_offset, instruction_set_);
96 current_stack_map_[StackMap::kDexPc] = dex_pc;
David Srbecky0b4e5a32018-06-11 16:25:29 +010097 if (stack_maps_.size() > 0) {
98 // Check that non-catch stack maps are sorted by pc.
99 // Catch stack maps are at the end and may be unordered.
100 if (stack_maps_.back()[StackMap::kKind] == StackMap::Kind::Catch) {
101 DCHECK(current_stack_map_[StackMap::kKind] == StackMap::Kind::Catch);
102 } else if (current_stack_map_[StackMap::kKind] != StackMap::Kind::Catch) {
103 DCHECK_LE(stack_maps_.back()[StackMap::kPackedNativePc],
104 current_stack_map_[StackMap::kPackedNativePc]);
105 }
106 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100107 if (register_mask != 0) {
108 uint32_t shift = LeastSignificantBit(register_mask);
David Srbeckycf7833e2018-06-14 16:45:22 +0100109 BitTableBuilder<RegisterMask>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100110 entry[RegisterMask::kValue] = register_mask >> shift;
111 entry[RegisterMask::kShift] = shift;
112 current_stack_map_[StackMap::kRegisterMaskIndex] = register_masks_.Dedup(&entry);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100113 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100114 // The compiler assumes the bit vector will be read during PrepareForFillIn(),
115 // and it might modify the data before that. Therefore, just store the pointer.
116 // See ClearSpillSlotsFromLoopPhisInStackMap in code_generator.h.
117 lazy_stack_masks_.push_back(stack_mask);
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100118 current_inline_infos_.clear();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100119 current_dex_registers_.clear();
Artem Serov2808be82018-12-20 19:15:11 +0000120 expected_num_dex_registers_ = needs_vreg_info ? num_dex_registers_ : 0u;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100121
David Srbecky049d6812018-05-18 14:46:49 +0100122 if (kVerifyStackMaps) {
123 size_t stack_map_index = stack_maps_.size();
124 // Create lambda method, which will be executed at the very end to verify data.
125 // Parameters and local variables will be captured(stored) by the lambda "[=]".
126 dchecks_.emplace_back([=](const CodeInfo& code_info) {
David Srbecky50fac062018-06-13 18:55:35 +0100127 if (kind == StackMap::Kind::Default || kind == StackMap::Kind::OSR) {
128 StackMap stack_map = code_info.GetStackMapForNativePcOffset(native_pc_offset,
129 instruction_set_);
130 CHECK_EQ(stack_map.Row(), stack_map_index);
131 } else if (kind == StackMap::Kind::Catch) {
132 StackMap stack_map = code_info.GetCatchStackMapForDexPc(dex_pc);
133 CHECK_EQ(stack_map.Row(), stack_map_index);
134 }
David Srbecky049d6812018-05-18 14:46:49 +0100135 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
136 CHECK_EQ(stack_map.GetNativePcOffset(instruction_set_), native_pc_offset);
David Srbecky50fac062018-06-13 18:55:35 +0100137 CHECK_EQ(stack_map.GetKind(), static_cast<uint32_t>(kind));
David Srbecky049d6812018-05-18 14:46:49 +0100138 CHECK_EQ(stack_map.GetDexPc(), dex_pc);
139 CHECK_EQ(code_info.GetRegisterMaskOf(stack_map), register_mask);
140 BitMemoryRegion seen_stack_mask = code_info.GetStackMaskOf(stack_map);
141 CHECK_GE(seen_stack_mask.size_in_bits(), stack_mask ? stack_mask->GetNumberOfBits() : 0);
142 for (size_t b = 0; b < seen_stack_mask.size_in_bits(); b++) {
143 CHECK_EQ(seen_stack_mask.LoadBit(b), stack_mask != nullptr && stack_mask->IsBitSet(b));
144 }
David Srbecky049d6812018-05-18 14:46:49 +0100145 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100146 }
Calin Juravlec416d332015-04-23 16:01:43 +0100147}
148
Calin Juravle4f46ac52015-04-23 18:47:21 +0100149void StackMapStream::EndStackMapEntry() {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100150 DCHECK(in_stack_map_) << "Mismatched Begin/End calls";
151 in_stack_map_ = false;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100152
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100153 // Generate index into the InlineInfo table.
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100154 size_t inlining_depth = current_inline_infos_.size();
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100155 if (!current_inline_infos_.empty()) {
David Srbeckyf325e282018-06-13 15:02:32 +0100156 current_inline_infos_.back()[InlineInfo::kIsLast] = InlineInfo::kLast;
157 current_stack_map_[StackMap::kInlineInfoIndex] =
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100158 inline_infos_.Dedup(current_inline_infos_.data(), current_inline_infos_.size());
David Srbecky71ec1cc2018-05-18 15:57:25 +0100159 }
160
David Srbecky6de88332018-06-03 12:00:11 +0100161 // Generate delta-compressed dex register map.
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100162 size_t num_dex_registers = current_dex_registers_.size();
163 if (!current_dex_registers_.empty()) {
164 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
165 CreateDexRegisterMap();
166 }
David Srbecky6de88332018-06-03 12:00:11 +0100167
David Srbecky71ec1cc2018-05-18 15:57:25 +0100168 stack_maps_.Add(current_stack_map_);
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100169
170 if (kVerifyStackMaps) {
171 size_t stack_map_index = stack_maps_.size() - 1;
172 dchecks_.emplace_back([=](const CodeInfo& code_info) {
173 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
174 CHECK_EQ(stack_map.HasDexRegisterMap(), (num_dex_registers != 0));
175 CHECK_EQ(stack_map.HasInlineInfo(), (inlining_depth != 0));
David Srbecky93bd3612018-07-02 19:30:18 +0100176 CHECK_EQ(code_info.GetInlineInfosOf(stack_map).size(), inlining_depth);
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100177 });
178 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100179}
180
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000181void StackMapStream::BeginInlineInfoEntry(ArtMethod* method,
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100182 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000183 uint32_t num_dex_registers,
184 const DexFile* outer_dex_file) {
David Srbeckyf6ba5b32018-06-23 22:05:49 +0100185 DCHECK(in_stack_map_) << "Call BeginStackMapEntry first";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100186 DCHECK(!in_inline_info_) << "Mismatched Begin/End calls";
187 in_inline_info_ = true;
188 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
189
David Srbecky6de88332018-06-03 12:00:11 +0100190 expected_num_dex_registers_ += num_dex_registers;
191
David Srbeckycf7833e2018-06-14 16:45:22 +0100192 BitTableBuilder<InlineInfo>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100193 entry[InlineInfo::kIsLast] = InlineInfo::kMore;
194 entry[InlineInfo::kDexPc] = dex_pc;
195 entry[InlineInfo::kNumberOfDexRegisters] = static_cast<uint32_t>(expected_num_dex_registers_);
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000196 if (EncodeArtMethodInInlineInfo(method)) {
David Srbeckyf325e282018-06-13 15:02:32 +0100197 entry[InlineInfo::kArtMethodHi] = High32Bits(reinterpret_cast<uintptr_t>(method));
198 entry[InlineInfo::kArtMethodLo] = Low32Bits(reinterpret_cast<uintptr_t>(method));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000199 } else {
200 if (dex_pc != static_cast<uint32_t>(-1) && kIsDebugBuild) {
201 ScopedObjectAccess soa(Thread::Current());
202 DCHECK(IsSameDexFile(*outer_dex_file, *method->GetDexFile()));
203 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100204 uint32_t dex_method_index = method->GetDexMethodIndex();
David Srbeckyf325e282018-06-13 15:02:32 +0100205 entry[InlineInfo::kMethodInfoIndex] = method_infos_.Dedup({dex_method_index});
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000206 }
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100207 current_inline_infos_.push_back(entry);
David Srbecky71ec1cc2018-05-18 15:57:25 +0100208
David Srbecky049d6812018-05-18 14:46:49 +0100209 if (kVerifyStackMaps) {
210 size_t stack_map_index = stack_maps_.size();
David Srbecky6eb4d5e2018-06-03 12:00:20 +0100211 size_t depth = current_inline_infos_.size() - 1;
David Srbecky049d6812018-05-18 14:46:49 +0100212 dchecks_.emplace_back([=](const CodeInfo& code_info) {
213 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
David Srbecky93bd3612018-07-02 19:30:18 +0100214 InlineInfo inline_info = code_info.GetInlineInfosOf(stack_map)[depth];
David Srbecky6e69e522018-06-03 12:00:14 +0100215 CHECK_EQ(inline_info.GetDexPc(), dex_pc);
David Srbecky049d6812018-05-18 14:46:49 +0100216 bool encode_art_method = EncodeArtMethodInInlineInfo(method);
David Srbecky6e69e522018-06-03 12:00:14 +0100217 CHECK_EQ(inline_info.EncodesArtMethod(), encode_art_method);
David Srbecky049d6812018-05-18 14:46:49 +0100218 if (encode_art_method) {
David Srbecky6e69e522018-06-03 12:00:14 +0100219 CHECK_EQ(inline_info.GetArtMethod(), method);
David Srbecky049d6812018-05-18 14:46:49 +0100220 } else {
David Srbecky8cd54542018-07-15 23:58:44 +0100221 CHECK_EQ(code_info.GetMethodIndexOf(inline_info), method->GetDexMethodIndex());
David Srbecky049d6812018-05-18 14:46:49 +0100222 }
David Srbecky049d6812018-05-18 14:46:49 +0100223 });
David Srbecky71ec1cc2018-05-18 15:57:25 +0100224 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100225}
226
227void StackMapStream::EndInlineInfoEntry() {
David Srbecky71ec1cc2018-05-18 15:57:25 +0100228 DCHECK(in_inline_info_) << "Mismatched Begin/End calls";
229 in_inline_info_ = false;
230 DCHECK_EQ(expected_num_dex_registers_, current_dex_registers_.size());
Calin Juravlec416d332015-04-23 16:01:43 +0100231}
232
David Srbecky6de88332018-06-03 12:00:11 +0100233// Create delta-compressed dex register map based on the current list of DexRegisterLocations.
234// All dex registers for a stack map are concatenated - inlined registers are just appended.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100235void StackMapStream::CreateDexRegisterMap() {
David Srbecky6de88332018-06-03 12:00:11 +0100236 // These are fields rather than local variables so that we can reuse the reserved memory.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100237 temp_dex_register_mask_.ClearAllBits();
238 temp_dex_register_map_.clear();
David Srbecky6de88332018-06-03 12:00:11 +0100239
240 // Ensure that the arrays that hold previous state are big enough to be safely indexed below.
241 if (previous_dex_registers_.size() < current_dex_registers_.size()) {
242 previous_dex_registers_.resize(current_dex_registers_.size(), DexRegisterLocation::None());
243 dex_register_timestamp_.resize(current_dex_registers_.size(), 0u);
244 }
245
246 // Set bit in the mask for each register that has been changed since the previous stack map.
247 // Modified registers are stored in the catalogue and the catalogue index added to the list.
David Srbecky71ec1cc2018-05-18 15:57:25 +0100248 for (size_t i = 0; i < current_dex_registers_.size(); i++) {
249 DexRegisterLocation reg = current_dex_registers_[i];
David Srbecky6de88332018-06-03 12:00:11 +0100250 // Distance is difference between this index and the index of last modification.
251 uint32_t distance = stack_maps_.size() - dex_register_timestamp_[i];
252 if (previous_dex_registers_[i] != reg || distance > kMaxDexRegisterMapSearchDistance) {
David Srbeckycf7833e2018-06-14 16:45:22 +0100253 BitTableBuilder<DexRegisterInfo>::Entry entry;
David Srbeckyf325e282018-06-13 15:02:32 +0100254 entry[DexRegisterInfo::kKind] = static_cast<uint32_t>(reg.GetKind());
255 entry[DexRegisterInfo::kPackedValue] =
256 DexRegisterInfo::PackValue(reg.GetKind(), reg.GetValue());
David Srbecky6de88332018-06-03 12:00:11 +0100257 uint32_t index = reg.IsLive() ? dex_register_catalog_.Dedup(&entry) : kNoValue;
David Srbecky71ec1cc2018-05-18 15:57:25 +0100258 temp_dex_register_mask_.SetBit(i);
David Srbeckyf325e282018-06-13 15:02:32 +0100259 temp_dex_register_map_.push_back({index});
David Srbecky6de88332018-06-03 12:00:11 +0100260 previous_dex_registers_[i] = reg;
261 dex_register_timestamp_[i] = stack_maps_.size();
David Srbecky71ec1cc2018-05-18 15:57:25 +0100262 }
Calin Juravlec416d332015-04-23 16:01:43 +0100263 }
Calin Juravlec416d332015-04-23 16:01:43 +0100264
David Srbecky6de88332018-06-03 12:00:11 +0100265 // Set the mask and map for the current StackMap (which includes inlined registers).
David Srbecky71ec1cc2018-05-18 15:57:25 +0100266 if (temp_dex_register_mask_.GetNumberOfBits() != 0) {
David Srbeckyf325e282018-06-13 15:02:32 +0100267 current_stack_map_[StackMap::kDexRegisterMaskIndex] =
David Srbecky6de88332018-06-03 12:00:11 +0100268 dex_register_masks_.Dedup(temp_dex_register_mask_.GetRawStorage(),
269 temp_dex_register_mask_.GetNumberOfBits());
Vladimir Marko225b6462015-09-28 12:17:40 +0100270 }
David Srbecky6de88332018-06-03 12:00:11 +0100271 if (!current_dex_registers_.empty()) {
David Srbeckyf325e282018-06-13 15:02:32 +0100272 current_stack_map_[StackMap::kDexRegisterMapIndex] =
David Srbecky6de88332018-06-03 12:00:11 +0100273 dex_register_maps_.Dedup(temp_dex_register_map_.data(),
274 temp_dex_register_map_.size());
David Srbecky052f8ca2018-04-26 15:42:54 +0100275 }
David Srbecky049d6812018-05-18 14:46:49 +0100276
277 if (kVerifyStackMaps) {
278 size_t stack_map_index = stack_maps_.size();
David Srbecky049d6812018-05-18 14:46:49 +0100279 // We need to make copy of the current registers for later (when the check is run).
David Srbecky6de88332018-06-03 12:00:11 +0100280 auto expected_dex_registers = std::make_shared<dchecked_vector<DexRegisterLocation>>(
David Srbecky049d6812018-05-18 14:46:49 +0100281 current_dex_registers_.begin(), current_dex_registers_.end());
282 dchecks_.emplace_back([=](const CodeInfo& code_info) {
283 StackMap stack_map = code_info.GetStackMapAt(stack_map_index);
David Srbecky6de88332018-06-03 12:00:11 +0100284 uint32_t expected_reg = 0;
285 for (DexRegisterLocation reg : code_info.GetDexRegisterMapOf(stack_map)) {
286 CHECK_EQ((*expected_dex_registers)[expected_reg++], reg);
David Srbecky049d6812018-05-18 14:46:49 +0100287 }
David Srbecky93bd3612018-07-02 19:30:18 +0100288 for (InlineInfo inline_info : code_info.GetInlineInfosOf(stack_map)) {
289 DexRegisterMap map = code_info.GetInlineDexRegisterMapOf(stack_map, inline_info);
290 for (DexRegisterLocation reg : map) {
David Srbecky6de88332018-06-03 12:00:11 +0100291 CHECK_EQ((*expected_dex_registers)[expected_reg++], reg);
292 }
293 }
294 CHECK_EQ(expected_reg, expected_dex_registers->size());
David Srbecky049d6812018-05-18 14:46:49 +0100295 });
296 }
Calin Juravlec416d332015-04-23 16:01:43 +0100297}
298
David Srbeckye7a91942018-08-01 17:23:53 +0100299ScopedArenaVector<uint8_t> StackMapStream::Encode() {
300 DCHECK(in_stack_map_ == false) << "Mismatched Begin/End calls";
301 DCHECK(in_inline_info_ == false) << "Mismatched Begin/End calls";
David Srbecky71ec1cc2018-05-18 15:57:25 +0100302
David Srbecky697c47a2019-06-16 21:53:07 +0100303 uint32_t flags = (inline_infos_.size() > 0) ? CodeInfo::kHasInlineInfo : 0;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000304 flags |= baseline_ ? CodeInfo::kIsBaseline : 0;
David Srbecky697c47a2019-06-16 21:53:07 +0100305 uint32_t bit_table_flags = 0;
306 ForEachBitTable([&bit_table_flags](size_t i, auto bit_table) {
307 if (bit_table->size() != 0) { // Record which bit-tables are stored.
308 bit_table_flags |= 1 << i;
309 }
310 });
311
David Srbeckye7a91942018-08-01 17:23:53 +0100312 ScopedArenaVector<uint8_t> buffer(allocator_->Adapter(kArenaAllocStackMapStream));
313 BitMemoryWriter<ScopedArenaVector<uint8_t>> out(&buffer);
David Srbecky6c4ec5c2019-06-20 07:23:19 +0000314 out.WriteInterleavedVarints(std::array<uint32_t, CodeInfo::kNumHeaders>{
315 flags,
316 packed_frame_size_,
317 core_spill_mask_,
318 fp_spill_mask_,
319 num_dex_registers_,
320 bit_table_flags,
321 });
David Srbecky697c47a2019-06-16 21:53:07 +0100322 ForEachBitTable([&out](size_t, auto bit_table) {
323 if (bit_table->size() != 0) { // Skip empty bit-tables.
324 bit_table->Encode(out);
325 }
326 });
David Srbecky45aa5982016-03-18 02:15:09 +0000327
David Srbeckya38e6cf2018-06-26 18:13:49 +0100328 // Verify that we can load the CodeInfo and check some essentials.
David Srbecky0d4567f2019-05-30 22:45:40 +0100329 size_t number_of_read_bits;
330 CodeInfo code_info(buffer.data(), &number_of_read_bits);
331 CHECK_EQ(number_of_read_bits, out.NumberOfWrittenBits());
David Srbeckya38e6cf2018-06-26 18:13:49 +0100332 CHECK_EQ(code_info.GetNumberOfStackMaps(), stack_maps_.size());
Mathieu Chartier1a20b682017-01-31 14:25:16 -0800333
David Srbecky049d6812018-05-18 14:46:49 +0100334 // Verify all written data (usually only in debug builds).
335 if (kVerifyStackMaps) {
David Srbecky049d6812018-05-18 14:46:49 +0100336 for (const auto& dcheck : dchecks_) {
337 dcheck(code_info);
David Srbecky1bbdfd72016-02-24 16:39:26 +0000338 }
David Srbecky71ec1cc2018-05-18 15:57:25 +0100339 }
David Srbeckye7a91942018-08-01 17:23:53 +0100340
341 return buffer;
David Srbecky1bbdfd72016-02-24 16:39:26 +0000342}
343
Calin Juravlec416d332015-04-23 16:01:43 +0100344} // namespace art