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 | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 18 | #include "dex_file.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 19 | #include "dex_file-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 20 | #include "dex_instruction.h" |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 21 | #include "dex_instruction-inl.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 22 | #include "builder.h" |
| 23 | #include "nodes.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 24 | #include "primitive.h" |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 28 | void HGraphBuilder::InitializeLocals(uint16_t count) { |
| 29 | graph_->SetNumberOfVRegs(count); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 30 | locals_.SetSize(count); |
| 31 | for (int i = 0; i < count; i++) { |
| 32 | HLocal* local = new (arena_) HLocal(i); |
| 33 | entry_block_->AddInstruction(local); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 34 | locals_.Put(i, local); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 38 | bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) { |
| 39 | // dex_compilation_unit_ is null only when unit testing. |
| 40 | if (dex_compilation_unit_ == nullptr) { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | graph_->SetNumberOfInVRegs(number_of_parameters); |
| 45 | const char* shorty = dex_compilation_unit_->GetShorty(); |
| 46 | int locals_index = locals_.Size() - number_of_parameters; |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 47 | int parameter_index = 0; |
| 48 | |
| 49 | if (!dex_compilation_unit_->IsStatic()) { |
| 50 | // Add the implicit 'this' argument, not expressed in the signature. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 51 | HParameterValue* parameter = |
| 52 | new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 53 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 54 | HLocal* local = GetLocalAt(locals_index++); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 55 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 56 | number_of_parameters--; |
| 57 | } |
| 58 | |
| 59 | uint32_t pos = 1; |
| 60 | for (int i = 0; i < number_of_parameters; i++) { |
| 61 | switch (shorty[pos++]) { |
| 62 | case 'F': |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 63 | case 'D': { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| 67 | default: { |
| 68 | // integer and reference parameters. |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 69 | HParameterValue* parameter = |
| 70 | new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos - 1])); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 71 | entry_block_->AddInstruction(parameter); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 72 | HLocal* local = GetLocalAt(locals_index++); |
| 73 | // Store the parameter value in the local that the dex code will use |
| 74 | // to reference that parameter. |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 75 | entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 76 | if (parameter->GetType() == Primitive::kPrimLong) { |
| 77 | i++; |
| 78 | locals_index++; |
| 79 | parameter_index++; |
| 80 | } |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 88 | static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 89 | if (code_item.tries_size_ > 0) { |
| 90 | return false; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 91 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 95 | template<typename T> |
| 96 | void HGraphBuilder::If_22t(const Instruction& instruction, int32_t dex_offset, bool is_not) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 97 | HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); |
| 98 | HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 99 | current_block_->AddInstruction(new (arena_) T(first, second)); |
| 100 | if (is_not) { |
| 101 | current_block_->AddInstruction(new (arena_) HNot(current_block_->GetLastInstruction())); |
| 102 | } |
| 103 | current_block_->AddInstruction(new (arena_) HIf(current_block_->GetLastInstruction())); |
| 104 | HBasicBlock* target = FindBlockStartingAt(instruction.GetTargetOffset() + dex_offset); |
| 105 | DCHECK(target != nullptr); |
| 106 | current_block_->AddSuccessor(target); |
| 107 | target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits()); |
| 108 | DCHECK(target != nullptr); |
| 109 | current_block_->AddSuccessor(target); |
| 110 | current_block_ = nullptr; |
| 111 | } |
| 112 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 113 | HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 114 | if (!CanHandleCodeItem(code_item)) { |
| 115 | return nullptr; |
| 116 | } |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 117 | |
| 118 | const uint16_t* code_ptr = code_item.insns_; |
| 119 | const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; |
| 120 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 121 | // Setup the graph with the entry block and exit block. |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 122 | graph_ = new (arena_) HGraph(arena_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 123 | entry_block_ = new (arena_) HBasicBlock(graph_); |
| 124 | graph_->AddBlock(entry_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 125 | exit_block_ = new (arena_) HBasicBlock(graph_); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 126 | graph_->SetEntryBlock(entry_block_); |
| 127 | graph_->SetExitBlock(exit_block_); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 128 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 129 | InitializeLocals(code_item.registers_size_); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 130 | graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 131 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 132 | // To avoid splitting blocks, we compute ahead of time the instructions that |
| 133 | // start a new block, and create these blocks. |
| 134 | ComputeBranchTargets(code_ptr, code_end); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 135 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 136 | if (!InitializeParameters(code_item.ins_size_)) { |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 140 | size_t dex_offset = 0; |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 141 | while (code_ptr < code_end) { |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 142 | // Update the current block if dex_offset starts a new block. |
| 143 | MaybeUpdateCurrentBlock(dex_offset); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 144 | const Instruction& instruction = *Instruction::At(code_ptr); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 145 | if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr; |
| 146 | dex_offset += instruction.SizeInCodeUnits(); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 147 | code_ptr += instruction.SizeInCodeUnits(); |
| 148 | } |
| 149 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 150 | // 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] | 151 | graph_->AddBlock(exit_block_); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 152 | exit_block_->AddInstruction(new (arena_) HExit()); |
| 153 | entry_block_->AddInstruction(new (arena_) HGoto()); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 154 | return graph_; |
| 155 | } |
| 156 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 157 | void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { |
| 158 | HBasicBlock* block = FindBlockStartingAt(index); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 159 | if (block == nullptr) { |
| 160 | return; |
| 161 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 162 | |
| 163 | if (current_block_ != nullptr) { |
| 164 | // Branching instructions clear current_block, so we know |
| 165 | // the last instruction of the current block is not a branching |
| 166 | // instruction. We add an unconditional goto to the found block. |
| 167 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 168 | current_block_->AddSuccessor(block); |
| 169 | } |
| 170 | graph_->AddBlock(block); |
| 171 | current_block_ = block; |
| 172 | } |
| 173 | |
| 174 | void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) { |
| 175 | // TODO: Support switch instructions. |
| 176 | branch_targets_.SetSize(code_end - code_ptr); |
| 177 | |
| 178 | // Create the first block for the dex instructions, single successor of the entry block. |
| 179 | HBasicBlock* block = new (arena_) HBasicBlock(graph_); |
| 180 | branch_targets_.Put(0, block); |
| 181 | entry_block_->AddSuccessor(block); |
| 182 | |
| 183 | // Iterate over all instructions and find branching instructions. Create blocks for |
| 184 | // the locations these instructions branch to. |
| 185 | size_t dex_offset = 0; |
| 186 | while (code_ptr < code_end) { |
| 187 | const Instruction& instruction = *Instruction::At(code_ptr); |
| 188 | if (instruction.IsBranch()) { |
| 189 | int32_t target = instruction.GetTargetOffset() + dex_offset; |
| 190 | // Create a block for the target instruction. |
| 191 | if (FindBlockStartingAt(target) == nullptr) { |
| 192 | block = new (arena_) HBasicBlock(graph_); |
| 193 | branch_targets_.Put(target, block); |
| 194 | } |
| 195 | dex_offset += instruction.SizeInCodeUnits(); |
| 196 | code_ptr += instruction.SizeInCodeUnits(); |
| 197 | if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) { |
| 198 | block = new (arena_) HBasicBlock(graph_); |
| 199 | branch_targets_.Put(dex_offset, block); |
| 200 | } |
| 201 | } else { |
| 202 | code_ptr += instruction.SizeInCodeUnits(); |
| 203 | dex_offset += instruction.SizeInCodeUnits(); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { |
| 209 | DCHECK_GE(index, 0); |
| 210 | return branch_targets_.Get(index); |
| 211 | } |
| 212 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 213 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 214 | void HGraphBuilder::Binop_32x(const Instruction& instruction, Primitive::Type type) { |
| 215 | HInstruction* first = LoadLocal(instruction.VRegB(), type); |
| 216 | HInstruction* second = LoadLocal(instruction.VRegC(), type); |
| 217 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 218 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 219 | } |
| 220 | |
| 221 | template<typename T> |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 222 | void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) { |
| 223 | HInstruction* first = LoadLocal(instruction.VRegA(), type); |
| 224 | HInstruction* second = LoadLocal(instruction.VRegB(), type); |
| 225 | current_block_->AddInstruction(new (arena_) T(type, first, second)); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 226 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 227 | } |
| 228 | |
| 229 | template<typename T> |
| 230 | void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 231 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 232 | HInstruction* second = GetIntConstant(instruction.VRegC_22s()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 233 | if (reverse) { |
| 234 | std::swap(first, second); |
| 235 | } |
| 236 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 237 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 238 | } |
| 239 | |
| 240 | template<typename T> |
| 241 | void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 242 | HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
| 243 | HInstruction* second = GetIntConstant(instruction.VRegC_22b()); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 244 | if (reverse) { |
| 245 | std::swap(first, second); |
| 246 | } |
| 247 | current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); |
| 248 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 249 | } |
| 250 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 251 | void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) { |
| 252 | if (type == Primitive::kPrimVoid) { |
| 253 | current_block_->AddInstruction(new (arena_) HReturnVoid()); |
| 254 | } else { |
| 255 | HInstruction* value = LoadLocal(instruction.VRegA(), type); |
| 256 | current_block_->AddInstruction(new (arena_) HReturn(value)); |
| 257 | } |
| 258 | current_block_->AddSuccessor(exit_block_); |
| 259 | current_block_ = nullptr; |
| 260 | } |
| 261 | |
| 262 | bool HGraphBuilder::BuildInvoke(const Instruction& instruction, |
| 263 | uint32_t dex_offset, |
| 264 | uint32_t method_idx, |
| 265 | uint32_t number_of_vreg_arguments, |
| 266 | bool is_range, |
| 267 | uint32_t* args, |
| 268 | uint32_t register_index) { |
| 269 | const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); |
| 270 | const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_); |
| 271 | const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_); |
| 272 | Primitive::Type return_type = Primitive::GetType(descriptor[0]); |
| 273 | bool is_instance_call = |
| 274 | instruction.Opcode() != Instruction::INVOKE_STATIC |
| 275 | && instruction.Opcode() != Instruction::INVOKE_STATIC_RANGE; |
| 276 | const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1); |
| 277 | |
| 278 | // Treat invoke-direct like static calls for now. |
| 279 | HInvoke* invoke = new (arena_) HInvokeStatic( |
| 280 | arena_, number_of_arguments, return_type, dex_offset, method_idx); |
| 281 | |
| 282 | size_t start_index = 0; |
| 283 | if (is_instance_call) { |
| 284 | HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 285 | invoke->SetArgumentAt(0, arg); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 286 | start_index = 1; |
| 287 | } |
| 288 | |
| 289 | uint32_t descriptor_index = 1; |
| 290 | uint32_t argument_index = start_index; |
| 291 | for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) { |
| 292 | Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]); |
| 293 | switch (type) { |
| 294 | case Primitive::kPrimFloat: |
| 295 | case Primitive::kPrimDouble: |
| 296 | return false; |
| 297 | |
| 298 | default: { |
| 299 | if (!is_range && type == Primitive::kPrimLong && args[i] + 1 != args[i + 1]) { |
| 300 | LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol() |
| 301 | << " at " << dex_offset; |
| 302 | // We do not implement non sequential register pair. |
| 303 | return false; |
| 304 | } |
| 305 | HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type); |
Nicolas Geoffray | db928fc | 2014-04-16 17:38:32 +0100 | [diff] [blame] | 306 | invoke->SetArgumentAt(argument_index, arg); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 307 | if (type == Primitive::kPrimLong) { |
| 308 | i++; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 314 | if (return_type == Primitive::kPrimDouble || return_type == Primitive::kPrimFloat) { |
| 315 | return false; |
| 316 | } |
| 317 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 318 | DCHECK_EQ(argument_index, number_of_arguments); |
| 319 | current_block_->AddInstruction(invoke); |
| 320 | return true; |
| 321 | } |
| 322 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 323 | bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, int32_t dex_offset) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 324 | if (current_block_ == nullptr) { |
| 325 | return true; // Dead code |
| 326 | } |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 327 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 328 | switch (instruction.Opcode()) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 329 | case Instruction::CONST_4: { |
| 330 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 331 | HIntConstant* constant = GetIntConstant(instruction.VRegB_11n()); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 332 | UpdateLocal(register_index, constant); |
| 333 | break; |
| 334 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 335 | |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 336 | case Instruction::CONST_16: { |
| 337 | int32_t register_index = instruction.VRegA(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 338 | HIntConstant* constant = GetIntConstant(instruction.VRegB_21s()); |
| 339 | UpdateLocal(register_index, constant); |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | case Instruction::CONST_WIDE_16: { |
| 344 | int32_t register_index = instruction.VRegA(); |
| 345 | HLongConstant* constant = GetLongConstant(instruction.VRegB_21s()); |
| 346 | UpdateLocal(register_index, constant); |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | case Instruction::CONST_WIDE_32: { |
| 351 | int32_t register_index = instruction.VRegA(); |
| 352 | HLongConstant* constant = GetLongConstant(instruction.VRegB_31i()); |
| 353 | UpdateLocal(register_index, constant); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | case Instruction::CONST_WIDE: { |
| 358 | int32_t register_index = instruction.VRegA(); |
| 359 | HLongConstant* constant = GetLongConstant(instruction.VRegB_51l()); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 360 | UpdateLocal(register_index, constant); |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | case Instruction::MOVE: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 365 | HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 366 | UpdateLocal(instruction.VRegA(), value); |
| 367 | break; |
| 368 | } |
| 369 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 370 | case Instruction::RETURN_VOID: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 371 | BuildReturn(instruction, Primitive::kPrimVoid); |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 372 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 375 | case Instruction::IF_EQ: { |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 376 | If_22t<HEqual>(instruction, dex_offset, false); |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | case Instruction::IF_NE: { |
| 381 | If_22t<HEqual>(instruction, dex_offset, true); |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 382 | break; |
| 383 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 384 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 385 | case Instruction::GOTO: |
| 386 | case Instruction::GOTO_16: |
| 387 | case Instruction::GOTO_32: { |
| 388 | HBasicBlock* target = FindBlockStartingAt(instruction.GetTargetOffset() + dex_offset); |
| 389 | DCHECK(target != nullptr); |
| 390 | current_block_->AddInstruction(new (arena_) HGoto()); |
| 391 | current_block_->AddSuccessor(target); |
| 392 | current_block_ = nullptr; |
| 393 | break; |
| 394 | } |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 395 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 396 | case Instruction::RETURN: { |
| 397 | BuildReturn(instruction, Primitive::kPrimInt); |
| 398 | break; |
| 399 | } |
| 400 | |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 401 | case Instruction::RETURN_OBJECT: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 402 | BuildReturn(instruction, Primitive::kPrimNot); |
| 403 | break; |
| 404 | } |
| 405 | |
| 406 | case Instruction::RETURN_WIDE: { |
| 407 | BuildReturn(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 408 | break; |
| 409 | } |
| 410 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 411 | case Instruction::INVOKE_STATIC: |
| 412 | case Instruction::INVOKE_DIRECT: { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 413 | uint32_t method_idx = instruction.VRegB_35c(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 414 | uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 415 | uint32_t args[5]; |
Ian Rogers | 29a2648 | 2014-05-02 15:27:29 -0700 | [diff] [blame] | 416 | instruction.GetVarArgs(args); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 417 | if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) { |
| 418 | return false; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 419 | } |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 420 | break; |
| 421 | } |
| 422 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 423 | case Instruction::INVOKE_STATIC_RANGE: |
| 424 | case Instruction::INVOKE_DIRECT_RANGE: { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 425 | uint32_t method_idx = instruction.VRegB_3rc(); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 426 | uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); |
| 427 | uint32_t register_index = instruction.VRegC(); |
| 428 | if (!BuildInvoke(instruction, dex_offset, method_idx, |
| 429 | number_of_vreg_arguments, true, nullptr, register_index)) { |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 430 | return false; |
| 431 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 435 | case Instruction::ADD_INT: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 436 | Binop_32x<HAdd>(instruction, Primitive::kPrimInt); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | case Instruction::ADD_LONG: { |
| 441 | Binop_32x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | |
| 445 | case Instruction::SUB_INT: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 446 | Binop_32x<HSub>(instruction, Primitive::kPrimInt); |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | case Instruction::SUB_LONG: { |
| 451 | Binop_32x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 452 | break; |
| 453 | } |
| 454 | |
| 455 | case Instruction::ADD_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 456 | Binop_12x<HAdd>(instruction, Primitive::kPrimInt); |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | case Instruction::ADD_LONG_2ADDR: { |
| 461 | Binop_12x<HAdd>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | |
| 465 | case Instruction::SUB_INT_2ADDR: { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 466 | Binop_12x<HSub>(instruction, Primitive::kPrimInt); |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | case Instruction::SUB_LONG_2ADDR: { |
| 471 | Binop_12x<HSub>(instruction, Primitive::kPrimLong); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | |
| 475 | case Instruction::ADD_INT_LIT16: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 476 | Binop_22s<HAdd>(instruction, false); |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | case Instruction::RSUB_INT: { |
| 481 | Binop_22s<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | |
| 485 | case Instruction::ADD_INT_LIT8: { |
Nicolas Geoffray | f583e59 | 2014-04-07 13:20:42 +0100 | [diff] [blame] | 486 | Binop_22b<HAdd>(instruction, false); |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | case Instruction::RSUB_INT_LIT8: { |
| 491 | Binop_22b<HSub>(instruction, true); |
Nicolas Geoffray | d8ee737 | 2014-03-28 15:43:40 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
| 494 | |
Nicolas Geoffray | 2e7038a | 2014-04-03 18:49:58 +0100 | [diff] [blame] | 495 | case Instruction::NEW_INSTANCE: { |
| 496 | current_block_->AddInstruction( |
| 497 | new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c())); |
| 498 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 499 | break; |
| 500 | } |
| 501 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 502 | case Instruction::MOVE_RESULT_WIDE: { |
| 503 | UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); |
| 504 | break; |
| 505 | } |
| 506 | |
Nicolas Geoffray | be9a92a | 2014-02-25 14:22:56 +0000 | [diff] [blame] | 507 | case Instruction::NOP: |
| 508 | break; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 509 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 510 | default: |
| 511 | return false; |
| 512 | } |
| 513 | return true; |
| 514 | } |
| 515 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 516 | HIntConstant* HGraphBuilder::GetIntConstant0() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 517 | if (constant0_ != nullptr) { |
| 518 | return constant0_; |
| 519 | } |
| 520 | constant0_ = new(arena_) HIntConstant(0); |
| 521 | entry_block_->AddInstruction(constant0_); |
| 522 | return constant0_; |
| 523 | } |
| 524 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 525 | HIntConstant* HGraphBuilder::GetIntConstant1() { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 526 | if (constant1_ != nullptr) { |
| 527 | return constant1_; |
| 528 | } |
| 529 | constant1_ = new(arena_) HIntConstant(1); |
| 530 | entry_block_->AddInstruction(constant1_); |
| 531 | return constant1_; |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 534 | HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 535 | switch (constant) { |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 536 | case 0: return GetIntConstant0(); |
| 537 | case 1: return GetIntConstant1(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 538 | default: { |
| 539 | HIntConstant* instruction = new (arena_) HIntConstant(constant); |
| 540 | entry_block_->AddInstruction(instruction); |
| 541 | return instruction; |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 546 | HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) { |
| 547 | HLongConstant* instruction = new (arena_) HLongConstant(constant); |
| 548 | entry_block_->AddInstruction(instruction); |
| 549 | return instruction; |
| 550 | } |
| 551 | |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 552 | HLocal* HGraphBuilder::GetLocalAt(int register_index) const { |
| 553 | return locals_.Get(register_index); |
| 554 | } |
| 555 | |
| 556 | void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { |
| 557 | HLocal* local = GetLocalAt(register_index); |
| 558 | current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); |
| 559 | } |
| 560 | |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 561 | HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 562 | HLocal* local = GetLocalAt(register_index); |
Nicolas Geoffray | 01bc96d | 2014-04-11 17:43:50 +0100 | [diff] [blame] | 563 | current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 564 | return current_block_->GetLastInstruction(); |
Nicolas Geoffray | 3ff386a | 2014-03-04 14:46:47 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Nicolas Geoffray | 818f210 | 2014-02-18 16:43:35 +0000 | [diff] [blame] | 567 | } // namespace art |