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