blob: 2a9a7b37ab9650b624740652d6afa3e90549a58a [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"
20#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010021#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070022#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000023#include "dex/verified_method.h"
24#include "driver/dex_compilation_unit.h"
25#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000026#include "leb128.h"
27#include "mapping_table.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010028#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000030#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032
33namespace art {
34
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010035void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010036 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
37 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
38 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010039 block_labels_.SetSize(blocks.Size());
40
41 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010042 if (!is_leaf) {
43 MarkNotLeaf();
44 }
Nicolas Geoffray39468442014-09-02 15:17:15 +010045 ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs()
46 + GetGraph()->GetNumberOfTemporaries()
47 + 1 /* filler */,
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +010048 0, /* the baseline compiler does not have live registers at slow path */
Nicolas Geoffray39468442014-09-02 15:17:15 +010049 GetGraph()->GetMaximumNumberOfOutVRegs()
50 + 1 /* current method */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010051 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010052
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010053 HGraphVisitor* location_builder = GetLocationBuilder();
54 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010055 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010056 HBasicBlock* block = blocks.Get(i);
57 Bind(GetLabelOf(block));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
59 HInstruction* current = it.Current();
60 current->Accept(location_builder);
61 InitLocations(current);
62 current->Accept(instruction_visitor);
63 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000064 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010066
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068 uint8_t* buffer = allocator->Allocate(code_size);
69 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000070 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000071}
72
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010073void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
74 // The frame size has already been computed during register allocation.
75 DCHECK_NE(frame_size_, kUninitializedFrameSize);
76 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
77 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
78 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
79 block_labels_.SetSize(blocks.Size());
80
81 GenerateFrameEntry();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010082 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010083 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
84 HBasicBlock* block = blocks.Get(i);
85 Bind(GetLabelOf(block));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010086 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
87 HInstruction* current = it.Current();
88 current->Accept(instruction_visitor);
89 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010092
93 size_t code_size = GetAssembler()->CodeSize();
94 uint8_t* buffer = allocator->Allocate(code_size);
95 MemoryRegion code(buffer, code_size);
96 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097}
98
Nicolas Geoffraye5038322014-07-04 09:41:32 +010099void CodeGenerator::GenerateSlowPaths() {
100 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
101 slow_paths_.Get(i)->EmitNativeCode(this);
102 }
103}
104
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100105size_t CodeGenerator::AllocateFreeRegisterInternal(
106 bool* blocked_registers, size_t number_of_registers) const {
107 for (size_t regno = 0; regno < number_of_registers; regno++) {
108 if (!blocked_registers[regno]) {
109 blocked_registers[regno] = true;
110 return regno;
111 }
112 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100113 return -1;
114}
115
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100116void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
117 size_t maximum_number_of_live_registers,
118 size_t number_of_out_slots) {
119 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
120
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100121 SetFrameSize(RoundUp(
122 number_of_spill_slots * kVRegSize
Nicolas Geoffray39468442014-09-02 15:17:15 +0100123 + number_of_out_slots * kVRegSize
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100124 + maximum_number_of_live_registers * GetWordSize()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100125 + FrameEntrySpillSize(),
126 kStackAlignment));
127}
128
129Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
130 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
131 // Use the temporary region (right below the dex registers).
132 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
133 - kVRegSize // filler
134 - (number_of_locals * kVRegSize)
135 - ((1 + temp->GetIndex()) * kVRegSize);
136 return Location::StackSlot(slot);
137}
138
139int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
140 uint16_t reg_number = local->GetRegNumber();
141 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
142 if (reg_number >= number_of_locals) {
143 // Local is a parameter of the method. It is stored in the caller's frame.
144 return GetFrameSize() + kVRegSize // ART method
145 + (reg_number - number_of_locals) * kVRegSize;
146 } else {
147 // Local is a temporary in this method. It is stored in this method's frame.
148 return GetFrameSize() - FrameEntrySpillSize()
149 - kVRegSize // filler.
150 - (number_of_locals * kVRegSize)
151 + (reg_number * kVRegSize);
152 }
153}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100154
155void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
156 LocationSummary* locations = instruction->GetLocations();
157 if (locations == nullptr) return;
158
159 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
160 blocked_registers_[i] = false;
161 }
162
163 // Mark all fixed input, temp and output registers as used.
164 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
165 Location loc = locations->InAt(i);
166 if (loc.IsRegister()) {
167 // Check that a register is not specified twice in the summary.
168 DCHECK(!blocked_registers_[loc.GetEncoding()]);
169 blocked_registers_[loc.GetEncoding()] = true;
170 }
171 }
172
173 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
174 Location loc = locations->GetTemp(i);
175 if (loc.IsRegister()) {
176 // Check that a register is not specified twice in the summary.
177 DCHECK(!blocked_registers_[loc.GetEncoding()]);
178 blocked_registers_[loc.GetEncoding()] = true;
179 }
180 }
181
182 SetupBlockedRegisters(blocked_registers_);
183
184 // Allocate all unallocated input locations.
185 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
186 Location loc = locations->InAt(i);
187 HInstruction* input = instruction->InputAt(i);
188 if (loc.IsUnallocated()) {
189 if (loc.GetPolicy() == Location::kRequiresRegister) {
190 loc = Location::RegisterLocation(
191 AllocateFreeRegister(input->GetType(), blocked_registers_));
192 } else {
193 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
194 HLoadLocal* load = input->AsLoadLocal();
195 if (load != nullptr) {
196 loc = GetStackLocation(load);
197 } else {
198 loc = Location::RegisterLocation(
199 AllocateFreeRegister(input->GetType(), blocked_registers_));
200 }
201 }
202 locations->SetInAt(i, loc);
203 }
204 }
205
206 // Allocate all unallocated temp locations.
207 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
208 Location loc = locations->GetTemp(i);
209 if (loc.IsUnallocated()) {
210 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
211 // TODO: Adjust handling of temps. We currently consider temps to use
212 // core registers. They may also use floating point registers at some point.
213 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
214 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
215 locations->SetTempAt(i, loc);
216 }
217 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100218 Location result_location = locations->Out();
219 if (result_location.IsUnallocated()) {
220 switch (result_location.GetPolicy()) {
221 case Location::kAny:
222 case Location::kRequiresRegister:
223 result_location = Location::RegisterLocation(
224 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
225 break;
226 case Location::kSameAsFirstInput:
227 result_location = locations->InAt(0);
228 break;
229 }
230 locations->SetOut(result_location);
231 }
232}
233
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000234void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100235 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100236 if (instruction->IsTemporary()) {
237 HInstruction* previous = instruction->GetPrevious();
238 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
239 Move(previous, temp_location, instruction);
240 previous->GetLocations()->SetOut(temp_location);
241 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100242 return;
243 }
244 AllocateRegistersLocally(instruction);
245 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000246 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000247 if (location.IsValid()) {
248 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100249 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000250 }
251 }
252}
253
254bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000255 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000256 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000257}
258
259Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000260 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000261}
262
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000263CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
264 HGraph* graph,
265 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000266 switch (instruction_set) {
267 case kArm:
268 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000269 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000270 }
271 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000273 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000274 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000275 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700276 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100277 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700278 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000279 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000280 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000281 }
282}
283
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000284void CodeGenerator::BuildNativeGCMap(
285 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
286 const std::vector<uint8_t>& gc_map_raw =
287 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
288 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
289
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000290 uint32_t max_native_offset = 0;
291 for (size_t i = 0; i < pc_infos_.Size(); i++) {
292 uint32_t native_offset = pc_infos_.Get(i).native_pc;
293 if (native_offset > max_native_offset) {
294 max_native_offset = native_offset;
295 }
296 }
297
298 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
299 for (size_t i = 0; i < pc_infos_.Size(); i++) {
300 struct PcInfo pc_info = pc_infos_.Get(i);
301 uint32_t native_offset = pc_info.native_pc;
302 uint32_t dex_pc = pc_info.dex_pc;
303 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
304 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
305 builder.AddEntry(native_offset, references);
306 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000307}
308
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700309void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000310 uint32_t pc2dex_data_size = 0u;
311 uint32_t pc2dex_entries = pc_infos_.Size();
312 uint32_t pc2dex_offset = 0u;
313 int32_t pc2dex_dalvik_offset = 0;
314 uint32_t dex2pc_data_size = 0u;
315 uint32_t dex2pc_entries = 0u;
316
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700317 if (src_map != nullptr) {
318 src_map->reserve(pc2dex_entries);
319 }
320
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000321 // We currently only have pc2dex entries.
322 for (size_t i = 0; i < pc2dex_entries; i++) {
323 struct PcInfo pc_info = pc_infos_.Get(i);
324 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
325 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
326 pc2dex_offset = pc_info.native_pc;
327 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700328 if (src_map != nullptr) {
329 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
330 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000331 }
332
333 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
334 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
335 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
336 data->resize(data_size);
337
338 uint8_t* data_ptr = &(*data)[0];
339 uint8_t* write_pos = data_ptr;
340 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
341 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
342 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
343 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
344
345 pc2dex_offset = 0u;
346 pc2dex_dalvik_offset = 0u;
347 for (size_t i = 0; i < pc2dex_entries; i++) {
348 struct PcInfo pc_info = pc_infos_.Get(i);
349 DCHECK(pc2dex_offset <= pc_info.native_pc);
350 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
351 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
352 pc2dex_offset = pc_info.native_pc;
353 pc2dex_dalvik_offset = pc_info.dex_pc;
354 }
355 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
356 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
357
358 if (kIsDebugBuild) {
359 // Verify the encoded table holds the expected data.
360 MappingTable table(data_ptr);
361 CHECK_EQ(table.TotalSize(), total_entries);
362 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
363 auto it = table.PcToDexBegin();
364 auto it2 = table.DexToPcBegin();
365 for (size_t i = 0; i < pc2dex_entries; i++) {
366 struct PcInfo pc_info = pc_infos_.Get(i);
367 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
368 CHECK_EQ(pc_info.dex_pc, it.DexPc());
369 ++it;
370 }
371 CHECK(it == table.PcToDexEnd());
372 CHECK(it2 == table.DexToPcEnd());
373 }
374}
375
376void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
377 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100378 // We currently don't use callee-saved registers.
379 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000380 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
381 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000382 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
383
384 *data = vmap_encoder.GetData();
385}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000386
Nicolas Geoffray39468442014-09-02 15:17:15 +0100387void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
388 uint32_t size = stack_map_stream_.ComputeNeededSize();
389 data->resize(size);
390 MemoryRegion region(data->data(), size);
391 stack_map_stream_.FillIn(region);
392}
393
394void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
395 // Collect PC infos for the mapping table.
396 struct PcInfo pc_info;
397 pc_info.dex_pc = dex_pc;
398 pc_info.native_pc = GetAssembler()->CodeSize();
399 pc_infos_.Add(pc_info);
400
401 // Populate stack map information.
402
403 if (instruction == nullptr) {
404 // For stack overflow checks.
405 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
406 return;
407 }
408
409 LocationSummary* locations = instruction->GetLocations();
410 HEnvironment* environment = instruction->GetEnvironment();
411
412 size_t environment_size = instruction->EnvironmentSize();
413
414 size_t register_mask = 0;
415 size_t inlining_depth = 0;
416 stack_map_stream_.AddStackMapEntry(
417 dex_pc, pc_info.native_pc, register_mask,
418 locations->GetStackMask(), environment_size, inlining_depth);
419
420 // Walk over the environment, and record the location of dex registers.
421 for (size_t i = 0; i < environment_size; ++i) {
422 HInstruction* current = environment->GetInstructionAt(i);
423 if (current == nullptr) {
424 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
425 continue;
426 }
427
428 Location location = locations->GetEnvironmentAt(i);
429 switch (location.GetKind()) {
430 case Location::kConstant: {
431 DCHECK(current == location.GetConstant());
432 if (current->IsLongConstant()) {
433 int64_t value = current->AsLongConstant()->GetValue();
434 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
435 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
436 ++i;
437 DCHECK_LT(i, environment_size);
438 } else {
439 DCHECK(current->IsIntConstant());
440 int32_t value = current->AsIntConstant()->GetValue();
441 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
442 }
443 break;
444 }
445
446 case Location::kStackSlot: {
447 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
448 break;
449 }
450
451 case Location::kDoubleStackSlot: {
452 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
453 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
454 location.GetHighStackIndex(kVRegSize));
455 ++i;
456 DCHECK_LT(i, environment_size);
457 break;
458 }
459
460 case Location::kRegister : {
461 int id = location.reg().RegId();
462 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
463 if (current->GetType() == Primitive::kPrimDouble
464 || current->GetType() == Primitive::kPrimLong) {
465 stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
466 ++i;
467 DCHECK_LT(i, environment_size);
468 }
469 break;
470 }
471
472 default:
473 LOG(FATAL) << "Unexpected kind " << location.GetKind();
474 }
475 }
476}
477
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100478size_t CodeGenerator::GetStackOffsetOfSavedRegister(size_t index) {
479 return first_register_slot_in_slow_path_ + index * GetWordSize();
480}
481
482void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
483 RegisterSet* register_set = locations->GetLiveRegisters();
484 uint32_t count = 0;
485 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
486 if (register_set->ContainsCoreRegister(i)) {
487 size_t stack_offset = GetStackOffsetOfSavedRegister(count);
488 ++count;
489 SaveCoreRegister(Location::StackSlot(stack_offset), i);
490 // If the register holds an object, update the stack mask.
491 if (locations->RegisterContainsObject(i)) {
492 locations->SetStackBit(stack_offset / kVRegSize);
493 }
494 }
495 }
496
497 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
498 if (register_set->ContainsFloatingPointRegister(i)) {
499 LOG(FATAL) << "Unimplemented";
500 }
501 }
502}
503
504void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
505 RegisterSet* register_set = locations->GetLiveRegisters();
506 uint32_t count = 0;
507 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
508 if (register_set->ContainsCoreRegister(i)) {
509 size_t stack_offset = GetStackOffsetOfSavedRegister(count);
510 ++count;
511 RestoreCoreRegister(Location::StackSlot(stack_offset), i);
512 }
513 }
514
515 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
516 if (register_set->ContainsFloatingPointRegister(i)) {
517 LOG(FATAL) << "Unimplemented";
518 }
519 }
520}
521
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100522void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
523 LocationSummary* locations = suspend_check->GetLocations();
524 HBasicBlock* block = suspend_check->GetBlock();
525 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
526 DCHECK(block->IsLoopHeader());
527
528 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
529 HInstruction* current = it.Current();
530 LiveInterval* interval = current->GetLiveInterval();
531 // We only need to clear bits of loop phis containing objects and allocated in register.
532 // Loop phis allocated on stack already have the object in the stack.
533 if (current->GetType() == Primitive::kPrimNot
534 && interval->HasRegister()
535 && interval->HasSpillSlot()) {
536 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
537 }
538 }
539}
540
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000541} // namespace art