Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2014 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 18 | #include "builder.h" |
| 19 | |
| 20 | #include "class_linker.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 21 | #include "dex_file.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 22 | #include "dex_file-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 23 | #include "dex_instruction.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 24 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 25 | #include "driver/compiler_driver-inl.h" |
| 26 | #include "mirror/art_field.h" |
| 27 | #include "mirror/art_field-inl.h" |
| 28 | #include "mirror/class_loader.h" |
| 29 | #include "mirror/dex_cache.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 30 | #include "nodes.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 31 | #include "primitive.h" |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 32 | #include "scoped_thread_state_change.h" |
| 33 | #include "thread.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 34 | |
| 35 | namespace art { |
| 36 | |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 37 | /** |
| 38 | * Helper class to add HTemporary instructions. This class is used when |
| 39 | * converting a DEX instruction to multiple HInstruction, and where those |
| 40 | * instructions do not die at the following instruction, but instead spans |
| 41 | * multiple instructions. |
| 42 | */ |
| 43 | class Temporaries : public ValueObject { |
| 44 | public: |
| 45 | Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) { |
| 46 | graph_->UpdateNumberOfTemporaries(count_); |
| 47 | } |
| 48 | |
| 49 | void Add(HInstruction* instruction) { |
| 50 | // We currently only support vreg size temps. |
| 51 | DCHECK(instruction->GetType() != Primitive::kPrimLong |
| 52 | && instruction->GetType() != Primitive::kPrimDouble); |
| 53 | HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++); |
| 54 | instruction->GetBlock()->AddInstruction(temp); |
| 55 | DCHECK(temp->GetPrevious() == instruction); |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | HGraph* const graph_; |
| 60 | |
| 61 | // The total number of temporaries that will be used. |
| 62 | const size_t count_; |
| 63 | |
| 64 | // Current index in the temporary stack, updated by `Add`. |
| 65 | size_t index_; |
| 66 | }; |
| 67 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 68 | static bool IsTypeSupported(Primitive::Type type) { |
| 69 | return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble; |
| 70 | } |
| 71 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 72 | void HGraphBuilder::InitializeLocals(uint16_t count) { |
| 73 | graph_->SetNumberOfVRegs(count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 74 | locals_.SetSize(count); |
| 75 | for (int i = 0; i < count; i++) { |
| 76 | HLocal* local = new (arena_) HLocal(i); |
| 77 | entry_block_->AddInstruction(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 78 | locals_.Put(i, local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 82 | bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) { |
| 83 | // dex_compilation_unit_ is null only when unit testing. |
| 84 | if (dex_compilation_unit_ == nullptr) { |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | graph_->SetNumberOfInVRegs(number_of_parameters); |
| 89 | const char* shorty = dex_compilation_unit_->GetShorty(); |
| 90 | int locals_index = locals_.Size() - number_of_parameters; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 91 | int parameter_index = 0; |
| 92 | |
| 93 | if (!dex_compilation_unit_->IsStatic()) { |
| 94 | // Add the implicit 'this' argument, not expressed in the signature. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 95 | HParameterValue* parameter = |
| 96 | new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 97 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 98 | HLocal* local = GetLocalAt(locals_index++); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 99 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 100 | number_of_parameters--; |
| 101 | } |
| 102 | |
| 103 | uint32_t pos = 1; |
| 104 | for (int i = 0; i < number_of_parameters; i++) { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 105 | HParameterValue* parameter = |
| 106 | new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++])); |
| 107 | entry_block_->AddInstruction(parameter); |
| 108 | HLocal* local = GetLocalAt(locals_index++); |
| 109 | // Store the parameter value in the local that the dex code will use |
| 110 | // to reference that parameter. |
| 111 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
| 112 | bool is_wide = (parameter->GetType() == Primitive::kPrimLong) |
| 113 | || (parameter->GetType() == Primitive::kPrimDouble); |
| 114 | if (is_wide) { |
| 115 | i++; |
| 116 | locals_index++; |
| 117 | parameter_index++; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 123 | static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 124 | if (code_item.tries_size_ > 0) { |
| 125 | return false; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 126 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
| 129 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 130 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 131 | void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 132 | int32_t target_offset = instruction.GetTargetOffset(); |
| 133 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 134 | HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 135 | HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 136 | T* comparison = new (arena_) T(first, second); |
| 137 | current_block_->AddInstruction(comparison); |
| 138 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 139 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 140 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 141 | DCHECK(target != nullptr); |
| 142 | current_block_->AddSuccessor(target); |
| 143 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 144 | DCHECK(target != nullptr); |
| 145 | current_block_->AddSuccessor(target); |
| 146 | current_block_ = nullptr; |
| 147 | } |
| 148 | |
| 149 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 150 | void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 151 | int32_t target_offset = instruction.GetTargetOffset(); |
| 152 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 153 | HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 154 | T* comparison = new (arena_) T(value, GetIntConstant(0)); |
| 155 | current_block_->AddInstruction(comparison); |
| 156 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 157 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 158 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 159 | DCHECK(target != nullptr); |
| 160 | current_block_->AddSuccessor(target); |
| 161 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 162 | DCHECK(target != nullptr); |
| 163 | current_block_->AddSuccessor(target); |
| 164 | current_block_ = nullptr; |
| 165 | } |
| 166 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 167 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 168 | if (!CanHandleCodeItem(code_item)) { |
| 169 | return nullptr; |
| 170 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 171 | |
| 172 | const uint16_t* code_ptr = code_item.insns_; |
| 173 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
| 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); |
| 419 | return true; |
| 420 | } |
| 421 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 422 | bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction, |
| 423 | uint32_t dex_offset, |
| 424 | bool is_put) { |
| 425 | uint32_t source_or_dest_reg = instruction.VRegA_22c(); |
| 426 | uint32_t obj_reg = instruction.VRegB_22c(); |
| 427 | uint16_t field_index = instruction.VRegC_22c(); |
| 428 | |
| 429 | ScopedObjectAccess soa(Thread::Current()); |
| 430 | StackHandleScope<1> hs(soa.Self()); |
| 431 | Handle<mirror::ArtField> resolved_field(hs.NewHandle( |
| 432 | compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa))); |
| 433 | |
| 434 | if (resolved_field.Get() == nullptr) { |
| 435 | return false; |
| 436 | } |
| 437 | if (resolved_field->IsVolatile()) { |
| 438 | return false; |
| 439 | } |
| 440 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 441 | Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); |
| 442 | if (!IsTypeSupported(field_type)) { |
| 443 | return false; |
| 444 | } |
| 445 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 446 | HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot); |
| 447 | current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset)); |
| 448 | if (is_put) { |
| 449 | Temporaries temps(graph_, 1); |
| 450 | HInstruction* null_check = current_block_->GetLastInstruction(); |
| 451 | // We need one temporary for the null check. |
| 452 | temps.Add(null_check); |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 453 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 454 | current_block_->AddInstruction(new (arena_) HInstanceFieldSet( |
| 455 | null_check, |
| 456 | value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 457 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 458 | resolved_field->GetOffset())); |
| 459 | } else { |
| 460 | current_block_->AddInstruction(new (arena_) HInstanceFieldGet( |
| 461 | current_block_->GetLastInstruction(), |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 462 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 463 | resolved_field->GetOffset())); |
| 464 | |
| 465 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 470 | void HGraphBuilder::BuildArrayAccess(const Instruction& instruction, |
| 471 | uint32_t dex_offset, |
| 472 | bool is_put, |
| 473 | Primitive::Type anticipated_type) { |
| 474 | uint8_t source_or_dest_reg = instruction.VRegA_23x(); |
| 475 | uint8_t array_reg = instruction.VRegB_23x(); |
| 476 | uint8_t index_reg = instruction.VRegC_23x(); |
| 477 | |
| 478 | DCHECK(IsTypeSupported(anticipated_type)); |
| 479 | |
| 480 | // We need one temporary for the null check, one for the index, and one for the length. |
| 481 | Temporaries temps(graph_, 3); |
| 482 | |
| 483 | HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot); |
| 484 | object = new (arena_) HNullCheck(object, dex_offset); |
| 485 | current_block_->AddInstruction(object); |
| 486 | temps.Add(object); |
| 487 | |
| 488 | HInstruction* length = new (arena_) HArrayLength(object); |
| 489 | current_block_->AddInstruction(length); |
| 490 | temps.Add(length); |
| 491 | HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt); |
| 492 | index = new (arena_) HBoundsCheck(index, length, dex_offset); |
| 493 | current_block_->AddInstruction(index); |
| 494 | temps.Add(index); |
| 495 | if (is_put) { |
| 496 | HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type); |
| 497 | // TODO: Insert a type check node if the type is Object. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 498 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 499 | object, index, value, anticipated_type, dex_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 500 | } else { |
| 501 | current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type)); |
| 502 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 503 | } |
| 504 | } |
| 505 | |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 506 | void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) { |
| 507 | if (target_offset <= 0) { |
| 508 | // Unconditionnally add a suspend check to backward branches. We can remove |
| 509 | // them after we recognize loops in the graph. |
| 510 | current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset)); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 515 | if (current_block_ == nullptr) { |
| 516 | return true; // Dead code |
| 517 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 518 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 519 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 520 | case Instruction::CONST_4: { |
| 521 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 522 | HIntConstant* constant = GetIntConstant(instruction.VRegB_11n()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 523 | UpdateLocal(register_index, constant); |
| 524 | break; |
| 525 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 526 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 527 | case Instruction::CONST_16: { |
| 528 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 529 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21s()); |
| 530 | UpdateLocal(register_index, constant); |
| 531 | break; |
| 532 | } |
| 533 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 534 | case Instruction::CONST: { |
| 535 | int32_t register_index = instruction.VRegA(); |
| 536 | HIntConstant* constant = GetIntConstant(instruction.VRegB_31i()); |
| 537 | UpdateLocal(register_index, constant); |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | case Instruction::CONST_HIGH16: { |
| 542 | int32_t register_index = instruction.VRegA(); |
| 543 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16); |
| 544 | UpdateLocal(register_index, constant); |
| 545 | break; |
| 546 | } |
| 547 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 548 | case Instruction::CONST_WIDE_16: { |
| 549 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 550 | // Get 16 bits of constant value, sign extended to 64 bits. |
| 551 | int64_t value = instruction.VRegB_21s(); |
| 552 | value <<= 48; |
| 553 | value >>= 48; |
| 554 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 555 | UpdateLocal(register_index, constant); |
| 556 | break; |
| 557 | } |
| 558 | |
| 559 | case Instruction::CONST_WIDE_32: { |
| 560 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 561 | // Get 32 bits of constant value, sign extended to 64 bits. |
| 562 | int64_t value = instruction.VRegB_31i(); |
| 563 | value <<= 32; |
| 564 | value >>= 32; |
| 565 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 566 | UpdateLocal(register_index, constant); |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | case Instruction::CONST_WIDE: { |
| 571 | int32_t register_index = instruction.VRegA(); |
| 572 | HLongConstant* constant = GetLongConstant(instruction.VRegB_51l()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 573 | UpdateLocal(register_index, constant); |
| 574 | break; |
| 575 | } |
| 576 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 577 | case Instruction::CONST_WIDE_HIGH16: { |
| 578 | int32_t register_index = instruction.VRegA(); |
| 579 | int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48; |
| 580 | HLongConstant* constant = GetLongConstant(value); |
| 581 | UpdateLocal(register_index, constant); |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | // TODO: these instructions are also used to move floating point values, so what is |
| 586 | // the type (int or float)? |
| 587 | case Instruction::MOVE: |
| 588 | case Instruction::MOVE_FROM16: |
| 589 | case Instruction::MOVE_16: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 590 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 591 | UpdateLocal(instruction.VRegA(), value); |
| 592 | break; |
| 593 | } |
| 594 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 595 | // TODO: these instructions are also used to move floating point values, so what is |
| 596 | // the type (long or double)? |
| 597 | case Instruction::MOVE_WIDE: |
| 598 | case Instruction::MOVE_WIDE_FROM16: |
| 599 | case Instruction::MOVE_WIDE_16: { |
| 600 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong); |
| 601 | UpdateLocal(instruction.VRegA(), value); |
| 602 | break; |
| 603 | } |
| 604 | |
| 605 | case Instruction::MOVE_OBJECT: |
| 606 | case Instruction::MOVE_OBJECT_16: |
| 607 | case Instruction::MOVE_OBJECT_FROM16: { |
| 608 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot); |
| 609 | UpdateLocal(instruction.VRegA(), value); |
| 610 | break; |
| 611 | } |
| 612 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 613 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 614 | BuildReturn(instruction, Primitive::kPrimVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 615 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 618 | #define IF_XX(comparison, cond) \ |
| 619 | case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \ |
| 620 | case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 621 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 622 | IF_XX(HEqual, EQ); |
| 623 | IF_XX(HNotEqual, NE); |
| 624 | IF_XX(HLessThan, LT); |
| 625 | IF_XX(HLessThanOrEqual, LE); |
| 626 | IF_XX(HGreaterThan, GT); |
| 627 | IF_XX(HGreaterThanOrEqual, GE); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 628 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 629 | case Instruction::GOTO: |
| 630 | case Instruction::GOTO_16: |
| 631 | case Instruction::GOTO_32: { |
Nicolas Geoffray | fbc695f | 2014-09-15 15:33:30 +0000 | [diff] [blame] | 632 | int32_t offset = instruction.GetTargetOffset(); |
| 633 | PotentiallyAddSuspendCheck(offset, dex_offset); |
| 634 | HBasicBlock* target = FindBlockStartingAt(offset + dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 635 | DCHECK(target != nullptr); |
| 636 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 637 | current_block_->AddSuccessor(target); |
| 638 | current_block_ = nullptr; |
| 639 | break; |
| 640 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 641 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 642 | case Instruction::RETURN: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 643 | DCHECK_NE(return_type_, Primitive::kPrimNot); |
| 644 | DCHECK_NE(return_type_, Primitive::kPrimLong); |
| 645 | DCHECK_NE(return_type_, Primitive::kPrimDouble); |
| 646 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 650 | case Instruction::RETURN_OBJECT: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 651 | DCHECK(return_type_ == Primitive::kPrimNot); |
| 652 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | |
| 656 | case Instruction::RETURN_WIDE: { |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 657 | DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong); |
| 658 | BuildReturn(instruction, return_type_); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 662 | case Instruction::INVOKE_STATIC: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 663 | case Instruction::INVOKE_DIRECT: |
| 664 | case Instruction::INVOKE_VIRTUAL: { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 665 | uint32_t method_idx = instruction.VRegB_35c(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 666 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 667 | uint32_t args[5]; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 668 | instruction.GetVarArgs(args); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 669 | if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) { |
| 670 | return false; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 671 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 672 | break; |
| 673 | } |
| 674 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 675 | case Instruction::INVOKE_STATIC_RANGE: |
Nicolas Geoffray | e982f0b | 2014-08-13 02:11:24 +0100 | [diff] [blame] | 676 | case Instruction::INVOKE_DIRECT_RANGE: |
| 677 | case Instruction::INVOKE_VIRTUAL_RANGE: { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 678 | uint32_t method_idx = instruction.VRegB_3rc(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 679 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 680 | uint32_t register_index = instruction.VRegC(); |
| 681 | if (!BuildInvoke(instruction, dex_offset, method_idx, |
| 682 | number_of_vreg_arguments, true, nullptr, register_index)) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 683 | return false; |
| 684 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | |
Roland Levillain | 88cb175 | 2014-10-20 16:36:47 +0100 | [diff] [blame] | 688 | case Instruction::NEG_INT: { |
| 689 | Unop_12x<HNeg>(instruction, Primitive::kPrimInt); |
| 690 | break; |
| 691 | } |
| 692 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 693 | case Instruction::ADD_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 694 | Binop_23x<HAdd>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
| 698 | case Instruction::ADD_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 699 | Binop_23x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 700 | break; |
| 701 | } |
| 702 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 703 | case Instruction::ADD_DOUBLE: { |
| 704 | Binop_23x<HAdd>(instruction, Primitive::kPrimDouble); |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | case Instruction::ADD_FLOAT: { |
| 709 | Binop_23x<HAdd>(instruction, Primitive::kPrimFloat); |
| 710 | break; |
| 711 | } |
| 712 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 713 | case Instruction::SUB_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 714 | Binop_23x<HSub>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 715 | break; |
| 716 | } |
| 717 | |
| 718 | case Instruction::SUB_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 719 | Binop_23x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 720 | break; |
| 721 | } |
| 722 | |
| 723 | case Instruction::ADD_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 724 | Binop_12x<HAdd>(instruction, Primitive::kPrimInt); |
| 725 | break; |
| 726 | } |
| 727 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 728 | case Instruction::MUL_INT: { |
| 729 | Binop_23x<HMul>(instruction, Primitive::kPrimInt); |
| 730 | break; |
| 731 | } |
| 732 | |
| 733 | case Instruction::MUL_LONG: { |
| 734 | Binop_23x<HMul>(instruction, Primitive::kPrimLong); |
| 735 | break; |
| 736 | } |
| 737 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 738 | case Instruction::ADD_LONG_2ADDR: { |
| 739 | Binop_12x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 740 | break; |
| 741 | } |
| 742 | |
Nicolas Geoffray | 7fb49da | 2014-10-06 09:12:41 +0100 | [diff] [blame] | 743 | case Instruction::ADD_DOUBLE_2ADDR: { |
| 744 | Binop_12x<HAdd>(instruction, Primitive::kPrimDouble); |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | case Instruction::ADD_FLOAT_2ADDR: { |
| 749 | Binop_12x<HAdd>(instruction, Primitive::kPrimFloat); |
| 750 | break; |
| 751 | } |
| 752 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 753 | case Instruction::SUB_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 754 | Binop_12x<HSub>(instruction, Primitive::kPrimInt); |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | case Instruction::SUB_LONG_2ADDR: { |
| 759 | Binop_12x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 760 | break; |
| 761 | } |
| 762 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 763 | case Instruction::MUL_INT_2ADDR: { |
| 764 | Binop_12x<HMul>(instruction, Primitive::kPrimInt); |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | case Instruction::MUL_LONG_2ADDR: { |
| 769 | Binop_12x<HMul>(instruction, Primitive::kPrimLong); |
| 770 | break; |
| 771 | } |
| 772 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 773 | case Instruction::ADD_INT_LIT16: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 774 | Binop_22s<HAdd>(instruction, false); |
| 775 | break; |
| 776 | } |
| 777 | |
| 778 | case Instruction::RSUB_INT: { |
| 779 | Binop_22s<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 780 | break; |
| 781 | } |
| 782 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 783 | case Instruction::MUL_INT_LIT16: { |
| 784 | Binop_22s<HMul>(instruction, false); |
| 785 | break; |
| 786 | } |
| 787 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 788 | case Instruction::ADD_INT_LIT8: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 789 | Binop_22b<HAdd>(instruction, false); |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | case Instruction::RSUB_INT_LIT8: { |
| 794 | Binop_22b<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 795 | break; |
| 796 | } |
| 797 | |
Calin Juravle | 34bacdf | 2014-10-07 20:23:36 +0100 | [diff] [blame] | 798 | case Instruction::MUL_INT_LIT8: { |
| 799 | Binop_22b<HMul>(instruction, false); |
| 800 | break; |
| 801 | } |
| 802 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 803 | case Instruction::NEW_INSTANCE: { |
| 804 | current_block_->AddInstruction( |
| 805 | new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c())); |
| 806 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 807 | break; |
| 808 | } |
| 809 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 810 | case Instruction::MOVE_RESULT: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 811 | case Instruction::MOVE_RESULT_WIDE: |
| 812 | case Instruction::MOVE_RESULT_OBJECT: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 813 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 814 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 815 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 816 | case Instruction::CMP_LONG: { |
| 817 | Binop_23x<HCompare>(instruction, Primitive::kPrimLong); |
| 818 | break; |
| 819 | } |
| 820 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 821 | case Instruction::NOP: |
| 822 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 823 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 824 | case Instruction::IGET: |
| 825 | case Instruction::IGET_WIDE: |
| 826 | case Instruction::IGET_OBJECT: |
| 827 | case Instruction::IGET_BOOLEAN: |
| 828 | case Instruction::IGET_BYTE: |
| 829 | case Instruction::IGET_CHAR: |
| 830 | case Instruction::IGET_SHORT: { |
| 831 | if (!BuildFieldAccess(instruction, dex_offset, false)) { |
| 832 | return false; |
| 833 | } |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | case Instruction::IPUT: |
| 838 | case Instruction::IPUT_WIDE: |
| 839 | case Instruction::IPUT_OBJECT: |
| 840 | case Instruction::IPUT_BOOLEAN: |
| 841 | case Instruction::IPUT_BYTE: |
| 842 | case Instruction::IPUT_CHAR: |
| 843 | case Instruction::IPUT_SHORT: { |
| 844 | if (!BuildFieldAccess(instruction, dex_offset, true)) { |
| 845 | return false; |
| 846 | } |
| 847 | break; |
| 848 | } |
| 849 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 850 | #define ARRAY_XX(kind, anticipated_type) \ |
| 851 | case Instruction::AGET##kind: { \ |
| 852 | BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \ |
| 853 | break; \ |
| 854 | } \ |
| 855 | case Instruction::APUT##kind: { \ |
| 856 | BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \ |
| 857 | break; \ |
| 858 | } |
| 859 | |
| 860 | ARRAY_XX(, Primitive::kPrimInt); |
| 861 | ARRAY_XX(_WIDE, Primitive::kPrimLong); |
| 862 | ARRAY_XX(_OBJECT, Primitive::kPrimNot); |
| 863 | ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean); |
| 864 | ARRAY_XX(_BYTE, Primitive::kPrimByte); |
| 865 | ARRAY_XX(_CHAR, Primitive::kPrimChar); |
| 866 | ARRAY_XX(_SHORT, Primitive::kPrimShort); |
| 867 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 868 | case Instruction::ARRAY_LENGTH: { |
| 869 | HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot); |
| 870 | current_block_->AddInstruction(new (arena_) HArrayLength(object)); |
| 871 | UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction()); |
| 872 | break; |
| 873 | } |
| 874 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 875 | default: |
| 876 | return false; |
| 877 | } |
| 878 | return true; |
| 879 | } |
| 880 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 881 | HIntConstant* HGraphBuilder::GetIntConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 882 | if (constant0_ != nullptr) { |
| 883 | return constant0_; |
| 884 | } |
| 885 | constant0_ = new(arena_) HIntConstant(0); |
| 886 | entry_block_->AddInstruction(constant0_); |
| 887 | return constant0_; |
| 888 | } |
| 889 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 890 | HIntConstant* HGraphBuilder::GetIntConstant1() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 891 | if (constant1_ != nullptr) { |
| 892 | return constant1_; |
| 893 | } |
| 894 | constant1_ = new(arena_) HIntConstant(1); |
| 895 | entry_block_->AddInstruction(constant1_); |
| 896 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 899 | HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 900 | switch (constant) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 901 | case 0: return GetIntConstant0(); |
| 902 | case 1: return GetIntConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 903 | default: { |
| 904 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 905 | entry_block_->AddInstruction(instruction); |
| 906 | return instruction; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 911 | HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) { |
| 912 | HLongConstant* instruction = new (arena_) HLongConstant(constant); |
| 913 | entry_block_->AddInstruction(instruction); |
| 914 | return instruction; |
| 915 | } |
| 916 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 917 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 918 | return locals_.Get(register_index); |
| 919 | } |
| 920 | |
| 921 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 922 | HLocal* local = GetLocalAt(register_index); |
| 923 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 924 | } |
| 925 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 926 | HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 927 | HLocal* local = GetLocalAt(register_index); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 928 | current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 929 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 932 | } // namespace art |