blob: b5152df994c6bdfc762ce741d489c7746ba8f142 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee395116c2013-02-27 14:30:25 -080017#include "compiler/dex/compiler_internals.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070018#include "dex_file-inl.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070019#include "gc_map.h"
20#include "verifier/dex_gc_map.h"
21#include "verifier/method_verifier.h"
buzbee1bc37c62012-11-20 13:35:41 -080022#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080023#include "codegen_util.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070024
buzbeee3acd072012-02-25 17:03:10 -080025namespace art {
26
buzbee4ef3e452012-12-14 13:35:28 -080027bool IsInexpensiveConstant(CompilationUnit* cu, RegLocation rl_src)
28{
29 bool res = false;
30 if (rl_src.is_const) {
31 if (rl_src.wide) {
32 if (rl_src.fp) {
buzbee311ca162013-02-28 15:56:43 -080033 res = cu->cg->InexpensiveConstantDouble(cu->mir_graph->ConstantValueWide(rl_src));
buzbee4ef3e452012-12-14 13:35:28 -080034 } else {
buzbee311ca162013-02-28 15:56:43 -080035 res = cu->cg->InexpensiveConstantLong(cu->mir_graph->ConstantValueWide(rl_src));
buzbee4ef3e452012-12-14 13:35:28 -080036 }
37 } else {
38 if (rl_src.fp) {
buzbee311ca162013-02-28 15:56:43 -080039 res = cu->cg->InexpensiveConstantFloat(cu->mir_graph->ConstantValue(rl_src));
buzbee4ef3e452012-12-14 13:35:28 -080040 } else {
buzbee311ca162013-02-28 15:56:43 -080041 res = cu->cg->InexpensiveConstantInt(cu->mir_graph->ConstantValue(rl_src));
buzbee4ef3e452012-12-14 13:35:28 -080042 }
43 }
44 }
45 return res;
46}
47
buzbee02031b12012-11-23 09:41:35 -080048void MarkSafepointPC(CompilationUnit* cu, LIR* inst)
49{
50 inst->def_mask = ENCODE_ALL;
51 LIR* safepoint_pc = NewLIR0(cu, kPseudoSafepointPC);
52 DCHECK_EQ(safepoint_pc->def_mask, ENCODE_ALL);
53}
54
55bool FastInstance(CompilationUnit* cu, uint32_t field_idx,
56 int& field_offset, bool& is_volatile, bool is_put)
57{
buzbee311ca162013-02-28 15:56:43 -080058 return cu->compiler_driver->ComputeInstanceFieldInfo(
59 field_idx, cu->mir_graph->GetCurrentDexCompilationUnit(), field_offset, is_volatile, is_put);
buzbee02031b12012-11-23 09:41:35 -080060}
61
buzbeecbd6d442012-11-17 14:11:25 -080062/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080063void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080064{
buzbeefa57c472012-11-21 12:06:18 -080065 lir->flags.is_nop = true;
buzbeecbd6d442012-11-17 14:11:25 -080066}
67
buzbee02031b12012-11-23 09:41:35 -080068void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type)
buzbee31a4a6f2012-02-28 15:36:15 -080069{
buzbeefa57c472012-11-21 12:06:18 -080070 uint64_t *mask_ptr;
buzbeeeaf09bc2012-11-15 14:51:41 -080071 uint64_t mask = ENCODE_MEM;;
buzbee02031b12012-11-23 09:41:35 -080072 Codegen* cg = cu->cg.get();
73 DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080074 if (is_load) {
75 mask_ptr = &lir->use_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070076 } else {
buzbeefa57c472012-11-21 12:06:18 -080077 mask_ptr = &lir->def_mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070078 }
79 /* Clear out the memref flags */
buzbeefa57c472012-11-21 12:06:18 -080080 *mask_ptr &= ~mask;
Bill Buzbeea114add2012-05-03 15:00:40 -070081 /* ..and then add back the one we need */
buzbeefa57c472012-11-21 12:06:18 -080082 switch (mem_type) {
Bill Buzbeea114add2012-05-03 15:00:40 -070083 case kLiteral:
buzbeefa57c472012-11-21 12:06:18 -080084 DCHECK(is_load);
85 *mask_ptr |= ENCODE_LITERAL;
Bill Buzbeea114add2012-05-03 15:00:40 -070086 break;
87 case kDalvikReg:
buzbeefa57c472012-11-21 12:06:18 -080088 *mask_ptr |= ENCODE_DALVIK_REG;
Bill Buzbeea114add2012-05-03 15:00:40 -070089 break;
90 case kHeapRef:
buzbeefa57c472012-11-21 12:06:18 -080091 *mask_ptr |= ENCODE_HEAP_REF;
Bill Buzbeea114add2012-05-03 15:00:40 -070092 break;
93 case kMustNotAlias:
94 /* Currently only loads can be marked as kMustNotAlias */
buzbee02031b12012-11-23 09:41:35 -080095 DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE));
buzbeefa57c472012-11-21 12:06:18 -080096 *mask_ptr |= ENCODE_MUST_NOT_ALIAS;
Bill Buzbeea114add2012-05-03 15:00:40 -070097 break;
98 default:
buzbeefa57c472012-11-21 12:06:18 -080099 LOG(FATAL) << "Oat: invalid memref kind - " << mem_type;
Bill Buzbeea114add2012-05-03 15:00:40 -0700100 }
buzbee31a4a6f2012-02-28 15:36:15 -0800101}
102
103/*
Ian Rogersb5d09b22012-03-06 22:14:17 -0800104 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -0800105 */
buzbee02031b12012-11-23 09:41:35 -0800106void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -0800107{
buzbee02031b12012-11-23 09:41:35 -0800108 SetMemRefType(cu, lir, is_load, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -0800109
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 /*
buzbeefa57c472012-11-21 12:06:18 -0800111 * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit
Bill Buzbeea114add2012-05-03 15:00:40 -0700112 * access.
113 */
buzbeefa57c472012-11-21 12:06:18 -0800114 lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -0800115}
116
117/*
118 * Mark the corresponding bit(s).
119 */
buzbeefa57c472012-11-21 12:06:18 -0800120void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -0800121{
buzbee02031b12012-11-23 09:41:35 -0800122 Codegen* cg = cu->cg.get();
123 *mask |= cg->GetRegMaskCommon(cu, reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800124}
125
126/*
127 * Set up the proper fields in the resource mask
128 */
buzbeefa57c472012-11-21 12:06:18 -0800129void SetupResourceMasks(CompilationUnit* cu, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -0800130{
Bill Buzbeea114add2012-05-03 15:00:40 -0700131 int opcode = lir->opcode;
buzbee02031b12012-11-23 09:41:35 -0800132 Codegen* cg = cu->cg.get();
buzbee31a4a6f2012-02-28 15:36:15 -0800133
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 if (opcode <= 0) {
buzbeefa57c472012-11-21 12:06:18 -0800135 lir->use_mask = lir->def_mask = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 return;
137 }
buzbee31a4a6f2012-02-28 15:36:15 -0800138
buzbee02031b12012-11-23 09:41:35 -0800139 uint64_t flags = cg->GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800140
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 if (flags & NEEDS_FIXUP) {
142 lir->flags.pcRelFixup = true;
143 }
buzbee31a4a6f2012-02-28 15:36:15 -0800144
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 /* Get the starting size of the instruction's template */
buzbee02031b12012-11-23 09:41:35 -0800146 lir->flags.size = cg->GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800147
Bill Buzbeea114add2012-05-03 15:00:40 -0700148 /* Set up the mask for resources that are updated */
149 if (flags & (IS_LOAD | IS_STORE)) {
150 /* Default to heap - will catch specialized classes later */
buzbee02031b12012-11-23 09:41:35 -0800151 SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 }
buzbee31a4a6f2012-02-28 15:36:15 -0800153
Bill Buzbeea114add2012-05-03 15:00:40 -0700154 /*
155 * Conservatively assume the branch here will call out a function that in
156 * turn will trash everything.
157 */
158 if (flags & IS_BRANCH) {
buzbeefa57c472012-11-21 12:06:18 -0800159 lir->def_mask = lir->use_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700160 return;
161 }
buzbee31a4a6f2012-02-28 15:36:15 -0800162
Bill Buzbeea114add2012-05-03 15:00:40 -0700163 if (flags & REG_DEF0) {
buzbeefa57c472012-11-21 12:06:18 -0800164 SetupRegMask(cu, &lir->def_mask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 }
buzbee31a4a6f2012-02-28 15:36:15 -0800166
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 if (flags & REG_DEF1) {
buzbeefa57c472012-11-21 12:06:18 -0800168 SetupRegMask(cu, &lir->def_mask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 }
buzbee31a4a6f2012-02-28 15:36:15 -0800170
buzbee31a4a6f2012-02-28 15:36:15 -0800171
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 if (flags & SETS_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800173 lir->def_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 }
buzbee31a4a6f2012-02-28 15:36:15 -0800175
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
177 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800178
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 for (i = 0; i < 4; i++) {
180 if (flags & (1 << (kRegUse0 + i))) {
buzbeefa57c472012-11-21 12:06:18 -0800181 SetupRegMask(cu, &lir->use_mask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 }
buzbee31a4a6f2012-02-28 15:36:15 -0800183 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 }
buzbee31a4a6f2012-02-28 15:36:15 -0800185
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 if (flags & USES_CCODES) {
buzbeefa57c472012-11-21 12:06:18 -0800187 lir->use_mask |= ENCODE_CCODE;
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 }
buzbee31a4a6f2012-02-28 15:36:15 -0800189
buzbeeb046e162012-10-30 15:48:42 -0700190 // Handle target-specific actions
buzbee02031b12012-11-23 09:41:35 -0800191 cg->SetupTargetResourceMasks(cu, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800192}
193
194/*
buzbee5de34942012-03-01 14:51:57 -0800195 * Debugging macros
196 */
197#define DUMP_RESOURCE_MASK(X)
buzbee5de34942012-03-01 14:51:57 -0800198
199/* Pretty-print a LIR instruction */
buzbeefa57c472012-11-21 12:06:18 -0800200void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr)
buzbee5de34942012-03-01 14:51:57 -0800201{
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 int offset = lir->offset;
203 int dest = lir->operands[0];
buzbeefa57c472012-11-21 12:06:18 -0800204 const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops));
buzbee02031b12012-11-23 09:41:35 -0800205 Codegen* cg = cu->cg.get();
buzbee5de34942012-03-01 14:51:57 -0800206
Bill Buzbeea114add2012-05-03 15:00:40 -0700207 /* Handle pseudo-ops individually, and all regular insns as a group */
208 switch (lir->opcode) {
209 case kPseudoMethodEntry:
210 LOG(INFO) << "-------- method entry "
buzbeefa57c472012-11-21 12:06:18 -0800211 << PrettyMethod(cu->method_idx, *cu->dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
213 case kPseudoMethodExit:
214 LOG(INFO) << "-------- Method_Exit";
215 break;
216 case kPseudoBarrier:
217 LOG(INFO) << "-------- BARRIER";
218 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700219 case kPseudoEntryBlock:
220 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
221 break;
222 case kPseudoDalvikByteCodeBoundary:
buzbee4ef3e452012-12-14 13:35:28 -0800223 if (lir->operands[0] == 0) {
224 lir->operands[0] = reinterpret_cast<uintptr_t>("No instruction string");
225 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeefa57c472012-11-21 12:06:18 -0800227 << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 break;
229 case kPseudoExitBlock:
230 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
231 break;
232 case kPseudoPseudoAlign4:
buzbeefa57c472012-11-21 12:06:18 -0800233 LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 << offset << "): .align4";
235 break;
236 case kPseudoEHBlockLabel:
237 LOG(INFO) << "Exception_Handling:";
238 break;
239 case kPseudoTargetLabel:
240 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800241 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 break;
243 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800244 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 break;
246 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800247 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 break;
249 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800250 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 break;
buzbee8320f382012-09-11 16:29:42 -0700252 case kPseudoSafepointPC:
buzbeefa57c472012-11-21 12:06:18 -0800253 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
buzbee8320f382012-09-11 16:29:42 -0700254 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700255 case kPseudoExportedPC:
buzbeefa57c472012-11-21 12:06:18 -0800256 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":";
Bill Buzbeea5b30242012-09-28 07:19:44 -0700257 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800259 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 << std::hex << lir->operands[0] << "|" << std::dec <<
261 lir->operands[0];
262 break;
263 default:
buzbeefa57c472012-11-21 12:06:18 -0800264 if (lir->flags.is_nop && !dump_nop) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 break;
266 } else {
buzbee02031b12012-11-23 09:41:35 -0800267 std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode),
268 lir, base_addr));
269 std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode),
270 lir, base_addr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeefa57c472012-11-21 12:06:18 -0800272 reinterpret_cast<unsigned int>(base_addr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700273 op_name.c_str(), op_operands.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800274 lir->flags.is_nop ? "(nop)" : "");
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 }
276 break;
277 }
buzbee5de34942012-03-01 14:51:57 -0800278
buzbeefa57c472012-11-21 12:06:18 -0800279 if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) {
280 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700281 }
buzbeefa57c472012-11-21 12:06:18 -0800282 if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) {
283 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 }
buzbee5de34942012-03-01 14:51:57 -0800285}
286
buzbeefa57c472012-11-21 12:06:18 -0800287void DumpPromotionMap(CompilationUnit *cu)
buzbee5de34942012-03-01 14:51:57 -0800288{
buzbee02031b12012-11-23 09:41:35 -0800289 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800290 int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1;
291 for (int i = 0; i < num_regs; i++) {
292 PromotionMap v_reg_map = cu->promotion_map[i];
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 std::string buf;
buzbeefa57c472012-11-21 12:06:18 -0800294 if (v_reg_map.fp_location == kLocPhysReg) {
buzbee02031b12012-11-23 09:41:35 -0800295 StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800296 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700297
298 std::string buf3;
buzbeefa57c472012-11-21 12:06:18 -0800299 if (i < cu->num_dalvik_registers) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 StringAppendF(&buf3, "%02d", i);
buzbeefa57c472012-11-21 12:06:18 -0800301 } else if (i == cu->method_sreg) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700302 buf3 = "Method*";
303 } else {
buzbeefa57c472012-11-21 12:06:18 -0800304 StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers);
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 }
306
307 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbeefa57c472012-11-21 12:06:18 -0800308 v_reg_map.core_location == kLocPhysReg ?
309 "r" : "SP+", v_reg_map.core_location == kLocPhysReg ?
310 v_reg_map.core_reg : SRegOffset(cu, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700311 buf.c_str());
312 }
buzbee5de34942012-03-01 14:51:57 -0800313}
314
Bill Buzbeea5b30242012-09-28 07:19:44 -0700315/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800316static void DumpMappingTable(const char* table_name, const std::string& descriptor,
317 const std::string& name, const std::string& signature,
318 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700319 if (v.size() > 0) {
320 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
321 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
322 std::replace(line.begin(), line.end(), ';', '_');
323 LOG(INFO) << line;
324 for (uint32_t i = 0; i < v.size(); i+=2) {
325 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
326 LOG(INFO) << line;
327 }
328 LOG(INFO) <<" };\n\n";
329 }
330}
331
buzbee5de34942012-03-01 14:51:57 -0800332/* Dump instructions and constant pool contents */
buzbeefa57c472012-11-21 12:06:18 -0800333void CodegenDump(CompilationUnit* cu)
buzbee5de34942012-03-01 14:51:57 -0800334{
Bill Buzbeea114add2012-05-03 15:00:40 -0700335 LOG(INFO) << "Dumping LIR insns for "
buzbeefa57c472012-11-21 12:06:18 -0800336 << PrettyMethod(cu->method_idx, *cu->dex_file);
337 LIR* lir_insn;
buzbee311ca162013-02-28 15:56:43 -0800338 int insns_size = cu->code_item->insns_size_in_code_units_;
buzbee5de34942012-03-01 14:51:57 -0800339
buzbeefa57c472012-11-21 12:06:18 -0800340 LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs;
341 LOG(INFO) << "Ins : " << cu->num_ins;
342 LOG(INFO) << "Outs : " << cu->num_outs;
343 LOG(INFO) << "CoreSpills : " << cu->num_core_spills;
344 LOG(INFO) << "FPSpills : " << cu->num_fp_spills;
345 LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps;
346 LOG(INFO) << "Frame size : " << cu->frame_size;
347 LOG(INFO) << "code size is " << cu->total_size <<
348 " bytes, Dalvik size is " << insns_size * 2;
Bill Buzbeea114add2012-05-03 15:00:40 -0700349 LOG(INFO) << "expansion factor: "
buzbeefa57c472012-11-21 12:06:18 -0800350 << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2);
351 DumpPromotionMap(cu);
buzbee28c9a832012-11-21 15:39:13 -0800352 for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800353 DumpLIRInsn(cu, lir_insn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700354 }
buzbee28c9a832012-11-21 15:39:13 -0800355 for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) {
buzbeefa57c472012-11-21 12:06:18 -0800356 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset,
357 lir_insn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 }
buzbee5de34942012-03-01 14:51:57 -0800359
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 const DexFile::MethodId& method_id =
buzbeefa57c472012-11-21 12:06:18 -0800361 cu->dex_file->GetMethodId(cu->method_idx);
362 std::string signature(cu->dex_file->GetMethodSignature(method_id));
363 std::string name(cu->dex_file->GetMethodName(method_id));
364 std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800365
Bill Buzbeea5b30242012-09-28 07:19:44 -0700366 // Dump mapping tables
buzbeefa57c472012-11-21 12:06:18 -0800367 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable);
368 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800369}
370
buzbeea2ebdd72012-03-04 14:57:06 -0800371
buzbeefa57c472012-11-21 12:06:18 -0800372LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700373 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800374{
buzbeefa57c472012-11-21 12:06:18 -0800375 LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
376 insn->dalvik_offset = dalvik_offset;
Bill Buzbeea114add2012-05-03 15:00:40 -0700377 insn->opcode = opcode;
378 insn->operands[0] = op0;
379 insn->operands[1] = op1;
380 insn->operands[2] = op2;
381 insn->operands[3] = op3;
382 insn->operands[4] = op4;
383 insn->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800384 SetupResourceMasks(cu, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700385 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
386 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700387 // Always make labels scheduling barriers
buzbeefa57c472012-11-21 12:06:18 -0800388 insn->use_mask = insn->def_mask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700389 }
390 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800391}
392
buzbee5de34942012-03-01 14:51:57 -0800393/*
buzbee31a4a6f2012-02-28 15:36:15 -0800394 * The following are building blocks to construct low-level IRs with 0 - 4
395 * operands.
396 */
buzbeefa57c472012-11-21 12:06:18 -0800397LIR* NewLIR0(CompilationUnit* cu, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800398{
buzbee02031b12012-11-23 09:41:35 -0800399 Codegen* cg = cu->cg.get();
400 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND))
401 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800402 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
403 << cu->current_dalvik_offset;
404 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode);
405 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700406 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800407}
408
buzbeefa57c472012-11-21 12:06:18 -0800409LIR* NewLIR1(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800411{
buzbee02031b12012-11-23 09:41:35 -0800412 Codegen* cg = cu->cg.get();
413 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP))
414 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800415 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
416 << cu->current_dalvik_offset;
417 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest);
418 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700419 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800420}
421
buzbeefa57c472012-11-21 12:06:18 -0800422LIR* NewLIR2(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800424{
buzbee02031b12012-11-23 09:41:35 -0800425 Codegen* cg = cu->cg.get();
426 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP))
427 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800428 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
429 << cu->current_dalvik_offset;
430 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1);
431 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800433}
434
buzbeefa57c472012-11-21 12:06:18 -0800435LIR* NewLIR3(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800437{
buzbee02031b12012-11-23 09:41:35 -0800438 Codegen* cg = cu->cg.get();
439 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
440 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800441 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
442 << cu->current_dalvik_offset;
443 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2);
444 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700445 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800446}
447
buzbeefa57c472012-11-21 12:06:18 -0800448LIR* NewLIR4(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700449 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800450{
buzbee02031b12012-11-23 09:41:35 -0800451 Codegen* cg = cu->cg.get();
452 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP))
453 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800454 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
455 << cu->current_dalvik_offset;
456 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info);
457 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800459}
buzbee31a4a6f2012-02-28 15:36:15 -0800460
buzbeefa57c472012-11-21 12:06:18 -0800461LIR* NewLIR5(CompilationUnit* cu, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700462 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800463{
buzbee02031b12012-11-23 09:41:35 -0800464 Codegen* cg = cu->cg.get();
465 DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP))
466 << cg->GetTargetInstName(opcode) << " " << opcode << " "
buzbeefa57c472012-11-21 12:06:18 -0800467 << PrettyMethod(cu->method_idx, *cu->dex_file) << " "
468 << cu->current_dalvik_offset;
469 LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2);
470 AppendLIR(cu, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800472}
473
buzbee31a4a6f2012-02-28 15:36:15 -0800474/*
475 * Search the existing constants in the literal pool for an exact or close match
476 * within specified delta (greater or equal to 0).
477 */
buzbeefa57c472012-11-21 12:06:18 -0800478LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800479{
buzbeefa57c472012-11-21 12:06:18 -0800480 while (data_target) {
481 if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta)
482 return data_target;
483 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700484 }
485 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800486}
487
488/* Search the existing constants in the literal pool for an exact wide match */
buzbeefa57c472012-11-21 12:06:18 -0800489LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800490{
buzbeefa57c472012-11-21 12:06:18 -0800491 bool lo_match = false;
492 LIR* lo_target = NULL;
493 while (data_target) {
494 if (lo_match && (data_target->operands[0] == val_hi)) {
buzbee4ef3e452012-12-14 13:35:28 -0800495 // Record high word in case we need to expand this later.
496 lo_target->operands[1] = val_hi;
buzbeefa57c472012-11-21 12:06:18 -0800497 return lo_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800498 }
buzbeefa57c472012-11-21 12:06:18 -0800499 lo_match = false;
500 if (data_target->operands[0] == val_lo) {
501 lo_match = true;
502 lo_target = data_target;
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 }
buzbeefa57c472012-11-21 12:06:18 -0800504 data_target = data_target->next;
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 }
506 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800507}
508
509/*
510 * The following are building blocks to insert constants into the pool or
511 * instruction streams.
512 */
513
buzbee4ef3e452012-12-14 13:35:28 -0800514/* Add a 32-bit constant to the constant pool */
buzbeefa57c472012-11-21 12:06:18 -0800515LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800516{
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 /* Add the constant to the literal pool */
buzbeefa57c472012-11-21 12:06:18 -0800518 if (constant_list_p) {
519 LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData));
520 new_value->operands[0] = value;
521 new_value->next = *constant_list_p;
522 *constant_list_p = new_value;
523 return new_value;
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 }
525 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800526}
527
528/* Add a 64-bit constant to the constant pool or mixed with code */
buzbeefa57c472012-11-21 12:06:18 -0800529LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p,
530 int val_lo, int val_hi)
buzbee31a4a6f2012-02-28 15:36:15 -0800531{
buzbeefa57c472012-11-21 12:06:18 -0800532 AddWordData(cu, constant_list_p, val_hi);
533 return AddWordData(cu, constant_list_p, val_lo);
buzbee31a4a6f2012-02-28 15:36:15 -0800534}
535
buzbeeaad94382012-11-21 07:40:50 -0800536static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 buf.push_back( data & 0xff);
538 buf.push_back( (data >> 8) & 0xff);
539 buf.push_back( (data >> 16) & 0xff);
540 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800541}
542
buzbeeaad94382012-11-21 07:40:50 -0800543static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700544 while (buf.size() < offset) {
545 buf.push_back(0);
546 }
buzbeee3acd072012-02-25 17:03:10 -0800547}
548
549/* Write the literal pool to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800550static void InstallLiteralPools(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800551{
buzbeefa57c472012-11-21 12:06:18 -0800552 AlignBuffer(cu->code_buffer, cu->data_offset);
553 LIR* data_lir = cu->literal_list;
554 while (data_lir != NULL) {
555 PushWord(cu->code_buffer, data_lir->operands[0]);
556 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700557 }
558 // Push code and method literals, record offsets for the compiler to patch.
buzbeefa57c472012-11-21 12:06:18 -0800559 data_lir = cu->code_literal_list;
560 while (data_lir != NULL) {
561 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800562 cu->compiler_driver->AddCodePatch(cu->dex_file,
563 cu->method_idx,
564 cu->invoke_type,
565 target,
566 static_cast<InvokeType>(data_lir->operands[1]),
567 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800568 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700569 // unique based on target to ensure code deduplication works
570 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800571 PushWord(cu->code_buffer, unique_patch_value);
572 data_lir = NEXT_LIR(data_lir);
Ian Rogers137e88f2012-10-08 17:46:47 -0700573 }
buzbeefa57c472012-11-21 12:06:18 -0800574 data_lir = cu->method_literal_list;
575 while (data_lir != NULL) {
576 uint32_t target = data_lir->operands[0];
Ian Rogers1212a022013-03-04 10:48:41 -0800577 cu->compiler_driver->AddMethodPatch(cu->dex_file,
578 cu->method_idx,
579 cu->invoke_type,
580 target,
581 static_cast<InvokeType>(data_lir->operands[1]),
582 cu->code_buffer.size());
buzbeefa57c472012-11-21 12:06:18 -0800583 const DexFile::MethodId& id = cu->dex_file->GetMethodId(target);
Ian Rogers137e88f2012-10-08 17:46:47 -0700584 // unique based on target to ensure code deduplication works
585 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbeefa57c472012-11-21 12:06:18 -0800586 PushWord(cu->code_buffer, unique_patch_value);
587 data_lir = NEXT_LIR(data_lir);
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 }
buzbeee3acd072012-02-25 17:03:10 -0800589}
590
591/* Write the switch tables to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800592static void InstallSwitchTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800593{
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800595 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 while (true) {
buzbee311ca162013-02-28 15:56:43 -0800597 Codegen::SwitchTable* tab_rec =
598 reinterpret_cast<Codegen::SwitchTable*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800599 if (tab_rec == NULL) break;
600 AlignBuffer(cu->code_buffer, tab_rec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 /*
602 * For Arm, our reference point is the address of the bx
603 * instruction that does the launch, so we have to subtract
604 * the auto pc-advance. For other targets the reference point
605 * is a label, so we can use the offset as-is.
606 */
buzbeefa57c472012-11-21 12:06:18 -0800607 int bx_offset = INVALID_OFFSET;
608 switch (cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700609 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800610 bx_offset = tab_rec->anchor->offset + 4;
buzbeeb046e162012-10-30 15:48:42 -0700611 break;
612 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800613 bx_offset = 0;
buzbeeb046e162012-10-30 15:48:42 -0700614 break;
615 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800616 bx_offset = tab_rec->anchor->offset;
buzbeeb046e162012-10-30 15:48:42 -0700617 break;
buzbeefa57c472012-11-21 12:06:18 -0800618 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700619 }
buzbeefa57c472012-11-21 12:06:18 -0800620 if (cu->verbose) {
621 LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset;
buzbeee3acd072012-02-25 17:03:10 -0800622 }
buzbeefa57c472012-11-21 12:06:18 -0800623 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
624 const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2]));
625 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
626 int disp = tab_rec->targets[elems]->offset - bx_offset;
627 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700628 LOG(INFO) << " Case[" << elems << "] key: 0x"
629 << std::hex << keys[elems] << ", disp: 0x"
630 << std::hex << disp;
631 }
buzbeefa57c472012-11-21 12:06:18 -0800632 PushWord(cu->code_buffer, keys[elems]);
633 PushWord(cu->code_buffer,
634 tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700635 }
636 } else {
buzbeefa57c472012-11-21 12:06:18 -0800637 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700638 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800639 for (int elems = 0; elems < tab_rec->table[1]; elems++) {
640 int disp = tab_rec->targets[elems]->offset - bx_offset;
641 if (cu->verbose) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700642 LOG(INFO) << " Case[" << elems << "] disp: 0x"
643 << std::hex << disp;
644 }
buzbeefa57c472012-11-21 12:06:18 -0800645 PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 }
647 }
648 }
buzbeee3acd072012-02-25 17:03:10 -0800649}
650
651/* Write the fill array dta to the output stream */
buzbeefa57c472012-11-21 12:06:18 -0800652static void InstallFillArrayData(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800653{
Bill Buzbeea114add2012-05-03 15:00:40 -0700654 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800655 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700656 while (true) {
buzbee311ca162013-02-28 15:56:43 -0800657 Codegen::FillArrayData *tab_rec =
658 reinterpret_cast<Codegen::FillArrayData*>(GrowableListIteratorNext( &iterator));
buzbeefa57c472012-11-21 12:06:18 -0800659 if (tab_rec == NULL) break;
660 AlignBuffer(cu->code_buffer, tab_rec->offset);
661 for (int i = 0; i < (tab_rec->size + 1) / 2; i++) {
662 cu->code_buffer.push_back( tab_rec->table[i] & 0xFF);
663 cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800664 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 }
buzbeee3acd072012-02-25 17:03:10 -0800666}
667
buzbeeaad94382012-11-21 07:40:50 -0800668static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800669{
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 for (;lir != NULL; lir = lir->next) {
671 lir->offset = offset;
672 offset += 4;
673 }
674 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800675}
676
buzbee6459e7c2012-10-02 14:42:41 -0700677// Make sure we have a code address for every declared catch entry
buzbeefa57c472012-11-21 12:06:18 -0800678static bool VerifyCatchEntries(CompilationUnit* cu)
buzbee6459e7c2012-10-02 14:42:41 -0700679{
680 bool success = true;
buzbee311ca162013-02-28 15:56:43 -0800681 for (std::set<uint32_t>::const_iterator it = cu->mir_graph->catches_.begin();
682 it != cu->mir_graph->catches_.end(); ++it) {
buzbeefa57c472012-11-21 12:06:18 -0800683 uint32_t dex_pc = *it;
buzbee6459e7c2012-10-02 14:42:41 -0700684 bool found = false;
buzbeefa57c472012-11-21 12:06:18 -0800685 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
686 if (dex_pc == cu->dex2pcMappingTable[i+1]) {
buzbee6459e7c2012-10-02 14:42:41 -0700687 found = true;
688 break;
689 }
690 }
691 if (!found) {
buzbeefa57c472012-11-21 12:06:18 -0800692 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700693 success = false;
694 }
695 }
696 // Now, try in the other direction
buzbeefa57c472012-11-21 12:06:18 -0800697 for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) {
698 uint32_t dex_pc = cu->dex2pcMappingTable[i+1];
buzbee311ca162013-02-28 15:56:43 -0800699 if (cu->mir_graph->catches_.find(dex_pc) == cu->mir_graph->catches_.end()) {
buzbeefa57c472012-11-21 12:06:18 -0800700 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc;
buzbee6459e7c2012-10-02 14:42:41 -0700701 success = false;
702 }
703 }
704 if (!success) {
buzbeefa57c472012-11-21 12:06:18 -0800705 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file);
buzbee311ca162013-02-28 15:56:43 -0800706 LOG(INFO) << "Entries @ decode: " << cu->mir_graph->catches_.size() << ", Entries in table: "
buzbeefa57c472012-11-21 12:06:18 -0800707 << cu->dex2pcMappingTable.size()/2;
buzbee6459e7c2012-10-02 14:42:41 -0700708 }
709 return success;
710}
711
buzbee311ca162013-02-28 15:56:43 -0800712
buzbeefa57c472012-11-21 12:06:18 -0800713static void CreateMappingTables(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800714{
buzbeefa57c472012-11-21 12:06:18 -0800715 for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) {
716 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) {
717 cu->pc2dexMappingTable.push_back(tgt_lir->offset);
718 cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700719 }
buzbeefa57c472012-11-21 12:06:18 -0800720 if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) {
721 cu->dex2pcMappingTable.push_back(tgt_lir->offset);
722 cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset);
buzbeee3acd072012-02-25 17:03:10 -0800723 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 }
buzbee311ca162013-02-28 15:56:43 -0800725 if (kIsDebugBuild) {
726 DCHECK(VerifyCatchEntries(cu));
727 }
buzbeefa57c472012-11-21 12:06:18 -0800728 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() +
729 cu->dex2pcMappingTable.size());
730 cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size());
731 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
732 cu->pc2dexMappingTable.begin(),
733 cu->pc2dexMappingTable.end());
734 cu->combined_mapping_table.insert(cu->combined_mapping_table.end(),
735 cu->dex2pcMappingTable.begin(),
736 cu->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800737}
738
Ian Rogers0c7abda2012-09-19 13:33:42 -0700739class NativePcToReferenceMapBuilder {
740 public:
741 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
742 size_t entries, uint32_t max_native_offset,
743 size_t references_width) : entries_(entries),
744 references_width_(references_width), in_use_(entries),
745 table_(table) {
746 // Compute width in bytes needed to hold max_native_offset.
747 native_offset_width_ = 0;
748 while (max_native_offset != 0) {
749 native_offset_width_++;
750 max_native_offset >>= 8;
751 }
752 // Resize table and set up header.
753 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700754 CHECK_LT(native_offset_width_, 1U << 3);
755 (*table)[0] = native_offset_width_ & 7;
756 CHECK_LT(references_width_, 1U << 13);
757 (*table)[0] |= (references_width_ << 3) & 0xFF;
758 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700759 CHECK_LT(entries, 1U << 16);
760 (*table)[2] = entries & 0xFF;
761 (*table)[3] = (entries >> 8) & 0xFF;
762 }
763
764 void AddEntry(uint32_t native_offset, const uint8_t* references) {
765 size_t table_index = TableIndex(native_offset);
766 while (in_use_[table_index]) {
767 table_index = (table_index + 1) % entries_;
768 }
769 in_use_[table_index] = true;
770 SetNativeOffset(table_index, native_offset);
771 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
772 SetReferences(table_index, references);
773 }
774
775 private:
776 size_t TableIndex(uint32_t native_offset) {
777 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
778 }
779
780 uint32_t GetNativeOffset(size_t table_index) {
781 uint32_t native_offset = 0;
782 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
783 for (size_t i = 0; i < native_offset_width_; i++) {
784 native_offset |= (*table_)[table_offset + i] << (i * 8);
785 }
786 return native_offset;
787 }
788
789 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
790 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
791 for (size_t i = 0; i < native_offset_width_; i++) {
792 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
793 }
794 }
795
796 void SetReferences(size_t table_index, const uint8_t* references) {
797 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
798 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
799 }
800
801 size_t EntryWidth() const {
802 return native_offset_width_ + references_width_;
803 }
804
805 // Number of entries in the table.
806 const size_t entries_;
807 // Number of bytes used to encode the reference bitmap.
808 const size_t references_width_;
809 // Number of bytes used to encode a native offset.
810 size_t native_offset_width_;
811 // Entries that are in use.
812 std::vector<bool> in_use_;
813 // The table we're building.
814 std::vector<uint8_t>* const table_;
815};
816
buzbeefa57c472012-11-21 12:06:18 -0800817static void CreateNativeGcMap(CompilationUnit* cu) {
818 const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700819 uint32_t max_native_offset = 0;
820 for (size_t i = 0; i < mapping_table.size(); i += 2) {
821 uint32_t native_offset = mapping_table[i + 0];
822 if (native_offset > max_native_offset) {
823 max_native_offset = native_offset;
824 }
825 }
Ian Rogers1212a022013-03-04 10:48:41 -0800826 CompilerDriver::MethodReference method_ref(cu->dex_file, cu->method_idx);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700827 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
828 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
829 // Compute native offset to references size.
buzbeefa57c472012-11-21 12:06:18 -0800830 NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700831 mapping_table.size() / 2, max_native_offset,
832 dex_gc_map.RegWidth());
833
834 for (size_t i = 0; i < mapping_table.size(); i += 2) {
835 uint32_t native_offset = mapping_table[i + 0];
836 uint32_t dex_pc = mapping_table[i + 1];
837 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700838 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
839 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700840 }
841}
842
buzbeee3acd072012-02-25 17:03:10 -0800843/* Determine the offset of each literal field */
buzbeefa57c472012-11-21 12:06:18 -0800844static int AssignLiteralOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800845{
buzbeefa57c472012-11-21 12:06:18 -0800846 offset = AssignLiteralOffsetCommon(cu->literal_list, offset);
847 offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset);
848 offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700849 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800850}
851
buzbeefa57c472012-11-21 12:06:18 -0800852static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800853{
Bill Buzbeea114add2012-05-03 15:00:40 -0700854 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800855 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700856 while (true) {
buzbee311ca162013-02-28 15:56:43 -0800857 Codegen::SwitchTable *tab_rec =
858 reinterpret_cast<Codegen::SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800859 if (tab_rec == NULL) break;
860 tab_rec->offset = offset;
861 if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
862 offset += tab_rec->table[1] * (sizeof(int) * 2);
Bill Buzbeea114add2012-05-03 15:00:40 -0700863 } else {
buzbeefa57c472012-11-21 12:06:18 -0800864 DCHECK_EQ(static_cast<int>(tab_rec->table[0]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700865 static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeefa57c472012-11-21 12:06:18 -0800866 offset += tab_rec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800867 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 }
869 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800870}
871
buzbeefa57c472012-11-21 12:06:18 -0800872static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800873{
Bill Buzbeea114add2012-05-03 15:00:40 -0700874 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -0800875 GrowableListIteratorInit(&cu->fill_array_data, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 while (true) {
buzbee311ca162013-02-28 15:56:43 -0800877 Codegen::FillArrayData *tab_rec =
878 reinterpret_cast<Codegen::FillArrayData*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -0800879 if (tab_rec == NULL) break;
880 tab_rec->offset = offset;
881 offset += tab_rec->size;
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 // word align
883 offset = (offset + 3) & ~3;
884 }
885 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800886}
887
buzbeea3a82b22012-11-27 16:09:55 -0800888// LIR offset assignment.
889static int AssignInsnOffsets(CompilationUnit* cu)
890{
891 LIR* lir;
892 int offset = 0;
893
894 for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) {
895 lir->offset = offset;
896 if (lir->opcode >= 0) {
897 if (!lir->flags.is_nop) {
898 offset += lir->flags.size;
899 }
900 } else if (lir->opcode == kPseudoPseudoAlign4) {
901 if (offset & 0x2) {
902 offset += 2;
903 lir->operands[0] = 1;
904 } else {
905 lir->operands[0] = 0;
906 }
907 }
908 /* Pseudo opcodes don't consume space */
909 }
910
911 return offset;
912}
913
buzbeee3acd072012-02-25 17:03:10 -0800914/*
915 * Walk the compilation unit and assign offsets to instructions
916 * and literals and compute the total size of the compiled unit.
917 */
buzbeefa57c472012-11-21 12:06:18 -0800918static void AssignOffsets(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800919{
buzbeea3a82b22012-11-27 16:09:55 -0800920 int offset = AssignInsnOffsets(cu);
buzbeee3acd072012-02-25 17:03:10 -0800921
Bill Buzbeea114add2012-05-03 15:00:40 -0700922 /* Const values have to be word aligned */
923 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800924
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 /* Set up offsets for literals */
buzbeefa57c472012-11-21 12:06:18 -0800926 cu->data_offset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800927
buzbeefa57c472012-11-21 12:06:18 -0800928 offset = AssignLiteralOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800929
buzbeefa57c472012-11-21 12:06:18 -0800930 offset = AssignSwitchTablesOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800931
buzbeefa57c472012-11-21 12:06:18 -0800932 offset = AssignFillArrayDataOffset(cu, offset);
buzbeee3acd072012-02-25 17:03:10 -0800933
buzbeefa57c472012-11-21 12:06:18 -0800934 cu->total_size = offset;
buzbeee3acd072012-02-25 17:03:10 -0800935}
936
937/*
938 * Go over each instruction in the list and calculate the offset from the top
939 * before sending them off to the assembler. If out-of-range branch distance is
940 * seen rearrange the instructions a bit to correct it.
941 */
buzbeefa57c472012-11-21 12:06:18 -0800942void AssembleLIR(CompilationUnit* cu)
buzbeee3acd072012-02-25 17:03:10 -0800943{
buzbee02031b12012-11-23 09:41:35 -0800944 Codegen* cg = cu->cg.get();
buzbeefa57c472012-11-21 12:06:18 -0800945 AssignOffsets(cu);
buzbee311ca162013-02-28 15:56:43 -0800946 int assembler_retries = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 /*
948 * Assemble here. Note that we generate code with optimistic assumptions
949 * and if found now to work, we'll have to redo the sequence and retry.
950 */
buzbeee3acd072012-02-25 17:03:10 -0800951
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 while (true) {
buzbee02031b12012-11-23 09:41:35 -0800953 AssemblerStatus res = cg->AssembleInstructions(cu, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 if (res == kSuccess) {
955 break;
956 } else {
buzbee311ca162013-02-28 15:56:43 -0800957 assembler_retries++;
958 if (assembler_retries > MAX_ASSEMBLER_RETRIES) {
buzbeefa57c472012-11-21 12:06:18 -0800959 CodegenDump(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 LOG(FATAL) << "Assembler error - too many retries";
961 }
962 // Redo offsets and try again
buzbeefa57c472012-11-21 12:06:18 -0800963 AssignOffsets(cu);
964 cu->code_buffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800965 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 }
buzbeee3acd072012-02-25 17:03:10 -0800967
Bill Buzbeea114add2012-05-03 15:00:40 -0700968 // Install literals
buzbeefa57c472012-11-21 12:06:18 -0800969 InstallLiteralPools(cu);
buzbeee3acd072012-02-25 17:03:10 -0800970
Bill Buzbeea114add2012-05-03 15:00:40 -0700971 // Install switch tables
buzbeefa57c472012-11-21 12:06:18 -0800972 InstallSwitchTables(cu);
buzbeee3acd072012-02-25 17:03:10 -0800973
Bill Buzbeea114add2012-05-03 15:00:40 -0700974 // Install fill array data
buzbeefa57c472012-11-21 12:06:18 -0800975 InstallFillArrayData(cu);
buzbeee3acd072012-02-25 17:03:10 -0800976
Ian Rogers0c7abda2012-09-19 13:33:42 -0700977 // Create the mapping table and native offset to reference map.
buzbeefa57c472012-11-21 12:06:18 -0800978 CreateMappingTables(cu);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700979
buzbeefa57c472012-11-21 12:06:18 -0800980 CreateNativeGcMap(cu);
buzbeee3acd072012-02-25 17:03:10 -0800981}
982
buzbee31a4a6f2012-02-28 15:36:15 -0800983/*
984 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
985 * offset vaddr. This label will be used to fix up the case
986 * branch table during the assembly phase. Be sure to set
987 * all resource flags on this to prevent code motion across
988 * target boundaries. KeyVal is just there for debugging.
989 */
buzbeefa57c472012-11-21 12:06:18 -0800990static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800991{
Bill Buzbeea114add2012-05-03 15:00:40 -0700992 SafeMap<unsigned int, LIR*>::iterator it;
buzbeefa57c472012-11-21 12:06:18 -0800993 it = cu->boundary_map.find(vaddr);
994 if (it == cu->boundary_map.end()) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700995 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
996 }
buzbeefa57c472012-11-21 12:06:18 -0800997 LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR));
998 new_label->dalvik_offset = vaddr;
999 new_label->opcode = kPseudoCaseLabel;
1000 new_label->operands[0] = keyVal;
1001 InsertLIRAfter(it->second, new_label);
1002 return new_label;
buzbee31a4a6f2012-02-28 15:36:15 -08001003}
1004
buzbee311ca162013-02-28 15:56:43 -08001005static void MarkPackedCaseLabels(CompilationUnit* cu, Codegen::SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001006{
buzbeefa57c472012-11-21 12:06:18 -08001007 const uint16_t* table = tab_rec->table;
1008 int base_vaddr = tab_rec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -08001009 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001010 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001011 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001012 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001013 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key);
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 }
buzbee31a4a6f2012-02-28 15:36:15 -08001015}
1016
buzbee311ca162013-02-28 15:56:43 -08001017static void MarkSparseCaseLabels(CompilationUnit* cu, Codegen::SwitchTable *tab_rec)
buzbee31a4a6f2012-02-28 15:36:15 -08001018{
buzbeefa57c472012-11-21 12:06:18 -08001019 const uint16_t* table = tab_rec->table;
1020 int base_vaddr = tab_rec->vaddr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001021 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001022 const int* keys = reinterpret_cast<const int*>(&table[2]);
1023 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001024 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001025 tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 }
buzbee31a4a6f2012-02-28 15:36:15 -08001027}
1028
buzbeefa57c472012-11-21 12:06:18 -08001029void ProcessSwitchTables(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -08001030{
Bill Buzbeea114add2012-05-03 15:00:40 -07001031 GrowableListIterator iterator;
buzbeefa57c472012-11-21 12:06:18 -08001032 GrowableListIteratorInit(&cu->switch_tables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -07001033 while (true) {
buzbee311ca162013-02-28 15:56:43 -08001034 Codegen::SwitchTable *tab_rec =
1035 reinterpret_cast<Codegen::SwitchTable*>(GrowableListIteratorNext(&iterator));
buzbeefa57c472012-11-21 12:06:18 -08001036 if (tab_rec == NULL) break;
1037 if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) {
1038 MarkPackedCaseLabels(cu, tab_rec);
1039 } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) {
1040 MarkSparseCaseLabels(cu, tab_rec);
Bill Buzbeea114add2012-05-03 15:00:40 -07001041 } else {
1042 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001043 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 }
buzbee31a4a6f2012-02-28 15:36:15 -08001045}
1046
buzbee52a77fc2012-11-20 19:50:46 -08001047void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001048 /*
1049 * Sparse switch data format:
1050 * ushort ident = 0x0200 magic value
1051 * ushort size number of entries in the table; > 0
1052 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1053 * int targets[size] branch targets, relative to switch opcode
1054 *
1055 * Total size is (2+size*4) 16-bit code units.
1056 */
buzbee31a4a6f2012-02-28 15:36:15 -08001057{
buzbeeeaf09bc2012-11-15 14:51:41 -08001058 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -07001059 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -08001060 const int* keys = reinterpret_cast<const int*>(&table[2]);
1061 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -07001062 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1063 << ", entries: " << std::dec << entries;
1064 for (int i = 0; i < entries; i++) {
1065 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1066 }
buzbee31a4a6f2012-02-28 15:36:15 -08001067}
1068
buzbee52a77fc2012-11-20 19:50:46 -08001069void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 /*
1071 * Packed switch data format:
1072 * ushort ident = 0x0100 magic value
1073 * ushort size number of entries in the table
1074 * int first_key first (and lowest) switch case value
1075 * int targets[size] branch targets, relative to switch opcode
1076 *
1077 * Total size is (4+size*2) 16-bit code units.
1078 */
buzbee31a4a6f2012-02-28 15:36:15 -08001079{
buzbeeeaf09bc2012-11-15 14:51:41 -08001080 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001081 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001082 int entries = table[1];
buzbeefa57c472012-11-21 12:06:18 -08001083 int low_key = s4FromSwitchData(&table[2]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001084 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
buzbeefa57c472012-11-21 12:06:18 -08001085 << ", entries: " << std::dec << entries << ", low_key: " << low_key;
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 for (int i = 0; i < entries; i++) {
buzbeefa57c472012-11-21 12:06:18 -08001087 LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 << targets[i];
1089 }
buzbee31a4a6f2012-02-28 15:36:15 -08001090}
buzbeee3acd072012-02-25 17:03:10 -08001091
buzbeed1643e42012-09-05 14:06:51 -07001092/*
1093 * Set up special LIR to mark a Dalvik byte-code instruction start and
buzbeefa57c472012-11-21 12:06:18 -08001094 * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in
buzbeed1643e42012-09-05 14:06:51 -07001095 * which we split a single Dalvik instruction, only the first MIR op
1096 * associated with a Dalvik PC should be entered into the map.
1097 */
buzbeefa57c472012-11-21 12:06:18 -08001098LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str)
buzbeed1643e42012-09-05 14:06:51 -07001099{
buzbeefa57c472012-11-21 12:06:18 -08001100 LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str));
1101 if (cu->boundary_map.find(offset) == cu->boundary_map.end()) {
1102 cu->boundary_map.Put(offset, res);
buzbeed1643e42012-09-05 14:06:51 -07001103 }
1104 return res;
1105}
buzbeee3acd072012-02-25 17:03:10 -08001106
buzbeee6285f92012-12-06 15:57:46 -08001107bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2)
1108{
1109 bool is_taken;
1110 switch (opcode) {
1111 case Instruction::IF_EQ: is_taken = (src1 == src2); break;
1112 case Instruction::IF_NE: is_taken = (src1 != src2); break;
1113 case Instruction::IF_LT: is_taken = (src1 < src2); break;
1114 case Instruction::IF_GE: is_taken = (src1 >= src2); break;
1115 case Instruction::IF_GT: is_taken = (src1 > src2); break;
1116 case Instruction::IF_LE: is_taken = (src1 <= src2); break;
1117 case Instruction::IF_EQZ: is_taken = (src1 == 0); break;
1118 case Instruction::IF_NEZ: is_taken = (src1 != 0); break;
1119 case Instruction::IF_LTZ: is_taken = (src1 < 0); break;
1120 case Instruction::IF_GEZ: is_taken = (src1 >= 0); break;
1121 case Instruction::IF_GTZ: is_taken = (src1 > 0); break;
1122 case Instruction::IF_LEZ: is_taken = (src1 <= 0); break;
1123 default:
1124 LOG(FATAL) << "Unexpected opcode " << opcode;
1125 is_taken = false;
1126 }
1127 return is_taken;
1128}
1129
buzbee4ef3e452012-12-14 13:35:28 -08001130// Convert relation of src1/src2 to src2/src1
1131ConditionCode FlipComparisonOrder(ConditionCode before) {
1132 ConditionCode res;
1133 switch (before) {
1134 case kCondEq: res = kCondEq; break;
1135 case kCondNe: res = kCondNe; break;
1136 case kCondLt: res = kCondGt; break;
1137 case kCondGt: res = kCondLt; break;
1138 case kCondLe: res = kCondGe; break;
1139 case kCondGe: res = kCondLe; break;
1140 default:
1141 res = static_cast<ConditionCode>(0);
1142 LOG(FATAL) << "Unexpected ccode " << before;
1143 }
1144 return res;
1145}
1146
buzbeea3a82b22012-11-27 16:09:55 -08001147} // namespace art