blob: 110146fbb44cd8f55443f1f46916972fb98a70af [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
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
buzbee395116c2013-02-27 14:30:25 -080017#include "compiler/dex/compiler_internals.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070018#include "gc_map.h"
19#include "verifier/dex_gc_map.h"
20#include "verifier/method_verifier.h"
buzbee1bc37c62012-11-20 13:35:41 -080021#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080022#include "codegen_util.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070023
buzbeee3acd072012-02-25 17:03:10 -080024namespace art {
25
buzbee4ef3e452012-12-14 13:35:28 -080026bool IsInexpensiveConstant(CompilationUnit* cu, RegLocation rl_src)
27{
28 bool res = false;
29 if (rl_src.is_const) {
30 if (rl_src.wide) {
31 if (rl_src.fp) {
32 res = cu->cg->InexpensiveConstantDouble(ConstantValueWide(cu, rl_src));
33 } else {
34 res = cu->cg->InexpensiveConstantLong(ConstantValueWide(cu, rl_src));
35 }
36 } else {
37 if (rl_src.fp) {
38 res = cu->cg->InexpensiveConstantFloat(ConstantValue(cu, rl_src));
39 } else {
40 res = cu->cg->InexpensiveConstantInt(ConstantValue(cu, rl_src));
41 }
42 }
43 }
44 return res;
45}
46
buzbee02031b12012-11-23 09:41:35 -080047void MarkSafepointPC(CompilationUnit* cu, LIR* inst)
48{
49 inst->def_mask = ENCODE_ALL;
50 LIR* safepoint_pc = NewLIR0(cu, kPseudoSafepointPC);
51 DCHECK_EQ(safepoint_pc->def_mask, ENCODE_ALL);
52}
53
54bool FastInstance(CompilationUnit* cu, uint32_t field_idx,
55 int& field_offset, bool& is_volatile, bool is_put)
56{
Brian Carlstrom265091e2013-01-30 14:08:26 -080057 DexCompilationUnit m_unit(cu);
Ian Rogers1212a022013-03-04 10:48:41 -080058 return cu->compiler_driver->ComputeInstanceFieldInfo(field_idx, &m_unit,
buzbee02031b12012-11-23 09:41:35 -080059 field_offset, is_volatile, is_put);
60}
61
buzbeecbd6d442012-11-17 14:11:25 -080062/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080063void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080064{
buzbeefa57c472012-11-21 12:06:18 -080065 lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -080066}
67
buzbee02031b12012-11-23 09:41:35 -080068void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type)
buzbee31a4a6f2012-02-28 15:36:15 -080069{
buzbeefa57c472012-11-21 12:06:18 -080070 uint64_t *mask_ptr;
buzbeeeaf09bc2012-11-15 14:51:41 -080071 uint64_t mask = ENCODE_MEM;;
buzbee02031b12012-11-23 09:41:35 -080072 Codegen* cg = cu->cg.get();
73 DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080074 if (is_load) {
75 mask_ptr = &lir->use_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070076 } else {
buzbeefa57c472012-11-21 12:06:18 -080077 mask_ptr = &lir->def_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070078 }
79 /* Clear out the memref flags */
buzbeefa57c472012-11-21 12:06:18 -080080 *mask_ptr &= ~mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070081 /* ..and then add back the one we need */
buzbeefa57c472012-11-21 12:06:18 -080082 switch (mem_type) {
Bill Buzbeea114add2012-05-03 15:00:40 -070083 case kLiteral:
buzbeefa57c472012-11-21 12:06:18 -080084 DCHECK(is_load);
85 *mask_ptr |= ENCODE_LITERAL;
Bill Buzbeea114add2012-05-03 15:00:40 -070086 break;
87 case kDalvikReg:
buzbeefa57c472012-11-21 12:06:18 -080088 *mask_ptr |= ENCODE_DALVIK_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -070089 break;
90 case kHeapRef:
buzbeefa57c472012-11-21 12:06:18 -080091 *mask_ptr |= ENCODE_HEAP_REF;
Bill Buzbeea114add2012-05-03 15:00:40 -070092 break;
93 case kMustNotAlias:
94 /* Currently only loads can be marked as kMustNotAlias */
buzbee02031b12012-11-23 09:41:35 -080095 DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080096 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
Bill Buzbeea114add2012-05-03 15:00:40 -070097 break;
98 default:
buzbeefa57c472012-11-21 12:06:18 -080099 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 }
buzbee31a4a6f2012-02-28 15:36:15 -0800101}
102
103/*
Ian Rogersb5d09b22012-03-06 22:14:17 -0800104 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -0800105 */
buzbee02031b12012-11-23 09:41:35 -0800106void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -0800107{
buzbee02031b12012-11-23 09:41:35 -0800108 SetMemRefType(cu, lir, is_load, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800109
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 /*
buzbeefa57c472012-11-21 12:06:18 -0800111 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 * access.
113 */
buzbeefa57c472012-11-21 12:06:18 -0800114 lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -0800115}
116
117/*
118 * Mark the corresponding bit(s).
119 */
buzbeefa57c472012-11-21 12:06:18 -0800120void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -0800121{
buzbee02031b12012-11-23 09:41:35 -0800122 Codegen* cg = cu->cg.get();
123 *mask |= cg->GetRegMaskCommon(cu, reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800124}
125
126/*
127 * Set up the proper fields in the resource mask
128 */
buzbeefa57c472012-11-21 12:06:18 -0800129void SetupResourceMasks(CompilationUnit* cu, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800130{
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 int opcode = lir->opcode;
buzbee02031b12012-11-23 09:41:35 -0800132 Codegen* cg = cu->cg.get();
buzbee31a4a6f2012-02-28 15:36:15 -0800133
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 if (opcode <= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800135 lir->use_mask = lir->def_mask = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 return;
137 }
buzbee31a4a6f2012-02-28 15:36:15 -0800138
buzbee02031b12012-11-23 09:41:35 -0800139 uint64_t flags = cg->GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800140
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 if (flags & NEEDS_FIXUP) {
142 lir->flags.pcRelFixup = true;
143 }
buzbee31a4a6f2012-02-28 15:36:15 -0800144
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 /* Get the starting size of the instruction's template */
buzbee02031b12012-11-23 09:41:35 -0800146 lir->flags.size = cg->GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800147
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 /* Set up the mask for resources that are updated */
149 if (flags & (IS_LOAD | IS_STORE)) {
150 /* Default to heap - will catch specialized classes later */
buzbee02031b12012-11-23 09:41:35 -0800151 SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 }
buzbee31a4a6f2012-02-28 15:36:15 -0800153
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 /*
155 * Conservatively assume the branch here will call out a function that in
156 * turn will trash everything.
157 */
158 if (flags & IS_BRANCH) {
buzbeefa57c472012-11-21 12:06:18 -0800159 lir->def_mask = lir->use_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 return;
161 }
buzbee31a4a6f2012-02-28 15:36:15 -0800162
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 if (flags & REG_DEF0) {
buzbeefa57c472012-11-21 12:06:18 -0800164 SetupRegMask(cu, &lir->def_mask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 }
buzbee31a4a6f2012-02-28 15:36:15 -0800166
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 if (flags & REG_DEF1) {
buzbeefa57c472012-11-21 12:06:18 -0800168 SetupRegMask(cu, &lir->def_mask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 }
buzbee31a4a6f2012-02-28 15:36:15 -0800170
buzbee31a4a6f2012-02-28 15:36:15 -0800171
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 if (flags & SETS_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800173 lir->def_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 }
buzbee31a4a6f2012-02-28 15:36:15 -0800175
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
177 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800178
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 for (i = 0; i < 4; i++) {
180 if (flags & (1 << (kRegUse0 + i))) {
buzbeefa57c472012-11-21 12:06:18 -0800181 SetupRegMask(cu, &lir->use_mask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 }
buzbee31a4a6f2012-02-28 15:36:15 -0800183 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 }
buzbee31a4a6f2012-02-28 15:36:15 -0800185
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 if (flags & USES_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800187 lir->use_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 }
buzbee31a4a6f2012-02-28 15:36:15 -0800189
buzbeeb046e162012-10-30 15:48:42 -0700190 // Handle target-specific actions
buzbee02031b12012-11-23 09:41:35 -0800191 cg->SetupTargetResourceMasks(cu, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800192}
193
194/*
buzbee5de34942012-03-01 14:51:57 -0800195 * Debugging macros
196 */
197#define DUMP_RESOURCE_MASK(X)
buzbee5de34942012-03-01 14:51:57 -0800198
199/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800200void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800201{
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 int offset = lir->offset;
203 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800204 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800205 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800206
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 /* Handle pseudo-ops individually, and all regular insns as a group */
208 switch (lir->opcode) {
209 case kPseudoMethodEntry:
210 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800211 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
213 case kPseudoMethodExit:
214 LOG(INFO) << "-------- Method_Exit";
215 break;
216 case kPseudoBarrier:
217 LOG(INFO) << "-------- BARRIER";
218 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 case kPseudoEntryBlock:
220 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
221 break;
222 case kPseudoDalvikByteCodeBoundary:
buzbee4ef3e452012-12-14 13:35:28 -0800223 if (lir->operands[0] == 0) {
224 lir->operands[0] = reinterpret_cast<uintptr_t>("No instruction string");
225 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800227 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 break;
229 case kPseudoExitBlock:
230 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
231 break;
232 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800233 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 << offset << "): .align4";
235 break;
236 case kPseudoEHBlockLabel:
237 LOG(INFO) << "Exception_Handling:";
238 break;
239 case kPseudoTargetLabel:
240 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800241 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 break;
243 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800244 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 break;
246 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800247 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 break;
249 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800250 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 break;
buzbee8320f382012-09-11 16:29:42 -0700252 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800253 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700254 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700255 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800256 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700257 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800259 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 << std::hex << lir->operands[0] << "|" << std::dec <<
261 lir->operands[0];
262 break;
263 default:
buzbeefa57c472012-11-21 12:06:18 -0800264 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 break;
266 } else {
buzbee02031b12012-11-23 09:41:35 -0800267 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
268 lir, base_addr));
269 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
270 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800272 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800274 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 }
276 break;
277 }
buzbee5de34942012-03-01 14:51:57 -0800278
buzbeefa57c472012-11-21 12:06:18 -0800279 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
280 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 }
buzbeefa57c472012-11-21 12:06:18 -0800282 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
283 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 }
buzbee5de34942012-03-01 14:51:57 -0800285}
286
buzbeefa57c472012-11-21 12:06:18 -0800287void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800288{
buzbee02031b12012-11-23 09:41:35 -0800289 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800290 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
291 for (int i = 0; i < num_regs; i++) {
292 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800294 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800295 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800296 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700297
298 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800299 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800301 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 buf3 = "Method*";
303 } else {
buzbeefa57c472012-11-21 12:06:18 -0800304 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 }
306
307 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800308 v_reg_map.core_location == kLocPhysReg ?
309 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
310 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 buf.c_str());
312 }
buzbee5de34942012-03-01 14:51:57 -0800313}
314
Bill Buzbeea5b30242012-09-28 07:19:44 -0700315/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800316static void DumpMappingTable(const char* table_name, const std::string& descriptor,
317 const std::string& name, const std::string& signature,
318 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700319 if (v.size() > 0) {
320 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
321 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
322 std::replace(line.begin(), line.end(), ';', '_');
323 LOG(INFO) << line;
324 for (uint32_t i = 0; i < v.size(); i+=2) {
325 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
326 LOG(INFO) << line;
327 }
328 LOG(INFO) <<" };\n\n";
329 }
330}
331
buzbee5de34942012-03-01 14:51:57 -0800332/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800333void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800334{
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800336 << PrettyMethod(cu->method_idx, *cu->dex_file);
337 LIR* lir_insn;
338 int insns_size = cu->insns_size;
buzbee5de34942012-03-01 14:51:57 -0800339
buzbeefa57c472012-11-21 12:06:18 -0800340 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
341 LOG(INFO) << "Ins : " << cu->num_ins;
342 LOG(INFO) << "Outs : " << cu->num_outs;
343 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
344 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
345 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
346 LOG(INFO) << "Frame size : " << cu->frame_size;
347 LOG(INFO) << "code size is " << cu->total_size <<
348 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800350 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
351 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800352 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800353 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 }
buzbee28c9a832012-11-21 15:39:13 -0800355 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800356 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
357 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
buzbee5de34942012-03-01 14:51:57 -0800359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800361 cu->dex_file->GetMethodId(cu->method_idx);
362 std::string signature(cu->dex_file->GetMethodSignature(method_id));
363 std::string name(cu->dex_file->GetMethodName(method_id));
364 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800365
Bill Buzbeea5b30242012-09-28 07:19:44 -0700366 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800367 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
368 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800369}
370
buzbeea2ebdd72012-03-04 14:57:06 -0800371
buzbeefa57c472012-11-21 12:06:18 -0800372LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800374{
buzbeefa57c472012-11-21 12:06:18 -0800375 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
376 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 insn->opcode = opcode;
378 insn->operands[0] = op0;
379 insn->operands[1] = op1;
380 insn->operands[2] = op2;
381 insn->operands[3] = op3;
382 insn->operands[4] = op4;
383 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800384 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700385 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
386 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800388 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 }
390 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800391}
392
buzbee5de34942012-03-01 14:51:57 -0800393/*
buzbee31a4a6f2012-02-28 15:36:15 -0800394 * The following are building blocks to construct low-level IRs with 0 - 4
395 * operands.
396 */
buzbeefa57c472012-11-21 12:06:18 -0800397LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800398{
buzbee02031b12012-11-23 09:41:35 -0800399 Codegen* cg = cu->cg.get();
400 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
401 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800402 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
403 << cu->current_dalvik_offset;
404 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
405 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700406 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800407}
408
buzbeefa57c472012-11-21 12:06:18 -0800409LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800411{
buzbee02031b12012-11-23 09:41:35 -0800412 Codegen* cg = cu->cg.get();
413 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
414 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800415 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
416 << cu->current_dalvik_offset;
417 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
418 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800420}
421
buzbeefa57c472012-11-21 12:06:18 -0800422LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800424{
buzbee02031b12012-11-23 09:41:35 -0800425 Codegen* cg = cu->cg.get();
426 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
427 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800428 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
429 << cu->current_dalvik_offset;
430 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
431 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800433}
434
buzbeefa57c472012-11-21 12:06:18 -0800435LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800437{
buzbee02031b12012-11-23 09:41:35 -0800438 Codegen* cg = cu->cg.get();
439 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
440 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800441 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
442 << cu->current_dalvik_offset;
443 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
444 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800446}
447
buzbeefa57c472012-11-21 12:06:18 -0800448LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800450{
buzbee02031b12012-11-23 09:41:35 -0800451 Codegen* cg = cu->cg.get();
452 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
453 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800454 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
455 << cu->current_dalvik_offset;
456 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
457 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800459}
buzbee31a4a6f2012-02-28 15:36:15 -0800460
buzbeefa57c472012-11-21 12:06:18 -0800461LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800463{
buzbee02031b12012-11-23 09:41:35 -0800464 Codegen* cg = cu->cg.get();
465 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
466 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800467 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
468 << cu->current_dalvik_offset;
469 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
470 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800472}
473
buzbee31a4a6f2012-02-28 15:36:15 -0800474/*
475 * Search the existing constants in the literal pool for an exact or close match
476 * within specified delta (greater or equal to 0).
477 */
buzbeefa57c472012-11-21 12:06:18 -0800478LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800479{
buzbeefa57c472012-11-21 12:06:18 -0800480 while (data_target) {
481 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
482 return data_target;
483 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 }
485 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800486}
487
488/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800489LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800490{
buzbeefa57c472012-11-21 12:06:18 -0800491 bool lo_match = false;
492 LIR* lo_target = NULL;
493 while (data_target) {
494 if (lo_match && (data_target->operands[0] == val_hi)) {
buzbee4ef3e452012-12-14 13:35:28 -0800495 // Record high word in case we need to expand this later.
496 lo_target->operands[1] = val_hi;
buzbeefa57c472012-11-21 12:06:18 -0800497 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800498 }
buzbeefa57c472012-11-21 12:06:18 -0800499 lo_match = false;
500 if (data_target->operands[0] == val_lo) {
501 lo_match = true;
502 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 }
buzbeefa57c472012-11-21 12:06:18 -0800504 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 }
506 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800507}
508
509/*
510 * The following are building blocks to insert constants into the pool or
511 * instruction streams.
512 */
513
buzbee4ef3e452012-12-14 13:35:28 -0800514/* Add a 32-bit constant to the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800515LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800516{
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800518 if (constant_list_p) {
519 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
520 new_value->operands[0] = value;
521 new_value->next = *constant_list_p;
522 *constant_list_p = new_value;
523 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 }
525 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800526}
527
528/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800529LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
530 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800531{
buzbeefa57c472012-11-21 12:06:18 -0800532 AddWordData(cu, constant_list_p, val_hi);
533 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800534}
535
buzbeeaad94382012-11-21 07:40:50 -0800536static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 buf.push_back( data & 0xff);
538 buf.push_back( (data >> 8) & 0xff);
539 buf.push_back( (data >> 16) & 0xff);
540 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800541}
542
buzbeeaad94382012-11-21 07:40:50 -0800543static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 while (buf.size() < offset) {
545 buf.push_back(0);
546 }
buzbeee3acd072012-02-25 17:03:10 -0800547}
548
549/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800550static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800551{
buzbeefa57c472012-11-21 12:06:18 -0800552 AlignBuffer(cu->code_buffer, cu->data_offset);
553 LIR* data_lir = cu->literal_list;
554 while (data_lir != NULL) {
555 PushWord(cu->code_buffer, data_lir->operands[0]);
556 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 }
558 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800559 data_lir = cu->code_literal_list;
560 while (data_lir != NULL) {
561 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800562 cu->compiler_driver->AddCodePatch(cu->dex_file,
563 cu->method_idx,
564 cu->invoke_type,
565 target,
566 static_cast<InvokeType>(data_lir->operands[1]),
567 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800568 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700569 // unique based on target to ensure code deduplication works
570 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800571 PushWord(cu->code_buffer, unique_patch_value);
572 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700573 }
buzbeefa57c472012-11-21 12:06:18 -0800574 data_lir = cu->method_literal_list;
575 while (data_lir != NULL) {
576 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800577 cu->compiler_driver->AddMethodPatch(cu->dex_file,
578 cu->method_idx,
579 cu->invoke_type,
580 target,
581 static_cast<InvokeType>(data_lir->operands[1]),
582 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800583 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700584 // unique based on target to ensure code deduplication works
585 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800586 PushWord(cu->code_buffer, unique_patch_value);
587 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 }
buzbeee3acd072012-02-25 17:03:10 -0800589}
590
591/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800592static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800593{
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800595 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800597 SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
598 if (tab_rec == NULL) break;
599 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 /*
601 * For Arm, our reference point is the address of the bx
602 * instruction that does the launch, so we have to subtract
603 * the auto pc-advance. For other targets the reference point
604 * is a label, so we can use the offset as-is.
605 */
buzbeefa57c472012-11-21 12:06:18 -0800606 int bx_offset = INVALID_OFFSET;
607 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700608 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800609 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700610 break;
611 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800612 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700613 break;
614 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800615 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700616 break;
buzbeefa57c472012-11-21 12:06:18 -0800617 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700618 }
buzbeefa57c472012-11-21 12:06:18 -0800619 if (cu->verbose) {
620 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800621 }
buzbeefa57c472012-11-21 12:06:18 -0800622 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
623 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
624 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
625 int disp = tab_rec->targets[elems]->offset - bx_offset;
626 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700627 LOG(INFO) << " Case[" << elems << "] key: 0x"
628 << std::hex << keys[elems] << ", disp: 0x"
629 << std::hex << disp;
630 }
buzbeefa57c472012-11-21 12:06:18 -0800631 PushWord(cu->code_buffer, keys[elems]);
632 PushWord(cu->code_buffer,
633 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700634 }
635 } else {
buzbeefa57c472012-11-21 12:06:18 -0800636 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800638 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
639 int disp = tab_rec->targets[elems]->offset - bx_offset;
640 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 LOG(INFO) << " Case[" << elems << "] disp: 0x"
642 << std::hex << disp;
643 }
buzbeefa57c472012-11-21 12:06:18 -0800644 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 }
646 }
647 }
buzbeee3acd072012-02-25 17:03:10 -0800648}
649
650/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800651static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800652{
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800654 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800656 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800657 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800658 if (tab_rec == NULL) break;
659 AlignBuffer(cu->code_buffer, tab_rec->offset);
660 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
661 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
662 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800663 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 }
buzbeee3acd072012-02-25 17:03:10 -0800665}
666
buzbeeaad94382012-11-21 07:40:50 -0800667static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800668{
Bill Buzbeea114add2012-05-03 15:00:40 -0700669 for (;lir != NULL; lir = lir->next) {
670 lir->offset = offset;
671 offset += 4;
672 }
673 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800674}
675
buzbee6459e7c2012-10-02 14:42:41 -0700676// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800677static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700678{
679 bool success = true;
buzbeefa57c472012-11-21 12:06:18 -0800680 for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) {
681 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700682 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800683 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
684 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700685 found = true;
686 break;
687 }
688 }
689 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800690 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700691 success = false;
692 }
693 }
694 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800695 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
696 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
697 if (cu->catches.find(dex_pc) == cu->catches.end()) {
698 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700699 success = false;
700 }
701 }
702 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800703 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
704 LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: "
705 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700706 }
707 return success;
708}
709
buzbeefa57c472012-11-21 12:06:18 -0800710static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800711{
buzbeefa57c472012-11-21 12:06:18 -0800712 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
713 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
714 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
715 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700716 }
buzbeefa57c472012-11-21 12:06:18 -0800717 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
718 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
719 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800720 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 }
buzbeefa57c472012-11-21 12:06:18 -0800722 DCHECK(VerifyCatchEntries(cu));
723 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
724 cu->dex2pcMappingTable.size());
725 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
726 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
727 cu->pc2dexMappingTable.begin(),
728 cu->pc2dexMappingTable.end());
729 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
730 cu->dex2pcMappingTable.begin(),
731 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800732}
733
Ian Rogers0c7abda2012-09-19 13:33:42 -0700734class NativePcToReferenceMapBuilder {
735 public:
736 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
737 size_t entries, uint32_t max_native_offset,
738 size_t references_width) : entries_(entries),
739 references_width_(references_width), in_use_(entries),
740 table_(table) {
741 // Compute width in bytes needed to hold max_native_offset.
742 native_offset_width_ = 0;
743 while (max_native_offset != 0) {
744 native_offset_width_++;
745 max_native_offset >>= 8;
746 }
747 // Resize table and set up header.
748 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700749 CHECK_LT(native_offset_width_, 1U << 3);
750 (*table)[0] = native_offset_width_ & 7;
751 CHECK_LT(references_width_, 1U << 13);
752 (*table)[0] |= (references_width_ << 3) & 0xFF;
753 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700754 CHECK_LT(entries, 1U << 16);
755 (*table)[2] = entries & 0xFF;
756 (*table)[3] = (entries >> 8) & 0xFF;
757 }
758
759 void AddEntry(uint32_t native_offset, const uint8_t* references) {
760 size_t table_index = TableIndex(native_offset);
761 while (in_use_[table_index]) {
762 table_index = (table_index + 1) % entries_;
763 }
764 in_use_[table_index] = true;
765 SetNativeOffset(table_index, native_offset);
766 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
767 SetReferences(table_index, references);
768 }
769
770 private:
771 size_t TableIndex(uint32_t native_offset) {
772 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
773 }
774
775 uint32_t GetNativeOffset(size_t table_index) {
776 uint32_t native_offset = 0;
777 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
778 for (size_t i = 0; i < native_offset_width_; i++) {
779 native_offset |= (*table_)[table_offset + i] << (i * 8);
780 }
781 return native_offset;
782 }
783
784 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
785 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
786 for (size_t i = 0; i < native_offset_width_; i++) {
787 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
788 }
789 }
790
791 void SetReferences(size_t table_index, const uint8_t* references) {
792 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
793 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
794 }
795
796 size_t EntryWidth() const {
797 return native_offset_width_ + references_width_;
798 }
799
800 // Number of entries in the table.
801 const size_t entries_;
802 // Number of bytes used to encode the reference bitmap.
803 const size_t references_width_;
804 // Number of bytes used to encode a native offset.
805 size_t native_offset_width_;
806 // Entries that are in use.
807 std::vector<bool> in_use_;
808 // The table we're building.
809 std::vector<uint8_t>* const table_;
810};
811
buzbeefa57c472012-11-21 12:06:18 -0800812static void CreateNativeGcMap(CompilationUnit* cu) {
813 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700814 uint32_t max_native_offset = 0;
815 for (size_t i = 0; i < mapping_table.size(); i += 2) {
816 uint32_t native_offset = mapping_table[i + 0];
817 if (native_offset > max_native_offset) {
818 max_native_offset = native_offset;
819 }
820 }
Ian Rogers1212a022013-03-04 10:48:41 -0800821 CompilerDriver::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700822 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
823 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
824 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800825 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700826 mapping_table.size() / 2, max_native_offset,
827 dex_gc_map.RegWidth());
828
829 for (size_t i = 0; i < mapping_table.size(); i += 2) {
830 uint32_t native_offset = mapping_table[i + 0];
831 uint32_t dex_pc = mapping_table[i + 1];
832 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700833 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
834 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700835 }
836}
837
buzbeee3acd072012-02-25 17:03:10 -0800838/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800839static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800840{
buzbeefa57c472012-11-21 12:06:18 -0800841 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
842 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
843 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700844 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800845}
846
buzbeefa57c472012-11-21 12:06:18 -0800847static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800848{
Bill Buzbeea114add2012-05-03 15:00:40 -0700849 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800850 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800852 SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
853 if (tab_rec == NULL) break;
854 tab_rec->offset = offset;
855 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
856 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 } else {
buzbeefa57c472012-11-21 12:06:18 -0800858 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700859 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800860 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800861 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 }
863 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800864}
865
buzbeefa57c472012-11-21 12:06:18 -0800866static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800867{
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800869 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700870 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800871 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800872 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800873 if (tab_rec == NULL) break;
874 tab_rec->offset = offset;
875 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 // word align
877 offset = (offset + 3) & ~3;
878 }
879 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800880}
881
buzbeea3a82b22012-11-27 16:09:55 -0800882// LIR offset assignment.
883static int AssignInsnOffsets(CompilationUnit* cu)
884{
885 LIR* lir;
886 int offset = 0;
887
888 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
889 lir->offset = offset;
890 if (lir->opcode >= 0) {
891 if (!lir->flags.is_nop) {
892 offset += lir->flags.size;
893 }
894 } else if (lir->opcode == kPseudoPseudoAlign4) {
895 if (offset & 0x2) {
896 offset += 2;
897 lir->operands[0] = 1;
898 } else {
899 lir->operands[0] = 0;
900 }
901 }
902 /* Pseudo opcodes don't consume space */
903 }
904
905 return offset;
906}
907
buzbeee3acd072012-02-25 17:03:10 -0800908/*
909 * Walk the compilation unit and assign offsets to instructions
910 * and literals and compute the total size of the compiled unit.
911 */
buzbeefa57c472012-11-21 12:06:18 -0800912static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800913{
buzbeea3a82b22012-11-27 16:09:55 -0800914 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800915
Bill Buzbeea114add2012-05-03 15:00:40 -0700916 /* Const values have to be word aligned */
917 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800918
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800920 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800921
buzbeefa57c472012-11-21 12:06:18 -0800922 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800923
buzbeefa57c472012-11-21 12:06:18 -0800924 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800925
buzbeefa57c472012-11-21 12:06:18 -0800926 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800927
buzbeefa57c472012-11-21 12:06:18 -0800928 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800929}
930
931/*
932 * Go over each instruction in the list and calculate the offset from the top
933 * before sending them off to the assembler. If out-of-range branch distance is
934 * seen rearrange the instructions a bit to correct it.
935 */
buzbeefa57c472012-11-21 12:06:18 -0800936void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800937{
buzbee02031b12012-11-23 09:41:35 -0800938 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800939 AssignOffsets(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 /*
941 * Assemble here. Note that we generate code with optimistic assumptions
942 * and if found now to work, we'll have to redo the sequence and retry.
943 */
buzbeee3acd072012-02-25 17:03:10 -0800944
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800946 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 if (res == kSuccess) {
948 break;
949 } else {
buzbeefa57c472012-11-21 12:06:18 -0800950 cu->assembler_retries++;
951 if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) {
952 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700953 LOG(FATAL) << "Assembler error - too many retries";
954 }
955 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800956 AssignOffsets(cu);
957 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800958 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 }
buzbeee3acd072012-02-25 17:03:10 -0800960
Bill Buzbeea114add2012-05-03 15:00:40 -0700961 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800962 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800963
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800965 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800966
Bill Buzbeea114add2012-05-03 15:00:40 -0700967 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800968 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800969
Ian Rogers0c7abda2012-09-19 13:33:42 -0700970 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800971 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700972
buzbeefa57c472012-11-21 12:06:18 -0800973 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800974}
975
buzbee31a4a6f2012-02-28 15:36:15 -0800976/*
977 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
978 * offset vaddr. This label will be used to fix up the case
979 * branch table during the assembly phase. Be sure to set
980 * all resource flags on this to prevent code motion across
981 * target boundaries. KeyVal is just there for debugging.
982 */
buzbeefa57c472012-11-21 12:06:18 -0800983static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800984{
Bill Buzbeea114add2012-05-03 15:00:40 -0700985 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800986 it = cu->boundary_map.find(vaddr);
987 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
989 }
buzbeefa57c472012-11-21 12:06:18 -0800990 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
991 new_label->dalvik_offset = vaddr;
992 new_label->opcode = kPseudoCaseLabel;
993 new_label->operands[0] = keyVal;
994 InsertLIRAfter(it->second, new_label);
995 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -0800996}
997
buzbeefa57c472012-11-21 12:06:18 -0800998static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -0800999{
buzbeefa57c472012-11-21 12:06:18 -08001000 const uint16_t* table = tab_rec->table;
1001 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -08001002 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001004 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001005 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001006 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -07001007 }
buzbee31a4a6f2012-02-28 15:36:15 -08001008}
1009
buzbeefa57c472012-11-21 12:06:18 -08001010static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001011{
buzbeefa57c472012-11-21 12:06:18 -08001012 const uint16_t* table = tab_rec->table;
1013 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001015 const int* keys = reinterpret_cast<const int*>(&table[2]);
1016 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001017 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001018 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 }
buzbee31a4a6f2012-02-28 15:36:15 -08001020}
1021
buzbeefa57c472012-11-21 12:06:18 -08001022void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001023{
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001025 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001027 SwitchTable *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -08001028 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001029 if (tab_rec == NULL) break;
1030 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1031 MarkPackedCaseLabels(cu, tab_rec);
1032 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1033 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001034 } else {
1035 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001036 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 }
buzbee31a4a6f2012-02-28 15:36:15 -08001038}
1039
buzbee52a77fc2012-11-20 19:50:46 -08001040void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001041 /*
1042 * Sparse switch data format:
1043 * ushort ident = 0x0200 magic value
1044 * ushort size number of entries in the table; > 0
1045 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1046 * int targets[size] branch targets, relative to switch opcode
1047 *
1048 * Total size is (2+size*4) 16-bit code units.
1049 */
buzbee31a4a6f2012-02-28 15:36:15 -08001050{
buzbeeeaf09bc2012-11-15 14:51:41 -08001051 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001053 const int* keys = reinterpret_cast<const int*>(&table[2]);
1054 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001055 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1056 << ", entries: " << std::dec << entries;
1057 for (int i = 0; i < entries; i++) {
1058 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1059 }
buzbee31a4a6f2012-02-28 15:36:15 -08001060}
1061
buzbee52a77fc2012-11-20 19:50:46 -08001062void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001063 /*
1064 * Packed switch data format:
1065 * ushort ident = 0x0100 magic value
1066 * ushort size number of entries in the table
1067 * int first_key first (and lowest) switch case value
1068 * int targets[size] branch targets, relative to switch opcode
1069 *
1070 * Total size is (4+size*2) 16-bit code units.
1071 */
buzbee31a4a6f2012-02-28 15:36:15 -08001072{
buzbeeeaf09bc2012-11-15 14:51:41 -08001073 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001074 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001075 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001076 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001077 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001078 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001079 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001080 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001081 << targets[i];
1082 }
buzbee31a4a6f2012-02-28 15:36:15 -08001083}
buzbeee3acd072012-02-25 17:03:10 -08001084
buzbeed1643e42012-09-05 14:06:51 -07001085/*
1086 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001087 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001088 * which we split a single Dalvik instruction, only the first MIR op
1089 * associated with a Dalvik PC should be entered into the map.
1090 */
buzbeefa57c472012-11-21 12:06:18 -08001091LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001092{
buzbeefa57c472012-11-21 12:06:18 -08001093 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1094 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1095 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001096 }
1097 return res;
1098}
buzbeee3acd072012-02-25 17:03:10 -08001099
buzbeee6285f92012-12-06 15:57:46 -08001100bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2)
1101{
1102 bool is_taken;
1103 switch (opcode) {
1104 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
1105 case Instruction::IF_NE: is_taken = (src1 != src2); break;
1106 case Instruction::IF_LT: is_taken = (src1 < src2); break;
1107 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
1108 case Instruction::IF_GT: is_taken = (src1 > src2); break;
1109 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
1110 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
1111 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
1112 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
1113 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
1114 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
1115 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
1116 default:
1117 LOG(FATAL) << "Unexpected opcode " << opcode;
1118 is_taken = false;
1119 }
1120 return is_taken;
1121}
1122
buzbee4ef3e452012-12-14 13:35:28 -08001123// Convert relation of src1/src2 to src2/src1
1124ConditionCode FlipComparisonOrder(ConditionCode before) {
1125 ConditionCode res;
1126 switch (before) {
1127 case kCondEq: res = kCondEq; break;
1128 case kCondNe: res = kCondNe; break;
1129 case kCondLt: res = kCondGt; break;
1130 case kCondGt: res = kCondLt; break;
1131 case kCondLe: res = kCondGe; break;
1132 case kCondGe: res = kCondLe; break;
1133 default:
1134 res = static_cast<ConditionCode>(0);
1135 LOG(FATAL) << "Unexpected ccode " << before;
1136 }
1137 return res;
1138}
1139
buzbeea3a82b22012-11-27 16:09:55 -08001140} // namespace art