blob: e0db0f18be67e102de222812de7acf63d03be8b4 [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"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000022#include "dex/verified_method.h"
23#include "driver/dex_compilation_unit.h"
24#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000025#include "leb128.h"
26#include "mapping_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000027#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000028#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000029#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030
31namespace art {
32
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010033void CodeGenerator::CompileBaseline(CodeAllocator* allocator) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010034 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
35 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
36 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010037 block_labels_.SetSize(blocks.Size());
38
39 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
40 ComputeFrameSize(GetGraph()->GetMaximumNumberOfOutVRegs()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +010041 + GetGraph()->GetNumberOfLocalVRegs()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010042 + GetGraph()->GetNumberOfTemporaries()
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010043 + 1 /* filler */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010044 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010045
Nicolas Geoffray804d0932014-05-02 08:46:00 +010046 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010047 HBasicBlock* block = blocks.Get(i);
48 Bind(GetLabelOf(block));
49 HGraphVisitor* location_builder = GetLocationBuilder();
50 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
51 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
52 HInstruction* current = it.Current();
53 current->Accept(location_builder);
54 InitLocations(current);
55 current->Accept(instruction_visitor);
56 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000057 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010059
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000061 uint8_t* buffer = allocator->Allocate(code_size);
62 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000063 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000064}
65
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010066void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
67 // The frame size has already been computed during register allocation.
68 DCHECK_NE(frame_size_, kUninitializedFrameSize);
69 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
70 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
71 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
72 block_labels_.SetSize(blocks.Size());
73
74 GenerateFrameEntry();
75 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
76 HBasicBlock* block = blocks.Get(i);
77 Bind(GetLabelOf(block));
78 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
79 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
80 HInstruction* current = it.Current();
81 current->Accept(instruction_visitor);
82 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000083 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010084 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010085
86 size_t code_size = GetAssembler()->CodeSize();
87 uint8_t* buffer = allocator->Allocate(code_size);
88 MemoryRegion code(buffer, code_size);
89 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090}
91
Nicolas Geoffraye5038322014-07-04 09:41:32 +010092void CodeGenerator::GenerateSlowPaths() {
93 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
94 slow_paths_.Get(i)->EmitNativeCode(this);
95 }
96}
97
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010098size_t CodeGenerator::AllocateFreeRegisterInternal(
99 bool* blocked_registers, size_t number_of_registers) const {
100 for (size_t regno = 0; regno < number_of_registers; regno++) {
101 if (!blocked_registers[regno]) {
102 blocked_registers[regno] = true;
103 return regno;
104 }
105 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100106 return -1;
107}
108
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100109void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots) {
110 SetFrameSize(RoundUp(
111 number_of_spill_slots * kVRegSize
112 + kVRegSize // Art method
113 + FrameEntrySpillSize(),
114 kStackAlignment));
115}
116
117Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
118 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
119 // Use the temporary region (right below the dex registers).
120 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
121 - kVRegSize // filler
122 - (number_of_locals * kVRegSize)
123 - ((1 + temp->GetIndex()) * kVRegSize);
124 return Location::StackSlot(slot);
125}
126
127int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
128 uint16_t reg_number = local->GetRegNumber();
129 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
130 if (reg_number >= number_of_locals) {
131 // Local is a parameter of the method. It is stored in the caller's frame.
132 return GetFrameSize() + kVRegSize // ART method
133 + (reg_number - number_of_locals) * kVRegSize;
134 } else {
135 // Local is a temporary in this method. It is stored in this method's frame.
136 return GetFrameSize() - FrameEntrySpillSize()
137 - kVRegSize // filler.
138 - (number_of_locals * kVRegSize)
139 + (reg_number * kVRegSize);
140 }
141}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100142
143void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
144 LocationSummary* locations = instruction->GetLocations();
145 if (locations == nullptr) return;
146
147 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
148 blocked_registers_[i] = false;
149 }
150
151 // Mark all fixed input, temp and output registers as used.
152 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
153 Location loc = locations->InAt(i);
154 if (loc.IsRegister()) {
155 // Check that a register is not specified twice in the summary.
156 DCHECK(!blocked_registers_[loc.GetEncoding()]);
157 blocked_registers_[loc.GetEncoding()] = true;
158 }
159 }
160
161 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
162 Location loc = locations->GetTemp(i);
163 if (loc.IsRegister()) {
164 // Check that a register is not specified twice in the summary.
165 DCHECK(!blocked_registers_[loc.GetEncoding()]);
166 blocked_registers_[loc.GetEncoding()] = true;
167 }
168 }
169
170 SetupBlockedRegisters(blocked_registers_);
171
172 // Allocate all unallocated input locations.
173 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
174 Location loc = locations->InAt(i);
175 HInstruction* input = instruction->InputAt(i);
176 if (loc.IsUnallocated()) {
177 if (loc.GetPolicy() == Location::kRequiresRegister) {
178 loc = Location::RegisterLocation(
179 AllocateFreeRegister(input->GetType(), blocked_registers_));
180 } else {
181 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
182 HLoadLocal* load = input->AsLoadLocal();
183 if (load != nullptr) {
184 loc = GetStackLocation(load);
185 } else {
186 loc = Location::RegisterLocation(
187 AllocateFreeRegister(input->GetType(), blocked_registers_));
188 }
189 }
190 locations->SetInAt(i, loc);
191 }
192 }
193
194 // Allocate all unallocated temp locations.
195 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
196 Location loc = locations->GetTemp(i);
197 if (loc.IsUnallocated()) {
198 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
199 // TODO: Adjust handling of temps. We currently consider temps to use
200 // core registers. They may also use floating point registers at some point.
201 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
202 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
203 locations->SetTempAt(i, loc);
204 }
205 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100206 Location result_location = locations->Out();
207 if (result_location.IsUnallocated()) {
208 switch (result_location.GetPolicy()) {
209 case Location::kAny:
210 case Location::kRequiresRegister:
211 result_location = Location::RegisterLocation(
212 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
213 break;
214 case Location::kSameAsFirstInput:
215 result_location = locations->InAt(0);
216 break;
217 }
218 locations->SetOut(result_location);
219 }
220}
221
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000222void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100223 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100224 if (instruction->IsTemporary()) {
225 HInstruction* previous = instruction->GetPrevious();
226 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
227 Move(previous, temp_location, instruction);
228 previous->GetLocations()->SetOut(temp_location);
229 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100230 return;
231 }
232 AllocateRegistersLocally(instruction);
233 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000234 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000235 if (location.IsValid()) {
236 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100237 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000238 }
239 }
240}
241
242bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000243 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000244 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000245}
246
247Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000248 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000249}
250
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000251CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
252 HGraph* graph,
253 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000254 switch (instruction_set) {
255 case kArm:
256 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000257 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000258 }
259 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000260 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000261 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000262 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000263 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700264 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100265 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700266 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000267 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000268 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000269 }
270}
271
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000272void CodeGenerator::BuildNativeGCMap(
273 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
274 const std::vector<uint8_t>& gc_map_raw =
275 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
276 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
277
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000278 uint32_t max_native_offset = 0;
279 for (size_t i = 0; i < pc_infos_.Size(); i++) {
280 uint32_t native_offset = pc_infos_.Get(i).native_pc;
281 if (native_offset > max_native_offset) {
282 max_native_offset = native_offset;
283 }
284 }
285
286 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
287 for (size_t i = 0; i < pc_infos_.Size(); i++) {
288 struct PcInfo pc_info = pc_infos_.Get(i);
289 uint32_t native_offset = pc_info.native_pc;
290 uint32_t dex_pc = pc_info.dex_pc;
291 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
292 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
293 builder.AddEntry(native_offset, references);
294 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000295}
296
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000297void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
298 uint32_t pc2dex_data_size = 0u;
299 uint32_t pc2dex_entries = pc_infos_.Size();
300 uint32_t pc2dex_offset = 0u;
301 int32_t pc2dex_dalvik_offset = 0;
302 uint32_t dex2pc_data_size = 0u;
303 uint32_t dex2pc_entries = 0u;
304
305 // We currently only have pc2dex entries.
306 for (size_t i = 0; i < pc2dex_entries; i++) {
307 struct PcInfo pc_info = pc_infos_.Get(i);
308 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
309 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
310 pc2dex_offset = pc_info.native_pc;
311 pc2dex_dalvik_offset = pc_info.dex_pc;
312 }
313
314 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
315 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
316 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
317 data->resize(data_size);
318
319 uint8_t* data_ptr = &(*data)[0];
320 uint8_t* write_pos = data_ptr;
321 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
322 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
323 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
324 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
325
326 pc2dex_offset = 0u;
327 pc2dex_dalvik_offset = 0u;
328 for (size_t i = 0; i < pc2dex_entries; i++) {
329 struct PcInfo pc_info = pc_infos_.Get(i);
330 DCHECK(pc2dex_offset <= pc_info.native_pc);
331 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
332 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
333 pc2dex_offset = pc_info.native_pc;
334 pc2dex_dalvik_offset = pc_info.dex_pc;
335 }
336 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
337 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
338
339 if (kIsDebugBuild) {
340 // Verify the encoded table holds the expected data.
341 MappingTable table(data_ptr);
342 CHECK_EQ(table.TotalSize(), total_entries);
343 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
344 auto it = table.PcToDexBegin();
345 auto it2 = table.DexToPcBegin();
346 for (size_t i = 0; i < pc2dex_entries; i++) {
347 struct PcInfo pc_info = pc_infos_.Get(i);
348 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
349 CHECK_EQ(pc_info.dex_pc, it.DexPc());
350 ++it;
351 }
352 CHECK(it == table.PcToDexEnd());
353 CHECK(it2 == table.DexToPcEnd());
354 }
355}
356
357void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
358 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100359 // We currently don't use callee-saved registers.
360 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000361 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
362 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000363 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
364
365 *data = vmap_encoder.GetData();
366}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000367
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000368} // namespace art