blob: 28ff1cf83ad855dbcc55040fef98793693fecd0b [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 Geoffray3c049742014-09-24 18:10:46 +010029#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000031#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000032#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033
34namespace art {
35
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010036void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010037 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
38 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
39 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010040 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010041
42 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010043 if (!is_leaf) {
44 MarkNotLeaf();
45 }
Nicolas Geoffray39468442014-09-02 15:17:15 +010046 ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs()
47 + GetGraph()->GetNumberOfTemporaries()
48 + 1 /* filler */,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010049 0, /* the baseline compiler does not have live registers at slow path */
Nicolas Geoffray39468442014-09-02 15:17:15 +010050 GetGraph()->GetMaximumNumberOfOutVRegs()
51 + 1 /* current method */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010052 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010053
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010054 HGraphVisitor* location_builder = GetLocationBuilder();
55 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010056 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010057 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010058 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010059 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
60 HInstruction* current = it.Current();
61 current->Accept(location_builder);
62 InitLocations(current);
63 current->Accept(instruction_visitor);
64 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000065 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010067
Nicolas Geoffray787c3072014-03-17 10:20:19 +000068 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000069 uint8_t* buffer = allocator->Allocate(code_size);
70 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000071 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000072}
73
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010074void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
75 // The frame size has already been computed during register allocation.
76 DCHECK_NE(frame_size_, kUninitializedFrameSize);
77 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
78 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
79 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010080 Initialize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010081
82 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010083 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010084 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
85 HBasicBlock* block = blocks.Get(i);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010086 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010087 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
88 HInstruction* current = it.Current();
89 current->Accept(instruction_visitor);
90 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010092 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010093
94 size_t code_size = GetAssembler()->CodeSize();
95 uint8_t* buffer = allocator->Allocate(code_size);
96 MemoryRegion code(buffer, code_size);
97 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098}
99
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100100void CodeGenerator::GenerateSlowPaths() {
101 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
102 slow_paths_.Get(i)->EmitNativeCode(this);
103 }
104}
105
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100106size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
107 for (size_t i = 0; i < length; ++i) {
108 if (!array[i]) {
109 array[i] = true;
110 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100111 }
112 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100113 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000114 UNREACHABLE();
115 return -1;
116}
117
118size_t CodeGenerator::FindTwoFreeConsecutiveEntries(bool* array, size_t length) {
119 for (size_t i = 0; i < length - 1; ++i) {
120 if (!array[i] && !array[i + 1]) {
121 array[i] = true;
122 array[i + 1] = true;
123 return i;
124 }
125 }
126 LOG(FATAL) << "Could not find a register in baseline register allocator";
127 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100128 return -1;
129}
130
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100131void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
132 size_t maximum_number_of_live_registers,
133 size_t number_of_out_slots) {
134 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
135
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100136 SetFrameSize(RoundUp(
137 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100138 + number_of_out_slots * kVRegSize
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100139 + maximum_number_of_live_registers * GetWordSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100140 + FrameEntrySpillSize(),
141 kStackAlignment));
142}
143
144Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
145 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
146 // Use the temporary region (right below the dex registers).
147 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
148 - kVRegSize // filler
149 - (number_of_locals * kVRegSize)
150 - ((1 + temp->GetIndex()) * kVRegSize);
151 return Location::StackSlot(slot);
152}
153
154int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
155 uint16_t reg_number = local->GetRegNumber();
156 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
157 if (reg_number >= number_of_locals) {
158 // Local is a parameter of the method. It is stored in the caller's frame.
159 return GetFrameSize() + kVRegSize // ART method
160 + (reg_number - number_of_locals) * kVRegSize;
161 } else {
162 // Local is a temporary in this method. It is stored in this method's frame.
163 return GetFrameSize() - FrameEntrySpillSize()
164 - kVRegSize // filler.
165 - (number_of_locals * kVRegSize)
166 + (reg_number * kVRegSize);
167 }
168}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100169
170void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
171 LocationSummary* locations = instruction->GetLocations();
172 if (locations == nullptr) return;
173
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100174 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
175 blocked_core_registers_[i] = false;
176 }
177
178 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
179 blocked_fpu_registers_[i] = false;
180 }
181
182 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
183 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100184 }
185
186 // Mark all fixed input, temp and output registers as used.
187 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
188 Location loc = locations->InAt(i);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100189 // The DCHECKS below check that a register is not specified twice in
190 // the summary.
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100191 if (loc.IsRegister()) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100192 DCHECK(!blocked_core_registers_[loc.reg()]);
193 blocked_core_registers_[loc.reg()] = true;
194 } else if (loc.IsFpuRegister()) {
195 DCHECK(!blocked_fpu_registers_[loc.reg()]);
196 blocked_fpu_registers_[loc.reg()] = true;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000197 } else if (loc.IsFpuRegisterPair()) {
198 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
199 blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
200 DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
201 blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100202 } else if (loc.IsRegisterPair()) {
203 DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
204 blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
205 DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
206 blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100207 }
208 }
209
210 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
211 Location loc = locations->GetTemp(i);
212 if (loc.IsRegister()) {
213 // Check that a register is not specified twice in the summary.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100214 DCHECK(!blocked_core_registers_[loc.reg()]);
215 blocked_core_registers_[loc.reg()] = true;
216 } else {
217 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100218 }
219 }
220
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100221 SetupBlockedRegisters();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100222
223 // Allocate all unallocated input locations.
224 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
225 Location loc = locations->InAt(i);
226 HInstruction* input = instruction->InputAt(i);
227 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100228 if ((loc.GetPolicy() == Location::kRequiresRegister)
229 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100230 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100231 } else {
232 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
233 HLoadLocal* load = input->AsLoadLocal();
234 if (load != nullptr) {
235 loc = GetStackLocation(load);
236 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100237 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100238 }
239 }
240 locations->SetInAt(i, loc);
241 }
242 }
243
244 // Allocate all unallocated temp locations.
245 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
246 Location loc = locations->GetTemp(i);
247 if (loc.IsUnallocated()) {
248 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
249 // TODO: Adjust handling of temps. We currently consider temps to use
250 // core registers. They may also use floating point registers at some point.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100251 loc = AllocateFreeRegister(Primitive::kPrimInt);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100252 locations->SetTempAt(i, loc);
253 }
254 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100255 Location result_location = locations->Out();
256 if (result_location.IsUnallocated()) {
257 switch (result_location.GetPolicy()) {
258 case Location::kAny:
259 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100260 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100261 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100262 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100263 case Location::kSameAsFirstInput:
264 result_location = locations->InAt(0);
265 break;
266 }
267 locations->SetOut(result_location);
268 }
269}
270
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000271void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100272 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100273 if (instruction->IsTemporary()) {
274 HInstruction* previous = instruction->GetPrevious();
275 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
276 Move(previous, temp_location, instruction);
277 previous->GetLocations()->SetOut(temp_location);
278 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100279 return;
280 }
281 AllocateRegistersLocally(instruction);
282 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000283 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000284 if (location.IsValid()) {
285 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100286 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000287 }
288 }
289}
290
291bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000292 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000293 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000294}
295
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000296CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
297 HGraph* graph,
298 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000299 switch (instruction_set) {
300 case kArm:
301 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000302 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000303 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100304 case kArm64: {
305 return new (allocator) arm64::CodeGeneratorARM64(graph);
306 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000307 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000308 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000309 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000310 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000311 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700312 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100313 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700314 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000315 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000316 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000317 }
318}
319
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000320void CodeGenerator::BuildNativeGCMap(
321 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
322 const std::vector<uint8_t>& gc_map_raw =
323 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
324 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
325
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000326 uint32_t max_native_offset = 0;
327 for (size_t i = 0; i < pc_infos_.Size(); i++) {
328 uint32_t native_offset = pc_infos_.Get(i).native_pc;
329 if (native_offset > max_native_offset) {
330 max_native_offset = native_offset;
331 }
332 }
333
334 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
335 for (size_t i = 0; i < pc_infos_.Size(); i++) {
336 struct PcInfo pc_info = pc_infos_.Get(i);
337 uint32_t native_offset = pc_info.native_pc;
338 uint32_t dex_pc = pc_info.dex_pc;
339 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
340 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
341 builder.AddEntry(native_offset, references);
342 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000343}
344
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700345void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000346 uint32_t pc2dex_data_size = 0u;
347 uint32_t pc2dex_entries = pc_infos_.Size();
348 uint32_t pc2dex_offset = 0u;
349 int32_t pc2dex_dalvik_offset = 0;
350 uint32_t dex2pc_data_size = 0u;
351 uint32_t dex2pc_entries = 0u;
352
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700353 if (src_map != nullptr) {
354 src_map->reserve(pc2dex_entries);
355 }
356
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000357 // We currently only have pc2dex entries.
358 for (size_t i = 0; i < pc2dex_entries; i++) {
359 struct PcInfo pc_info = pc_infos_.Get(i);
360 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
361 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
362 pc2dex_offset = pc_info.native_pc;
363 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700364 if (src_map != nullptr) {
365 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
366 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000367 }
368
369 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
370 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
371 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
372 data->resize(data_size);
373
374 uint8_t* data_ptr = &(*data)[0];
375 uint8_t* write_pos = data_ptr;
376 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
377 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
378 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
379 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
380
381 pc2dex_offset = 0u;
382 pc2dex_dalvik_offset = 0u;
383 for (size_t i = 0; i < pc2dex_entries; i++) {
384 struct PcInfo pc_info = pc_infos_.Get(i);
385 DCHECK(pc2dex_offset <= pc_info.native_pc);
386 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
387 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
388 pc2dex_offset = pc_info.native_pc;
389 pc2dex_dalvik_offset = pc_info.dex_pc;
390 }
391 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
392 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
393
394 if (kIsDebugBuild) {
395 // Verify the encoded table holds the expected data.
396 MappingTable table(data_ptr);
397 CHECK_EQ(table.TotalSize(), total_entries);
398 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
399 auto it = table.PcToDexBegin();
400 auto it2 = table.DexToPcBegin();
401 for (size_t i = 0; i < pc2dex_entries; i++) {
402 struct PcInfo pc_info = pc_infos_.Get(i);
403 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
404 CHECK_EQ(pc_info.dex_pc, it.DexPc());
405 ++it;
406 }
407 CHECK(it == table.PcToDexEnd());
408 CHECK(it2 == table.DexToPcEnd());
409 }
410}
411
412void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
413 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100414 // We currently don't use callee-saved registers.
415 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000416 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
417 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000418 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
419
420 *data = vmap_encoder.GetData();
421}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000422
Nicolas Geoffray39468442014-09-02 15:17:15 +0100423void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
424 uint32_t size = stack_map_stream_.ComputeNeededSize();
425 data->resize(size);
426 MemoryRegion region(data->data(), size);
427 stack_map_stream_.FillIn(region);
428}
429
430void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
431 // Collect PC infos for the mapping table.
432 struct PcInfo pc_info;
433 pc_info.dex_pc = dex_pc;
434 pc_info.native_pc = GetAssembler()->CodeSize();
435 pc_infos_.Add(pc_info);
436
437 // Populate stack map information.
438
439 if (instruction == nullptr) {
440 // For stack overflow checks.
441 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
442 return;
443 }
444
445 LocationSummary* locations = instruction->GetLocations();
446 HEnvironment* environment = instruction->GetEnvironment();
447
448 size_t environment_size = instruction->EnvironmentSize();
449
450 size_t register_mask = 0;
451 size_t inlining_depth = 0;
452 stack_map_stream_.AddStackMapEntry(
453 dex_pc, pc_info.native_pc, register_mask,
454 locations->GetStackMask(), environment_size, inlining_depth);
455
456 // Walk over the environment, and record the location of dex registers.
457 for (size_t i = 0; i < environment_size; ++i) {
458 HInstruction* current = environment->GetInstructionAt(i);
459 if (current == nullptr) {
460 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
461 continue;
462 }
463
464 Location location = locations->GetEnvironmentAt(i);
465 switch (location.GetKind()) {
466 case Location::kConstant: {
467 DCHECK(current == location.GetConstant());
468 if (current->IsLongConstant()) {
469 int64_t value = current->AsLongConstant()->GetValue();
470 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
471 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
472 ++i;
473 DCHECK_LT(i, environment_size);
474 } else {
475 DCHECK(current->IsIntConstant());
476 int32_t value = current->AsIntConstant()->GetValue();
477 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
478 }
479 break;
480 }
481
482 case Location::kStackSlot: {
483 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
484 break;
485 }
486
487 case Location::kDoubleStackSlot: {
488 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
489 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
490 location.GetHighStackIndex(kVRegSize));
491 ++i;
492 DCHECK_LT(i, environment_size);
493 break;
494 }
495
496 case Location::kRegister : {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100497 int id = location.reg();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100498 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100499 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
501 ++i;
502 DCHECK_LT(i, environment_size);
503 }
504 break;
505 }
506
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100507 case Location::kFpuRegister : {
508 int id = location.reg();
509 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
510 if (current->GetType() == Primitive::kPrimDouble) {
511 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
512 ++i;
513 DCHECK_LT(i, environment_size);
514 }
515 break;
516 }
517
Nicolas Geoffray39468442014-09-02 15:17:15 +0100518 default:
519 LOG(FATAL) << "Unexpected kind " << location.GetKind();
520 }
521 }
522}
523
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100524void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
525 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100526 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100527 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
528 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100529 // If the register holds an object, update the stack mask.
530 if (locations->RegisterContainsObject(i)) {
531 locations->SetStackBit(stack_offset / kVRegSize);
532 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100533 stack_offset += SaveCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100534 }
535 }
536
537 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
538 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100539 stack_offset += SaveFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100540 }
541 }
542}
543
544void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
545 RegisterSet* register_set = locations->GetLiveRegisters();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100546 size_t stack_offset = first_register_slot_in_slow_path_;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100547 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
548 if (register_set->ContainsCoreRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100549 stack_offset += RestoreCoreRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100550 }
551 }
552
553 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
554 if (register_set->ContainsFloatingPointRegister(i)) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100555 stack_offset += RestoreFloatingPointRegister(stack_offset, i);
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100556 }
557 }
558}
559
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100560void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
561 LocationSummary* locations = suspend_check->GetLocations();
562 HBasicBlock* block = suspend_check->GetBlock();
563 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
564 DCHECK(block->IsLoopHeader());
565
566 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
567 HInstruction* current = it.Current();
568 LiveInterval* interval = current->GetLiveInterval();
569 // We only need to clear bits of loop phis containing objects and allocated in register.
570 // Loop phis allocated on stack already have the object in the stack.
571 if (current->GetType() == Primitive::kPrimNot
572 && interval->HasRegister()
573 && interval->HasSpillSlot()) {
574 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
575 }
576 }
577}
578
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000579} // namespace art