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