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