blob: 50821852ed30146760a17bd4d1777d48b9668a25 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
buzbee1bc37c62012-11-20 13:35:41 -080017#include "../compiler_internals.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070018#include "gc_map.h"
19#include "verifier/dex_gc_map.h"
20#include "verifier/method_verifier.h"
buzbee1bc37c62012-11-20 13:35:41 -080021#include "ralloc_util.h"
buzbeeeaf09bc2012-11-15 14:51:41 -080022#include "codegen_util.h"
Ian Rogers0c7abda2012-09-19 13:33:42 -070023
buzbeee3acd072012-02-25 17:03:10 -080024namespace art {
25
buzbeecbd6d442012-11-17 14:11:25 -080026/* Convert an instruction to a NOP */
buzbee52a77fc2012-11-20 19:50:46 -080027void NopLIR( LIR* lir)
buzbeecbd6d442012-11-17 14:11:25 -080028{
29 lir->flags.isNop = true;
30}
31
buzbee52a77fc2012-11-20 19:50:46 -080032void SetMemRefType(LIR* lir, bool isLoad, int memType)
buzbee31a4a6f2012-02-28 15:36:15 -080033{
buzbeeeaf09bc2012-11-15 14:51:41 -080034 uint64_t *maskPtr;
35 uint64_t mask = ENCODE_MEM;;
buzbee52a77fc2012-11-20 19:50:46 -080036 DCHECK(GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -070037 if (isLoad) {
38 maskPtr = &lir->useMask;
39 } else {
40 maskPtr = &lir->defMask;
41 }
42 /* Clear out the memref flags */
43 *maskPtr &= ~mask;
44 /* ..and then add back the one we need */
45 switch (memType) {
46 case kLiteral:
47 DCHECK(isLoad);
48 *maskPtr |= ENCODE_LITERAL;
49 break;
50 case kDalvikReg:
51 *maskPtr |= ENCODE_DALVIK_REG;
52 break;
53 case kHeapRef:
54 *maskPtr |= ENCODE_HEAP_REF;
55 break;
56 case kMustNotAlias:
57 /* Currently only loads can be marked as kMustNotAlias */
buzbee52a77fc2012-11-20 19:50:46 -080058 DCHECK(!(GetTargetInstFlags(lir->opcode) & IS_STORE));
Bill Buzbeea114add2012-05-03 15:00:40 -070059 *maskPtr |= ENCODE_MUST_NOT_ALIAS;
60 break;
61 default:
62 LOG(FATAL) << "Oat: invalid memref kind - " << memType;
63 }
buzbee31a4a6f2012-02-28 15:36:15 -080064}
65
66/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080067 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -080068 */
buzbee52a77fc2012-11-20 19:50:46 -080069void AnnotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -080070{
buzbee52a77fc2012-11-20 19:50:46 -080071 SetMemRefType(lir, isLoad, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -080072
Bill Buzbeea114add2012-05-03 15:00:40 -070073 /*
74 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
75 * access.
76 */
buzbeeec137432012-11-13 12:13:16 -080077 lir->aliasInfo = ENCODE_ALIAS_INFO(regId, is64bit);
buzbee31a4a6f2012-02-28 15:36:15 -080078}
79
80/*
81 * Mark the corresponding bit(s).
82 */
buzbee52a77fc2012-11-20 19:50:46 -080083void SetupRegMask(CompilationUnit* cUnit, uint64_t* mask, int reg)
buzbee31a4a6f2012-02-28 15:36:15 -080084{
buzbee52a77fc2012-11-20 19:50:46 -080085 *mask |= GetRegMaskCommon(cUnit, reg);
buzbee31a4a6f2012-02-28 15:36:15 -080086}
87
88/*
89 * Set up the proper fields in the resource mask
90 */
buzbee52a77fc2012-11-20 19:50:46 -080091void SetupResourceMasks(CompilationUnit* cUnit, LIR* lir)
buzbee31a4a6f2012-02-28 15:36:15 -080092{
Bill Buzbeea114add2012-05-03 15:00:40 -070093 int opcode = lir->opcode;
buzbee31a4a6f2012-02-28 15:36:15 -080094
Bill Buzbeea114add2012-05-03 15:00:40 -070095 if (opcode <= 0) {
96 lir->useMask = lir->defMask = 0;
97 return;
98 }
buzbee31a4a6f2012-02-28 15:36:15 -080099
buzbee52a77fc2012-11-20 19:50:46 -0800100 uint64_t flags = GetTargetInstFlags(opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800101
Bill Buzbeea114add2012-05-03 15:00:40 -0700102 if (flags & NEEDS_FIXUP) {
103 lir->flags.pcRelFixup = true;
104 }
buzbee31a4a6f2012-02-28 15:36:15 -0800105
Bill Buzbeea114add2012-05-03 15:00:40 -0700106 /* Get the starting size of the instruction's template */
buzbee52a77fc2012-11-20 19:50:46 -0800107 lir->flags.size = GetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800108
Bill Buzbeea114add2012-05-03 15:00:40 -0700109 /* Set up the mask for resources that are updated */
110 if (flags & (IS_LOAD | IS_STORE)) {
111 /* Default to heap - will catch specialized classes later */
buzbee52a77fc2012-11-20 19:50:46 -0800112 SetMemRefType(lir, flags & IS_LOAD, kHeapRef);
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 }
buzbee31a4a6f2012-02-28 15:36:15 -0800114
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 /*
116 * Conservatively assume the branch here will call out a function that in
117 * turn will trash everything.
118 */
119 if (flags & IS_BRANCH) {
120 lir->defMask = lir->useMask = ENCODE_ALL;
121 return;
122 }
buzbee31a4a6f2012-02-28 15:36:15 -0800123
Bill Buzbeea114add2012-05-03 15:00:40 -0700124 if (flags & REG_DEF0) {
buzbee52a77fc2012-11-20 19:50:46 -0800125 SetupRegMask(cUnit, &lir->defMask, lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700126 }
buzbee31a4a6f2012-02-28 15:36:15 -0800127
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 if (flags & REG_DEF1) {
buzbee52a77fc2012-11-20 19:50:46 -0800129 SetupRegMask(cUnit, &lir->defMask, lir->operands[1]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700130 }
buzbee31a4a6f2012-02-28 15:36:15 -0800131
buzbee31a4a6f2012-02-28 15:36:15 -0800132
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 if (flags & SETS_CCODES) {
134 lir->defMask |= ENCODE_CCODE;
135 }
buzbee31a4a6f2012-02-28 15:36:15 -0800136
Bill Buzbeea114add2012-05-03 15:00:40 -0700137 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
138 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800139
Bill Buzbeea114add2012-05-03 15:00:40 -0700140 for (i = 0; i < 4; i++) {
141 if (flags & (1 << (kRegUse0 + i))) {
buzbee52a77fc2012-11-20 19:50:46 -0800142 SetupRegMask(cUnit, &lir->useMask, lir->operands[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 }
buzbee31a4a6f2012-02-28 15:36:15 -0800144 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 }
buzbee31a4a6f2012-02-28 15:36:15 -0800146
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (flags & USES_CCODES) {
148 lir->useMask |= ENCODE_CCODE;
149 }
buzbee31a4a6f2012-02-28 15:36:15 -0800150
buzbeeb046e162012-10-30 15:48:42 -0700151 // Handle target-specific actions
buzbee52a77fc2012-11-20 19:50:46 -0800152 SetupTargetResourceMasks(cUnit, lir);
buzbee31a4a6f2012-02-28 15:36:15 -0800153}
154
155/*
buzbee5de34942012-03-01 14:51:57 -0800156 * Debugging macros
157 */
158#define DUMP_RESOURCE_MASK(X)
159#define DUMP_SSA_REP(X)
160
161/* Pretty-print a LIR instruction */
buzbee52a77fc2012-11-20 19:50:46 -0800162void DumpLIRInsn(CompilationUnit* cUnit, LIR* lir, unsigned char* baseAddr)
buzbee5de34942012-03-01 14:51:57 -0800163{
Bill Buzbeea114add2012-05-03 15:00:40 -0700164 int offset = lir->offset;
165 int dest = lir->operands[0];
166 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800167
Bill Buzbeea114add2012-05-03 15:00:40 -0700168 /* Handle pseudo-ops individually, and all regular insns as a group */
169 switch (lir->opcode) {
170 case kPseudoMethodEntry:
171 LOG(INFO) << "-------- method entry "
172 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
173 break;
174 case kPseudoMethodExit:
175 LOG(INFO) << "-------- Method_Exit";
176 break;
177 case kPseudoBarrier:
178 LOG(INFO) << "-------- BARRIER";
179 break;
180 case kPseudoExtended:
buzbeecbd6d442012-11-17 14:11:25 -0800181 LOG(INFO) << "-------- " << reinterpret_cast<char*>(dest);
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 break;
183 case kPseudoSSARep:
buzbeecbd6d442012-11-17 14:11:25 -0800184 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << reinterpret_cast<char*>(dest));
Bill Buzbeea114add2012-05-03 15:00:40 -0700185 break;
186 case kPseudoEntryBlock:
187 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
188 break;
189 case kPseudoDalvikByteCodeBoundary:
190 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
buzbeecbd6d442012-11-17 14:11:25 -0800191 << lir->dalvikOffset << " @ " << reinterpret_cast<char*>(lir->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 break;
193 case kPseudoExitBlock:
194 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
195 break;
196 case kPseudoPseudoAlign4:
buzbeecbd6d442012-11-17 14:11:25 -0800197 LOG(INFO) << reinterpret_cast<uintptr_t>(baseAddr) + offset << " (0x" << std::hex
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 << offset << "): .align4";
199 break;
200 case kPseudoEHBlockLabel:
201 LOG(INFO) << "Exception_Handling:";
202 break;
203 case kPseudoTargetLabel:
204 case kPseudoNormalBlockLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800205 LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 break;
207 case kPseudoThrowTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800208 LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 break;
210 case kPseudoIntrinsicRetry:
buzbeecbd6d442012-11-17 14:11:25 -0800211 LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700212 break;
213 case kPseudoSuspendTarget:
buzbeecbd6d442012-11-17 14:11:25 -0800214 LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":";
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 break;
buzbee8320f382012-09-11 16:29:42 -0700216 case kPseudoSafepointPC:
217 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvikOffset << ":";
218 break;
Bill Buzbeea5b30242012-09-28 07:19:44 -0700219 case kPseudoExportedPC:
220 LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvikOffset << ":";
221 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700222 case kPseudoCaseLabel:
buzbeecbd6d442012-11-17 14:11:25 -0800223 LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x"
Bill Buzbeea114add2012-05-03 15:00:40 -0700224 << std::hex << lir->operands[0] << "|" << std::dec <<
225 lir->operands[0];
226 break;
227 default:
228 if (lir->flags.isNop && !dumpNop) {
229 break;
230 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800231 std::string op_name(BuildInsnString(GetTargetInstName(lir->opcode),
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 lir, baseAddr));
buzbee52a77fc2012-11-20 19:50:46 -0800233 std::string op_operands(BuildInsnString(GetTargetInstFmt(lir->opcode),
buzbee1bc37c62012-11-20 13:35:41 -0800234 lir, baseAddr));
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
buzbeecbd6d442012-11-17 14:11:25 -0800236 reinterpret_cast<unsigned int>(baseAddr + offset),
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 op_name.c_str(), op_operands.c_str(),
238 lir->flags.isNop ? "(nop)" : "");
239 }
240 break;
241 }
buzbee5de34942012-03-01 14:51:57 -0800242
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
buzbee52a77fc2012-11-20 19:50:46 -0800244 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->useMask, "use"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 }
246 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
buzbee52a77fc2012-11-20 19:50:46 -0800247 DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->defMask, "def"));
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 }
buzbee5de34942012-03-01 14:51:57 -0800249}
250
buzbee52a77fc2012-11-20 19:50:46 -0800251void DumpPromotionMap(CompilationUnit *cUnit)
buzbee5de34942012-03-01 14:51:57 -0800252{
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
254 for (int i = 0; i < numRegs; i++) {
255 PromotionMap vRegMap = cUnit->promotionMap[i];
256 std::string buf;
257 if (vRegMap.fpLocation == kLocPhysReg) {
buzbee52a77fc2012-11-20 19:50:46 -0800258 StringAppendF(&buf, " : s%d", vRegMap.FpReg & FpRegMask());
buzbee5de34942012-03-01 14:51:57 -0800259 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700260
261 std::string buf3;
262 if (i < cUnit->numDalvikRegisters) {
263 StringAppendF(&buf3, "%02d", i);
264 } else if (i == cUnit->methodSReg) {
265 buf3 = "Method*";
266 } else {
267 StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters);
268 }
269
270 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
271 vRegMap.coreLocation == kLocPhysReg ?
272 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
buzbee52a77fc2012-11-20 19:50:46 -0800273 vRegMap.coreReg : SRegOffset(cUnit, i),
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 buf.c_str());
275 }
buzbee5de34942012-03-01 14:51:57 -0800276}
277
Bill Buzbeea5b30242012-09-28 07:19:44 -0700278/* Dump a mapping table */
buzbeeaad94382012-11-21 07:40:50 -0800279static void DumpMappingTable(const char* table_name, const std::string& descriptor,
280 const std::string& name, const std::string& signature,
281 const std::vector<uint32_t>& v) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700282 if (v.size() > 0) {
283 std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name,
284 descriptor.c_str(), name.c_str(), signature.c_str(), v.size()));
285 std::replace(line.begin(), line.end(), ';', '_');
286 LOG(INFO) << line;
287 for (uint32_t i = 0; i < v.size(); i+=2) {
288 line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]);
289 LOG(INFO) << line;
290 }
291 LOG(INFO) <<" };\n\n";
292 }
293}
294
buzbee5de34942012-03-01 14:51:57 -0800295/* Dump instructions and constant pool contents */
buzbee52a77fc2012-11-20 19:50:46 -0800296void CodegenDump(CompilationUnit* cUnit)
buzbee5de34942012-03-01 14:51:57 -0800297{
Bill Buzbeea114add2012-05-03 15:00:40 -0700298 LOG(INFO) << "Dumping LIR insns for "
299 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
300 LIR* lirInsn;
Bill Buzbeea114add2012-05-03 15:00:40 -0700301 int insnsSize = cUnit->insnsSize;
buzbee5de34942012-03-01 14:51:57 -0800302
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
304 LOG(INFO) << "Ins : " << cUnit->numIns;
305 LOG(INFO) << "Outs : " << cUnit->numOuts;
306 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
307 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
308 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
309 LOG(INFO) << "Frame size : " << cUnit->frameSize;
310 LOG(INFO) << "code size is " << cUnit->totalSize <<
311 " bytes, Dalvik size is " << insnsSize * 2;
312 LOG(INFO) << "expansion factor: "
buzbeecbd6d442012-11-17 14:11:25 -0800313 << static_cast<float>(cUnit->totalSize) / static_cast<float>(insnsSize * 2);
buzbee52a77fc2012-11-20 19:50:46 -0800314 DumpPromotionMap(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700315 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
buzbee52a77fc2012-11-20 19:50:46 -0800316 DumpLIRInsn(cUnit, lirInsn, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700318 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
buzbeecbd6d442012-11-17 14:11:25 -0800319 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lirInsn->offset, lirInsn->offset,
320 lirInsn->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 }
buzbee5de34942012-03-01 14:51:57 -0800322
Bill Buzbeea114add2012-05-03 15:00:40 -0700323 const DexFile::MethodId& method_id =
324 cUnit->dex_file->GetMethodId(cUnit->method_idx);
325 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
326 std::string name(cUnit->dex_file->GetMethodName(method_id));
327 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800328
Bill Buzbeea5b30242012-09-28 07:19:44 -0700329 // Dump mapping tables
buzbee52a77fc2012-11-20 19:50:46 -0800330 DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cUnit->pc2dexMappingTable);
331 DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cUnit->dex2pcMappingTable);
buzbee5de34942012-03-01 14:51:57 -0800332}
333
buzbeea2ebdd72012-03-04 14:57:06 -0800334
buzbee52a77fc2012-11-20 19:50:46 -0800335LIR* RawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800337{
buzbee52a77fc2012-11-20 19:50:46 -0800338 LIR* insn = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700339 insn->dalvikOffset = dalvikOffset;
340 insn->opcode = opcode;
341 insn->operands[0] = op0;
342 insn->operands[1] = op1;
343 insn->operands[2] = op2;
344 insn->operands[3] = op3;
345 insn->operands[4] = op4;
346 insn->target = target;
buzbee52a77fc2012-11-20 19:50:46 -0800347 SetupResourceMasks(cUnit, insn);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700348 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) ||
349 (opcode == kPseudoExportedPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700350 // Always make labels scheduling barriers
buzbee8320f382012-09-11 16:29:42 -0700351 insn->useMask = insn->defMask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 }
353 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800354}
355
buzbee5de34942012-03-01 14:51:57 -0800356/*
buzbee31a4a6f2012-02-28 15:36:15 -0800357 * The following are building blocks to construct low-level IRs with 0 - 4
358 * operands.
359 */
buzbee52a77fc2012-11-20 19:50:46 -0800360LIR* NewLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800361{
buzbee52a77fc2012-11-20 19:50:46 -0800362 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & NO_OPERAND))
363 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
365 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800366 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
367 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800369}
370
buzbee52a77fc2012-11-20 19:50:46 -0800371LIR* NewLIR1(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700372 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800373{
buzbee52a77fc2012-11-20 19:50:46 -0800374 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & IS_UNARY_OP))
375 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
377 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800378 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
379 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800381}
382
buzbee52a77fc2012-11-20 19:50:46 -0800383LIR* NewLIR2(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700384 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800385{
buzbee52a77fc2012-11-20 19:50:46 -0800386 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & IS_BINARY_OP))
387 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700388 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
389 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800390 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
391 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800393}
394
buzbee52a77fc2012-11-20 19:50:46 -0800395LIR* NewLIR3(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800397{
buzbee52a77fc2012-11-20 19:50:46 -0800398 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & IS_TERTIARY_OP))
399 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700400 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
401 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800402 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2);
403 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700404 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800405}
406
buzbee52a77fc2012-11-20 19:50:46 -0800407LIR* NewLIR4(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800409{
buzbee52a77fc2012-11-20 19:50:46 -0800410 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & IS_QUAD_OP))
411 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
413 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800414 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2, info);
415 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700416 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800417}
buzbee31a4a6f2012-02-28 15:36:15 -0800418
buzbee52a77fc2012-11-20 19:50:46 -0800419LIR* NewLIR5(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700420 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800421{
buzbee52a77fc2012-11-20 19:50:46 -0800422 DCHECK(isPseudoOpcode(opcode) || (GetTargetInstFlags(opcode) & IS_QUIN_OP))
423 << GetTargetInstName(opcode) << " " << opcode << " "
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
425 << cUnit->currentDalvikOffset;
buzbee52a77fc2012-11-20 19:50:46 -0800426 LIR* insn = RawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1, src2, info1, info2);
427 AppendLIR(cUnit, insn);
Bill Buzbeea114add2012-05-03 15:00:40 -0700428 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800429}
430
buzbee31a4a6f2012-02-28 15:36:15 -0800431/*
432 * Search the existing constants in the literal pool for an exact or close match
433 * within specified delta (greater or equal to 0).
434 */
buzbee52a77fc2012-11-20 19:50:46 -0800435LIR* ScanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
buzbee31a4a6f2012-02-28 15:36:15 -0800436{
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 while (dataTarget) {
buzbeecbd6d442012-11-17 14:11:25 -0800438 if ((static_cast<unsigned>(value - dataTarget->operands[0])) <= delta)
439 return dataTarget;
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 dataTarget = dataTarget->next;
441 }
442 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800443}
444
445/* Search the existing constants in the literal pool for an exact wide match */
buzbee52a77fc2012-11-20 19:50:46 -0800446LIR* ScanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800447{
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 bool loMatch = false;
449 LIR* loTarget = NULL;
450 while (dataTarget) {
buzbeecbd6d442012-11-17 14:11:25 -0800451 if (loMatch && (dataTarget->operands[0] == valHi)) {
452 return loTarget;
buzbee31a4a6f2012-02-28 15:36:15 -0800453 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 loMatch = false;
buzbeecbd6d442012-11-17 14:11:25 -0800455 if (dataTarget->operands[0] == valLo) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 loMatch = true;
457 loTarget = dataTarget;
458 }
459 dataTarget = dataTarget->next;
460 }
461 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800462}
463
464/*
465 * The following are building blocks to insert constants into the pool or
466 * instruction streams.
467 */
468
buzbee5de34942012-03-01 14:51:57 -0800469/* Add a 32-bit constant either in the constant pool */
buzbee52a77fc2012-11-20 19:50:46 -0800470LIR* AddWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800471{
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 /* Add the constant to the literal pool */
473 if (constantListP) {
buzbee52a77fc2012-11-20 19:50:46 -0800474 LIR* newValue = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocData));
Bill Buzbeea114add2012-05-03 15:00:40 -0700475 newValue->operands[0] = value;
476 newValue->next = *constantListP;
buzbeecbd6d442012-11-17 14:11:25 -0800477 *constantListP = newValue;
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 return newValue;
479 }
480 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800481}
482
483/* Add a 64-bit constant to the constant pool or mixed with code */
buzbee52a77fc2012-11-20 19:50:46 -0800484LIR* AddWideData(CompilationUnit* cUnit, LIR* *constantListP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800486{
buzbee52a77fc2012-11-20 19:50:46 -0800487 AddWordData(cUnit, constantListP, valHi);
488 return AddWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800489}
490
buzbeeaad94382012-11-21 07:40:50 -0800491static void PushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 buf.push_back( data & 0xff);
493 buf.push_back( (data >> 8) & 0xff);
494 buf.push_back( (data >> 16) & 0xff);
495 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800496}
497
buzbeeaad94382012-11-21 07:40:50 -0800498static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 while (buf.size() < offset) {
500 buf.push_back(0);
501 }
buzbeee3acd072012-02-25 17:03:10 -0800502}
503
504/* Write the literal pool to the output stream */
buzbeeaad94382012-11-21 07:40:50 -0800505static void InstallLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800506{
buzbee52a77fc2012-11-20 19:50:46 -0800507 AlignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 LIR* dataLIR = cUnit->literalList;
509 while (dataLIR != NULL) {
buzbee52a77fc2012-11-20 19:50:46 -0800510 PushWord(cUnit->codeBuffer, dataLIR->operands[0]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700511 dataLIR = NEXT_LIR(dataLIR);
512 }
513 // Push code and method literals, record offsets for the compiler to patch.
514 dataLIR = cUnit->codeLiteralList;
Ian Rogers137e88f2012-10-08 17:46:47 -0700515 while (dataLIR != NULL) {
516 uint32_t target = dataLIR->operands[0];
517 cUnit->compiler->AddCodePatch(cUnit->dex_file,
518 cUnit->method_idx,
519 cUnit->invoke_type,
520 target,
521 static_cast<InvokeType>(dataLIR->operands[1]),
522 cUnit->codeBuffer.size());
523 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
524 // unique based on target to ensure code deduplication works
525 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbee52a77fc2012-11-20 19:50:46 -0800526 PushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers137e88f2012-10-08 17:46:47 -0700527 dataLIR = NEXT_LIR(dataLIR);
528 }
529 dataLIR = cUnit->methodLiteralList;
530 while (dataLIR != NULL) {
531 uint32_t target = dataLIR->operands[0];
532 cUnit->compiler->AddMethodPatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700533 cUnit->method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700534 cUnit->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 target,
Ian Rogers08f753d2012-08-24 14:35:25 -0700536 static_cast<InvokeType>(dataLIR->operands[1]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 cUnit->codeBuffer.size());
Ian Rogers137e88f2012-10-08 17:46:47 -0700538 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
539 // unique based on target to ensure code deduplication works
540 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
buzbee52a77fc2012-11-20 19:50:46 -0800541 PushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers137e88f2012-10-08 17:46:47 -0700542 dataLIR = NEXT_LIR(dataLIR);
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 }
buzbeee3acd072012-02-25 17:03:10 -0800544}
545
546/* Write the switch tables to the output stream */
buzbeeaad94382012-11-21 07:40:50 -0800547static void InstallSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800548{
Bill Buzbeea114add2012-05-03 15:00:40 -0700549 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800550 GrowableListIteratorInit(&cUnit->switchTables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700551 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800552 SwitchTable* tabRec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700553 if (tabRec == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -0800554 AlignBuffer(cUnit->codeBuffer, tabRec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700555 /*
556 * For Arm, our reference point is the address of the bx
557 * instruction that does the launch, so we have to subtract
558 * the auto pc-advance. For other targets the reference point
559 * is a label, so we can use the offset as-is.
560 */
buzbeeb046e162012-10-30 15:48:42 -0700561 int bxOffset = INVALID_OFFSET;
562 switch (cUnit->instructionSet) {
563 case kThumb2:
564 bxOffset = tabRec->anchor->offset + 4;
565 break;
566 case kX86:
567 bxOffset = 0;
568 break;
569 case kMips:
570 bxOffset = tabRec->anchor->offset;
571 break;
572 default: LOG(FATAL) << "Unexpected instruction set: " << cUnit->instructionSet;
573 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700574 if (cUnit->printMe) {
575 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800576 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeecbd6d442012-11-17 14:11:25 -0800578 const int* keys = reinterpret_cast<const int*>(&(tabRec->table[2]));
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 for (int elems = 0; elems < tabRec->table[1]; elems++) {
580 int disp = tabRec->targets[elems]->offset - bxOffset;
581 if (cUnit->printMe) {
582 LOG(INFO) << " Case[" << elems << "] key: 0x"
583 << std::hex << keys[elems] << ", disp: 0x"
584 << std::hex << disp;
585 }
buzbee52a77fc2012-11-20 19:50:46 -0800586 PushWord(cUnit->codeBuffer, keys[elems]);
587 PushWord(cUnit->codeBuffer,
Bill Buzbeea114add2012-05-03 15:00:40 -0700588 tabRec->targets[elems]->offset - bxOffset);
589 }
590 } else {
591 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
592 static_cast<int>(Instruction::kPackedSwitchSignature));
593 for (int elems = 0; elems < tabRec->table[1]; elems++) {
594 int disp = tabRec->targets[elems]->offset - bxOffset;
595 if (cUnit->printMe) {
596 LOG(INFO) << " Case[" << elems << "] disp: 0x"
597 << std::hex << disp;
598 }
buzbee52a77fc2012-11-20 19:50:46 -0800599 PushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700600 }
601 }
602 }
buzbeee3acd072012-02-25 17:03:10 -0800603}
604
605/* Write the fill array dta to the output stream */
buzbeeaad94382012-11-21 07:40:50 -0800606static void InstallFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800607{
Bill Buzbeea114add2012-05-03 15:00:40 -0700608 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800609 GrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700610 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800611 FillArrayData *tabRec =
buzbee52a77fc2012-11-20 19:50:46 -0800612 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700613 if (tabRec == NULL) break;
buzbee52a77fc2012-11-20 19:50:46 -0800614 AlignBuffer(cUnit->codeBuffer, tabRec->offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
616 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
617 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800618 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700619 }
buzbeee3acd072012-02-25 17:03:10 -0800620}
621
buzbeeaad94382012-11-21 07:40:50 -0800622static int AssignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800623{
Bill Buzbeea114add2012-05-03 15:00:40 -0700624 for (;lir != NULL; lir = lir->next) {
625 lir->offset = offset;
626 offset += 4;
627 }
628 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800629}
630
buzbee6459e7c2012-10-02 14:42:41 -0700631// Make sure we have a code address for every declared catch entry
buzbeeaad94382012-11-21 07:40:50 -0800632static bool VerifyCatchEntries(CompilationUnit* cUnit)
buzbee6459e7c2012-10-02 14:42:41 -0700633{
634 bool success = true;
635 for (std::set<uint32_t>::const_iterator it = cUnit->catches.begin(); it != cUnit->catches.end(); ++it) {
636 uint32_t dexPc = *it;
637 bool found = false;
638 for (size_t i = 0; i < cUnit->dex2pcMappingTable.size(); i += 2) {
639 if (dexPc == cUnit->dex2pcMappingTable[i+1]) {
640 found = true;
641 break;
642 }
643 }
644 if (!found) {
645 LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dexPc;
646 success = false;
647 }
648 }
649 // Now, try in the other direction
650 for (size_t i = 0; i < cUnit->dex2pcMappingTable.size(); i += 2) {
651 uint32_t dexPc = cUnit->dex2pcMappingTable[i+1];
652 if (cUnit->catches.find(dexPc) == cUnit->catches.end()) {
653 LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dexPc;
654 success = false;
655 }
656 }
657 if (!success) {
658 LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
659 LOG(INFO) << "Entries @ decode: " << cUnit->catches.size() << ", Entries in table: "
660 << cUnit->dex2pcMappingTable.size()/2;
661 }
662 return success;
663}
664
buzbeeaad94382012-11-21 07:40:50 -0800665static void CreateMappingTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800666{
buzbeecbd6d442012-11-17 14:11:25 -0800667 for (LIR* tgtLIR = cUnit->firstLIRInsn; tgtLIR != NULL; tgtLIR = NEXT_LIR(tgtLIR)) {
buzbee8320f382012-09-11 16:29:42 -0700668 if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoSafepointPC)) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700669 cUnit->pc2dexMappingTable.push_back(tgtLIR->offset);
670 cUnit->pc2dexMappingTable.push_back(tgtLIR->dalvikOffset);
671 }
672 if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoExportedPC)) {
673 cUnit->dex2pcMappingTable.push_back(tgtLIR->offset);
674 cUnit->dex2pcMappingTable.push_back(tgtLIR->dalvikOffset);
buzbeee3acd072012-02-25 17:03:10 -0800675 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700676 }
buzbee52a77fc2012-11-20 19:50:46 -0800677 DCHECK(VerifyCatchEntries(cUnit));
Bill Buzbeea5b30242012-09-28 07:19:44 -0700678 cUnit->combinedMappingTable.push_back(cUnit->pc2dexMappingTable.size() +
679 cUnit->dex2pcMappingTable.size());
680 cUnit->combinedMappingTable.push_back(cUnit->pc2dexMappingTable.size());
681 cUnit->combinedMappingTable.insert(cUnit->combinedMappingTable.end(),
682 cUnit->pc2dexMappingTable.begin(),
683 cUnit->pc2dexMappingTable.end());
684 cUnit->combinedMappingTable.insert(cUnit->combinedMappingTable.end(),
685 cUnit->dex2pcMappingTable.begin(),
686 cUnit->dex2pcMappingTable.end());
buzbeee3acd072012-02-25 17:03:10 -0800687}
688
Ian Rogers0c7abda2012-09-19 13:33:42 -0700689class NativePcToReferenceMapBuilder {
690 public:
691 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
692 size_t entries, uint32_t max_native_offset,
693 size_t references_width) : entries_(entries),
694 references_width_(references_width), in_use_(entries),
695 table_(table) {
696 // Compute width in bytes needed to hold max_native_offset.
697 native_offset_width_ = 0;
698 while (max_native_offset != 0) {
699 native_offset_width_++;
700 max_native_offset >>= 8;
701 }
702 // Resize table and set up header.
703 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700704 CHECK_LT(native_offset_width_, 1U << 3);
705 (*table)[0] = native_offset_width_ & 7;
706 CHECK_LT(references_width_, 1U << 13);
707 (*table)[0] |= (references_width_ << 3) & 0xFF;
708 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700709 CHECK_LT(entries, 1U << 16);
710 (*table)[2] = entries & 0xFF;
711 (*table)[3] = (entries >> 8) & 0xFF;
712 }
713
714 void AddEntry(uint32_t native_offset, const uint8_t* references) {
715 size_t table_index = TableIndex(native_offset);
716 while (in_use_[table_index]) {
717 table_index = (table_index + 1) % entries_;
718 }
719 in_use_[table_index] = true;
720 SetNativeOffset(table_index, native_offset);
721 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
722 SetReferences(table_index, references);
723 }
724
725 private:
726 size_t TableIndex(uint32_t native_offset) {
727 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
728 }
729
730 uint32_t GetNativeOffset(size_t table_index) {
731 uint32_t native_offset = 0;
732 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
733 for (size_t i = 0; i < native_offset_width_; i++) {
734 native_offset |= (*table_)[table_offset + i] << (i * 8);
735 }
736 return native_offset;
737 }
738
739 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
740 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
741 for (size_t i = 0; i < native_offset_width_; i++) {
742 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
743 }
744 }
745
746 void SetReferences(size_t table_index, const uint8_t* references) {
747 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
748 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
749 }
750
751 size_t EntryWidth() const {
752 return native_offset_width_ + references_width_;
753 }
754
755 // Number of entries in the table.
756 const size_t entries_;
757 // Number of bytes used to encode the reference bitmap.
758 const size_t references_width_;
759 // Number of bytes used to encode a native offset.
760 size_t native_offset_width_;
761 // Entries that are in use.
762 std::vector<bool> in_use_;
763 // The table we're building.
764 std::vector<uint8_t>* const table_;
765};
766
buzbee52a77fc2012-11-20 19:50:46 -0800767static void CreateNativeGcMap(CompilationUnit* cUnit) {
Bill Buzbeea5b30242012-09-28 07:19:44 -0700768 const std::vector<uint32_t>& mapping_table = cUnit->pc2dexMappingTable;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700769 uint32_t max_native_offset = 0;
770 for (size_t i = 0; i < mapping_table.size(); i += 2) {
771 uint32_t native_offset = mapping_table[i + 0];
772 if (native_offset > max_native_offset) {
773 max_native_offset = native_offset;
774 }
775 }
776 Compiler::MethodReference method_ref(cUnit->dex_file, cUnit->method_idx);
777 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
778 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
779 // Compute native offset to references size.
780 NativePcToReferenceMapBuilder native_gc_map_builder(&cUnit->nativeGcMap,
781 mapping_table.size() / 2, max_native_offset,
782 dex_gc_map.RegWidth());
783
784 for (size_t i = 0; i < mapping_table.size(); i += 2) {
785 uint32_t native_offset = mapping_table[i + 0];
786 uint32_t dex_pc = mapping_table[i + 1];
787 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Bill Buzbeea5b30242012-09-28 07:19:44 -0700788 CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
789 native_gc_map_builder.AddEntry(native_offset, references);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700790 }
791}
792
buzbeee3acd072012-02-25 17:03:10 -0800793/* Determine the offset of each literal field */
buzbeeaad94382012-11-21 07:40:50 -0800794static int AssignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800795{
buzbee52a77fc2012-11-20 19:50:46 -0800796 offset = AssignLiteralOffsetCommon(cUnit->literalList, offset);
797 offset = AssignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
798 offset = AssignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
Bill Buzbeea114add2012-05-03 15:00:40 -0700799 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800800}
801
buzbeeaad94382012-11-21 07:40:50 -0800802static int AssignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800803{
Bill Buzbeea114add2012-05-03 15:00:40 -0700804 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800805 GrowableListIteratorInit(&cUnit->switchTables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700806 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800807 SwitchTable *tabRec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700808 if (tabRec == NULL) break;
809 tabRec->offset = offset;
810 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
811 offset += tabRec->table[1] * (sizeof(int) * 2);
812 } else {
813 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
814 static_cast<int>(Instruction::kPackedSwitchSignature));
815 offset += tabRec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800816 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700817 }
818 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800819}
820
buzbeeaad94382012-11-21 07:40:50 -0800821static int AssignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800822{
Bill Buzbeea114add2012-05-03 15:00:40 -0700823 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800824 GrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 while (true) {
buzbeecbd6d442012-11-17 14:11:25 -0800826 FillArrayData *tabRec =
buzbee52a77fc2012-11-20 19:50:46 -0800827 reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 if (tabRec == NULL) break;
829 tabRec->offset = offset;
830 offset += tabRec->size;
831 // word align
832 offset = (offset + 3) & ~3;
833 }
834 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800835}
836
837/*
838 * Walk the compilation unit and assign offsets to instructions
839 * and literals and compute the total size of the compiled unit.
840 */
buzbeeaad94382012-11-21 07:40:50 -0800841static void AssignOffsets(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800842{
buzbee52a77fc2012-11-20 19:50:46 -0800843 int offset = AssignInsnOffsets(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800844
Bill Buzbeea114add2012-05-03 15:00:40 -0700845 /* Const values have to be word aligned */
846 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800847
Bill Buzbeea114add2012-05-03 15:00:40 -0700848 /* Set up offsets for literals */
849 cUnit->dataOffset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800850
buzbee52a77fc2012-11-20 19:50:46 -0800851 offset = AssignLiteralOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800852
buzbeeaad94382012-11-21 07:40:50 -0800853 offset = AssignSwitchTablesOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800854
buzbee52a77fc2012-11-20 19:50:46 -0800855 offset = AssignFillArrayDataOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800856
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 cUnit->totalSize = offset;
buzbeee3acd072012-02-25 17:03:10 -0800858}
859
860/*
861 * Go over each instruction in the list and calculate the offset from the top
862 * before sending them off to the assembler. If out-of-range branch distance is
863 * seen rearrange the instructions a bit to correct it.
864 */
buzbee52a77fc2012-11-20 19:50:46 -0800865void AssembleLIR(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800866{
buzbee52a77fc2012-11-20 19:50:46 -0800867 AssignOffsets(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700868 /*
869 * Assemble here. Note that we generate code with optimistic assumptions
870 * and if found now to work, we'll have to redo the sequence and retry.
871 */
buzbeee3acd072012-02-25 17:03:10 -0800872
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 while (true) {
buzbee52a77fc2012-11-20 19:50:46 -0800874 AssemblerStatus res = AssembleInstructions(cUnit, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700875 if (res == kSuccess) {
876 break;
877 } else {
878 cUnit->assemblerRetries++;
879 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
buzbee52a77fc2012-11-20 19:50:46 -0800880 CodegenDump(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700881 LOG(FATAL) << "Assembler error - too many retries";
882 }
883 // Redo offsets and try again
buzbee52a77fc2012-11-20 19:50:46 -0800884 AssignOffsets(cUnit);
Bill Buzbeea114add2012-05-03 15:00:40 -0700885 cUnit->codeBuffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800886 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700887 }
buzbeee3acd072012-02-25 17:03:10 -0800888
Bill Buzbeea114add2012-05-03 15:00:40 -0700889 // Install literals
buzbee52a77fc2012-11-20 19:50:46 -0800890 InstallLiteralPools(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800891
Bill Buzbeea114add2012-05-03 15:00:40 -0700892 // Install switch tables
buzbee52a77fc2012-11-20 19:50:46 -0800893 InstallSwitchTables(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800894
Bill Buzbeea114add2012-05-03 15:00:40 -0700895 // Install fill array data
buzbee52a77fc2012-11-20 19:50:46 -0800896 InstallFillArrayData(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800897
Ian Rogers0c7abda2012-09-19 13:33:42 -0700898 // Create the mapping table and native offset to reference map.
buzbee52a77fc2012-11-20 19:50:46 -0800899 CreateMappingTables(cUnit);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700900
buzbee52a77fc2012-11-20 19:50:46 -0800901 CreateNativeGcMap(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800902}
903
buzbee31a4a6f2012-02-28 15:36:15 -0800904/*
905 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
906 * offset vaddr. This label will be used to fix up the case
907 * branch table during the assembly phase. Be sure to set
908 * all resource flags on this to prevent code motion across
909 * target boundaries. KeyVal is just there for debugging.
910 */
buzbeeaad94382012-11-21 07:40:50 -0800911static LIR* InsertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
buzbee31a4a6f2012-02-28 15:36:15 -0800912{
Bill Buzbeea114add2012-05-03 15:00:40 -0700913 SafeMap<unsigned int, LIR*>::iterator it;
914 it = cUnit->boundaryMap.find(vaddr);
915 if (it == cUnit->boundaryMap.end()) {
916 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
917 }
buzbee52a77fc2012-11-20 19:50:46 -0800918 LIR* newLabel = static_cast<LIR*>(NewMem(cUnit, sizeof(LIR), true, kAllocLIR));
Bill Buzbeea114add2012-05-03 15:00:40 -0700919 newLabel->dalvikOffset = vaddr;
920 newLabel->opcode = kPseudoCaseLabel;
921 newLabel->operands[0] = keyVal;
buzbee52a77fc2012-11-20 19:50:46 -0800922 InsertLIRAfter(it->second, newLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -0700923 return newLabel;
buzbee31a4a6f2012-02-28 15:36:15 -0800924}
925
buzbeeaad94382012-11-21 07:40:50 -0800926static void MarkPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee31a4a6f2012-02-28 15:36:15 -0800927{
buzbeeeaf09bc2012-11-15 14:51:41 -0800928 const uint16_t* table = tabRec->table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700929 int baseVaddr = tabRec->vaddr;
buzbeecbd6d442012-11-17 14:11:25 -0800930 const int *targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700931 int entries = table[1];
932 int lowKey = s4FromSwitchData(&table[2]);
933 for (int i = 0; i < entries; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800934 tabRec->targets[i] = InsertCaseLabel(cUnit, baseVaddr + targets[i], i + lowKey);
Bill Buzbeea114add2012-05-03 15:00:40 -0700935 }
buzbee31a4a6f2012-02-28 15:36:15 -0800936}
937
buzbeeaad94382012-11-21 07:40:50 -0800938static void MarkSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee31a4a6f2012-02-28 15:36:15 -0800939{
buzbeeeaf09bc2012-11-15 14:51:41 -0800940 const uint16_t* table = tabRec->table;
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 int baseVaddr = tabRec->vaddr;
942 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -0800943 const int* keys = reinterpret_cast<const int*>(&table[2]);
944 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -0700945 for (int i = 0; i < entries; i++) {
buzbee52a77fc2012-11-20 19:50:46 -0800946 tabRec->targets[i] = InsertCaseLabel(cUnit, baseVaddr + targets[i], keys[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700947 }
buzbee31a4a6f2012-02-28 15:36:15 -0800948}
949
buzbee52a77fc2012-11-20 19:50:46 -0800950void ProcessSwitchTables(CompilationUnit* cUnit)
buzbee31a4a6f2012-02-28 15:36:15 -0800951{
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 GrowableListIterator iterator;
buzbee52a77fc2012-11-20 19:50:46 -0800953 GrowableListIteratorInit(&cUnit->switchTables, &iterator);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 while (true) {
955 SwitchTable *tabRec =
buzbee52a77fc2012-11-20 19:50:46 -0800956 reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator));
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 if (tabRec == NULL) break;
958 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee52a77fc2012-11-20 19:50:46 -0800959 MarkPackedCaseLabels(cUnit, tabRec);
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee52a77fc2012-11-20 19:50:46 -0800961 MarkSparseCaseLabels(cUnit, tabRec);
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 } else {
963 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -0800964 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700965 }
buzbee31a4a6f2012-02-28 15:36:15 -0800966}
967
buzbee52a77fc2012-11-20 19:50:46 -0800968void DumpSparseSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700969 /*
970 * Sparse switch data format:
971 * ushort ident = 0x0200 magic value
972 * ushort size number of entries in the table; > 0
973 * int keys[size] keys, sorted low-to-high; 32-bit aligned
974 * int targets[size] branch targets, relative to switch opcode
975 *
976 * Total size is (2+size*4) 16-bit code units.
977 */
buzbee31a4a6f2012-02-28 15:36:15 -0800978{
buzbeeeaf09bc2012-11-15 14:51:41 -0800979 uint16_t ident = table[0];
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 int entries = table[1];
buzbeecbd6d442012-11-17 14:11:25 -0800981 const int* keys = reinterpret_cast<const int*>(&table[2]);
982 const int* targets = &keys[entries];
Bill Buzbeea114add2012-05-03 15:00:40 -0700983 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
984 << ", entries: " << std::dec << entries;
985 for (int i = 0; i < entries; i++) {
986 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
987 }
buzbee31a4a6f2012-02-28 15:36:15 -0800988}
989
buzbee52a77fc2012-11-20 19:50:46 -0800990void DumpPackedSwitchTable(const uint16_t* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700991 /*
992 * Packed switch data format:
993 * ushort ident = 0x0100 magic value
994 * ushort size number of entries in the table
995 * int first_key first (and lowest) switch case value
996 * int targets[size] branch targets, relative to switch opcode
997 *
998 * Total size is (4+size*2) 16-bit code units.
999 */
buzbee31a4a6f2012-02-28 15:36:15 -08001000{
buzbeeeaf09bc2012-11-15 14:51:41 -08001001 uint16_t ident = table[0];
buzbeecbd6d442012-11-17 14:11:25 -08001002 const int* targets = reinterpret_cast<const int*>(&table[4]);
Bill Buzbeea114add2012-05-03 15:00:40 -07001003 int entries = table[1];
1004 int lowKey = s4FromSwitchData(&table[2]);
1005 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
1006 << ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
1007 for (int i = 0; i < entries; i++) {
1008 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex
1009 << targets[i];
1010 }
buzbee31a4a6f2012-02-28 15:36:15 -08001011}
buzbeee3acd072012-02-25 17:03:10 -08001012
buzbeed1643e42012-09-05 14:06:51 -07001013/*
1014 * Set up special LIR to mark a Dalvik byte-code instruction start and
1015 * record it in the boundaryMap. NOTE: in cases such as kMirOpCheck in
1016 * which we split a single Dalvik instruction, only the first MIR op
1017 * associated with a Dalvik PC should be entered into the map.
1018 */
buzbee52a77fc2012-11-20 19:50:46 -08001019LIR* MarkBoundary(CompilationUnit* cUnit, int offset, const char* instStr)
buzbeed1643e42012-09-05 14:06:51 -07001020{
buzbee52a77fc2012-11-20 19:50:46 -08001021 LIR* res = NewLIR1(cUnit, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(instStr));
buzbeed1643e42012-09-05 14:06:51 -07001022 if (cUnit->boundaryMap.find(offset) == cUnit->boundaryMap.end()) {
1023 cUnit->boundaryMap.Put(offset, res);
1024 }
1025 return res;
1026}
buzbeee3acd072012-02-25 17:03:10 -08001027
buzbeed1643e42012-09-05 14:06:51 -07001028}
1029 // namespace art