blob: ad05b93c8d30b390887ecae948884c3d15d7f45f [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
Brian Carlstrom641ce032013-01-31 15:21:37 -080017#include "compiler/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,
TDYa127dc5daa02013-01-09 21:31:37 +080037 *cu->dex_file, cu->code_item,
38 cu->class_def_idx, cu->method_idx,
39 cu->access_flags);
buzbee02031b12012-11-23 09:41:35 -080040 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)
buzbee5de34942012-03-01 14:51:57 -0800180
181/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800182void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800183{
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 int offset = lir->offset;
185 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800186 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800187 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800188
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 /* Handle pseudo-ops individually, and all regular insns as a group */
190 switch (lir->opcode) {
191 case kPseudoMethodEntry:
192 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800193 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 break;
195 case kPseudoMethodExit:
196 LOG(INFO) << "-------- Method_Exit";
197 break;
198 case kPseudoBarrier:
199 LOG(INFO) << "-------- BARRIER";
200 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 case kPseudoEntryBlock:
202 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
203 break;
204 case kPseudoDalvikByteCodeBoundary:
205 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800206 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 break;
208 case kPseudoExitBlock:
209 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
210 break;
211 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800212 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 << offset << "): .align4";
214 break;
215 case kPseudoEHBlockLabel:
216 LOG(INFO) << "Exception_Handling:";
217 break;
218 case kPseudoTargetLabel:
219 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800220 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700221 break;
222 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800223 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 break;
225 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800226 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 break;
228 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800229 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 break;
buzbee8320f382012-09-11 16:29:42 -0700231 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800232 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700233 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700234 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800235 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700236 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800238 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 << std::hex << lir->operands[0] << "|" << std::dec <<
240 lir->operands[0];
241 break;
242 default:
buzbeefa57c472012-11-21 12:06:18 -0800243 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 break;
245 } else {
buzbee02031b12012-11-23 09:41:35 -0800246 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
247 lir, base_addr));
248 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
249 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800251 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800253 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
255 break;
256 }
buzbee5de34942012-03-01 14:51:57 -0800257
buzbeefa57c472012-11-21 12:06:18 -0800258 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
259 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 }
buzbeefa57c472012-11-21 12:06:18 -0800261 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
262 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700263 }
buzbee5de34942012-03-01 14:51:57 -0800264}
265
buzbeefa57c472012-11-21 12:06:18 -0800266void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800267{
buzbee02031b12012-11-23 09:41:35 -0800268 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800269 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
270 for (int i = 0; i < num_regs; i++) {
271 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800273 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800274 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800275 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700276
277 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800278 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800280 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 buf3 = "Method*";
282 } else {
buzbeefa57c472012-11-21 12:06:18 -0800283 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 }
285
286 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800287 v_reg_map.core_location == kLocPhysReg ?
288 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
289 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700290 buf.c_str());
291 }
buzbee5de34942012-03-01 14:51:57 -0800292}
293
Bill Buzbeea5b30242012-09-28 07:19:44 -0700294/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800295static void DumpMappingTable(const char* table_name, const std::string& descriptor,
296 const std::string& name, const std::string& signature,
297 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700298 if (v.size() > 0) {
299 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
300 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
301 std::replace(line.begin(), line.end(), ';', '_');
302 LOG(INFO) << line;
303 for (uint32_t i = 0; i < v.size(); i+=2) {
304 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
305 LOG(INFO) << line;
306 }
307 LOG(INFO) <<" };\n\n";
308 }
309}
310
buzbee5de34942012-03-01 14:51:57 -0800311/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800312void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800313{
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800315 << PrettyMethod(cu->method_idx, *cu->dex_file);
316 LIR* lir_insn;
317 int insns_size = cu->insns_size;
buzbee5de34942012-03-01 14:51:57 -0800318
buzbeefa57c472012-11-21 12:06:18 -0800319 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
320 LOG(INFO) << "Ins : " << cu->num_ins;
321 LOG(INFO) << "Outs : " << cu->num_outs;
322 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
323 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
324 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
325 LOG(INFO) << "Frame size : " << cu->frame_size;
326 LOG(INFO) << "code size is " << cu->total_size <<
327 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700328 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800329 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
330 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800331 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800332 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 }
buzbee28c9a832012-11-21 15:39:13 -0800334 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800335 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
336 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700337 }
buzbee5de34942012-03-01 14:51:57 -0800338
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800340 cu->dex_file->GetMethodId(cu->method_idx);
341 std::string signature(cu->dex_file->GetMethodSignature(method_id));
342 std::string name(cu->dex_file->GetMethodName(method_id));
343 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800344
Bill Buzbeea5b30242012-09-28 07:19:44 -0700345 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800346 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
347 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800348}
349
buzbeea2ebdd72012-03-04 14:57:06 -0800350
buzbeefa57c472012-11-21 12:06:18 -0800351LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800353{
buzbeefa57c472012-11-21 12:06:18 -0800354 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
355 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700356 insn->opcode = opcode;
357 insn->operands[0] = op0;
358 insn->operands[1] = op1;
359 insn->operands[2] = op2;
360 insn->operands[3] = op3;
361 insn->operands[4] = op4;
362 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800363 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700364 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
365 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800367 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 }
369 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800370}
371
buzbee5de34942012-03-01 14:51:57 -0800372/*
buzbee31a4a6f2012-02-28 15:36:15 -0800373 * The following are building blocks to construct low-level IRs with 0 - 4
374 * operands.
375 */
buzbeefa57c472012-11-21 12:06:18 -0800376LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800377{
buzbee02031b12012-11-23 09:41:35 -0800378 Codegen* cg = cu->cg.get();
379 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
380 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800381 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
382 << cu->current_dalvik_offset;
383 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
384 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800386}
387
buzbeefa57c472012-11-21 12:06:18 -0800388LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800390{
buzbee02031b12012-11-23 09:41:35 -0800391 Codegen* cg = cu->cg.get();
392 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
393 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800394 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
395 << cu->current_dalvik_offset;
396 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
397 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700398 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800399}
400
buzbeefa57c472012-11-21 12:06:18 -0800401LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800403{
buzbee02031b12012-11-23 09:41:35 -0800404 Codegen* cg = cu->cg.get();
405 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
406 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800407 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
408 << cu->current_dalvik_offset;
409 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
410 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700411 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800412}
413
buzbeefa57c472012-11-21 12:06:18 -0800414LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800416{
buzbee02031b12012-11-23 09:41:35 -0800417 Codegen* cg = cu->cg.get();
418 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
419 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800420 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
421 << cu->current_dalvik_offset;
422 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
423 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800425}
426
buzbeefa57c472012-11-21 12:06:18 -0800427LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800429{
buzbee02031b12012-11-23 09:41:35 -0800430 Codegen* cg = cu->cg.get();
431 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
432 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800433 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
434 << cu->current_dalvik_offset;
435 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
436 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800438}
buzbee31a4a6f2012-02-28 15:36:15 -0800439
buzbeefa57c472012-11-21 12:06:18 -0800440LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700441 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800442{
buzbee02031b12012-11-23 09:41:35 -0800443 Codegen* cg = cu->cg.get();
444 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
445 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800446 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
447 << cu->current_dalvik_offset;
448 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
449 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800451}
452
buzbee31a4a6f2012-02-28 15:36:15 -0800453/*
454 * Search the existing constants in the literal pool for an exact or close match
455 * within specified delta (greater or equal to 0).
456 */
buzbeefa57c472012-11-21 12:06:18 -0800457LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800458{
buzbeefa57c472012-11-21 12:06:18 -0800459 while (data_target) {
460 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
461 return data_target;
462 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 }
464 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800465}
466
467/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800468LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800469{
buzbeefa57c472012-11-21 12:06:18 -0800470 bool lo_match = false;
471 LIR* lo_target = NULL;
472 while (data_target) {
473 if (lo_match && (data_target->operands[0] == val_hi)) {
474 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800475 }
buzbeefa57c472012-11-21 12:06:18 -0800476 lo_match = false;
477 if (data_target->operands[0] == val_lo) {
478 lo_match = true;
479 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 }
buzbeefa57c472012-11-21 12:06:18 -0800481 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700482 }
483 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800484}
485
486/*
487 * The following are building blocks to insert constants into the pool or
488 * instruction streams.
489 */
490
buzbee5de34942012-03-01 14:51:57 -0800491/* Add a 32-bit constant either in the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800492LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800493{
Bill Buzbeea114add2012-05-03 15:00:40 -0700494 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800495 if (constant_list_p) {
496 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
497 new_value->operands[0] = value;
498 new_value->next = *constant_list_p;
499 *constant_list_p = new_value;
500 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 }
502 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800503}
504
505/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800506LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
507 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800508{
buzbeefa57c472012-11-21 12:06:18 -0800509 AddWordData(cu, constant_list_p, val_hi);
510 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800511}
512
buzbeeaad94382012-11-21 07:40:50 -0800513static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 buf.push_back( data & 0xff);
515 buf.push_back( (data >> 8) & 0xff);
516 buf.push_back( (data >> 16) & 0xff);
517 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800518}
519
buzbeeaad94382012-11-21 07:40:50 -0800520static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 while (buf.size() < offset) {
522 buf.push_back(0);
523 }
buzbeee3acd072012-02-25 17:03:10 -0800524}
525
526/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800527static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800528{
buzbeefa57c472012-11-21 12:06:18 -0800529 AlignBuffer(cu->code_buffer, cu->data_offset);
530 LIR* data_lir = cu->literal_list;
531 while (data_lir != NULL) {
532 PushWord(cu->code_buffer, data_lir->operands[0]);
533 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700534 }
535 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800536 data_lir = cu->code_literal_list;
537 while (data_lir != NULL) {
538 uint32_t target = data_lir->operands[0];
539 cu->compiler->AddCodePatch(cu->dex_file,
540 cu->method_idx,
541 cu->invoke_type,
Ian Rogers137e88f2012-10-08 17:46:47 -0700542 target,
buzbeefa57c472012-11-21 12:06:18 -0800543 static_cast<InvokeType>(data_lir->operands[1]),
544 cu->code_buffer.size());
545 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700546 // unique based on target to ensure code deduplication works
547 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800548 PushWord(cu->code_buffer, unique_patch_value);
549 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700550 }
buzbeefa57c472012-11-21 12:06:18 -0800551 data_lir = cu->method_literal_list;
552 while (data_lir != NULL) {
553 uint32_t target = data_lir->operands[0];
554 cu->compiler->AddMethodPatch(cu->dex_file,
555 cu->method_idx,
556 cu->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 target,
buzbeefa57c472012-11-21 12:06:18 -0800558 static_cast<InvokeType>(data_lir->operands[1]),
559 cu->code_buffer.size());
560 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700561 // unique based on target to ensure code deduplication works
562 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800563 PushWord(cu->code_buffer, unique_patch_value);
564 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700565 }
buzbeee3acd072012-02-25 17:03:10 -0800566}
567
568/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800569static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800570{
Bill Buzbeea114add2012-05-03 15:00:40 -0700571 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800572 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700573 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800574 SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
575 if (tab_rec == NULL) break;
576 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 /*
578 * For Arm, our reference point is the address of the bx
579 * instruction that does the launch, so we have to subtract
580 * the auto pc-advance. For other targets the reference point
581 * is a label, so we can use the offset as-is.
582 */
buzbeefa57c472012-11-21 12:06:18 -0800583 int bx_offset = INVALID_OFFSET;
584 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700585 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800586 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700587 break;
588 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800589 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700590 break;
591 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800592 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700593 break;
buzbeefa57c472012-11-21 12:06:18 -0800594 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700595 }
buzbeefa57c472012-11-21 12:06:18 -0800596 if (cu->verbose) {
597 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800598 }
buzbeefa57c472012-11-21 12:06:18 -0800599 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
600 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
601 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
602 int disp = tab_rec->targets[elems]->offset - bx_offset;
603 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700604 LOG(INFO) << " Case[" << elems << "] key: 0x"
605 << std::hex << keys[elems] << ", disp: 0x"
606 << std::hex << disp;
607 }
buzbeefa57c472012-11-21 12:06:18 -0800608 PushWord(cu->code_buffer, keys[elems]);
609 PushWord(cu->code_buffer,
610 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700611 }
612 } else {
buzbeefa57c472012-11-21 12:06:18 -0800613 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800615 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
616 int disp = tab_rec->targets[elems]->offset - bx_offset;
617 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 LOG(INFO) << " Case[" << elems << "] disp: 0x"
619 << std::hex << disp;
620 }
buzbeefa57c472012-11-21 12:06:18 -0800621 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 }
623 }
624 }
buzbeee3acd072012-02-25 17:03:10 -0800625}
626
627/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800628static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800629{
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800631 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700632 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800633 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800634 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800635 if (tab_rec == NULL) break;
636 AlignBuffer(cu->code_buffer, tab_rec->offset);
637 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
638 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
639 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800640 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 }
buzbeee3acd072012-02-25 17:03:10 -0800642}
643
buzbeeaad94382012-11-21 07:40:50 -0800644static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800645{
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 for (;lir != NULL; lir = lir->next) {
647 lir->offset = offset;
648 offset += 4;
649 }
650 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800651}
652
buzbee6459e7c2012-10-02 14:42:41 -0700653// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800654static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700655{
656 bool success = true;
buzbeefa57c472012-11-21 12:06:18 -0800657 for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) {
658 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700659 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800660 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
661 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700662 found = true;
663 break;
664 }
665 }
666 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800667 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700668 success = false;
669 }
670 }
671 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800672 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
673 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
674 if (cu->catches.find(dex_pc) == cu->catches.end()) {
675 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700676 success = false;
677 }
678 }
679 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800680 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
681 LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: "
682 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700683 }
684 return success;
685}
686
buzbeefa57c472012-11-21 12:06:18 -0800687static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800688{
buzbeefa57c472012-11-21 12:06:18 -0800689 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
690 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
691 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
692 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700693 }
buzbeefa57c472012-11-21 12:06:18 -0800694 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
695 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
696 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800697 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700698 }
buzbeefa57c472012-11-21 12:06:18 -0800699 DCHECK(VerifyCatchEntries(cu));
700 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
701 cu->dex2pcMappingTable.size());
702 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
703 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
704 cu->pc2dexMappingTable.begin(),
705 cu->pc2dexMappingTable.end());
706 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
707 cu->dex2pcMappingTable.begin(),
708 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800709}
710
Ian Rogers0c7abda2012-09-19 13:33:42 -0700711class NativePcToReferenceMapBuilder {
712 public:
713 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
714 size_t entries, uint32_t max_native_offset,
715 size_t references_width) : entries_(entries),
716 references_width_(references_width), in_use_(entries),
717 table_(table) {
718 // Compute width in bytes needed to hold max_native_offset.
719 native_offset_width_ = 0;
720 while (max_native_offset != 0) {
721 native_offset_width_++;
722 max_native_offset >>= 8;
723 }
724 // Resize table and set up header.
725 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700726 CHECK_LT(native_offset_width_, 1U << 3);
727 (*table)[0] = native_offset_width_ & 7;
728 CHECK_LT(references_width_, 1U << 13);
729 (*table)[0] |= (references_width_ << 3) & 0xFF;
730 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700731 CHECK_LT(entries, 1U << 16);
732 (*table)[2] = entries & 0xFF;
733 (*table)[3] = (entries >> 8) & 0xFF;
734 }
735
736 void AddEntry(uint32_t native_offset, const uint8_t* references) {
737 size_t table_index = TableIndex(native_offset);
738 while (in_use_[table_index]) {
739 table_index = (table_index + 1) % entries_;
740 }
741 in_use_[table_index] = true;
742 SetNativeOffset(table_index, native_offset);
743 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
744 SetReferences(table_index, references);
745 }
746
747 private:
748 size_t TableIndex(uint32_t native_offset) {
749 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
750 }
751
752 uint32_t GetNativeOffset(size_t table_index) {
753 uint32_t native_offset = 0;
754 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
755 for (size_t i = 0; i < native_offset_width_; i++) {
756 native_offset |= (*table_)[table_offset + i] << (i * 8);
757 }
758 return native_offset;
759 }
760
761 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
762 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
763 for (size_t i = 0; i < native_offset_width_; i++) {
764 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
765 }
766 }
767
768 void SetReferences(size_t table_index, const uint8_t* references) {
769 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
770 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
771 }
772
773 size_t EntryWidth() const {
774 return native_offset_width_ + references_width_;
775 }
776
777 // Number of entries in the table.
778 const size_t entries_;
779 // Number of bytes used to encode the reference bitmap.
780 const size_t references_width_;
781 // Number of bytes used to encode a native offset.
782 size_t native_offset_width_;
783 // Entries that are in use.
784 std::vector<bool> in_use_;
785 // The table we're building.
786 std::vector<uint8_t>* const table_;
787};
788
buzbeefa57c472012-11-21 12:06:18 -0800789static void CreateNativeGcMap(CompilationUnit* cu) {
790 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700791 uint32_t max_native_offset = 0;
792 for (size_t i = 0; i < mapping_table.size(); i += 2) {
793 uint32_t native_offset = mapping_table[i + 0];
794 if (native_offset > max_native_offset) {
795 max_native_offset = native_offset;
796 }
797 }
buzbeefa57c472012-11-21 12:06:18 -0800798 Compiler::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700799 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
800 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
801 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800802 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700803 mapping_table.size() / 2, max_native_offset,
804 dex_gc_map.RegWidth());
805
806 for (size_t i = 0; i < mapping_table.size(); i += 2) {
807 uint32_t native_offset = mapping_table[i + 0];
808 uint32_t dex_pc = mapping_table[i + 1];
809 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700810 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
811 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700812 }
813}
814
buzbeee3acd072012-02-25 17:03:10 -0800815/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800816static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800817{
buzbeefa57c472012-11-21 12:06:18 -0800818 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
819 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
820 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700821 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800822}
823
buzbeefa57c472012-11-21 12:06:18 -0800824static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800825{
Bill Buzbeea114add2012-05-03 15:00:40 -0700826 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800827 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800829 SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
830 if (tab_rec == NULL) break;
831 tab_rec->offset = offset;
832 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
833 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 } else {
buzbeefa57c472012-11-21 12:06:18 -0800835 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700836 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800837 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800838 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 }
840 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800841}
842
buzbeefa57c472012-11-21 12:06:18 -0800843static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800844{
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800846 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 while (true) {
buzbeefa57c472012-11-21 12:06:18 -0800848 FillArrayData *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -0800849 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800850 if (tab_rec == NULL) break;
851 tab_rec->offset = offset;
852 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700853 // word align
854 offset = (offset + 3) & ~3;
855 }
856 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800857}
858
buzbeea3a82b22012-11-27 16:09:55 -0800859// LIR offset assignment.
860static int AssignInsnOffsets(CompilationUnit* cu)
861{
862 LIR* lir;
863 int offset = 0;
864
865 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
866 lir->offset = offset;
867 if (lir->opcode >= 0) {
868 if (!lir->flags.is_nop) {
869 offset += lir->flags.size;
870 }
871 } else if (lir->opcode == kPseudoPseudoAlign4) {
872 if (offset & 0x2) {
873 offset += 2;
874 lir->operands[0] = 1;
875 } else {
876 lir->operands[0] = 0;
877 }
878 }
879 /* Pseudo opcodes don't consume space */
880 }
881
882 return offset;
883}
884
buzbeee3acd072012-02-25 17:03:10 -0800885/*
886 * Walk the compilation unit and assign offsets to instructions
887 * and literals and compute the total size of the compiled unit.
888 */
buzbeefa57c472012-11-21 12:06:18 -0800889static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800890{
buzbeea3a82b22012-11-27 16:09:55 -0800891 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800892
Bill Buzbeea114add2012-05-03 15:00:40 -0700893 /* Const values have to be word aligned */
894 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800895
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800897 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800898
buzbeefa57c472012-11-21 12:06:18 -0800899 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800900
buzbeefa57c472012-11-21 12:06:18 -0800901 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800902
buzbeefa57c472012-11-21 12:06:18 -0800903 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800904
buzbeefa57c472012-11-21 12:06:18 -0800905 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800906}
907
908/*
909 * Go over each instruction in the list and calculate the offset from the top
910 * before sending them off to the assembler. If out-of-range branch distance is
911 * seen rearrange the instructions a bit to correct it.
912 */
buzbeefa57c472012-11-21 12:06:18 -0800913void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800914{
buzbee02031b12012-11-23 09:41:35 -0800915 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800916 AssignOffsets(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700917 /*
918 * Assemble here. Note that we generate code with optimistic assumptions
919 * and if found now to work, we'll have to redo the sequence and retry.
920 */
buzbeee3acd072012-02-25 17:03:10 -0800921
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800923 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 if (res == kSuccess) {
925 break;
926 } else {
buzbeefa57c472012-11-21 12:06:18 -0800927 cu->assembler_retries++;
928 if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) {
929 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 LOG(FATAL) << "Assembler error - too many retries";
931 }
932 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800933 AssignOffsets(cu);
934 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800935 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 }
buzbeee3acd072012-02-25 17:03:10 -0800937
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800939 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800940
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800942 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800943
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800945 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800946
Ian Rogers0c7abda2012-09-19 13:33:42 -0700947 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800948 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700949
buzbeefa57c472012-11-21 12:06:18 -0800950 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800951}
952
buzbee31a4a6f2012-02-28 15:36:15 -0800953/*
954 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
955 * offset vaddr. This label will be used to fix up the case
956 * branch table during the assembly phase. Be sure to set
957 * all resource flags on this to prevent code motion across
958 * target boundaries. KeyVal is just there for debugging.
959 */
buzbeefa57c472012-11-21 12:06:18 -0800960static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800961{
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800963 it = cu->boundary_map.find(vaddr);
964 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700965 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
966 }
buzbeefa57c472012-11-21 12:06:18 -0800967 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
968 new_label->dalvik_offset = vaddr;
969 new_label->opcode = kPseudoCaseLabel;
970 new_label->operands[0] = keyVal;
971 InsertLIRAfter(it->second, new_label);
972 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -0800973}
974
buzbeefa57c472012-11-21 12:06:18 -0800975static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -0800976{
buzbeefa57c472012-11-21 12:06:18 -0800977 const uint16_t* table = tab_rec->table;
978 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -0800979 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -0800981 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700982 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800983 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 }
buzbee31a4a6f2012-02-28 15:36:15 -0800985}
986
buzbeefa57c472012-11-21 12:06:18 -0800987static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -0800988{
buzbeefa57c472012-11-21 12:06:18 -0800989 const uint16_t* table = tab_rec->table;
990 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -0800992 const int* keys = reinterpret_cast<const int*>(&table[2]);
993 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -0700994 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800995 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 }
buzbee31a4a6f2012-02-28 15:36:15 -0800997}
998
buzbeefa57c472012-11-21 12:06:18 -0800999void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001000{
Bill Buzbeea114add2012-05-03 15:00:40 -07001001 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001002 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 while (true) {
buzbeefa57c472012-11-21 12:06:18 -08001004 SwitchTable *tab_rec =
buzbee52a77fc2012-11-20 19:50:46 -08001005 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001006 if (tab_rec == NULL) break;
1007 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1008 MarkPackedCaseLabels(cu, tab_rec);
1009 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1010 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001011 } else {
1012 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001013 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 }
buzbee31a4a6f2012-02-28 15:36:15 -08001015}
1016
buzbee52a77fc2012-11-20 19:50:46 -08001017void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 /*
1019 * Sparse switch data format:
1020 * ushort ident = 0x0200 magic value
1021 * ushort size number of entries in the table; > 0
1022 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1023 * int targets[size] branch targets, relative to switch opcode
1024 *
1025 * Total size is (2+size*4) 16-bit code units.
1026 */
buzbee31a4a6f2012-02-28 15:36:15 -08001027{
buzbeeeaf09bc2012-11-15 14:51:41 -08001028 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001029 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001030 const int* keys = reinterpret_cast<const int*>(&table[2]);
1031 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1033 << ", entries: " << std::dec << entries;
1034 for (int i = 0; i < entries; i++) {
1035 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1036 }
buzbee31a4a6f2012-02-28 15:36:15 -08001037}
1038
buzbee52a77fc2012-11-20 19:50:46 -08001039void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 /*
1041 * Packed switch data format:
1042 * ushort ident = 0x0100 magic value
1043 * ushort size number of entries in the table
1044 * int first_key first (and lowest) switch case value
1045 * int targets[size] branch targets, relative to switch opcode
1046 *
1047 * Total size is (4+size*2) 16-bit code units.
1048 */
buzbee31a4a6f2012-02-28 15:36:15 -08001049{
buzbeeeaf09bc2012-11-15 14:51:41 -08001050 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001051 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001053 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001055 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001056 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001057 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 << targets[i];
1059 }
buzbee31a4a6f2012-02-28 15:36:15 -08001060}
buzbeee3acd072012-02-25 17:03:10 -08001061
buzbeed1643e42012-09-05 14:06:51 -07001062/*
1063 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001064 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001065 * which we split a single Dalvik instruction, only the first MIR op
1066 * associated with a Dalvik PC should be entered into the map.
1067 */
buzbeefa57c472012-11-21 12:06:18 -08001068LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001069{
buzbeefa57c472012-11-21 12:06:18 -08001070 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1071 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1072 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001073 }
1074 return res;
1075}
buzbeee3acd072012-02-25 17:03:10 -08001076
buzbeee6285f92012-12-06 15:57:46 -08001077bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2)
1078{
1079 bool is_taken;
1080 switch (opcode) {
1081 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
1082 case Instruction::IF_NE: is_taken = (src1 != src2); break;
1083 case Instruction::IF_LT: is_taken = (src1 < src2); break;
1084 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
1085 case Instruction::IF_GT: is_taken = (src1 > src2); break;
1086 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
1087 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
1088 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
1089 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
1090 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
1091 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
1092 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
1093 default:
1094 LOG(FATAL) << "Unexpected opcode " << opcode;
1095 is_taken = false;
1096 }
1097 return is_taken;
1098}
1099
buzbeea3a82b22012-11-27 16:09:55 -08001100} // namespace art