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