blob: ff316e5b048e09c75dbe646e363433cb744f8ed4 [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 Geoffray92cf83e2014-03-18 17:59:20 +000021#include "dex/verified_method.h"
22#include "driver/dex_compilation_unit.h"
23#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000024#include "leb128.h"
25#include "mapping_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000026#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000027#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000028#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000029
30namespace art {
31
32void CodeGenerator::Compile(CodeAllocator* allocator) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +000033 const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks();
34 DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock());
35 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1)));
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010036 GenerateFrameEntry();
37 for (size_t i = 0; i < blocks->Size(); i++) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038 CompileBlock(blocks->Get(i));
39 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040 size_t code_size = GetAssembler()->CodeSize();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041 uint8_t* buffer = allocator->Allocate(code_size);
42 MemoryRegion code(buffer, code_size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000043 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044}
45
46void CodeGenerator::CompileBlock(HBasicBlock* block) {
47 Bind(GetLabelOf(block));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000048 HGraphVisitor* location_builder = GetLocationBuilder();
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050 for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000051 HInstruction* current = it.Current();
52 current->Accept(location_builder);
53 InitLocations(current);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000054 current->Accept(instruction_visitor);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000055 }
56}
57
Nicolas Geoffraya7aca372014-04-28 17:47:12 +010058size_t CodeGenerator::AllocateFreeRegisterInternal(
59 bool* blocked_registers, size_t number_of_registers) const {
60 for (size_t regno = 0; regno < number_of_registers; regno++) {
61 if (!blocked_registers[regno]) {
62 blocked_registers[regno] = true;
63 return regno;
64 }
65 }
66 LOG(FATAL) << "Unreachable";
67 return -1;
68}
69
70
71void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
72 LocationSummary* locations = instruction->GetLocations();
73 if (locations == nullptr) return;
74
75 for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) {
76 blocked_registers_[i] = false;
77 }
78
79 // Mark all fixed input, temp and output registers as used.
80 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
81 Location loc = locations->InAt(i);
82 if (loc.IsRegister()) {
83 // Check that a register is not specified twice in the summary.
84 DCHECK(!blocked_registers_[loc.GetEncoding()]);
85 blocked_registers_[loc.GetEncoding()] = true;
86 }
87 }
88
89 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
90 Location loc = locations->GetTemp(i);
91 if (loc.IsRegister()) {
92 // Check that a register is not specified twice in the summary.
93 DCHECK(!blocked_registers_[loc.GetEncoding()]);
94 blocked_registers_[loc.GetEncoding()] = true;
95 }
96 }
97
98 SetupBlockedRegisters(blocked_registers_);
99
100 // Allocate all unallocated input locations.
101 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
102 Location loc = locations->InAt(i);
103 HInstruction* input = instruction->InputAt(i);
104 if (loc.IsUnallocated()) {
105 if (loc.GetPolicy() == Location::kRequiresRegister) {
106 loc = Location::RegisterLocation(
107 AllocateFreeRegister(input->GetType(), blocked_registers_));
108 } else {
109 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
110 HLoadLocal* load = input->AsLoadLocal();
111 if (load != nullptr) {
112 loc = GetStackLocation(load);
113 } else {
114 loc = Location::RegisterLocation(
115 AllocateFreeRegister(input->GetType(), blocked_registers_));
116 }
117 }
118 locations->SetInAt(i, loc);
119 }
120 }
121
122 // Allocate all unallocated temp locations.
123 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
124 Location loc = locations->GetTemp(i);
125 if (loc.IsUnallocated()) {
126 DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister);
127 // TODO: Adjust handling of temps. We currently consider temps to use
128 // core registers. They may also use floating point registers at some point.
129 loc = Location::RegisterLocation(static_cast<ManagedRegister>(
130 AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_)));
131 locations->SetTempAt(i, loc);
132 }
133 }
134
135 Location result_location = locations->Out();
136 if (result_location.IsUnallocated()) {
137 switch (result_location.GetPolicy()) {
138 case Location::kAny:
139 case Location::kRequiresRegister:
140 result_location = Location::RegisterLocation(
141 AllocateFreeRegister(instruction->GetType(), blocked_registers_));
142 break;
143 case Location::kSameAsFirstInput:
144 result_location = locations->InAt(0);
145 break;
146 }
147 locations->SetOut(result_location);
148 }
149}
150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000151void CodeGenerator::InitLocations(HInstruction* instruction) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100152 if (instruction->GetLocations() == nullptr) {
153 return;
154 }
155 AllocateRegistersLocally(instruction);
156 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000157 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000158 if (location.IsValid()) {
159 // Move the input to the desired location.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100160 Move(instruction->InputAt(i), location, instruction);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000161 }
162 }
163}
164
165bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000166 // We currently iterate over the block in insertion order.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 return current->GetBlockId() + 1 == next->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000168}
169
170Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000171 return block_labels_.GetRawStorage() + block->GetBlockId();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000172}
173
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000174CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator,
175 HGraph* graph,
176 InstructionSet instruction_set) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000177 switch (instruction_set) {
178 case kArm:
179 case kThumb2: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180 return new (allocator) arm::CodeGeneratorARM(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 }
182 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000183 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000184 case kX86: {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000185 return new (allocator) x86::CodeGeneratorX86(graph);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000186 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700187 case kX86_64: {
188 return new (allocator) x86::CodeGeneratorX86(graph);
189 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000190 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000191 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000192 }
193}
194
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000195void CodeGenerator::BuildNativeGCMap(
196 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
197 const std::vector<uint8_t>& gc_map_raw =
198 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
199 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
200
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000201 uint32_t max_native_offset = 0;
202 for (size_t i = 0; i < pc_infos_.Size(); i++) {
203 uint32_t native_offset = pc_infos_.Get(i).native_pc;
204 if (native_offset > max_native_offset) {
205 max_native_offset = native_offset;
206 }
207 }
208
209 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
210 for (size_t i = 0; i < pc_infos_.Size(); i++) {
211 struct PcInfo pc_info = pc_infos_.Get(i);
212 uint32_t native_offset = pc_info.native_pc;
213 uint32_t dex_pc = pc_info.dex_pc;
214 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
215 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
216 builder.AddEntry(native_offset, references);
217 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000218}
219
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000220void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
221 uint32_t pc2dex_data_size = 0u;
222 uint32_t pc2dex_entries = pc_infos_.Size();
223 uint32_t pc2dex_offset = 0u;
224 int32_t pc2dex_dalvik_offset = 0;
225 uint32_t dex2pc_data_size = 0u;
226 uint32_t dex2pc_entries = 0u;
227
228 // We currently only have pc2dex entries.
229 for (size_t i = 0; i < pc2dex_entries; i++) {
230 struct PcInfo pc_info = pc_infos_.Get(i);
231 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
232 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
233 pc2dex_offset = pc_info.native_pc;
234 pc2dex_dalvik_offset = pc_info.dex_pc;
235 }
236
237 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
238 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
239 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
240 data->resize(data_size);
241
242 uint8_t* data_ptr = &(*data)[0];
243 uint8_t* write_pos = data_ptr;
244 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
245 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
246 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
247 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
248
249 pc2dex_offset = 0u;
250 pc2dex_dalvik_offset = 0u;
251 for (size_t i = 0; i < pc2dex_entries; i++) {
252 struct PcInfo pc_info = pc_infos_.Get(i);
253 DCHECK(pc2dex_offset <= pc_info.native_pc);
254 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
255 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
256 pc2dex_offset = pc_info.native_pc;
257 pc2dex_dalvik_offset = pc_info.dex_pc;
258 }
259 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
260 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
261
262 if (kIsDebugBuild) {
263 // Verify the encoded table holds the expected data.
264 MappingTable table(data_ptr);
265 CHECK_EQ(table.TotalSize(), total_entries);
266 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
267 auto it = table.PcToDexBegin();
268 auto it2 = table.DexToPcBegin();
269 for (size_t i = 0; i < pc2dex_entries; i++) {
270 struct PcInfo pc_info = pc_infos_.Get(i);
271 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
272 CHECK_EQ(pc_info.dex_pc, it.DexPc());
273 ++it;
274 }
275 CHECK(it == table.PcToDexEnd());
276 CHECK(it2 == table.DexToPcEnd());
277 }
278}
279
280void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
281 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100282 // We currently don't use callee-saved registers.
283 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000284 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
285 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000286 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
287
288 *data = vmap_encoder.GetData();
289}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000290
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000291} // namespace art