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