blob: 11ab9c0a965aa5ed9df39db05a842ab7d3a019c2 [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
buzbee1bc37c62012-11-20 13:35:41 -080017#include "../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
buzbee02031b12012-11-23 09:41:35 -080026void MarkSafepointPC(CompilationUnit* cu, LIR* inst)
27{
28 inst->def_mask = ENCODE_ALL;
29 LIR* safepoint_pc = NewLIR0(cu, kPseudoSafepointPC);
30 DCHECK_EQ(safepoint_pc->def_mask, ENCODE_ALL);
31}
32
33bool FastInstance(CompilationUnit* cu, uint32_t field_idx,
34 int& field_offset, bool& is_volatile, bool is_put)
35{
36 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
37 *cu->dex_file,
38 cu->code_item, cu->method_idx,
39 cu->access_flags);
40 return cu->compiler->ComputeInstanceFieldInfo(field_idx, &m_unit,
41 field_offset, is_volatile, is_put);
42}
43
buzbeecbd6d442012-11-17 14:11:25 -080044/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080045void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080046{
buzbeefa57c472012-11-21 12:06:18 -080047 lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -080048}
49
buzbee02031b12012-11-23 09:41:35 -080050void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type)
buzbee31a4a6f2012-02-28 15:36:15 -080051{
buzbeefa57c472012-11-21 12:06:18 -080052 uint64_t *mask_ptr;
buzbeeeaf09bc2012-11-15 14:51:41 -080053 uint64_t mask = ENCODE_MEM;;
buzbee02031b12012-11-23 09:41:35 -080054 Codegen* cg = cu->cg.get();
55 DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080056 if (is_load) {
57 mask_ptr = &lir->use_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070058 } else {
buzbeefa57c472012-11-21 12:06:18 -080059 mask_ptr = &lir->def_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070060 }
61 /* Clear out the memref flags */
buzbeefa57c472012-11-21 12:06:18 -080062 *mask_ptr &= ~mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070063 /* ..and then add back the one we need */
buzbeefa57c472012-11-21 12:06:18 -080064 switch (mem_type) {
Bill Buzbeea114add2012-05-03 15:00:40 -070065 case kLiteral:
buzbeefa57c472012-11-21 12:06:18 -080066 DCHECK(is_load);
67 *mask_ptr |= ENCODE_LITERAL;
Bill Buzbeea114add2012-05-03 15:00:40 -070068 break;
69 case kDalvikReg:
buzbeefa57c472012-11-21 12:06:18 -080070 *mask_ptr |= ENCODE_DALVIK_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -070071 break;
72 case kHeapRef:
buzbeefa57c472012-11-21 12:06:18 -080073 *mask_ptr |= ENCODE_HEAP_REF;
Bill Buzbeea114add2012-05-03 15:00:40 -070074 break;
75 case kMustNotAlias:
76 /* Currently only loads can be marked as kMustNotAlias */
buzbee02031b12012-11-23 09:41:35 -080077 DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080078 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
Bill Buzbeea114add2012-05-03 15:00:40 -070079 break;
80 default:
buzbeefa57c472012-11-21 12:06:18 -080081 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
Bill Buzbeea114add2012-05-03 15:00:40 -070082 }
buzbee31a4a6f2012-02-28 15:36:15 -080083}
84
85/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080086 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -080087 */
buzbee02031b12012-11-23 09:41:35 -080088void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -080089{
buzbee02031b12012-11-23 09:41:35 -080090 SetMemRefType(cu, lir, is_load, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -080091
Bill Buzbeea114add2012-05-03 15:00:40 -070092 /*
buzbeefa57c472012-11-21 12:06:18 -080093 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
Bill Buzbeea114add2012-05-03 15:00:40 -070094 * access.
95 */
buzbeefa57c472012-11-21 12:06:18 -080096 lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -080097}
98
99/*
100 * Mark the corresponding bit(s).
101 */
buzbeefa57c472012-11-21 12:06:18 -0800102void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -0800103{
buzbee02031b12012-11-23 09:41:35 -0800104 Codegen* cg = cu->cg.get();
105 *mask |= cg->GetRegMaskCommon(cu, reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800106}
107
108/*
109 * Set up the proper fields in the resource mask
110 */
buzbeefa57c472012-11-21 12:06:18 -0800111void SetupResourceMasks(CompilationUnit* cu, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800112{
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 int opcode = lir->opcode;
buzbee02031b12012-11-23 09:41:35 -0800114 Codegen* cg = cu->cg.get();
buzbee31a4a6f2012-02-28 15:36:15 -0800115
Bill Buzbeea114add2012-05-03 15:00:40 -0700116 if (opcode <= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800117 lir->use_mask = lir->def_mask = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 return;
119 }
buzbee31a4a6f2012-02-28 15:36:15 -0800120
buzbee02031b12012-11-23 09:41:35 -0800121 uint64_t flags = cg->GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800122
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 if (flags & NEEDS_FIXUP) {
124 lir->flags.pcRelFixup = true;
125 }
buzbee31a4a6f2012-02-28 15:36:15 -0800126
Bill Buzbeea114add2012-05-03 15:00:40 -0700127 /* Get the starting size of the instruction's template */
buzbee02031b12012-11-23 09:41:35 -0800128 lir->flags.size = cg->GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800129
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 /* Set up the mask for resources that are updated */
131 if (flags & (IS_LOAD | IS_STORE)) {
132 /* Default to heap - will catch specialized classes later */
buzbee02031b12012-11-23 09:41:35 -0800133 SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 }
buzbee31a4a6f2012-02-28 15:36:15 -0800135
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 /*
137 * Conservatively assume the branch here will call out a function that in
138 * turn will trash everything.
139 */
140 if (flags & IS_BRANCH) {
buzbeefa57c472012-11-21 12:06:18 -0800141 lir->def_mask = lir->use_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700142 return;
143 }
buzbee31a4a6f2012-02-28 15:36:15 -0800144
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 if (flags & REG_DEF0) {
buzbeefa57c472012-11-21 12:06:18 -0800146 SetupRegMask(cu, &lir->def_mask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 }
buzbee31a4a6f2012-02-28 15:36:15 -0800148
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 if (flags & REG_DEF1) {
buzbeefa57c472012-11-21 12:06:18 -0800150 SetupRegMask(cu, &lir->def_mask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 }
buzbee31a4a6f2012-02-28 15:36:15 -0800152
buzbee31a4a6f2012-02-28 15:36:15 -0800153
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 if (flags & SETS_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800155 lir->def_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700156 }
buzbee31a4a6f2012-02-28 15:36:15 -0800157
Bill Buzbeea114add2012-05-03 15:00:40 -0700158 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
159 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800160
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 for (i = 0; i < 4; i++) {
162 if (flags & (1 << (kRegUse0 + i))) {
buzbeefa57c472012-11-21 12:06:18 -0800163 SetupRegMask(cu, &lir->use_mask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 }
buzbee31a4a6f2012-02-28 15:36:15 -0800165 }
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 & USES_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800169 lir->use_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 }
buzbee31a4a6f2012-02-28 15:36:15 -0800171
buzbeeb046e162012-10-30 15:48:42 -0700172 // Handle target-specific actions
buzbee02031b12012-11-23 09:41:35 -0800173 cg->SetupTargetResourceMasks(cu, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800174}
175
176/*
buzbee5de34942012-03-01 14:51:57 -0800177 * Debugging macros
178 */
179#define DUMP_RESOURCE_MASK(X)
180#define DUMP_SSA_REP(X)
181
182/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800183void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800184{
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 int offset = lir->offset;
186 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800187 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800188 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800189
Bill Buzbeea114add2012-05-03 15:00:40 -0700190 /* Handle pseudo-ops individually, and all regular insns as a group */
191 switch (lir->opcode) {
192 case kPseudoMethodEntry:
193 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800194 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 break;
196 case kPseudoMethodExit:
197 LOG(INFO) << "-------- Method_Exit";
198 break;
199 case kPseudoBarrier:
200 LOG(INFO) << "-------- BARRIER";
201 break;
202 case kPseudoExtended:
buzbeecbd6d442012-11-17 14:11:25 -0800203 LOG(INFO) << "-------- " << reinterpret_cast<char*>(dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700204 break;
205 case kPseudoSSARep:
buzbeecbd6d442012-11-17 14:11:25 -0800206 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << reinterpret_cast<char*>(dest));
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 break;
208 case kPseudoEntryBlock:
209 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
210 break;
211 case kPseudoDalvikByteCodeBoundary:
212 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800213 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 break;
215 case kPseudoExitBlock:
216 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
217 break;
218 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800219 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700220 << offset << "): .align4";
221 break;
222 case kPseudoEHBlockLabel:
223 LOG(INFO) << "Exception_Handling:";
224 break;
225 case kPseudoTargetLabel:
226 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800227 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 break;
229 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800230 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 break;
232 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800233 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 break;
235 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800236 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 break;
buzbee8320f382012-09-11 16:29:42 -0700238 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800239 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700240 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700241 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800242 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700243 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800245 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700246 << std::hex << lir->operands[0] << "|" << std::dec <<
247 lir->operands[0];
248 break;
249 default:
buzbeefa57c472012-11-21 12:06:18 -0800250 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 break;
252 } else {
buzbee02031b12012-11-23 09:41:35 -0800253 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
254 lir, base_addr));
255 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
256 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800258 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800260 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 }
262 break;
263 }
buzbee5de34942012-03-01 14:51:57 -0800264
buzbeefa57c472012-11-21 12:06:18 -0800265 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
266 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700267 }
buzbeefa57c472012-11-21 12:06:18 -0800268 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
269 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700270 }
buzbee5de34942012-03-01 14:51:57 -0800271}
272
buzbeefa57c472012-11-21 12:06:18 -0800273void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800274{
buzbee02031b12012-11-23 09:41:35 -0800275 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800276 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
277 for (int i = 0; i < num_regs; i++) {
278 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800280 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800281 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800282 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700283
284 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800285 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800287 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 buf3 = "Method*";
289 } else {
buzbeefa57c472012-11-21 12:06:18 -0800290 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 }
292
293 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800294 v_reg_map.core_location == kLocPhysReg ?
295 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
296 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700297 buf.c_str());
298 }
buzbee5de34942012-03-01 14:51:57 -0800299}
300
Bill Buzbeea5b30242012-09-28 07:19:44 -0700301/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800302static void DumpMappingTable(const char* table_name, const std::string& descriptor,
303 const std::string& name, const std::string& signature,
304 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700305 if (v.size() > 0) {
306 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
307 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
308 std::replace(line.begin(), line.end(), ';', '_');
309 LOG(INFO) << line;
310 for (uint32_t i = 0; i < v.size(); i+=2) {
311 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
312 LOG(INFO) << line;
313 }
314 LOG(INFO) <<" };\n\n";
315 }
316}
317
buzbee5de34942012-03-01 14:51:57 -0800318/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800319void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800320{
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800322 << PrettyMethod(cu->method_idx, *cu->dex_file);
323 LIR* lir_insn;
324 int insns_size = cu->insns_size;
buzbee5de34942012-03-01 14:51:57 -0800325
buzbeefa57c472012-11-21 12:06:18 -0800326 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
327 LOG(INFO) << "Ins : " << cu->num_ins;
328 LOG(INFO) << "Outs : " << cu->num_outs;
329 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
330 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
331 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
332 LOG(INFO) << "Frame size : " << cu->frame_size;
333 LOG(INFO) << "code size is " << cu->total_size <<
334 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800336 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
337 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800338 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800339 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700340 }
buzbee28c9a832012-11-21 15:39:13 -0800341 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800342 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
343 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 }
buzbee5de34942012-03-01 14:51:57 -0800345
Bill Buzbeea114add2012-05-03 15:00:40 -0700346 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800347 cu->dex_file->GetMethodId(cu->method_idx);
348 std::string signature(cu->dex_file->GetMethodSignature(method_id));
349 std::string name(cu->dex_file->GetMethodName(method_id));
350 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800351
Bill Buzbeea5b30242012-09-28 07:19:44 -0700352 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800353 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
354 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800355}
356
buzbeea2ebdd72012-03-04 14:57:06 -0800357
buzbeefa57c472012-11-21 12:06:18 -0800358LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700359 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800360{
buzbeefa57c472012-11-21 12:06:18 -0800361 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
362 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700363 insn->opcode = opcode;
364 insn->operands[0] = op0;
365 insn->operands[1] = op1;
366 insn->operands[2] = op2;
367 insn->operands[3] = op3;
368 insn->operands[4] = op4;
369 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800370 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700371 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
372 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800374 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 }
376 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800377}
378
buzbee5de34942012-03-01 14:51:57 -0800379/*
buzbee31a4a6f2012-02-28 15:36:15 -0800380 * The following are building blocks to construct low-level IRs with 0 - 4
381 * operands.
382 */
buzbeefa57c472012-11-21 12:06:18 -0800383LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800384{
buzbee02031b12012-11-23 09:41:35 -0800385 Codegen* cg = cu->cg.get();
386 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
387 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800388 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
389 << cu->current_dalvik_offset;
390 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
391 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800393}
394
buzbeefa57c472012-11-21 12:06:18 -0800395LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800397{
buzbee02031b12012-11-23 09:41:35 -0800398 Codegen* cg = cu->cg.get();
399 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
400 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800401 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
402 << cu->current_dalvik_offset;
403 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
404 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700405 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800406}
407
buzbeefa57c472012-11-21 12:06:18 -0800408LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800410{
buzbee02031b12012-11-23 09:41:35 -0800411 Codegen* cg = cu->cg.get();
412 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
413 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800414 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
415 << cu->current_dalvik_offset;
416 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
417 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800419}
420
buzbeefa57c472012-11-21 12:06:18 -0800421LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800423{
buzbee02031b12012-11-23 09:41:35 -0800424 Codegen* cg = cu->cg.get();
425 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
426 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800427 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
428 << cu->current_dalvik_offset;
429 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
430 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800432}
433
buzbeefa57c472012-11-21 12:06:18 -0800434LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800436{
buzbee02031b12012-11-23 09:41:35 -0800437 Codegen* cg = cu->cg.get();
438 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
439 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800440 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
441 << cu->current_dalvik_offset;
442 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
443 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800445}
buzbee31a4a6f2012-02-28 15:36:15 -0800446
buzbeefa57c472012-11-21 12:06:18 -0800447LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800449{
buzbee02031b12012-11-23 09:41:35 -0800450 Codegen* cg = cu->cg.get();
451 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
452 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800453 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
454 << cu->current_dalvik_offset;
455 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
456 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800458}
459
buzbee31a4a6f2012-02-28 15:36:15 -0800460/*
461 * Search the existing constants in the literal pool for an exact or close match
462 * within specified delta (greater or equal to 0).
463 */
buzbeefa57c472012-11-21 12:06:18 -0800464LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800465{
buzbeefa57c472012-11-21 12:06:18 -0800466 while (data_target) {
467 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
468 return data_target;
469 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700470 }
471 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800472}
473
474/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800475LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800476{
buzbeefa57c472012-11-21 12:06:18 -0800477 bool lo_match = false;
478 LIR* lo_target = NULL;
479 while (data_target) {
480 if (lo_match && (data_target->operands[0] == val_hi)) {
481 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800482 }
buzbeefa57c472012-11-21 12:06:18 -0800483 lo_match = false;
484 if (data_target->operands[0] == val_lo) {
485 lo_match = true;
486 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 }
buzbeefa57c472012-11-21 12:06:18 -0800488 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700489 }
490 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800491}
492
493/*
494 * The following are building blocks to insert constants into the pool or
495 * instruction streams.
496 */
497
buzbee5de34942012-03-01 14:51:57 -0800498/* Add a 32-bit constant either in the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800499LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800500{
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800502 if (constant_list_p) {
503 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
504 new_value->operands[0] = value;
505 new_value->next = *constant_list_p;
506 *constant_list_p = new_value;
507 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 }
509 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800510}
511
512/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800513LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
514 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800515{
buzbeefa57c472012-11-21 12:06:18 -0800516 AddWordData(cu, constant_list_p, val_hi);
517 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800518}
519
buzbeeaad94382012-11-21 07:40:50 -0800520static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 buf.push_back( data & 0xff);
522 buf.push_back( (data >> 8) & 0xff);
523 buf.push_back( (data >> 16) & 0xff);
524 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800525}
526
buzbeeaad94382012-11-21 07:40:50 -0800527static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 while (buf.size() < offset) {
529 buf.push_back(0);
530 }
buzbeee3acd072012-02-25 17:03:10 -0800531}
532
533/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800534static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800535{
buzbeefa57c472012-11-21 12:06:18 -0800536 AlignBuffer(cu->code_buffer, cu->data_offset);
537 LIR* data_lir = cu->literal_list;
538 while (data_lir != NULL) {
539 PushWord(cu->code_buffer, data_lir->operands[0]);
540 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 }
542 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800543 data_lir = cu->code_literal_list;
544 while (data_lir != NULL) {
545 uint32_t target = data_lir->operands[0];
546 cu->compiler->AddCodePatch(cu->dex_file,
547 cu->method_idx,
548 cu->invoke_type,
Ian Rogers137e88f2012-10-08 17:46:47 -0700549 target,
buzbeefa57c472012-11-21 12:06:18 -0800550 static_cast<InvokeType>(data_lir->operands[1]),
551 cu->code_buffer.size());
552 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700553 // unique based on target to ensure code deduplication works
554 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800555 PushWord(cu->code_buffer, unique_patch_value);
556 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700557 }
buzbeefa57c472012-11-21 12:06:18 -0800558 data_lir = cu->method_literal_list;
559 while (data_lir != NULL) {
560 uint32_t target = data_lir->operands[0];
561 cu->compiler->AddMethodPatch(cu->dex_file,
562 cu->method_idx,
563 cu->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700564 target,
buzbeefa57c472012-11-21 12:06:18 -0800565 static_cast<InvokeType>(data_lir->operands[1]),
566 cu->code_buffer.size());
567 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700568 // unique based on target to ensure code deduplication works
569 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800570 PushWord(cu->code_buffer, unique_patch_value);
571 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700572 }
buzbeee3acd072012-02-25 17:03:10 -0800573}
574
575/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800576static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800577{
Bill Buzbeea114add2012-05-03 15:00:40 -0700578 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800579 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700580 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800581 SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
582 if (tab_rec == NULL) break;
583 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700584 /*
585 * For Arm, our reference point is the address of the bx
586 * instruction that does the launch, so we have to subtract
587 * the auto pc-advance. For other targets the reference point
588 * is a label, so we can use the offset as-is.
589 */
buzbeefa57c472012-11-21 12:06:18 -0800590 int bx_offset = INVALID_OFFSET;
591 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700592 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800593 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700594 break;
595 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800596 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700597 break;
598 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800599 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700600 break;
buzbeefa57c472012-11-21 12:06:18 -0800601 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700602 }
buzbeefa57c472012-11-21 12:06:18 -0800603 if (cu->verbose) {
604 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800605 }
buzbeefa57c472012-11-21 12:06:18 -0800606 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
607 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
608 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
609 int disp = tab_rec->targets[elems]->offset - bx_offset;
610 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 LOG(INFO) << " Case[" << elems << "] key: 0x"
612 << std::hex << keys[elems] << ", disp: 0x"
613 << std::hex << disp;
614 }
buzbeefa57c472012-11-21 12:06:18 -0800615 PushWord(cu->code_buffer, keys[elems]);
616 PushWord(cu->code_buffer,
617 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 }
619 } else {
buzbeefa57c472012-11-21 12:06:18 -0800620 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700621 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800622 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
623 int disp = tab_rec->targets[elems]->offset - bx_offset;
624 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700625 LOG(INFO) << " Case[" << elems << "] disp: 0x"
626 << std::hex << disp;
627 }
buzbeefa57c472012-11-21 12:06:18 -0800628 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 }
630 }
631 }
buzbeee3acd072012-02-25 17:03:10 -0800632}
633
634/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800635static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800636{
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800638 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700639 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800640 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800641 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800642 if (tab_rec == NULL) break;
643 AlignBuffer(cu->code_buffer, tab_rec->offset);
644 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
645 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
646 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800647 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 }
buzbeee3acd072012-02-25 17:03:10 -0800649}
650
buzbeeaad94382012-11-21 07:40:50 -0800651static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800652{
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 for (;lir != NULL; lir = lir->next) {
654 lir->offset = offset;
655 offset += 4;
656 }
657 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800658}
659
buzbee6459e7c2012-10-02 14:42:41 -0700660// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800661static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700662{
663 bool success = true;
buzbeefa57c472012-11-21 12:06:18 -0800664 for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) {
665 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700666 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800667 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
668 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700669 found = true;
670 break;
671 }
672 }
673 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800674 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700675 success = false;
676 }
677 }
678 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800679 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
680 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
681 if (cu->catches.find(dex_pc) == cu->catches.end()) {
682 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700683 success = false;
684 }
685 }
686 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800687 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
688 LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: "
689 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700690 }
691 return success;
692}
693
buzbeefa57c472012-11-21 12:06:18 -0800694static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800695{
buzbeefa57c472012-11-21 12:06:18 -0800696 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
697 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
698 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
699 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700700 }
buzbeefa57c472012-11-21 12:06:18 -0800701 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
702 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
703 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800704 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
buzbeefa57c472012-11-21 12:06:18 -0800706 DCHECK(VerifyCatchEntries(cu));
707 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
708 cu->dex2pcMappingTable.size());
709 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
710 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
711 cu->pc2dexMappingTable.begin(),
712 cu->pc2dexMappingTable.end());
713 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
714 cu->dex2pcMappingTable.begin(),
715 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800716}
717
Ian Rogers0c7abda2012-09-19 13:33:42 -0700718class NativePcToReferenceMapBuilder {
719 public:
720 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
721 size_t entries, uint32_t max_native_offset,
722 size_t references_width) : entries_(entries),
723 references_width_(references_width), in_use_(entries),
724 table_(table) {
725 // Compute width in bytes needed to hold max_native_offset.
726 native_offset_width_ = 0;
727 while (max_native_offset != 0) {
728 native_offset_width_++;
729 max_native_offset >>= 8;
730 }
731 // Resize table and set up header.
732 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700733 CHECK_LT(native_offset_width_, 1U << 3);
734 (*table)[0] = native_offset_width_ & 7;
735 CHECK_LT(references_width_, 1U << 13);
736 (*table)[0] |= (references_width_ << 3) & 0xFF;
737 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700738 CHECK_LT(entries, 1U << 16);
739 (*table)[2] = entries & 0xFF;
740 (*table)[3] = (entries >> 8) & 0xFF;
741 }
742
743 void AddEntry(uint32_t native_offset, const uint8_t* references) {
744 size_t table_index = TableIndex(native_offset);
745 while (in_use_[table_index]) {
746 table_index = (table_index + 1) % entries_;
747 }
748 in_use_[table_index] = true;
749 SetNativeOffset(table_index, native_offset);
750 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
751 SetReferences(table_index, references);
752 }
753
754 private:
755 size_t TableIndex(uint32_t native_offset) {
756 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
757 }
758
759 uint32_t GetNativeOffset(size_t table_index) {
760 uint32_t native_offset = 0;
761 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
762 for (size_t i = 0; i < native_offset_width_; i++) {
763 native_offset |= (*table_)[table_offset + i] << (i * 8);
764 }
765 return native_offset;
766 }
767
768 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
769 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
770 for (size_t i = 0; i < native_offset_width_; i++) {
771 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
772 }
773 }
774
775 void SetReferences(size_t table_index, const uint8_t* references) {
776 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
777 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
778 }
779
780 size_t EntryWidth() const {
781 return native_offset_width_ + references_width_;
782 }
783
784 // Number of entries in the table.
785 const size_t entries_;
786 // Number of bytes used to encode the reference bitmap.
787 const size_t references_width_;
788 // Number of bytes used to encode a native offset.
789 size_t native_offset_width_;
790 // Entries that are in use.
791 std::vector<bool> in_use_;
792 // The table we're building.
793 std::vector<uint8_t>* const table_;
794};
795
buzbeefa57c472012-11-21 12:06:18 -0800796static void CreateNativeGcMap(CompilationUnit* cu) {
797 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700798 uint32_t max_native_offset = 0;
799 for (size_t i = 0; i < mapping_table.size(); i += 2) {
800 uint32_t native_offset = mapping_table[i + 0];
801 if (native_offset > max_native_offset) {
802 max_native_offset = native_offset;
803 }
804 }
buzbeefa57c472012-11-21 12:06:18 -0800805 Compiler::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700806 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
807 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
808 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800809 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700810 mapping_table.size() / 2, max_native_offset,
811 dex_gc_map.RegWidth());
812
813 for (size_t i = 0; i < mapping_table.size(); i += 2) {
814 uint32_t native_offset = mapping_table[i + 0];
815 uint32_t dex_pc = mapping_table[i + 1];
816 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700817 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
818 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700819 }
820}
821
buzbeee3acd072012-02-25 17:03:10 -0800822/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800823static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800824{
buzbeefa57c472012-11-21 12:06:18 -0800825 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
826 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
827 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800829}
830
buzbeefa57c472012-11-21 12:06:18 -0800831static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800832{
Bill Buzbeea114add2012-05-03 15:00:40 -0700833 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800834 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800836 SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
837 if (tab_rec == NULL) break;
838 tab_rec->offset = offset;
839 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
840 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 } else {
buzbeefa57c472012-11-21 12:06:18 -0800842 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700843 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800844 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800845 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 }
847 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800848}
849
buzbeefa57c472012-11-21 12:06:18 -0800850static int AssignFillArrayDataOffset(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->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800855 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800856 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800857 if (tab_rec == NULL) break;
858 tab_rec->offset = offset;
859 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700860 // word align
861 offset = (offset + 3) & ~3;
862 }
863 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800864}
865
buzbeea3a82b22012-11-27 16:09:55 -0800866// LIR offset assignment.
867static int AssignInsnOffsets(CompilationUnit* cu)
868{
869 LIR* lir;
870 int offset = 0;
871
872 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
873 lir->offset = offset;
874 if (lir->opcode >= 0) {
875 if (!lir->flags.is_nop) {
876 offset += lir->flags.size;
877 }
878 } else if (lir->opcode == kPseudoPseudoAlign4) {
879 if (offset & 0x2) {
880 offset += 2;
881 lir->operands[0] = 1;
882 } else {
883 lir->operands[0] = 0;
884 }
885 }
886 /* Pseudo opcodes don't consume space */
887 }
888
889 return offset;
890}
891
buzbeee3acd072012-02-25 17:03:10 -0800892/*
893 * Walk the compilation unit and assign offsets to instructions
894 * and literals and compute the total size of the compiled unit.
895 */
buzbeefa57c472012-11-21 12:06:18 -0800896static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800897{
buzbeea3a82b22012-11-27 16:09:55 -0800898 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800899
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 /* Const values have to be word aligned */
901 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800902
Bill Buzbeea114add2012-05-03 15:00:40 -0700903 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800904 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800905
buzbeefa57c472012-11-21 12:06:18 -0800906 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800907
buzbeefa57c472012-11-21 12:06:18 -0800908 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800909
buzbeefa57c472012-11-21 12:06:18 -0800910 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800911
buzbeefa57c472012-11-21 12:06:18 -0800912 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800913}
914
915/*
916 * Go over each instruction in the list and calculate the offset from the top
917 * before sending them off to the assembler. If out-of-range branch distance is
918 * seen rearrange the instructions a bit to correct it.
919 */
buzbeefa57c472012-11-21 12:06:18 -0800920void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800921{
buzbee02031b12012-11-23 09:41:35 -0800922 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800923 AssignOffsets(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 /*
925 * Assemble here. Note that we generate code with optimistic assumptions
926 * and if found now to work, we'll have to redo the sequence and retry.
927 */
buzbeee3acd072012-02-25 17:03:10 -0800928
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800930 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 if (res == kSuccess) {
932 break;
933 } else {
buzbeefa57c472012-11-21 12:06:18 -0800934 cu->assembler_retries++;
935 if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) {
936 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700937 LOG(FATAL) << "Assembler error - too many retries";
938 }
939 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800940 AssignOffsets(cu);
941 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800942 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700943 }
buzbeee3acd072012-02-25 17:03:10 -0800944
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800946 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800947
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800949 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800950
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800952 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800953
Ian Rogers0c7abda2012-09-19 13:33:42 -0700954 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800955 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700956
buzbeefa57c472012-11-21 12:06:18 -0800957 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800958}
959
buzbee31a4a6f2012-02-28 15:36:15 -0800960/*
961 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
962 * offset vaddr. This label will be used to fix up the case
963 * branch table during the assembly phase. Be sure to set
964 * all resource flags on this to prevent code motion across
965 * target boundaries. KeyVal is just there for debugging.
966 */
buzbeefa57c472012-11-21 12:06:18 -0800967static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800968{
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800970 it = cu->boundary_map.find(vaddr);
971 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
973 }
buzbeefa57c472012-11-21 12:06:18 -0800974 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
975 new_label->dalvik_offset = vaddr;
976 new_label->opcode = kPseudoCaseLabel;
977 new_label->operands[0] = keyVal;
978 InsertLIRAfter(it->second, new_label);
979 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -0800980}
981
buzbeefa57c472012-11-21 12:06:18 -0800982static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -0800983{
buzbeefa57c472012-11-21 12:06:18 -0800984 const uint16_t* table = tab_rec->table;
985 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -0800986 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700987 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -0800988 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800990 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 }
buzbee31a4a6f2012-02-28 15:36:15 -0800992}
993
buzbeefa57c472012-11-21 12:06:18 -0800994static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -0800995{
buzbeefa57c472012-11-21 12:06:18 -0800996 const uint16_t* table = tab_rec->table;
997 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -0800999 const int* keys = reinterpret_cast<const int*>(&table[2]);
1000 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001001 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001002 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 }
buzbee31a4a6f2012-02-28 15:36:15 -08001004}
1005
buzbeefa57c472012-11-21 12:06:18 -08001006void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001007{
Bill Buzbeea114add2012-05-03 15:00:40 -07001008 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001009 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001011 SwitchTable *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -08001012 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001013 if (tab_rec == NULL) break;
1014 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1015 MarkPackedCaseLabels(cu, tab_rec);
1016 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1017 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 } else {
1019 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001020 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001021 }
buzbee31a4a6f2012-02-28 15:36:15 -08001022}
1023
buzbee52a77fc2012-11-20 19:50:46 -08001024void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001025 /*
1026 * Sparse switch data format:
1027 * ushort ident = 0x0200 magic value
1028 * ushort size number of entries in the table; > 0
1029 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1030 * int targets[size] branch targets, relative to switch opcode
1031 *
1032 * Total size is (2+size*4) 16-bit code units.
1033 */
buzbee31a4a6f2012-02-28 15:36:15 -08001034{
buzbeeeaf09bc2012-11-15 14:51:41 -08001035 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001037 const int* keys = reinterpret_cast<const int*>(&table[2]);
1038 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1040 << ", entries: " << std::dec << entries;
1041 for (int i = 0; i < entries; i++) {
1042 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1043 }
buzbee31a4a6f2012-02-28 15:36:15 -08001044}
1045
buzbee52a77fc2012-11-20 19:50:46 -08001046void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 /*
1048 * Packed switch data format:
1049 * ushort ident = 0x0100 magic value
1050 * ushort size number of entries in the table
1051 * int first_key first (and lowest) switch case value
1052 * int targets[size] branch targets, relative to switch opcode
1053 *
1054 * Total size is (4+size*2) 16-bit code units.
1055 */
buzbee31a4a6f2012-02-28 15:36:15 -08001056{
buzbeeeaf09bc2012-11-15 14:51:41 -08001057 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001058 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001060 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001061 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001062 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001063 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001064 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 << targets[i];
1066 }
buzbee31a4a6f2012-02-28 15:36:15 -08001067}
buzbeee3acd072012-02-25 17:03:10 -08001068
buzbeed1643e42012-09-05 14:06:51 -07001069/*
1070 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001071 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001072 * which we split a single Dalvik instruction, only the first MIR op
1073 * associated with a Dalvik PC should be entered into the map.
1074 */
buzbeefa57c472012-11-21 12:06:18 -08001075LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001076{
buzbeefa57c472012-11-21 12:06:18 -08001077 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1078 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1079 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001080 }
1081 return res;
1082}
buzbeee3acd072012-02-25 17:03:10 -08001083
buzbeea3a82b22012-11-27 16:09:55 -08001084} // namespace art