blob: 24955f68b68f6ab6d8068205a3931625f07c9c33 [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 Rogers4f6ad8a2013-03-18 15:27:28 -070018#include "dex_file-inl.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070019#include "gc_map.h"
20#include "verifier/dex_gc_map.h"
21#include "verifier/method_verifier.h"
buzbee1bc37c62012-11-20 13:35:41 -080022#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080023#include "codegen_util.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070024
buzbeee3acd072012-02-25 17:03:10 -080025namespace art {
26
buzbee4ef3e452012-12-14 13:35:28 -080027bool IsInexpensiveConstant(CompilationUnit* cu, RegLocation rl_src)
28{
29 bool res = false;
30 if (rl_src.is_const) {
31 if (rl_src.wide) {
32 if (rl_src.fp) {
33 res = cu->cg->InexpensiveConstantDouble(ConstantValueWide(cu, rl_src));
34 } else {
35 res = cu->cg->InexpensiveConstantLong(ConstantValueWide(cu, rl_src));
36 }
37 } else {
38 if (rl_src.fp) {
39 res = cu->cg->InexpensiveConstantFloat(ConstantValue(cu, rl_src));
40 } else {
41 res = cu->cg->InexpensiveConstantInt(ConstantValue(cu, rl_src));
42 }
43 }
44 }
45 return res;
46}
47
buzbee02031b12012-11-23 09:41:35 -080048void MarkSafepointPC(CompilationUnit* cu, LIR* inst)
49{
50 inst->def_mask = ENCODE_ALL;
51 LIR* safepoint_pc = NewLIR0(cu, kPseudoSafepointPC);
52 DCHECK_EQ(safepoint_pc->def_mask, ENCODE_ALL);
53}
54
55bool FastInstance(CompilationUnit* cu, uint32_t field_idx,
56 int& field_offset, bool& is_volatile, bool is_put)
57{
Brian Carlstrom265091e2013-01-30 14:08:26 -080058 DexCompilationUnit m_unit(cu);
Ian Rogers1212a022013-03-04 10:48:41 -080059 return cu->compiler_driver->ComputeInstanceFieldInfo(field_idx, &m_unit,
buzbee02031b12012-11-23 09:41:35 -080060 field_offset, is_volatile, is_put);
61}
62
buzbeecbd6d442012-11-17 14:11:25 -080063/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080064void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080065{
buzbeefa57c472012-11-21 12:06:18 -080066 lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -080067}
68
buzbee02031b12012-11-23 09:41:35 -080069void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type)
buzbee31a4a6f2012-02-28 15:36:15 -080070{
buzbeefa57c472012-11-21 12:06:18 -080071 uint64_t *mask_ptr;
buzbeeeaf09bc2012-11-15 14:51:41 -080072 uint64_t mask = ENCODE_MEM;;
buzbee02031b12012-11-23 09:41:35 -080073 Codegen* cg = cu->cg.get();
74 DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080075 if (is_load) {
76 mask_ptr = &lir->use_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070077 } else {
buzbeefa57c472012-11-21 12:06:18 -080078 mask_ptr = &lir->def_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070079 }
80 /* Clear out the memref flags */
buzbeefa57c472012-11-21 12:06:18 -080081 *mask_ptr &= ~mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070082 /* ..and then add back the one we need */
buzbeefa57c472012-11-21 12:06:18 -080083 switch (mem_type) {
Bill Buzbeea114add2012-05-03 15:00:40 -070084 case kLiteral:
buzbeefa57c472012-11-21 12:06:18 -080085 DCHECK(is_load);
86 *mask_ptr |= ENCODE_LITERAL;
Bill Buzbeea114add2012-05-03 15:00:40 -070087 break;
88 case kDalvikReg:
buzbeefa57c472012-11-21 12:06:18 -080089 *mask_ptr |= ENCODE_DALVIK_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -070090 break;
91 case kHeapRef:
buzbeefa57c472012-11-21 12:06:18 -080092 *mask_ptr |= ENCODE_HEAP_REF;
Bill Buzbeea114add2012-05-03 15:00:40 -070093 break;
94 case kMustNotAlias:
95 /* Currently only loads can be marked as kMustNotAlias */
buzbee02031b12012-11-23 09:41:35 -080096 DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080097 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
Bill Buzbeea114add2012-05-03 15:00:40 -070098 break;
99 default:
buzbeefa57c472012-11-21 12:06:18 -0800100 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700101 }
buzbee31a4a6f2012-02-28 15:36:15 -0800102}
103
104/*
Ian Rogersb5d09b22012-03-06 22:14:17 -0800105 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -0800106 */
buzbee02031b12012-11-23 09:41:35 -0800107void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -0800108{
buzbee02031b12012-11-23 09:41:35 -0800109 SetMemRefType(cu, lir, is_load, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800110
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 /*
buzbeefa57c472012-11-21 12:06:18 -0800112 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 * access.
114 */
buzbeefa57c472012-11-21 12:06:18 -0800115 lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -0800116}
117
118/*
119 * Mark the corresponding bit(s).
120 */
buzbeefa57c472012-11-21 12:06:18 -0800121void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -0800122{
buzbee02031b12012-11-23 09:41:35 -0800123 Codegen* cg = cu->cg.get();
124 *mask |= cg->GetRegMaskCommon(cu, reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800125}
126
127/*
128 * Set up the proper fields in the resource mask
129 */
buzbeefa57c472012-11-21 12:06:18 -0800130void SetupResourceMasks(CompilationUnit* cu, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800131{
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 int opcode = lir->opcode;
buzbee02031b12012-11-23 09:41:35 -0800133 Codegen* cg = cu->cg.get();
buzbee31a4a6f2012-02-28 15:36:15 -0800134
Bill Buzbeea114add2012-05-03 15:00:40 -0700135 if (opcode <= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800136 lir->use_mask = lir->def_mask = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 return;
138 }
buzbee31a4a6f2012-02-28 15:36:15 -0800139
buzbee02031b12012-11-23 09:41:35 -0800140 uint64_t flags = cg->GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800141
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 if (flags & NEEDS_FIXUP) {
143 lir->flags.pcRelFixup = true;
144 }
buzbee31a4a6f2012-02-28 15:36:15 -0800145
Bill Buzbeea114add2012-05-03 15:00:40 -0700146 /* Get the starting size of the instruction's template */
buzbee02031b12012-11-23 09:41:35 -0800147 lir->flags.size = cg->GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800148
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 /* Set up the mask for resources that are updated */
150 if (flags & (IS_LOAD | IS_STORE)) {
151 /* Default to heap - will catch specialized classes later */
buzbee02031b12012-11-23 09:41:35 -0800152 SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700153 }
buzbee31a4a6f2012-02-28 15:36:15 -0800154
Bill Buzbeea114add2012-05-03 15:00:40 -0700155 /*
156 * Conservatively assume the branch here will call out a function that in
157 * turn will trash everything.
158 */
159 if (flags & IS_BRANCH) {
buzbeefa57c472012-11-21 12:06:18 -0800160 lir->def_mask = lir->use_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 return;
162 }
buzbee31a4a6f2012-02-28 15:36:15 -0800163
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 if (flags & REG_DEF0) {
buzbeefa57c472012-11-21 12:06:18 -0800165 SetupRegMask(cu, &lir->def_mask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 }
buzbee31a4a6f2012-02-28 15:36:15 -0800167
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 if (flags & REG_DEF1) {
buzbeefa57c472012-11-21 12:06:18 -0800169 SetupRegMask(cu, &lir->def_mask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 }
buzbee31a4a6f2012-02-28 15:36:15 -0800171
buzbee31a4a6f2012-02-28 15:36:15 -0800172
Bill Buzbeea114add2012-05-03 15:00:40 -0700173 if (flags & SETS_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800174 lir->def_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700175 }
buzbee31a4a6f2012-02-28 15:36:15 -0800176
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
178 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800179
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 for (i = 0; i < 4; i++) {
181 if (flags & (1 << (kRegUse0 + i))) {
buzbeefa57c472012-11-21 12:06:18 -0800182 SetupRegMask(cu, &lir->use_mask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700183 }
buzbee31a4a6f2012-02-28 15:36:15 -0800184 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 }
buzbee31a4a6f2012-02-28 15:36:15 -0800186
Bill Buzbeea114add2012-05-03 15:00:40 -0700187 if (flags & USES_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800188 lir->use_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 }
buzbee31a4a6f2012-02-28 15:36:15 -0800190
buzbeeb046e162012-10-30 15:48:42 -0700191 // Handle target-specific actions
buzbee02031b12012-11-23 09:41:35 -0800192 cg->SetupTargetResourceMasks(cu, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800193}
194
195/*
buzbee5de34942012-03-01 14:51:57 -0800196 * Debugging macros
197 */
198#define DUMP_RESOURCE_MASK(X)
buzbee5de34942012-03-01 14:51:57 -0800199
200/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800201void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800202{
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 int offset = lir->offset;
204 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800205 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800206 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800207
Bill Buzbeea114add2012-05-03 15:00:40 -0700208 /* Handle pseudo-ops individually, and all regular insns as a group */
209 switch (lir->opcode) {
210 case kPseudoMethodEntry:
211 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800212 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 break;
214 case kPseudoMethodExit:
215 LOG(INFO) << "-------- Method_Exit";
216 break;
217 case kPseudoBarrier:
218 LOG(INFO) << "-------- BARRIER";
219 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 case kPseudoEntryBlock:
221 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
222 break;
223 case kPseudoDalvikByteCodeBoundary:
buzbee4ef3e452012-12-14 13:35:28 -0800224 if (lir->operands[0] == 0) {
225 lir->operands[0] = reinterpret_cast<uintptr_t>("No instruction string");
226 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800228 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700229 break;
230 case kPseudoExitBlock:
231 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
232 break;
233 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800234 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 << offset << "): .align4";
236 break;
237 case kPseudoEHBlockLabel:
238 LOG(INFO) << "Exception_Handling:";
239 break;
240 case kPseudoTargetLabel:
241 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800242 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 break;
244 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800245 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 break;
247 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800248 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 break;
250 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800251 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 break;
buzbee8320f382012-09-11 16:29:42 -0700253 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800254 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700255 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700256 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800257 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700258 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800260 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 << std::hex << lir->operands[0] << "|" << std::dec <<
262 lir->operands[0];
263 break;
264 default:
buzbeefa57c472012-11-21 12:06:18 -0800265 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 break;
267 } else {
buzbee02031b12012-11-23 09:41:35 -0800268 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
269 lir, base_addr));
270 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
271 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800273 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800275 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700276 }
277 break;
278 }
buzbee5de34942012-03-01 14:51:57 -0800279
buzbeefa57c472012-11-21 12:06:18 -0800280 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
281 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700282 }
buzbeefa57c472012-11-21 12:06:18 -0800283 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
284 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700285 }
buzbee5de34942012-03-01 14:51:57 -0800286}
287
buzbeefa57c472012-11-21 12:06:18 -0800288void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800289{
buzbee02031b12012-11-23 09:41:35 -0800290 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800291 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
292 for (int i = 0; i < num_regs; i++) {
293 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700294 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800295 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800296 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800297 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700298
299 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800300 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800302 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 buf3 = "Method*";
304 } else {
buzbeefa57c472012-11-21 12:06:18 -0800305 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700306 }
307
308 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800309 v_reg_map.core_location == kLocPhysReg ?
310 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
311 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700312 buf.c_str());
313 }
buzbee5de34942012-03-01 14:51:57 -0800314}
315
Bill Buzbeea5b30242012-09-28 07:19:44 -0700316/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800317static void DumpMappingTable(const char* table_name, const std::string& descriptor,
318 const std::string& name, const std::string& signature,
319 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700320 if (v.size() > 0) {
321 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
322 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
323 std::replace(line.begin(), line.end(), ';', '_');
324 LOG(INFO) << line;
325 for (uint32_t i = 0; i < v.size(); i+=2) {
326 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
327 LOG(INFO) << line;
328 }
329 LOG(INFO) <<" };\n\n";
330 }
331}
332
buzbee5de34942012-03-01 14:51:57 -0800333/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800334void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800335{
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800337 << PrettyMethod(cu->method_idx, *cu->dex_file);
338 LIR* lir_insn;
339 int insns_size = cu->insns_size;
buzbee5de34942012-03-01 14:51:57 -0800340
buzbeefa57c472012-11-21 12:06:18 -0800341 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
342 LOG(INFO) << "Ins : " << cu->num_ins;
343 LOG(INFO) << "Outs : " << cu->num_outs;
344 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
345 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
346 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
347 LOG(INFO) << "Frame size : " << cu->frame_size;
348 LOG(INFO) << "code size is " << cu->total_size <<
349 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800351 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
352 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800353 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800354 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700355 }
buzbee28c9a832012-11-21 15:39:13 -0800356 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800357 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
358 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700359 }
buzbee5de34942012-03-01 14:51:57 -0800360
Bill Buzbeea114add2012-05-03 15:00:40 -0700361 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800362 cu->dex_file->GetMethodId(cu->method_idx);
363 std::string signature(cu->dex_file->GetMethodSignature(method_id));
364 std::string name(cu->dex_file->GetMethodName(method_id));
365 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800366
Bill Buzbeea5b30242012-09-28 07:19:44 -0700367 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800368 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
369 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800370}
371
buzbeea2ebdd72012-03-04 14:57:06 -0800372
buzbeefa57c472012-11-21 12:06:18 -0800373LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700374 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800375{
buzbeefa57c472012-11-21 12:06:18 -0800376 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
377 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 insn->opcode = opcode;
379 insn->operands[0] = op0;
380 insn->operands[1] = op1;
381 insn->operands[2] = op2;
382 insn->operands[3] = op3;
383 insn->operands[4] = op4;
384 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800385 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700386 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
387 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800389 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700390 }
391 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800392}
393
buzbee5de34942012-03-01 14:51:57 -0800394/*
buzbee31a4a6f2012-02-28 15:36:15 -0800395 * The following are building blocks to construct low-level IRs with 0 - 4
396 * operands.
397 */
buzbeefa57c472012-11-21 12:06:18 -0800398LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800399{
buzbee02031b12012-11-23 09:41:35 -0800400 Codegen* cg = cu->cg.get();
401 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
402 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800403 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
404 << cu->current_dalvik_offset;
405 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
406 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700407 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800408}
409
buzbeefa57c472012-11-21 12:06:18 -0800410LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800412{
buzbee02031b12012-11-23 09:41:35 -0800413 Codegen* cg = cu->cg.get();
414 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
415 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800416 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
417 << cu->current_dalvik_offset;
418 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
419 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800421}
422
buzbeefa57c472012-11-21 12:06:18 -0800423LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800425{
buzbee02031b12012-11-23 09:41:35 -0800426 Codegen* cg = cu->cg.get();
427 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
428 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800429 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
430 << cu->current_dalvik_offset;
431 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
432 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700433 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800434}
435
buzbeefa57c472012-11-21 12:06:18 -0800436LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800438{
buzbee02031b12012-11-23 09:41:35 -0800439 Codegen* cg = cu->cg.get();
440 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
441 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800442 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
443 << cu->current_dalvik_offset;
444 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
445 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700446 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800447}
448
buzbeefa57c472012-11-21 12:06:18 -0800449LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800451{
buzbee02031b12012-11-23 09:41:35 -0800452 Codegen* cg = cu->cg.get();
453 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
454 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800455 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
456 << cu->current_dalvik_offset;
457 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
458 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800460}
buzbee31a4a6f2012-02-28 15:36:15 -0800461
buzbeefa57c472012-11-21 12:06:18 -0800462LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800464{
buzbee02031b12012-11-23 09:41:35 -0800465 Codegen* cg = cu->cg.get();
466 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
467 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800468 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
469 << cu->current_dalvik_offset;
470 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
471 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800473}
474
buzbee31a4a6f2012-02-28 15:36:15 -0800475/*
476 * Search the existing constants in the literal pool for an exact or close match
477 * within specified delta (greater or equal to 0).
478 */
buzbeefa57c472012-11-21 12:06:18 -0800479LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800480{
buzbeefa57c472012-11-21 12:06:18 -0800481 while (data_target) {
482 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
483 return data_target;
484 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 }
486 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800487}
488
489/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800490LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800491{
buzbeefa57c472012-11-21 12:06:18 -0800492 bool lo_match = false;
493 LIR* lo_target = NULL;
494 while (data_target) {
495 if (lo_match && (data_target->operands[0] == val_hi)) {
buzbee4ef3e452012-12-14 13:35:28 -0800496 // Record high word in case we need to expand this later.
497 lo_target->operands[1] = val_hi;
buzbeefa57c472012-11-21 12:06:18 -0800498 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800499 }
buzbeefa57c472012-11-21 12:06:18 -0800500 lo_match = false;
501 if (data_target->operands[0] == val_lo) {
502 lo_match = true;
503 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 }
buzbeefa57c472012-11-21 12:06:18 -0800505 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 }
507 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800508}
509
510/*
511 * The following are building blocks to insert constants into the pool or
512 * instruction streams.
513 */
514
buzbee4ef3e452012-12-14 13:35:28 -0800515/* Add a 32-bit constant to the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800516LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800517{
Bill Buzbeea114add2012-05-03 15:00:40 -0700518 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800519 if (constant_list_p) {
520 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
521 new_value->operands[0] = value;
522 new_value->next = *constant_list_p;
523 *constant_list_p = new_value;
524 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 }
526 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800527}
528
529/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800530LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
531 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800532{
buzbeefa57c472012-11-21 12:06:18 -0800533 AddWordData(cu, constant_list_p, val_hi);
534 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800535}
536
buzbeeaad94382012-11-21 07:40:50 -0800537static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700538 buf.push_back( data & 0xff);
539 buf.push_back( (data >> 8) & 0xff);
540 buf.push_back( (data >> 16) & 0xff);
541 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800542}
543
buzbeeaad94382012-11-21 07:40:50 -0800544static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 while (buf.size() < offset) {
546 buf.push_back(0);
547 }
buzbeee3acd072012-02-25 17:03:10 -0800548}
549
550/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800551static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800552{
buzbeefa57c472012-11-21 12:06:18 -0800553 AlignBuffer(cu->code_buffer, cu->data_offset);
554 LIR* data_lir = cu->literal_list;
555 while (data_lir != NULL) {
556 PushWord(cu->code_buffer, data_lir->operands[0]);
557 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700558 }
559 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800560 data_lir = cu->code_literal_list;
561 while (data_lir != NULL) {
562 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800563 cu->compiler_driver->AddCodePatch(cu->dex_file,
564 cu->method_idx,
565 cu->invoke_type,
566 target,
567 static_cast<InvokeType>(data_lir->operands[1]),
568 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800569 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700570 // unique based on target to ensure code deduplication works
571 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800572 PushWord(cu->code_buffer, unique_patch_value);
573 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700574 }
buzbeefa57c472012-11-21 12:06:18 -0800575 data_lir = cu->method_literal_list;
576 while (data_lir != NULL) {
577 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800578 cu->compiler_driver->AddMethodPatch(cu->dex_file,
579 cu->method_idx,
580 cu->invoke_type,
581 target,
582 static_cast<InvokeType>(data_lir->operands[1]),
583 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800584 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700585 // unique based on target to ensure code deduplication works
586 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800587 PushWord(cu->code_buffer, unique_patch_value);
588 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 }
buzbeee3acd072012-02-25 17:03:10 -0800590}
591
592/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800593static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800594{
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800596 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700597 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800598 SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
599 if (tab_rec == NULL) break;
600 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 /*
602 * For Arm, our reference point is the address of the bx
603 * instruction that does the launch, so we have to subtract
604 * the auto pc-advance. For other targets the reference point
605 * is a label, so we can use the offset as-is.
606 */
buzbeefa57c472012-11-21 12:06:18 -0800607 int bx_offset = INVALID_OFFSET;
608 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700609 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800610 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700611 break;
612 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800613 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700614 break;
615 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800616 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700617 break;
buzbeefa57c472012-11-21 12:06:18 -0800618 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700619 }
buzbeefa57c472012-11-21 12:06:18 -0800620 if (cu->verbose) {
621 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800622 }
buzbeefa57c472012-11-21 12:06:18 -0800623 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
624 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
625 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
626 int disp = tab_rec->targets[elems]->offset - bx_offset;
627 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 LOG(INFO) << " Case[" << elems << "] key: 0x"
629 << std::hex << keys[elems] << ", disp: 0x"
630 << std::hex << disp;
631 }
buzbeefa57c472012-11-21 12:06:18 -0800632 PushWord(cu->code_buffer, keys[elems]);
633 PushWord(cu->code_buffer,
634 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 }
636 } else {
buzbeefa57c472012-11-21 12:06:18 -0800637 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800639 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
640 int disp = tab_rec->targets[elems]->offset - bx_offset;
641 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 LOG(INFO) << " Case[" << elems << "] disp: 0x"
643 << std::hex << disp;
644 }
buzbeefa57c472012-11-21 12:06:18 -0800645 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 }
647 }
648 }
buzbeee3acd072012-02-25 17:03:10 -0800649}
650
651/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800652static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800653{
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800655 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800657 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800658 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800659 if (tab_rec == NULL) break;
660 AlignBuffer(cu->code_buffer, tab_rec->offset);
661 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
662 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
663 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800664 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 }
buzbeee3acd072012-02-25 17:03:10 -0800666}
667
buzbeeaad94382012-11-21 07:40:50 -0800668static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800669{
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 for (;lir != NULL; lir = lir->next) {
671 lir->offset = offset;
672 offset += 4;
673 }
674 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800675}
676
buzbee6459e7c2012-10-02 14:42:41 -0700677// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800678static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700679{
680 bool success = true;
buzbeefa57c472012-11-21 12:06:18 -0800681 for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) {
682 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700683 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800684 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
685 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700686 found = true;
687 break;
688 }
689 }
690 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800691 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700692 success = false;
693 }
694 }
695 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800696 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
697 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
698 if (cu->catches.find(dex_pc) == cu->catches.end()) {
699 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700700 success = false;
701 }
702 }
703 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800704 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
705 LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: "
706 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700707 }
708 return success;
709}
710
buzbeefa57c472012-11-21 12:06:18 -0800711static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800712{
buzbeefa57c472012-11-21 12:06:18 -0800713 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
714 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
715 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
716 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700717 }
buzbeefa57c472012-11-21 12:06:18 -0800718 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
719 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
720 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800721 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700722 }
buzbeefa57c472012-11-21 12:06:18 -0800723 DCHECK(VerifyCatchEntries(cu));
724 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
725 cu->dex2pcMappingTable.size());
726 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
727 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
728 cu->pc2dexMappingTable.begin(),
729 cu->pc2dexMappingTable.end());
730 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
731 cu->dex2pcMappingTable.begin(),
732 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800733}
734
Ian Rogers0c7abda2012-09-19 13:33:42 -0700735class NativePcToReferenceMapBuilder {
736 public:
737 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
738 size_t entries, uint32_t max_native_offset,
739 size_t references_width) : entries_(entries),
740 references_width_(references_width), in_use_(entries),
741 table_(table) {
742 // Compute width in bytes needed to hold max_native_offset.
743 native_offset_width_ = 0;
744 while (max_native_offset != 0) {
745 native_offset_width_++;
746 max_native_offset >>= 8;
747 }
748 // Resize table and set up header.
749 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700750 CHECK_LT(native_offset_width_, 1U << 3);
751 (*table)[0] = native_offset_width_ & 7;
752 CHECK_LT(references_width_, 1U << 13);
753 (*table)[0] |= (references_width_ << 3) & 0xFF;
754 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700755 CHECK_LT(entries, 1U << 16);
756 (*table)[2] = entries & 0xFF;
757 (*table)[3] = (entries >> 8) & 0xFF;
758 }
759
760 void AddEntry(uint32_t native_offset, const uint8_t* references) {
761 size_t table_index = TableIndex(native_offset);
762 while (in_use_[table_index]) {
763 table_index = (table_index + 1) % entries_;
764 }
765 in_use_[table_index] = true;
766 SetNativeOffset(table_index, native_offset);
767 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
768 SetReferences(table_index, references);
769 }
770
771 private:
772 size_t TableIndex(uint32_t native_offset) {
773 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
774 }
775
776 uint32_t GetNativeOffset(size_t table_index) {
777 uint32_t native_offset = 0;
778 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
779 for (size_t i = 0; i < native_offset_width_; i++) {
780 native_offset |= (*table_)[table_offset + i] << (i * 8);
781 }
782 return native_offset;
783 }
784
785 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
786 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
787 for (size_t i = 0; i < native_offset_width_; i++) {
788 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
789 }
790 }
791
792 void SetReferences(size_t table_index, const uint8_t* references) {
793 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
794 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
795 }
796
797 size_t EntryWidth() const {
798 return native_offset_width_ + references_width_;
799 }
800
801 // Number of entries in the table.
802 const size_t entries_;
803 // Number of bytes used to encode the reference bitmap.
804 const size_t references_width_;
805 // Number of bytes used to encode a native offset.
806 size_t native_offset_width_;
807 // Entries that are in use.
808 std::vector<bool> in_use_;
809 // The table we're building.
810 std::vector<uint8_t>* const table_;
811};
812
buzbeefa57c472012-11-21 12:06:18 -0800813static void CreateNativeGcMap(CompilationUnit* cu) {
814 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700815 uint32_t max_native_offset = 0;
816 for (size_t i = 0; i < mapping_table.size(); i += 2) {
817 uint32_t native_offset = mapping_table[i + 0];
818 if (native_offset > max_native_offset) {
819 max_native_offset = native_offset;
820 }
821 }
Ian Rogers1212a022013-03-04 10:48:41 -0800822 CompilerDriver::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700823 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
824 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
825 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800826 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700827 mapping_table.size() / 2, max_native_offset,
828 dex_gc_map.RegWidth());
829
830 for (size_t i = 0; i < mapping_table.size(); i += 2) {
831 uint32_t native_offset = mapping_table[i + 0];
832 uint32_t dex_pc = mapping_table[i + 1];
833 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700834 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
835 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700836 }
837}
838
buzbeee3acd072012-02-25 17:03:10 -0800839/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800840static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800841{
buzbeefa57c472012-11-21 12:06:18 -0800842 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
843 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
844 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800846}
847
buzbeefa57c472012-11-21 12:06:18 -0800848static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800849{
Bill Buzbeea114add2012-05-03 15:00:40 -0700850 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800851 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700852 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800853 SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
854 if (tab_rec == NULL) break;
855 tab_rec->offset = offset;
856 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
857 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700858 } else {
buzbeefa57c472012-11-21 12:06:18 -0800859 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800861 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800862 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 }
864 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800865}
866
buzbeefa57c472012-11-21 12:06:18 -0800867static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800868{
Bill Buzbeea114add2012-05-03 15:00:40 -0700869 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800870 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700871 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800872 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800873 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800874 if (tab_rec == NULL) break;
875 tab_rec->offset = offset;
876 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700877 // word align
878 offset = (offset + 3) & ~3;
879 }
880 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800881}
882
buzbeea3a82b22012-11-27 16:09:55 -0800883// LIR offset assignment.
884static int AssignInsnOffsets(CompilationUnit* cu)
885{
886 LIR* lir;
887 int offset = 0;
888
889 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
890 lir->offset = offset;
891 if (lir->opcode >= 0) {
892 if (!lir->flags.is_nop) {
893 offset += lir->flags.size;
894 }
895 } else if (lir->opcode == kPseudoPseudoAlign4) {
896 if (offset & 0x2) {
897 offset += 2;
898 lir->operands[0] = 1;
899 } else {
900 lir->operands[0] = 0;
901 }
902 }
903 /* Pseudo opcodes don't consume space */
904 }
905
906 return offset;
907}
908
buzbeee3acd072012-02-25 17:03:10 -0800909/*
910 * Walk the compilation unit and assign offsets to instructions
911 * and literals and compute the total size of the compiled unit.
912 */
buzbeefa57c472012-11-21 12:06:18 -0800913static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800914{
buzbeea3a82b22012-11-27 16:09:55 -0800915 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800916
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 /* Const values have to be word aligned */
918 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800919
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800921 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800922
buzbeefa57c472012-11-21 12:06:18 -0800923 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800924
buzbeefa57c472012-11-21 12:06:18 -0800925 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800926
buzbeefa57c472012-11-21 12:06:18 -0800927 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800928
buzbeefa57c472012-11-21 12:06:18 -0800929 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800930}
931
932/*
933 * Go over each instruction in the list and calculate the offset from the top
934 * before sending them off to the assembler. If out-of-range branch distance is
935 * seen rearrange the instructions a bit to correct it.
936 */
buzbeefa57c472012-11-21 12:06:18 -0800937void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800938{
buzbee02031b12012-11-23 09:41:35 -0800939 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800940 AssignOffsets(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 /*
942 * Assemble here. Note that we generate code with optimistic assumptions
943 * and if found now to work, we'll have to redo the sequence and retry.
944 */
buzbeee3acd072012-02-25 17:03:10 -0800945
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800947 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 if (res == kSuccess) {
949 break;
950 } else {
buzbeefa57c472012-11-21 12:06:18 -0800951 cu->assembler_retries++;
952 if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) {
953 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 LOG(FATAL) << "Assembler error - too many retries";
955 }
956 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800957 AssignOffsets(cu);
958 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800959 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 }
buzbeee3acd072012-02-25 17:03:10 -0800961
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800963 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800964
Bill Buzbeea114add2012-05-03 15:00:40 -0700965 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800966 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800967
Bill Buzbeea114add2012-05-03 15:00:40 -0700968 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800969 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800970
Ian Rogers0c7abda2012-09-19 13:33:42 -0700971 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800972 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700973
buzbeefa57c472012-11-21 12:06:18 -0800974 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800975}
976
buzbee31a4a6f2012-02-28 15:36:15 -0800977/*
978 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
979 * offset vaddr. This label will be used to fix up the case
980 * branch table during the assembly phase. Be sure to set
981 * all resource flags on this to prevent code motion across
982 * target boundaries. KeyVal is just there for debugging.
983 */
buzbeefa57c472012-11-21 12:06:18 -0800984static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800985{
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800987 it = cu->boundary_map.find(vaddr);
988 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
990 }
buzbeefa57c472012-11-21 12:06:18 -0800991 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
992 new_label->dalvik_offset = vaddr;
993 new_label->opcode = kPseudoCaseLabel;
994 new_label->operands[0] = keyVal;
995 InsertLIRAfter(it->second, new_label);
996 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -0800997}
998
buzbeefa57c472012-11-21 12:06:18 -0800999static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001000{
buzbeefa57c472012-11-21 12:06:18 -08001001 const uint16_t* table = tab_rec->table;
1002 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -08001003 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001005 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001006 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001007 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 }
buzbee31a4a6f2012-02-28 15:36:15 -08001009}
1010
buzbeefa57c472012-11-21 12:06:18 -08001011static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001012{
buzbeefa57c472012-11-21 12:06:18 -08001013 const uint16_t* table = tab_rec->table;
1014 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001015 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001016 const int* keys = reinterpret_cast<const int*>(&table[2]);
1017 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001019 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001020 }
buzbee31a4a6f2012-02-28 15:36:15 -08001021}
1022
buzbeefa57c472012-11-21 12:06:18 -08001023void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001024{
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001026 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001028 SwitchTable *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -08001029 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001030 if (tab_rec == NULL) break;
1031 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1032 MarkPackedCaseLabels(cu, tab_rec);
1033 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1034 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001035 } else {
1036 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001037 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001038 }
buzbee31a4a6f2012-02-28 15:36:15 -08001039}
1040
buzbee52a77fc2012-11-20 19:50:46 -08001041void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001042 /*
1043 * Sparse switch data format:
1044 * ushort ident = 0x0200 magic value
1045 * ushort size number of entries in the table; > 0
1046 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1047 * int targets[size] branch targets, relative to switch opcode
1048 *
1049 * Total size is (2+size*4) 16-bit code units.
1050 */
buzbee31a4a6f2012-02-28 15:36:15 -08001051{
buzbeeeaf09bc2012-11-15 14:51:41 -08001052 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001054 const int* keys = reinterpret_cast<const int*>(&table[2]);
1055 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1057 << ", entries: " << std::dec << entries;
1058 for (int i = 0; i < entries; i++) {
1059 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1060 }
buzbee31a4a6f2012-02-28 15:36:15 -08001061}
1062
buzbee52a77fc2012-11-20 19:50:46 -08001063void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 /*
1065 * Packed switch data format:
1066 * ushort ident = 0x0100 magic value
1067 * ushort size number of entries in the table
1068 * int first_key first (and lowest) switch case value
1069 * int targets[size] branch targets, relative to switch opcode
1070 *
1071 * Total size is (4+size*2) 16-bit code units.
1072 */
buzbee31a4a6f2012-02-28 15:36:15 -08001073{
buzbeeeaf09bc2012-11-15 14:51:41 -08001074 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001075 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001076 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001077 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001078 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001079 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001080 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001081 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 << targets[i];
1083 }
buzbee31a4a6f2012-02-28 15:36:15 -08001084}
buzbeee3acd072012-02-25 17:03:10 -08001085
buzbeed1643e42012-09-05 14:06:51 -07001086/*
1087 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001088 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001089 * which we split a single Dalvik instruction, only the first MIR op
1090 * associated with a Dalvik PC should be entered into the map.
1091 */
buzbeefa57c472012-11-21 12:06:18 -08001092LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001093{
buzbeefa57c472012-11-21 12:06:18 -08001094 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1095 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1096 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001097 }
1098 return res;
1099}
buzbeee3acd072012-02-25 17:03:10 -08001100
buzbeee6285f92012-12-06 15:57:46 -08001101bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2)
1102{
1103 bool is_taken;
1104 switch (opcode) {
1105 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
1106 case Instruction::IF_NE: is_taken = (src1 != src2); break;
1107 case Instruction::IF_LT: is_taken = (src1 < src2); break;
1108 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
1109 case Instruction::IF_GT: is_taken = (src1 > src2); break;
1110 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
1111 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
1112 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
1113 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
1114 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
1115 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
1116 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
1117 default:
1118 LOG(FATAL) << "Unexpected opcode " << opcode;
1119 is_taken = false;
1120 }
1121 return is_taken;
1122}
1123
buzbee4ef3e452012-12-14 13:35:28 -08001124// Convert relation of src1/src2 to src2/src1
1125ConditionCode FlipComparisonOrder(ConditionCode before) {
1126 ConditionCode res;
1127 switch (before) {
1128 case kCondEq: res = kCondEq; break;
1129 case kCondNe: res = kCondNe; break;
1130 case kCondLt: res = kCondGt; break;
1131 case kCondGt: res = kCondLt; break;
1132 case kCondLe: res = kCondGe; break;
1133 case kCondGe: res = kCondLe; break;
1134 default:
1135 res = static_cast<ConditionCode>(0);
1136 LOG(FATAL) << "Unexpected ccode " << before;
1137 }
1138 return res;
1139}
1140
buzbeea3a82b22012-11-27 16:09:55 -08001141} // namespace art