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++) { |
| 105 | switch (shorty[pos++]) { |
| 106 | case 'F': |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 107 | case 'D': { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
| 111 | default: { |
| 112 | // integer and reference parameters. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 113 | HParameterValue* parameter = |
| 114 | new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos - 1])); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 115 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 116 | HLocal* local = GetLocalAt(locals_index++); |
| 117 | // Store the parameter value in the local that the dex code will use |
| 118 | // to reference that parameter. |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 119 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 120 | if (parameter->GetType() == Primitive::kPrimLong) { |
| 121 | i++; |
| 122 | locals_index++; |
| 123 | parameter_index++; |
| 124 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | return true; |
| 130 | } |
| 131 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 132 | static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 133 | if (code_item.tries_size_ > 0) { |
| 134 | return false; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 135 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 136 | return true; |
| 137 | } |
| 138 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 139 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 140 | void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 141 | int32_t target_offset = instruction.GetTargetOffset(); |
| 142 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 143 | HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 144 | HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 145 | T* comparison = new (arena_) T(first, second); |
| 146 | current_block_->AddInstruction(comparison); |
| 147 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 148 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 149 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 150 | DCHECK(target != nullptr); |
| 151 | current_block_->AddSuccessor(target); |
| 152 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 153 | DCHECK(target != nullptr); |
| 154 | current_block_->AddSuccessor(target); |
| 155 | current_block_ = nullptr; |
| 156 | } |
| 157 | |
| 158 | template<typename T> |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 159 | void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 160 | int32_t target_offset = instruction.GetTargetOffset(); |
| 161 | PotentiallyAddSuspendCheck(target_offset, dex_offset); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 162 | HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 163 | T* comparison = new (arena_) T(value, GetIntConstant(0)); |
| 164 | current_block_->AddInstruction(comparison); |
| 165 | HInstruction* ifinst = new (arena_) HIf(comparison); |
| 166 | current_block_->AddInstruction(ifinst); |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 167 | fprintf(stderr, "%d and %d\n", dex_offset, target_offset); |
| 168 | HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 169 | DCHECK(target != nullptr); |
| 170 | current_block_->AddSuccessor(target); |
| 171 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 172 | DCHECK(target != nullptr); |
| 173 | current_block_->AddSuccessor(target); |
| 174 | current_block_ = nullptr; |
| 175 | } |
| 176 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 177 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 178 | if (!CanHandleCodeItem(code_item)) { |
| 179 | return nullptr; |
| 180 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 181 | |
| 182 | const uint16_t* code_ptr = code_item.insns_; |
| 183 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
| 184 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 185 | // Setup the graph with the entry block and exit block. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 186 | graph_ = new (arena_) HGraph(arena_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 187 | entry_block_ = new (arena_) HBasicBlock(graph_); |
| 188 | graph_->AddBlock(entry_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 189 | exit_block_ = new (arena_) HBasicBlock(graph_); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 190 | graph_->SetEntryBlock(entry_block_); |
| 191 | graph_->SetExitBlock(exit_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 192 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 193 | InitializeLocals(code_item.registers_size_); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 194 | graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 195 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 196 | // To avoid splitting blocks, we compute ahead of time the instructions that |
| 197 | // start a new block, and create these blocks. |
| 198 | ComputeBranchTargets(code_ptr, code_end); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 199 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 200 | if (!InitializeParameters(code_item.ins_size_)) { |
| 201 | return nullptr; |
| 202 | } |
| 203 | |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 204 | // Add the suspend check to the entry block. |
| 205 | entry_block_->AddInstruction(new (arena_) HSuspendCheck(0)); |
| 206 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 207 | size_t dex_offset = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 208 | while (code_ptr < code_end) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 209 | // Update the current block if dex_offset starts a new block. |
| 210 | MaybeUpdateCurrentBlock(dex_offset); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 211 | const Instruction& instruction = *Instruction::At(code_ptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 212 | if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr; |
| 213 | dex_offset += instruction.SizeInCodeUnits(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 214 | code_ptr += instruction.SizeInCodeUnits(); |
| 215 | } |
| 216 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 217 | // 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] | 218 | graph_->AddBlock(exit_block_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 219 | exit_block_->AddInstruction(new (arena_) HExit()); |
| 220 | entry_block_->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 221 | return graph_; |
| 222 | } |
| 223 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 224 | void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { |
| 225 | HBasicBlock* block = FindBlockStartingAt(index); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 226 | if (block == nullptr) { |
| 227 | return; |
| 228 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 229 | |
| 230 | if (current_block_ != nullptr) { |
| 231 | // Branching instructions clear current_block, so we know |
| 232 | // the last instruction of the current block is not a branching |
| 233 | // instruction. We add an unconditional goto to the found block. |
| 234 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 235 | current_block_->AddSuccessor(block); |
| 236 | } |
| 237 | graph_->AddBlock(block); |
| 238 | current_block_ = block; |
| 239 | } |
| 240 | |
| 241 | void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) { |
| 242 | // TODO: Support switch instructions. |
| 243 | branch_targets_.SetSize(code_end - code_ptr); |
| 244 | |
| 245 | // Create the first block for the dex instructions, single successor of the entry block. |
| 246 | HBasicBlock* block = new (arena_) HBasicBlock(graph_); |
| 247 | branch_targets_.Put(0, block); |
| 248 | entry_block_->AddSuccessor(block); |
| 249 | |
| 250 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 251 | // the locations these instructions branch to. |
| 252 | size_t dex_offset = 0; |
| 253 | while (code_ptr < code_end) { |
| 254 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 255 | if (instruction.IsBranch()) { |
| 256 | int32_t target = instruction.GetTargetOffset() + dex_offset; |
| 257 | // Create a block for the target instruction. |
| 258 | if (FindBlockStartingAt(target) == nullptr) { |
| 259 | block = new (arena_) HBasicBlock(graph_); |
| 260 | branch_targets_.Put(target, block); |
| 261 | } |
| 262 | dex_offset += instruction.SizeInCodeUnits(); |
| 263 | code_ptr += instruction.SizeInCodeUnits(); |
| 264 | if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) { |
| 265 | block = new (arena_) HBasicBlock(graph_); |
| 266 | branch_targets_.Put(dex_offset, block); |
| 267 | } |
| 268 | } else { |
| 269 | code_ptr += instruction.SizeInCodeUnits(); |
| 270 | dex_offset += instruction.SizeInCodeUnits(); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { |
| 276 | DCHECK_GE(index, 0); |
| 277 | return branch_targets_.Get(index); |
| 278 | } |
| 279 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 280 | template<typename T> |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 281 | void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 282 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 283 | HInstruction* second = LoadLocal(instruction.VRegC(), type); |
| 284 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 285 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 286 | } |
| 287 | |
| 288 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 289 | void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) { |
| 290 | HInstruction* first = LoadLocal(instruction.VRegA(), type); |
| 291 | HInstruction* second = LoadLocal(instruction.VRegB(), type); |
| 292 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 293 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 294 | } |
| 295 | |
| 296 | template<typename T> |
| 297 | void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 298 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 299 | HInstruction* second = GetIntConstant(instruction.VRegC_22s()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 300 | if (reverse) { |
| 301 | std::swap(first, second); |
| 302 | } |
| 303 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 304 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 305 | } |
| 306 | |
| 307 | template<typename T> |
| 308 | void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 309 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 310 | HInstruction* second = GetIntConstant(instruction.VRegC_22b()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 311 | if (reverse) { |
| 312 | std::swap(first, second); |
| 313 | } |
| 314 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 315 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 316 | } |
| 317 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 318 | void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) { |
| 319 | if (type == Primitive::kPrimVoid) { |
| 320 | current_block_->AddInstruction(new (arena_) HReturnVoid()); |
| 321 | } else { |
| 322 | HInstruction* value = LoadLocal(instruction.VRegA(), type); |
| 323 | current_block_->AddInstruction(new (arena_) HReturn(value)); |
| 324 | } |
| 325 | current_block_->AddSuccessor(exit_block_); |
| 326 | current_block_ = nullptr; |
| 327 | } |
| 328 | |
| 329 | bool HGraphBuilder::BuildInvoke(const Instruction& instruction, |
| 330 | uint32_t dex_offset, |
| 331 | uint32_t method_idx, |
| 332 | uint32_t number_of_vreg_arguments, |
| 333 | bool is_range, |
| 334 | uint32_t* args, |
| 335 | uint32_t register_index) { |
| 336 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 337 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_); |
| 338 | const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_); |
| 339 | Primitive::Type return_type = Primitive::GetType(descriptor[0]); |
| 340 | bool is_instance_call = |
| 341 | instruction.Opcode() != Instruction::INVOKE_STATIC |
| 342 | && instruction.Opcode() != Instruction::INVOKE_STATIC_RANGE; |
| 343 | const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1); |
| 344 | |
| 345 | // Treat invoke-direct like static calls for now. |
| 346 | HInvoke* invoke = new (arena_) HInvokeStatic( |
| 347 | arena_, number_of_arguments, return_type, dex_offset, method_idx); |
| 348 | |
| 349 | size_t start_index = 0; |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 350 | Temporaries temps(graph_, is_instance_call ? 1 : 0); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 351 | if (is_instance_call) { |
| 352 | HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot); |
Nicolas Geoffray | f12feb8 | 2014-07-17 18:32:41 +0100 | [diff] [blame] | 353 | HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset); |
| 354 | current_block_->AddInstruction(null_check); |
| 355 | temps.Add(null_check); |
| 356 | invoke->SetArgumentAt(0, null_check); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 357 | start_index = 1; |
| 358 | } |
| 359 | |
| 360 | uint32_t descriptor_index = 1; |
| 361 | uint32_t argument_index = start_index; |
| 362 | for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) { |
| 363 | Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]); |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 364 | if (!IsTypeSupported(type)) { |
| 365 | return false; |
| 366 | } |
| 367 | if (!is_range && type == Primitive::kPrimLong && args[i] + 1 != args[i + 1]) { |
| 368 | LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol() |
| 369 | << " at " << dex_offset; |
| 370 | // We do not implement non sequential register pair. |
| 371 | return false; |
| 372 | } |
| 373 | HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type); |
| 374 | invoke->SetArgumentAt(argument_index, arg); |
| 375 | if (type == Primitive::kPrimLong) { |
| 376 | i++; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 380 | if (!IsTypeSupported(return_type)) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 384 | DCHECK_EQ(argument_index, number_of_arguments); |
| 385 | current_block_->AddInstruction(invoke); |
| 386 | return true; |
| 387 | } |
| 388 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 389 | bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction, |
| 390 | uint32_t dex_offset, |
| 391 | bool is_put) { |
| 392 | uint32_t source_or_dest_reg = instruction.VRegA_22c(); |
| 393 | uint32_t obj_reg = instruction.VRegB_22c(); |
| 394 | uint16_t field_index = instruction.VRegC_22c(); |
| 395 | |
| 396 | ScopedObjectAccess soa(Thread::Current()); |
| 397 | StackHandleScope<1> hs(soa.Self()); |
| 398 | Handle<mirror::ArtField> resolved_field(hs.NewHandle( |
| 399 | compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa))); |
| 400 | |
| 401 | if (resolved_field.Get() == nullptr) { |
| 402 | return false; |
| 403 | } |
| 404 | if (resolved_field->IsVolatile()) { |
| 405 | return false; |
| 406 | } |
| 407 | |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 408 | Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); |
| 409 | if (!IsTypeSupported(field_type)) { |
| 410 | return false; |
| 411 | } |
| 412 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 413 | HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot); |
| 414 | current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset)); |
| 415 | if (is_put) { |
| 416 | Temporaries temps(graph_, 1); |
| 417 | HInstruction* null_check = current_block_->GetLastInstruction(); |
| 418 | // We need one temporary for the null check. |
| 419 | temps.Add(null_check); |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 420 | HInstruction* value = LoadLocal(source_or_dest_reg, field_type); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 421 | current_block_->AddInstruction(new (arena_) HInstanceFieldSet( |
| 422 | null_check, |
| 423 | value, |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 424 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 425 | resolved_field->GetOffset())); |
| 426 | } else { |
| 427 | current_block_->AddInstruction(new (arena_) HInstanceFieldGet( |
| 428 | current_block_->GetLastInstruction(), |
Nicolas Geoffray | abed4d0 | 2014-07-14 15:24:11 +0100 | [diff] [blame] | 429 | field_type, |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 430 | resolved_field->GetOffset())); |
| 431 | |
| 432 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 433 | } |
| 434 | return true; |
| 435 | } |
| 436 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 437 | void HGraphBuilder::BuildArrayAccess(const Instruction& instruction, |
| 438 | uint32_t dex_offset, |
| 439 | bool is_put, |
| 440 | Primitive::Type anticipated_type) { |
| 441 | uint8_t source_or_dest_reg = instruction.VRegA_23x(); |
| 442 | uint8_t array_reg = instruction.VRegB_23x(); |
| 443 | uint8_t index_reg = instruction.VRegC_23x(); |
| 444 | |
| 445 | DCHECK(IsTypeSupported(anticipated_type)); |
| 446 | |
| 447 | // We need one temporary for the null check, one for the index, and one for the length. |
| 448 | Temporaries temps(graph_, 3); |
| 449 | |
| 450 | HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot); |
| 451 | object = new (arena_) HNullCheck(object, dex_offset); |
| 452 | current_block_->AddInstruction(object); |
| 453 | temps.Add(object); |
| 454 | |
| 455 | HInstruction* length = new (arena_) HArrayLength(object); |
| 456 | current_block_->AddInstruction(length); |
| 457 | temps.Add(length); |
| 458 | HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt); |
| 459 | index = new (arena_) HBoundsCheck(index, length, dex_offset); |
| 460 | current_block_->AddInstruction(index); |
| 461 | temps.Add(index); |
| 462 | if (is_put) { |
| 463 | HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type); |
| 464 | // TODO: Insert a type check node if the type is Object. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 465 | current_block_->AddInstruction(new (arena_) HArraySet( |
| 466 | object, index, value, anticipated_type, dex_offset)); |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 467 | } else { |
| 468 | current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type)); |
| 469 | UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); |
| 470 | } |
| 471 | } |
| 472 | |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 473 | void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) { |
| 474 | if (target_offset <= 0) { |
| 475 | // Unconditionnally add a suspend check to backward branches. We can remove |
| 476 | // them after we recognize loops in the graph. |
| 477 | current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 482 | if (current_block_ == nullptr) { |
| 483 | return true; // Dead code |
| 484 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 485 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 486 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 487 | case Instruction::CONST_4: { |
| 488 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 489 | HIntConstant* constant = GetIntConstant(instruction.VRegB_11n()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 490 | UpdateLocal(register_index, constant); |
| 491 | break; |
| 492 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 493 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 494 | case Instruction::CONST_16: { |
| 495 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 496 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21s()); |
| 497 | UpdateLocal(register_index, constant); |
| 498 | break; |
| 499 | } |
| 500 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 501 | case Instruction::CONST: { |
| 502 | int32_t register_index = instruction.VRegA(); |
| 503 | HIntConstant* constant = GetIntConstant(instruction.VRegB_31i()); |
| 504 | UpdateLocal(register_index, constant); |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | case Instruction::CONST_HIGH16: { |
| 509 | int32_t register_index = instruction.VRegA(); |
| 510 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16); |
| 511 | UpdateLocal(register_index, constant); |
| 512 | break; |
| 513 | } |
| 514 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 515 | case Instruction::CONST_WIDE_16: { |
| 516 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 517 | // Get 16 bits of constant value, sign extended to 64 bits. |
| 518 | int64_t value = instruction.VRegB_21s(); |
| 519 | value <<= 48; |
| 520 | value >>= 48; |
| 521 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 522 | UpdateLocal(register_index, constant); |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | case Instruction::CONST_WIDE_32: { |
| 527 | int32_t register_index = instruction.VRegA(); |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 528 | // Get 32 bits of constant value, sign extended to 64 bits. |
| 529 | int64_t value = instruction.VRegB_31i(); |
| 530 | value <<= 32; |
| 531 | value >>= 32; |
| 532 | HLongConstant* constant = GetLongConstant(value); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 533 | UpdateLocal(register_index, constant); |
| 534 | break; |
| 535 | } |
| 536 | |
| 537 | case Instruction::CONST_WIDE: { |
| 538 | int32_t register_index = instruction.VRegA(); |
| 539 | HLongConstant* constant = GetLongConstant(instruction.VRegB_51l()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 540 | UpdateLocal(register_index, constant); |
| 541 | break; |
| 542 | } |
| 543 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 544 | case Instruction::CONST_WIDE_HIGH16: { |
| 545 | int32_t register_index = instruction.VRegA(); |
| 546 | int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48; |
| 547 | HLongConstant* constant = GetLongConstant(value); |
| 548 | UpdateLocal(register_index, constant); |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | // TODO: these instructions are also used to move floating point values, so what is |
| 553 | // the type (int or float)? |
| 554 | case Instruction::MOVE: |
| 555 | case Instruction::MOVE_FROM16: |
| 556 | case Instruction::MOVE_16: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 557 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 558 | UpdateLocal(instruction.VRegA(), value); |
| 559 | break; |
| 560 | } |
| 561 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 562 | // TODO: these instructions are also used to move floating point values, so what is |
| 563 | // the type (long or double)? |
| 564 | case Instruction::MOVE_WIDE: |
| 565 | case Instruction::MOVE_WIDE_FROM16: |
| 566 | case Instruction::MOVE_WIDE_16: { |
| 567 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong); |
| 568 | UpdateLocal(instruction.VRegA(), value); |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | case Instruction::MOVE_OBJECT: |
| 573 | case Instruction::MOVE_OBJECT_16: |
| 574 | case Instruction::MOVE_OBJECT_FROM16: { |
| 575 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot); |
| 576 | UpdateLocal(instruction.VRegA(), value); |
| 577 | break; |
| 578 | } |
| 579 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 580 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 581 | BuildReturn(instruction, Primitive::kPrimVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 582 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 585 | #define IF_XX(comparison, cond) \ |
| 586 | case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \ |
| 587 | case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 588 | |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 589 | IF_XX(HEqual, EQ); |
| 590 | IF_XX(HNotEqual, NE); |
| 591 | IF_XX(HLessThan, LT); |
| 592 | IF_XX(HLessThanOrEqual, LE); |
| 593 | IF_XX(HGreaterThan, GT); |
| 594 | IF_XX(HGreaterThanOrEqual, GE); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 595 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 596 | case Instruction::GOTO: |
| 597 | case Instruction::GOTO_16: |
| 598 | case Instruction::GOTO_32: { |
Nicolas Geoffray | 6fbce02 | 2014-09-10 10:23:58 +0100 | [diff] [blame^] | 599 | int32_t offset = instruction.GetTargetOffset(); |
| 600 | PotentiallyAddSuspendCheck(offset, dex_offset); |
| 601 | HBasicBlock* target = FindBlockStartingAt(offset + dex_offset); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 602 | DCHECK(target != nullptr); |
| 603 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 604 | current_block_->AddSuccessor(target); |
| 605 | current_block_ = nullptr; |
| 606 | break; |
| 607 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 608 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 609 | case Instruction::RETURN: { |
| 610 | BuildReturn(instruction, Primitive::kPrimInt); |
| 611 | break; |
| 612 | } |
| 613 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 614 | case Instruction::RETURN_OBJECT: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 615 | BuildReturn(instruction, Primitive::kPrimNot); |
| 616 | break; |
| 617 | } |
| 618 | |
| 619 | case Instruction::RETURN_WIDE: { |
| 620 | BuildReturn(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 624 | case Instruction::INVOKE_STATIC: |
| 625 | case Instruction::INVOKE_DIRECT: { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 626 | uint32_t method_idx = instruction.VRegB_35c(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 627 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 628 | uint32_t args[5]; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 629 | instruction.GetVarArgs(args); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 630 | if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) { |
| 631 | return false; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 632 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 636 | case Instruction::INVOKE_STATIC_RANGE: |
| 637 | case Instruction::INVOKE_DIRECT_RANGE: { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 638 | uint32_t method_idx = instruction.VRegB_3rc(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 639 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 640 | uint32_t register_index = instruction.VRegC(); |
| 641 | if (!BuildInvoke(instruction, dex_offset, method_idx, |
| 642 | number_of_vreg_arguments, true, nullptr, register_index)) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 643 | return false; |
| 644 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 645 | break; |
| 646 | } |
| 647 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 648 | case Instruction::ADD_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 649 | Binop_23x<HAdd>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | |
| 653 | case Instruction::ADD_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 654 | Binop_23x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | |
| 658 | case Instruction::SUB_INT: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 659 | Binop_23x<HSub>(instruction, Primitive::kPrimInt); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | |
| 663 | case Instruction::SUB_LONG: { |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 664 | Binop_23x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 665 | break; |
| 666 | } |
| 667 | |
| 668 | case Instruction::ADD_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 669 | Binop_12x<HAdd>(instruction, Primitive::kPrimInt); |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | case Instruction::ADD_LONG_2ADDR: { |
| 674 | Binop_12x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 675 | break; |
| 676 | } |
| 677 | |
| 678 | case Instruction::SUB_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 679 | Binop_12x<HSub>(instruction, Primitive::kPrimInt); |
| 680 | break; |
| 681 | } |
| 682 | |
| 683 | case Instruction::SUB_LONG_2ADDR: { |
| 684 | Binop_12x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | |
| 688 | case Instruction::ADD_INT_LIT16: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 689 | Binop_22s<HAdd>(instruction, false); |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | case Instruction::RSUB_INT: { |
| 694 | Binop_22s<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
| 698 | case Instruction::ADD_INT_LIT8: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 699 | Binop_22b<HAdd>(instruction, false); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | case Instruction::RSUB_INT_LIT8: { |
| 704 | Binop_22b<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 705 | break; |
| 706 | } |
| 707 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 708 | case Instruction::NEW_INSTANCE: { |
| 709 | current_block_->AddInstruction( |
| 710 | new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c())); |
| 711 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 712 | break; |
| 713 | } |
| 714 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 715 | case Instruction::MOVE_RESULT: |
Dave Allison | 20dfc79 | 2014-06-16 20:44:29 -0700 | [diff] [blame] | 716 | case Instruction::MOVE_RESULT_WIDE: |
| 717 | case Instruction::MOVE_RESULT_OBJECT: |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 718 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 719 | break; |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 720 | |
Nicolas Geoffray | 412f10c | 2014-06-19 10:00:34 +0100 | [diff] [blame] | 721 | case Instruction::CMP_LONG: { |
| 722 | Binop_23x<HCompare>(instruction, Primitive::kPrimLong); |
| 723 | break; |
| 724 | } |
| 725 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 726 | case Instruction::NOP: |
| 727 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 728 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 729 | case Instruction::IGET: |
| 730 | case Instruction::IGET_WIDE: |
| 731 | case Instruction::IGET_OBJECT: |
| 732 | case Instruction::IGET_BOOLEAN: |
| 733 | case Instruction::IGET_BYTE: |
| 734 | case Instruction::IGET_CHAR: |
| 735 | case Instruction::IGET_SHORT: { |
| 736 | if (!BuildFieldAccess(instruction, dex_offset, false)) { |
| 737 | return false; |
| 738 | } |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | case Instruction::IPUT: |
| 743 | case Instruction::IPUT_WIDE: |
| 744 | case Instruction::IPUT_OBJECT: |
| 745 | case Instruction::IPUT_BOOLEAN: |
| 746 | case Instruction::IPUT_BYTE: |
| 747 | case Instruction::IPUT_CHAR: |
| 748 | case Instruction::IPUT_SHORT: { |
| 749 | if (!BuildFieldAccess(instruction, dex_offset, true)) { |
| 750 | return false; |
| 751 | } |
| 752 | break; |
| 753 | } |
| 754 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 755 | #define ARRAY_XX(kind, anticipated_type) \ |
| 756 | case Instruction::AGET##kind: { \ |
| 757 | BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \ |
| 758 | break; \ |
| 759 | } \ |
| 760 | case Instruction::APUT##kind: { \ |
| 761 | BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \ |
| 762 | break; \ |
| 763 | } |
| 764 | |
| 765 | ARRAY_XX(, Primitive::kPrimInt); |
| 766 | ARRAY_XX(_WIDE, Primitive::kPrimLong); |
| 767 | ARRAY_XX(_OBJECT, Primitive::kPrimNot); |
| 768 | ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean); |
| 769 | ARRAY_XX(_BYTE, Primitive::kPrimByte); |
| 770 | ARRAY_XX(_CHAR, Primitive::kPrimChar); |
| 771 | ARRAY_XX(_SHORT, Primitive::kPrimShort); |
| 772 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 773 | case Instruction::ARRAY_LENGTH: { |
| 774 | HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot); |
| 775 | current_block_->AddInstruction(new (arena_) HArrayLength(object)); |
| 776 | UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction()); |
| 777 | break; |
| 778 | } |
| 779 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 780 | default: |
| 781 | return false; |
| 782 | } |
| 783 | return true; |
| 784 | } |
| 785 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 786 | HIntConstant* HGraphBuilder::GetIntConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 787 | if (constant0_ != nullptr) { |
| 788 | return constant0_; |
| 789 | } |
| 790 | constant0_ = new(arena_) HIntConstant(0); |
| 791 | entry_block_->AddInstruction(constant0_); |
| 792 | return constant0_; |
| 793 | } |
| 794 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 795 | HIntConstant* HGraphBuilder::GetIntConstant1() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 796 | if (constant1_ != nullptr) { |
| 797 | return constant1_; |
| 798 | } |
| 799 | constant1_ = new(arena_) HIntConstant(1); |
| 800 | entry_block_->AddInstruction(constant1_); |
| 801 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 804 | HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 805 | switch (constant) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 806 | case 0: return GetIntConstant0(); |
| 807 | case 1: return GetIntConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 808 | default: { |
| 809 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 810 | entry_block_->AddInstruction(instruction); |
| 811 | return instruction; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 816 | HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) { |
| 817 | HLongConstant* instruction = new (arena_) HLongConstant(constant); |
| 818 | entry_block_->AddInstruction(instruction); |
| 819 | return instruction; |
| 820 | } |
| 821 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 822 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 823 | return locals_.Get(register_index); |
| 824 | } |
| 825 | |
| 826 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 827 | HLocal* local = GetLocalAt(register_index); |
| 828 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 829 | } |
| 830 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 831 | HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 832 | HLocal* local = GetLocalAt(register_index); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 833 | current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 834 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 837 | } // namespace art |