blob: 9d172638e1e6f3b81298ed80b13a746e8602581b [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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#include "code_generator.h"
18
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010039size_t CodeGenerator::GetCacheOffset(uint32_t index) {
40 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
41}
42
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010043void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
45 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
46 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010047 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010048
49 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010050 if (!is_leaf) {
51 MarkNotLeaf();
52 }
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs()
54 + GetGraph()->GetNumberOfTemporaries()
55 + 1 /* filler */,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010056 0, /* the baseline compiler does not have live registers at slow path */
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 GetGraph()->GetMaximumNumberOfOutVRegs()
58 + 1 /* current method */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010059 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010060
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010061 HGraphVisitor* location_builder = GetLocationBuilder();
62 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010063 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010064 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010065 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010066 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
67 HInstruction* current = it.Current();
68 current->Accept(location_builder);
69 InitLocations(current);
70 current->Accept(instruction_visitor);
71 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010074
Nicolas Geoffray787c3072014-03-17 10:20:19 +000075 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076 uint8_t* buffer = allocator->Allocate(code_size);
77 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079}
80
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010081void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
82 // The frame size has already been computed during register allocation.
83 DCHECK_NE(frame_size_, kUninitializedFrameSize);
84 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
85 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
86 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010087 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088
89 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010090 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010091 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
92 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010093 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010094 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
95 HInstruction* current = it.Current();
96 current->Accept(instruction_visitor);
97 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010099 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100
101 size_t code_size = GetAssembler()->CodeSize();
102 uint8_t* buffer = allocator->Allocate(code_size);
103 MemoryRegion code(buffer, code_size);
104 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105}
106
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100107void CodeGenerator::GenerateSlowPaths() {
108 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
109 slow_paths_.Get(i)->EmitNativeCode(this);
110 }
111}
112
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100113size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
114 for (size_t i = 0; i < length; ++i) {
115 if (!array[i]) {
116 array[i] = true;
117 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100118 }
119 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100120 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000121 UNREACHABLE();
122 return -1;
123}
124
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000125size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
126 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000127 if (!array[i] && !array[i + 1]) {
128 array[i] = true;
129 array[i + 1] = true;
130 return i;
131 }
132 }
133 LOG(FATAL) << "Could not find a register in baseline register allocator";
134 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100135 return -1;
136}
137
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100138void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
139 size_t maximum_number_of_live_registers,
140 size_t number_of_out_slots) {
141 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
142
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100143 SetFrameSize(RoundUp(
144 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100145 + number_of_out_slots * kVRegSize
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100146 + maximum_number_of_live_registers * GetWordSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100147 + FrameEntrySpillSize(),
148 kStackAlignment));
149}
150
151Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
152 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
153 // Use the temporary region (right below the dex registers).
154 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
155 - kVRegSize // filler
156 - (number_of_locals * kVRegSize)
157 - ((1 + temp->GetIndex()) * kVRegSize);
158 return Location::StackSlot(slot);
159}
160
161int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
162 uint16_t reg_number = local->GetRegNumber();
163 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
164 if (reg_number >= number_of_locals) {
165 // Local is a parameter of the method. It is stored in the caller's frame.
166 return GetFrameSize() + kVRegSize // ART method
167 + (reg_number - number_of_locals) * kVRegSize;
168 } else {
169 // Local is a temporary in this method. It is stored in this method's frame.
170 return GetFrameSize() - FrameEntrySpillSize()
171 - kVRegSize // filler.
172 - (number_of_locals * kVRegSize)
173 + (reg_number * kVRegSize);
174 }
175}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100176
177void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
178 LocationSummary* locations = instruction->GetLocations();
179 if (locations == nullptr) return;
180
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100181 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
182 blocked_core_registers_[i] = false;
183 }
184
185 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
186 blocked_fpu_registers_[i] = false;
187 }
188
189 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
190 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100191 }
192
193 // Mark all fixed input, temp and output registers as used.
194 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
195 Location loc = locations->InAt(i);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100196 // The DCHECKS below check that a register is not specified twice in
197 // the summary.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100198 if (loc.IsRegister()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100199 DCHECK(!blocked_core_registers_[loc.reg()]);
200 blocked_core_registers_[loc.reg()] = true;
201 } else if (loc.IsFpuRegister()) {
202 DCHECK(!blocked_fpu_registers_[loc.reg()]);
203 blocked_fpu_registers_[loc.reg()] = true;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000204 } else if (loc.IsFpuRegisterPair()) {
205 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
206 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
207 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
208 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100209 } else if (loc.IsRegisterPair()) {
210 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
211 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
212 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
213 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100214 }
215 }
216
217 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
218 Location loc = locations->GetTemp(i);
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000219 // The DCHECKS below check that a register is not specified twice in
220 // the summary.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100221 if (loc.IsRegister()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100222 DCHECK(!blocked_core_registers_[loc.reg()]);
223 blocked_core_registers_[loc.reg()] = true;
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000224 } else if (loc.IsFpuRegister()) {
225 DCHECK(!blocked_fpu_registers_[loc.reg()]);
226 blocked_fpu_registers_[loc.reg()] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100227 } else {
228 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100229 }
230 }
231
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100232 SetupBlockedRegisters();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100233
234 // Allocate all unallocated input locations.
235 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
236 Location loc = locations->InAt(i);
237 HInstruction* input = instruction->InputAt(i);
238 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100239 if ((loc.GetPolicy() == Location::kRequiresRegister)
240 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100241 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100242 } else {
243 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
244 HLoadLocal* load = input->AsLoadLocal();
245 if (load != nullptr) {
246 loc = GetStackLocation(load);
247 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100248 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100249 }
250 }
251 locations->SetInAt(i, loc);
252 }
253 }
254
255 // Allocate all unallocated temp locations.
256 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
257 Location loc = locations->GetTemp(i);
258 if (loc.IsUnallocated()) {
259 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
260 // TODO: Adjust handling of temps. We currently consider temps to use
261 // core registers. They may also use floating point registers at some point.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100262 loc = AllocateFreeRegister(Primitive::kPrimInt);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100263 locations->SetTempAt(i, loc);
264 }
265 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100266 Location result_location = locations->Out();
267 if (result_location.IsUnallocated()) {
268 switch (result_location.GetPolicy()) {
269 case Location::kAny:
270 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100271 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100272 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100273 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100274 case Location::kSameAsFirstInput:
275 result_location = locations->InAt(0);
276 break;
277 }
278 locations->SetOut(result_location);
279 }
280}
281
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000282void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100283 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 if (instruction->IsTemporary()) {
285 HInstruction* previous = instruction->GetPrevious();
286 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
287 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100288 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289 return;
290 }
291 AllocateRegistersLocally(instruction);
292 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000293 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000294 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000295 if (location.IsValid()) {
296 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000297 if (input->GetNext()->IsTemporary()) {
298 // If the input was stored in a temporary, use that temporary to
299 // perform the move.
300 Move(input->GetNext(), location, instruction);
301 } else {
302 Move(input, location, instruction);
303 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000304 }
305 }
306}
307
308bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000309 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000310 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000311}
312
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
314 HGraph* graph,
315 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000316 switch (instruction_set) {
317 case kArm:
318 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000320 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100321 case kArm64: {
322 return new (allocator) arm64::CodeGeneratorARM64(graph);
323 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000324 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000326 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700329 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100330 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700331 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000333 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000334 }
335}
336
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000337void CodeGenerator::BuildNativeGCMap(
338 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
339 const std::vector<uint8_t>& gc_map_raw =
340 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
341 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
342
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000343 uint32_t max_native_offset = 0;
344 for (size_t i = 0; i < pc_infos_.Size(); i++) {
345 uint32_t native_offset = pc_infos_.Get(i).native_pc;
346 if (native_offset > max_native_offset) {
347 max_native_offset = native_offset;
348 }
349 }
350
351 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
352 for (size_t i = 0; i < pc_infos_.Size(); i++) {
353 struct PcInfo pc_info = pc_infos_.Get(i);
354 uint32_t native_offset = pc_info.native_pc;
355 uint32_t dex_pc = pc_info.dex_pc;
356 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
357 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
358 builder.AddEntry(native_offset, references);
359 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000360}
361
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700362void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000363 uint32_t pc2dex_data_size = 0u;
364 uint32_t pc2dex_entries = pc_infos_.Size();
365 uint32_t pc2dex_offset = 0u;
366 int32_t pc2dex_dalvik_offset = 0;
367 uint32_t dex2pc_data_size = 0u;
368 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000369 uint32_t dex2pc_offset = 0u;
370 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000371
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700372 if (src_map != nullptr) {
373 src_map->reserve(pc2dex_entries);
374 }
375
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000376 for (size_t i = 0; i < pc2dex_entries; i++) {
377 struct PcInfo pc_info = pc_infos_.Get(i);
378 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
379 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
380 pc2dex_offset = pc_info.native_pc;
381 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700382 if (src_map != nullptr) {
383 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
384 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000385 }
386
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000387 // Walk over the blocks and find which ones correspond to catch block entries.
388 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
389 HBasicBlock* block = graph_->GetBlocks().Get(i);
390 if (block->IsCatchBlock()) {
391 intptr_t native_pc = GetAddressOf(block);
392 ++dex2pc_entries;
393 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
394 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
395 dex2pc_offset = native_pc;
396 dex2pc_dalvik_offset = block->GetDexPc();
397 }
398 }
399
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000400 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
401 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
402 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
403 data->resize(data_size);
404
405 uint8_t* data_ptr = &(*data)[0];
406 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000407
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000408 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
409 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
410 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
411 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
412
413 pc2dex_offset = 0u;
414 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000415 dex2pc_offset = 0u;
416 dex2pc_dalvik_offset = 0u;
417
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000418 for (size_t i = 0; i < pc2dex_entries; i++) {
419 struct PcInfo pc_info = pc_infos_.Get(i);
420 DCHECK(pc2dex_offset <= pc_info.native_pc);
421 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
422 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
423 pc2dex_offset = pc_info.native_pc;
424 pc2dex_dalvik_offset = pc_info.dex_pc;
425 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000426
427 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
428 HBasicBlock* block = graph_->GetBlocks().Get(i);
429 if (block->IsCatchBlock()) {
430 intptr_t native_pc = GetAddressOf(block);
431 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
432 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
433 dex2pc_offset = native_pc;
434 dex2pc_dalvik_offset = block->GetDexPc();
435 }
436 }
437
438
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000439 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
440 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
441
442 if (kIsDebugBuild) {
443 // Verify the encoded table holds the expected data.
444 MappingTable table(data_ptr);
445 CHECK_EQ(table.TotalSize(), total_entries);
446 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
447 auto it = table.PcToDexBegin();
448 auto it2 = table.DexToPcBegin();
449 for (size_t i = 0; i < pc2dex_entries; i++) {
450 struct PcInfo pc_info = pc_infos_.Get(i);
451 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
452 CHECK_EQ(pc_info.dex_pc, it.DexPc());
453 ++it;
454 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000455 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
456 HBasicBlock* block = graph_->GetBlocks().Get(i);
457 if (block->IsCatchBlock()) {
458 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
459 CHECK_EQ(block->GetDexPc(), it2.DexPc());
460 ++it2;
461 }
462 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000463 CHECK(it == table.PcToDexEnd());
464 CHECK(it2 == table.DexToPcEnd());
465 }
466}
467
468void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
469 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100470 // We currently don't use callee-saved registers.
471 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000472 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
473 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000474 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
475
476 *data = vmap_encoder.GetData();
477}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000478
Nicolas Geoffray39468442014-09-02 15:17:15 +0100479void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
480 uint32_t size = stack_map_stream_.ComputeNeededSize();
481 data->resize(size);
482 MemoryRegion region(data->data(), size);
483 stack_map_stream_.FillIn(region);
484}
485
486void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
487 // Collect PC infos for the mapping table.
488 struct PcInfo pc_info;
489 pc_info.dex_pc = dex_pc;
490 pc_info.native_pc = GetAssembler()->CodeSize();
491 pc_infos_.Add(pc_info);
492
493 // Populate stack map information.
494
495 if (instruction == nullptr) {
496 // For stack overflow checks.
497 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
498 return;
499 }
500
501 LocationSummary* locations = instruction->GetLocations();
502 HEnvironment* environment = instruction->GetEnvironment();
503
504 size_t environment_size = instruction->EnvironmentSize();
505
506 size_t register_mask = 0;
507 size_t inlining_depth = 0;
508 stack_map_stream_.AddStackMapEntry(
509 dex_pc, pc_info.native_pc, register_mask,
510 locations->GetStackMask(), environment_size, inlining_depth);
511
512 // Walk over the environment, and record the location of dex registers.
513 for (size_t i = 0; i < environment_size; ++i) {
514 HInstruction* current = environment->GetInstructionAt(i);
515 if (current == nullptr) {
516 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
517 continue;
518 }
519
520 Location location = locations->GetEnvironmentAt(i);
521 switch (location.GetKind()) {
522 case Location::kConstant: {
523 DCHECK(current == location.GetConstant());
524 if (current->IsLongConstant()) {
525 int64_t value = current->AsLongConstant()->GetValue();
526 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
527 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
528 ++i;
529 DCHECK_LT(i, environment_size);
530 } else {
531 DCHECK(current->IsIntConstant());
532 int32_t value = current->AsIntConstant()->GetValue();
533 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
534 }
535 break;
536 }
537
538 case Location::kStackSlot: {
539 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
540 break;
541 }
542
543 case Location::kDoubleStackSlot: {
544 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
545 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
546 location.GetHighStackIndex(kVRegSize));
547 ++i;
548 DCHECK_LT(i, environment_size);
549 break;
550 }
551
552 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100554 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100555 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100556 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
557 ++i;
558 DCHECK_LT(i, environment_size);
559 }
560 break;
561 }
562
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100563 case Location::kFpuRegister : {
564 int id = location.reg();
565 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
566 if (current->GetType() == Primitive::kPrimDouble) {
567 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
568 ++i;
569 DCHECK_LT(i, environment_size);
570 }
571 break;
572 }
573
Nicolas Geoffray39468442014-09-02 15:17:15 +0100574 default:
575 LOG(FATAL) << "Unexpected kind " << location.GetKind();
576 }
577 }
578}
579
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100580void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
581 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100582 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100583 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
584 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100585 // If the register holds an object, update the stack mask.
586 if (locations->RegisterContainsObject(i)) {
587 locations->SetStackBit(stack_offset / kVRegSize);
588 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100589 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100590 }
591 }
592
593 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
594 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100595 stack_offset += SaveFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100596 }
597 }
598}
599
600void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
601 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100602 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100603 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
604 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100605 stack_offset += RestoreCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100606 }
607 }
608
609 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
610 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100611 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100612 }
613 }
614}
615
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100616void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
617 LocationSummary* locations = suspend_check->GetLocations();
618 HBasicBlock* block = suspend_check->GetBlock();
619 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
620 DCHECK(block->IsLoopHeader());
621
622 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
623 HInstruction* current = it.Current();
624 LiveInterval* interval = current->GetLiveInterval();
625 // We only need to clear bits of loop phis containing objects and allocated in register.
626 // Loop phis allocated on stack already have the object in the stack.
627 if (current->GetType() == Primitive::kPrimNot
628 && interval->HasRegister()
629 && interval->HasSpillSlot()) {
630 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
631 }
632 }
633}
634
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000635} // namespace art