blob: ca0a933789d067db632fa23299a5d2c45decf7df [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
Ian Rogers0c7abda2012-09-19 13:33:42 -070017#include "gc_map.h"
18#include "verifier/dex_gc_map.h"
19#include "verifier/method_verifier.h"
20
buzbeee3acd072012-02-25 17:03:10 -080021namespace art {
22
buzbee31a4a6f2012-02-28 15:36:15 -080023void setMemRefType(LIR* lir, bool isLoad, int memType)
24{
Bill Buzbeea114add2012-05-03 15:00:40 -070025 u8 *maskPtr;
26 u8 mask = ENCODE_MEM;;
27 DCHECK(EncodingMap[lir->opcode].flags & (IS_LOAD | IS_STORE));
28 if (isLoad) {
29 maskPtr = &lir->useMask;
30 } else {
31 maskPtr = &lir->defMask;
32 }
33 /* Clear out the memref flags */
34 *maskPtr &= ~mask;
35 /* ..and then add back the one we need */
36 switch (memType) {
37 case kLiteral:
38 DCHECK(isLoad);
39 *maskPtr |= ENCODE_LITERAL;
40 break;
41 case kDalvikReg:
42 *maskPtr |= ENCODE_DALVIK_REG;
43 break;
44 case kHeapRef:
45 *maskPtr |= ENCODE_HEAP_REF;
46 break;
47 case kMustNotAlias:
48 /* Currently only loads can be marked as kMustNotAlias */
49 DCHECK(!(EncodingMap[lir->opcode].flags & IS_STORE));
50 *maskPtr |= ENCODE_MUST_NOT_ALIAS;
51 break;
52 default:
53 LOG(FATAL) << "Oat: invalid memref kind - " << memType;
54 }
buzbee31a4a6f2012-02-28 15:36:15 -080055}
56
57/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080058 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -080059 */
Ian Rogersb5d09b22012-03-06 22:14:17 -080060void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -080061{
Bill Buzbeea114add2012-05-03 15:00:40 -070062 setMemRefType(lir, isLoad, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -080063
Bill Buzbeea114add2012-05-03 15:00:40 -070064 /*
65 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
66 * access.
67 */
68 lir->aliasInfo = regId;
69 if (is64bit) {
70 lir->aliasInfo |= 0x80000000;
71 }
buzbee31a4a6f2012-02-28 15:36:15 -080072}
73
74/*
75 * Decode the register id.
76 */
77inline u8 getRegMaskCommon(int reg)
78{
Bill Buzbeea114add2012-05-03 15:00:40 -070079 u8 seed;
80 int shift;
buzbee31a4a6f2012-02-28 15:36:15 -080081
jeffhaoe2962482012-06-28 11:29:57 -070082#if defined(TARGET_X86)
jeffhao854029c2012-07-23 17:31:30 -070083 int regId = reg & 0xf;
jeffhaoe2962482012-06-28 11:29:57 -070084 /*
85 * Double registers in x86 are just a single FP register
86 */
87 seed = 1;
88#else
jeffhao854029c2012-07-23 17:31:30 -070089 int regId = reg & 0x1f;
Bill Buzbeea114add2012-05-03 15:00:40 -070090 /*
91 * Each double register is equal to a pair of single-precision FP registers
92 */
93 seed = DOUBLEREG(reg) ? 3 : 1;
jeffhaoe2962482012-06-28 11:29:57 -070094#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070095 /* FP register starts at bit position 16 */
96 shift = FPREG(reg) ? kFPReg0 : 0;
97 /* Expand the double register id into single offset */
98 shift += regId;
99 return (seed << shift);
buzbee31a4a6f2012-02-28 15:36:15 -0800100}
101
102/*
103 * Mark the corresponding bit(s).
104 */
105inline void setupRegMask(u8* mask, int reg)
106{
Bill Buzbeea114add2012-05-03 15:00:40 -0700107 *mask |= getRegMaskCommon(reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800108}
109
110/*
111 * Set up the proper fields in the resource mask
112 */
113void setupResourceMasks(LIR* lir)
114{
Bill Buzbeea114add2012-05-03 15:00:40 -0700115 int opcode = lir->opcode;
116 int flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800117
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 if (opcode <= 0) {
119 lir->useMask = lir->defMask = 0;
120 return;
121 }
buzbee31a4a6f2012-02-28 15:36:15 -0800122
Bill Buzbeea114add2012-05-03 15:00:40 -0700123 flags = EncodingMap[lir->opcode].flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800124
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 if (flags & NEEDS_FIXUP) {
126 lir->flags.pcRelFixup = true;
127 }
buzbee31a4a6f2012-02-28 15:36:15 -0800128
Bill Buzbeea114add2012-05-03 15:00:40 -0700129 /* Get the starting size of the instruction's template */
130 lir->flags.size = oatGetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800131
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 /* Set up the mask for resources that are updated */
133 if (flags & (IS_LOAD | IS_STORE)) {
134 /* Default to heap - will catch specialized classes later */
135 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
136 }
buzbee31a4a6f2012-02-28 15:36:15 -0800137
Bill Buzbeea114add2012-05-03 15:00:40 -0700138 /*
139 * Conservatively assume the branch here will call out a function that in
140 * turn will trash everything.
141 */
142 if (flags & IS_BRANCH) {
143 lir->defMask = lir->useMask = ENCODE_ALL;
144 return;
145 }
buzbee31a4a6f2012-02-28 15:36:15 -0800146
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (flags & REG_DEF0) {
148 setupRegMask(&lir->defMask, lir->operands[0]);
149 }
buzbee31a4a6f2012-02-28 15:36:15 -0800150
Bill Buzbeea114add2012-05-03 15:00:40 -0700151 if (flags & REG_DEF1) {
152 setupRegMask(&lir->defMask, lir->operands[1]);
153 }
buzbee31a4a6f2012-02-28 15:36:15 -0800154
jeffhaoe2962482012-06-28 11:29:57 -0700155#if defined(TARGET_X86)
156 if (flags & REG_DEFA) {
157 setupRegMask(&lir->defMask, rAX);
158 }
159
160 if (flags & REG_DEFD) {
161 setupRegMask(&lir->defMask, rDX);
162 }
163#endif
164
Bill Buzbeea114add2012-05-03 15:00:40 -0700165 if (flags & REG_DEF_SP) {
166 lir->defMask |= ENCODE_REG_SP;
167 }
buzbee31a4a6f2012-02-28 15:36:15 -0800168
buzbeea7678db2012-03-05 15:35:46 -0800169#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700170 if (flags & REG_DEF_LR) {
171 lir->defMask |= ENCODE_REG_LR;
172 }
buzbeea7678db2012-03-05 15:35:46 -0800173#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800174
jeffhaoe2962482012-06-28 11:29:57 -0700175#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 if (flags & REG_DEF_LIST0) {
177 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
178 }
buzbee31a4a6f2012-02-28 15:36:15 -0800179
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 if (flags & REG_DEF_LIST1) {
181 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
182 }
buzbee31a4a6f2012-02-28 15:36:15 -0800183
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 if (flags & REG_DEF_FPCS_LIST0) {
185 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
186 }
buzbee31a4a6f2012-02-28 15:36:15 -0800187
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 if (flags & REG_DEF_FPCS_LIST2) {
189 for (int i = 0; i < lir->operands[2]; i++) {
190 setupRegMask(&lir->defMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800191 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 }
buzbee5de34942012-03-01 14:51:57 -0800193#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800194
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 if (flags & SETS_CCODES) {
196 lir->defMask |= ENCODE_CCODE;
197 }
buzbee31a4a6f2012-02-28 15:36:15 -0800198
199#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700200 /* Conservatively treat the IT block */
201 if (flags & IS_IT) {
202 lir->defMask = ENCODE_ALL;
203 }
buzbee31a4a6f2012-02-28 15:36:15 -0800204#endif
205
Bill Buzbeea114add2012-05-03 15:00:40 -0700206 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
207 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800208
Bill Buzbeea114add2012-05-03 15:00:40 -0700209 for (i = 0; i < 4; i++) {
210 if (flags & (1 << (kRegUse0 + i))) {
211 setupRegMask(&lir->useMask, lir->operands[i]);
212 }
buzbee31a4a6f2012-02-28 15:36:15 -0800213 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700214 }
buzbee31a4a6f2012-02-28 15:36:15 -0800215
jeffhaoe2962482012-06-28 11:29:57 -0700216#if defined(TARGET_X86)
217 if (flags & REG_USEA) {
218 setupRegMask(&lir->useMask, rAX);
219 }
220
221 if (flags & REG_USEC) {
222 setupRegMask(&lir->useMask, rCX);
223 }
224
225 if (flags & REG_USED) {
226 setupRegMask(&lir->useMask, rDX);
227 }
228#endif
229
buzbeea7678db2012-03-05 15:35:46 -0800230#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700231 if (flags & REG_USE_PC) {
232 lir->useMask |= ENCODE_REG_PC;
233 }
buzbeea7678db2012-03-05 15:35:46 -0800234#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800235
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 if (flags & REG_USE_SP) {
237 lir->useMask |= ENCODE_REG_SP;
238 }
buzbee31a4a6f2012-02-28 15:36:15 -0800239
jeffhaoe2962482012-06-28 11:29:57 -0700240#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 if (flags & REG_USE_LIST0) {
242 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
243 }
buzbee31a4a6f2012-02-28 15:36:15 -0800244
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 if (flags & REG_USE_LIST1) {
246 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
247 }
buzbee31a4a6f2012-02-28 15:36:15 -0800248
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 if (flags & REG_USE_FPCS_LIST0) {
250 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
251 }
buzbee31a4a6f2012-02-28 15:36:15 -0800252
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 if (flags & REG_USE_FPCS_LIST2) {
254 for (int i = 0; i < lir->operands[2]; i++) {
255 setupRegMask(&lir->useMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800256 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 }
buzbee5de34942012-03-01 14:51:57 -0800258#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800259
Bill Buzbeea114add2012-05-03 15:00:40 -0700260 if (flags & USES_CCODES) {
261 lir->useMask |= ENCODE_CCODE;
262 }
buzbee31a4a6f2012-02-28 15:36:15 -0800263
264#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 /* Fixup for kThumbPush/lr and kThumbPop/pc */
266 if (opcode == kThumbPush || opcode == kThumbPop) {
267 u8 r8Mask = getRegMaskCommon(r8);
268 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
269 lir->useMask &= ~r8Mask;
270 lir->useMask |= ENCODE_REG_LR;
271 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
272 lir->defMask &= ~r8Mask;
273 lir->defMask |= ENCODE_REG_PC;
buzbee31a4a6f2012-02-28 15:36:15 -0800274 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 }
buzbee31a4a6f2012-02-28 15:36:15 -0800276#endif
277}
278
279/*
buzbee5de34942012-03-01 14:51:57 -0800280 * Debugging macros
281 */
282#define DUMP_RESOURCE_MASK(X)
283#define DUMP_SSA_REP(X)
284
285/* Pretty-print a LIR instruction */
286void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
287{
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 LIR* lir = (LIR*) arg;
289 int offset = lir->offset;
290 int dest = lir->operands[0];
291 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800292
Bill Buzbeea114add2012-05-03 15:00:40 -0700293 /* Handle pseudo-ops individually, and all regular insns as a group */
294 switch (lir->opcode) {
295 case kPseudoMethodEntry:
296 LOG(INFO) << "-------- method entry "
297 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
298 break;
299 case kPseudoMethodExit:
300 LOG(INFO) << "-------- Method_Exit";
301 break;
302 case kPseudoBarrier:
303 LOG(INFO) << "-------- BARRIER";
304 break;
305 case kPseudoExtended:
306 LOG(INFO) << "-------- " << (char* ) dest;
307 break;
308 case kPseudoSSARep:
309 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
310 break;
311 case kPseudoEntryBlock:
312 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
313 break;
314 case kPseudoDalvikByteCodeBoundary:
315 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
316 << lir->dalvikOffset << " @ " << (char* )lir->operands[0];
317 break;
318 case kPseudoExitBlock:
319 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
320 break;
321 case kPseudoPseudoAlign4:
322 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex
323 << offset << "): .align4";
324 break;
325 case kPseudoEHBlockLabel:
326 LOG(INFO) << "Exception_Handling:";
327 break;
328 case kPseudoTargetLabel:
329 case kPseudoNormalBlockLabel:
330 LOG(INFO) << "L" << (void*)lir << ":";
331 break;
332 case kPseudoThrowTarget:
333 LOG(INFO) << "LT" << (void*)lir << ":";
334 break;
335 case kPseudoIntrinsicRetry:
336 LOG(INFO) << "IR" << (void*)lir << ":";
337 break;
338 case kPseudoSuspendTarget:
339 LOG(INFO) << "LS" << (void*)lir << ":";
340 break;
buzbee8320f382012-09-11 16:29:42 -0700341 case kPseudoSafepointPC:
342 LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvikOffset << ":";
343 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700344 case kPseudoCaseLabel:
345 LOG(INFO) << "LC" << (void*)lir << ": Case target 0x"
346 << std::hex << lir->operands[0] << "|" << std::dec <<
347 lir->operands[0];
348 break;
349 default:
350 if (lir->flags.isNop && !dumpNop) {
351 break;
352 } else {
353 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name,
354 lir, baseAddr));
355 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt
356 , lir, baseAddr));
357 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
358 (unsigned int)(baseAddr + offset),
359 op_name.c_str(), op_operands.c_str(),
360 lir->flags.isNop ? "(nop)" : "");
361 }
362 break;
363 }
buzbee5de34942012-03-01 14:51:57 -0800364
Bill Buzbeea114add2012-05-03 15:00:40 -0700365 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
366 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->useMask, "use"));
367 }
368 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
369 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->defMask, "def"));
370 }
buzbee5de34942012-03-01 14:51:57 -0800371}
372
373void oatDumpPromotionMap(CompilationUnit *cUnit)
374{
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
376 for (int i = 0; i < numRegs; i++) {
377 PromotionMap vRegMap = cUnit->promotionMap[i];
378 std::string buf;
379 if (vRegMap.fpLocation == kLocPhysReg) {
380 StringAppendF(&buf, " : s%d", vRegMap.fpReg & FP_REG_MASK);
buzbee5de34942012-03-01 14:51:57 -0800381 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700382
383 std::string buf3;
384 if (i < cUnit->numDalvikRegisters) {
385 StringAppendF(&buf3, "%02d", i);
386 } else if (i == cUnit->methodSReg) {
387 buf3 = "Method*";
388 } else {
389 StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters);
390 }
391
392 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
393 vRegMap.coreLocation == kLocPhysReg ?
394 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
395 vRegMap.coreReg : oatSRegOffset(cUnit, i),
396 buf.c_str());
397 }
buzbee5de34942012-03-01 14:51:57 -0800398}
399
buzbee5de34942012-03-01 14:51:57 -0800400/* Dump instructions and constant pool contents */
401void oatCodegenDump(CompilationUnit* cUnit)
402{
Bill Buzbeea114add2012-05-03 15:00:40 -0700403 LOG(INFO) << "Dumping LIR insns for "
404 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
405 LIR* lirInsn;
406 LIR* thisLIR;
407 int insnsSize = cUnit->insnsSize;
buzbee5de34942012-03-01 14:51:57 -0800408
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
410 LOG(INFO) << "Ins : " << cUnit->numIns;
411 LOG(INFO) << "Outs : " << cUnit->numOuts;
412 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
413 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
414 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
415 LOG(INFO) << "Frame size : " << cUnit->frameSize;
416 LOG(INFO) << "code size is " << cUnit->totalSize <<
417 " bytes, Dalvik size is " << insnsSize * 2;
418 LOG(INFO) << "expansion factor: "
419 << (float)cUnit->totalSize / (float)(insnsSize * 2);
420 oatDumpPromotionMap(cUnit);
421 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
422 oatDumpLIRInsn(cUnit, lirInsn, 0);
423 }
424 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
425 thisLIR = (LIR*) lirInsn;
426 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
427 thisLIR->offset, thisLIR->offset,
428 ((CallsiteInfo *)
429 thisLIR->operands[0])->classDescriptor);
430 }
431 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
432 thisLIR = (LIR*) lirInsn;
433 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
434 thisLIR->offset, thisLIR->offset,
435 thisLIR->operands[0]);
436 }
buzbee5de34942012-03-01 14:51:57 -0800437
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 const DexFile::MethodId& method_id =
439 cUnit->dex_file->GetMethodId(cUnit->method_idx);
440 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
441 std::string name(cUnit->dex_file->GetMethodName(method_id));
442 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800443
Bill Buzbeea114add2012-05-03 15:00:40 -0700444 // Dump mapping table
445 if (cUnit->mappingTable.size() > 0) {
446 std::string
447 line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
448 descriptor.c_str(), name.c_str(), signature.c_str(),
449 cUnit->mappingTable.size()));
450 std::replace(line.begin(), line.end(), ';', '_');
451 LOG(INFO) << line;
452 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
453 line = StringPrintf(" {0x%05x, 0x%04x},",
454 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
455 LOG(INFO) << line;
buzbee5de34942012-03-01 14:51:57 -0800456 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 LOG(INFO) <<" };\n\n";
458 }
buzbee5de34942012-03-01 14:51:57 -0800459}
460
buzbeea2ebdd72012-03-04 14:57:06 -0800461
462LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800464{
Bill Buzbeea114add2012-05-03 15:00:40 -0700465 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
466 insn->dalvikOffset = dalvikOffset;
467 insn->opcode = opcode;
468 insn->operands[0] = op0;
469 insn->operands[1] = op1;
470 insn->operands[2] = op2;
471 insn->operands[3] = op3;
472 insn->operands[4] = op4;
473 insn->target = target;
474 oatSetupResourceMasks(insn);
buzbee8320f382012-09-11 16:29:42 -0700475 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700476 // Always make labels scheduling barriers
buzbee8320f382012-09-11 16:29:42 -0700477 insn->useMask = insn->defMask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700478 }
479 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800480}
481
buzbee5de34942012-03-01 14:51:57 -0800482/*
buzbee31a4a6f2012-02-28 15:36:15 -0800483 * The following are building blocks to construct low-level IRs with 0 - 4
484 * operands.
485 */
buzbee5de34942012-03-01 14:51:57 -0800486LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800487{
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
489 << EncodingMap[opcode].name << " " << (int)opcode << " "
490 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
491 << cUnit->currentDalvikOffset;
492 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
493 oatAppendLIR(cUnit, (LIR*) insn);
494 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800495}
496
buzbee5de34942012-03-01 14:51:57 -0800497LIR* newLIR1(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700498 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800499{
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
501 << EncodingMap[opcode].name << " " << (int)opcode << " "
502 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
503 << cUnit->currentDalvikOffset;
504 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
505 oatAppendLIR(cUnit, (LIR*) insn);
506 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800507}
508
buzbee5de34942012-03-01 14:51:57 -0800509LIR* newLIR2(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800511{
Bill Buzbeea114add2012-05-03 15:00:40 -0700512 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
513 << EncodingMap[opcode].name << " " << (int)opcode << " "
514 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
515 << cUnit->currentDalvikOffset;
516 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
517 oatAppendLIR(cUnit, (LIR*) insn);
518 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800519}
520
buzbee5de34942012-03-01 14:51:57 -0800521LIR* newLIR3(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800523{
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
525 << EncodingMap[opcode].name << " " << (int)opcode << " "
526 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
527 << cUnit->currentDalvikOffset;
528 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
529 src2);
530 oatAppendLIR(cUnit, (LIR*) insn);
531 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800532}
533
buzbee5de34942012-03-01 14:51:57 -0800534LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700535 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800536{
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
538 << EncodingMap[opcode].name << " " << (int)opcode << " "
539 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
540 << cUnit->currentDalvikOffset;
541 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
542 src2, info);
543 oatAppendLIR(cUnit, (LIR*) insn);
544 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800545}
buzbee31a4a6f2012-02-28 15:36:15 -0800546
Ian Rogersb5d09b22012-03-06 22:14:17 -0800547LIR* newLIR5(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700548 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800549{
Bill Buzbeea114add2012-05-03 15:00:40 -0700550 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
551 << EncodingMap[opcode].name << " " << (int)opcode << " "
552 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
553 << cUnit->currentDalvikOffset;
554 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
555 src2, info1, info2);
556 oatAppendLIR(cUnit, (LIR*) insn);
557 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800558}
559
buzbee31a4a6f2012-02-28 15:36:15 -0800560/*
561 * Search the existing constants in the literal pool for an exact or close match
562 * within specified delta (greater or equal to 0).
563 */
564LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
565{
Bill Buzbeea114add2012-05-03 15:00:40 -0700566 while (dataTarget) {
567 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= delta)
568 return (LIR* ) dataTarget;
569 dataTarget = dataTarget->next;
570 }
571 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800572}
573
574/* Search the existing constants in the literal pool for an exact wide match */
575LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
576{
Bill Buzbeea114add2012-05-03 15:00:40 -0700577 bool loMatch = false;
578 LIR* loTarget = NULL;
579 while (dataTarget) {
580 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
581 return (LIR*)loTarget;
buzbee31a4a6f2012-02-28 15:36:15 -0800582 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700583 loMatch = false;
584 if (((LIR*)dataTarget)->operands[0] == valLo) {
585 loMatch = true;
586 loTarget = dataTarget;
587 }
588 dataTarget = dataTarget->next;
589 }
590 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800591}
592
593/*
594 * The following are building blocks to insert constants into the pool or
595 * instruction streams.
596 */
597
buzbee5de34942012-03-01 14:51:57 -0800598/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700599LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800600{
Bill Buzbeea114add2012-05-03 15:00:40 -0700601 /* Add the constant to the literal pool */
602 if (constantListP) {
603 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocData);
604 newValue->operands[0] = value;
605 newValue->next = *constantListP;
606 *constantListP = (LIR*) newValue;
607 return newValue;
608 }
609 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800610}
611
612/* Add a 64-bit constant to the constant pool or mixed with code */
613LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700614 int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800615{
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 //FIXME: hard-coded little endian, need BE variant
617 // Insert high word into list first
618 addWordData(cUnit, constantListP, valHi);
619 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800620}
621
Ian Rogersab058bb2012-03-11 22:19:38 -0700622void pushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 buf.push_back( data & 0xff);
624 buf.push_back( (data >> 8) & 0xff);
625 buf.push_back( (data >> 16) & 0xff);
626 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800627}
628
Ian Rogersab058bb2012-03-11 22:19:38 -0700629void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700630 while (buf.size() < offset) {
631 buf.push_back(0);
632 }
buzbeee3acd072012-02-25 17:03:10 -0800633}
634
Brian Carlstromf5822582012-03-19 22:34:31 -0700635bool IsDirect(int invokeType) {
636 InvokeType type = static_cast<InvokeType>(invokeType);
637 return type == kStatic || type == kDirect;
638}
639
buzbeee3acd072012-02-25 17:03:10 -0800640/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800641void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800642{
Bill Buzbeea114add2012-05-03 15:00:40 -0700643 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
644 LIR* dataLIR = cUnit->literalList;
645 while (dataLIR != NULL) {
646 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
647 dataLIR = NEXT_LIR(dataLIR);
648 }
649 // Push code and method literals, record offsets for the compiler to patch.
650 dataLIR = cUnit->codeLiteralList;
651 if (dataLIR != NULL) {
buzbeee3acd072012-02-25 17:03:10 -0800652 while (dataLIR != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700653 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 cUnit->compiler->AddCodePatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 cUnit->method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700656 cUnit->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 target,
Ian Rogers08f753d2012-08-24 14:35:25 -0700658 static_cast<InvokeType>(dataLIR->operands[1]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 cUnit->codeBuffer.size());
660 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
661 // unique based on target to ensure code deduplication works
662 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
663 pushWord(cUnit->codeBuffer, unique_patch_value);
664 dataLIR = NEXT_LIR(dataLIR);
buzbeee3acd072012-02-25 17:03:10 -0800665 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700666 dataLIR = cUnit->methodLiteralList;
667 while (dataLIR != NULL) {
668 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700669 cUnit->compiler->AddMethodPatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700670 cUnit->method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700671 cUnit->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 target,
Ian Rogers08f753d2012-08-24 14:35:25 -0700673 static_cast<InvokeType>(dataLIR->operands[1]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 cUnit->codeBuffer.size());
675 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
676 // unique based on target to ensure code deduplication works
677 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
678 pushWord(cUnit->codeBuffer, unique_patch_value);
679 dataLIR = NEXT_LIR(dataLIR);
Ian Rogers3fa13792012-03-18 15:53:45 -0700680 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700682
buzbeee3acd072012-02-25 17:03:10 -0800683}
684
685/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800686void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800687{
Bill Buzbeea114add2012-05-03 15:00:40 -0700688 GrowableListIterator iterator;
689 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
690 while (true) {
691 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
692 &iterator);
693 if (tabRec == NULL) break;
694 alignBuffer(cUnit->codeBuffer, tabRec->offset);
695 /*
696 * For Arm, our reference point is the address of the bx
697 * instruction that does the launch, so we have to subtract
698 * the auto pc-advance. For other targets the reference point
699 * is a label, so we can use the offset as-is.
700 */
buzbeec5159d52012-03-03 11:48:39 -0800701#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700702 int bxOffset = tabRec->anchor->offset + 4;
Ian Rogers7caad772012-03-30 01:07:54 -0700703#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 int bxOffset = 0;
buzbeec5159d52012-03-03 11:48:39 -0800705#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700706 int bxOffset = tabRec->anchor->offset;
buzbeec5159d52012-03-03 11:48:39 -0800707#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700708 if (cUnit->printMe) {
709 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800710 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700711 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
712 int* keys = (int*)&(tabRec->table[2]);
713 for (int elems = 0; elems < tabRec->table[1]; elems++) {
714 int disp = tabRec->targets[elems]->offset - bxOffset;
715 if (cUnit->printMe) {
716 LOG(INFO) << " Case[" << elems << "] key: 0x"
717 << std::hex << keys[elems] << ", disp: 0x"
718 << std::hex << disp;
719 }
720 pushWord(cUnit->codeBuffer, keys[elems]);
721 pushWord(cUnit->codeBuffer,
722 tabRec->targets[elems]->offset - bxOffset);
723 }
724 } else {
725 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
726 static_cast<int>(Instruction::kPackedSwitchSignature));
727 for (int elems = 0; elems < tabRec->table[1]; elems++) {
728 int disp = tabRec->targets[elems]->offset - bxOffset;
729 if (cUnit->printMe) {
730 LOG(INFO) << " Case[" << elems << "] disp: 0x"
731 << std::hex << disp;
732 }
733 pushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset);
734 }
735 }
736 }
buzbeee3acd072012-02-25 17:03:10 -0800737}
738
739/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800740void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800741{
Bill Buzbeea114add2012-05-03 15:00:40 -0700742 GrowableListIterator iterator;
743 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
744 while (true) {
745 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
746 &iterator);
747 if (tabRec == NULL) break;
748 alignBuffer(cUnit->codeBuffer, tabRec->offset);
749 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
750 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
751 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800752 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 }
buzbeee3acd072012-02-25 17:03:10 -0800754}
755
buzbee31a4a6f2012-02-28 15:36:15 -0800756int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800757{
Bill Buzbeea114add2012-05-03 15:00:40 -0700758 for (;lir != NULL; lir = lir->next) {
759 lir->offset = offset;
760 offset += 4;
761 }
762 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800763}
764
buzbee31a4a6f2012-02-28 15:36:15 -0800765void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800766{
buzbee8320f382012-09-11 16:29:42 -0700767 for (LIR* tgtLIR = (LIR *) cUnit->firstLIRInsn; tgtLIR != NULL; tgtLIR = NEXT_LIR(tgtLIR)) {
768 if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoSafepointPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700769 cUnit->mappingTable.push_back(tgtLIR->offset);
770 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
buzbeee3acd072012-02-25 17:03:10 -0800771 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 }
buzbeee3acd072012-02-25 17:03:10 -0800773}
774
Ian Rogers0c7abda2012-09-19 13:33:42 -0700775class NativePcToReferenceMapBuilder {
776 public:
777 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
778 size_t entries, uint32_t max_native_offset,
779 size_t references_width) : entries_(entries),
780 references_width_(references_width), in_use_(entries),
781 table_(table) {
782 // Compute width in bytes needed to hold max_native_offset.
783 native_offset_width_ = 0;
784 while (max_native_offset != 0) {
785 native_offset_width_++;
786 max_native_offset >>= 8;
787 }
788 // Resize table and set up header.
789 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
790 CHECK_LT(native_offset_width_, 1U << 8);
791 (*table)[0] = native_offset_width_;
792 CHECK_LT(references_width_, 1U << 8);
793 (*table)[1] = references_width_;
794 CHECK_LT(entries, 1U << 16);
795 (*table)[2] = entries & 0xFF;
796 (*table)[3] = (entries >> 8) & 0xFF;
797 }
798
799 void AddEntry(uint32_t native_offset, const uint8_t* references) {
800 size_t table_index = TableIndex(native_offset);
801 while (in_use_[table_index]) {
802 table_index = (table_index + 1) % entries_;
803 }
804 in_use_[table_index] = true;
805 SetNativeOffset(table_index, native_offset);
806 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
807 SetReferences(table_index, references);
808 }
809
810 private:
811 size_t TableIndex(uint32_t native_offset) {
812 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
813 }
814
815 uint32_t GetNativeOffset(size_t table_index) {
816 uint32_t native_offset = 0;
817 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
818 for (size_t i = 0; i < native_offset_width_; i++) {
819 native_offset |= (*table_)[table_offset + i] << (i * 8);
820 }
821 return native_offset;
822 }
823
824 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
825 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
826 for (size_t i = 0; i < native_offset_width_; i++) {
827 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
828 }
829 }
830
831 void SetReferences(size_t table_index, const uint8_t* references) {
832 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
833 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
834 }
835
836 size_t EntryWidth() const {
837 return native_offset_width_ + references_width_;
838 }
839
840 // Number of entries in the table.
841 const size_t entries_;
842 // Number of bytes used to encode the reference bitmap.
843 const size_t references_width_;
844 // Number of bytes used to encode a native offset.
845 size_t native_offset_width_;
846 // Entries that are in use.
847 std::vector<bool> in_use_;
848 // The table we're building.
849 std::vector<uint8_t>* const table_;
850};
851
852static void createNativeGcMap(CompilationUnit* cUnit) {
853 const std::vector<uint32_t>& mapping_table = cUnit->mappingTable;
854 uint32_t max_native_offset = 0;
855 for (size_t i = 0; i < mapping_table.size(); i += 2) {
856 uint32_t native_offset = mapping_table[i + 0];
857 if (native_offset > max_native_offset) {
858 max_native_offset = native_offset;
859 }
860 }
861 Compiler::MethodReference method_ref(cUnit->dex_file, cUnit->method_idx);
862 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
863 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
864 // Compute native offset to references size.
865 NativePcToReferenceMapBuilder native_gc_map_builder(&cUnit->nativeGcMap,
866 mapping_table.size() / 2, max_native_offset,
867 dex_gc_map.RegWidth());
868
869 for (size_t i = 0; i < mapping_table.size(); i += 2) {
870 uint32_t native_offset = mapping_table[i + 0];
871 uint32_t dex_pc = mapping_table[i + 1];
872 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
873 if (references != NULL) {
874 native_gc_map_builder.AddEntry(native_offset, references);
875 } else {
876 // TODO: there is a mapping table entry but no reference bitmap. This happens because of
877 // catch block entries. We should check that the dex_pc corresponds with a catch block
878 // here.
879 }
880 }
881}
882
buzbeee3acd072012-02-25 17:03:10 -0800883/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800884int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800885{
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
887 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
888 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
889 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800890}
891
buzbee31a4a6f2012-02-28 15:36:15 -0800892int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800893{
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 GrowableListIterator iterator;
895 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
896 while (true) {
897 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
898 &iterator);
899 if (tabRec == NULL) break;
900 tabRec->offset = offset;
901 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
902 offset += tabRec->table[1] * (sizeof(int) * 2);
903 } else {
904 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
905 static_cast<int>(Instruction::kPackedSwitchSignature));
906 offset += tabRec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800907 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
909 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800910}
911
buzbee31a4a6f2012-02-28 15:36:15 -0800912int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800913{
Bill Buzbeea114add2012-05-03 15:00:40 -0700914 GrowableListIterator iterator;
915 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
916 while (true) {
917 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
918 &iterator);
919 if (tabRec == NULL) break;
920 tabRec->offset = offset;
921 offset += tabRec->size;
922 // word align
923 offset = (offset + 3) & ~3;
924 }
925 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800926}
927
928/*
929 * Walk the compilation unit and assign offsets to instructions
930 * and literals and compute the total size of the compiled unit.
931 */
932void oatAssignOffsets(CompilationUnit* cUnit)
933{
Bill Buzbeea114add2012-05-03 15:00:40 -0700934 int offset = oatAssignInsnOffsets(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800935
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 /* Const values have to be word aligned */
937 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800938
Bill Buzbeea114add2012-05-03 15:00:40 -0700939 /* Set up offsets for literals */
940 cUnit->dataOffset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800941
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 offset = assignLiteralOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800943
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 offset = assignSwitchTablesOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800945
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 offset = assignFillArrayDataOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800947
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 cUnit->totalSize = offset;
buzbeee3acd072012-02-25 17:03:10 -0800949}
950
951/*
952 * Go over each instruction in the list and calculate the offset from the top
953 * before sending them off to the assembler. If out-of-range branch distance is
954 * seen rearrange the instructions a bit to correct it.
955 */
956void oatAssembleLIR(CompilationUnit* cUnit)
957{
Bill Buzbeea114add2012-05-03 15:00:40 -0700958 oatAssignOffsets(cUnit);
959 /*
960 * Assemble here. Note that we generate code with optimistic assumptions
961 * and if found now to work, we'll have to redo the sequence and retry.
962 */
buzbeee3acd072012-02-25 17:03:10 -0800963
Bill Buzbeea114add2012-05-03 15:00:40 -0700964 while (true) {
965 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
966 if (res == kSuccess) {
967 break;
968 } else {
969 cUnit->assemblerRetries++;
970 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
971 oatCodegenDump(cUnit);
972 LOG(FATAL) << "Assembler error - too many retries";
973 }
974 // Redo offsets and try again
975 oatAssignOffsets(cUnit);
976 cUnit->codeBuffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800977 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700978 }
buzbeee3acd072012-02-25 17:03:10 -0800979
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 // Install literals
981 installLiteralPools(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800982
Bill Buzbeea114add2012-05-03 15:00:40 -0700983 // Install switch tables
984 installSwitchTables(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800985
Bill Buzbeea114add2012-05-03 15:00:40 -0700986 // Install fill array data
987 installFillArrayData(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800988
Ian Rogers0c7abda2012-09-19 13:33:42 -0700989 // Create the mapping table and native offset to reference map.
Bill Buzbeea114add2012-05-03 15:00:40 -0700990 createMappingTable(cUnit);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700991
992 createNativeGcMap(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800993}
994
buzbee31a4a6f2012-02-28 15:36:15 -0800995/*
996 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
997 * offset vaddr. This label will be used to fix up the case
998 * branch table during the assembly phase. Be sure to set
999 * all resource flags on this to prevent code motion across
1000 * target boundaries. KeyVal is just there for debugging.
1001 */
1002LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
1003{
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 SafeMap<unsigned int, LIR*>::iterator it;
1005 it = cUnit->boundaryMap.find(vaddr);
1006 if (it == cUnit->boundaryMap.end()) {
1007 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
1008 }
1009 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
1010 newLabel->dalvikOffset = vaddr;
1011 newLabel->opcode = kPseudoCaseLabel;
1012 newLabel->operands[0] = keyVal;
1013 oatInsertLIRAfter(it->second, (LIR*)newLabel);
1014 return newLabel;
buzbee31a4a6f2012-02-28 15:36:15 -08001015}
1016
1017void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
1018{
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 const u2* table = tabRec->table;
1020 int baseVaddr = tabRec->vaddr;
1021 int *targets = (int*)&table[4];
1022 int entries = table[1];
1023 int lowKey = s4FromSwitchData(&table[2]);
1024 for (int i = 0; i < entries; i++) {
1025 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
1026 i + lowKey);
1027 }
buzbee31a4a6f2012-02-28 15:36:15 -08001028}
1029
1030void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
1031{
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 const u2* table = tabRec->table;
1033 int baseVaddr = tabRec->vaddr;
1034 int entries = table[1];
1035 int* keys = (int*)&table[2];
1036 int* targets = &keys[entries];
1037 for (int i = 0; i < entries; i++) {
1038 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
1039 keys[i]);
1040 }
buzbee31a4a6f2012-02-28 15:36:15 -08001041}
1042
1043void oatProcessSwitchTables(CompilationUnit* cUnit)
1044{
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 GrowableListIterator iterator;
1046 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
1047 while (true) {
1048 SwitchTable *tabRec =
1049 (SwitchTable *) oatGrowableListIteratorNext(&iterator);
1050 if (tabRec == NULL) break;
1051 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
1052 markPackedCaseLabels(cUnit, tabRec);
1053 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
1054 markSparseCaseLabels(cUnit, tabRec);
1055 } else {
1056 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001057 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 }
buzbee31a4a6f2012-02-28 15:36:15 -08001059}
1060
1061//FIXME: Do we have endian issues here?
1062
1063void dumpSparseSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 /*
1065 * Sparse switch data format:
1066 * ushort ident = 0x0200 magic value
1067 * ushort size number of entries in the table; > 0
1068 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1069 * int targets[size] branch targets, relative to switch opcode
1070 *
1071 * Total size is (2+size*4) 16-bit code units.
1072 */
buzbee31a4a6f2012-02-28 15:36:15 -08001073{
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 u2 ident = table[0];
1075 int entries = table[1];
1076 int* keys = (int*)&table[2];
1077 int* targets = &keys[entries];
1078 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1079 << ", entries: " << std::dec << entries;
1080 for (int i = 0; i < entries; i++) {
1081 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1082 }
buzbee31a4a6f2012-02-28 15:36:15 -08001083}
1084
1085void dumpPackedSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 /*
1087 * Packed switch data format:
1088 * ushort ident = 0x0100 magic value
1089 * ushort size number of entries in the table
1090 * int first_key first (and lowest) switch case value
1091 * int targets[size] branch targets, relative to switch opcode
1092 *
1093 * Total size is (4+size*2) 16-bit code units.
1094 */
buzbee31a4a6f2012-02-28 15:36:15 -08001095{
Bill Buzbeea114add2012-05-03 15:00:40 -07001096 u2 ident = table[0];
1097 int* targets = (int*)&table[4];
1098 int entries = table[1];
1099 int lowKey = s4FromSwitchData(&table[2]);
1100 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
1101 << ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
1102 for (int i = 0; i < entries; i++) {
1103 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex
1104 << targets[i];
1105 }
buzbee31a4a6f2012-02-28 15:36:15 -08001106}
buzbeee3acd072012-02-25 17:03:10 -08001107
buzbeed1643e42012-09-05 14:06:51 -07001108/*
1109 * Set up special LIR to mark a Dalvik byte-code instruction start and
1110 * record it in the boundaryMap. NOTE: in cases such as kMirOpCheck in
1111 * which we split a single Dalvik instruction, only the first MIR op
1112 * associated with a Dalvik PC should be entered into the map.
1113 */
1114LIR* markBoundary(CompilationUnit* cUnit, int offset, const char* instStr)
1115{
1116 LIR* res = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary, (intptr_t) instStr);
1117 if (cUnit->boundaryMap.find(offset) == cUnit->boundaryMap.end()) {
1118 cUnit->boundaryMap.Put(offset, res);
1119 }
1120 return res;
1121}
buzbeee3acd072012-02-25 17:03:10 -08001122
buzbeed1643e42012-09-05 14:06:51 -07001123}
1124 // namespace art