blob: 0ab7782e2cee7ad2079f6d2f28f5330ece2633f9 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
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 Geoffraye5038322014-07-04 09:41:32 +010018#include "builder.h"
19
20#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000021#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000022#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000024#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010025#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 Geoffray818f2102014-02-18 16:43:35 +000030#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000031#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010032#include "scoped_thread_state_change.h"
33#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000034
35namespace art {
36
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010037/**
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 */
43class 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 Geoffrayabed4d02014-07-14 15:24:11 +010068static bool IsTypeSupported(Primitive::Type type) {
69 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
70}
71
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010072void HGraphBuilder::InitializeLocals(uint16_t count) {
73 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000074 locals_.SetSize(count);
75 for (int i = 0; i < count; i++) {
76 HLocal* local = new (arena_) HLocal(i);
77 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000078 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000079 }
80}
81
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010082bool 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 Geoffrayf583e592014-04-07 13:20:42 +010091 int parameter_index = 0;
92
93 if (!dex_compilation_unit_->IsStatic()) {
94 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010095 HParameterValue* parameter =
96 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010097 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010099 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100100 number_of_parameters--;
101 }
102
103 uint32_t pos = 1;
104 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100105 HParameterValue* parameter =
106 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
107 entry_block_->AddInstruction(parameter);
108 HLocal* local = GetLocalAt(locals_index++);
109 // Store the parameter value in the local that the dex code will use
110 // to reference that parameter.
111 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
112 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
113 || (parameter->GetType() == Primitive::kPrimDouble);
114 if (is_wide) {
115 i++;
116 locals_index++;
117 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100118 }
119 }
120 return true;
121}
122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124 if (code_item.tries_size_ > 0) {
125 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000126 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000127 return true;
128}
129
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100130template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100131void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000132 int32_t target_offset = instruction.GetTargetOffset();
133 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100134 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
135 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700136 T* comparison = new (arena_) T(first, second);
137 current_block_->AddInstruction(comparison);
138 HInstruction* ifinst = new (arena_) HIf(comparison);
139 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700141 DCHECK(target != nullptr);
142 current_block_->AddSuccessor(target);
143 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
144 DCHECK(target != nullptr);
145 current_block_->AddSuccessor(target);
146 current_block_ = nullptr;
147}
148
149template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100150void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000151 int32_t target_offset = instruction.GetTargetOffset();
152 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700153 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
154 T* comparison = new (arena_) T(value, GetIntConstant(0));
155 current_block_->AddInstruction(comparison);
156 HInstruction* ifinst = new (arena_) HIf(comparison);
157 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100159 DCHECK(target != nullptr);
160 current_block_->AddSuccessor(target);
161 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
162 DCHECK(target != nullptr);
163 current_block_->AddSuccessor(target);
164 current_block_ = nullptr;
165}
166
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000167HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000168 if (!CanHandleCodeItem(code_item)) {
169 return nullptr;
170 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000171
172 const uint16_t* code_ptr = code_item.insns_;
173 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100174 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000175
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000177 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100180 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000181 graph_->SetEntryBlock(entry_block_);
182 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000186
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187 // To avoid splitting blocks, we compute ahead of time the instructions that
188 // start a new block, and create these blocks.
189 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000190
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100191 if (!InitializeParameters(code_item.ins_size_)) {
192 return nullptr;
193 }
194
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000195 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000196 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 // Update the current block if dex_offset starts a new block.
198 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
201 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202 code_ptr += instruction.SizeInCodeUnits();
203 }
204
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 // Add the suspend check to the entry block.
209 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000211 return graph_;
212}
213
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
215 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000216 if (block == nullptr) {
217 return;
218 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219
220 if (current_block_ != nullptr) {
221 // Branching instructions clear current_block, so we know
222 // the last instruction of the current block is not a branching
223 // instruction. We add an unconditional goto to the found block.
224 current_block_->AddInstruction(new (arena_) HGoto());
225 current_block_->AddSuccessor(block);
226 }
227 graph_->AddBlock(block);
228 current_block_ = block;
229}
230
231void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
232 // TODO: Support switch instructions.
233 branch_targets_.SetSize(code_end - code_ptr);
234
235 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100236 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000237 branch_targets_.Put(0, block);
238 entry_block_->AddSuccessor(block);
239
240 // Iterate over all instructions and find branching instructions. Create blocks for
241 // the locations these instructions branch to.
242 size_t dex_offset = 0;
243 while (code_ptr < code_end) {
244 const Instruction& instruction = *Instruction::At(code_ptr);
245 if (instruction.IsBranch()) {
246 int32_t target = instruction.GetTargetOffset() + dex_offset;
247 // Create a block for the target instruction.
248 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100249 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 branch_targets_.Put(target, block);
251 }
252 dex_offset += instruction.SizeInCodeUnits();
253 code_ptr += instruction.SizeInCodeUnits();
254 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100255 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256 branch_targets_.Put(dex_offset, block);
257 }
258 } else {
259 code_ptr += instruction.SizeInCodeUnits();
260 dex_offset += instruction.SizeInCodeUnits();
261 }
262 }
263}
264
265HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
266 DCHECK_GE(index, 0);
267 return branch_targets_.Get(index);
268}
269
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100270template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100271void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
272 HInstruction* first = LoadLocal(instruction.VRegB(), type);
273 current_block_->AddInstruction(new (arena_) T(type, first));
274 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
275}
276
277template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100278void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100279 HInstruction* first = LoadLocal(instruction.VRegB(), type);
280 HInstruction* second = LoadLocal(instruction.VRegC(), type);
281 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100282 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
283}
284
285template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100286void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
287 HInstruction* first = LoadLocal(instruction.VRegA(), type);
288 HInstruction* second = LoadLocal(instruction.VRegB(), type);
289 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100290 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
291}
292
293template<typename T>
294void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100295 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
296 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100297 if (reverse) {
298 std::swap(first, second);
299 }
300 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
301 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
302}
303
304template<typename T>
305void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100306 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
307 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100308 if (reverse) {
309 std::swap(first, second);
310 }
311 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
312 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
313}
314
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100315void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
316 if (type == Primitive::kPrimVoid) {
317 current_block_->AddInstruction(new (arena_) HReturnVoid());
318 } else {
319 HInstruction* value = LoadLocal(instruction.VRegA(), type);
320 current_block_->AddInstruction(new (arena_) HReturn(value));
321 }
322 current_block_->AddSuccessor(exit_block_);
323 current_block_ = nullptr;
324}
325
326bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
327 uint32_t dex_offset,
328 uint32_t method_idx,
329 uint32_t number_of_vreg_arguments,
330 bool is_range,
331 uint32_t* args,
332 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100333 Instruction::Code opcode = instruction.Opcode();
334 InvokeType invoke_type;
335 switch (opcode) {
336 case Instruction::INVOKE_STATIC:
337 case Instruction::INVOKE_STATIC_RANGE:
338 invoke_type = kStatic;
339 break;
340 case Instruction::INVOKE_DIRECT:
341 case Instruction::INVOKE_DIRECT_RANGE:
342 invoke_type = kDirect;
343 break;
344 case Instruction::INVOKE_VIRTUAL:
345 case Instruction::INVOKE_VIRTUAL_RANGE:
346 invoke_type = kVirtual;
347 break;
348 case Instruction::INVOKE_INTERFACE:
349 case Instruction::INVOKE_INTERFACE_RANGE:
350 invoke_type = kInterface;
351 break;
352 case Instruction::INVOKE_SUPER_RANGE:
353 case Instruction::INVOKE_SUPER:
354 invoke_type = kSuper;
355 break;
356 default:
357 LOG(FATAL) << "Unexpected invoke op: " << opcode;
358 return false;
359 }
360
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100361 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
362 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
363 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
364 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100365 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100366 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
367
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100368 HInvoke* invoke = nullptr;
369 if (invoke_type == kVirtual) {
370 MethodReference target_method(dex_file_, method_idx);
371 uintptr_t direct_code;
372 uintptr_t direct_method;
373 int vtable_index;
374 // TODO: Add devirtualization support.
375 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
376 &invoke_type, &target_method, &vtable_index,
377 &direct_code, &direct_method);
378 if (vtable_index == -1) {
379 return false;
380 }
381 invoke = new (arena_) HInvokeVirtual(
382 arena_, number_of_arguments, return_type, dex_offset, vtable_index);
383 } else {
384 // Treat invoke-direct like static calls for now.
385 invoke = new (arena_) HInvokeStatic(
386 arena_, number_of_arguments, return_type, dex_offset, method_idx);
387 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100388
389 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100390 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100391 if (is_instance_call) {
392 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100393 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
394 current_block_->AddInstruction(null_check);
395 temps.Add(null_check);
396 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100397 start_index = 1;
398 }
399
400 uint32_t descriptor_index = 1;
401 uint32_t argument_index = start_index;
402 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
403 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100404 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
405 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100406 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
407 << " at " << dex_offset;
408 // We do not implement non sequential register pair.
409 return false;
410 }
411 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
412 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100413 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100414 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100415 }
416 }
417
418 DCHECK_EQ(argument_index, number_of_arguments);
419 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100420 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100421 return true;
422}
423
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100424bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
425 uint32_t dex_offset,
426 bool is_put) {
427 uint32_t source_or_dest_reg = instruction.VRegA_22c();
428 uint32_t obj_reg = instruction.VRegB_22c();
429 uint16_t field_index = instruction.VRegC_22c();
430
431 ScopedObjectAccess soa(Thread::Current());
432 StackHandleScope<1> hs(soa.Self());
433 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
434 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
435
436 if (resolved_field.Get() == nullptr) {
437 return false;
438 }
439 if (resolved_field->IsVolatile()) {
440 return false;
441 }
442
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100443 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
444 if (!IsTypeSupported(field_type)) {
445 return false;
446 }
447
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
449 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
450 if (is_put) {
451 Temporaries temps(graph_, 1);
452 HInstruction* null_check = current_block_->GetLastInstruction();
453 // We need one temporary for the null check.
454 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100455 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
457 null_check,
458 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100459 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100460 resolved_field->GetOffset()));
461 } else {
462 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
463 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100464 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100465 resolved_field->GetOffset()));
466
467 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
468 }
469 return true;
470}
471
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100472void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
473 uint32_t dex_offset,
474 bool is_put,
475 Primitive::Type anticipated_type) {
476 uint8_t source_or_dest_reg = instruction.VRegA_23x();
477 uint8_t array_reg = instruction.VRegB_23x();
478 uint8_t index_reg = instruction.VRegC_23x();
479
480 DCHECK(IsTypeSupported(anticipated_type));
481
482 // We need one temporary for the null check, one for the index, and one for the length.
483 Temporaries temps(graph_, 3);
484
485 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
486 object = new (arena_) HNullCheck(object, dex_offset);
487 current_block_->AddInstruction(object);
488 temps.Add(object);
489
490 HInstruction* length = new (arena_) HArrayLength(object);
491 current_block_->AddInstruction(length);
492 temps.Add(length);
493 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
494 index = new (arena_) HBoundsCheck(index, length, dex_offset);
495 current_block_->AddInstruction(index);
496 temps.Add(index);
497 if (is_put) {
498 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
499 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100500 current_block_->AddInstruction(new (arena_) HArraySet(
501 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100502 } else {
503 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
504 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
505 }
506}
507
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100508void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
509 uint32_t type_index,
510 uint32_t number_of_vreg_arguments,
511 bool is_range,
512 uint32_t* args,
513 uint32_t register_index) {
514 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
515 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
516 current_block_->AddInstruction(object);
517
518 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
519 DCHECK_EQ(descriptor[0], '[') << descriptor;
520 char primitive = descriptor[1];
521 DCHECK(primitive == 'I'
522 || primitive == 'L'
523 || primitive == '[') << descriptor;
524 bool is_reference_array = (primitive == 'L') || (primitive == '[');
525 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
526
527 Temporaries temps(graph_, 1);
528 temps.Add(object);
529 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
530 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
531 HInstruction* index = GetIntConstant(i);
532 current_block_->AddInstruction(
533 new (arena_) HArraySet(object, index, value, type, dex_offset));
534 }
535 latest_result_ = object;
536}
537
538template <typename T>
539void HGraphBuilder::BuildFillArrayData(HInstruction* object,
540 const T* data,
541 uint32_t element_count,
542 Primitive::Type anticipated_type,
543 uint32_t dex_offset) {
544 for (uint32_t i = 0; i < element_count; ++i) {
545 HInstruction* index = GetIntConstant(i);
546 HInstruction* value = GetIntConstant(data[i]);
547 current_block_->AddInstruction(new (arena_) HArraySet(
548 object, index, value, anticipated_type, dex_offset));
549 }
550}
551
552void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
553 const uint64_t* data,
554 uint32_t element_count,
555 uint32_t dex_offset) {
556 for (uint32_t i = 0; i < element_count; ++i) {
557 HInstruction* index = GetIntConstant(i);
558 HInstruction* value = GetLongConstant(data[i]);
559 current_block_->AddInstruction(new (arena_) HArraySet(
560 object, index, value, Primitive::kPrimLong, dex_offset));
561 }
562}
563
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000564void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
565 if (target_offset <= 0) {
566 // Unconditionnally add a suspend check to backward branches. We can remove
567 // them after we recognize loops in the graph.
568 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
569 }
570}
571
572bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000573 if (current_block_ == nullptr) {
574 return true; // Dead code
575 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000576
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000577 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000578 case Instruction::CONST_4: {
579 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100580 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000581 UpdateLocal(register_index, constant);
582 break;
583 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000584
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100585 case Instruction::CONST_16: {
586 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100587 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
588 UpdateLocal(register_index, constant);
589 break;
590 }
591
Dave Allison20dfc792014-06-16 20:44:29 -0700592 case Instruction::CONST: {
593 int32_t register_index = instruction.VRegA();
594 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
595 UpdateLocal(register_index, constant);
596 break;
597 }
598
599 case Instruction::CONST_HIGH16: {
600 int32_t register_index = instruction.VRegA();
601 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
602 UpdateLocal(register_index, constant);
603 break;
604 }
605
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100606 case Instruction::CONST_WIDE_16: {
607 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700608 // Get 16 bits of constant value, sign extended to 64 bits.
609 int64_t value = instruction.VRegB_21s();
610 value <<= 48;
611 value >>= 48;
612 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100613 UpdateLocal(register_index, constant);
614 break;
615 }
616
617 case Instruction::CONST_WIDE_32: {
618 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700619 // Get 32 bits of constant value, sign extended to 64 bits.
620 int64_t value = instruction.VRegB_31i();
621 value <<= 32;
622 value >>= 32;
623 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 UpdateLocal(register_index, constant);
625 break;
626 }
627
628 case Instruction::CONST_WIDE: {
629 int32_t register_index = instruction.VRegA();
630 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100631 UpdateLocal(register_index, constant);
632 break;
633 }
634
Dave Allison20dfc792014-06-16 20:44:29 -0700635 case Instruction::CONST_WIDE_HIGH16: {
636 int32_t register_index = instruction.VRegA();
637 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
638 HLongConstant* constant = GetLongConstant(value);
639 UpdateLocal(register_index, constant);
640 break;
641 }
642
643 // TODO: these instructions are also used to move floating point values, so what is
644 // the type (int or float)?
645 case Instruction::MOVE:
646 case Instruction::MOVE_FROM16:
647 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100649 UpdateLocal(instruction.VRegA(), value);
650 break;
651 }
652
Dave Allison20dfc792014-06-16 20:44:29 -0700653 // TODO: these instructions are also used to move floating point values, so what is
654 // the type (long or double)?
655 case Instruction::MOVE_WIDE:
656 case Instruction::MOVE_WIDE_FROM16:
657 case Instruction::MOVE_WIDE_16: {
658 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
659 UpdateLocal(instruction.VRegA(), value);
660 break;
661 }
662
663 case Instruction::MOVE_OBJECT:
664 case Instruction::MOVE_OBJECT_16:
665 case Instruction::MOVE_OBJECT_FROM16: {
666 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
667 UpdateLocal(instruction.VRegA(), value);
668 break;
669 }
670
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000671 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000674 }
675
Dave Allison20dfc792014-06-16 20:44:29 -0700676#define IF_XX(comparison, cond) \
677 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
678 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100679
Dave Allison20dfc792014-06-16 20:44:29 -0700680 IF_XX(HEqual, EQ);
681 IF_XX(HNotEqual, NE);
682 IF_XX(HLessThan, LT);
683 IF_XX(HLessThanOrEqual, LE);
684 IF_XX(HGreaterThan, GT);
685 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000686
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000687 case Instruction::GOTO:
688 case Instruction::GOTO_16:
689 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000690 int32_t offset = instruction.GetTargetOffset();
691 PotentiallyAddSuspendCheck(offset, dex_offset);
692 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000693 DCHECK(target != nullptr);
694 current_block_->AddInstruction(new (arena_) HGoto());
695 current_block_->AddSuccessor(target);
696 current_block_ = nullptr;
697 break;
698 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100700 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100701 DCHECK_NE(return_type_, Primitive::kPrimNot);
702 DCHECK_NE(return_type_, Primitive::kPrimLong);
703 DCHECK_NE(return_type_, Primitive::kPrimDouble);
704 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100705 break;
706 }
707
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100708 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100709 DCHECK(return_type_ == Primitive::kPrimNot);
710 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100711 break;
712 }
713
714 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100715 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
716 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000717 break;
718 }
719
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100720 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100721 case Instruction::INVOKE_DIRECT:
722 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000723 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100725 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700726 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100727 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
728 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100729 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100730 break;
731 }
732
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100733 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100734 case Instruction::INVOKE_DIRECT_RANGE:
735 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100736 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100737 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
738 uint32_t register_index = instruction.VRegC();
739 if (!BuildInvoke(instruction, dex_offset, method_idx,
740 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100741 return false;
742 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000743 break;
744 }
745
Roland Levillain88cb1752014-10-20 16:36:47 +0100746 case Instruction::NEG_INT: {
747 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
748 break;
749 }
750
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100751 case Instruction::NEG_LONG: {
752 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
753 break;
754 }
755
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100756 case Instruction::NOT_INT: {
757 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
758 break;
759 }
760
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000761 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100762 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 break;
764 }
765
766 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100767 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100768 break;
769 }
770
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100771 case Instruction::ADD_DOUBLE: {
772 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
773 break;
774 }
775
776 case Instruction::ADD_FLOAT: {
777 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
778 break;
779 }
780
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100781 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100782 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100783 break;
784 }
785
786 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100787 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000788 break;
789 }
790
Calin Juravle096cc022014-10-23 17:01:13 +0100791 case Instruction::SUB_FLOAT: {
792 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
793 break;
794 }
795
796 case Instruction::SUB_DOUBLE: {
797 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
798 break;
799 }
800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000801 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100802 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
803 break;
804 }
805
Calin Juravle34bacdf2014-10-07 20:23:36 +0100806 case Instruction::MUL_INT: {
807 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
808 break;
809 }
810
811 case Instruction::MUL_LONG: {
812 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
813 break;
814 }
815
Calin Juravleb5bfa962014-10-21 18:02:24 +0100816 case Instruction::MUL_FLOAT: {
817 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
818 break;
819 }
820
821 case Instruction::MUL_DOUBLE: {
822 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
823 break;
824 }
825
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100826 case Instruction::ADD_LONG_2ADDR: {
827 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100828 break;
829 }
830
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100831 case Instruction::ADD_DOUBLE_2ADDR: {
832 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
833 break;
834 }
835
836 case Instruction::ADD_FLOAT_2ADDR: {
837 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
838 break;
839 }
840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100841 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
843 break;
844 }
845
846 case Instruction::SUB_LONG_2ADDR: {
847 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000848 break;
849 }
850
Calin Juravle096cc022014-10-23 17:01:13 +0100851 case Instruction::SUB_FLOAT_2ADDR: {
852 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
853 break;
854 }
855
856 case Instruction::SUB_DOUBLE_2ADDR: {
857 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
858 break;
859 }
860
Calin Juravle34bacdf2014-10-07 20:23:36 +0100861 case Instruction::MUL_INT_2ADDR: {
862 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
863 break;
864 }
865
866 case Instruction::MUL_LONG_2ADDR: {
867 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
868 break;
869 }
870
Calin Juravleb5bfa962014-10-21 18:02:24 +0100871 case Instruction::MUL_FLOAT_2ADDR: {
872 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
873 break;
874 }
875
876 case Instruction::MUL_DOUBLE_2ADDR: {
877 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
878 break;
879 }
880
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000881 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100882 Binop_22s<HAdd>(instruction, false);
883 break;
884 }
885
886 case Instruction::RSUB_INT: {
887 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000888 break;
889 }
890
Calin Juravle34bacdf2014-10-07 20:23:36 +0100891 case Instruction::MUL_INT_LIT16: {
892 Binop_22s<HMul>(instruction, false);
893 break;
894 }
895
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000896 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100897 Binop_22b<HAdd>(instruction, false);
898 break;
899 }
900
901 case Instruction::RSUB_INT_LIT8: {
902 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000903 break;
904 }
905
Calin Juravle34bacdf2014-10-07 20:23:36 +0100906 case Instruction::MUL_INT_LIT8: {
907 Binop_22b<HMul>(instruction, false);
908 break;
909 }
910
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100911 case Instruction::NEW_INSTANCE: {
912 current_block_->AddInstruction(
913 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
914 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
915 break;
916 }
917
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100918 case Instruction::NEW_ARRAY: {
919 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
920 current_block_->AddInstruction(
921 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
922 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
923 break;
924 }
925
926 case Instruction::FILLED_NEW_ARRAY: {
927 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
928 uint32_t type_index = instruction.VRegB_35c();
929 uint32_t args[5];
930 instruction.GetVarArgs(args);
931 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
932 break;
933 }
934
935 case Instruction::FILLED_NEW_ARRAY_RANGE: {
936 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
937 uint32_t type_index = instruction.VRegB_3rc();
938 uint32_t register_index = instruction.VRegC_3rc();
939 BuildFilledNewArray(
940 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
941 break;
942 }
943
944 case Instruction::FILL_ARRAY_DATA: {
945 Temporaries temps(graph_, 1);
946 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
947 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
948 current_block_->AddInstruction(null_check);
949 temps.Add(null_check);
950
951 HInstruction* length = new (arena_) HArrayLength(null_check);
952 current_block_->AddInstruction(length);
953
954 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
955 const Instruction::ArrayDataPayload* payload =
956 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
957 const uint8_t* data = payload->data;
958 uint32_t element_count = payload->element_count;
959
960 // Implementation of this DEX instruction seems to be that the bounds check is
961 // done before doing any stores.
962 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
963 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
964
965 switch (payload->element_width) {
966 case 1:
967 BuildFillArrayData(null_check, data, element_count, Primitive::kPrimByte, dex_offset);
968 break;
969 case 2:
970 BuildFillArrayData(null_check,
971 reinterpret_cast<const uint16_t*>(data),
972 element_count,
973 Primitive::kPrimShort,
974 dex_offset);
975 break;
976 case 4:
977 BuildFillArrayData(null_check,
978 reinterpret_cast<const uint32_t*>(data),
979 element_count,
980 Primitive::kPrimInt,
981 dex_offset);
982 break;
983 case 8:
984 BuildFillWideArrayData(null_check,
985 reinterpret_cast<const uint64_t*>(data),
986 element_count,
987 dex_offset);
988 break;
989 default:
990 LOG(FATAL) << "Unknown element width for " << payload->element_width;
991 }
992 break;
993 }
994
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100995 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700996 case Instruction::MOVE_RESULT_WIDE:
997 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100998 UpdateLocal(instruction.VRegA(), latest_result_);
999 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001000 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001002 case Instruction::CMP_LONG: {
1003 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1004 break;
1005 }
1006
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001007 case Instruction::NOP:
1008 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001009
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001010 case Instruction::IGET:
1011 case Instruction::IGET_WIDE:
1012 case Instruction::IGET_OBJECT:
1013 case Instruction::IGET_BOOLEAN:
1014 case Instruction::IGET_BYTE:
1015 case Instruction::IGET_CHAR:
1016 case Instruction::IGET_SHORT: {
1017 if (!BuildFieldAccess(instruction, dex_offset, false)) {
1018 return false;
1019 }
1020 break;
1021 }
1022
1023 case Instruction::IPUT:
1024 case Instruction::IPUT_WIDE:
1025 case Instruction::IPUT_OBJECT:
1026 case Instruction::IPUT_BOOLEAN:
1027 case Instruction::IPUT_BYTE:
1028 case Instruction::IPUT_CHAR:
1029 case Instruction::IPUT_SHORT: {
1030 if (!BuildFieldAccess(instruction, dex_offset, true)) {
1031 return false;
1032 }
1033 break;
1034 }
1035
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001036#define ARRAY_XX(kind, anticipated_type) \
1037 case Instruction::AGET##kind: { \
1038 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1039 break; \
1040 } \
1041 case Instruction::APUT##kind: { \
1042 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1043 break; \
1044 }
1045
1046 ARRAY_XX(, Primitive::kPrimInt);
1047 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1048 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1049 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1050 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1051 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1052 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1053
Nicolas Geoffray39468442014-09-02 15:17:15 +01001054 case Instruction::ARRAY_LENGTH: {
1055 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
1056 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1057 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1058 break;
1059 }
1060
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 default:
1062 return false;
1063 }
1064 return true;
1065}
1066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001068 if (constant0_ != nullptr) {
1069 return constant0_;
1070 }
1071 constant0_ = new(arena_) HIntConstant(0);
1072 entry_block_->AddInstruction(constant0_);
1073 return constant0_;
1074}
1075
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077 if (constant1_ != nullptr) {
1078 return constant1_;
1079 }
1080 constant1_ = new(arena_) HIntConstant(1);
1081 entry_block_->AddInstruction(constant1_);
1082 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001083}
1084
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001085HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001086 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 case 0: return GetIntConstant0();
1088 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001089 default: {
1090 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1091 entry_block_->AddInstruction(instruction);
1092 return instruction;
1093 }
1094 }
1095}
1096
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1098 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1099 entry_block_->AddInstruction(instruction);
1100 return instruction;
1101}
1102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001103HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1104 return locals_.Get(register_index);
1105}
1106
1107void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1108 HLocal* local = GetLocalAt(register_index);
1109 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1110}
1111
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001115 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001116}
1117
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118} // namespace art