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