blob: a7ca77af3706c39962412164e3ebe3f044ae49dc [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{
57 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +080058 *cu->dex_file, cu->code_item,
59 cu->class_def_idx, cu->method_idx,
60 cu->access_flags);
Ian Rogers1212a022013-03-04 10:48:41 -080061 return cu->compiler_driver->ComputeInstanceFieldInfo(field_idx, &m_unit,
buzbee02031b12012-11-23 09:41:35 -080062 field_offset, is_volatile, is_put);
63}
64
buzbeecbd6d442012-11-17 14:11:25 -080065/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080066void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080067{
buzbeefa57c472012-11-21 12:06:18 -080068 lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -080069}
70
buzbee02031b12012-11-23 09:41:35 -080071void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type)
buzbee31a4a6f2012-02-28 15:36:15 -080072{
buzbeefa57c472012-11-21 12:06:18 -080073 uint64_t *mask_ptr;
buzbeeeaf09bc2012-11-15 14:51:41 -080074 uint64_t mask = ENCODE_MEM;;
buzbee02031b12012-11-23 09:41:35 -080075 Codegen* cg = cu->cg.get();
76 DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080077 if (is_load) {
78 mask_ptr = &lir->use_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070079 } else {
buzbeefa57c472012-11-21 12:06:18 -080080 mask_ptr = &lir->def_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070081 }
82 /* Clear out the memref flags */
buzbeefa57c472012-11-21 12:06:18 -080083 *mask_ptr &= ~mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070084 /* ..and then add back the one we need */
buzbeefa57c472012-11-21 12:06:18 -080085 switch (mem_type) {
Bill Buzbeea114add2012-05-03 15:00:40 -070086 case kLiteral:
buzbeefa57c472012-11-21 12:06:18 -080087 DCHECK(is_load);
88 *mask_ptr |= ENCODE_LITERAL;
Bill Buzbeea114add2012-05-03 15:00:40 -070089 break;
90 case kDalvikReg:
buzbeefa57c472012-11-21 12:06:18 -080091 *mask_ptr |= ENCODE_DALVIK_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -070092 break;
93 case kHeapRef:
buzbeefa57c472012-11-21 12:06:18 -080094 *mask_ptr |= ENCODE_HEAP_REF;
Bill Buzbeea114add2012-05-03 15:00:40 -070095 break;
96 case kMustNotAlias:
97 /* Currently only loads can be marked as kMustNotAlias */
buzbee02031b12012-11-23 09:41:35 -080098 DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080099 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 break;
101 default:
buzbeefa57c472012-11-21 12:06:18 -0800102 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 }
buzbee31a4a6f2012-02-28 15:36:15 -0800104}
105
106/*
Ian Rogersb5d09b22012-03-06 22:14:17 -0800107 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -0800108 */
buzbee02031b12012-11-23 09:41:35 -0800109void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -0800110{
buzbee02031b12012-11-23 09:41:35 -0800111 SetMemRefType(cu, lir, is_load, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800112
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 /*
buzbeefa57c472012-11-21 12:06:18 -0800114 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 * access.
116 */
buzbeefa57c472012-11-21 12:06:18 -0800117 lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -0800118}
119
120/*
121 * Mark the corresponding bit(s).
122 */
buzbeefa57c472012-11-21 12:06:18 -0800123void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -0800124{
buzbee02031b12012-11-23 09:41:35 -0800125 Codegen* cg = cu->cg.get();
126 *mask |= cg->GetRegMaskCommon(cu, reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800127}
128
129/*
130 * Set up the proper fields in the resource mask
131 */
buzbeefa57c472012-11-21 12:06:18 -0800132void SetupResourceMasks(CompilationUnit* cu, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800133{
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 int opcode = lir->opcode;
buzbee02031b12012-11-23 09:41:35 -0800135 Codegen* cg = cu->cg.get();
buzbee31a4a6f2012-02-28 15:36:15 -0800136
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 if (opcode <= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800138 lir->use_mask = lir->def_mask = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 return;
140 }
buzbee31a4a6f2012-02-28 15:36:15 -0800141
buzbee02031b12012-11-23 09:41:35 -0800142 uint64_t flags = cg->GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800143
Bill Buzbeea114add2012-05-03 15:00:40 -0700144 if (flags & NEEDS_FIXUP) {
145 lir->flags.pcRelFixup = true;
146 }
buzbee31a4a6f2012-02-28 15:36:15 -0800147
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 /* Get the starting size of the instruction's template */
buzbee02031b12012-11-23 09:41:35 -0800149 lir->flags.size = cg->GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800150
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 /* Set up the mask for resources that are updated */
152 if (flags & (IS_LOAD | IS_STORE)) {
153 /* Default to heap - will catch specialized classes later */
buzbee02031b12012-11-23 09:41:35 -0800154 SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 }
buzbee31a4a6f2012-02-28 15:36:15 -0800156
Bill Buzbeea114add2012-05-03 15:00:40 -0700157 /*
158 * Conservatively assume the branch here will call out a function that in
159 * turn will trash everything.
160 */
161 if (flags & IS_BRANCH) {
buzbeefa57c472012-11-21 12:06:18 -0800162 lir->def_mask = lir->use_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 return;
164 }
buzbee31a4a6f2012-02-28 15:36:15 -0800165
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 if (flags & REG_DEF0) {
buzbeefa57c472012-11-21 12:06:18 -0800167 SetupRegMask(cu, &lir->def_mask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 }
buzbee31a4a6f2012-02-28 15:36:15 -0800169
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 if (flags & REG_DEF1) {
buzbeefa57c472012-11-21 12:06:18 -0800171 SetupRegMask(cu, &lir->def_mask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 }
buzbee31a4a6f2012-02-28 15:36:15 -0800173
buzbee31a4a6f2012-02-28 15:36:15 -0800174
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 if (flags & SETS_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800176 lir->def_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 }
buzbee31a4a6f2012-02-28 15:36:15 -0800178
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
180 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800181
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 for (i = 0; i < 4; i++) {
183 if (flags & (1 << (kRegUse0 + i))) {
buzbeefa57c472012-11-21 12:06:18 -0800184 SetupRegMask(cu, &lir->use_mask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 }
buzbee31a4a6f2012-02-28 15:36:15 -0800186 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 }
buzbee31a4a6f2012-02-28 15:36:15 -0800188
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 if (flags & USES_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800190 lir->use_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
buzbee31a4a6f2012-02-28 15:36:15 -0800192
buzbeeb046e162012-10-30 15:48:42 -0700193 // Handle target-specific actions
buzbee02031b12012-11-23 09:41:35 -0800194 cg->SetupTargetResourceMasks(cu, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800195}
196
197/*
buzbee5de34942012-03-01 14:51:57 -0800198 * Debugging macros
199 */
200#define DUMP_RESOURCE_MASK(X)
buzbee5de34942012-03-01 14:51:57 -0800201
202/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800203void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800204{
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 int offset = lir->offset;
206 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800207 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800208 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800209
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 /* Handle pseudo-ops individually, and all regular insns as a group */
211 switch (lir->opcode) {
212 case kPseudoMethodEntry:
213 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800214 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 break;
216 case kPseudoMethodExit:
217 LOG(INFO) << "-------- Method_Exit";
218 break;
219 case kPseudoBarrier:
220 LOG(INFO) << "-------- BARRIER";
221 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 case kPseudoEntryBlock:
223 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
224 break;
225 case kPseudoDalvikByteCodeBoundary:
buzbee4ef3e452012-12-14 13:35:28 -0800226 if (lir->operands[0] == 0) {
227 lir->operands[0] = reinterpret_cast<uintptr_t>("No instruction string");
228 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800230 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 break;
232 case kPseudoExitBlock:
233 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
234 break;
235 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800236 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 << offset << "): .align4";
238 break;
239 case kPseudoEHBlockLabel:
240 LOG(INFO) << "Exception_Handling:";
241 break;
242 case kPseudoTargetLabel:
243 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800244 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 break;
246 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800247 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 break;
249 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800250 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 break;
252 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800253 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 break;
buzbee8320f382012-09-11 16:29:42 -0700255 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800256 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700257 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700258 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800259 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700260 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800262 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 << std::hex << lir->operands[0] << "|" << std::dec <<
264 lir->operands[0];
265 break;
266 default:
buzbeefa57c472012-11-21 12:06:18 -0800267 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 break;
269 } else {
buzbee02031b12012-11-23 09:41:35 -0800270 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
271 lir, base_addr));
272 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
273 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800275 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800277 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700278 }
279 break;
280 }
buzbee5de34942012-03-01 14:51:57 -0800281
buzbeefa57c472012-11-21 12:06:18 -0800282 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
283 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 }
buzbeefa57c472012-11-21 12:06:18 -0800285 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
286 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700287 }
buzbee5de34942012-03-01 14:51:57 -0800288}
289
buzbeefa57c472012-11-21 12:06:18 -0800290void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800291{
buzbee02031b12012-11-23 09:41:35 -0800292 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800293 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
294 for (int i = 0; i < num_regs; i++) {
295 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800297 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800298 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800299 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700300
301 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800302 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800304 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 buf3 = "Method*";
306 } else {
buzbeefa57c472012-11-21 12:06:18 -0800307 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700308 }
309
310 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800311 v_reg_map.core_location == kLocPhysReg ?
312 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
313 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 buf.c_str());
315 }
buzbee5de34942012-03-01 14:51:57 -0800316}
317
Bill Buzbeea5b30242012-09-28 07:19:44 -0700318/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800319static void DumpMappingTable(const char* table_name, const std::string& descriptor,
320 const std::string& name, const std::string& signature,
321 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700322 if (v.size() > 0) {
323 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
324 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
325 std::replace(line.begin(), line.end(), ';', '_');
326 LOG(INFO) << line;
327 for (uint32_t i = 0; i < v.size(); i+=2) {
328 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
329 LOG(INFO) << line;
330 }
331 LOG(INFO) <<" };\n\n";
332 }
333}
334
buzbee5de34942012-03-01 14:51:57 -0800335/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800336void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800337{
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800339 << PrettyMethod(cu->method_idx, *cu->dex_file);
340 LIR* lir_insn;
341 int insns_size = cu->insns_size;
buzbee5de34942012-03-01 14:51:57 -0800342
buzbeefa57c472012-11-21 12:06:18 -0800343 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
344 LOG(INFO) << "Ins : " << cu->num_ins;
345 LOG(INFO) << "Outs : " << cu->num_outs;
346 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
347 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
348 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
349 LOG(INFO) << "Frame size : " << cu->frame_size;
350 LOG(INFO) << "code size is " << cu->total_size <<
351 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800353 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
354 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800355 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800356 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 }
buzbee28c9a832012-11-21 15:39:13 -0800358 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800359 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
360 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 }
buzbee5de34942012-03-01 14:51:57 -0800362
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800364 cu->dex_file->GetMethodId(cu->method_idx);
365 std::string signature(cu->dex_file->GetMethodSignature(method_id));
366 std::string name(cu->dex_file->GetMethodName(method_id));
367 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800368
Bill Buzbeea5b30242012-09-28 07:19:44 -0700369 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800370 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
371 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800372}
373
buzbeea2ebdd72012-03-04 14:57:06 -0800374
buzbeefa57c472012-11-21 12:06:18 -0800375LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800377{
buzbeefa57c472012-11-21 12:06:18 -0800378 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
379 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 insn->opcode = opcode;
381 insn->operands[0] = op0;
382 insn->operands[1] = op1;
383 insn->operands[2] = op2;
384 insn->operands[3] = op3;
385 insn->operands[4] = op4;
386 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800387 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700388 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
389 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800391 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 }
393 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800394}
395
buzbee5de34942012-03-01 14:51:57 -0800396/*
buzbee31a4a6f2012-02-28 15:36:15 -0800397 * The following are building blocks to construct low-level IRs with 0 - 4
398 * operands.
399 */
buzbeefa57c472012-11-21 12:06:18 -0800400LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800401{
buzbee02031b12012-11-23 09:41:35 -0800402 Codegen* cg = cu->cg.get();
403 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
404 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800405 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
406 << cu->current_dalvik_offset;
407 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
408 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800410}
411
buzbeefa57c472012-11-21 12:06:18 -0800412LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800414{
buzbee02031b12012-11-23 09:41:35 -0800415 Codegen* cg = cu->cg.get();
416 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
417 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800418 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
419 << cu->current_dalvik_offset;
420 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
421 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800423}
424
buzbeefa57c472012-11-21 12:06:18 -0800425LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800427{
buzbee02031b12012-11-23 09:41:35 -0800428 Codegen* cg = cu->cg.get();
429 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
430 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800431 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
432 << cu->current_dalvik_offset;
433 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
434 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800436}
437
buzbeefa57c472012-11-21 12:06:18 -0800438LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700439 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800440{
buzbee02031b12012-11-23 09:41:35 -0800441 Codegen* cg = cu->cg.get();
442 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
443 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800444 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
445 << cu->current_dalvik_offset;
446 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
447 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800449}
450
buzbeefa57c472012-11-21 12:06:18 -0800451LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800453{
buzbee02031b12012-11-23 09:41:35 -0800454 Codegen* cg = cu->cg.get();
455 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
456 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800457 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
458 << cu->current_dalvik_offset;
459 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
460 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800462}
buzbee31a4a6f2012-02-28 15:36:15 -0800463
buzbeefa57c472012-11-21 12:06:18 -0800464LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700465 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800466{
buzbee02031b12012-11-23 09:41:35 -0800467 Codegen* cg = cu->cg.get();
468 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
469 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800470 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
471 << cu->current_dalvik_offset;
472 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
473 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800475}
476
buzbee31a4a6f2012-02-28 15:36:15 -0800477/*
478 * Search the existing constants in the literal pool for an exact or close match
479 * within specified delta (greater or equal to 0).
480 */
buzbeefa57c472012-11-21 12:06:18 -0800481LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800482{
buzbeefa57c472012-11-21 12:06:18 -0800483 while (data_target) {
484 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
485 return data_target;
486 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
488 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800489}
490
491/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800492LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800493{
buzbeefa57c472012-11-21 12:06:18 -0800494 bool lo_match = false;
495 LIR* lo_target = NULL;
496 while (data_target) {
497 if (lo_match && (data_target->operands[0] == val_hi)) {
buzbee4ef3e452012-12-14 13:35:28 -0800498 // Record high word in case we need to expand this later.
499 lo_target->operands[1] = val_hi;
buzbeefa57c472012-11-21 12:06:18 -0800500 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800501 }
buzbeefa57c472012-11-21 12:06:18 -0800502 lo_match = false;
503 if (data_target->operands[0] == val_lo) {
504 lo_match = true;
505 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 }
buzbeefa57c472012-11-21 12:06:18 -0800507 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 }
509 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800510}
511
512/*
513 * The following are building blocks to insert constants into the pool or
514 * instruction streams.
515 */
516
buzbee4ef3e452012-12-14 13:35:28 -0800517/* Add a 32-bit constant to the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800518LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800519{
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800521 if (constant_list_p) {
522 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
523 new_value->operands[0] = value;
524 new_value->next = *constant_list_p;
525 *constant_list_p = new_value;
526 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700527 }
528 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800529}
530
531/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800532LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
533 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800534{
buzbeefa57c472012-11-21 12:06:18 -0800535 AddWordData(cu, constant_list_p, val_hi);
536 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800537}
538
buzbeeaad94382012-11-21 07:40:50 -0800539static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700540 buf.push_back( data & 0xff);
541 buf.push_back( (data >> 8) & 0xff);
542 buf.push_back( (data >> 16) & 0xff);
543 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800544}
545
buzbeeaad94382012-11-21 07:40:50 -0800546static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700547 while (buf.size() < offset) {
548 buf.push_back(0);
549 }
buzbeee3acd072012-02-25 17:03:10 -0800550}
551
552/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800553static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800554{
buzbeefa57c472012-11-21 12:06:18 -0800555 AlignBuffer(cu->code_buffer, cu->data_offset);
556 LIR* data_lir = cu->literal_list;
557 while (data_lir != NULL) {
558 PushWord(cu->code_buffer, data_lir->operands[0]);
559 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700560 }
561 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800562 data_lir = cu->code_literal_list;
563 while (data_lir != NULL) {
564 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800565 cu->compiler_driver->AddCodePatch(cu->dex_file,
566 cu->method_idx,
567 cu->invoke_type,
568 target,
569 static_cast<InvokeType>(data_lir->operands[1]),
570 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800571 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700572 // unique based on target to ensure code deduplication works
573 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800574 PushWord(cu->code_buffer, unique_patch_value);
575 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700576 }
buzbeefa57c472012-11-21 12:06:18 -0800577 data_lir = cu->method_literal_list;
578 while (data_lir != NULL) {
579 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800580 cu->compiler_driver->AddMethodPatch(cu->dex_file,
581 cu->method_idx,
582 cu->invoke_type,
583 target,
584 static_cast<InvokeType>(data_lir->operands[1]),
585 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800586 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700587 // unique based on target to ensure code deduplication works
588 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800589 PushWord(cu->code_buffer, unique_patch_value);
590 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700591 }
buzbeee3acd072012-02-25 17:03:10 -0800592}
593
594/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800595static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800596{
Bill Buzbeea114add2012-05-03 15:00:40 -0700597 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800598 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800600 SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
601 if (tab_rec == NULL) break;
602 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700603 /*
604 * For Arm, our reference point is the address of the bx
605 * instruction that does the launch, so we have to subtract
606 * the auto pc-advance. For other targets the reference point
607 * is a label, so we can use the offset as-is.
608 */
buzbeefa57c472012-11-21 12:06:18 -0800609 int bx_offset = INVALID_OFFSET;
610 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700611 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800612 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700613 break;
614 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800615 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700616 break;
617 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800618 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700619 break;
buzbeefa57c472012-11-21 12:06:18 -0800620 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700621 }
buzbeefa57c472012-11-21 12:06:18 -0800622 if (cu->verbose) {
623 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800624 }
buzbeefa57c472012-11-21 12:06:18 -0800625 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
626 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
627 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
628 int disp = tab_rec->targets[elems]->offset - bx_offset;
629 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 LOG(INFO) << " Case[" << elems << "] key: 0x"
631 << std::hex << keys[elems] << ", disp: 0x"
632 << std::hex << disp;
633 }
buzbeefa57c472012-11-21 12:06:18 -0800634 PushWord(cu->code_buffer, keys[elems]);
635 PushWord(cu->code_buffer,
636 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 }
638 } else {
buzbeefa57c472012-11-21 12:06:18 -0800639 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800641 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
642 int disp = tab_rec->targets[elems]->offset - bx_offset;
643 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 LOG(INFO) << " Case[" << elems << "] disp: 0x"
645 << std::hex << disp;
646 }
buzbeefa57c472012-11-21 12:06:18 -0800647 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
649 }
650 }
buzbeee3acd072012-02-25 17:03:10 -0800651}
652
653/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800654static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800655{
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800657 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700658 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800659 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800660 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800661 if (tab_rec == NULL) break;
662 AlignBuffer(cu->code_buffer, tab_rec->offset);
663 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
664 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
665 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800666 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 }
buzbeee3acd072012-02-25 17:03:10 -0800668}
669
buzbeeaad94382012-11-21 07:40:50 -0800670static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800671{
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 for (;lir != NULL; lir = lir->next) {
673 lir->offset = offset;
674 offset += 4;
675 }
676 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800677}
678
buzbee6459e7c2012-10-02 14:42:41 -0700679// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800680static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700681{
682 bool success = true;
buzbeefa57c472012-11-21 12:06:18 -0800683 for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) {
684 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700685 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800686 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
687 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700688 found = true;
689 break;
690 }
691 }
692 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800693 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700694 success = false;
695 }
696 }
697 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800698 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
699 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
700 if (cu->catches.find(dex_pc) == cu->catches.end()) {
701 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700702 success = false;
703 }
704 }
705 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800706 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
707 LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: "
708 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700709 }
710 return success;
711}
712
buzbeefa57c472012-11-21 12:06:18 -0800713static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800714{
buzbeefa57c472012-11-21 12:06:18 -0800715 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
716 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
717 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
718 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700719 }
buzbeefa57c472012-11-21 12:06:18 -0800720 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
721 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
722 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800723 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 }
buzbeefa57c472012-11-21 12:06:18 -0800725 DCHECK(VerifyCatchEntries(cu));
726 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
727 cu->dex2pcMappingTable.size());
728 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
729 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
730 cu->pc2dexMappingTable.begin(),
731 cu->pc2dexMappingTable.end());
732 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
733 cu->dex2pcMappingTable.begin(),
734 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800735}
736
Ian Rogers0c7abda2012-09-19 13:33:42 -0700737class NativePcToReferenceMapBuilder {
738 public:
739 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
740 size_t entries, uint32_t max_native_offset,
741 size_t references_width) : entries_(entries),
742 references_width_(references_width), in_use_(entries),
743 table_(table) {
744 // Compute width in bytes needed to hold max_native_offset.
745 native_offset_width_ = 0;
746 while (max_native_offset != 0) {
747 native_offset_width_++;
748 max_native_offset >>= 8;
749 }
750 // Resize table and set up header.
751 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700752 CHECK_LT(native_offset_width_, 1U << 3);
753 (*table)[0] = native_offset_width_ & 7;
754 CHECK_LT(references_width_, 1U << 13);
755 (*table)[0] |= (references_width_ << 3) & 0xFF;
756 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700757 CHECK_LT(entries, 1U << 16);
758 (*table)[2] = entries & 0xFF;
759 (*table)[3] = (entries >> 8) & 0xFF;
760 }
761
762 void AddEntry(uint32_t native_offset, const uint8_t* references) {
763 size_t table_index = TableIndex(native_offset);
764 while (in_use_[table_index]) {
765 table_index = (table_index + 1) % entries_;
766 }
767 in_use_[table_index] = true;
768 SetNativeOffset(table_index, native_offset);
769 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
770 SetReferences(table_index, references);
771 }
772
773 private:
774 size_t TableIndex(uint32_t native_offset) {
775 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
776 }
777
778 uint32_t GetNativeOffset(size_t table_index) {
779 uint32_t native_offset = 0;
780 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
781 for (size_t i = 0; i < native_offset_width_; i++) {
782 native_offset |= (*table_)[table_offset + i] << (i * 8);
783 }
784 return native_offset;
785 }
786
787 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
788 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
789 for (size_t i = 0; i < native_offset_width_; i++) {
790 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
791 }
792 }
793
794 void SetReferences(size_t table_index, const uint8_t* references) {
795 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
796 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
797 }
798
799 size_t EntryWidth() const {
800 return native_offset_width_ + references_width_;
801 }
802
803 // Number of entries in the table.
804 const size_t entries_;
805 // Number of bytes used to encode the reference bitmap.
806 const size_t references_width_;
807 // Number of bytes used to encode a native offset.
808 size_t native_offset_width_;
809 // Entries that are in use.
810 std::vector<bool> in_use_;
811 // The table we're building.
812 std::vector<uint8_t>* const table_;
813};
814
buzbeefa57c472012-11-21 12:06:18 -0800815static void CreateNativeGcMap(CompilationUnit* cu) {
816 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700817 uint32_t max_native_offset = 0;
818 for (size_t i = 0; i < mapping_table.size(); i += 2) {
819 uint32_t native_offset = mapping_table[i + 0];
820 if (native_offset > max_native_offset) {
821 max_native_offset = native_offset;
822 }
823 }
Ian Rogers1212a022013-03-04 10:48:41 -0800824 CompilerDriver::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700825 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
826 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
827 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800828 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700829 mapping_table.size() / 2, max_native_offset,
830 dex_gc_map.RegWidth());
831
832 for (size_t i = 0; i < mapping_table.size(); i += 2) {
833 uint32_t native_offset = mapping_table[i + 0];
834 uint32_t dex_pc = mapping_table[i + 1];
835 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700836 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
837 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700838 }
839}
840
buzbeee3acd072012-02-25 17:03:10 -0800841/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800842static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800843{
buzbeefa57c472012-11-21 12:06:18 -0800844 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
845 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
846 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800848}
849
buzbeefa57c472012-11-21 12:06:18 -0800850static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800851{
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800853 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800855 SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
856 if (tab_rec == NULL) break;
857 tab_rec->offset = offset;
858 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
859 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 } else {
buzbeefa57c472012-11-21 12:06:18 -0800861 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700862 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800863 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800864 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 }
866 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800867}
868
buzbeefa57c472012-11-21 12:06:18 -0800869static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800870{
Bill Buzbeea114add2012-05-03 15:00:40 -0700871 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800872 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800874 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800875 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800876 if (tab_rec == NULL) break;
877 tab_rec->offset = offset;
878 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 // word align
880 offset = (offset + 3) & ~3;
881 }
882 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800883}
884
buzbeea3a82b22012-11-27 16:09:55 -0800885// LIR offset assignment.
886static int AssignInsnOffsets(CompilationUnit* cu)
887{
888 LIR* lir;
889 int offset = 0;
890
891 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
892 lir->offset = offset;
893 if (lir->opcode >= 0) {
894 if (!lir->flags.is_nop) {
895 offset += lir->flags.size;
896 }
897 } else if (lir->opcode == kPseudoPseudoAlign4) {
898 if (offset & 0x2) {
899 offset += 2;
900 lir->operands[0] = 1;
901 } else {
902 lir->operands[0] = 0;
903 }
904 }
905 /* Pseudo opcodes don't consume space */
906 }
907
908 return offset;
909}
910
buzbeee3acd072012-02-25 17:03:10 -0800911/*
912 * Walk the compilation unit and assign offsets to instructions
913 * and literals and compute the total size of the compiled unit.
914 */
buzbeefa57c472012-11-21 12:06:18 -0800915static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800916{
buzbeea3a82b22012-11-27 16:09:55 -0800917 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800918
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 /* Const values have to be word aligned */
920 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800921
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800923 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800924
buzbeefa57c472012-11-21 12:06:18 -0800925 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800926
buzbeefa57c472012-11-21 12:06:18 -0800927 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800928
buzbeefa57c472012-11-21 12:06:18 -0800929 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800930
buzbeefa57c472012-11-21 12:06:18 -0800931 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800932}
933
934/*
935 * Go over each instruction in the list and calculate the offset from the top
936 * before sending them off to the assembler. If out-of-range branch distance is
937 * seen rearrange the instructions a bit to correct it.
938 */
buzbeefa57c472012-11-21 12:06:18 -0800939void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800940{
buzbee02031b12012-11-23 09:41:35 -0800941 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800942 AssignOffsets(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700943 /*
944 * Assemble here. Note that we generate code with optimistic assumptions
945 * and if found now to work, we'll have to redo the sequence and retry.
946 */
buzbeee3acd072012-02-25 17:03:10 -0800947
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800949 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 if (res == kSuccess) {
951 break;
952 } else {
buzbeefa57c472012-11-21 12:06:18 -0800953 cu->assembler_retries++;
954 if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) {
955 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700956 LOG(FATAL) << "Assembler error - too many retries";
957 }
958 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800959 AssignOffsets(cu);
960 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800961 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 }
buzbeee3acd072012-02-25 17:03:10 -0800963
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800965 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800966
Bill Buzbeea114add2012-05-03 15:00:40 -0700967 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800968 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800969
Bill Buzbeea114add2012-05-03 15:00:40 -0700970 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800971 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800972
Ian Rogers0c7abda2012-09-19 13:33:42 -0700973 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800974 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700975
buzbeefa57c472012-11-21 12:06:18 -0800976 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800977}
978
buzbee31a4a6f2012-02-28 15:36:15 -0800979/*
980 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
981 * offset vaddr. This label will be used to fix up the case
982 * branch table during the assembly phase. Be sure to set
983 * all resource flags on this to prevent code motion across
984 * target boundaries. KeyVal is just there for debugging.
985 */
buzbeefa57c472012-11-21 12:06:18 -0800986static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800987{
Bill Buzbeea114add2012-05-03 15:00:40 -0700988 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800989 it = cu->boundary_map.find(vaddr);
990 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
992 }
buzbeefa57c472012-11-21 12:06:18 -0800993 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
994 new_label->dalvik_offset = vaddr;
995 new_label->opcode = kPseudoCaseLabel;
996 new_label->operands[0] = keyVal;
997 InsertLIRAfter(it->second, new_label);
998 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -0800999}
1000
buzbeefa57c472012-11-21 12:06:18 -08001001static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001002{
buzbeefa57c472012-11-21 12:06:18 -08001003 const uint16_t* table = tab_rec->table;
1004 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -08001005 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001006 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001007 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001009 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 }
buzbee31a4a6f2012-02-28 15:36:15 -08001011}
1012
buzbeefa57c472012-11-21 12:06:18 -08001013static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001014{
buzbeefa57c472012-11-21 12:06:18 -08001015 const uint16_t* table = tab_rec->table;
1016 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001017 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001018 const int* keys = reinterpret_cast<const int*>(&table[2]);
1019 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001020 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001021 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 }
buzbee31a4a6f2012-02-28 15:36:15 -08001023}
1024
buzbeefa57c472012-11-21 12:06:18 -08001025void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001026{
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001028 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001029 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001030 SwitchTable *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -08001031 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001032 if (tab_rec == NULL) break;
1033 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1034 MarkPackedCaseLabels(cu, tab_rec);
1035 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1036 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001037 } else {
1038 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001039 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 }
buzbee31a4a6f2012-02-28 15:36:15 -08001041}
1042
buzbee52a77fc2012-11-20 19:50:46 -08001043void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 /*
1045 * Sparse switch data format:
1046 * ushort ident = 0x0200 magic value
1047 * ushort size number of entries in the table; > 0
1048 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1049 * int targets[size] branch targets, relative to switch opcode
1050 *
1051 * Total size is (2+size*4) 16-bit code units.
1052 */
buzbee31a4a6f2012-02-28 15:36:15 -08001053{
buzbeeeaf09bc2012-11-15 14:51:41 -08001054 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001055 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001056 const int* keys = reinterpret_cast<const int*>(&table[2]);
1057 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1059 << ", entries: " << std::dec << entries;
1060 for (int i = 0; i < entries; i++) {
1061 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1062 }
buzbee31a4a6f2012-02-28 15:36:15 -08001063}
1064
buzbee52a77fc2012-11-20 19:50:46 -08001065void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 /*
1067 * Packed switch data format:
1068 * ushort ident = 0x0100 magic value
1069 * ushort size number of entries in the table
1070 * int first_key first (and lowest) switch case value
1071 * int targets[size] branch targets, relative to switch opcode
1072 *
1073 * Total size is (4+size*2) 16-bit code units.
1074 */
buzbee31a4a6f2012-02-28 15:36:15 -08001075{
buzbeeeaf09bc2012-11-15 14:51:41 -08001076 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001077 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001079 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001080 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001081 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001083 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001084 << targets[i];
1085 }
buzbee31a4a6f2012-02-28 15:36:15 -08001086}
buzbeee3acd072012-02-25 17:03:10 -08001087
buzbeed1643e42012-09-05 14:06:51 -07001088/*
1089 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001090 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001091 * which we split a single Dalvik instruction, only the first MIR op
1092 * associated with a Dalvik PC should be entered into the map.
1093 */
buzbeefa57c472012-11-21 12:06:18 -08001094LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001095{
buzbeefa57c472012-11-21 12:06:18 -08001096 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1097 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1098 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001099 }
1100 return res;
1101}
buzbeee3acd072012-02-25 17:03:10 -08001102
buzbeee6285f92012-12-06 15:57:46 -08001103bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2)
1104{
1105 bool is_taken;
1106 switch (opcode) {
1107 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
1108 case Instruction::IF_NE: is_taken = (src1 != src2); break;
1109 case Instruction::IF_LT: is_taken = (src1 < src2); break;
1110 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
1111 case Instruction::IF_GT: is_taken = (src1 > src2); break;
1112 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
1113 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
1114 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
1115 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
1116 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
1117 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
1118 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
1119 default:
1120 LOG(FATAL) << "Unexpected opcode " << opcode;
1121 is_taken = false;
1122 }
1123 return is_taken;
1124}
1125
buzbee4ef3e452012-12-14 13:35:28 -08001126// Convert relation of src1/src2 to src2/src1
1127ConditionCode FlipComparisonOrder(ConditionCode before) {
1128 ConditionCode res;
1129 switch (before) {
1130 case kCondEq: res = kCondEq; break;
1131 case kCondNe: res = kCondNe; break;
1132 case kCondLt: res = kCondGt; break;
1133 case kCondGt: res = kCondLt; break;
1134 case kCondLe: res = kCondGe; break;
1135 case kCondGe: res = kCondLe; break;
1136 default:
1137 res = static_cast<ConditionCode>(0);
1138 LOG(FATAL) << "Unexpected ccode " << before;
1139 }
1140 return res;
1141}
1142
buzbeea3a82b22012-11-27 16:09:55 -08001143} // namespace art