blob: 92b24e140aef8da5aab026f4ac04bdf08151dc4e [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"
Ian Rogers96faf5b2013-08-09 22:05:32 -070020#include "mapping_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "mir_to_lir-inl.h"
22#include "verifier/dex_gc_map.h"
23#include "verifier/method_verifier.h"
24
25namespace art {
26
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070027bool Mir2Lir::IsInexpensiveConstant(RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070028 bool res = false;
29 if (rl_src.is_const) {
30 if (rl_src.wide) {
31 if (rl_src.fp) {
32 res = InexpensiveConstantDouble(mir_graph_->ConstantValueWide(rl_src));
33 } else {
34 res = InexpensiveConstantLong(mir_graph_->ConstantValueWide(rl_src));
35 }
36 } else {
37 if (rl_src.fp) {
38 res = InexpensiveConstantFloat(mir_graph_->ConstantValue(rl_src));
39 } else {
40 res = InexpensiveConstantInt(mir_graph_->ConstantValue(rl_src));
41 }
42 }
43 }
44 return res;
45}
46
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070047void Mir2Lir::MarkSafepointPC(LIR* inst) {
buzbeeb48819d2013-09-14 16:15:25 -070048 DCHECK(!inst->flags.use_def_invalid);
49 inst->u.m.def_mask = ENCODE_ALL;
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 LIR* safepoint_pc = NewLIR0(kPseudoSafepointPC);
buzbeeb48819d2013-09-14 16:15:25 -070051 DCHECK_EQ(safepoint_pc->u.m.def_mask, ENCODE_ALL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070052}
53
Ian Rogers9b297bf2013-09-06 11:11:25 -070054bool Mir2Lir::FastInstance(uint32_t field_idx, bool is_put, int* field_offset, bool* is_volatile) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 return cu_->compiler_driver->ComputeInstanceFieldInfo(
Ian Rogers9b297bf2013-09-06 11:11:25 -070056 field_idx, mir_graph_->GetCurrentDexCompilationUnit(), is_put, field_offset, is_volatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -070057}
58
buzbee252254b2013-09-08 16:20:53 -070059/* Remove a LIR from the list. */
60void Mir2Lir::UnlinkLIR(LIR* lir) {
61 if (UNLIKELY(lir == first_lir_insn_)) {
62 first_lir_insn_ = lir->next;
63 if (lir->next != NULL) {
64 lir->next->prev = NULL;
65 } else {
66 DCHECK(lir->next == NULL);
67 DCHECK(lir == last_lir_insn_);
68 last_lir_insn_ = NULL;
69 }
70 } else if (lir == last_lir_insn_) {
71 last_lir_insn_ = lir->prev;
72 lir->prev->next = NULL;
73 } else if ((lir->prev != NULL) && (lir->next != NULL)) {
74 lir->prev->next = lir->next;
75 lir->next->prev = lir->prev;
76 }
77}
78
Brian Carlstrom7940e442013-07-12 13:46:57 -070079/* Convert an instruction to a NOP */
Brian Carlstromdf629502013-07-17 22:39:56 -070080void Mir2Lir::NopLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 lir->flags.is_nop = true;
buzbee252254b2013-09-08 16:20:53 -070082 if (!cu_->verbose) {
83 UnlinkLIR(lir);
84 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070085}
86
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070087void Mir2Lir::SetMemRefType(LIR* lir, bool is_load, int mem_type) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 uint64_t *mask_ptr;
Brian Carlstromf69863b2013-07-17 21:53:13 -070089 uint64_t mask = ENCODE_MEM;
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeeb48819d2013-09-14 16:15:25 -070091 DCHECK(!lir->flags.use_def_invalid);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 if (is_load) {
buzbeeb48819d2013-09-14 16:15:25 -070093 mask_ptr = &lir->u.m.use_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 } else {
buzbeeb48819d2013-09-14 16:15:25 -070095 mask_ptr = &lir->u.m.def_mask;
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 }
97 /* Clear out the memref flags */
98 *mask_ptr &= ~mask;
99 /* ..and then add back the one we need */
100 switch (mem_type) {
101 case kLiteral:
102 DCHECK(is_load);
103 *mask_ptr |= ENCODE_LITERAL;
104 break;
105 case kDalvikReg:
106 *mask_ptr |= ENCODE_DALVIK_REG;
107 break;
108 case kHeapRef:
109 *mask_ptr |= ENCODE_HEAP_REF;
110 break;
111 case kMustNotAlias:
112 /* Currently only loads can be marked as kMustNotAlias */
113 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
114 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
115 break;
116 default:
117 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
118 }
119}
120
121/*
122 * Mark load/store instructions that access Dalvik registers through the stack.
123 */
124void Mir2Lir::AnnotateDalvikRegAccess(LIR* lir, int reg_id, bool is_load,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700125 bool is64bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126 SetMemRefType(lir, is_load, kDalvikReg);
127
128 /*
129 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
130 * access.
131 */
buzbeeb48819d2013-09-14 16:15:25 -0700132 lir->flags.alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133}
134
135/*
136 * Debugging macros
137 */
138#define DUMP_RESOURCE_MASK(X)
139
140/* Pretty-print a LIR instruction */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700141void Mir2Lir::DumpLIRInsn(LIR* lir, unsigned char* base_addr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 int offset = lir->offset;
143 int dest = lir->operands[0];
144 const bool dump_nop = (cu_->enable_debug & (1 << kDebugShowNops));
145
146 /* Handle pseudo-ops individually, and all regular insns as a group */
147 switch (lir->opcode) {
148 case kPseudoMethodEntry:
149 LOG(INFO) << "-------- method entry "
150 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
151 break;
152 case kPseudoMethodExit:
153 LOG(INFO) << "-------- Method_Exit";
154 break;
155 case kPseudoBarrier:
156 LOG(INFO) << "-------- BARRIER";
157 break;
158 case kPseudoEntryBlock:
159 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
160 break;
161 case kPseudoDalvikByteCodeBoundary:
162 if (lir->operands[0] == 0) {
buzbee0d829482013-10-11 15:24:55 -0700163 // NOTE: only used for debug listings.
164 lir->operands[0] = WrapPointer(ArenaStrdup("No instruction string"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 }
166 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
Bill Buzbee0b1191c2013-10-28 22:11:59 +0000167 << lir->dalvik_offset << " @ "
168 << reinterpret_cast<char*>(UnwrapPointer(lir->operands[0]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 break;
170 case kPseudoExitBlock:
171 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
172 break;
173 case kPseudoPseudoAlign4:
174 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
175 << offset << "): .align4";
176 break;
177 case kPseudoEHBlockLabel:
178 LOG(INFO) << "Exception_Handling:";
179 break;
180 case kPseudoTargetLabel:
181 case kPseudoNormalBlockLabel:
182 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
183 break;
184 case kPseudoThrowTarget:
185 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
186 break;
187 case kPseudoIntrinsicRetry:
188 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
189 break;
190 case kPseudoSuspendTarget:
191 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
192 break;
193 case kPseudoSafepointPC:
194 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
195 break;
196 case kPseudoExportedPC:
197 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
198 break;
199 case kPseudoCaseLabel:
200 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
201 << std::hex << lir->operands[0] << "|" << std::dec <<
202 lir->operands[0];
203 break;
204 default:
205 if (lir->flags.is_nop && !dump_nop) {
206 break;
207 } else {
208 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
209 lir, base_addr));
210 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
211 lir, base_addr));
212 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
213 reinterpret_cast<unsigned int>(base_addr + offset),
214 op_name.c_str(), op_operands.c_str(),
215 lir->flags.is_nop ? "(nop)" : "");
216 }
217 break;
218 }
219
buzbeeb48819d2013-09-14 16:15:25 -0700220 if (lir->u.m.use_mask && (!lir->flags.is_nop || dump_nop)) {
221 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.use_mask, "use"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 }
buzbeeb48819d2013-09-14 16:15:25 -0700223 if (lir->u.m.def_mask && (!lir->flags.is_nop || dump_nop)) {
224 DUMP_RESOURCE_MASK(DumpResourceMask(lir, lir->u.m.def_mask, "def"));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 }
226}
227
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700228void Mir2Lir::DumpPromotionMap() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 int num_regs = cu_->num_dalvik_registers + cu_->num_compiler_temps + 1;
230 for (int i = 0; i < num_regs; i++) {
231 PromotionMap v_reg_map = promotion_map_[i];
232 std::string buf;
233 if (v_reg_map.fp_location == kLocPhysReg) {
234 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & FpRegMask());
235 }
236
237 std::string buf3;
238 if (i < cu_->num_dalvik_registers) {
239 StringAppendF(&buf3, "%02d", i);
240 } else if (i == mir_graph_->GetMethodSReg()) {
241 buf3 = "Method*";
242 } else {
243 StringAppendF(&buf3, "ct%d", i - cu_->num_dalvik_registers);
244 }
245
246 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
247 v_reg_map.core_location == kLocPhysReg ?
248 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
249 v_reg_map.core_reg : SRegOffset(i),
250 buf.c_str());
251 }
252}
253
254/* Dump a mapping table */
Ian Rogersd91d6d62013-09-25 20:26:14 -0700255void Mir2Lir::DumpMappingTable(const char* table_name, const char* descriptor,
256 const char* name, const Signature& signature,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 const std::vector<uint32_t>& v) {
258 if (v.size() > 0) {
259 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700260 descriptor, name, signature.ToString().c_str(), v.size()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 std::replace(line.begin(), line.end(), ';', '_');
262 LOG(INFO) << line;
263 for (uint32_t i = 0; i < v.size(); i+=2) {
264 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
265 LOG(INFO) << line;
266 }
267 LOG(INFO) <<" };\n\n";
268 }
269}
270
271/* Dump instructions and constant pool contents */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700272void Mir2Lir::CodegenDump() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 LOG(INFO) << "Dumping LIR insns for "
274 << PrettyMethod(cu_->method_idx, *cu_->dex_file);
275 LIR* lir_insn;
276 int insns_size = cu_->code_item->insns_size_in_code_units_;
277
278 LOG(INFO) << "Regs (excluding ins) : " << cu_->num_regs;
279 LOG(INFO) << "Ins : " << cu_->num_ins;
280 LOG(INFO) << "Outs : " << cu_->num_outs;
281 LOG(INFO) << "CoreSpills : " << num_core_spills_;
282 LOG(INFO) << "FPSpills : " << num_fp_spills_;
283 LOG(INFO) << "CompilerTemps : " << cu_->num_compiler_temps;
284 LOG(INFO) << "Frame size : " << frame_size_;
285 LOG(INFO) << "code size is " << total_size_ <<
286 " bytes, Dalvik size is " << insns_size * 2;
287 LOG(INFO) << "expansion factor: "
288 << static_cast<float>(total_size_) / static_cast<float>(insns_size * 2);
289 DumpPromotionMap();
290 for (lir_insn = first_lir_insn_; lir_insn != NULL; lir_insn = lir_insn->next) {
291 DumpLIRInsn(lir_insn, 0);
292 }
293 for (lir_insn = literal_list_; lir_insn != NULL; lir_insn = lir_insn->next) {
294 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
295 lir_insn->operands[0]);
296 }
297
298 const DexFile::MethodId& method_id =
299 cu_->dex_file->GetMethodId(cu_->method_idx);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700300 const Signature signature = cu_->dex_file->GetMethodSignature(method_id);
301 const char* name = cu_->dex_file->GetMethodName(method_id);
302 const char* descriptor(cu_->dex_file->GetMethodDeclaringClassDescriptor(method_id));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303
304 // Dump mapping tables
305 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, pc2dex_mapping_table_);
306 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, dex2pc_mapping_table_);
307}
308
309/*
310 * Search the existing constants in the literal pool for an exact or close match
311 * within specified delta (greater or equal to 0).
312 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313LIR* Mir2Lir::ScanLiteralPool(LIR* data_target, int value, unsigned int delta) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 while (data_target) {
315 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
316 return data_target;
317 data_target = data_target->next;
318 }
319 return NULL;
320}
321
322/* Search the existing constants in the literal pool for an exact wide match */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700323LIR* Mir2Lir::ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 bool lo_match = false;
325 LIR* lo_target = NULL;
326 while (data_target) {
327 if (lo_match && (data_target->operands[0] == val_hi)) {
328 // Record high word in case we need to expand this later.
329 lo_target->operands[1] = val_hi;
330 return lo_target;
331 }
332 lo_match = false;
333 if (data_target->operands[0] == val_lo) {
334 lo_match = true;
335 lo_target = data_target;
336 }
337 data_target = data_target->next;
338 }
339 return NULL;
340}
341
342/*
343 * The following are building blocks to insert constants into the pool or
344 * instruction streams.
345 */
346
347/* Add a 32-bit constant to the constant pool */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700348LIR* Mir2Lir::AddWordData(LIR* *constant_list_p, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 /* Add the constant to the literal pool */
350 if (constant_list_p) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700351 LIR* new_value = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 new_value->operands[0] = value;
353 new_value->next = *constant_list_p;
354 *constant_list_p = new_value;
buzbeeb48819d2013-09-14 16:15:25 -0700355 estimated_native_code_size_ += sizeof(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 return new_value;
357 }
358 return NULL;
359}
360
361/* Add a 64-bit constant to the constant pool or mixed with code */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700362LIR* Mir2Lir::AddWideData(LIR* *constant_list_p, int val_lo, int val_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 AddWordData(constant_list_p, val_hi);
364 return AddWordData(constant_list_p, val_lo);
365}
366
367static void PushWord(std::vector<uint8_t>&buf, int data) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700368 buf.push_back(data & 0xff);
369 buf.push_back((data >> 8) & 0xff);
370 buf.push_back((data >> 16) & 0xff);
371 buf.push_back((data >> 24) & 0xff);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372}
373
buzbee0d829482013-10-11 15:24:55 -0700374// Push 8 bytes on 64-bit systems; 4 on 32-bit systems.
375static void PushPointer(std::vector<uint8_t>&buf, void const* pointer) {
376 uintptr_t data = reinterpret_cast<uintptr_t>(pointer);
377 if (sizeof(void*) == sizeof(uint64_t)) {
378 PushWord(buf, (data >> (sizeof(void*) * 4)) & 0xFFFFFFFF);
379 PushWord(buf, data & 0xFFFFFFFF);
380 } else {
381 PushWord(buf, data);
382 }
383}
384
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
386 while (buf.size() < offset) {
387 buf.push_back(0);
388 }
389}
390
391/* Write the literal pool to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700392void Mir2Lir::InstallLiteralPools() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 AlignBuffer(code_buffer_, data_offset_);
394 LIR* data_lir = literal_list_;
395 while (data_lir != NULL) {
396 PushWord(code_buffer_, data_lir->operands[0]);
397 data_lir = NEXT_LIR(data_lir);
398 }
399 // Push code and method literals, record offsets for the compiler to patch.
400 data_lir = code_literal_list_;
401 while (data_lir != NULL) {
402 uint32_t target = data_lir->operands[0];
403 cu_->compiler_driver->AddCodePatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700404 cu_->class_def_idx,
405 cu_->method_idx,
406 cu_->invoke_type,
407 target,
408 static_cast<InvokeType>(data_lir->operands[1]),
409 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700411 // unique value based on target to ensure code deduplication works
412 PushPointer(code_buffer_, &id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 data_lir = NEXT_LIR(data_lir);
414 }
415 data_lir = method_literal_list_;
416 while (data_lir != NULL) {
417 uint32_t target = data_lir->operands[0];
418 cu_->compiler_driver->AddMethodPatch(cu_->dex_file,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700419 cu_->class_def_idx,
420 cu_->method_idx,
421 cu_->invoke_type,
422 target,
423 static_cast<InvokeType>(data_lir->operands[1]),
424 code_buffer_.size());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 const DexFile::MethodId& id = cu_->dex_file->GetMethodId(target);
buzbee0d829482013-10-11 15:24:55 -0700426 // unique value based on target to ensure code deduplication works
427 PushPointer(code_buffer_, &id);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 data_lir = NEXT_LIR(data_lir);
429 }
430}
431
432/* Write the switch tables to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700433void Mir2Lir::InstallSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
435 while (true) {
436 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
437 if (tab_rec == NULL) break;
438 AlignBuffer(code_buffer_, tab_rec->offset);
439 /*
440 * For Arm, our reference point is the address of the bx
441 * instruction that does the launch, so we have to subtract
442 * the auto pc-advance. For other targets the reference point
443 * is a label, so we can use the offset as-is.
444 */
445 int bx_offset = INVALID_OFFSET;
446 switch (cu_->instruction_set) {
447 case kThumb2:
buzbeeb48819d2013-09-14 16:15:25 -0700448 DCHECK(tab_rec->anchor->flags.fixup != kFixupNone);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 bx_offset = tab_rec->anchor->offset + 4;
450 break;
451 case kX86:
452 bx_offset = 0;
453 break;
454 case kMips:
455 bx_offset = tab_rec->anchor->offset;
456 break;
457 default: LOG(FATAL) << "Unexpected instruction set: " << cu_->instruction_set;
458 }
459 if (cu_->verbose) {
460 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
461 }
462 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee0d829482013-10-11 15:24:55 -0700463 const int32_t* keys = reinterpret_cast<const int32_t*>(&(tab_rec->table[2]));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700464 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
465 int disp = tab_rec->targets[elems]->offset - bx_offset;
466 if (cu_->verbose) {
467 LOG(INFO) << " Case[" << elems << "] key: 0x"
468 << std::hex << keys[elems] << ", disp: 0x"
469 << std::hex << disp;
470 }
471 PushWord(code_buffer_, keys[elems]);
472 PushWord(code_buffer_,
473 tab_rec->targets[elems]->offset - bx_offset);
474 }
475 } else {
476 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
477 static_cast<int>(Instruction::kPackedSwitchSignature));
478 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
479 int disp = tab_rec->targets[elems]->offset - bx_offset;
480 if (cu_->verbose) {
481 LOG(INFO) << " Case[" << elems << "] disp: 0x"
482 << std::hex << disp;
483 }
484 PushWord(code_buffer_, tab_rec->targets[elems]->offset - bx_offset);
485 }
486 }
487 }
488}
489
490/* Write the fill array dta to the output stream */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700491void Mir2Lir::InstallFillArrayData() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
493 while (true) {
494 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
495 if (tab_rec == NULL) break;
496 AlignBuffer(code_buffer_, tab_rec->offset);
497 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700498 code_buffer_.push_back(tab_rec->table[i] & 0xFF);
499 code_buffer_.push_back((tab_rec->table[i] >> 8) & 0xFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700500 }
501 }
502}
503
buzbee0d829482013-10-11 15:24:55 -0700504static int AssignLiteralOffsetCommon(LIR* lir, CodeOffset offset) {
Brian Carlstrom02c8cc62013-07-18 15:54:44 -0700505 for (; lir != NULL; lir = lir->next) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 lir->offset = offset;
507 offset += 4;
508 }
509 return offset;
510}
511
buzbee0d829482013-10-11 15:24:55 -0700512static int AssignLiteralPointerOffsetCommon(LIR* lir, CodeOffset offset) {
513 unsigned int element_size = sizeof(void*);
514 // Align to natural pointer size.
515 offset = (offset + (element_size - 1)) & ~(element_size - 1);
516 for (; lir != NULL; lir = lir->next) {
517 lir->offset = offset;
518 offset += element_size;
519 }
520 return offset;
521}
522
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523// Make sure we have a code address for every declared catch entry
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700524bool Mir2Lir::VerifyCatchEntries() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 bool success = true;
526 for (std::set<uint32_t>::const_iterator it = mir_graph_->catches_.begin();
527 it != mir_graph_->catches_.end(); ++it) {
528 uint32_t dex_pc = *it;
529 bool found = false;
530 for (size_t i = 0; i < dex2pc_mapping_table_.size(); i += 2) {
531 if (dex_pc == dex2pc_mapping_table_[i+1]) {
532 found = true;
533 break;
534 }
535 }
536 if (!found) {
537 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
538 success = false;
539 }
540 }
541 // Now, try in the other direction
542 for (size_t i = 0; i < dex2pc_mapping_table_.size(); i += 2) {
543 uint32_t dex_pc = dex2pc_mapping_table_[i+1];
544 if (mir_graph_->catches_.find(dex_pc) == mir_graph_->catches_.end()) {
545 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
546 success = false;
547 }
548 }
549 if (!success) {
550 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu_->method_idx, *cu_->dex_file);
551 LOG(INFO) << "Entries @ decode: " << mir_graph_->catches_.size() << ", Entries in table: "
552 << dex2pc_mapping_table_.size()/2;
553 }
554 return success;
555}
556
557
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700558void Mir2Lir::CreateMappingTables() {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000559 uint32_t pc2dex_data_size = 0u;
560 uint32_t pc2dex_entries = 0u;
561 uint32_t pc2dex_offset = 0u;
562 uint32_t pc2dex_dalvik_offset = 0u;
563 uint32_t dex2pc_data_size = 0u;
564 uint32_t dex2pc_entries = 0u;
565 uint32_t dex2pc_offset = 0u;
566 uint32_t dex2pc_dalvik_offset = 0u;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 for (LIR* tgt_lir = first_lir_insn_; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
568 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000569 pc2dex_entries += 1;
570 DCHECK(pc2dex_offset <= tgt_lir->offset);
571 pc2dex_data_size += UnsignedLeb128Size(tgt_lir->offset - pc2dex_offset);
572 pc2dex_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
573 static_cast<int32_t>(pc2dex_dalvik_offset));
574 pc2dex_offset = tgt_lir->offset;
575 pc2dex_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 pc2dex_mapping_table_.push_back(tgt_lir->offset);
577 pc2dex_mapping_table_.push_back(tgt_lir->dalvik_offset);
578 }
579 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000580 dex2pc_entries += 1;
581 DCHECK(dex2pc_offset <= tgt_lir->offset);
582 dex2pc_data_size += UnsignedLeb128Size(tgt_lir->offset - dex2pc_offset);
583 dex2pc_data_size += SignedLeb128Size(static_cast<int32_t>(tgt_lir->dalvik_offset) -
584 static_cast<int32_t>(dex2pc_dalvik_offset));
585 dex2pc_offset = tgt_lir->offset;
586 dex2pc_dalvik_offset = tgt_lir->dalvik_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587 dex2pc_mapping_table_.push_back(tgt_lir->offset);
588 dex2pc_mapping_table_.push_back(tgt_lir->dalvik_offset);
589 }
590 }
591 if (kIsDebugBuild) {
Ian Rogers96faf5b2013-08-09 22:05:32 -0700592 CHECK(VerifyCatchEntries());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000594 DCHECK_EQ(pc2dex_mapping_table_.size(), 2u * pc2dex_entries);
595 DCHECK_EQ(dex2pc_mapping_table_.size(), 2u * dex2pc_entries);
596
597 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
598 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
599 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
600 encoded_mapping_table_.Reserve(data_size);
601 encoded_mapping_table_.PushBackUnsigned(total_entries);
602 encoded_mapping_table_.PushBackUnsigned(pc2dex_entries);
603
604 dex2pc_offset = 0u;
605 dex2pc_dalvik_offset = 0u;
606 pc2dex_offset = 0u;
607 pc2dex_dalvik_offset = 0u;
608 for (uint32_t i = 0; i != pc2dex_entries; ++i) {
609 encoded_mapping_table_.PushBackUnsigned(pc2dex_mapping_table_[2 * i] - pc2dex_offset);
610 encoded_mapping_table_.PushBackSigned(static_cast<int32_t>(pc2dex_mapping_table_[2 * i + 1]) -
611 static_cast<int32_t>(pc2dex_dalvik_offset));
612 pc2dex_offset = pc2dex_mapping_table_[2 * i];
613 pc2dex_dalvik_offset = pc2dex_mapping_table_[2 * i + 1];
614 }
615 DCHECK(encoded_mapping_table_.GetData().size() == hdr_data_size + pc2dex_data_size);
616 for (uint32_t i = 0; i != dex2pc_entries; ++i) {
617 encoded_mapping_table_.PushBackUnsigned(dex2pc_mapping_table_[2 * i] - dex2pc_offset);
618 encoded_mapping_table_.PushBackSigned(static_cast<int32_t>(dex2pc_mapping_table_[2 * i + 1]) -
619 static_cast<int32_t>(dex2pc_dalvik_offset));
620 dex2pc_offset = dex2pc_mapping_table_[2 * i];
621 dex2pc_dalvik_offset = dex2pc_mapping_table_[2 * i + 1];
622 }
623 DCHECK(encoded_mapping_table_.GetData().size() == data_size);
624
Ian Rogers96faf5b2013-08-09 22:05:32 -0700625 if (kIsDebugBuild) {
626 // Verify the encoded table holds the expected data.
627 MappingTable table(&encoded_mapping_table_.GetData()[0]);
628 CHECK_EQ(table.TotalSize(), total_entries);
629 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
630 CHECK_EQ(table.DexToPcSize(), dex2pc_mapping_table_.size() / 2);
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000631 auto it = table.PcToDexBegin();
Ian Rogers96faf5b2013-08-09 22:05:32 -0700632 for (uint32_t i = 0; i < pc2dex_mapping_table_.size(); ++i, ++it) {
633 CHECK_EQ(pc2dex_mapping_table_.at(i), it.NativePcOffset());
634 ++i;
635 CHECK_EQ(pc2dex_mapping_table_.at(i), it.DexPc());
636 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000637 CHECK(it == table.PcToDexEnd());
638 auto it2 = table.DexToPcBegin();
Ian Rogers96faf5b2013-08-09 22:05:32 -0700639 for (uint32_t i = 0; i < dex2pc_mapping_table_.size(); ++i, ++it2) {
640 CHECK_EQ(dex2pc_mapping_table_.at(i), it2.NativePcOffset());
641 ++i;
642 CHECK_EQ(dex2pc_mapping_table_.at(i), it2.DexPc());
643 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +0000644 CHECK(it2 == table.DexToPcEnd());
Ian Rogers96faf5b2013-08-09 22:05:32 -0700645 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700646}
647
648class NativePcToReferenceMapBuilder {
649 public:
650 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
651 size_t entries, uint32_t max_native_offset,
652 size_t references_width) : entries_(entries),
653 references_width_(references_width), in_use_(entries),
654 table_(table) {
655 // Compute width in bytes needed to hold max_native_offset.
656 native_offset_width_ = 0;
657 while (max_native_offset != 0) {
658 native_offset_width_++;
659 max_native_offset >>= 8;
660 }
661 // Resize table and set up header.
662 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
663 CHECK_LT(native_offset_width_, 1U << 3);
664 (*table)[0] = native_offset_width_ & 7;
665 CHECK_LT(references_width_, 1U << 13);
666 (*table)[0] |= (references_width_ << 3) & 0xFF;
667 (*table)[1] = (references_width_ >> 5) & 0xFF;
668 CHECK_LT(entries, 1U << 16);
669 (*table)[2] = entries & 0xFF;
670 (*table)[3] = (entries >> 8) & 0xFF;
671 }
672
673 void AddEntry(uint32_t native_offset, const uint8_t* references) {
674 size_t table_index = TableIndex(native_offset);
675 while (in_use_[table_index]) {
676 table_index = (table_index + 1) % entries_;
677 }
678 in_use_[table_index] = true;
buzbee0d829482013-10-11 15:24:55 -0700679 SetCodeOffset(table_index, native_offset);
680 DCHECK_EQ(native_offset, GetCodeOffset(table_index));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 SetReferences(table_index, references);
682 }
683
684 private:
685 size_t TableIndex(uint32_t native_offset) {
686 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
687 }
688
buzbee0d829482013-10-11 15:24:55 -0700689 uint32_t GetCodeOffset(size_t table_index) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700690 uint32_t native_offset = 0;
691 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
692 for (size_t i = 0; i < native_offset_width_; i++) {
693 native_offset |= (*table_)[table_offset + i] << (i * 8);
694 }
695 return native_offset;
696 }
697
buzbee0d829482013-10-11 15:24:55 -0700698 void SetCodeOffset(size_t table_index, uint32_t native_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
700 for (size_t i = 0; i < native_offset_width_; i++) {
701 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
702 }
703 }
704
705 void SetReferences(size_t table_index, const uint8_t* references) {
706 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
707 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
708 }
709
710 size_t EntryWidth() const {
711 return native_offset_width_ + references_width_;
712 }
713
714 // Number of entries in the table.
715 const size_t entries_;
716 // Number of bytes used to encode the reference bitmap.
717 const size_t references_width_;
718 // Number of bytes used to encode a native offset.
719 size_t native_offset_width_;
720 // Entries that are in use.
721 std::vector<bool> in_use_;
722 // The table we're building.
723 std::vector<uint8_t>* const table_;
724};
725
726void Mir2Lir::CreateNativeGcMap() {
727 const std::vector<uint32_t>& mapping_table = pc2dex_mapping_table_;
728 uint32_t max_native_offset = 0;
729 for (size_t i = 0; i < mapping_table.size(); i += 2) {
730 uint32_t native_offset = mapping_table[i + 0];
731 if (native_offset > max_native_offset) {
732 max_native_offset = native_offset;
733 }
734 }
735 MethodReference method_ref(cu_->dex_file, cu_->method_idx);
736 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
737 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
738 // Compute native offset to references size.
739 NativePcToReferenceMapBuilder native_gc_map_builder(&native_gc_map_,
740 mapping_table.size() / 2, max_native_offset,
741 dex_gc_map.RegWidth());
742
743 for (size_t i = 0; i < mapping_table.size(); i += 2) {
744 uint32_t native_offset = mapping_table[i + 0];
745 uint32_t dex_pc = mapping_table[i + 1];
746 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
747 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
748 native_gc_map_builder.AddEntry(native_offset, references);
749 }
750}
751
752/* Determine the offset of each literal field */
buzbee0d829482013-10-11 15:24:55 -0700753int Mir2Lir::AssignLiteralOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700754 offset = AssignLiteralOffsetCommon(literal_list_, offset);
buzbee0d829482013-10-11 15:24:55 -0700755 offset = AssignLiteralPointerOffsetCommon(code_literal_list_, offset);
756 offset = AssignLiteralPointerOffsetCommon(method_literal_list_, offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757 return offset;
758}
759
buzbee0d829482013-10-11 15:24:55 -0700760int Mir2Lir::AssignSwitchTablesOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700761 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
762 while (true) {
buzbee0d829482013-10-11 15:24:55 -0700763 Mir2Lir::SwitchTable* tab_rec = iterator.Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700764 if (tab_rec == NULL) break;
765 tab_rec->offset = offset;
766 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
767 offset += tab_rec->table[1] * (sizeof(int) * 2);
768 } else {
769 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
770 static_cast<int>(Instruction::kPackedSwitchSignature));
771 offset += tab_rec->table[1] * sizeof(int);
772 }
773 }
774 return offset;
775}
776
buzbee0d829482013-10-11 15:24:55 -0700777int Mir2Lir::AssignFillArrayDataOffset(CodeOffset offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 GrowableArray<FillArrayData*>::Iterator iterator(&fill_array_data_);
779 while (true) {
780 Mir2Lir::FillArrayData *tab_rec = iterator.Next();
781 if (tab_rec == NULL) break;
782 tab_rec->offset = offset;
783 offset += tab_rec->size;
784 // word align
785 offset = (offset + 3) & ~3;
786 }
787 return offset;
788}
789
Brian Carlstrom7940e442013-07-12 13:46:57 -0700790/*
791 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
buzbeeb48819d2013-09-14 16:15:25 -0700792 * offset vaddr if pretty-printing, otherise use the standard block
793 * label. The selected label will be used to fix up the case
buzbee252254b2013-09-08 16:20:53 -0700794 * branch table during the assembly phase. All resource flags
795 * are set to prevent code motion. KeyVal is just there for debugging.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700796 */
buzbee0d829482013-10-11 15:24:55 -0700797LIR* Mir2Lir::InsertCaseLabel(DexOffset vaddr, int keyVal) {
buzbee252254b2013-09-08 16:20:53 -0700798 LIR* boundary_lir = &block_label_list_[mir_graph_->FindBlock(vaddr)->id];
buzbeeb48819d2013-09-14 16:15:25 -0700799 LIR* res = boundary_lir;
800 if (cu_->verbose) {
801 // Only pay the expense if we're pretty-printing.
802 LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), ArenaAllocator::kAllocLIR));
803 new_label->dalvik_offset = vaddr;
804 new_label->opcode = kPseudoCaseLabel;
805 new_label->operands[0] = keyVal;
806 new_label->flags.fixup = kFixupLabel;
807 DCHECK(!new_label->flags.use_def_invalid);
808 new_label->u.m.def_mask = ENCODE_ALL;
809 InsertLIRAfter(boundary_lir, new_label);
810 res = new_label;
811 }
812 return res;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813}
814
buzbee0d829482013-10-11 15:24:55 -0700815void Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700816 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700817 DexOffset base_vaddr = tab_rec->vaddr;
818 const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 int entries = table[1];
820 int low_key = s4FromSwitchData(&table[2]);
821 for (int i = 0; i < entries; i++) {
822 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], i + low_key);
823 }
824}
825
buzbee0d829482013-10-11 15:24:55 -0700826void Mir2Lir::MarkSparseCaseLabels(Mir2Lir::SwitchTable* tab_rec) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700827 const uint16_t* table = tab_rec->table;
buzbee0d829482013-10-11 15:24:55 -0700828 DexOffset base_vaddr = tab_rec->vaddr;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700829 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700830 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
831 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700832 for (int i = 0; i < entries; i++) {
833 tab_rec->targets[i] = InsertCaseLabel(base_vaddr + targets[i], keys[i]);
834 }
835}
836
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700837void Mir2Lir::ProcessSwitchTables() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 GrowableArray<SwitchTable*>::Iterator iterator(&switch_tables_);
839 while (true) {
840 Mir2Lir::SwitchTable *tab_rec = iterator.Next();
841 if (tab_rec == NULL) break;
842 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
843 MarkPackedCaseLabels(tab_rec);
844 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
845 MarkSparseCaseLabels(tab_rec);
846 } else {
847 LOG(FATAL) << "Invalid switch table";
848 }
849 }
850}
851
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700852void Mir2Lir::DumpSparseSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700853 /*
854 * Sparse switch data format:
855 * ushort ident = 0x0200 magic value
856 * ushort size number of entries in the table; > 0
857 * int keys[size] keys, sorted low-to-high; 32-bit aligned
858 * int targets[size] branch targets, relative to switch opcode
859 *
860 * Total size is (2+size*4) 16-bit code units.
861 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700862 uint16_t ident = table[0];
863 int entries = table[1];
buzbee0d829482013-10-11 15:24:55 -0700864 const int32_t* keys = reinterpret_cast<const int32_t*>(&table[2]);
865 const int32_t* targets = &keys[entries];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
867 << ", entries: " << std::dec << entries;
868 for (int i = 0; i < entries; i++) {
869 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
870 }
871}
872
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700873void Mir2Lir::DumpPackedSwitchTable(const uint16_t* table) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 /*
875 * Packed switch data format:
876 * ushort ident = 0x0100 magic value
877 * ushort size number of entries in the table
878 * int first_key first (and lowest) switch case value
879 * int targets[size] branch targets, relative to switch opcode
880 *
881 * Total size is (4+size*2) 16-bit code units.
882 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700883 uint16_t ident = table[0];
buzbee0d829482013-10-11 15:24:55 -0700884 const int32_t* targets = reinterpret_cast<const int32_t*>(&table[4]);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700885 int entries = table[1];
886 int low_key = s4FromSwitchData(&table[2]);
887 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
888 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
889 for (int i = 0; i < entries; i++) {
890 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
891 << targets[i];
892 }
893}
894
buzbee252254b2013-09-08 16:20:53 -0700895/* Set up special LIR to mark a Dalvik byte-code instruction start for pretty printing */
buzbee0d829482013-10-11 15:24:55 -0700896void Mir2Lir::MarkBoundary(DexOffset offset, const char* inst_str) {
897 // NOTE: only used for debug listings.
898 NewLIR1(kPseudoDalvikByteCodeBoundary, WrapPointer(ArenaStrdup(inst_str)));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700899}
900
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700901bool Mir2Lir::EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700902 bool is_taken;
903 switch (opcode) {
904 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
905 case Instruction::IF_NE: is_taken = (src1 != src2); break;
906 case Instruction::IF_LT: is_taken = (src1 < src2); break;
907 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
908 case Instruction::IF_GT: is_taken = (src1 > src2); break;
909 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
910 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
911 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
912 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
913 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
914 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
915 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
916 default:
917 LOG(FATAL) << "Unexpected opcode " << opcode;
918 is_taken = false;
919 }
920 return is_taken;
921}
922
923// Convert relation of src1/src2 to src2/src1
924ConditionCode Mir2Lir::FlipComparisonOrder(ConditionCode before) {
925 ConditionCode res;
926 switch (before) {
927 case kCondEq: res = kCondEq; break;
928 case kCondNe: res = kCondNe; break;
929 case kCondLt: res = kCondGt; break;
930 case kCondGt: res = kCondLt; break;
931 case kCondLe: res = kCondGe; break;
932 case kCondGe: res = kCondLe; break;
933 default:
934 res = static_cast<ConditionCode>(0);
935 LOG(FATAL) << "Unexpected ccode " << before;
936 }
937 return res;
938}
939
940// TODO: move to mir_to_lir.cc
941Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena)
942 : Backend(arena),
943 literal_list_(NULL),
944 method_literal_list_(NULL),
945 code_literal_list_(NULL),
buzbeeb48819d2013-09-14 16:15:25 -0700946 first_fixup_(NULL),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947 cu_(cu),
948 mir_graph_(mir_graph),
949 switch_tables_(arena, 4, kGrowableArraySwitchTables),
950 fill_array_data_(arena, 4, kGrowableArrayFillArrayData),
951 throw_launchpads_(arena, 2048, kGrowableArrayThrowLaunchPads),
952 suspend_launchpads_(arena, 4, kGrowableArraySuspendLaunchPads),
953 intrinsic_launchpads_(arena, 2048, kGrowableArrayMisc),
buzbeebd663de2013-09-10 15:41:31 -0700954 tempreg_info_(arena, 20, kGrowableArrayMisc),
955 reginfo_map_(arena, 64, kGrowableArrayMisc),
buzbee0d829482013-10-11 15:24:55 -0700956 pointer_storage_(arena, 128, kGrowableArrayMisc),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 data_offset_(0),
958 total_size_(0),
959 block_label_list_(NULL),
960 current_dalvik_offset_(0),
buzbeeb48819d2013-09-14 16:15:25 -0700961 estimated_native_code_size_(0),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700962 reg_pool_(NULL),
963 live_sreg_(0),
964 num_core_spills_(0),
965 num_fp_spills_(0),
966 frame_size_(0),
967 core_spill_mask_(0),
968 fp_spill_mask_(0),
969 first_lir_insn_(NULL),
Vladimir Marko5c96e6b2013-11-14 15:34:17 +0000970 last_lir_insn_(NULL),
971 inliner_(nullptr) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700972 promotion_map_ = static_cast<PromotionMap*>
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700973 (arena_->Alloc((cu_->num_dalvik_registers + cu_->num_compiler_temps + 1) *
974 sizeof(promotion_map_[0]), ArenaAllocator::kAllocRegAlloc));
buzbee0d829482013-10-11 15:24:55 -0700975 // Reserve pointer id 0 for NULL.
976 size_t null_idx = WrapPointer(NULL);
977 DCHECK_EQ(null_idx, 0U);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978}
979
980void Mir2Lir::Materialize() {
buzbeea61f4952013-08-23 14:27:06 -0700981 cu_->NewTimingSplit("RegisterAllocation");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700982 CompilerInitializeRegAlloc(); // Needs to happen after SSA naming
983
984 /* Allocate Registers using simple local allocation scheme */
985 SimpleRegAlloc();
986
buzbee479f83c2013-07-19 10:58:21 -0700987 if (mir_graph_->IsSpecialCase()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 /*
989 * Custom codegen for special cases. If for any reason the
990 * special codegen doesn't succeed, first_lir_insn_ will
991 * set to NULL;
992 */
buzbeea61f4952013-08-23 14:27:06 -0700993 cu_->NewTimingSplit("SpecialMIR2LIR");
buzbee479f83c2013-07-19 10:58:21 -0700994 SpecialMIR2LIR(mir_graph_->GetSpecialCase());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700995 }
996
997 /* Convert MIR to LIR, etc. */
998 if (first_lir_insn_ == NULL) {
999 MethodMIR2LIR();
1000 }
1001
1002 /* Method is not empty */
1003 if (first_lir_insn_) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001004 // mark the targets of switch statement case labels
1005 ProcessSwitchTables();
1006
1007 /* Convert LIR into machine code. */
1008 AssembleLIR();
1009
1010 if (cu_->verbose) {
1011 CodegenDump();
1012 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 }
Brian Carlstrom7940e442013-07-12 13:46:57 -07001014}
1015
1016CompiledMethod* Mir2Lir::GetCompiledMethod() {
1017 // Combine vmap tables - core regs, then fp regs - into vmap_table
Ian Rogers96faf5b2013-08-09 22:05:32 -07001018 std::vector<uint16_t> raw_vmap_table;
Brian Carlstrom7940e442013-07-12 13:46:57 -07001019 // Core regs may have been inserted out of order - sort first
1020 std::sort(core_vmap_table_.begin(), core_vmap_table_.end());
Mathieu Chartier193bad92013-08-29 18:46:00 -07001021 for (size_t i = 0 ; i < core_vmap_table_.size(); ++i) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001022 // Copy, stripping out the phys register sort key
Ian Rogers96faf5b2013-08-09 22:05:32 -07001023 raw_vmap_table.push_back(~(-1 << VREG_NUM_WIDTH) & core_vmap_table_[i]);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001024 }
1025 // If we have a frame, push a marker to take place of lr
1026 if (frame_size_ > 0) {
Ian Rogers96faf5b2013-08-09 22:05:32 -07001027 raw_vmap_table.push_back(INVALID_VREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001028 } else {
1029 DCHECK_EQ(__builtin_popcount(core_spill_mask_), 0);
1030 DCHECK_EQ(__builtin_popcount(fp_spill_mask_), 0);
1031 }
1032 // Combine vmap tables - core regs, then fp regs. fp regs already sorted
1033 for (uint32_t i = 0; i < fp_vmap_table_.size(); i++) {
Ian Rogers96faf5b2013-08-09 22:05:32 -07001034 raw_vmap_table.push_back(fp_vmap_table_[i]);
1035 }
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001036 Leb128EncodingVector vmap_encoder;
Ian Rogers96faf5b2013-08-09 22:05:32 -07001037 // Prefix the encoded data with its size.
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001038 vmap_encoder.PushBackUnsigned(raw_vmap_table.size());
Mathieu Chartier193bad92013-08-29 18:46:00 -07001039 for (uint16_t cur : raw_vmap_table) {
Vladimir Marko1e6cb632013-11-28 16:27:29 +00001040 vmap_encoder.PushBackUnsigned(cur);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001041 }
1042 CompiledMethod* result =
Mathieu Chartier193bad92013-08-29 18:46:00 -07001043 new CompiledMethod(*cu_->compiler_driver, cu_->instruction_set, code_buffer_, frame_size_,
1044 core_spill_mask_, fp_spill_mask_, encoded_mapping_table_.GetData(),
1045 vmap_encoder.GetData(), native_gc_map_);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001046 return result;
1047}
1048
1049int Mir2Lir::ComputeFrameSize() {
1050 /* Figure out the frame size */
1051 static const uint32_t kAlignMask = kStackAlignment - 1;
1052 uint32_t size = (num_core_spills_ + num_fp_spills_ +
1053 1 /* filler word */ + cu_->num_regs + cu_->num_outs +
1054 cu_->num_compiler_temps + 1 /* cur_method* */)
1055 * sizeof(uint32_t);
1056 /* Align and set */
1057 return (size + kAlignMask) & ~(kAlignMask);
1058}
1059
1060/*
1061 * Append an LIR instruction to the LIR list maintained by a compilation
1062 * unit
1063 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001064void Mir2Lir::AppendLIR(LIR* lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001065 if (first_lir_insn_ == NULL) {
1066 DCHECK(last_lir_insn_ == NULL);
1067 last_lir_insn_ = first_lir_insn_ = lir;
1068 lir->prev = lir->next = NULL;
1069 } else {
1070 last_lir_insn_->next = lir;
1071 lir->prev = last_lir_insn_;
1072 lir->next = NULL;
1073 last_lir_insn_ = lir;
1074 }
1075}
1076
1077/*
1078 * Insert an LIR instruction before the current instruction, which cannot be the
1079 * first instruction.
1080 *
1081 * prev_lir <-> new_lir <-> current_lir
1082 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001083void Mir2Lir::InsertLIRBefore(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001084 DCHECK(current_lir->prev != NULL);
1085 LIR *prev_lir = current_lir->prev;
1086
1087 prev_lir->next = new_lir;
1088 new_lir->prev = prev_lir;
1089 new_lir->next = current_lir;
1090 current_lir->prev = new_lir;
1091}
1092
1093/*
1094 * Insert an LIR instruction after the current instruction, which cannot be the
1095 * first instruction.
1096 *
1097 * current_lir -> new_lir -> old_next
1098 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -07001099void Mir2Lir::InsertLIRAfter(LIR* current_lir, LIR* new_lir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -07001100 new_lir->prev = current_lir;
1101 new_lir->next = current_lir->next;
1102 current_lir->next = new_lir;
1103 new_lir->next->prev = new_lir;
1104}
1105
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001106} // namespace art