blob: 4e39ffcdf8fd5058ff8301dfdcc747c69636f263 [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 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
425 thisLIR = (LIR*) lirInsn;
426 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
427 thisLIR->offset, thisLIR->offset,
428 thisLIR->operands[0]);
429 }
buzbee5de34942012-03-01 14:51:57 -0800430
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 const DexFile::MethodId& method_id =
432 cUnit->dex_file->GetMethodId(cUnit->method_idx);
433 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
434 std::string name(cUnit->dex_file->GetMethodName(method_id));
435 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800436
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 // Dump mapping table
438 if (cUnit->mappingTable.size() > 0) {
439 std::string
440 line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
441 descriptor.c_str(), name.c_str(), signature.c_str(),
442 cUnit->mappingTable.size()));
443 std::replace(line.begin(), line.end(), ';', '_');
444 LOG(INFO) << line;
445 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
446 line = StringPrintf(" {0x%05x, 0x%04x},",
447 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
448 LOG(INFO) << line;
buzbee5de34942012-03-01 14:51:57 -0800449 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 LOG(INFO) <<" };\n\n";
451 }
buzbee5de34942012-03-01 14:51:57 -0800452}
453
buzbeea2ebdd72012-03-04 14:57:06 -0800454
455LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800457{
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
459 insn->dalvikOffset = dalvikOffset;
460 insn->opcode = opcode;
461 insn->operands[0] = op0;
462 insn->operands[1] = op1;
463 insn->operands[2] = op2;
464 insn->operands[3] = op3;
465 insn->operands[4] = op4;
466 insn->target = target;
467 oatSetupResourceMasks(insn);
buzbee8320f382012-09-11 16:29:42 -0700468 if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 // Always make labels scheduling barriers
buzbee8320f382012-09-11 16:29:42 -0700470 insn->useMask = insn->defMask = ENCODE_ALL;
Bill Buzbeea114add2012-05-03 15:00:40 -0700471 }
472 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800473}
474
buzbee5de34942012-03-01 14:51:57 -0800475/*
buzbee31a4a6f2012-02-28 15:36:15 -0800476 * The following are building blocks to construct low-level IRs with 0 - 4
477 * operands.
478 */
buzbee5de34942012-03-01 14:51:57 -0800479LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800480{
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
482 << EncodingMap[opcode].name << " " << (int)opcode << " "
483 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
484 << cUnit->currentDalvikOffset;
485 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
486 oatAppendLIR(cUnit, (LIR*) insn);
487 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800488}
489
buzbee5de34942012-03-01 14:51:57 -0800490LIR* newLIR1(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800492{
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
494 << EncodingMap[opcode].name << " " << (int)opcode << " "
495 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
496 << cUnit->currentDalvikOffset;
497 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
498 oatAppendLIR(cUnit, (LIR*) insn);
499 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800500}
501
buzbee5de34942012-03-01 14:51:57 -0800502LIR* newLIR2(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800504{
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
506 << EncodingMap[opcode].name << " " << (int)opcode << " "
507 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
508 << cUnit->currentDalvikOffset;
509 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
510 oatAppendLIR(cUnit, (LIR*) insn);
511 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800512}
513
buzbee5de34942012-03-01 14:51:57 -0800514LIR* newLIR3(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800516{
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
518 << EncodingMap[opcode].name << " " << (int)opcode << " "
519 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
520 << cUnit->currentDalvikOffset;
521 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
522 src2);
523 oatAppendLIR(cUnit, (LIR*) insn);
524 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800525}
526
buzbee5de34942012-03-01 14:51:57 -0800527LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800529{
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
531 << EncodingMap[opcode].name << " " << (int)opcode << " "
532 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
533 << cUnit->currentDalvikOffset;
534 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
535 src2, info);
536 oatAppendLIR(cUnit, (LIR*) insn);
537 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800538}
buzbee31a4a6f2012-02-28 15:36:15 -0800539
Ian Rogersb5d09b22012-03-06 22:14:17 -0800540LIR* newLIR5(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800542{
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
544 << EncodingMap[opcode].name << " " << (int)opcode << " "
545 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
546 << cUnit->currentDalvikOffset;
547 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
548 src2, info1, info2);
549 oatAppendLIR(cUnit, (LIR*) insn);
550 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800551}
552
buzbee31a4a6f2012-02-28 15:36:15 -0800553/*
554 * Search the existing constants in the literal pool for an exact or close match
555 * within specified delta (greater or equal to 0).
556 */
557LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
558{
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 while (dataTarget) {
560 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= delta)
561 return (LIR* ) dataTarget;
562 dataTarget = dataTarget->next;
563 }
564 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800565}
566
567/* Search the existing constants in the literal pool for an exact wide match */
568LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
569{
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 bool loMatch = false;
571 LIR* loTarget = NULL;
572 while (dataTarget) {
573 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
574 return (LIR*)loTarget;
buzbee31a4a6f2012-02-28 15:36:15 -0800575 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 loMatch = false;
577 if (((LIR*)dataTarget)->operands[0] == valLo) {
578 loMatch = true;
579 loTarget = dataTarget;
580 }
581 dataTarget = dataTarget->next;
582 }
583 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800584}
585
586/*
587 * The following are building blocks to insert constants into the pool or
588 * instruction streams.
589 */
590
buzbee5de34942012-03-01 14:51:57 -0800591/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700592LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800593{
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 /* Add the constant to the literal pool */
595 if (constantListP) {
596 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocData);
597 newValue->operands[0] = value;
598 newValue->next = *constantListP;
599 *constantListP = (LIR*) newValue;
600 return newValue;
601 }
602 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800603}
604
605/* Add a 64-bit constant to the constant pool or mixed with code */
606LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800608{
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 //FIXME: hard-coded little endian, need BE variant
610 // Insert high word into list first
611 addWordData(cUnit, constantListP, valHi);
612 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800613}
614
Ian Rogersab058bb2012-03-11 22:19:38 -0700615void pushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 buf.push_back( data & 0xff);
617 buf.push_back( (data >> 8) & 0xff);
618 buf.push_back( (data >> 16) & 0xff);
619 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800620}
621
Ian Rogersab058bb2012-03-11 22:19:38 -0700622void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 while (buf.size() < offset) {
624 buf.push_back(0);
625 }
buzbeee3acd072012-02-25 17:03:10 -0800626}
627
Brian Carlstromf5822582012-03-19 22:34:31 -0700628bool IsDirect(int invokeType) {
629 InvokeType type = static_cast<InvokeType>(invokeType);
630 return type == kStatic || type == kDirect;
631}
632
buzbeee3acd072012-02-25 17:03:10 -0800633/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800634void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800635{
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
637 LIR* dataLIR = cUnit->literalList;
638 while (dataLIR != NULL) {
639 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
640 dataLIR = NEXT_LIR(dataLIR);
641 }
642 // Push code and method literals, record offsets for the compiler to patch.
643 dataLIR = cUnit->codeLiteralList;
644 if (dataLIR != NULL) {
buzbeee3acd072012-02-25 17:03:10 -0800645 while (dataLIR != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 cUnit->compiler->AddCodePatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 cUnit->method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700649 cUnit->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700650 target,
Ian Rogers08f753d2012-08-24 14:35:25 -0700651 static_cast<InvokeType>(dataLIR->operands[1]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 cUnit->codeBuffer.size());
653 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
654 // unique based on target to ensure code deduplication works
655 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
656 pushWord(cUnit->codeBuffer, unique_patch_value);
657 dataLIR = NEXT_LIR(dataLIR);
buzbeee3acd072012-02-25 17:03:10 -0800658 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 dataLIR = cUnit->methodLiteralList;
660 while (dataLIR != NULL) {
661 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 cUnit->compiler->AddMethodPatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 cUnit->method_idx,
Ian Rogers08f753d2012-08-24 14:35:25 -0700664 cUnit->invoke_type,
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 target,
Ian Rogers08f753d2012-08-24 14:35:25 -0700666 static_cast<InvokeType>(dataLIR->operands[1]),
Bill Buzbeea114add2012-05-03 15:00:40 -0700667 cUnit->codeBuffer.size());
668 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
669 // unique based on target to ensure code deduplication works
670 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
671 pushWord(cUnit->codeBuffer, unique_patch_value);
672 dataLIR = NEXT_LIR(dataLIR);
Ian Rogers3fa13792012-03-18 15:53:45 -0700673 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700675
buzbeee3acd072012-02-25 17:03:10 -0800676}
677
678/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800679void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800680{
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 GrowableListIterator iterator;
682 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
683 while (true) {
684 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
685 &iterator);
686 if (tabRec == NULL) break;
687 alignBuffer(cUnit->codeBuffer, tabRec->offset);
688 /*
689 * For Arm, our reference point is the address of the bx
690 * instruction that does the launch, so we have to subtract
691 * the auto pc-advance. For other targets the reference point
692 * is a label, so we can use the offset as-is.
693 */
buzbeec5159d52012-03-03 11:48:39 -0800694#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 int bxOffset = tabRec->anchor->offset + 4;
Ian Rogers7caad772012-03-30 01:07:54 -0700696#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 int bxOffset = 0;
buzbeec5159d52012-03-03 11:48:39 -0800698#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 int bxOffset = tabRec->anchor->offset;
buzbeec5159d52012-03-03 11:48:39 -0800700#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 if (cUnit->printMe) {
702 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800703 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
705 int* keys = (int*)&(tabRec->table[2]);
706 for (int elems = 0; elems < tabRec->table[1]; elems++) {
707 int disp = tabRec->targets[elems]->offset - bxOffset;
708 if (cUnit->printMe) {
709 LOG(INFO) << " Case[" << elems << "] key: 0x"
710 << std::hex << keys[elems] << ", disp: 0x"
711 << std::hex << disp;
712 }
713 pushWord(cUnit->codeBuffer, keys[elems]);
714 pushWord(cUnit->codeBuffer,
715 tabRec->targets[elems]->offset - bxOffset);
716 }
717 } else {
718 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
719 static_cast<int>(Instruction::kPackedSwitchSignature));
720 for (int elems = 0; elems < tabRec->table[1]; elems++) {
721 int disp = tabRec->targets[elems]->offset - bxOffset;
722 if (cUnit->printMe) {
723 LOG(INFO) << " Case[" << elems << "] disp: 0x"
724 << std::hex << disp;
725 }
726 pushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset);
727 }
728 }
729 }
buzbeee3acd072012-02-25 17:03:10 -0800730}
731
732/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800733void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800734{
Bill Buzbeea114add2012-05-03 15:00:40 -0700735 GrowableListIterator iterator;
736 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
737 while (true) {
738 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
739 &iterator);
740 if (tabRec == NULL) break;
741 alignBuffer(cUnit->codeBuffer, tabRec->offset);
742 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
743 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
744 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800745 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 }
buzbeee3acd072012-02-25 17:03:10 -0800747}
748
buzbee31a4a6f2012-02-28 15:36:15 -0800749int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800750{
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 for (;lir != NULL; lir = lir->next) {
752 lir->offset = offset;
753 offset += 4;
754 }
755 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800756}
757
buzbee31a4a6f2012-02-28 15:36:15 -0800758void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800759{
buzbee8320f382012-09-11 16:29:42 -0700760 for (LIR* tgtLIR = (LIR *) cUnit->firstLIRInsn; tgtLIR != NULL; tgtLIR = NEXT_LIR(tgtLIR)) {
761 if (!tgtLIR->flags.isNop && (tgtLIR->opcode == kPseudoSafepointPC)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700762 cUnit->mappingTable.push_back(tgtLIR->offset);
763 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
buzbeee3acd072012-02-25 17:03:10 -0800764 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700765 }
buzbeee3acd072012-02-25 17:03:10 -0800766}
767
Ian Rogers0c7abda2012-09-19 13:33:42 -0700768class NativePcToReferenceMapBuilder {
769 public:
770 NativePcToReferenceMapBuilder(std::vector<uint8_t>* table,
771 size_t entries, uint32_t max_native_offset,
772 size_t references_width) : entries_(entries),
773 references_width_(references_width), in_use_(entries),
774 table_(table) {
775 // Compute width in bytes needed to hold max_native_offset.
776 native_offset_width_ = 0;
777 while (max_native_offset != 0) {
778 native_offset_width_++;
779 max_native_offset >>= 8;
780 }
781 // Resize table and set up header.
782 table->resize((EntryWidth() * entries) + sizeof(uint32_t));
Ian Rogers000d7242012-09-21 16:07:36 -0700783 CHECK_LT(native_offset_width_, 1U << 3);
784 (*table)[0] = native_offset_width_ & 7;
785 CHECK_LT(references_width_, 1U << 13);
786 (*table)[0] |= (references_width_ << 3) & 0xFF;
787 (*table)[1] = (references_width_ >> 5) & 0xFF;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700788 CHECK_LT(entries, 1U << 16);
789 (*table)[2] = entries & 0xFF;
790 (*table)[3] = (entries >> 8) & 0xFF;
791 }
792
793 void AddEntry(uint32_t native_offset, const uint8_t* references) {
794 size_t table_index = TableIndex(native_offset);
795 while (in_use_[table_index]) {
796 table_index = (table_index + 1) % entries_;
797 }
798 in_use_[table_index] = true;
799 SetNativeOffset(table_index, native_offset);
800 DCHECK_EQ(native_offset, GetNativeOffset(table_index));
801 SetReferences(table_index, references);
802 }
803
804 private:
805 size_t TableIndex(uint32_t native_offset) {
806 return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_;
807 }
808
809 uint32_t GetNativeOffset(size_t table_index) {
810 uint32_t native_offset = 0;
811 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
812 for (size_t i = 0; i < native_offset_width_; i++) {
813 native_offset |= (*table_)[table_offset + i] << (i * 8);
814 }
815 return native_offset;
816 }
817
818 void SetNativeOffset(size_t table_index, uint32_t native_offset) {
819 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
820 for (size_t i = 0; i < native_offset_width_; i++) {
821 (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF;
822 }
823 }
824
825 void SetReferences(size_t table_index, const uint8_t* references) {
826 size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t);
827 memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_);
828 }
829
830 size_t EntryWidth() const {
831 return native_offset_width_ + references_width_;
832 }
833
834 // Number of entries in the table.
835 const size_t entries_;
836 // Number of bytes used to encode the reference bitmap.
837 const size_t references_width_;
838 // Number of bytes used to encode a native offset.
839 size_t native_offset_width_;
840 // Entries that are in use.
841 std::vector<bool> in_use_;
842 // The table we're building.
843 std::vector<uint8_t>* const table_;
844};
845
846static void createNativeGcMap(CompilationUnit* cUnit) {
847 const std::vector<uint32_t>& mapping_table = cUnit->mappingTable;
848 uint32_t max_native_offset = 0;
849 for (size_t i = 0; i < mapping_table.size(); i += 2) {
850 uint32_t native_offset = mapping_table[i + 0];
851 if (native_offset > max_native_offset) {
852 max_native_offset = native_offset;
853 }
854 }
855 Compiler::MethodReference method_ref(cUnit->dex_file, cUnit->method_idx);
856 const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref);
857 verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4);
858 // Compute native offset to references size.
859 NativePcToReferenceMapBuilder native_gc_map_builder(&cUnit->nativeGcMap,
860 mapping_table.size() / 2, max_native_offset,
861 dex_gc_map.RegWidth());
862
863 for (size_t i = 0; i < mapping_table.size(); i += 2) {
864 uint32_t native_offset = mapping_table[i + 0];
865 uint32_t dex_pc = mapping_table[i + 1];
866 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
867 if (references != NULL) {
868 native_gc_map_builder.AddEntry(native_offset, references);
869 } else {
870 // TODO: there is a mapping table entry but no reference bitmap. This happens because of
871 // catch block entries. We should check that the dex_pc corresponds with a catch block
872 // here.
873 }
874 }
875}
876
buzbeee3acd072012-02-25 17:03:10 -0800877/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800878int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800879{
Bill Buzbeea114add2012-05-03 15:00:40 -0700880 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
881 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
882 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
883 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800884}
885
buzbee31a4a6f2012-02-28 15:36:15 -0800886int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800887{
Bill Buzbeea114add2012-05-03 15:00:40 -0700888 GrowableListIterator iterator;
889 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
890 while (true) {
891 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
892 &iterator);
893 if (tabRec == NULL) break;
894 tabRec->offset = offset;
895 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
896 offset += tabRec->table[1] * (sizeof(int) * 2);
897 } else {
898 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
899 static_cast<int>(Instruction::kPackedSwitchSignature));
900 offset += tabRec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800901 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 }
903 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800904}
905
buzbee31a4a6f2012-02-28 15:36:15 -0800906int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800907{
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 GrowableListIterator iterator;
909 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
910 while (true) {
911 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
912 &iterator);
913 if (tabRec == NULL) break;
914 tabRec->offset = offset;
915 offset += tabRec->size;
916 // word align
917 offset = (offset + 3) & ~3;
918 }
919 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800920}
921
922/*
923 * Walk the compilation unit and assign offsets to instructions
924 * and literals and compute the total size of the compiled unit.
925 */
926void oatAssignOffsets(CompilationUnit* cUnit)
927{
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 int offset = oatAssignInsnOffsets(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800929
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 /* Const values have to be word aligned */
931 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800932
Bill Buzbeea114add2012-05-03 15:00:40 -0700933 /* Set up offsets for literals */
934 cUnit->dataOffset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800935
Bill Buzbeea114add2012-05-03 15:00:40 -0700936 offset = assignLiteralOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800937
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 offset = assignSwitchTablesOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800939
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 offset = assignFillArrayDataOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800941
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 cUnit->totalSize = offset;
buzbeee3acd072012-02-25 17:03:10 -0800943}
944
945/*
946 * Go over each instruction in the list and calculate the offset from the top
947 * before sending them off to the assembler. If out-of-range branch distance is
948 * seen rearrange the instructions a bit to correct it.
949 */
950void oatAssembleLIR(CompilationUnit* cUnit)
951{
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 oatAssignOffsets(cUnit);
953 /*
954 * Assemble here. Note that we generate code with optimistic assumptions
955 * and if found now to work, we'll have to redo the sequence and retry.
956 */
buzbeee3acd072012-02-25 17:03:10 -0800957
Bill Buzbeea114add2012-05-03 15:00:40 -0700958 while (true) {
959 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
960 if (res == kSuccess) {
961 break;
962 } else {
963 cUnit->assemblerRetries++;
964 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
965 oatCodegenDump(cUnit);
966 LOG(FATAL) << "Assembler error - too many retries";
967 }
968 // Redo offsets and try again
969 oatAssignOffsets(cUnit);
970 cUnit->codeBuffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800971 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700972 }
buzbeee3acd072012-02-25 17:03:10 -0800973
Bill Buzbeea114add2012-05-03 15:00:40 -0700974 // Install literals
975 installLiteralPools(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800976
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 // Install switch tables
978 installSwitchTables(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800979
Bill Buzbeea114add2012-05-03 15:00:40 -0700980 // Install fill array data
981 installFillArrayData(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800982
Ian Rogers0c7abda2012-09-19 13:33:42 -0700983 // Create the mapping table and native offset to reference map.
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 createMappingTable(cUnit);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700985
986 createNativeGcMap(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800987}
988
buzbee31a4a6f2012-02-28 15:36:15 -0800989/*
990 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
991 * offset vaddr. This label will be used to fix up the case
992 * branch table during the assembly phase. Be sure to set
993 * all resource flags on this to prevent code motion across
994 * target boundaries. KeyVal is just there for debugging.
995 */
996LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
997{
Bill Buzbeea114add2012-05-03 15:00:40 -0700998 SafeMap<unsigned int, LIR*>::iterator it;
999 it = cUnit->boundaryMap.find(vaddr);
1000 if (it == cUnit->boundaryMap.end()) {
1001 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
1002 }
1003 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
1004 newLabel->dalvikOffset = vaddr;
1005 newLabel->opcode = kPseudoCaseLabel;
1006 newLabel->operands[0] = keyVal;
1007 oatInsertLIRAfter(it->second, (LIR*)newLabel);
1008 return newLabel;
buzbee31a4a6f2012-02-28 15:36:15 -08001009}
1010
1011void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
1012{
Bill Buzbeea114add2012-05-03 15:00:40 -07001013 const u2* table = tabRec->table;
1014 int baseVaddr = tabRec->vaddr;
1015 int *targets = (int*)&table[4];
1016 int entries = table[1];
1017 int lowKey = s4FromSwitchData(&table[2]);
1018 for (int i = 0; i < entries; i++) {
1019 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
1020 i + lowKey);
1021 }
buzbee31a4a6f2012-02-28 15:36:15 -08001022}
1023
1024void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
1025{
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 const u2* table = tabRec->table;
1027 int baseVaddr = tabRec->vaddr;
1028 int entries = table[1];
1029 int* keys = (int*)&table[2];
1030 int* targets = &keys[entries];
1031 for (int i = 0; i < entries; i++) {
1032 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
1033 keys[i]);
1034 }
buzbee31a4a6f2012-02-28 15:36:15 -08001035}
1036
1037void oatProcessSwitchTables(CompilationUnit* cUnit)
1038{
Bill Buzbeea114add2012-05-03 15:00:40 -07001039 GrowableListIterator iterator;
1040 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
1041 while (true) {
1042 SwitchTable *tabRec =
1043 (SwitchTable *) oatGrowableListIteratorNext(&iterator);
1044 if (tabRec == NULL) break;
1045 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
1046 markPackedCaseLabels(cUnit, tabRec);
1047 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
1048 markSparseCaseLabels(cUnit, tabRec);
1049 } else {
1050 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -08001051 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 }
buzbee31a4a6f2012-02-28 15:36:15 -08001053}
1054
1055//FIXME: Do we have endian issues here?
1056
1057void dumpSparseSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 /*
1059 * Sparse switch data format:
1060 * ushort ident = 0x0200 magic value
1061 * ushort size number of entries in the table; > 0
1062 * int keys[size] keys, sorted low-to-high; 32-bit aligned
1063 * int targets[size] branch targets, relative to switch opcode
1064 *
1065 * Total size is (2+size*4) 16-bit code units.
1066 */
buzbee31a4a6f2012-02-28 15:36:15 -08001067{
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 u2 ident = table[0];
1069 int entries = table[1];
1070 int* keys = (int*)&table[2];
1071 int* targets = &keys[entries];
1072 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
1073 << ", entries: " << std::dec << entries;
1074 for (int i = 0; i < entries; i++) {
1075 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
1076 }
buzbee31a4a6f2012-02-28 15:36:15 -08001077}
1078
1079void dumpPackedSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -07001080 /*
1081 * Packed switch data format:
1082 * ushort ident = 0x0100 magic value
1083 * ushort size number of entries in the table
1084 * int first_key first (and lowest) switch case value
1085 * int targets[size] branch targets, relative to switch opcode
1086 *
1087 * Total size is (4+size*2) 16-bit code units.
1088 */
buzbee31a4a6f2012-02-28 15:36:15 -08001089{
Bill Buzbeea114add2012-05-03 15:00:40 -07001090 u2 ident = table[0];
1091 int* targets = (int*)&table[4];
1092 int entries = table[1];
1093 int lowKey = s4FromSwitchData(&table[2]);
1094 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
1095 << ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
1096 for (int i = 0; i < entries; i++) {
1097 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex
1098 << targets[i];
1099 }
buzbee31a4a6f2012-02-28 15:36:15 -08001100}
buzbeee3acd072012-02-25 17:03:10 -08001101
buzbeed1643e42012-09-05 14:06:51 -07001102/*
1103 * Set up special LIR to mark a Dalvik byte-code instruction start and
1104 * record it in the boundaryMap. NOTE: in cases such as kMirOpCheck in
1105 * which we split a single Dalvik instruction, only the first MIR op
1106 * associated with a Dalvik PC should be entered into the map.
1107 */
1108LIR* markBoundary(CompilationUnit* cUnit, int offset, const char* instStr)
1109{
1110 LIR* res = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary, (intptr_t) instStr);
1111 if (cUnit->boundaryMap.find(offset) == cUnit->boundaryMap.end()) {
1112 cUnit->boundaryMap.Put(offset, res);
1113 }
1114 return res;
1115}
buzbeee3acd072012-02-25 17:03:10 -08001116
buzbeed1643e42012-09-05 14:06:51 -07001117}
1118 // namespace art