blob: 7269fff62c6de78d51af972578a8c9709de642b1 [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 Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000029#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031
32namespace art {
33
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010034void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010035 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
36 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
37 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010038 block_labels_.SetSize(blocks.Size());
39
40 DCHECK_EQ(frame_size_, kUninitializedFrameSize);
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010041 if (!is_leaf) {
42 MarkNotLeaf();
43 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010044 ComputeFrameSize(GetGraph()->GetMaximumNumberOfOutVRegs()
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +010045 + GetGraph()->GetNumberOfLocalVRegs()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046 + GetGraph()->GetNumberOfTemporaries()
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010047 + 1 /* filler */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010048 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010049
Nicolas Geoffray804d0932014-05-02 08:46:00 +010050 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010051 HBasicBlock* block = blocks.Get(i);
52 Bind(GetLabelOf(block));
53 HGraphVisitor* location_builder = GetLocationBuilder();
54 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
55 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
56 HInstruction* current = it.Current();
57 current->Accept(location_builder);
58 InitLocations(current);
59 current->Accept(instruction_visitor);
60 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000061 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010062 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010063
Nicolas Geoffray787c3072014-03-17 10:20:19 +000064 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000065 uint8_t* buffer = allocator->Allocate(code_size);
66 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000068}
69
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010070void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
71 // The frame size has already been computed during register allocation.
72 DCHECK_NE(frame_size_, kUninitializedFrameSize);
73 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
74 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
75 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
76 block_labels_.SetSize(blocks.Size());
77
78 GenerateFrameEntry();
79 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
80 HBasicBlock* block = blocks.Get(i);
81 Bind(GetLabelOf(block));
82 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
83 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
84 HInstruction* current = it.Current();
85 current->Accept(instruction_visitor);
86 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000087 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 GenerateSlowPaths();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010089
90 size_t code_size = GetAssembler()->CodeSize();
91 uint8_t* buffer = allocator->Allocate(code_size);
92 MemoryRegion code(buffer, code_size);
93 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094}
95
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096void CodeGenerator::GenerateSlowPaths() {
97 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
98 slow_paths_.Get(i)->EmitNativeCode(this);
99 }
100}
101
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100102size_t CodeGenerator::AllocateFreeRegisterInternal(
103 bool* blocked_registers, size_t number_of_registers) const {
104 for (size_t regno = 0; regno < number_of_registers; regno++) {
105 if (!blocked_registers[regno]) {
106 blocked_registers[regno] = true;
107 return regno;
108 }
109 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100110 return -1;
111}
112
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100113void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots) {
114 SetFrameSize(RoundUp(
115 number_of_spill_slots * kVRegSize
116 + kVRegSize // Art method
117 + FrameEntrySpillSize(),
118 kStackAlignment));
119}
120
121Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
122 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
123 // Use the temporary region (right below the dex registers).
124 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
125 - kVRegSize // filler
126 - (number_of_locals * kVRegSize)
127 - ((1 + temp->GetIndex()) * kVRegSize);
128 return Location::StackSlot(slot);
129}
130
131int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
132 uint16_t reg_number = local->GetRegNumber();
133 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
134 if (reg_number >= number_of_locals) {
135 // Local is a parameter of the method. It is stored in the caller's frame.
136 return GetFrameSize() + kVRegSize // ART method
137 + (reg_number - number_of_locals) * kVRegSize;
138 } else {
139 // Local is a temporary in this method. It is stored in this method's frame.
140 return GetFrameSize() - FrameEntrySpillSize()
141 - kVRegSize // filler.
142 - (number_of_locals * kVRegSize)
143 + (reg_number * kVRegSize);
144 }
145}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100146
147void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
148 LocationSummary* locations = instruction->GetLocations();
149 if (locations == nullptr) return;
150
151 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
152 blocked_registers_[i] = false;
153 }
154
155 // Mark all fixed input, temp and output registers as used.
156 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
157 Location loc = locations->InAt(i);
158 if (loc.IsRegister()) {
159 // Check that a register is not specified twice in the summary.
160 DCHECK(!blocked_registers_[loc.GetEncoding()]);
161 blocked_registers_[loc.GetEncoding()] = true;
162 }
163 }
164
165 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
166 Location loc = locations->GetTemp(i);
167 if (loc.IsRegister()) {
168 // Check that a register is not specified twice in the summary.
169 DCHECK(!blocked_registers_[loc.GetEncoding()]);
170 blocked_registers_[loc.GetEncoding()] = true;
171 }
172 }
173
174 SetupBlockedRegisters(blocked_registers_);
175
176 // Allocate all unallocated input locations.
177 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
178 Location loc = locations->InAt(i);
179 HInstruction* input = instruction->InputAt(i);
180 if (loc.IsUnallocated()) {
181 if (loc.GetPolicy() == Location::kRequiresRegister) {
182 loc = Location::RegisterLocation(
183 AllocateFreeRegister(input->GetType(), blocked_registers_));
184 } else {
185 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
186 HLoadLocal* load = input->AsLoadLocal();
187 if (load != nullptr) {
188 loc = GetStackLocation(load);
189 } else {
190 loc = Location::RegisterLocation(
191 AllocateFreeRegister(input->GetType(), blocked_registers_));
192 }
193 }
194 locations->SetInAt(i, loc);
195 }
196 }
197
198 // Allocate all unallocated temp locations.
199 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
200 Location loc = locations->GetTemp(i);
201 if (loc.IsUnallocated()) {
202 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
203 // TODO: Adjust handling of temps. We currently consider temps to use
204 // core registers. They may also use floating point registers at some point.
205 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
206 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
207 locations->SetTempAt(i, loc);
208 }
209 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100210 Location result_location = locations->Out();
211 if (result_location.IsUnallocated()) {
212 switch (result_location.GetPolicy()) {
213 case Location::kAny:
214 case Location::kRequiresRegister:
215 result_location = Location::RegisterLocation(
216 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
217 break;
218 case Location::kSameAsFirstInput:
219 result_location = locations->InAt(0);
220 break;
221 }
222 locations->SetOut(result_location);
223 }
224}
225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000226void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100227 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100228 if (instruction->IsTemporary()) {
229 HInstruction* previous = instruction->GetPrevious();
230 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
231 Move(previous, temp_location, instruction);
232 previous->GetLocations()->SetOut(temp_location);
233 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100234 return;
235 }
236 AllocateRegistersLocally(instruction);
237 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000238 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000239 if (location.IsValid()) {
240 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100241 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000242 }
243 }
244}
245
246bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000248 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000249}
250
251Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000252 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000253}
254
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000255CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
256 HGraph* graph,
257 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000258 switch (instruction_set) {
259 case kArm:
260 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000261 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000262 }
263 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000264 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000265 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000266 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000267 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700268 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100269 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700270 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000271 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000273 }
274}
275
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000276void CodeGenerator::BuildNativeGCMap(
277 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
278 const std::vector<uint8_t>& gc_map_raw =
279 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
280 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
281
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000282 uint32_t max_native_offset = 0;
283 for (size_t i = 0; i < pc_infos_.Size(); i++) {
284 uint32_t native_offset = pc_infos_.Get(i).native_pc;
285 if (native_offset > max_native_offset) {
286 max_native_offset = native_offset;
287 }
288 }
289
290 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
291 for (size_t i = 0; i < pc_infos_.Size(); i++) {
292 struct PcInfo pc_info = pc_infos_.Get(i);
293 uint32_t native_offset = pc_info.native_pc;
294 uint32_t dex_pc = pc_info.dex_pc;
295 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
296 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
297 builder.AddEntry(native_offset, references);
298 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000299}
300
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700301void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000302 uint32_t pc2dex_data_size = 0u;
303 uint32_t pc2dex_entries = pc_infos_.Size();
304 uint32_t pc2dex_offset = 0u;
305 int32_t pc2dex_dalvik_offset = 0;
306 uint32_t dex2pc_data_size = 0u;
307 uint32_t dex2pc_entries = 0u;
308
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700309 if (src_map != nullptr) {
310 src_map->reserve(pc2dex_entries);
311 }
312
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000313 // We currently only have pc2dex entries.
314 for (size_t i = 0; i < pc2dex_entries; i++) {
315 struct PcInfo pc_info = pc_infos_.Get(i);
316 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
317 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
318 pc2dex_offset = pc_info.native_pc;
319 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700320 if (src_map != nullptr) {
321 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
322 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000323 }
324
325 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
326 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
327 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
328 data->resize(data_size);
329
330 uint8_t* data_ptr = &(*data)[0];
331 uint8_t* write_pos = data_ptr;
332 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
333 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
334 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
335 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
336
337 pc2dex_offset = 0u;
338 pc2dex_dalvik_offset = 0u;
339 for (size_t i = 0; i < pc2dex_entries; i++) {
340 struct PcInfo pc_info = pc_infos_.Get(i);
341 DCHECK(pc2dex_offset <= pc_info.native_pc);
342 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
343 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
344 pc2dex_offset = pc_info.native_pc;
345 pc2dex_dalvik_offset = pc_info.dex_pc;
346 }
347 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
348 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
349
350 if (kIsDebugBuild) {
351 // Verify the encoded table holds the expected data.
352 MappingTable table(data_ptr);
353 CHECK_EQ(table.TotalSize(), total_entries);
354 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
355 auto it = table.PcToDexBegin();
356 auto it2 = table.DexToPcBegin();
357 for (size_t i = 0; i < pc2dex_entries; i++) {
358 struct PcInfo pc_info = pc_infos_.Get(i);
359 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
360 CHECK_EQ(pc_info.dex_pc, it.DexPc());
361 ++it;
362 }
363 CHECK(it == table.PcToDexEnd());
364 CHECK(it2 == table.DexToPcEnd());
365 }
366}
367
368void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
369 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100370 // We currently don't use callee-saved registers.
371 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000372 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
373 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000374 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
375
376 *data = vmap_encoder.GetData();
377}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000378
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000379} // namespace art