blob: b8332ad2a39a7f0e515faaae97f66541038452c7 [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()
41 + GetGraph()->GetNumberOfVRegs()
42 + 1 /* filler */);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010043 GenerateFrameEntry();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010044
Nicolas Geoffray804d0932014-05-02 08:46:00 +010045 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010046 HBasicBlock* block = blocks.Get(i);
47 Bind(GetLabelOf(block));
48 HGraphVisitor* location_builder = GetLocationBuilder();
49 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
50 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
51 HInstruction* current = it.Current();
52 current->Accept(location_builder);
53 InitLocations(current);
54 current->Accept(instruction_visitor);
55 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010057
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000059 uint8_t* buffer = allocator->Allocate(code_size);
60 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000061 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000062}
63
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010064void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
65 // The frame size has already been computed during register allocation.
66 DCHECK_NE(frame_size_, kUninitializedFrameSize);
67 const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
68 DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
69 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
70 block_labels_.SetSize(blocks.Size());
71
72 GenerateFrameEntry();
73 for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
74 HBasicBlock* block = blocks.Get(i);
75 Bind(GetLabelOf(block));
76 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
77 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
78 HInstruction* current = it.Current();
79 current->Accept(instruction_visitor);
80 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000081 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010082
83 size_t code_size = GetAssembler()->CodeSize();
84 uint8_t* buffer = allocator->Allocate(code_size);
85 MemoryRegion code(buffer, code_size);
86 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000087}
88
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010089size_t CodeGenerator::AllocateFreeRegisterInternal(
90 bool* blocked_registers, size_t number_of_registers) const {
91 for (size_t regno = 0; regno < number_of_registers; regno++) {
92 if (!blocked_registers[regno]) {
93 blocked_registers[regno] = true;
94 return regno;
95 }
96 }
97 LOG(FATAL) << "Unreachable";
98 return -1;
99}
100
101
102void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
103 LocationSummary* locations = instruction->GetLocations();
104 if (locations == nullptr) return;
105
106 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
107 blocked_registers_[i] = false;
108 }
109
110 // Mark all fixed input, temp and output registers as used.
111 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
112 Location loc = locations->InAt(i);
113 if (loc.IsRegister()) {
114 // Check that a register is not specified twice in the summary.
115 DCHECK(!blocked_registers_[loc.GetEncoding()]);
116 blocked_registers_[loc.GetEncoding()] = true;
117 }
118 }
119
120 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
121 Location loc = locations->GetTemp(i);
122 if (loc.IsRegister()) {
123 // Check that a register is not specified twice in the summary.
124 DCHECK(!blocked_registers_[loc.GetEncoding()]);
125 blocked_registers_[loc.GetEncoding()] = true;
126 }
127 }
128
129 SetupBlockedRegisters(blocked_registers_);
130
131 // Allocate all unallocated input locations.
132 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
133 Location loc = locations->InAt(i);
134 HInstruction* input = instruction->InputAt(i);
135 if (loc.IsUnallocated()) {
136 if (loc.GetPolicy() == Location::kRequiresRegister) {
137 loc = Location::RegisterLocation(
138 AllocateFreeRegister(input->GetType(), blocked_registers_));
139 } else {
140 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
141 HLoadLocal* load = input->AsLoadLocal();
142 if (load != nullptr) {
143 loc = GetStackLocation(load);
144 } else {
145 loc = Location::RegisterLocation(
146 AllocateFreeRegister(input->GetType(), blocked_registers_));
147 }
148 }
149 locations->SetInAt(i, loc);
150 }
151 }
152
153 // Allocate all unallocated temp locations.
154 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
155 Location loc = locations->GetTemp(i);
156 if (loc.IsUnallocated()) {
157 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
158 // TODO: Adjust handling of temps. We currently consider temps to use
159 // core registers. They may also use floating point registers at some point.
160 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
161 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
162 locations->SetTempAt(i, loc);
163 }
164 }
165
Nicolas Geoffrayf529d772014-05-02 11:03:30 +0100166 // Make all registers available for the return value.
167 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
168 blocked_registers_[i] = false;
169 }
170 SetupBlockedRegisters(blocked_registers_);
171
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100172 Location result_location = locations->Out();
173 if (result_location.IsUnallocated()) {
174 switch (result_location.GetPolicy()) {
175 case Location::kAny:
176 case Location::kRequiresRegister:
177 result_location = Location::RegisterLocation(
178 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
179 break;
180 case Location::kSameAsFirstInput:
181 result_location = locations->InAt(0);
182 break;
183 }
184 locations->SetOut(result_location);
185 }
186}
187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000188void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100189 if (instruction->GetLocations() == nullptr) {
190 return;
191 }
192 AllocateRegistersLocally(instruction);
193 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000194 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000195 if (location.IsValid()) {
196 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100197 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000198 }
199 }
200}
201
202bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000203 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000204 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000205}
206
207Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000208 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000209}
210
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000211CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
212 HGraph* graph,
213 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000214 switch (instruction_set) {
215 case kArm:
216 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000217 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000218 }
219 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000220 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000221 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000222 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000223 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700224 case kX86_64: {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100225 return new (allocator) x86_64::CodeGeneratorX86_64(graph);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700226 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000227 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000228 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000229 }
230}
231
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000232void CodeGenerator::BuildNativeGCMap(
233 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
234 const std::vector<uint8_t>& gc_map_raw =
235 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
236 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
237
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000238 uint32_t max_native_offset = 0;
239 for (size_t i = 0; i < pc_infos_.Size(); i++) {
240 uint32_t native_offset = pc_infos_.Get(i).native_pc;
241 if (native_offset > max_native_offset) {
242 max_native_offset = native_offset;
243 }
244 }
245
246 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
247 for (size_t i = 0; i < pc_infos_.Size(); i++) {
248 struct PcInfo pc_info = pc_infos_.Get(i);
249 uint32_t native_offset = pc_info.native_pc;
250 uint32_t dex_pc = pc_info.dex_pc;
251 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
252 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
253 builder.AddEntry(native_offset, references);
254 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000255}
256
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000257void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
258 uint32_t pc2dex_data_size = 0u;
259 uint32_t pc2dex_entries = pc_infos_.Size();
260 uint32_t pc2dex_offset = 0u;
261 int32_t pc2dex_dalvik_offset = 0;
262 uint32_t dex2pc_data_size = 0u;
263 uint32_t dex2pc_entries = 0u;
264
265 // We currently only have pc2dex entries.
266 for (size_t i = 0; i < pc2dex_entries; i++) {
267 struct PcInfo pc_info = pc_infos_.Get(i);
268 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
269 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
270 pc2dex_offset = pc_info.native_pc;
271 pc2dex_dalvik_offset = pc_info.dex_pc;
272 }
273
274 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
275 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
276 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
277 data->resize(data_size);
278
279 uint8_t* data_ptr = &(*data)[0];
280 uint8_t* write_pos = data_ptr;
281 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
282 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
283 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
284 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
285
286 pc2dex_offset = 0u;
287 pc2dex_dalvik_offset = 0u;
288 for (size_t i = 0; i < pc2dex_entries; i++) {
289 struct PcInfo pc_info = pc_infos_.Get(i);
290 DCHECK(pc2dex_offset <= pc_info.native_pc);
291 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
292 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
293 pc2dex_offset = pc_info.native_pc;
294 pc2dex_dalvik_offset = pc_info.dex_pc;
295 }
296 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
297 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
298
299 if (kIsDebugBuild) {
300 // Verify the encoded table holds the expected data.
301 MappingTable table(data_ptr);
302 CHECK_EQ(table.TotalSize(), total_entries);
303 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
304 auto it = table.PcToDexBegin();
305 auto it2 = table.DexToPcBegin();
306 for (size_t i = 0; i < pc2dex_entries; i++) {
307 struct PcInfo pc_info = pc_infos_.Get(i);
308 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
309 CHECK_EQ(pc_info.dex_pc, it.DexPc());
310 ++it;
311 }
312 CHECK(it == table.PcToDexEnd());
313 CHECK(it2 == table.DexToPcEnd());
314 }
315}
316
317void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
318 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 // We currently don't use callee-saved registers.
320 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000321 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
322 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000323 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
324
325 *data = vmap_encoder.GetData();
326}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000327
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000328} // namespace art