buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Brian Carlstrom | 641ce03 | 2013-01-31 15:21:37 -0800 | [diff] [blame] | 17 | #include "compiler/compiler_internals.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 18 | #include "gc_map.h" |
| 19 | #include "verifier/dex_gc_map.h" |
| 20 | #include "verifier/method_verifier.h" |
buzbee | 1bc37c6 | 2012-11-20 13:35:41 -0800 | [diff] [blame] | 21 | #include "ralloc_util.h" |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 22 | #include "codegen_util.h" |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 23 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 24 | namespace art { |
| 25 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 26 | void MarkSafepointPC(CompilationUnit* cu, LIR* inst) |
| 27 | { |
| 28 | inst->def_mask = ENCODE_ALL; |
| 29 | LIR* safepoint_pc = NewLIR0(cu, kPseudoSafepointPC); |
| 30 | DCHECK_EQ(safepoint_pc->def_mask, ENCODE_ALL); |
| 31 | } |
| 32 | |
| 33 | bool FastInstance(CompilationUnit* cu, uint32_t field_idx, |
| 34 | int& field_offset, bool& is_volatile, bool is_put) |
| 35 | { |
| 36 | OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, |
TDYa127 | dc5daa0 | 2013-01-09 21:31:37 +0800 | [diff] [blame] | 37 | *cu->dex_file, cu->code_item, |
| 38 | cu->class_def_idx, cu->method_idx, |
| 39 | cu->access_flags); |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 40 | return cu->compiler->ComputeInstanceFieldInfo(field_idx, &m_unit, |
| 41 | field_offset, is_volatile, is_put); |
| 42 | } |
| 43 | |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 44 | /* Convert an instruction to a NOP */ |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 45 | void NopLIR( LIR* lir) |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 46 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 47 | lir->flags.is_nop = true; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 48 | } |
| 49 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 50 | void SetMemRefType(CompilationUnit* cu, LIR* lir, bool is_load, int mem_type) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 51 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 52 | uint64_t *mask_ptr; |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 53 | uint64_t mask = ENCODE_MEM;; |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 54 | Codegen* cg = cu->cg.get(); |
| 55 | DCHECK(cg->GetTargetInstFlags(lir->opcode) & (IS_LOAD | IS_STORE)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 56 | if (is_load) { |
| 57 | mask_ptr = &lir->use_mask; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 58 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 59 | mask_ptr = &lir->def_mask; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 60 | } |
| 61 | /* Clear out the memref flags */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 62 | *mask_ptr &= ~mask; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 63 | /* ..and then add back the one we need */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 64 | switch (mem_type) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 65 | case kLiteral: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 66 | DCHECK(is_load); |
| 67 | *mask_ptr |= ENCODE_LITERAL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 68 | break; |
| 69 | case kDalvikReg: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 70 | *mask_ptr |= ENCODE_DALVIK_REG; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 71 | break; |
| 72 | case kHeapRef: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 73 | *mask_ptr |= ENCODE_HEAP_REF; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 74 | break; |
| 75 | case kMustNotAlias: |
| 76 | /* Currently only loads can be marked as kMustNotAlias */ |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 77 | DCHECK(!(cg->GetTargetInstFlags(lir->opcode) & IS_STORE)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 78 | *mask_ptr |= ENCODE_MUST_NOT_ALIAS; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 79 | break; |
| 80 | default: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 81 | LOG(FATAL) << "Oat: invalid memref kind - " << mem_type; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 82 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 86 | * Mark load/store instructions that access Dalvik registers through the stack. |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 87 | */ |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 88 | void AnnotateDalvikRegAccess(CompilationUnit* cu, LIR* lir, int reg_id, bool is_load, bool is64bit) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 89 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 90 | SetMemRefType(cu, lir, is_load, kDalvikReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 91 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 92 | /* |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 93 | * Store the Dalvik register id in alias_info. Mark the MSB if it is a 64-bit |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 94 | * access. |
| 95 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 96 | lir->alias_info = ENCODE_ALIAS_INFO(reg_id, is64bit); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Mark the corresponding bit(s). |
| 101 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 102 | void SetupRegMask(CompilationUnit* cu, uint64_t* mask, int reg) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 103 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 104 | Codegen* cg = cu->cg.get(); |
| 105 | *mask |= cg->GetRegMaskCommon(cu, reg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Set up the proper fields in the resource mask |
| 110 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 111 | void SetupResourceMasks(CompilationUnit* cu, LIR* lir) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 112 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 113 | int opcode = lir->opcode; |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 114 | Codegen* cg = cu->cg.get(); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 115 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 116 | if (opcode <= 0) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 117 | lir->use_mask = lir->def_mask = 0; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 118 | return; |
| 119 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 120 | |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 121 | uint64_t flags = cg->GetTargetInstFlags(opcode); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 122 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 123 | if (flags & NEEDS_FIXUP) { |
| 124 | lir->flags.pcRelFixup = true; |
| 125 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 126 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 127 | /* Get the starting size of the instruction's template */ |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 128 | lir->flags.size = cg->GetInsnSize(lir); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 129 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 130 | /* Set up the mask for resources that are updated */ |
| 131 | if (flags & (IS_LOAD | IS_STORE)) { |
| 132 | /* Default to heap - will catch specialized classes later */ |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 133 | SetMemRefType(cu, lir, flags & IS_LOAD, kHeapRef); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 134 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 135 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 136 | /* |
| 137 | * Conservatively assume the branch here will call out a function that in |
| 138 | * turn will trash everything. |
| 139 | */ |
| 140 | if (flags & IS_BRANCH) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 141 | lir->def_mask = lir->use_mask = ENCODE_ALL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 144 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 145 | if (flags & REG_DEF0) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 146 | SetupRegMask(cu, &lir->def_mask, lir->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 147 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 148 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 149 | if (flags & REG_DEF1) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 150 | SetupRegMask(cu, &lir->def_mask, lir->operands[1]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 151 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 152 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 153 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 154 | if (flags & SETS_CCODES) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 155 | lir->def_mask |= ENCODE_CCODE; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 156 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 157 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 158 | if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) { |
| 159 | int i; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 160 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 161 | for (i = 0; i < 4; i++) { |
| 162 | if (flags & (1 << (kRegUse0 + i))) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 163 | SetupRegMask(cu, &lir->use_mask, lir->operands[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 164 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 165 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 166 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 167 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 168 | if (flags & USES_CCODES) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 169 | lir->use_mask |= ENCODE_CCODE; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 170 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 171 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 172 | // Handle target-specific actions |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 173 | cg->SetupTargetResourceMasks(cu, lir); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 177 | * Debugging macros |
| 178 | */ |
| 179 | #define DUMP_RESOURCE_MASK(X) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 180 | |
| 181 | /* Pretty-print a LIR instruction */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 182 | void DumpLIRInsn(CompilationUnit* cu, LIR* lir, unsigned char* base_addr) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 183 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 184 | int offset = lir->offset; |
| 185 | int dest = lir->operands[0]; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 186 | const bool dump_nop = (cu->enable_debug & (1 << kDebugShowNops)); |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 187 | Codegen* cg = cu->cg.get(); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 188 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 189 | /* Handle pseudo-ops individually, and all regular insns as a group */ |
| 190 | switch (lir->opcode) { |
| 191 | case kPseudoMethodEntry: |
| 192 | LOG(INFO) << "-------- method entry " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 193 | << PrettyMethod(cu->method_idx, *cu->dex_file); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 194 | break; |
| 195 | case kPseudoMethodExit: |
| 196 | LOG(INFO) << "-------- Method_Exit"; |
| 197 | break; |
| 198 | case kPseudoBarrier: |
| 199 | LOG(INFO) << "-------- BARRIER"; |
| 200 | break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 201 | case kPseudoEntryBlock: |
| 202 | LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest; |
| 203 | break; |
| 204 | case kPseudoDalvikByteCodeBoundary: |
| 205 | LOG(INFO) << "-------- dalvik offset: 0x" << std::hex |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 206 | << lir->dalvik_offset << " @ " << reinterpret_cast<char*>(lir->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 207 | break; |
| 208 | case kPseudoExitBlock: |
| 209 | LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest; |
| 210 | break; |
| 211 | case kPseudoPseudoAlign4: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 212 | LOG(INFO) << reinterpret_cast<uintptr_t>(base_addr) + offset << " (0x" << std::hex |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 213 | << offset << "): .align4"; |
| 214 | break; |
| 215 | case kPseudoEHBlockLabel: |
| 216 | LOG(INFO) << "Exception_Handling:"; |
| 217 | break; |
| 218 | case kPseudoTargetLabel: |
| 219 | case kPseudoNormalBlockLabel: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 220 | LOG(INFO) << "L" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 221 | break; |
| 222 | case kPseudoThrowTarget: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 223 | LOG(INFO) << "LT" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 224 | break; |
| 225 | case kPseudoIntrinsicRetry: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 226 | LOG(INFO) << "IR" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 227 | break; |
| 228 | case kPseudoSuspendTarget: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 229 | LOG(INFO) << "LS" << reinterpret_cast<void*>(lir) << ":"; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 230 | break; |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 231 | case kPseudoSafepointPC: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 232 | LOG(INFO) << "LsafepointPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
buzbee | 8320f38 | 2012-09-11 16:29:42 -0700 | [diff] [blame] | 233 | break; |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 234 | case kPseudoExportedPC: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 235 | LOG(INFO) << "LexportedPC_0x" << std::hex << lir->offset << "_" << lir->dalvik_offset << ":"; |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 236 | break; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 237 | case kPseudoCaseLabel: |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 238 | LOG(INFO) << "LC" << reinterpret_cast<void*>(lir) << ": Case target 0x" |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 239 | << std::hex << lir->operands[0] << "|" << std::dec << |
| 240 | lir->operands[0]; |
| 241 | break; |
| 242 | default: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 243 | if (lir->flags.is_nop && !dump_nop) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 244 | break; |
| 245 | } else { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 246 | std::string op_name(cg->BuildInsnString(cg->GetTargetInstName(lir->opcode), |
| 247 | lir, base_addr)); |
| 248 | std::string op_operands(cg->BuildInsnString(cg->GetTargetInstFmt(lir->opcode), |
| 249 | lir, base_addr)); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 250 | LOG(INFO) << StringPrintf("%05x: %-9s%s%s", |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 251 | reinterpret_cast<unsigned int>(base_addr + offset), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 252 | op_name.c_str(), op_operands.c_str(), |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 253 | lir->flags.is_nop ? "(nop)" : ""); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 254 | } |
| 255 | break; |
| 256 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 257 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 258 | if (lir->use_mask && (!lir->flags.is_nop || dump_nop)) { |
| 259 | DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->use_mask, "use")); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 260 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 261 | if (lir->def_mask && (!lir->flags.is_nop || dump_nop)) { |
| 262 | DUMP_RESOURCE_MASK(DumpResourceMask((LIR* ) lir, lir->def_mask, "def")); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 263 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 264 | } |
| 265 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 266 | void DumpPromotionMap(CompilationUnit *cu) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 267 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 268 | Codegen* cg = cu->cg.get(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 269 | int num_regs = cu->num_dalvik_registers + cu->num_compiler_temps + 1; |
| 270 | for (int i = 0; i < num_regs; i++) { |
| 271 | PromotionMap v_reg_map = cu->promotion_map[i]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 272 | std::string buf; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 273 | if (v_reg_map.fp_location == kLocPhysReg) { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 274 | StringAppendF(&buf, " : s%d", v_reg_map.FpReg & cg->FpRegMask()); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 275 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 276 | |
| 277 | std::string buf3; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 278 | if (i < cu->num_dalvik_registers) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 279 | StringAppendF(&buf3, "%02d", i); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 280 | } else if (i == cu->method_sreg) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 281 | buf3 = "Method*"; |
| 282 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 283 | StringAppendF(&buf3, "ct%d", i - cu->num_dalvik_registers); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(), |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 287 | v_reg_map.core_location == kLocPhysReg ? |
| 288 | "r" : "SP+", v_reg_map.core_location == kLocPhysReg ? |
| 289 | v_reg_map.core_reg : SRegOffset(cu, i), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 290 | buf.c_str()); |
| 291 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 294 | /* Dump a mapping table */ |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 295 | static void DumpMappingTable(const char* table_name, const std::string& descriptor, |
| 296 | const std::string& name, const std::string& signature, |
| 297 | const std::vector<uint32_t>& v) { |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 298 | if (v.size() > 0) { |
| 299 | std::string line(StringPrintf("\n %s %s%s_%s_table[%zu] = {", table_name, |
| 300 | descriptor.c_str(), name.c_str(), signature.c_str(), v.size())); |
| 301 | std::replace(line.begin(), line.end(), ';', '_'); |
| 302 | LOG(INFO) << line; |
| 303 | for (uint32_t i = 0; i < v.size(); i+=2) { |
| 304 | line = StringPrintf(" {0x%05x, 0x%04x},", v[i], v[i+1]); |
| 305 | LOG(INFO) << line; |
| 306 | } |
| 307 | LOG(INFO) <<" };\n\n"; |
| 308 | } |
| 309 | } |
| 310 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 311 | /* Dump instructions and constant pool contents */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 312 | void CodegenDump(CompilationUnit* cu) |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 313 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 314 | LOG(INFO) << "Dumping LIR insns for " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 315 | << PrettyMethod(cu->method_idx, *cu->dex_file); |
| 316 | LIR* lir_insn; |
| 317 | int insns_size = cu->insns_size; |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 318 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 319 | LOG(INFO) << "Regs (excluding ins) : " << cu->num_regs; |
| 320 | LOG(INFO) << "Ins : " << cu->num_ins; |
| 321 | LOG(INFO) << "Outs : " << cu->num_outs; |
| 322 | LOG(INFO) << "CoreSpills : " << cu->num_core_spills; |
| 323 | LOG(INFO) << "FPSpills : " << cu->num_fp_spills; |
| 324 | LOG(INFO) << "CompilerTemps : " << cu->num_compiler_temps; |
| 325 | LOG(INFO) << "Frame size : " << cu->frame_size; |
| 326 | LOG(INFO) << "code size is " << cu->total_size << |
| 327 | " bytes, Dalvik size is " << insns_size * 2; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 328 | LOG(INFO) << "expansion factor: " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 329 | << static_cast<float>(cu->total_size) / static_cast<float>(insns_size * 2); |
| 330 | DumpPromotionMap(cu); |
buzbee | 28c9a83 | 2012-11-21 15:39:13 -0800 | [diff] [blame] | 331 | for (lir_insn = cu->first_lir_insn; lir_insn != NULL; lir_insn = lir_insn->next) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 332 | DumpLIRInsn(cu, lir_insn, 0); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 333 | } |
buzbee | 28c9a83 | 2012-11-21 15:39:13 -0800 | [diff] [blame] | 334 | for (lir_insn = cu->literal_list; lir_insn != NULL; lir_insn = lir_insn->next) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 335 | LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)", lir_insn->offset, lir_insn->offset, |
| 336 | lir_insn->operands[0]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 337 | } |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 338 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 339 | const DexFile::MethodId& method_id = |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 340 | cu->dex_file->GetMethodId(cu->method_idx); |
| 341 | std::string signature(cu->dex_file->GetMethodSignature(method_id)); |
| 342 | std::string name(cu->dex_file->GetMethodName(method_id)); |
| 343 | std::string descriptor(cu->dex_file->GetMethodDeclaringClassDescriptor(method_id)); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 344 | |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 345 | // Dump mapping tables |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 346 | DumpMappingTable("PC2Dex_MappingTable", descriptor, name, signature, cu->pc2dexMappingTable); |
| 347 | DumpMappingTable("Dex2PC_MappingTable", descriptor, name, signature, cu->dex2pcMappingTable); |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 348 | } |
| 349 | |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 350 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 351 | LIR* RawLIR(CompilationUnit* cu, int dalvik_offset, int opcode, int op0, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 352 | int op1, int op2, int op3, int op4, LIR* target) |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 353 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 354 | LIR* insn = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR)); |
| 355 | insn->dalvik_offset = dalvik_offset; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 356 | insn->opcode = opcode; |
| 357 | insn->operands[0] = op0; |
| 358 | insn->operands[1] = op1; |
| 359 | insn->operands[2] = op2; |
| 360 | insn->operands[3] = op3; |
| 361 | insn->operands[4] = op4; |
| 362 | insn->target = target; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 363 | SetupResourceMasks(cu, insn); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 364 | if ((opcode == kPseudoTargetLabel) || (opcode == kPseudoSafepointPC) || |
| 365 | (opcode == kPseudoExportedPC)) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 366 | // Always make labels scheduling barriers |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 367 | insn->use_mask = insn->def_mask = ENCODE_ALL; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 368 | } |
| 369 | return insn; |
buzbee | a2ebdd7 | 2012-03-04 14:57:06 -0800 | [diff] [blame] | 370 | } |
| 371 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 372 | /* |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 373 | * The following are building blocks to construct low-level IRs with 0 - 4 |
| 374 | * operands. |
| 375 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 376 | LIR* NewLIR0(CompilationUnit* cu, int opcode) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 377 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 378 | Codegen* cg = cu->cg.get(); |
| 379 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & NO_OPERAND)) |
| 380 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 381 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 382 | << cu->current_dalvik_offset; |
| 383 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode); |
| 384 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 385 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 386 | } |
| 387 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 388 | LIR* NewLIR1(CompilationUnit* cu, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 389 | int dest) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 390 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 391 | Codegen* cg = cu->cg.get(); |
| 392 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_UNARY_OP)) |
| 393 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 394 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 395 | << cu->current_dalvik_offset; |
| 396 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest); |
| 397 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 398 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 399 | } |
| 400 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 401 | LIR* NewLIR2(CompilationUnit* cu, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 402 | int dest, int src1) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 403 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 404 | Codegen* cg = cu->cg.get(); |
| 405 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_BINARY_OP)) |
| 406 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 407 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 408 | << cu->current_dalvik_offset; |
| 409 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1); |
| 410 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 411 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 412 | } |
| 413 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 414 | LIR* NewLIR3(CompilationUnit* cu, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 415 | int dest, int src1, int src2) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 416 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 417 | Codegen* cg = cu->cg.get(); |
| 418 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_TERTIARY_OP)) |
| 419 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 420 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 421 | << cu->current_dalvik_offset; |
| 422 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2); |
| 423 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 424 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 425 | } |
| 426 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 427 | LIR* NewLIR4(CompilationUnit* cu, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 428 | int dest, int src1, int src2, int info) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 429 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 430 | Codegen* cg = cu->cg.get(); |
| 431 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUAD_OP)) |
| 432 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 433 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 434 | << cu->current_dalvik_offset; |
| 435 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info); |
| 436 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 437 | return insn; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 438 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 439 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 440 | LIR* NewLIR5(CompilationUnit* cu, int opcode, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 441 | int dest, int src1, int src2, int info1, int info2) |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 442 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 443 | Codegen* cg = cu->cg.get(); |
| 444 | DCHECK(is_pseudo_opcode(opcode) || (cg->GetTargetInstFlags(opcode) & IS_QUIN_OP)) |
| 445 | << cg->GetTargetInstName(opcode) << " " << opcode << " " |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 446 | << PrettyMethod(cu->method_idx, *cu->dex_file) << " " |
| 447 | << cu->current_dalvik_offset; |
| 448 | LIR* insn = RawLIR(cu, cu->current_dalvik_offset, opcode, dest, src1, src2, info1, info2); |
| 449 | AppendLIR(cu, insn); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 450 | return insn; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 451 | } |
| 452 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 453 | /* |
| 454 | * Search the existing constants in the literal pool for an exact or close match |
| 455 | * within specified delta (greater or equal to 0). |
| 456 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 457 | LIR* ScanLiteralPool(LIR* data_target, int value, unsigned int delta) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 458 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 459 | while (data_target) { |
| 460 | if ((static_cast<unsigned>(value - data_target->operands[0])) <= delta) |
| 461 | return data_target; |
| 462 | data_target = data_target->next; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 463 | } |
| 464 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* Search the existing constants in the literal pool for an exact wide match */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 468 | LIR* ScanLiteralPoolWide(LIR* data_target, int val_lo, int val_hi) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 469 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 470 | bool lo_match = false; |
| 471 | LIR* lo_target = NULL; |
| 472 | while (data_target) { |
| 473 | if (lo_match && (data_target->operands[0] == val_hi)) { |
| 474 | return lo_target; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 475 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 476 | lo_match = false; |
| 477 | if (data_target->operands[0] == val_lo) { |
| 478 | lo_match = true; |
| 479 | lo_target = data_target; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 480 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 481 | data_target = data_target->next; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 482 | } |
| 483 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* |
| 487 | * The following are building blocks to insert constants into the pool or |
| 488 | * instruction streams. |
| 489 | */ |
| 490 | |
buzbee | 5de3494 | 2012-03-01 14:51:57 -0800 | [diff] [blame] | 491 | /* Add a 32-bit constant either in the constant pool */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 492 | LIR* AddWordData(CompilationUnit* cu, LIR* *constant_list_p, int value) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 493 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 494 | /* Add the constant to the literal pool */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 495 | if (constant_list_p) { |
| 496 | LIR* new_value = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocData)); |
| 497 | new_value->operands[0] = value; |
| 498 | new_value->next = *constant_list_p; |
| 499 | *constant_list_p = new_value; |
| 500 | return new_value; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 501 | } |
| 502 | return NULL; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* Add a 64-bit constant to the constant pool or mixed with code */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 506 | LIR* AddWideData(CompilationUnit* cu, LIR* *constant_list_p, |
| 507 | int val_lo, int val_hi) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 508 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 509 | AddWordData(cu, constant_list_p, val_hi); |
| 510 | return AddWordData(cu, constant_list_p, val_lo); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 511 | } |
| 512 | |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 513 | static void PushWord(std::vector<uint8_t>&buf, int data) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 514 | buf.push_back( data & 0xff); |
| 515 | buf.push_back( (data >> 8) & 0xff); |
| 516 | buf.push_back( (data >> 16) & 0xff); |
| 517 | buf.push_back( (data >> 24) & 0xff); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 518 | } |
| 519 | |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 520 | static void AlignBuffer(std::vector<uint8_t>&buf, size_t offset) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 521 | while (buf.size() < offset) { |
| 522 | buf.push_back(0); |
| 523 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* Write the literal pool to the output stream */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 527 | static void InstallLiteralPools(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 528 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 529 | AlignBuffer(cu->code_buffer, cu->data_offset); |
| 530 | LIR* data_lir = cu->literal_list; |
| 531 | while (data_lir != NULL) { |
| 532 | PushWord(cu->code_buffer, data_lir->operands[0]); |
| 533 | data_lir = NEXT_LIR(data_lir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 534 | } |
| 535 | // Push code and method literals, record offsets for the compiler to patch. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 536 | data_lir = cu->code_literal_list; |
| 537 | while (data_lir != NULL) { |
| 538 | uint32_t target = data_lir->operands[0]; |
| 539 | cu->compiler->AddCodePatch(cu->dex_file, |
| 540 | cu->method_idx, |
| 541 | cu->invoke_type, |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 542 | target, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 543 | static_cast<InvokeType>(data_lir->operands[1]), |
| 544 | cu->code_buffer.size()); |
| 545 | const DexFile::MethodId& id = cu->dex_file->GetMethodId(target); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 546 | // unique based on target to ensure code deduplication works |
| 547 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 548 | PushWord(cu->code_buffer, unique_patch_value); |
| 549 | data_lir = NEXT_LIR(data_lir); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 550 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 551 | data_lir = cu->method_literal_list; |
| 552 | while (data_lir != NULL) { |
| 553 | uint32_t target = data_lir->operands[0]; |
| 554 | cu->compiler->AddMethodPatch(cu->dex_file, |
| 555 | cu->method_idx, |
| 556 | cu->invoke_type, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 557 | target, |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 558 | static_cast<InvokeType>(data_lir->operands[1]), |
| 559 | cu->code_buffer.size()); |
| 560 | const DexFile::MethodId& id = cu->dex_file->GetMethodId(target); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 561 | // unique based on target to ensure code deduplication works |
| 562 | uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 563 | PushWord(cu->code_buffer, unique_patch_value); |
| 564 | data_lir = NEXT_LIR(data_lir); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 565 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* Write the switch tables to the output stream */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 569 | static void InstallSwitchTables(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 570 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 571 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 572 | GrowableListIteratorInit(&cu->switch_tables, &iterator); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 573 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 574 | SwitchTable* tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext( &iterator)); |
| 575 | if (tab_rec == NULL) break; |
| 576 | AlignBuffer(cu->code_buffer, tab_rec->offset); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 577 | /* |
| 578 | * For Arm, our reference point is the address of the bx |
| 579 | * instruction that does the launch, so we have to subtract |
| 580 | * the auto pc-advance. For other targets the reference point |
| 581 | * is a label, so we can use the offset as-is. |
| 582 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 583 | int bx_offset = INVALID_OFFSET; |
| 584 | switch (cu->instruction_set) { |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 585 | case kThumb2: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 586 | bx_offset = tab_rec->anchor->offset + 4; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 587 | break; |
| 588 | case kX86: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 589 | bx_offset = 0; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 590 | break; |
| 591 | case kMips: |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 592 | bx_offset = tab_rec->anchor->offset; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 593 | break; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 594 | default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set; |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 595 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 596 | if (cu->verbose) { |
| 597 | LOG(INFO) << "Switch table for offset 0x" << std::hex << bx_offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 598 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 599 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 600 | const int* keys = reinterpret_cast<const int*>(&(tab_rec->table[2])); |
| 601 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 602 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 603 | if (cu->verbose) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 604 | LOG(INFO) << " Case[" << elems << "] key: 0x" |
| 605 | << std::hex << keys[elems] << ", disp: 0x" |
| 606 | << std::hex << disp; |
| 607 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 608 | PushWord(cu->code_buffer, keys[elems]); |
| 609 | PushWord(cu->code_buffer, |
| 610 | tab_rec->targets[elems]->offset - bx_offset); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 611 | } |
| 612 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 613 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 614 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 615 | for (int elems = 0; elems < tab_rec->table[1]; elems++) { |
| 616 | int disp = tab_rec->targets[elems]->offset - bx_offset; |
| 617 | if (cu->verbose) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 618 | LOG(INFO) << " Case[" << elems << "] disp: 0x" |
| 619 | << std::hex << disp; |
| 620 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 621 | PushWord(cu->code_buffer, tab_rec->targets[elems]->offset - bx_offset); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | /* Write the fill array dta to the output stream */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 628 | static void InstallFillArrayData(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 629 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 630 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 631 | GrowableListIteratorInit(&cu->fill_array_data, &iterator); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 632 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 633 | FillArrayData *tab_rec = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 634 | reinterpret_cast<FillArrayData*>(GrowableListIteratorNext( &iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 635 | if (tab_rec == NULL) break; |
| 636 | AlignBuffer(cu->code_buffer, tab_rec->offset); |
| 637 | for (int i = 0; i < (tab_rec->size + 1) / 2; i++) { |
| 638 | cu->code_buffer.push_back( tab_rec->table[i] & 0xFF); |
| 639 | cu->code_buffer.push_back( (tab_rec->table[i] >> 8) & 0xFF); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 640 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 641 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 642 | } |
| 643 | |
buzbee | aad9438 | 2012-11-21 07:40:50 -0800 | [diff] [blame] | 644 | static int AssignLiteralOffsetCommon(LIR* lir, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 645 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 646 | for (;lir != NULL; lir = lir->next) { |
| 647 | lir->offset = offset; |
| 648 | offset += 4; |
| 649 | } |
| 650 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 651 | } |
| 652 | |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 653 | // Make sure we have a code address for every declared catch entry |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 654 | static bool VerifyCatchEntries(CompilationUnit* cu) |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 655 | { |
| 656 | bool success = true; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 657 | for (std::set<uint32_t>::const_iterator it = cu->catches.begin(); it != cu->catches.end(); ++it) { |
| 658 | uint32_t dex_pc = *it; |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 659 | bool found = false; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 660 | for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) { |
| 661 | if (dex_pc == cu->dex2pcMappingTable[i+1]) { |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 662 | found = true; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | if (!found) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 667 | LOG(INFO) << "Missing native PC for catch entry @ 0x" << std::hex << dex_pc; |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 668 | success = false; |
| 669 | } |
| 670 | } |
| 671 | // Now, try in the other direction |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 672 | for (size_t i = 0; i < cu->dex2pcMappingTable.size(); i += 2) { |
| 673 | uint32_t dex_pc = cu->dex2pcMappingTable[i+1]; |
| 674 | if (cu->catches.find(dex_pc) == cu->catches.end()) { |
| 675 | LOG(INFO) << "Unexpected catch entry @ dex pc 0x" << std::hex << dex_pc; |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 676 | success = false; |
| 677 | } |
| 678 | } |
| 679 | if (!success) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 680 | LOG(INFO) << "Bad dex2pcMapping table in " << PrettyMethod(cu->method_idx, *cu->dex_file); |
| 681 | LOG(INFO) << "Entries @ decode: " << cu->catches.size() << ", Entries in table: " |
| 682 | << cu->dex2pcMappingTable.size()/2; |
buzbee | 6459e7c | 2012-10-02 14:42:41 -0700 | [diff] [blame] | 683 | } |
| 684 | return success; |
| 685 | } |
| 686 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 687 | static void CreateMappingTables(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 688 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 689 | for (LIR* tgt_lir = cu->first_lir_insn; tgt_lir != NULL; tgt_lir = NEXT_LIR(tgt_lir)) { |
| 690 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoSafepointPC)) { |
| 691 | cu->pc2dexMappingTable.push_back(tgt_lir->offset); |
| 692 | cu->pc2dexMappingTable.push_back(tgt_lir->dalvik_offset); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 693 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 694 | if (!tgt_lir->flags.is_nop && (tgt_lir->opcode == kPseudoExportedPC)) { |
| 695 | cu->dex2pcMappingTable.push_back(tgt_lir->offset); |
| 696 | cu->dex2pcMappingTable.push_back(tgt_lir->dalvik_offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 697 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 698 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 699 | DCHECK(VerifyCatchEntries(cu)); |
| 700 | cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size() + |
| 701 | cu->dex2pcMappingTable.size()); |
| 702 | cu->combined_mapping_table.push_back(cu->pc2dexMappingTable.size()); |
| 703 | cu->combined_mapping_table.insert(cu->combined_mapping_table.end(), |
| 704 | cu->pc2dexMappingTable.begin(), |
| 705 | cu->pc2dexMappingTable.end()); |
| 706 | cu->combined_mapping_table.insert(cu->combined_mapping_table.end(), |
| 707 | cu->dex2pcMappingTable.begin(), |
| 708 | cu->dex2pcMappingTable.end()); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 711 | class NativePcToReferenceMapBuilder { |
| 712 | public: |
| 713 | NativePcToReferenceMapBuilder(std::vector<uint8_t>* table, |
| 714 | size_t entries, uint32_t max_native_offset, |
| 715 | size_t references_width) : entries_(entries), |
| 716 | references_width_(references_width), in_use_(entries), |
| 717 | table_(table) { |
| 718 | // Compute width in bytes needed to hold max_native_offset. |
| 719 | native_offset_width_ = 0; |
| 720 | while (max_native_offset != 0) { |
| 721 | native_offset_width_++; |
| 722 | max_native_offset >>= 8; |
| 723 | } |
| 724 | // Resize table and set up header. |
| 725 | table->resize((EntryWidth() * entries) + sizeof(uint32_t)); |
Ian Rogers | 000d724 | 2012-09-21 16:07:36 -0700 | [diff] [blame] | 726 | CHECK_LT(native_offset_width_, 1U << 3); |
| 727 | (*table)[0] = native_offset_width_ & 7; |
| 728 | CHECK_LT(references_width_, 1U << 13); |
| 729 | (*table)[0] |= (references_width_ << 3) & 0xFF; |
| 730 | (*table)[1] = (references_width_ >> 5) & 0xFF; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 731 | CHECK_LT(entries, 1U << 16); |
| 732 | (*table)[2] = entries & 0xFF; |
| 733 | (*table)[3] = (entries >> 8) & 0xFF; |
| 734 | } |
| 735 | |
| 736 | void AddEntry(uint32_t native_offset, const uint8_t* references) { |
| 737 | size_t table_index = TableIndex(native_offset); |
| 738 | while (in_use_[table_index]) { |
| 739 | table_index = (table_index + 1) % entries_; |
| 740 | } |
| 741 | in_use_[table_index] = true; |
| 742 | SetNativeOffset(table_index, native_offset); |
| 743 | DCHECK_EQ(native_offset, GetNativeOffset(table_index)); |
| 744 | SetReferences(table_index, references); |
| 745 | } |
| 746 | |
| 747 | private: |
| 748 | size_t TableIndex(uint32_t native_offset) { |
| 749 | return NativePcOffsetToReferenceMap::Hash(native_offset) % entries_; |
| 750 | } |
| 751 | |
| 752 | uint32_t GetNativeOffset(size_t table_index) { |
| 753 | uint32_t native_offset = 0; |
| 754 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 755 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 756 | native_offset |= (*table_)[table_offset + i] << (i * 8); |
| 757 | } |
| 758 | return native_offset; |
| 759 | } |
| 760 | |
| 761 | void SetNativeOffset(size_t table_index, uint32_t native_offset) { |
| 762 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 763 | for (size_t i = 0; i < native_offset_width_; i++) { |
| 764 | (*table_)[table_offset + i] = (native_offset >> (i * 8)) & 0xFF; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | void SetReferences(size_t table_index, const uint8_t* references) { |
| 769 | size_t table_offset = (table_index * EntryWidth()) + sizeof(uint32_t); |
| 770 | memcpy(&(*table_)[table_offset + native_offset_width_], references, references_width_); |
| 771 | } |
| 772 | |
| 773 | size_t EntryWidth() const { |
| 774 | return native_offset_width_ + references_width_; |
| 775 | } |
| 776 | |
| 777 | // Number of entries in the table. |
| 778 | const size_t entries_; |
| 779 | // Number of bytes used to encode the reference bitmap. |
| 780 | const size_t references_width_; |
| 781 | // Number of bytes used to encode a native offset. |
| 782 | size_t native_offset_width_; |
| 783 | // Entries that are in use. |
| 784 | std::vector<bool> in_use_; |
| 785 | // The table we're building. |
| 786 | std::vector<uint8_t>* const table_; |
| 787 | }; |
| 788 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 789 | static void CreateNativeGcMap(CompilationUnit* cu) { |
| 790 | const std::vector<uint32_t>& mapping_table = cu->pc2dexMappingTable; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 791 | uint32_t max_native_offset = 0; |
| 792 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 793 | uint32_t native_offset = mapping_table[i + 0]; |
| 794 | if (native_offset > max_native_offset) { |
| 795 | max_native_offset = native_offset; |
| 796 | } |
| 797 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 798 | Compiler::MethodReference method_ref(cu->dex_file, cu->method_idx); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 799 | const std::vector<uint8_t>* gc_map_raw = verifier::MethodVerifier::GetDexGcMap(method_ref); |
| 800 | verifier::DexPcToReferenceMap dex_gc_map(&(*gc_map_raw)[4], gc_map_raw->size() - 4); |
| 801 | // Compute native offset to references size. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 802 | NativePcToReferenceMapBuilder native_gc_map_builder(&cu->native_gc_map, |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 803 | mapping_table.size() / 2, max_native_offset, |
| 804 | dex_gc_map.RegWidth()); |
| 805 | |
| 806 | for (size_t i = 0; i < mapping_table.size(); i += 2) { |
| 807 | uint32_t native_offset = mapping_table[i + 0]; |
| 808 | uint32_t dex_pc = mapping_table[i + 1]; |
| 809 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
Bill Buzbee | a5b3024 | 2012-09-28 07:19:44 -0700 | [diff] [blame] | 810 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 811 | native_gc_map_builder.AddEntry(native_offset, references); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 815 | /* Determine the offset of each literal field */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 816 | static int AssignLiteralOffset(CompilationUnit* cu, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 817 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 818 | offset = AssignLiteralOffsetCommon(cu->literal_list, offset); |
| 819 | offset = AssignLiteralOffsetCommon(cu->code_literal_list, offset); |
| 820 | offset = AssignLiteralOffsetCommon(cu->method_literal_list, offset); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 821 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 822 | } |
| 823 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 824 | static int AssignSwitchTablesOffset(CompilationUnit* cu, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 825 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 826 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 827 | GrowableListIteratorInit(&cu->switch_tables, &iterator); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 828 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 829 | SwitchTable *tab_rec = reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator)); |
| 830 | if (tab_rec == NULL) break; |
| 831 | tab_rec->offset = offset; |
| 832 | if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 833 | offset += tab_rec->table[1] * (sizeof(int) * 2); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 834 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 835 | DCHECK_EQ(static_cast<int>(tab_rec->table[0]), |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 836 | static_cast<int>(Instruction::kPackedSwitchSignature)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 837 | offset += tab_rec->table[1] * sizeof(int); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 838 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 839 | } |
| 840 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 841 | } |
| 842 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 843 | static int AssignFillArrayDataOffset(CompilationUnit* cu, int offset) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 844 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 845 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 846 | GrowableListIteratorInit(&cu->fill_array_data, &iterator); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 847 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 848 | FillArrayData *tab_rec = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 849 | reinterpret_cast<FillArrayData*>(GrowableListIteratorNext(&iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 850 | if (tab_rec == NULL) break; |
| 851 | tab_rec->offset = offset; |
| 852 | offset += tab_rec->size; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 853 | // word align |
| 854 | offset = (offset + 3) & ~3; |
| 855 | } |
| 856 | return offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 857 | } |
| 858 | |
buzbee | a3a82b2 | 2012-11-27 16:09:55 -0800 | [diff] [blame] | 859 | // LIR offset assignment. |
| 860 | static int AssignInsnOffsets(CompilationUnit* cu) |
| 861 | { |
| 862 | LIR* lir; |
| 863 | int offset = 0; |
| 864 | |
| 865 | for (lir = cu->first_lir_insn; lir != NULL; lir = NEXT_LIR(lir)) { |
| 866 | lir->offset = offset; |
| 867 | if (lir->opcode >= 0) { |
| 868 | if (!lir->flags.is_nop) { |
| 869 | offset += lir->flags.size; |
| 870 | } |
| 871 | } else if (lir->opcode == kPseudoPseudoAlign4) { |
| 872 | if (offset & 0x2) { |
| 873 | offset += 2; |
| 874 | lir->operands[0] = 1; |
| 875 | } else { |
| 876 | lir->operands[0] = 0; |
| 877 | } |
| 878 | } |
| 879 | /* Pseudo opcodes don't consume space */ |
| 880 | } |
| 881 | |
| 882 | return offset; |
| 883 | } |
| 884 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 885 | /* |
| 886 | * Walk the compilation unit and assign offsets to instructions |
| 887 | * and literals and compute the total size of the compiled unit. |
| 888 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 889 | static void AssignOffsets(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 890 | { |
buzbee | a3a82b2 | 2012-11-27 16:09:55 -0800 | [diff] [blame] | 891 | int offset = AssignInsnOffsets(cu); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 892 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 893 | /* Const values have to be word aligned */ |
| 894 | offset = (offset + 3) & ~3; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 895 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 896 | /* Set up offsets for literals */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 897 | cu->data_offset = offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 898 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 899 | offset = AssignLiteralOffset(cu, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 900 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 901 | offset = AssignSwitchTablesOffset(cu, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 902 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 903 | offset = AssignFillArrayDataOffset(cu, offset); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 904 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 905 | cu->total_size = offset; |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Go over each instruction in the list and calculate the offset from the top |
| 910 | * before sending them off to the assembler. If out-of-range branch distance is |
| 911 | * seen rearrange the instructions a bit to correct it. |
| 912 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 913 | void AssembleLIR(CompilationUnit* cu) |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 914 | { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 915 | Codegen* cg = cu->cg.get(); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 916 | AssignOffsets(cu); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 917 | /* |
| 918 | * Assemble here. Note that we generate code with optimistic assumptions |
| 919 | * and if found now to work, we'll have to redo the sequence and retry. |
| 920 | */ |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 921 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 922 | while (true) { |
buzbee | 02031b1 | 2012-11-23 09:41:35 -0800 | [diff] [blame] | 923 | AssemblerStatus res = cg->AssembleInstructions(cu, 0); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 924 | if (res == kSuccess) { |
| 925 | break; |
| 926 | } else { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 927 | cu->assembler_retries++; |
| 928 | if (cu->assembler_retries > MAX_ASSEMBLER_RETRIES) { |
| 929 | CodegenDump(cu); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 930 | LOG(FATAL) << "Assembler error - too many retries"; |
| 931 | } |
| 932 | // Redo offsets and try again |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 933 | AssignOffsets(cu); |
| 934 | cu->code_buffer.clear(); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 935 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 936 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 937 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 938 | // Install literals |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 939 | InstallLiteralPools(cu); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 940 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 941 | // Install switch tables |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 942 | InstallSwitchTables(cu); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 943 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 944 | // Install fill array data |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 945 | InstallFillArrayData(cu); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 946 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 947 | // Create the mapping table and native offset to reference map. |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 948 | CreateMappingTables(cu); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 949 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 950 | CreateNativeGcMap(cu); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 951 | } |
| 952 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 953 | /* |
| 954 | * Insert a kPseudoCaseLabel at the beginning of the Dalvik |
| 955 | * offset vaddr. This label will be used to fix up the case |
| 956 | * branch table during the assembly phase. Be sure to set |
| 957 | * all resource flags on this to prevent code motion across |
| 958 | * target boundaries. KeyVal is just there for debugging. |
| 959 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 960 | static LIR* InsertCaseLabel(CompilationUnit* cu, int vaddr, int keyVal) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 961 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 962 | SafeMap<unsigned int, LIR*>::iterator it; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 963 | it = cu->boundary_map.find(vaddr); |
| 964 | if (it == cu->boundary_map.end()) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 965 | LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr; |
| 966 | } |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 967 | LIR* new_label = static_cast<LIR*>(NewMem(cu, sizeof(LIR), true, kAllocLIR)); |
| 968 | new_label->dalvik_offset = vaddr; |
| 969 | new_label->opcode = kPseudoCaseLabel; |
| 970 | new_label->operands[0] = keyVal; |
| 971 | InsertLIRAfter(it->second, new_label); |
| 972 | return new_label; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 973 | } |
| 974 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 975 | static void MarkPackedCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 976 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 977 | const uint16_t* table = tab_rec->table; |
| 978 | int base_vaddr = tab_rec->vaddr; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 979 | const int *targets = reinterpret_cast<const int*>(&table[4]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 980 | int entries = table[1]; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 981 | int low_key = s4FromSwitchData(&table[2]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 982 | for (int i = 0; i < entries; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 983 | tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], i + low_key); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 984 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 985 | } |
| 986 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 987 | static void MarkSparseCaseLabels(CompilationUnit* cu, SwitchTable *tab_rec) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 988 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 989 | const uint16_t* table = tab_rec->table; |
| 990 | int base_vaddr = tab_rec->vaddr; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 991 | int entries = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 992 | const int* keys = reinterpret_cast<const int*>(&table[2]); |
| 993 | const int* targets = &keys[entries]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 994 | for (int i = 0; i < entries; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 995 | tab_rec->targets[i] = InsertCaseLabel(cu, base_vaddr + targets[i], keys[i]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 996 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 997 | } |
| 998 | |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 999 | void ProcessSwitchTables(CompilationUnit* cu) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1000 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1001 | GrowableListIterator iterator; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1002 | GrowableListIteratorInit(&cu->switch_tables, &iterator); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1003 | while (true) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1004 | SwitchTable *tab_rec = |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 1005 | reinterpret_cast<SwitchTable*>(GrowableListIteratorNext(&iterator)); |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1006 | if (tab_rec == NULL) break; |
| 1007 | if (tab_rec->table[0] == Instruction::kPackedSwitchSignature) { |
| 1008 | MarkPackedCaseLabels(cu, tab_rec); |
| 1009 | } else if (tab_rec->table[0] == Instruction::kSparseSwitchSignature) { |
| 1010 | MarkSparseCaseLabels(cu, tab_rec); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1011 | } else { |
| 1012 | LOG(FATAL) << "Invalid switch table"; |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1013 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1014 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 1017 | void DumpSparseSwitchTable(const uint16_t* table) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * Sparse switch data format: |
| 1020 | * ushort ident = 0x0200 magic value |
| 1021 | * ushort size number of entries in the table; > 0 |
| 1022 | * int keys[size] keys, sorted low-to-high; 32-bit aligned |
| 1023 | * int targets[size] branch targets, relative to switch opcode |
| 1024 | * |
| 1025 | * Total size is (2+size*4) 16-bit code units. |
| 1026 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1027 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 1028 | uint16_t ident = table[0]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1029 | int entries = table[1]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 1030 | const int* keys = reinterpret_cast<const int*>(&table[2]); |
| 1031 | const int* targets = &keys[entries]; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1032 | LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident |
| 1033 | << ", entries: " << std::dec << entries; |
| 1034 | for (int i = 0; i < entries; i++) { |
| 1035 | LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i]; |
| 1036 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
buzbee | 52a77fc | 2012-11-20 19:50:46 -0800 | [diff] [blame] | 1039 | void DumpPackedSwitchTable(const uint16_t* table) |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1040 | /* |
| 1041 | * Packed switch data format: |
| 1042 | * ushort ident = 0x0100 magic value |
| 1043 | * ushort size number of entries in the table |
| 1044 | * int first_key first (and lowest) switch case value |
| 1045 | * int targets[size] branch targets, relative to switch opcode |
| 1046 | * |
| 1047 | * Total size is (4+size*2) 16-bit code units. |
| 1048 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1049 | { |
buzbee | eaf09bc | 2012-11-15 14:51:41 -0800 | [diff] [blame] | 1050 | uint16_t ident = table[0]; |
buzbee | cbd6d44 | 2012-11-17 14:11:25 -0800 | [diff] [blame] | 1051 | const int* targets = reinterpret_cast<const int*>(&table[4]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1052 | int entries = table[1]; |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1053 | int low_key = s4FromSwitchData(&table[2]); |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1054 | LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1055 | << ", entries: " << std::dec << entries << ", low_key: " << low_key; |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1056 | for (int i = 0; i < entries; i++) { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1057 | LOG(INFO) << " Key[" << (i + low_key) << "] -> 0x" << std::hex |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 1058 | << targets[i]; |
| 1059 | } |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 1060 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1061 | |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1062 | /* |
| 1063 | * Set up special LIR to mark a Dalvik byte-code instruction start and |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1064 | * record it in the boundary_map. NOTE: in cases such as kMirOpCheck in |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1065 | * which we split a single Dalvik instruction, only the first MIR op |
| 1066 | * associated with a Dalvik PC should be entered into the map. |
| 1067 | */ |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1068 | LIR* MarkBoundary(CompilationUnit* cu, int offset, const char* inst_str) |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1069 | { |
buzbee | fa57c47 | 2012-11-21 12:06:18 -0800 | [diff] [blame] | 1070 | LIR* res = NewLIR1(cu, kPseudoDalvikByteCodeBoundary, reinterpret_cast<uintptr_t>(inst_str)); |
| 1071 | if (cu->boundary_map.find(offset) == cu->boundary_map.end()) { |
| 1072 | cu->boundary_map.Put(offset, res); |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 1073 | } |
| 1074 | return res; |
| 1075 | } |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 1076 | |
buzbee | e6285f9 | 2012-12-06 15:57:46 -0800 | [diff] [blame] | 1077 | bool EvaluateBranch(Instruction::Code opcode, int32_t src1, int32_t src2) |
| 1078 | { |
| 1079 | bool is_taken; |
| 1080 | switch (opcode) { |
| 1081 | case Instruction::IF_EQ: is_taken = (src1 == src2); break; |
| 1082 | case Instruction::IF_NE: is_taken = (src1 != src2); break; |
| 1083 | case Instruction::IF_LT: is_taken = (src1 < src2); break; |
| 1084 | case Instruction::IF_GE: is_taken = (src1 >= src2); break; |
| 1085 | case Instruction::IF_GT: is_taken = (src1 > src2); break; |
| 1086 | case Instruction::IF_LE: is_taken = (src1 <= src2); break; |
| 1087 | case Instruction::IF_EQZ: is_taken = (src1 == 0); break; |
| 1088 | case Instruction::IF_NEZ: is_taken = (src1 != 0); break; |
| 1089 | case Instruction::IF_LTZ: is_taken = (src1 < 0); break; |
| 1090 | case Instruction::IF_GEZ: is_taken = (src1 >= 0); break; |
| 1091 | case Instruction::IF_GTZ: is_taken = (src1 > 0); break; |
| 1092 | case Instruction::IF_LEZ: is_taken = (src1 <= 0); break; |
| 1093 | default: |
| 1094 | LOG(FATAL) << "Unexpected opcode " << opcode; |
| 1095 | is_taken = false; |
| 1096 | } |
| 1097 | return is_taken; |
| 1098 | } |
| 1099 | |
buzbee | a3a82b2 | 2012-11-27 16:09:55 -0800 | [diff] [blame] | 1100 | } // namespace art |