blob: ec0fb43571fc02325e349cfc51242c7626321dbc [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 "dex/compiler_internals.h"
18#include "dex_file-inl.h"
19#include "gc_map.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000020#include "gc_map_builder.h"
Ian Rogers96faf5b2013-08-09 22:05:32 -070021#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mir_to_lir-inl.h"
Vladimir Marko5816ed42013-11-27 17:04:20 +000023#include "dex/quick/dex_file_method_inliner.h"
24#include "dex/quick/dex_file_to_method_inliner_map.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000025#include "dex/verification_results.h"
Vladimir Marko2730db02014-01-27 11:15:17 +000026#include "dex/verified_method.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "verifier/dex_gc_map.h"
28#include "verifier/method_verifier.h"
Vladimir Marko2e589aa2014-02-25 17:53:53 +000029#include "vmap_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030
31namespace art {
32
Vladimir Marko06606b92013-12-02 15:31:08 +000033namespace {
34
35/* Dump a mapping table */
36template <typename It>
37void DumpMappingTable(const char* table_name, const char* descriptor, const char* name,
38 const Signature& signature, uint32_t size, It first) {
39 if (size != 0) {
Ian Rogers107c31e2014-01-23 20:55:29 -080040 std::string line(StringPrintf("\n %s %s%s_%s_table[%u] = {", table_name,
Vladimir Marko06606b92013-12-02 15:31:08 +000041 descriptor, name, signature.ToString().c_str(), size));
42 std::replace(line.begin(), line.end(), ';', '_');
43 LOG(INFO) << line;
44 for (uint32_t i = 0; i != size; ++i) {
45 line = StringPrintf(" {0x%05x, 0x%04x},", first.NativePcOffset(), first.DexPc());
46 ++first;
47 LOG(INFO) << line;
48 }
49 LOG(INFO) <<" };\n\n";
50 }
51}
52
53} // anonymous namespace
54
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070055bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 bool res = false;
57 if (rl_src.is_const) {
58 if (rl_src.wide) {
59 if (rl_src.fp) {
60 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
61 } else {
62 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
63 }
64 } else {
65 if (rl_src.fp) {
66 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
67 } else {
68 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
69 }
70 }
71 }
72 return res;
73}
74
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070075void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070076 DCHECK(!inst->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010077 inst->u.m.def_mask = &kEncodeAll;
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
Vladimir Marko8dea81c2014-06-06 14:50:36 +010079 DCHECK(safepoint_pc->u.m.def_mask->Equals(kEncodeAll));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080}
81
buzbee252254b2013-09-08 16:20:53 -070082/* Remove a LIR from the list. */
83void Mir2Lir::UnlinkLIR(LIR* lir) {
84 if (UNLIKELY(lir == first_lir_insn_)) {
85 first_lir_insn_ = lir->next;
86 if (lir->next != NULL) {
87 lir->next->prev = NULL;
88 } else {
89 DCHECK(lir->next == NULL);
90 DCHECK(lir == last_lir_insn_);
91 last_lir_insn_ = NULL;
92 }
93 } else if (lir == last_lir_insn_) {
94 last_lir_insn_ = lir->prev;
95 lir->prev->next = NULL;
96 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
97 lir->prev->next = lir->next;
98 lir->next->prev = lir->prev;
99 }
100}
101
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -0700103void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -0700105 if (!cu_->verbose) {
106 UnlinkLIR(lir);
107 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108}
109
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700110void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -0700112 DCHECK(!lir->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100113 // TODO: Avoid the extra Arena allocation!
114 const ResourceMask** mask_ptr;
115 ResourceMask mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -0700117 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 } else {
buzbeeb48819d2013-09-14 16:15:25 -0700119 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100121 mask = **mask_ptr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 /* Clear out the memref flags */
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100123 mask.ClearBits(kEncodeMem);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 /* ..and then add back the one we need */
125 switch (mem_type) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100126 case ResourceMask::kLiteral:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127 DCHECK(is_load);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100128 mask.SetBit(ResourceMask::kLiteral);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100130 case ResourceMask::kDalvikReg:
131 mask.SetBit(ResourceMask::kDalvikReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100133 case ResourceMask::kHeapRef:
134 mask.SetBit(ResourceMask::kHeapRef);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 break;
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100136 case ResourceMask::kMustNotAlias:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 /* Currently only loads can be marked as kMustNotAlias */
138 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100139 mask.SetBit(ResourceMask::kMustNotAlias);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 break;
141 default:
142 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
143 }
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100144 *mask_ptr = mask_cache_.GetMask(mask);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145}
146
147/*
148 * Mark load/store instructions that access Dalvik registers through the stack.
149 */
150void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700151 bool is64bit) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100152 DCHECK((is_load ? lir->u.m.use_mask : lir->u.m.def_mask)->Intersection(kEncodeMem).Equals(
153 kEncodeDalvikReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154
155 /*
156 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
157 * access.
158 */
buzbeeb48819d2013-09-14 16:15:25 -0700159 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160}
161
162/*
163 * Debugging macros
164 */
165#define DUMP_RESOURCE_MASK(X)
166
167/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700168void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 int offset = lir->offset;
170 int dest = lir->operands[0];
171 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
172
173 /* Handle pseudo-ops individually, and all regular insns as a group */
174 switch (lir->opcode) {
175 case kPseudoMethodEntry:
176 LOG(INFO) << "-------- method entry "
177 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
178 break;
179 case kPseudoMethodExit:
180 LOG(INFO) << "-------- Method_Exit";
181 break;
182 case kPseudoBarrier:
183 LOG(INFO) << "-------- BARRIER";
184 break;
185 case kPseudoEntryBlock:
186 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
187 break;
188 case kPseudoDalvikByteCodeBoundary:
189 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700190 // NOTE: only used for debug listings.
191 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 }
193 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000194 << lir->dalvik_offset << " @ "
195 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 break;
197 case kPseudoExitBlock:
198 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
199 break;
200 case kPseudoPseudoAlign4:
201 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
202 << offset << "): .align4";
203 break;
204 case kPseudoEHBlockLabel:
205 LOG(INFO) << "Exception_Handling:";
206 break;
207 case kPseudoTargetLabel:
208 case kPseudoNormalBlockLabel:
209 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
210 break;
211 case kPseudoThrowTarget:
212 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
213 break;
214 case kPseudoIntrinsicRetry:
215 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
216 break;
217 case kPseudoSuspendTarget:
218 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
219 break;
220 case kPseudoSafepointPC:
221 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
222 break;
223 case kPseudoExportedPC:
224 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
225 break;
226 case kPseudoCaseLabel:
227 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
228 << std::hex << lir->operands[0] << "|" << std::dec <<
229 lir->operands[0];
230 break;
231 default:
232 if (lir->flags.is_nop && !dump_nop) {
233 break;
234 } else {
235 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
236 lir, base_addr));
237 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
238 lir, base_addr));
Ian Rogers107c31e2014-01-23 20:55:29 -0800239 LOG(INFO) << StringPrintf("%5p: %-9s%s%s",
240 base_addr + offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 op_name.c_str(), op_operands.c_str(),
242 lir->flags.is_nop ? "(nop)" : "");
243 }
244 break;
245 }
246
buzbeeb48819d2013-09-14 16:15:25 -0700247 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100248 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 }
buzbeeb48819d2013-09-14 16:15:25 -0700250 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100251 DUMP_RESOURCE_MASK(DumpResourceMask(lir, *lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 }
253}
254
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700255void Mir2Lir::DumpPromotionMap() {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800256 int num_regs = cu_->num_dalvik_registers + mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 for (int i = 0; i < num_regs; i++) {
258 PromotionMap v_reg_map = promotion_map_[i];
259 std::string buf;
260 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee091cc402014-03-31 10:14:40 -0700261 StringAppendF(&buf, " : s%d", RegStorage::RegNum(v_reg_map.FpReg));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 }
263
264 std::string buf3;
265 if (i < cu_->num_dalvik_registers) {
266 StringAppendF(&buf3, "%02d", i);
267 } else if (i == mir_graph_->GetMethodSReg()) {
268 buf3 = "Method*";
269 } else {
270 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
271 }
272
273 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
274 v_reg_map.core_location == kLocPhysReg ?
275 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
276 v_reg_map.core_reg : SRegOffset(i),
277 buf.c_str());
278 }
279}
280
buzbee7a11ab02014-04-28 20:02:38 -0700281void Mir2Lir::UpdateLIROffsets() {
282 // Only used for code listings.
283 size_t offset = 0;
284 for (LIR* lir = first_lir_insn_; lir != nullptr; lir = lir->next) {
285 lir->offset = offset;
286 if (!lir->flags.is_nop && !IsPseudoLirOp(lir->opcode)) {
287 offset += GetInsnSize(lir);
288 } else if (lir->opcode == kPseudoPseudoAlign4) {
289 offset += (offset & 0x2);
290 }
291 }
292}
293
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700295void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 LOG(INFO) << "Dumping LIR insns for "
297 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
298 LIR* lir_insn;
299 int insns_size = cu_->code_item->insns_size_in_code_units_;
300
301 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
302 LOG(INFO) << "Ins : " << cu_->num_ins;
303 LOG(INFO) << "Outs : " << cu_->num_outs;
304 LOG(INFO) << "CoreSpills : " << num_core_spills_;
305 LOG(INFO) << "FPSpills : " << num_fp_spills_;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800306 LOG(INFO) << "CompilerTemps : " << mir_graph_->GetNumUsedCompilerTemps();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 LOG(INFO) << "Frame size : " << frame_size_;
308 LOG(INFO) << "code size is " << total_size_ <<
309 " bytes, Dalvik size is " << insns_size * 2;
310 LOG(INFO) << "expansion factor: "
311 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
312 DumpPromotionMap();
buzbee7a11ab02014-04-28 20:02:38 -0700313 UpdateLIROffsets();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
315 DumpLIRInsn(lir_insn, 0);
316 }
317 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
318 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
319 lir_insn->operands[0]);
320 }
321
322 const DexFile::MethodId& method_id =
323 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700324 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
325 const char* name = cu_->dex_file->GetMethodName(method_id);
326 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327
328 // Dump mapping tables
Vladimir Marko06606b92013-12-02 15:31:08 +0000329 if (!encoded_mapping_table_.empty()) {
330 MappingTable table(&encoded_mapping_table_[0]);
331 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature,
332 table.PcToDexSize(), table.PcToDexBegin());
333 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature,
334 table.DexToPcSize(), table.DexToPcBegin());
335 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336}
337
338/*
339 * Search the existing constants in the literal pool for an exact or close match
340 * within specified delta (greater or equal to 0).
341 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700342LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 while (data_target) {
344 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
345 return data_target;
346 data_target = data_target->next;
347 }
348 return NULL;
349}
350
351/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700352LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 bool lo_match = false;
354 LIR* lo_target = NULL;
355 while (data_target) {
356 if (lo_match && (data_target->operands[0] == val_hi)) {
357 // Record high word in case we need to expand this later.
358 lo_target->operands[1] = val_hi;
359 return lo_target;
360 }
361 lo_match = false;
362 if (data_target->operands[0] == val_lo) {
363 lo_match = true;
364 lo_target = data_target;
365 }
366 data_target = data_target->next;
367 }
368 return NULL;
369}
370
Vladimir Markoa51a0b02014-05-21 12:08:39 +0100371/* Search the existing constants in the literal pool for an exact method match */
372LIR* Mir2Lir::ScanLiteralPoolMethod(LIR* data_target, const MethodReference& method) {
373 while (data_target) {
374 if (static_cast<uint32_t>(data_target->operands[0]) == method.dex_method_index &&
375 UnwrapPointer(data_target->operands[1]) == method.dex_file) {
376 return data_target;
377 }
378 data_target = data_target->next;
379 }
380 return nullptr;
381}
382
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383/*
384 * The following are building blocks to insert constants into the pool or
385 * instruction streams.
386 */
387
388/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700389LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 /* Add the constant to the literal pool */
391 if (constant_list_p) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000392 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 new_value->operands[0] = value;
394 new_value->next = *constant_list_p;
395 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700396 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 return new_value;
398 }
399 return NULL;
400}
401
402/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700403LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 AddWordData(constant_list_p, val_hi);
405 return AddWordData(constant_list_p, val_lo);
406}
407
Andreas Gampe2da88232014-02-27 12:26:20 -0800408static void Push32(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700409 buf.push_back(data & 0xff);
410 buf.push_back((data >> 8) & 0xff);
411 buf.push_back((data >> 16) & 0xff);
412 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413}
414
Andreas Gampe2da88232014-02-27 12:26:20 -0800415// Push 8 bytes on 64-bit target systems; 4 on 32-bit target systems.
416static void PushPointer(std::vector<uint8_t>&buf, const void* pointer, bool target64) {
417 uint64_t data = reinterpret_cast<uintptr_t>(pointer);
418 if (target64) {
419 Push32(buf, data & 0xFFFFFFFF);
420 Push32(buf, (data >> 32) & 0xFFFFFFFF);
buzbee0d829482013-10-11 15:24:55 -0700421 } else {
Andreas Gampe2da88232014-02-27 12:26:20 -0800422 Push32(buf, static_cast<uint32_t>(data));
buzbee0d829482013-10-11 15:24:55 -0700423 }
424}
425
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
427 while (buf.size() < offset) {
428 buf.push_back(0);
429 }
430}
431
432/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700433void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 AlignBuffer(code_buffer_, data_offset_);
435 LIR* data_lir = literal_list_;
436 while (data_lir != NULL) {
Andreas Gampe2da88232014-02-27 12:26:20 -0800437 Push32(code_buffer_, data_lir->operands[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 data_lir = NEXT_LIR(data_lir);
439 }
440 // Push code and method literals, record offsets for the compiler to patch.
441 data_lir = code_literal_list_;
442 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700443 uint32_t target_method_idx = data_lir->operands[0];
444 const DexFile* target_dex_file =
445 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700447 cu_->class_def_idx,
448 cu_->method_idx,
449 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700450 target_method_idx,
451 target_dex_file,
452 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700453 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700454 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700455 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700456 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 data_lir = NEXT_LIR(data_lir);
458 }
459 data_lir = method_literal_list_;
460 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700461 uint32_t target_method_idx = data_lir->operands[0];
462 const DexFile* target_dex_file =
463 reinterpret_cast<const DexFile*>(UnwrapPointer(data_lir->operands[1]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700465 cu_->class_def_idx,
466 cu_->method_idx,
467 cu_->invoke_type,
Jeff Hao49161ce2014-03-12 11:05:25 -0700468 target_method_idx,
469 target_dex_file,
470 static_cast<InvokeType>(data_lir->operands[2]),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700471 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700472 const DexFile::MethodId& target_method_id = target_dex_file->GetMethodId(target_method_idx);
buzbee0d829482013-10-11 15:24:55 -0700473 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700474 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 data_lir = NEXT_LIR(data_lir);
476 }
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800477 // Push class literals.
478 data_lir = class_literal_list_;
479 while (data_lir != NULL) {
Jeff Hao49161ce2014-03-12 11:05:25 -0700480 uint32_t target_method_idx = data_lir->operands[0];
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800481 cu_->compiler_driver->AddClassPatch(cu_->dex_file,
482 cu_->class_def_idx,
483 cu_->method_idx,
Jeff Hao49161ce2014-03-12 11:05:25 -0700484 target_method_idx,
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800485 code_buffer_.size());
Jeff Hao49161ce2014-03-12 11:05:25 -0700486 const DexFile::TypeId& target_method_id = cu_->dex_file->GetTypeId(target_method_idx);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800487 // unique value based on target to ensure code deduplication works
Jeff Hao49161ce2014-03-12 11:05:25 -0700488 PushPointer(code_buffer_, &target_method_id, cu_->target64);
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800489 data_lir = NEXT_LIR(data_lir);
490 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491}
492
493/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700494void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
496 while (true) {
497 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
498 if (tab_rec == NULL) break;
499 AlignBuffer(code_buffer_, tab_rec->offset);
500 /*
501 * For Arm, our reference point is the address of the bx
502 * instruction that does the launch, so we have to subtract
503 * the auto pc-advance. For other targets the reference point
504 * is a label, so we can use the offset as-is.
505 */
506 int bx_offset = INVALID_OFFSET;
507 switch (cu_->instruction_set) {
508 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700509 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 bx_offset = tab_rec->anchor->offset + 4;
511 break;
512 case kX86:
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700513 case kX86_64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 bx_offset = 0;
515 break;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100516 case kArm64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 case kMips:
518 bx_offset = tab_rec->anchor->offset;
519 break;
520 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
521 }
522 if (cu_->verbose) {
523 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
524 }
525 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700526 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
528 int disp = tab_rec->targets[elems]->offset - bx_offset;
529 if (cu_->verbose) {
530 LOG(INFO) << " Case[" << elems << "] key: 0x"
531 << std::hex << keys[elems] << ", disp: 0x"
532 << std::hex << disp;
533 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800534 Push32(code_buffer_, keys[elems]);
535 Push32(code_buffer_,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 tab_rec->targets[elems]->offset - bx_offset);
537 }
538 } else {
539 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
540 static_cast<int>(Instruction::kPackedSwitchSignature));
541 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
542 int disp = tab_rec->targets[elems]->offset - bx_offset;
543 if (cu_->verbose) {
544 LOG(INFO) << " Case[" << elems << "] disp: 0x"
545 << std::hex << disp;
546 }
Andreas Gampe2da88232014-02-27 12:26:20 -0800547 Push32(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 }
549 }
550 }
551}
552
553/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700554void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
556 while (true) {
557 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
558 if (tab_rec == NULL) break;
559 AlignBuffer(code_buffer_, tab_rec->offset);
560 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700561 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
562 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700563 }
564 }
565}
566
buzbee0d829482013-10-11 15:24:55 -0700567static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700568 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 lir->offset = offset;
570 offset += 4;
571 }
572 return offset;
573}
574
Ian Rogersff093b32014-04-30 19:04:27 -0700575static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset,
576 unsigned int element_size) {
buzbee0d829482013-10-11 15:24:55 -0700577 // Align to natural pointer size.
Andreas Gampe66018822014-05-05 20:47:19 -0700578 offset = RoundUp(offset, element_size);
buzbee0d829482013-10-11 15:24:55 -0700579 for (; lir != NULL; lir = lir->next) {
580 lir->offset = offset;
581 offset += element_size;
582 }
583 return offset;
584}
585
Brian Carlstrom7940e442013-07-12 13:46:57 -0700586// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700587bool Mir2Lir::VerifyCatchEntries() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000588 MappingTable table(&encoded_mapping_table_[0]);
589 std::vector<uint32_t> dex_pcs;
590 dex_pcs.reserve(table.DexToPcSize());
591 for (auto it = table.DexToPcBegin(), end = table.DexToPcEnd(); it != end; ++it) {
592 dex_pcs.push_back(it.DexPc());
593 }
594 // Sort dex_pcs, so that we can quickly check it against the ordered mir_graph_->catches_.
595 std::sort(dex_pcs.begin(), dex_pcs.end());
596
Brian Carlstrom7940e442013-07-12 13:46:57 -0700597 bool success = true;
Vladimir Marko06606b92013-12-02 15:31:08 +0000598 auto it = dex_pcs.begin(), end = dex_pcs.end();
599 for (uint32_t dex_pc : mir_graph_->catches_) {
600 while (it != end && *it < dex_pc) {
601 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << *it;
602 ++it;
603 success = false;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000605 if (it == end || *it > dex_pc) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
607 success = false;
Vladimir Marko06606b92013-12-02 15:31:08 +0000608 } else {
609 ++it;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700610 }
611 }
612 if (!success) {
613 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
614 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
Vladimir Marko06606b92013-12-02 15:31:08 +0000615 << table.DexToPcSize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700616 }
617 return success;
618}
619
620
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700621void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000622 uint32_t pc2dex_data_size = 0u;
623 uint32_t pc2dex_entries = 0u;
624 uint32_t pc2dex_offset = 0u;
625 uint32_t pc2dex_dalvik_offset = 0u;
626 uint32_t dex2pc_data_size = 0u;
627 uint32_t dex2pc_entries = 0u;
628 uint32_t dex2pc_offset = 0u;
629 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
631 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000632 pc2dex_entries += 1;
633 DCHECK(pc2dex_offset <= tgt_lir->offset);
634 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
635 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
636 static_cast<int32_t>(pc2dex_dalvik_offset));
637 pc2dex_offset = tgt_lir->offset;
638 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639 }
640 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000641 dex2pc_entries += 1;
642 DCHECK(dex2pc_offset <= tgt_lir->offset);
643 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
644 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
645 static_cast<int32_t>(dex2pc_dalvik_offset));
646 dex2pc_offset = tgt_lir->offset;
647 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700648 }
649 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000650
651 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
652 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
653 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
Vladimir Marko06606b92013-12-02 15:31:08 +0000654 encoded_mapping_table_.resize(data_size);
655 uint8_t* write_pos = &encoded_mapping_table_[0];
656 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
657 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
658 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]), hdr_data_size);
659 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000660
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000661 pc2dex_offset = 0u;
662 pc2dex_dalvik_offset = 0u;
Vladimir Marko06606b92013-12-02 15:31:08 +0000663 dex2pc_offset = 0u;
664 dex2pc_dalvik_offset = 0u;
665 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
666 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
667 DCHECK(pc2dex_offset <= tgt_lir->offset);
668 write_pos = EncodeUnsignedLeb128(write_pos, tgt_lir->offset - pc2dex_offset);
669 write_pos = EncodeSignedLeb128(write_pos, static_cast<int32_t>(tgt_lir->dalvik_offset) -
670 static_cast<int32_t>(pc2dex_dalvik_offset));
671 pc2dex_offset = tgt_lir->offset;
672 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
673 }
674 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
675 DCHECK(dex2pc_offset <= tgt_lir->offset);
676 write_pos2 = EncodeUnsignedLeb128(write_pos2, tgt_lir->offset - dex2pc_offset);
677 write_pos2 = EncodeSignedLeb128(write_pos2, static_cast<int32_t>(tgt_lir->dalvik_offset) -
678 static_cast<int32_t>(dex2pc_dalvik_offset));
679 dex2pc_offset = tgt_lir->offset;
680 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
681 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000682 }
Vladimir Marko06606b92013-12-02 15:31:08 +0000683 DCHECK_EQ(static_cast<size_t>(write_pos - &encoded_mapping_table_[0]),
684 hdr_data_size + pc2dex_data_size);
685 DCHECK_EQ(static_cast<size_t>(write_pos2 - &encoded_mapping_table_[0]), data_size);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000686
Ian Rogers96faf5b2013-08-09 22:05:32 -0700687 if (kIsDebugBuild) {
Vladimir Marko06606b92013-12-02 15:31:08 +0000688 CHECK(VerifyCatchEntries());
689
Ian Rogers96faf5b2013-08-09 22:05:32 -0700690 // Verify the encoded table holds the expected data.
Vladimir Marko06606b92013-12-02 15:31:08 +0000691 MappingTable table(&encoded_mapping_table_[0]);
Ian Rogers96faf5b2013-08-09 22:05:32 -0700692 CHECK_EQ(table.TotalSize(), total_entries);
693 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000694 auto it = table.PcToDexBegin();
Vladimir Marko06606b92013-12-02 15:31:08 +0000695 auto it2 = table.DexToPcBegin();
696 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
697 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
698 CHECK_EQ(tgt_lir->offset, it.NativePcOffset());
699 CHECK_EQ(tgt_lir->dalvik_offset, it.DexPc());
700 ++it;
701 }
702 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
703 CHECK_EQ(tgt_lir->offset, it2.NativePcOffset());
704 CHECK_EQ(tgt_lir->dalvik_offset, it2.DexPc());
705 ++it2;
706 }
Ian Rogers96faf5b2013-08-09 22:05:32 -0700707 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000708 CHECK(it == table.PcToDexEnd());
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000709 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700710 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700711}
712
Brian Carlstrom7940e442013-07-12 13:46:57 -0700713void Mir2Lir::CreateNativeGcMap() {
Vladimir Marko06606b92013-12-02 15:31:08 +0000714 DCHECK(!encoded_mapping_table_.empty());
715 MappingTable mapping_table(&encoded_mapping_table_[0]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 uint32_t max_native_offset = 0;
Vladimir Marko06606b92013-12-02 15:31:08 +0000717 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
718 uint32_t native_offset = it.NativePcOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700719 if (native_offset > max_native_offset) {
720 max_native_offset = native_offset;
721 }
722 }
723 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
Vladimir Marko2730db02014-01-27 11:15:17 +0000724 const std::vector<uint8_t>& gc_map_raw =
725 mir_graph_->GetCurrentDexCompilationUnit()->GetVerifiedMethod()->GetDexGcMap();
726 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
727 DCHECK_EQ(gc_map_raw.size(), dex_gc_map.RawSize());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700728 // Compute native offset to references size.
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000729 GcMapBuilder native_gc_map_builder(&native_gc_map_,
730 mapping_table.PcToDexSize(),
731 max_native_offset, dex_gc_map.RegWidth());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732
Vladimir Marko06606b92013-12-02 15:31:08 +0000733 for (auto it = mapping_table.PcToDexBegin(), end = mapping_table.PcToDexEnd(); it != end; ++it) {
734 uint32_t native_offset = it.NativePcOffset();
735 uint32_t dex_pc = it.DexPc();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700736 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Dave Allisonf9439142014-03-27 15:10:22 -0700737 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc <<
738 ": " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700739 native_gc_map_builder.AddEntry(native_offset, references);
740 }
741}
742
743/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700744int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 offset = AssignLiteralOffsetCommon(literal_list_, offset);
Ian Rogersff093b32014-04-30 19:04:27 -0700746 unsigned int ptr_size = GetInstructionSetPointerSize(cu_->instruction_set);
747 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset, ptr_size);
748 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset, ptr_size);
749 offset = AssignLiteralPointerOffsetCommon(class_literal_list_, offset, ptr_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700750 return offset;
751}
752
buzbee0d829482013-10-11 15:24:55 -0700753int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
755 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700756 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 if (tab_rec == NULL) break;
758 tab_rec->offset = offset;
759 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
760 offset += tab_rec->table[1] * (sizeof(int) * 2);
761 } else {
762 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
763 static_cast<int>(Instruction::kPackedSwitchSignature));
764 offset += tab_rec->table[1] * sizeof(int);
765 }
766 }
767 return offset;
768}
769
buzbee0d829482013-10-11 15:24:55 -0700770int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700771 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
772 while (true) {
773 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
774 if (tab_rec == NULL) break;
775 tab_rec->offset = offset;
776 offset += tab_rec->size;
777 // word align
Andreas Gampe66018822014-05-05 20:47:19 -0700778 offset = RoundUp(offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700779 }
780 return offset;
781}
782
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783/*
784 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700785 * offset vaddr if pretty-printing, otherise use the standard block
786 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700787 * branch table during the assembly phase. All resource flags
788 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789 */
buzbee0d829482013-10-11 15:24:55 -0700790LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700791 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700792 LIR* res = boundary_lir;
793 if (cu_->verbose) {
794 // Only pay the expense if we're pretty-printing.
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000795 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR));
buzbeeb48819d2013-09-14 16:15:25 -0700796 new_label->dalvik_offset = vaddr;
797 new_label->opcode = kPseudoCaseLabel;
798 new_label->operands[0] = keyVal;
799 new_label->flags.fixup = kFixupLabel;
800 DCHECK(!new_label->flags.use_def_invalid);
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100801 new_label->u.m.def_mask = &kEncodeAll;
buzbeeb48819d2013-09-14 16:15:25 -0700802 InsertLIRAfter(boundary_lir, new_label);
803 res = new_label;
804 }
805 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700806}
807
buzbee0d829482013-10-11 15:24:55 -0700808void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700809 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700810 DexOffset base_vaddr = tab_rec->vaddr;
811 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700812 int entries = table[1];
813 int low_key = s4FromSwitchData(&table[2]);
814 for (int i = 0; i < entries; i++) {
815 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
816 }
817}
818
buzbee0d829482013-10-11 15:24:55 -0700819void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700821 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700822 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700823 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
824 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700825 for (int i = 0; i < entries; i++) {
826 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
827 }
828}
829
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700830void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700831 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
832 while (true) {
833 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
834 if (tab_rec == NULL) break;
835 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
836 MarkPackedCaseLabels(tab_rec);
837 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
838 MarkSparseCaseLabels(tab_rec);
839 } else {
840 LOG(FATAL) << "Invalid switch table";
841 }
842 }
843}
844
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700845void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700846 /*
847 * Sparse switch data format:
848 * ushort ident = 0x0200 magic value
849 * ushort size number of entries in the table; > 0
850 * int keys[size] keys, sorted low-to-high; 32-bit aligned
851 * int targets[size] branch targets, relative to switch opcode
852 *
853 * Total size is (2+size*4) 16-bit code units.
854 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700855 uint16_t ident = table[0];
856 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700857 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
858 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700859 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
860 << ", entries: " << std::dec << entries;
861 for (int i = 0; i < entries; i++) {
862 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
863 }
864}
865
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700866void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 /*
868 * Packed switch data format:
869 * ushort ident = 0x0100 magic value
870 * ushort size number of entries in the table
871 * int first_key first (and lowest) switch case value
872 * int targets[size] branch targets, relative to switch opcode
873 *
874 * Total size is (4+size*2) 16-bit code units.
875 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700877 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700878 int entries = table[1];
879 int low_key = s4FromSwitchData(&table[2]);
880 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
881 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
882 for (int i = 0; i < entries; i++) {
883 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
884 << targets[i];
885 }
886}
887
buzbee252254b2013-09-08 16:20:53 -0700888/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700889void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
890 // NOTE: only used for debug listings.
891 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700892}
893
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700894bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700895 bool is_taken;
896 switch (opcode) {
897 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
898 case Instruction::IF_NE: is_taken = (src1 != src2); break;
899 case Instruction::IF_LT: is_taken = (src1 < src2); break;
900 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
901 case Instruction::IF_GT: is_taken = (src1 > src2); break;
902 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
903 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
904 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
905 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
906 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
907 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
908 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
909 default:
910 LOG(FATAL) << "Unexpected opcode " << opcode;
911 is_taken = false;
912 }
913 return is_taken;
914}
915
916// Convert relation of src1/src2 to src2/src1
917ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
918 ConditionCode res;
919 switch (before) {
920 case kCondEq: res = kCondEq; break;
921 case kCondNe: res = kCondNe; break;
922 case kCondLt: res = kCondGt; break;
923 case kCondGt: res = kCondLt; break;
924 case kCondLe: res = kCondGe; break;
925 case kCondGe: res = kCondLe; break;
926 default:
927 res = static_cast<ConditionCode>(0);
928 LOG(FATAL) << "Unexpected ccode " << before;
929 }
930 return res;
931}
932
Vladimir Markoa1a70742014-03-03 10:28:05 +0000933ConditionCode Mir2Lir::NegateComparison(ConditionCode before) {
934 ConditionCode res;
935 switch (before) {
936 case kCondEq: res = kCondNe; break;
937 case kCondNe: res = kCondEq; break;
938 case kCondLt: res = kCondGe; break;
939 case kCondGt: res = kCondLe; break;
940 case kCondLe: res = kCondGt; break;
941 case kCondGe: res = kCondLt; break;
942 default:
943 res = static_cast<ConditionCode>(0);
944 LOG(FATAL) << "Unexpected ccode " << before;
945 }
946 return res;
947}
948
Brian Carlstrom7940e442013-07-12 13:46:57 -0700949// TODO: move to mir_to_lir.cc
950Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
951 : Backend(arena),
952 literal_list_(NULL),
953 method_literal_list_(NULL),
Hiroshi Yamauchibe1ca552014-01-15 11:46:48 -0800954 class_literal_list_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700955 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700956 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 cu_(cu),
958 mir_graph_(mir_graph),
959 switch_tables_(arena, 4, kGrowableArraySwitchTables),
960 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
buzbeebd663de2013-09-10 15:41:31 -0700961 tempreg_info_(arena, 20, kGrowableArrayMisc),
buzbee091cc402014-03-31 10:14:40 -0700962 reginfo_map_(arena, RegStorage::kMaxRegs, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -0700963 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964 data_offset_(0),
965 total_size_(0),
966 block_label_list_(NULL),
buzbeed69835d2014-02-03 14:40:27 -0800967 promotion_map_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700968 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700969 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700970 reg_pool_(NULL),
971 live_sreg_(0),
972 num_core_spills_(0),
973 num_fp_spills_(0),
974 frame_size_(0),
975 core_spill_mask_(0),
976 fp_spill_mask_(0),
977 first_lir_insn_(NULL),
Dave Allisonbcec6fb2014-01-17 12:52:22 -0800978 last_lir_insn_(NULL),
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100979 slow_paths_(arena, 32, kGrowableArraySlowPaths),
980 mem_ref_type_(ResourceMask::kHeapRef),
981 mask_cache_(arena) {
buzbee0d829482013-10-11 15:24:55 -0700982 // Reserve pointer id 0 for NULL.
983 size_t null_idx = WrapPointer(NULL);
984 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985}
986
987void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -0700988 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700989 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
990
991 /* Allocate Registers using simple local allocation scheme */
992 SimpleRegAlloc();
993
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800994 /* First try the custom light codegen for special cases. */
Vladimir Marko5816ed42013-11-27 17:04:20 +0000995 DCHECK(cu_->compiler_driver->GetMethodInlinerMap() != nullptr);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800996 bool special_worked = cu_->compiler_driver->GetMethodInlinerMap()->GetMethodInliner(cu_->dex_file)
Vladimir Marko5816ed42013-11-27 17:04:20 +0000997 ->GenSpecial(this, cu_->method_idx);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700998
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800999 /* Take normal path for converting MIR to LIR only if the special codegen did not succeed. */
1000 if (special_worked == false) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001001 MethodMIR2LIR();
1002 }
1003
1004 /* Method is not empty */
1005 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 // mark the targets of switch statement case labels
1007 ProcessSwitchTables();
1008
1009 /* Convert LIR into machine code. */
1010 AssembleLIR();
1011
buzbeeb01bf152014-05-13 15:59:07 -07001012 if ((cu_->enable_debug & (1 << kDebugCodegenDump)) != 0) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 CodegenDump();
1014 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001015 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016}
1017
1018CompiledMethod* Mir2Lir::GetCompiledMethod() {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001019 // Combine vmap tables - core regs, then fp regs - into vmap_table.
1020 Leb128EncodingVector vmap_encoder;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001021 if (frame_size_ > 0) {
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001022 // Prefix the encoded data with its size.
1023 size_t size = core_vmap_table_.size() + 1 /* marker */ + fp_vmap_table_.size();
1024 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
1025 vmap_encoder.PushBackUnsigned(size);
1026 // Core regs may have been inserted out of order - sort first.
1027 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
1028 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
1029 // Copy, stripping out the phys register sort key.
1030 vmap_encoder.PushBackUnsigned(
1031 ~(-1 << VREG_NUM_WIDTH) & (core_vmap_table_[i] + VmapTable::kEntryAdjustment));
1032 }
1033 // Push a marker to take place of lr.
1034 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
1035 // fp regs already sorted.
1036 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
1037 vmap_encoder.PushBackUnsigned(fp_vmap_table_[i] + VmapTable::kEntryAdjustment);
1038 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001039 } else {
Vladimir Marko81949632014-05-02 11:53:22 +01001040 DCHECK_EQ(POPCOUNT(core_spill_mask_), 0);
1041 DCHECK_EQ(POPCOUNT(fp_spill_mask_), 0);
Vladimir Marko2e589aa2014-02-25 17:53:53 +00001042 DCHECK_EQ(core_vmap_table_.size(), 0u);
1043 DCHECK_EQ(fp_vmap_table_.size(), 0u);
1044 vmap_encoder.PushBackUnsigned(0u); // Size is 0.
Brian Carlstrom7940e442013-07-12 13:46:57 -07001045 }
Mark Mendellae9fd932014-02-10 16:14:35 -08001046
Ian Rogers700a4022014-05-19 16:49:03 -07001047 std::unique_ptr<std::vector<uint8_t>> cfi_info(ReturnCallFrameInformation());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001048 CompiledMethod* result =
Ian Rogers72d32622014-05-06 16:20:11 -07001049 new CompiledMethod(cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
Vladimir Marko06606b92013-12-02 15:31:08 +00001050 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_,
Dave Allisond6ed6422014-04-09 23:36:15 +00001051 vmap_encoder.GetData(), native_gc_map_, cfi_info.get());
Brian Carlstrom7940e442013-07-12 13:46:57 -07001052 return result;
1053}
1054
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -08001055size_t Mir2Lir::GetMaxPossibleCompilerTemps() const {
1056 // Chose a reasonably small value in order to contain stack growth.
1057 // Backends that are smarter about spill region can return larger values.
1058 const size_t max_compiler_temps = 10;
1059 return max_compiler_temps;
1060}
1061
1062size_t Mir2Lir::GetNumBytesForCompilerTempSpillRegion() {
1063 // By default assume that the Mir2Lir will need one slot for each temporary.
1064 // If the backend can better determine temps that have non-overlapping ranges and
1065 // temps that do not need spilled, it can actually provide a small region.
1066 return (mir_graph_->GetNumUsedCompilerTemps() * sizeof(uint32_t));
1067}
1068
Brian Carlstrom7940e442013-07-12 13:46:57 -07001069int Mir2Lir::ComputeFrameSize() {
1070 /* Figure out the frame size */
Dmitry Petrochenkof29a4242014-05-05 20:28:47 +07001071 uint32_t size = num_core_spills_ * GetBytesPerGprSpillLocation(cu_->instruction_set)
1072 + num_fp_spills_ * GetBytesPerFprSpillLocation(cu_->instruction_set)
1073 + sizeof(uint32_t) // Filler.
1074 + (cu_->num_regs + cu_->num_outs) * sizeof(uint32_t)
1075 + GetNumBytesForCompilerTempSpillRegion();
Brian Carlstrom7940e442013-07-12 13:46:57 -07001076 /* Align and set */
Andreas Gampe66018822014-05-05 20:47:19 -07001077 return RoundUp(size, kStackAlignment);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001078}
1079
1080/*
1081 * Append an LIR instruction to the LIR list maintained by a compilation
1082 * unit
1083 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001084void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001085 if (first_lir_insn_ == NULL) {
1086 DCHECK(last_lir_insn_ == NULL);
1087 last_lir_insn_ = first_lir_insn_ = lir;
1088 lir->prev = lir->next = NULL;
1089 } else {
1090 last_lir_insn_->next = lir;
1091 lir->prev = last_lir_insn_;
1092 lir->next = NULL;
1093 last_lir_insn_ = lir;
1094 }
1095}
1096
1097/*
1098 * Insert an LIR instruction before the current instruction, which cannot be the
1099 * first instruction.
1100 *
1101 * prev_lir <-> new_lir <-> current_lir
1102 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001103void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001104 DCHECK(current_lir->prev != NULL);
1105 LIR *prev_lir = current_lir->prev;
1106
1107 prev_lir->next = new_lir;
1108 new_lir->prev = prev_lir;
1109 new_lir->next = current_lir;
1110 current_lir->prev = new_lir;
1111}
1112
1113/*
1114 * Insert an LIR instruction after the current instruction, which cannot be the
1115 * first instruction.
1116 *
1117 * current_lir -> new_lir -> old_next
1118 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001119void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001120 new_lir->prev = current_lir;
1121 new_lir->next = current_lir->next;
1122 current_lir->next = new_lir;
1123 new_lir->next->prev = new_lir;
1124}
1125
Mark Mendell4708dcd2014-01-22 09:05:18 -08001126bool Mir2Lir::IsPowerOfTwo(uint64_t x) {
1127 return (x & (x - 1)) == 0;
1128}
1129
1130// Returns the index of the lowest set bit in 'x'.
1131int32_t Mir2Lir::LowestSetBit(uint64_t x) {
1132 int bit_posn = 0;
1133 while ((x & 0xf) == 0) {
1134 bit_posn += 4;
1135 x >>= 4;
1136 }
1137 while ((x & 1) == 0) {
1138 bit_posn++;
1139 x >>= 1;
1140 }
1141 return bit_posn;
1142}
1143
1144bool Mir2Lir::BadOverlap(RegLocation rl_src, RegLocation rl_dest) {
1145 DCHECK(rl_src.wide);
1146 DCHECK(rl_dest.wide);
1147 return (abs(mir_graph_->SRegToVReg(rl_src.s_reg_low) - mir_graph_->SRegToVReg(rl_dest.s_reg_low)) == 1);
1148}
1149
buzbee2700f7e2014-03-07 09:46:20 -08001150LIR *Mir2Lir::OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Mark Mendell766e9292014-01-27 07:55:47 -08001151 int offset, int check_value, LIR* target) {
1152 // Handle this for architectures that can't compare to memory.
buzbee695d13a2014-04-19 13:32:20 -07001153 Load32Disp(base_reg, offset, temp_reg);
Mark Mendell766e9292014-01-27 07:55:47 -08001154 LIR* branch = OpCmpImmBranch(cond, temp_reg, check_value, target);
1155 return branch;
1156}
1157
Dave Allisonbcec6fb2014-01-17 12:52:22 -08001158void Mir2Lir::AddSlowPath(LIRSlowPath* slowpath) {
1159 slow_paths_.Insert(slowpath);
1160}
Mark Mendell55d0eac2014-02-06 11:02:52 -08001161
Jeff Hao49161ce2014-03-12 11:05:25 -07001162void Mir2Lir::LoadCodeAddress(const MethodReference& target_method, InvokeType type,
1163 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001164 LIR* data_target = ScanLiteralPoolMethod(code_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001165 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001166 data_target = AddWordData(&code_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001167 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001168 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1169 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1170 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001171 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001172 }
1173 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1174 AppendLIR(load_pc_rel);
1175 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1176}
1177
Jeff Hao49161ce2014-03-12 11:05:25 -07001178void Mir2Lir::LoadMethodAddress(const MethodReference& target_method, InvokeType type,
1179 SpecialTargetRegister symbolic_reg) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001180 LIR* data_target = ScanLiteralPoolMethod(method_literal_list_, target_method);
Mark Mendell55d0eac2014-02-06 11:02:52 -08001181 if (data_target == NULL) {
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001182 data_target = AddWordData(&method_literal_list_, target_method.dex_method_index);
Jeff Hao49161ce2014-03-12 11:05:25 -07001183 data_target->operands[1] = WrapPointer(const_cast<DexFile*>(target_method.dex_file));
Vladimir Markoa51a0b02014-05-21 12:08:39 +01001184 // NOTE: The invoke type doesn't contribute to the literal identity. In fact, we can have
1185 // the same method invoked with kVirtual, kSuper and kInterface but the class linker will
1186 // resolve these invokes to the same method, so we don't care which one we record here.
Jeff Hao49161ce2014-03-12 11:05:25 -07001187 data_target->operands[2] = type;
Mark Mendell55d0eac2014-02-06 11:02:52 -08001188 }
1189 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1190 AppendLIR(load_pc_rel);
1191 DCHECK_NE(cu_->instruction_set, kMips) << reinterpret_cast<void*>(data_target);
1192}
1193
1194void Mir2Lir::LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg) {
1195 // Use the literal pool and a PC-relative load from a data word.
1196 LIR* data_target = ScanLiteralPool(class_literal_list_, type_idx, 0);
1197 if (data_target == nullptr) {
1198 data_target = AddWordData(&class_literal_list_, type_idx);
1199 }
1200 LIR* load_pc_rel = OpPcRelLoad(TargetReg(symbolic_reg), data_target);
1201 AppendLIR(load_pc_rel);
1202}
1203
Mark Mendellae9fd932014-02-10 16:14:35 -08001204std::vector<uint8_t>* Mir2Lir::ReturnCallFrameInformation() {
1205 // Default case is to do nothing.
1206 return nullptr;
1207}
1208
buzbee2700f7e2014-03-07 09:46:20 -08001209RegLocation Mir2Lir::NarrowRegLoc(RegLocation loc) {
buzbee091cc402014-03-31 10:14:40 -07001210 if (loc.location == kLocPhysReg) {
buzbee85089dd2014-05-25 15:10:52 -07001211 DCHECK(!loc.reg.Is32Bit());
buzbee091cc402014-03-31 10:14:40 -07001212 if (loc.reg.IsPair()) {
buzbee85089dd2014-05-25 15:10:52 -07001213 RegisterInfo* info_lo = GetRegInfo(loc.reg.GetLow());
1214 RegisterInfo* info_hi = GetRegInfo(loc.reg.GetHigh());
1215 info_lo->SetIsWide(false);
1216 info_hi->SetIsWide(false);
1217 loc.reg = info_lo->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001218 } else {
buzbee85089dd2014-05-25 15:10:52 -07001219 RegisterInfo* info = GetRegInfo(loc.reg);
1220 RegisterInfo* info_new = info->FindMatchingView(RegisterInfo::k32SoloStorageMask);
1221 DCHECK(info_new != nullptr);
1222 if (info->IsLive() && (info->SReg() == loc.s_reg_low)) {
1223 info->MarkDead();
1224 info_new->MarkLive(loc.s_reg_low);
1225 }
1226 loc.reg = info_new->GetReg();
buzbee091cc402014-03-31 10:14:40 -07001227 }
buzbee85089dd2014-05-25 15:10:52 -07001228 DCHECK(loc.reg.Valid());
buzbee2700f7e2014-03-07 09:46:20 -08001229 }
buzbee85089dd2014-05-25 15:10:52 -07001230 loc.wide = false;
buzbee2700f7e2014-03-07 09:46:20 -08001231 return loc;
1232}
1233
Mark Mendelld65c51a2014-04-29 16:55:20 -04001234void Mir2Lir::GenMachineSpecificExtendedMethodMIR(BasicBlock* bb, MIR* mir) {
1235 LOG(FATAL) << "Unknown MIR opcode not supported on this architecture";
1236}
1237
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001238} // namespace art