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