blob: 4d8154e6a00bf8a0c1fd4f47d13160f6af8fbee8 [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()
Calin Juravlef97f9fb2014-11-11 15:38:19 +000054 + GetGraph()->GetTemporariesVRegSlots()
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 + 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();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000074 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000075}
76
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010077void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
78 // The frame size has already been computed during register allocation.
79 DCHECK_NE(frame_size_, kUninitializedFrameSize);
80 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
81 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
82 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010083 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084
85 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010086 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010087 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
88 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010089 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010090 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
91 HInstruction* current = it.Current();
92 current->Accept(instruction_visitor);
93 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010095 GenerateSlowPaths();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000096 Finalize(allocator);
97}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010098
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +000099void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100 size_t code_size = GetAssembler()->CodeSize();
101 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000102
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100103 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();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000153 // The type of the previous instruction tells us if we need a single or double stack slot.
154 Primitive::Type type = temp->GetType();
155 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 // Use the temporary region (right below the dex registers).
157 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
158 - kVRegSize // filler
159 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000160 - ((temp_size + temp->GetIndex()) * kVRegSize);
161 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100162}
163
164int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
165 uint16_t reg_number = local->GetRegNumber();
166 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
167 if (reg_number >= number_of_locals) {
168 // Local is a parameter of the method. It is stored in the caller's frame.
169 return GetFrameSize() + kVRegSize // ART method
170 + (reg_number - number_of_locals) * kVRegSize;
171 } else {
172 // Local is a temporary in this method. It is stored in this method's frame.
173 return GetFrameSize() - FrameEntrySpillSize()
174 - kVRegSize // filler.
175 - (number_of_locals * kVRegSize)
176 + (reg_number * kVRegSize);
177 }
178}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100179
180void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
181 LocationSummary* locations = instruction->GetLocations();
182 if (locations == nullptr) return;
183
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100184 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
185 blocked_core_registers_[i] = false;
186 }
187
188 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
189 blocked_fpu_registers_[i] = false;
190 }
191
192 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
193 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100194 }
195
196 // Mark all fixed input, temp and output registers as used.
197 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
198 Location loc = locations->InAt(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000199 // The DCHECKS below check that a register is not specified twice in
200 // the summary.
201 if (loc.IsRegister()) {
202 DCHECK(!blocked_core_registers_[loc.reg()]);
203 blocked_core_registers_[loc.reg()] = true;
204 } else if (loc.IsFpuRegister()) {
205 DCHECK(!blocked_fpu_registers_[loc.reg()]);
206 blocked_fpu_registers_[loc.reg()] = true;
207 } else if (loc.IsFpuRegisterPair()) {
208 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
209 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
210 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
211 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
212 } else if (loc.IsRegisterPair()) {
213 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
214 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
215 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
216 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
217 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100218 }
219
220 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
221 Location loc = locations->GetTemp(i);
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000222 // The DCHECKS below check that a register is not specified twice in
223 // the summary.
224 if (loc.IsRegister()) {
225 DCHECK(!blocked_core_registers_[loc.reg()]);
226 blocked_core_registers_[loc.reg()] = true;
227 } else if (loc.IsFpuRegister()) {
228 DCHECK(!blocked_fpu_registers_[loc.reg()]);
229 blocked_fpu_registers_[loc.reg()] = true;
230 } else {
231 DCHECK(loc.GetPolicy() == Location::kRequiresRegister
232 || loc.GetPolicy() == Location::kRequiresFpuRegister);
233 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100234 }
235
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100236 SetupBlockedRegisters();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100237
238 // Allocate all unallocated input locations.
239 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
240 Location loc = locations->InAt(i);
241 HInstruction* input = instruction->InputAt(i);
242 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100243 if ((loc.GetPolicy() == Location::kRequiresRegister)
244 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100245 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100246 } else {
247 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
248 HLoadLocal* load = input->AsLoadLocal();
249 if (load != nullptr) {
250 loc = GetStackLocation(load);
251 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100252 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100253 }
254 }
255 locations->SetInAt(i, loc);
256 }
257 }
258
259 // Allocate all unallocated temp locations.
260 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
261 Location loc = locations->GetTemp(i);
262 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000263 switch (loc.GetPolicy()) {
264 case Location::kRequiresRegister:
265 // Allocate a core register (large enough to fit a 32-bit integer).
266 loc = AllocateFreeRegister(Primitive::kPrimInt);
267 break;
268
269 case Location::kRequiresFpuRegister:
270 // Allocate a core register (large enough to fit a 64-bit double).
271 loc = AllocateFreeRegister(Primitive::kPrimDouble);
272 break;
273
274 default:
275 LOG(FATAL) << "Unexpected policy for temporary location "
276 << loc.GetPolicy();
277 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100278 locations->SetTempAt(i, loc);
279 }
280 }
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +0000281 Location result_location = locations->Out();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100282 if (result_location.IsUnallocated()) {
283 switch (result_location.GetPolicy()) {
284 case Location::kAny:
285 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100286 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100287 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100288 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289 case Location::kSameAsFirstInput:
290 result_location = locations->InAt(0);
291 break;
292 }
293 locations->SetOut(result_location);
294 }
295}
296
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000297void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100298 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100299 if (instruction->IsTemporary()) {
300 HInstruction* previous = instruction->GetPrevious();
301 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
302 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100303 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100304 return;
305 }
306 AllocateRegistersLocally(instruction);
307 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000308 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000309 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000310 if (location.IsValid()) {
311 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000312 if (input->GetNext()->IsTemporary()) {
313 // If the input was stored in a temporary, use that temporary to
314 // perform the move.
315 Move(input->GetNext(), location, instruction);
316 } else {
317 Move(input, location, instruction);
318 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000319 }
320 }
321}
322
323bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000324 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000326}
327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
329 HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000330 InstructionSet instruction_set,
331 const InstructionSetFeatures& isa_features) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000332 switch (instruction_set) {
333 case kArm:
334 case kThumb2: {
Calin Juravle34166012014-12-19 17:22:29 +0000335 return new (allocator) arm::CodeGeneratorARM(graph,
336 isa_features.AsArmInstructionSetFeatures());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000337 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100338 case kArm64: {
339 return new (allocator) arm64::CodeGeneratorARM64(graph);
340 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000341 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000342 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000343 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000345 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700346 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100347 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700348 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000349 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000351 }
352}
353
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000354void CodeGenerator::BuildNativeGCMap(
355 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
356 const std::vector<uint8_t>& gc_map_raw =
357 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
358 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
359
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000360 uint32_t max_native_offset = 0;
361 for (size_t i = 0; i < pc_infos_.Size(); i++) {
362 uint32_t native_offset = pc_infos_.Get(i).native_pc;
363 if (native_offset > max_native_offset) {
364 max_native_offset = native_offset;
365 }
366 }
367
368 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
369 for (size_t i = 0; i < pc_infos_.Size(); i++) {
370 struct PcInfo pc_info = pc_infos_.Get(i);
371 uint32_t native_offset = pc_info.native_pc;
372 uint32_t dex_pc = pc_info.dex_pc;
373 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
374 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
375 builder.AddEntry(native_offset, references);
376 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000377}
378
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800379void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000380 uint32_t pc2dex_data_size = 0u;
381 uint32_t pc2dex_entries = pc_infos_.Size();
382 uint32_t pc2dex_offset = 0u;
383 int32_t pc2dex_dalvik_offset = 0;
384 uint32_t dex2pc_data_size = 0u;
385 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000386 uint32_t dex2pc_offset = 0u;
387 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000388
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700389 if (src_map != nullptr) {
390 src_map->reserve(pc2dex_entries);
391 }
392
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000393 for (size_t i = 0; i < pc2dex_entries; i++) {
394 struct PcInfo pc_info = pc_infos_.Get(i);
395 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
396 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
397 pc2dex_offset = pc_info.native_pc;
398 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700399 if (src_map != nullptr) {
400 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
401 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000402 }
403
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000404 // Walk over the blocks and find which ones correspond to catch block entries.
405 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
406 HBasicBlock* block = graph_->GetBlocks().Get(i);
407 if (block->IsCatchBlock()) {
408 intptr_t native_pc = GetAddressOf(block);
409 ++dex2pc_entries;
410 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
411 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
412 dex2pc_offset = native_pc;
413 dex2pc_dalvik_offset = block->GetDexPc();
414 }
415 }
416
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000417 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
418 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
419 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
420 data->resize(data_size);
421
422 uint8_t* data_ptr = &(*data)[0];
423 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000424
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000425 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
426 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
427 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
428 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
429
430 pc2dex_offset = 0u;
431 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000432 dex2pc_offset = 0u;
433 dex2pc_dalvik_offset = 0u;
434
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000435 for (size_t i = 0; i < pc2dex_entries; i++) {
436 struct PcInfo pc_info = pc_infos_.Get(i);
437 DCHECK(pc2dex_offset <= pc_info.native_pc);
438 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
439 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
440 pc2dex_offset = pc_info.native_pc;
441 pc2dex_dalvik_offset = pc_info.dex_pc;
442 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000443
444 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
445 HBasicBlock* block = graph_->GetBlocks().Get(i);
446 if (block->IsCatchBlock()) {
447 intptr_t native_pc = GetAddressOf(block);
448 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
449 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
450 dex2pc_offset = native_pc;
451 dex2pc_dalvik_offset = block->GetDexPc();
452 }
453 }
454
455
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000456 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
457 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
458
459 if (kIsDebugBuild) {
460 // Verify the encoded table holds the expected data.
461 MappingTable table(data_ptr);
462 CHECK_EQ(table.TotalSize(), total_entries);
463 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
464 auto it = table.PcToDexBegin();
465 auto it2 = table.DexToPcBegin();
466 for (size_t i = 0; i < pc2dex_entries; i++) {
467 struct PcInfo pc_info = pc_infos_.Get(i);
468 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
469 CHECK_EQ(pc_info.dex_pc, it.DexPc());
470 ++it;
471 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000472 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
473 HBasicBlock* block = graph_->GetBlocks().Get(i);
474 if (block->IsCatchBlock()) {
475 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
476 CHECK_EQ(block->GetDexPc(), it2.DexPc());
477 ++it2;
478 }
479 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000480 CHECK(it == table.PcToDexEnd());
481 CHECK(it2 == table.DexToPcEnd());
482 }
483}
484
485void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
486 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100487 // We currently don't use callee-saved registers.
488 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000489 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
490 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000491 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
492
493 *data = vmap_encoder.GetData();
494}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000495
Nicolas Geoffray39468442014-09-02 15:17:15 +0100496void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
497 uint32_t size = stack_map_stream_.ComputeNeededSize();
498 data->resize(size);
499 MemoryRegion region(data->data(), size);
500 stack_map_stream_.FillIn(region);
501}
502
503void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000504 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000505 // The code generated for some type conversions may call the
506 // runtime, thus normally requiring a subsequent call to this
507 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000508 // information for certain instructions, which are considered "atomic"
509 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000510 // Therefore we do not currently record PC information for such
511 // instructions. As this may change later, we added this special
512 // case so that code generators may nevertheless call
513 // CodeGenerator::RecordPcInfo without triggering an error in
514 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
515 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000516 if (instruction->IsTypeConversion()) {
517 return;
518 }
519 if (instruction->IsRem()) {
520 Primitive::Type type = instruction->AsRem()->GetResultType();
521 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
522 return;
523 }
524 }
Roland Levillain624279f2014-12-04 11:54:28 +0000525 }
526
Nicolas Geoffray39468442014-09-02 15:17:15 +0100527 // Collect PC infos for the mapping table.
528 struct PcInfo pc_info;
529 pc_info.dex_pc = dex_pc;
530 pc_info.native_pc = GetAssembler()->CodeSize();
531 pc_infos_.Add(pc_info);
532
533 // Populate stack map information.
534
535 if (instruction == nullptr) {
536 // For stack overflow checks.
537 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
538 return;
539 }
540
541 LocationSummary* locations = instruction->GetLocations();
542 HEnvironment* environment = instruction->GetEnvironment();
543
544 size_t environment_size = instruction->EnvironmentSize();
545
546 size_t register_mask = 0;
547 size_t inlining_depth = 0;
548 stack_map_stream_.AddStackMapEntry(
549 dex_pc, pc_info.native_pc, register_mask,
550 locations->GetStackMask(), environment_size, inlining_depth);
551
552 // Walk over the environment, and record the location of dex registers.
553 for (size_t i = 0; i < environment_size; ++i) {
554 HInstruction* current = environment->GetInstructionAt(i);
555 if (current == nullptr) {
556 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
557 continue;
558 }
559
560 Location location = locations->GetEnvironmentAt(i);
561 switch (location.GetKind()) {
562 case Location::kConstant: {
563 DCHECK(current == location.GetConstant());
564 if (current->IsLongConstant()) {
565 int64_t value = current->AsLongConstant()->GetValue();
566 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
567 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
568 ++i;
569 DCHECK_LT(i, environment_size);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000570 } else if (current->IsDoubleConstant()) {
571 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
572 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
573 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
574 ++i;
575 DCHECK_LT(i, environment_size);
576 } else if (current->IsIntConstant()) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100577 int32_t value = current->AsIntConstant()->GetValue();
578 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000579 } else {
580 DCHECK(current->IsFloatConstant());
581 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
582 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100583 }
584 break;
585 }
586
587 case Location::kStackSlot: {
588 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
589 break;
590 }
591
592 case Location::kDoubleStackSlot: {
593 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
594 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
595 location.GetHighStackIndex(kVRegSize));
596 ++i;
597 DCHECK_LT(i, environment_size);
598 break;
599 }
600
601 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100602 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100603 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100604 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100605 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
606 ++i;
607 DCHECK_LT(i, environment_size);
608 }
609 break;
610 }
611
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100612 case Location::kFpuRegister : {
613 int id = location.reg();
614 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
615 if (current->GetType() == Primitive::kPrimDouble) {
616 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
617 ++i;
618 DCHECK_LT(i, environment_size);
619 }
620 break;
621 }
622
Nicolas Geoffray39468442014-09-02 15:17:15 +0100623 default:
624 LOG(FATAL) << "Unexpected kind " << location.GetKind();
625 }
626 }
627}
628
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100629void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
630 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100631 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100632 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
633 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100634 // If the register holds an object, update the stack mask.
635 if (locations->RegisterContainsObject(i)) {
636 locations->SetStackBit(stack_offset / kVRegSize);
637 }
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000638 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100639 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100640 }
641 }
642
643 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
644 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000645 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100646 stack_offset += SaveFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100647 }
648 }
649}
650
651void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
652 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100653 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100654 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
655 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000656 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100657 stack_offset += RestoreCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100658 }
659 }
660
661 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
662 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray87d03762014-11-19 15:17:56 +0000663 DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100664 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100665 }
666 }
667}
668
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100669void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
670 LocationSummary* locations = suspend_check->GetLocations();
671 HBasicBlock* block = suspend_check->GetBlock();
672 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
673 DCHECK(block->IsLoopHeader());
674
675 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
676 HInstruction* current = it.Current();
677 LiveInterval* interval = current->GetLiveInterval();
678 // We only need to clear bits of loop phis containing objects and allocated in register.
679 // Loop phis allocated on stack already have the object in the stack.
680 if (current->GetType() == Primitive::kPrimNot
681 && interval->HasRegister()
682 && interval->HasSpillSlot()) {
683 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
684 }
685 }
686}
687
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000688void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
689 MoveOperands move1(from1, to1, nullptr);
690 MoveOperands move2(from2, to2, nullptr);
691 HParallelMove parallel_move(GetGraph()->GetArena());
692 parallel_move.AddMove(&move1);
693 parallel_move.AddMove(&move2);
694 GetMoveResolver()->EmitNativeCode(&parallel_move);
695}
696
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000697} // namespace art