Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 2 | * Copyright (C) 2014 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 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 17 | #include "builder.h" |
| 18 | |
| 19 | #include "class_linker.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 20 | #include "dex_file.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "dex_instruction.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 23 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "mirror/art_field.h" |
| 26 | #include "mirror/art_field-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 29 | #include "nodes.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 30 | #include "primitive.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 31 | #include "scoped_thread_state_change.h" |
| 32 | #include "thread.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 36 | /** |
| 37 | * Helper class to add HTemporary instructions. This class is used when |
| 38 | * converting a DEX instruction to multiple HInstruction, and where those |
| 39 | * instructions do not die at the following instruction, but instead spans |
| 40 | * multiple instructions. |
| 41 | */ |
| 42 | class Temporaries : public ValueObject { |
| 43 | public: |
| 44 | Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) { |
| 45 | graph_->UpdateNumberOfTemporaries(count_); |
| 46 | } |
| 47 | |
| 48 | void Add(HInstruction* instruction) { |
| 49 | // We currently only support vreg size temps. |
| 50 | DCHECK(instruction->GetType() != Primitive::kPrimLong |
| 51 | && instruction->GetType() != Primitive::kPrimDouble); |
| 52 | HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++); |
| 53 | instruction->GetBlock()->AddInstruction(temp); |
| 54 | DCHECK(temp->GetPrevious() == instruction); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | HGraph* const graph_; |
| 59 | |
| 60 | // The total number of temporaries that will be used. |
| 61 | const size_t count_; |
| 62 | |
| 63 | // Current index in the temporary stack, updated by `Add`. |
| 64 | size_t index_; |
| 65 | }; |
| 66 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 67 | static bool IsTypeSupported(Primitive::Type type) { |
| 68 | return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble; |
| 69 | } |
| 70 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 71 | void HGraphBuilder::InitializeLocals(uint16_t count) { |
| 72 | graph_->SetNumberOfVRegs(count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 73 | locals_.SetSize(count); |
| 74 | for (int i = 0; i < count; i++) { |
| 75 | HLocal* local = new (arena_) HLocal(i); |
| 76 | entry_block_->AddInstruction(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 77 | locals_.Put(i, local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 81 | bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) { |
| 82 | // dex_compilation_unit_ is null only when unit testing. |
| 83 | if (dex_compilation_unit_ == nullptr) { |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | graph_->SetNumberOfInVRegs(number_of_parameters); |
| 88 | const char* shorty = dex_compilation_unit_->GetShorty(); |
| 89 | int locals_index = locals_.Size() - number_of_parameters; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 90 | int parameter_index = 0; |
| 91 | |
| 92 | if (!dex_compilation_unit_->IsStatic()) { |
| 93 | // Add the implicit 'this' argument, not expressed in the signature. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 94 | HParameterValue* parameter = |
| 95 | new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 96 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 97 | HLocal* local = GetLocalAt(locals_index++); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 98 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 99 | number_of_parameters--; |
| 100 | } |
| 101 | |
| 102 | uint32_t pos = 1; |
| 103 | for (int i = 0; i < number_of_parameters; i++) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 104 | HParameterValue* parameter = |
| 105 | new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++])); |
| 106 | entry_block_->AddInstruction(parameter); |
| 107 | HLocal* local = GetLocalAt(locals_index++); |
| 108 | // Store the parameter value in the local that the dex code will use |
| 109 | // to reference that parameter. |
| 110 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
| 111 | bool is_wide = (parameter->GetType() == Primitive::kPrimLong) |
| 112 | || (parameter->GetType() == Primitive::kPrimDouble); |
| 113 | if (is_wide) { |
| 114 | i++; |
| 115 | locals_index++; |
| 116 | parameter_index++; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 122 | static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 123 | if (code_item.tries_size_ > 0) { |
| 124 | return false; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 125 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 129 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 130 | void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 131 | int32_t target_offset = instruction.GetTargetOffset(); |
| 132 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 133 | HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 134 | HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 135 | T* comparison = new (arena_) T(first, second); |
| 136 | current_block_->AddInstruction(comparison); |
| 137 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 138 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 139 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 140 | DCHECK(target != nullptr); |
| 141 | current_block_->AddSuccessor(target); |
| 142 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 143 | DCHECK(target != nullptr); |
| 144 | current_block_->AddSuccessor(target); |
| 145 | current_block_ = nullptr; |
| 146 | } |
| 147 | |
| 148 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 149 | void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 150 | int32_t target_offset = instruction.GetTargetOffset(); |
| 151 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 152 | HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 153 | T* comparison = new (arena_) T(value, GetIntConstant(0)); |
| 154 | current_block_->AddInstruction(comparison); |
| 155 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 156 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 157 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 158 | DCHECK(target != nullptr); |
| 159 | current_block_->AddSuccessor(target); |
| 160 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 161 | DCHECK(target != nullptr); |
| 162 | current_block_->AddSuccessor(target); |
| 163 | current_block_ = nullptr; |
| 164 | } |
| 165 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 166 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 167 | if (!CanHandleCodeItem(code_item)) { |
| 168 | return nullptr; |
| 169 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 170 | |
| 171 | const uint16_t* code_ptr = code_item.insns_; |
| 172 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 173 | code_start_ = code_ptr; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 174 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 175 | // Setup the graph with the entry block and exit block. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 176 | graph_ = new (arena_) HGraph(arena_); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 177 | entry_block_ = new (arena_) HBasicBlock(graph_, 0); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 178 | graph_->AddBlock(entry_block_); |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 179 | exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 180 | graph_->SetEntryBlock(entry_block_); |
| 181 | graph_->SetExitBlock(exit_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 182 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 183 | InitializeLocals(code_item.registers_size_); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 184 | graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 185 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 186 | // To avoid splitting blocks, we compute ahead of time the instructions that |
| 187 | // start a new block, and create these blocks. |
| 188 | ComputeBranchTargets(code_ptr, code_end); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 189 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 190 | if (!InitializeParameters(code_item.ins_size_)) { |
| 191 | return nullptr; |
| 192 | } |
| 193 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 194 | size_t dex_offset = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 195 | while (code_ptr < code_end) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 196 | // Update the current block if dex_offset starts a new block. |
| 197 | MaybeUpdateCurrentBlock(dex_offset); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 198 | const Instruction& instruction = *Instruction::At(code_ptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 199 | if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr; |
| 200 | dex_offset += instruction.SizeInCodeUnits(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 201 | code_ptr += instruction.SizeInCodeUnits(); |
| 202 | } |
| 203 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 204 | // Add the exit block at the end to give it the highest id. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 205 | graph_->AddBlock(exit_block_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 206 | exit_block_->AddInstruction(new (arena_) HExit()); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 207 | // Add the suspend check to the entry block. |
| 208 | entry_block_->AddInstruction(new (arena_) HSuspendCheck(0)); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 209 | entry_block_->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 210 | return graph_; |
| 211 | } |
| 212 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 213 | void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { |
| 214 | HBasicBlock* block = FindBlockStartingAt(index); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 215 | if (block == nullptr) { |
| 216 | return; |
| 217 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 218 | |
| 219 | if (current_block_ != nullptr) { |
| 220 | // Branching instructions clear current_block, so we know |
| 221 | // the last instruction of the current block is not a branching |
| 222 | // instruction. We add an unconditional goto to the found block. |
| 223 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 224 | current_block_->AddSuccessor(block); |
| 225 | } |
| 226 | graph_->AddBlock(block); |
| 227 | current_block_ = block; |
| 228 | } |
| 229 | |
| 230 | void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) { |
| 231 | // TODO: Support switch instructions. |
| 232 | branch_targets_.SetSize(code_end - code_ptr); |
| 233 | |
| 234 | // Create the first block for the dex instructions, single successor of the entry block. |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 235 | HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 236 | branch_targets_.Put(0, block); |
| 237 | entry_block_->AddSuccessor(block); |
| 238 | |
| 239 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 240 | // the locations these instructions branch to. |
| 241 | size_t dex_offset = 0; |
| 242 | while (code_ptr < code_end) { |
| 243 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 244 | if (instruction.IsBranch()) { |
| 245 | int32_t target = instruction.GetTargetOffset() + dex_offset; |
| 246 | // Create a block for the target instruction. |
| 247 | if (FindBlockStartingAt(target) == nullptr) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 248 | block = new (arena_) HBasicBlock(graph_, target); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 249 | branch_targets_.Put(target, block); |
| 250 | } |
| 251 | dex_offset += instruction.SizeInCodeUnits(); |
| 252 | code_ptr += instruction.SizeInCodeUnits(); |
| 253 | if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) { |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 254 | block = new (arena_) HBasicBlock(graph_, dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 255 | branch_targets_.Put(dex_offset, block); |
| 256 | } |
| 257 | } else { |
| 258 | code_ptr += instruction.SizeInCodeUnits(); |
| 259 | dex_offset += instruction.SizeInCodeUnits(); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { |
| 265 | DCHECK_GE(index, 0); |
| 266 | return branch_targets_.Get(index); |
| 267 | } |
| 268 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 269 | template<typename T> |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 270 | void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) { |
| 271 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 272 | current_block_->AddInstruction(new (arena_) T(type, first)); |
| 273 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 274 | } |
| 275 | |
| 276 | template<typename T> |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 277 | void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 278 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 279 | HInstruction* second = LoadLocal(instruction.VRegC(), type); |
| 280 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 281 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 282 | } |
| 283 | |
| 284 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 285 | void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) { |
| 286 | HInstruction* first = LoadLocal(instruction.VRegA(), type); |
| 287 | HInstruction* second = LoadLocal(instruction.VRegB(), type); |
| 288 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 289 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 290 | } |
| 291 | |
| 292 | template<typename T> |
| 293 | void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 294 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 295 | HInstruction* second = GetIntConstant(instruction.VRegC_22s()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 296 | if (reverse) { |
| 297 | std::swap(first, second); |
| 298 | } |
| 299 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 300 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 301 | } |
| 302 | |
| 303 | template<typename T> |
| 304 | void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 305 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 306 | HInstruction* second = GetIntConstant(instruction.VRegC_22b()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 307 | if (reverse) { |
| 308 | std::swap(first, second); |
| 309 | } |
| 310 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 311 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 312 | } |
| 313 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 314 | void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) { |
| 315 | if (type == Primitive::kPrimVoid) { |
| 316 | current_block_->AddInstruction(new (arena_) HReturnVoid()); |
| 317 | } else { |
| 318 | HInstruction* value = LoadLocal(instruction.VRegA(), type); |
| 319 | current_block_->AddInstruction(new (arena_) HReturn(value)); |
| 320 | } |
| 321 | current_block_->AddSuccessor(exit_block_); |
| 322 | current_block_ = nullptr; |
| 323 | } |
| 324 | |
| 325 | bool HGraphBuilder::BuildInvoke(const Instruction& instruction, |
| 326 | uint32_t dex_offset, |
| 327 | uint32_t method_idx, |
| 328 | uint32_t number_of_vreg_arguments, |
| 329 | bool is_range, |
| 330 | uint32_t* args, |
| 331 | uint32_t register_index) { |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 332 | Instruction::Code opcode = instruction.Opcode(); |
| 333 | InvokeType invoke_type; |
| 334 | switch (opcode) { |
| 335 | case Instruction::INVOKE_STATIC: |
| 336 | case Instruction::INVOKE_STATIC_RANGE: |
| 337 | invoke_type = kStatic; |
| 338 | break; |
| 339 | case Instruction::INVOKE_DIRECT: |
| 340 | case Instruction::INVOKE_DIRECT_RANGE: |
| 341 | invoke_type = kDirect; |
| 342 | break; |
| 343 | case Instruction::INVOKE_VIRTUAL: |
| 344 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 345 | invoke_type = kVirtual; |
| 346 | break; |
| 347 | case Instruction::INVOKE_INTERFACE: |
| 348 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 349 | invoke_type = kInterface; |
| 350 | break; |
| 351 | case Instruction::INVOKE_SUPER_RANGE: |
| 352 | case Instruction::INVOKE_SUPER: |
| 353 | invoke_type = kSuper; |
| 354 | break; |
| 355 | default: |
| 356 | LOG(FATAL) << "Unexpected invoke op: " << opcode; |
| 357 | return false; |
| 358 | } |
| 359 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 360 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 361 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_); |
| 362 | const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_); |
| 363 | Primitive::Type return_type = Primitive::GetType(descriptor[0]); |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 364 | bool is_instance_call = invoke_type != kStatic; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 365 | const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1); |
| 366 | |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 367 | HInvoke* invoke = nullptr; |
| 368 | if (invoke_type == kVirtual) { |
| 369 | MethodReference target_method(dex_file_, method_idx); |
| 370 | uintptr_t direct_code; |
| 371 | uintptr_t direct_method; |
| 372 | int vtable_index; |
| 373 | // TODO: Add devirtualization support. |
| 374 | compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true, |
| 375 | &invoke_type, &target_method, &vtable_index, |
| 376 | &direct_code, &direct_method); |
| 377 | if (vtable_index == -1) { |
| 378 | return false; |
| 379 | } |
| 380 | invoke = new (arena_) HInvokeVirtual( |
| 381 | arena_, number_of_arguments, return_type, dex_offset, vtable_index); |
| 382 | } else { |
| 383 | // Treat invoke-direct like static calls for now. |
| 384 | invoke = new (arena_) HInvokeStatic( |
| 385 | arena_, number_of_arguments, return_type, dex_offset, method_idx); |
| 386 | } |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 387 | |
| 388 | size_t start_index = 0; |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 389 | Temporaries temps(graph_, is_instance_call ? 1 : 0); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 390 | if (is_instance_call) { |
| 391 | HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot); |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 392 | HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset); |
| 393 | current_block_->AddInstruction(null_check); |
| 394 | temps.Add(null_check); |
| 395 | invoke->SetArgumentAt(0, null_check); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 396 | start_index = 1; |
| 397 | } |
| 398 | |
| 399 | uint32_t descriptor_index = 1; |
| 400 | uint32_t argument_index = start_index; |
| 401 | for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) { |
| 402 | Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 403 | bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); |
| 404 | if (!is_range && is_wide && args[i] + 1 != args[i + 1]) { |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 405 | LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol() |
| 406 | << " at " << dex_offset; |
| 407 | // We do not implement non sequential register pair. |
| 408 | return false; |
| 409 | } |
| 410 | HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type); |
| 411 | invoke->SetArgumentAt(argument_index, arg); |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 412 | if (is_wide) { |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 413 | i++; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | DCHECK_EQ(argument_index, number_of_arguments); |
| 418 | current_block_->AddInstruction(invoke); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 419 | latest_result_ = invoke; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 420 | return true; |
| 421 | } |
| 422 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 423 | bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction, |
| 424 | uint32_t dex_offset, |
| 425 | bool is_put) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 426 | uint32_t source_or_dest_reg = instruction.VRegA_22c(); |
| 427 | uint32_t obj_reg = instruction.VRegB_22c(); |
| 428 | uint16_t field_index = instruction.VRegC_22c(); |
| 429 | |
| 430 | ScopedObjectAccess soa(Thread::Current()); |
| 431 | StackHandleScope<1> hs(soa.Self()); |
| 432 | Handle<mirror::ArtField> resolved_field(hs.NewHandle( |
| 433 | compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa))); |
| 434 | |
| 435 | if (resolved_field.Get() == nullptr) { |
| 436 | return false; |
| 437 | } |
| 438 | if (resolved_field->IsVolatile()) { |
| 439 | return false; |
| 440 | } |
| 441 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 442 | Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); |
| 443 | if (!IsTypeSupported(field_type)) { |
| 444 | return false; |
| 445 | } |
| 446 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 447 | HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot); |
| 448 | current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset)); |
| 449 | if (is_put) { |
| 450 | Temporaries temps(graph_, 1); |
| 451 | HInstruction* null_check = current_block_->GetLastInstruction(); |
| 452 | // We need one temporary for the null check. |
| 453 | temps.Add(null_check); |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 454 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 455 | current_block_->AddInstruction(new (arena_) HInstanceFieldSet( |
| 456 | null_check, |
| 457 | value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 458 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 459 | resolved_field->GetOffset())); |
| 460 | } else { |
| 461 | current_block_->AddInstruction(new (arena_) HInstanceFieldGet( |
| 462 | current_block_->GetLastInstruction(), |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 463 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 464 | resolved_field->GetOffset())); |
| 465 | |
| 466 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 471 | |
| 472 | |
| 473 | bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction, |
| 474 | uint32_t dex_offset, |
| 475 | bool is_put) { |
| 476 | uint32_t source_or_dest_reg = instruction.VRegA_21c(); |
| 477 | uint16_t field_index = instruction.VRegB_21c(); |
| 478 | |
| 479 | uint32_t storage_index; |
| 480 | bool is_referrers_class; |
| 481 | bool is_initialized; |
| 482 | bool is_volatile; |
| 483 | MemberOffset field_offset(0u); |
| 484 | Primitive::Type field_type; |
| 485 | |
| 486 | bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index, |
| 487 | dex_compilation_unit_, |
| 488 | is_put, |
| 489 | &field_offset, |
| 490 | &storage_index, |
| 491 | &is_referrers_class, |
| 492 | &is_volatile, |
| 493 | &is_initialized, |
| 494 | &field_type); |
| 495 | if (!fast_path) { |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | if (is_volatile) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | if (!IsTypeSupported(field_type)) { |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | HLoadClass* constant = new (arena_) HLoadClass( |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 508 | storage_index, is_referrers_class, dex_offset); |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 509 | current_block_->AddInstruction(constant); |
| 510 | |
| 511 | HInstruction* cls = constant; |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 512 | if (!is_initialized) { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 513 | cls = new (arena_) HClinitCheck(constant, dex_offset); |
| 514 | current_block_->AddInstruction(cls); |
| 515 | } |
| 516 | |
| 517 | if (is_put) { |
| 518 | // We need to keep the class alive before loading the value. |
| 519 | Temporaries temps(graph_, 1); |
| 520 | temps.Add(cls); |
| 521 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
| 522 | DCHECK_EQ(value->GetType(), field_type); |
| 523 | current_block_->AddInstruction( |
| 524 | new (arena_) HStaticFieldSet(cls, value, field_type, field_offset)); |
| 525 | } else { |
| 526 | current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset)); |
| 527 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 528 | } |
| 529 | return true; |
| 530 | } |
| 531 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 532 | void HGraphBuilder::BuildArrayAccess(const Instruction& instruction, |
| 533 | uint32_t dex_offset, |
| 534 | bool is_put, |
| 535 | Primitive::Type anticipated_type) { |
| 536 | uint8_t source_or_dest_reg = instruction.VRegA_23x(); |
| 537 | uint8_t array_reg = instruction.VRegB_23x(); |
| 538 | uint8_t index_reg = instruction.VRegC_23x(); |
| 539 | |
| 540 | DCHECK(IsTypeSupported(anticipated_type)); |
| 541 | |
| 542 | // We need one temporary for the null check, one for the index, and one for the length. |
| 543 | Temporaries temps(graph_, 3); |
| 544 | |
| 545 | HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot); |
| 546 | object = new (arena_) HNullCheck(object, dex_offset); |
| 547 | current_block_->AddInstruction(object); |
| 548 | temps.Add(object); |
| 549 | |
| 550 | HInstruction* length = new (arena_) HArrayLength(object); |
| 551 | current_block_->AddInstruction(length); |
| 552 | temps.Add(length); |
| 553 | HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt); |
| 554 | index = new (arena_) HBoundsCheck(index, length, dex_offset); |
| 555 | current_block_->AddInstruction(index); |
| 556 | temps.Add(index); |
| 557 | if (is_put) { |
| 558 | HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type); |
| 559 | // TODO: Insert a type check node if the type is Object. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 560 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 561 | object, index, value, anticipated_type, dex_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 562 | } else { |
| 563 | current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type)); |
| 564 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 565 | } |
| 566 | } |
| 567 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 568 | void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset, |
| 569 | uint32_t type_index, |
| 570 | uint32_t number_of_vreg_arguments, |
| 571 | bool is_range, |
| 572 | uint32_t* args, |
| 573 | uint32_t register_index) { |
| 574 | HInstruction* length = GetIntConstant(number_of_vreg_arguments); |
| 575 | HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index); |
| 576 | current_block_->AddInstruction(object); |
| 577 | |
| 578 | const char* descriptor = dex_file_->StringByTypeIdx(type_index); |
| 579 | DCHECK_EQ(descriptor[0], '[') << descriptor; |
| 580 | char primitive = descriptor[1]; |
| 581 | DCHECK(primitive == 'I' |
| 582 | || primitive == 'L' |
| 583 | || primitive == '[') << descriptor; |
| 584 | bool is_reference_array = (primitive == 'L') || (primitive == '['); |
| 585 | Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt; |
| 586 | |
| 587 | Temporaries temps(graph_, 1); |
| 588 | temps.Add(object); |
| 589 | for (size_t i = 0; i < number_of_vreg_arguments; ++i) { |
| 590 | HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type); |
| 591 | HInstruction* index = GetIntConstant(i); |
| 592 | current_block_->AddInstruction( |
| 593 | new (arena_) HArraySet(object, index, value, type, dex_offset)); |
| 594 | } |
| 595 | latest_result_ = object; |
| 596 | } |
| 597 | |
| 598 | template <typename T> |
| 599 | void HGraphBuilder::BuildFillArrayData(HInstruction* object, |
| 600 | const T* data, |
| 601 | uint32_t element_count, |
| 602 | Primitive::Type anticipated_type, |
| 603 | uint32_t dex_offset) { |
| 604 | for (uint32_t i = 0; i < element_count; ++i) { |
| 605 | HInstruction* index = GetIntConstant(i); |
| 606 | HInstruction* value = GetIntConstant(data[i]); |
| 607 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 608 | object, index, value, anticipated_type, dex_offset)); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | void HGraphBuilder::BuildFillWideArrayData(HInstruction* object, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 613 | const int64_t* data, |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 614 | uint32_t element_count, |
| 615 | uint32_t dex_offset) { |
| 616 | for (uint32_t i = 0; i < element_count; ++i) { |
| 617 | HInstruction* index = GetIntConstant(i); |
| 618 | HInstruction* value = GetLongConstant(data[i]); |
| 619 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 620 | object, index, value, Primitive::kPrimLong, dex_offset)); |
| 621 | } |
| 622 | } |
| 623 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 624 | void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) { |
| 625 | if (target_offset <= 0) { |
| 626 | // Unconditionnally add a suspend check to backward branches. We can remove |
| 627 | // them after we recognize loops in the graph. |
| 628 | current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset)); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 633 | if (current_block_ == nullptr) { |
| 634 | return true; // Dead code |
| 635 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 636 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 637 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 638 | case Instruction::CONST_4: { |
| 639 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 640 | HIntConstant* constant = GetIntConstant(instruction.VRegB_11n()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 641 | UpdateLocal(register_index, constant); |
| 642 | break; |
| 643 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 644 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 645 | case Instruction::CONST_16: { |
| 646 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 647 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21s()); |
| 648 | UpdateLocal(register_index, constant); |
| 649 | break; |
| 650 | } |
| 651 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 652 | case Instruction::CONST: { |
| 653 | int32_t register_index = instruction.VRegA(); |
| 654 | HIntConstant* constant = GetIntConstant(instruction.VRegB_31i()); |
| 655 | UpdateLocal(register_index, constant); |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | case Instruction::CONST_HIGH16: { |
| 660 | int32_t register_index = instruction.VRegA(); |
| 661 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16); |
| 662 | UpdateLocal(register_index, constant); |
| 663 | break; |
| 664 | } |
| 665 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 666 | case Instruction::CONST_WIDE_16: { |
| 667 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 668 | // Get 16 bits of constant value, sign extended to 64 bits. |
| 669 | int64_t value = instruction.VRegB_21s(); |
| 670 | value <<= 48; |
| 671 | value >>= 48; |
| 672 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 673 | UpdateLocal(register_index, constant); |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | case Instruction::CONST_WIDE_32: { |
| 678 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 679 | // Get 32 bits of constant value, sign extended to 64 bits. |
| 680 | int64_t value = instruction.VRegB_31i(); |
| 681 | value <<= 32; |
| 682 | value >>= 32; |
| 683 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 684 | UpdateLocal(register_index, constant); |
| 685 | break; |
| 686 | } |
| 687 | |
| 688 | case Instruction::CONST_WIDE: { |
| 689 | int32_t register_index = instruction.VRegA(); |
| 690 | HLongConstant* constant = GetLongConstant(instruction.VRegB_51l()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 691 | UpdateLocal(register_index, constant); |
| 692 | break; |
| 693 | } |
| 694 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 695 | case Instruction::CONST_WIDE_HIGH16: { |
| 696 | int32_t register_index = instruction.VRegA(); |
| 697 | int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48; |
| 698 | HLongConstant* constant = GetLongConstant(value); |
| 699 | UpdateLocal(register_index, constant); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | // TODO: these instructions are also used to move floating point values, so what is |
| 704 | // the type (int or float)? |
| 705 | case Instruction::MOVE: |
| 706 | case Instruction::MOVE_FROM16: |
| 707 | case Instruction::MOVE_16: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 708 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 709 | UpdateLocal(instruction.VRegA(), value); |
| 710 | break; |
| 711 | } |
| 712 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 713 | // TODO: these instructions are also used to move floating point values, so what is |
| 714 | // the type (long or double)? |
| 715 | case Instruction::MOVE_WIDE: |
| 716 | case Instruction::MOVE_WIDE_FROM16: |
| 717 | case Instruction::MOVE_WIDE_16: { |
| 718 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong); |
| 719 | UpdateLocal(instruction.VRegA(), value); |
| 720 | break; |
| 721 | } |
| 722 | |
| 723 | case Instruction::MOVE_OBJECT: |
| 724 | case Instruction::MOVE_OBJECT_16: |
| 725 | case Instruction::MOVE_OBJECT_FROM16: { |
| 726 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot); |
| 727 | UpdateLocal(instruction.VRegA(), value); |
| 728 | break; |
| 729 | } |
| 730 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 731 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 732 | BuildReturn(instruction, Primitive::kPrimVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 733 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 736 | #define IF_XX(comparison, cond) \ |
| 737 | case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \ |
| 738 | case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 739 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 740 | IF_XX(HEqual, EQ); |
| 741 | IF_XX(HNotEqual, NE); |
| 742 | IF_XX(HLessThan, LT); |
| 743 | IF_XX(HLessThanOrEqual, LE); |
| 744 | IF_XX(HGreaterThan, GT); |
| 745 | IF_XX(HGreaterThanOrEqual, GE); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 746 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 747 | case Instruction::GOTO: |
| 748 | case Instruction::GOTO_16: |
| 749 | case Instruction::GOTO_32: { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 750 | int32_t offset = instruction.GetTargetOffset(); |
| 751 | PotentiallyAddSuspendCheck(offset, dex_offset); |
| 752 | HBasicBlock* target = FindBlockStartingAt(offset + dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 753 | DCHECK(target != nullptr); |
| 754 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 755 | current_block_->AddSuccessor(target); |
| 756 | current_block_ = nullptr; |
| 757 | break; |
| 758 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 759 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 760 | case Instruction::RETURN: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 761 | DCHECK_NE(return_type_, Primitive::kPrimNot); |
| 762 | DCHECK_NE(return_type_, Primitive::kPrimLong); |
| 763 | DCHECK_NE(return_type_, Primitive::kPrimDouble); |
| 764 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 768 | case Instruction::RETURN_OBJECT: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 769 | DCHECK(return_type_ == Primitive::kPrimNot); |
| 770 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 771 | break; |
| 772 | } |
| 773 | |
| 774 | case Instruction::RETURN_WIDE: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 775 | DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong); |
| 776 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 780 | case Instruction::INVOKE_STATIC: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 781 | case Instruction::INVOKE_DIRECT: |
| 782 | case Instruction::INVOKE_VIRTUAL: { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 783 | uint32_t method_idx = instruction.VRegB_35c(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 784 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 785 | uint32_t args[5]; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 786 | instruction.GetVarArgs(args); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 787 | if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) { |
| 788 | return false; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 789 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 790 | break; |
| 791 | } |
| 792 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 793 | case Instruction::INVOKE_STATIC_RANGE: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 794 | case Instruction::INVOKE_DIRECT_RANGE: |
| 795 | case Instruction::INVOKE_VIRTUAL_RANGE: { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 796 | uint32_t method_idx = instruction.VRegB_3rc(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 797 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 798 | uint32_t register_index = instruction.VRegC(); |
| 799 | if (!BuildInvoke(instruction, dex_offset, method_idx, |
| 800 | number_of_vreg_arguments, true, nullptr, register_index)) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 801 | return false; |
| 802 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 803 | break; |
| 804 | } |
| 805 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 806 | case Instruction::NEG_INT: { |
| 807 | Unop_12x<HNeg>(instruction, Primitive::kPrimInt); |
| 808 | break; |
| 809 | } |
| 810 | |
Roland Levillain | 2e07b4f | 2014-10-23 18:12:09 +0100 | [diff] [blame] | 811 | case Instruction::NEG_LONG: { |
| 812 | Unop_12x<HNeg>(instruction, Primitive::kPrimLong); |
| 813 | break; |
| 814 | } |
| 815 | |
Roland Levillain | 1cc5f251 | 2014-10-22 18:06:21 +0100 | [diff] [blame] | 816 | case Instruction::NOT_INT: { |
| 817 | Unop_12x<HNot>(instruction, Primitive::kPrimInt); |
| 818 | break; |
| 819 | } |
| 820 | |
Roland Levillain | 7056643 | 2014-10-24 16:20:17 +0100 | [diff] [blame] | 821 | case Instruction::NOT_LONG: { |
| 822 | Unop_12x<HNot>(instruction, Primitive::kPrimLong); |
| 823 | break; |
| 824 | } |
| 825 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 826 | case Instruction::ADD_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 827 | Binop_23x<HAdd>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | |
| 831 | case Instruction::ADD_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 832 | Binop_23x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 833 | break; |
| 834 | } |
| 835 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 836 | case Instruction::ADD_DOUBLE: { |
| 837 | Binop_23x<HAdd>(instruction, Primitive::kPrimDouble); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | case Instruction::ADD_FLOAT: { |
| 842 | Binop_23x<HAdd>(instruction, Primitive::kPrimFloat); |
| 843 | break; |
| 844 | } |
| 845 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 846 | case Instruction::SUB_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 847 | Binop_23x<HSub>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | |
| 851 | case Instruction::SUB_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 852 | Binop_23x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
Calin Juravle | 096cc02 | 2014-10-23 17:01:13 +0100 | [diff] [blame] | 856 | case Instruction::SUB_FLOAT: { |
| 857 | Binop_23x<HSub>(instruction, Primitive::kPrimFloat); |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | case Instruction::SUB_DOUBLE: { |
| 862 | Binop_23x<HSub>(instruction, Primitive::kPrimDouble); |
| 863 | break; |
| 864 | } |
| 865 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 866 | case Instruction::ADD_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 867 | Binop_12x<HAdd>(instruction, Primitive::kPrimInt); |
| 868 | break; |
| 869 | } |
| 870 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 871 | case Instruction::MUL_INT: { |
| 872 | Binop_23x<HMul>(instruction, Primitive::kPrimInt); |
| 873 | break; |
| 874 | } |
| 875 | |
| 876 | case Instruction::MUL_LONG: { |
| 877 | Binop_23x<HMul>(instruction, Primitive::kPrimLong); |
| 878 | break; |
| 879 | } |
| 880 | |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 881 | case Instruction::MUL_FLOAT: { |
| 882 | Binop_23x<HMul>(instruction, Primitive::kPrimFloat); |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | case Instruction::MUL_DOUBLE: { |
| 887 | Binop_23x<HMul>(instruction, Primitive::kPrimDouble); |
| 888 | break; |
| 889 | } |
| 890 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 891 | case Instruction::DIV_FLOAT: { |
| 892 | Binop_23x<HDiv>(instruction, Primitive::kPrimFloat); |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | case Instruction::DIV_DOUBLE: { |
| 897 | Binop_23x<HDiv>(instruction, Primitive::kPrimDouble); |
| 898 | break; |
| 899 | } |
| 900 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 901 | case Instruction::ADD_LONG_2ADDR: { |
| 902 | Binop_12x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 903 | break; |
| 904 | } |
| 905 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 906 | case Instruction::ADD_DOUBLE_2ADDR: { |
| 907 | Binop_12x<HAdd>(instruction, Primitive::kPrimDouble); |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | case Instruction::ADD_FLOAT_2ADDR: { |
| 912 | Binop_12x<HAdd>(instruction, Primitive::kPrimFloat); |
| 913 | break; |
| 914 | } |
| 915 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 916 | case Instruction::SUB_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 917 | Binop_12x<HSub>(instruction, Primitive::kPrimInt); |
| 918 | break; |
| 919 | } |
| 920 | |
| 921 | case Instruction::SUB_LONG_2ADDR: { |
| 922 | Binop_12x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | |
Calin Juravle | 096cc02 | 2014-10-23 17:01:13 +0100 | [diff] [blame] | 926 | case Instruction::SUB_FLOAT_2ADDR: { |
| 927 | Binop_12x<HSub>(instruction, Primitive::kPrimFloat); |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | case Instruction::SUB_DOUBLE_2ADDR: { |
| 932 | Binop_12x<HSub>(instruction, Primitive::kPrimDouble); |
| 933 | break; |
| 934 | } |
| 935 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 936 | case Instruction::MUL_INT_2ADDR: { |
| 937 | Binop_12x<HMul>(instruction, Primitive::kPrimInt); |
| 938 | break; |
| 939 | } |
| 940 | |
| 941 | case Instruction::MUL_LONG_2ADDR: { |
| 942 | Binop_12x<HMul>(instruction, Primitive::kPrimLong); |
| 943 | break; |
| 944 | } |
| 945 | |
Calin Juravle | b5bfa96 | 2014-10-21 18:02:24 +0100 | [diff] [blame] | 946 | case Instruction::MUL_FLOAT_2ADDR: { |
| 947 | Binop_12x<HMul>(instruction, Primitive::kPrimFloat); |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | case Instruction::MUL_DOUBLE_2ADDR: { |
| 952 | Binop_12x<HMul>(instruction, Primitive::kPrimDouble); |
| 953 | break; |
| 954 | } |
| 955 | |
Calin Juravle | 7c4954d | 2014-10-28 16:57:40 +0000 | [diff] [blame] | 956 | case Instruction::DIV_FLOAT_2ADDR: { |
| 957 | Binop_12x<HDiv>(instruction, Primitive::kPrimFloat); |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | case Instruction::DIV_DOUBLE_2ADDR: { |
| 962 | Binop_12x<HDiv>(instruction, Primitive::kPrimDouble); |
| 963 | break; |
| 964 | } |
| 965 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 966 | case Instruction::ADD_INT_LIT16: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 967 | Binop_22s<HAdd>(instruction, false); |
| 968 | break; |
| 969 | } |
| 970 | |
| 971 | case Instruction::RSUB_INT: { |
| 972 | Binop_22s<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 976 | case Instruction::MUL_INT_LIT16: { |
| 977 | Binop_22s<HMul>(instruction, false); |
| 978 | break; |
| 979 | } |
| 980 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 981 | case Instruction::ADD_INT_LIT8: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 982 | Binop_22b<HAdd>(instruction, false); |
| 983 | break; |
| 984 | } |
| 985 | |
| 986 | case Instruction::RSUB_INT_LIT8: { |
| 987 | Binop_22b<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 988 | break; |
| 989 | } |
| 990 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 991 | case Instruction::MUL_INT_LIT8: { |
| 992 | Binop_22b<HMul>(instruction, false); |
| 993 | break; |
| 994 | } |
| 995 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 996 | case Instruction::NEW_INSTANCE: { |
| 997 | current_block_->AddInstruction( |
| 998 | new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c())); |
| 999 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 1000 | break; |
| 1001 | } |
| 1002 | |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1003 | case Instruction::NEW_ARRAY: { |
| 1004 | HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt); |
| 1005 | current_block_->AddInstruction( |
| 1006 | new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c())); |
| 1007 | UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction()); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | case Instruction::FILLED_NEW_ARRAY: { |
| 1012 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
| 1013 | uint32_t type_index = instruction.VRegB_35c(); |
| 1014 | uint32_t args[5]; |
| 1015 | instruction.GetVarArgs(args); |
| 1016 | BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0); |
| 1017 | break; |
| 1018 | } |
| 1019 | |
| 1020 | case Instruction::FILLED_NEW_ARRAY_RANGE: { |
| 1021 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 1022 | uint32_t type_index = instruction.VRegB_3rc(); |
| 1023 | uint32_t register_index = instruction.VRegC_3rc(); |
| 1024 | BuildFilledNewArray( |
| 1025 | dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index); |
| 1026 | break; |
| 1027 | } |
| 1028 | |
| 1029 | case Instruction::FILL_ARRAY_DATA: { |
| 1030 | Temporaries temps(graph_, 1); |
| 1031 | HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot); |
| 1032 | HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset); |
| 1033 | current_block_->AddInstruction(null_check); |
| 1034 | temps.Add(null_check); |
| 1035 | |
| 1036 | HInstruction* length = new (arena_) HArrayLength(null_check); |
| 1037 | current_block_->AddInstruction(length); |
| 1038 | |
| 1039 | int32_t payload_offset = instruction.VRegB_31t() + dex_offset; |
| 1040 | const Instruction::ArrayDataPayload* payload = |
| 1041 | reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset); |
| 1042 | const uint8_t* data = payload->data; |
| 1043 | uint32_t element_count = payload->element_count; |
| 1044 | |
| 1045 | // Implementation of this DEX instruction seems to be that the bounds check is |
| 1046 | // done before doing any stores. |
| 1047 | HInstruction* last_index = GetIntConstant(payload->element_count - 1); |
| 1048 | current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset)); |
| 1049 | |
| 1050 | switch (payload->element_width) { |
| 1051 | case 1: |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 1052 | BuildFillArrayData(null_check, |
| 1053 | reinterpret_cast<const int8_t*>(data), |
| 1054 | element_count, |
| 1055 | Primitive::kPrimByte, |
| 1056 | dex_offset); |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1057 | break; |
| 1058 | case 2: |
| 1059 | BuildFillArrayData(null_check, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 1060 | reinterpret_cast<const int16_t*>(data), |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1061 | element_count, |
| 1062 | Primitive::kPrimShort, |
| 1063 | dex_offset); |
| 1064 | break; |
| 1065 | case 4: |
| 1066 | BuildFillArrayData(null_check, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 1067 | reinterpret_cast<const int32_t*>(data), |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1068 | element_count, |
| 1069 | Primitive::kPrimInt, |
| 1070 | dex_offset); |
| 1071 | break; |
| 1072 | case 8: |
| 1073 | BuildFillWideArrayData(null_check, |
Nicolas Geoffray | 8d6ae52 | 2014-10-23 18:32:13 +0100 | [diff] [blame] | 1074 | reinterpret_cast<const int64_t*>(data), |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1075 | element_count, |
| 1076 | dex_offset); |
| 1077 | break; |
| 1078 | default: |
| 1079 | LOG(FATAL) << "Unknown element width for " << payload->element_width; |
| 1080 | } |
| 1081 | break; |
| 1082 | } |
| 1083 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1084 | case Instruction::MOVE_RESULT: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 1085 | case Instruction::MOVE_RESULT_WIDE: |
| 1086 | case Instruction::MOVE_RESULT_OBJECT: |
Nicolas Geoffray | a3d05a4 | 2014-10-20 17:41:32 +0100 | [diff] [blame] | 1087 | UpdateLocal(instruction.VRegA(), latest_result_); |
| 1088 | latest_result_ = nullptr; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1089 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1090 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 1091 | case Instruction::CMP_LONG: { |
| 1092 | Binop_23x<HCompare>(instruction, Primitive::kPrimLong); |
| 1093 | break; |
| 1094 | } |
| 1095 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 1096 | case Instruction::NOP: |
| 1097 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1098 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1099 | case Instruction::IGET: |
| 1100 | case Instruction::IGET_WIDE: |
| 1101 | case Instruction::IGET_OBJECT: |
| 1102 | case Instruction::IGET_BOOLEAN: |
| 1103 | case Instruction::IGET_BYTE: |
| 1104 | case Instruction::IGET_CHAR: |
| 1105 | case Instruction::IGET_SHORT: { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1106 | if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1107 | return false; |
| 1108 | } |
| 1109 | break; |
| 1110 | } |
| 1111 | |
| 1112 | case Instruction::IPUT: |
| 1113 | case Instruction::IPUT_WIDE: |
| 1114 | case Instruction::IPUT_OBJECT: |
| 1115 | case Instruction::IPUT_BOOLEAN: |
| 1116 | case Instruction::IPUT_BYTE: |
| 1117 | case Instruction::IPUT_CHAR: |
| 1118 | case Instruction::IPUT_SHORT: { |
Nicolas Geoffray | 19a19cf | 2014-10-22 16:07:05 +0100 | [diff] [blame] | 1119 | if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) { |
| 1120 | return false; |
| 1121 | } |
| 1122 | break; |
| 1123 | } |
| 1124 | |
| 1125 | case Instruction::SGET: |
| 1126 | case Instruction::SGET_WIDE: |
| 1127 | case Instruction::SGET_OBJECT: |
| 1128 | case Instruction::SGET_BOOLEAN: |
| 1129 | case Instruction::SGET_BYTE: |
| 1130 | case Instruction::SGET_CHAR: |
| 1131 | case Instruction::SGET_SHORT: { |
| 1132 | if (!BuildStaticFieldAccess(instruction, dex_offset, false)) { |
| 1133 | return false; |
| 1134 | } |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | case Instruction::SPUT: |
| 1139 | case Instruction::SPUT_WIDE: |
| 1140 | case Instruction::SPUT_OBJECT: |
| 1141 | case Instruction::SPUT_BOOLEAN: |
| 1142 | case Instruction::SPUT_BYTE: |
| 1143 | case Instruction::SPUT_CHAR: |
| 1144 | case Instruction::SPUT_SHORT: { |
| 1145 | if (!BuildStaticFieldAccess(instruction, dex_offset, true)) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 1146 | return false; |
| 1147 | } |
| 1148 | break; |
| 1149 | } |
| 1150 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 1151 | #define ARRAY_XX(kind, anticipated_type) \ |
| 1152 | case Instruction::AGET##kind: { \ |
| 1153 | BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \ |
| 1154 | break; \ |
| 1155 | } \ |
| 1156 | case Instruction::APUT##kind: { \ |
| 1157 | BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \ |
| 1158 | break; \ |
| 1159 | } |
| 1160 | |
| 1161 | ARRAY_XX(, Primitive::kPrimInt); |
| 1162 | ARRAY_XX(_WIDE, Primitive::kPrimLong); |
| 1163 | ARRAY_XX(_OBJECT, Primitive::kPrimNot); |
| 1164 | ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean); |
| 1165 | ARRAY_XX(_BYTE, Primitive::kPrimByte); |
| 1166 | ARRAY_XX(_CHAR, Primitive::kPrimChar); |
| 1167 | ARRAY_XX(_SHORT, Primitive::kPrimShort); |
| 1168 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1169 | case Instruction::ARRAY_LENGTH: { |
| 1170 | HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot); |
| 1171 | current_block_->AddInstruction(new (arena_) HArrayLength(object)); |
| 1172 | UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction()); |
| 1173 | break; |
| 1174 | } |
| 1175 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 1176 | case Instruction::CONST_STRING: { |
| 1177 | current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset)); |
| 1178 | UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); |
| 1179 | break; |
| 1180 | } |
| 1181 | |
| 1182 | case Instruction::CONST_STRING_JUMBO: { |
| 1183 | current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset)); |
| 1184 | UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction()); |
| 1185 | break; |
| 1186 | } |
| 1187 | |
Nicolas Geoffray | 424f676 | 2014-11-03 14:51:25 +0000 | [diff] [blame] | 1188 | case Instruction::CONST_CLASS: { |
| 1189 | uint16_t type_index = instruction.VRegB_21c(); |
| 1190 | bool type_known_final; |
| 1191 | bool type_known_abstract; |
| 1192 | bool is_referrers_class; |
| 1193 | bool can_access = compiler_driver_->CanAccessTypeWithoutChecks( |
| 1194 | dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, |
| 1195 | &type_known_final, &type_known_abstract, &is_referrers_class); |
| 1196 | if (!can_access) { |
| 1197 | return false; |
| 1198 | } |
| 1199 | current_block_->AddInstruction( |
| 1200 | new (arena_) HLoadClass(instruction.VRegB_21c(), is_referrers_class, dex_offset)); |
| 1201 | UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); |
| 1202 | break; |
| 1203 | } |
| 1204 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1205 | default: |
| 1206 | return false; |
| 1207 | } |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1211 | HIntConstant* HGraphBuilder::GetIntConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1212 | if (constant0_ != nullptr) { |
| 1213 | return constant0_; |
| 1214 | } |
| 1215 | constant0_ = new(arena_) HIntConstant(0); |
| 1216 | entry_block_->AddInstruction(constant0_); |
| 1217 | return constant0_; |
| 1218 | } |
| 1219 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1220 | HIntConstant* HGraphBuilder::GetIntConstant1() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 1221 | if (constant1_ != nullptr) { |
| 1222 | return constant1_; |
| 1223 | } |
| 1224 | constant1_ = new(arena_) HIntConstant(1); |
| 1225 | entry_block_->AddInstruction(constant1_); |
| 1226 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1229 | HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1230 | switch (constant) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1231 | case 0: return GetIntConstant0(); |
| 1232 | case 1: return GetIntConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1233 | default: { |
| 1234 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 1235 | entry_block_->AddInstruction(instruction); |
| 1236 | return instruction; |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1241 | HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) { |
| 1242 | HLongConstant* instruction = new (arena_) HLongConstant(constant); |
| 1243 | entry_block_->AddInstruction(instruction); |
| 1244 | return instruction; |
| 1245 | } |
| 1246 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1247 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 1248 | return locals_.Get(register_index); |
| 1249 | } |
| 1250 | |
| 1251 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 1252 | HLocal* local = GetLocalAt(register_index); |
| 1253 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 1254 | } |
| 1255 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1256 | HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1257 | HLocal* local = GetLocalAt(register_index); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 1258 | current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 1259 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1262 | } // namespace art |