blob: 434d9efbcfb5143516aa88758ef67a8c8e1e8838 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nicolas Geoffraye5038322014-07-04 09:41:32 +010017#include "builder.h"
18
19#include "class_linker.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000020#include "dex_file.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000021#include "dex_file-inl.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "dex_instruction.h"
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000023#include "dex_instruction-inl.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010024#include "driver/compiler_driver-inl.h"
25#include "mirror/art_field.h"
26#include "mirror/art_field-inl.h"
27#include "mirror/class_loader.h"
28#include "mirror/dex_cache.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029#include "nodes.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000030#include "primitive.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010031#include "scoped_thread_state_change.h"
32#include "thread.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033
34namespace art {
35
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036/**
37 * Helper class to add HTemporary instructions. This class is used when
38 * converting a DEX instruction to multiple HInstruction, and where those
39 * instructions do not die at the following instruction, but instead spans
40 * multiple instructions.
41 */
42class Temporaries : public ValueObject {
43 public:
44 Temporaries(HGraph* graph, size_t count) : graph_(graph), count_(count), index_(0) {
45 graph_->UpdateNumberOfTemporaries(count_);
46 }
47
48 void Add(HInstruction* instruction) {
49 // We currently only support vreg size temps.
50 DCHECK(instruction->GetType() != Primitive::kPrimLong
51 && instruction->GetType() != Primitive::kPrimDouble);
52 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_++);
53 instruction->GetBlock()->AddInstruction(temp);
54 DCHECK(temp->GetPrevious() == instruction);
55 }
56
57 private:
58 HGraph* const graph_;
59
60 // The total number of temporaries that will be used.
61 const size_t count_;
62
63 // Current index in the temporary stack, updated by `Add`.
64 size_t index_;
65};
66
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +010067static bool IsTypeSupported(Primitive::Type type) {
68 return type != Primitive::kPrimFloat && type != Primitive::kPrimDouble;
69}
70
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010071void HGraphBuilder::InitializeLocals(uint16_t count) {
72 graph_->SetNumberOfVRegs(count);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000073 locals_.SetSize(count);
74 for (int i = 0; i < count; i++) {
75 HLocal* local = new (arena_) HLocal(i);
76 entry_block_->AddInstruction(local);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077 locals_.Put(i, local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000078 }
79}
80
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010081bool HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) {
82 // dex_compilation_unit_ is null only when unit testing.
83 if (dex_compilation_unit_ == nullptr) {
84 return true;
85 }
86
87 graph_->SetNumberOfInVRegs(number_of_parameters);
88 const char* shorty = dex_compilation_unit_->GetShorty();
89 int locals_index = locals_.Size() - number_of_parameters;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010090 int parameter_index = 0;
91
92 if (!dex_compilation_unit_->IsStatic()) {
93 // Add the implicit 'this' argument, not expressed in the signature.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010094 HParameterValue* parameter =
95 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010096 entry_block_->AddInstruction(parameter);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010097 HLocal* local = GetLocalAt(locals_index++);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +010098 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010099 number_of_parameters--;
100 }
101
102 uint32_t pos = 1;
103 for (int i = 0; i < number_of_parameters; i++) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100104 HParameterValue* parameter =
105 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++]));
106 entry_block_->AddInstruction(parameter);
107 HLocal* local = GetLocalAt(locals_index++);
108 // Store the parameter value in the local that the dex code will use
109 // to reference that parameter.
110 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter));
111 bool is_wide = (parameter->GetType() == Primitive::kPrimLong)
112 || (parameter->GetType() == Primitive::kPrimDouble);
113 if (is_wide) {
114 i++;
115 locals_index++;
116 parameter_index++;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100117 }
118 }
119 return true;
120}
121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000122static bool CanHandleCodeItem(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000123 if (code_item.tries_size_ > 0) {
124 return false;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000125 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000126 return true;
127}
128
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100129template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000131 int32_t target_offset = instruction.GetTargetOffset();
132 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100133 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
134 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Dave Allison20dfc792014-06-16 20:44:29 -0700135 T* comparison = new (arena_) T(first, second);
136 current_block_->AddInstruction(comparison);
137 HInstruction* ifinst = new (arena_) HIf(comparison);
138 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000139 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700140 DCHECK(target != nullptr);
141 current_block_->AddSuccessor(target);
142 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
143 DCHECK(target != nullptr);
144 current_block_->AddSuccessor(target);
145 current_block_ = nullptr;
146}
147
148template<typename T>
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100149void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000150 int32_t target_offset = instruction.GetTargetOffset();
151 PotentiallyAddSuspendCheck(target_offset, dex_offset);
Dave Allison20dfc792014-06-16 20:44:29 -0700152 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt);
153 T* comparison = new (arena_) T(value, GetIntConstant(0));
154 current_block_->AddInstruction(comparison);
155 HInstruction* ifinst = new (arena_) HIf(comparison);
156 current_block_->AddInstruction(ifinst);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000157 HBasicBlock* target = FindBlockStartingAt(dex_offset + target_offset);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100158 DCHECK(target != nullptr);
159 current_block_->AddSuccessor(target);
160 target = FindBlockStartingAt(dex_offset + instruction.SizeInCodeUnits());
161 DCHECK(target != nullptr);
162 current_block_->AddSuccessor(target);
163 current_block_ = nullptr;
164}
165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000166HGraph* HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000167 if (!CanHandleCodeItem(code_item)) {
168 return nullptr;
169 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000170
171 const uint16_t* code_ptr = code_item.insns_;
172 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100173 code_start_ = code_ptr;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000174
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 // Setup the graph with the entry block and exit block.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 graph_ = new (arena_) HGraph(arena_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100177 entry_block_ = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 graph_->AddBlock(entry_block_);
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100179 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180 graph_->SetEntryBlock(entry_block_);
181 graph_->SetExitBlock(exit_block_);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 InitializeLocals(code_item.registers_size_);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100184 graph_->UpdateMaximumNumberOfOutVRegs(code_item.outs_size_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000185
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 // To avoid splitting blocks, we compute ahead of time the instructions that
187 // start a new block, and create these blocks.
188 ComputeBranchTargets(code_ptr, code_end);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100190 if (!InitializeParameters(code_item.ins_size_)) {
191 return nullptr;
192 }
193
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000194 size_t dex_offset = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195 while (code_ptr < code_end) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196 // Update the current block if dex_offset starts a new block.
197 MaybeUpdateCurrentBlock(dex_offset);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198 const Instruction& instruction = *Instruction::At(code_ptr);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 if (!AnalyzeDexInstruction(instruction, dex_offset)) return nullptr;
200 dex_offset += instruction.SizeInCodeUnits();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 code_ptr += instruction.SizeInCodeUnits();
202 }
203
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000204 // Add the exit block at the end to give it the highest id.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000205 graph_->AddBlock(exit_block_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000206 exit_block_->AddInstruction(new (arena_) HExit());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207 // Add the suspend check to the entry block.
208 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000209 entry_block_->AddInstruction(new (arena_) HGoto());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000210 return graph_;
211}
212
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000213void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) {
214 HBasicBlock* block = FindBlockStartingAt(index);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000215 if (block == nullptr) {
216 return;
217 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000218
219 if (current_block_ != nullptr) {
220 // Branching instructions clear current_block, so we know
221 // the last instruction of the current block is not a branching
222 // instruction. We add an unconditional goto to the found block.
223 current_block_->AddInstruction(new (arena_) HGoto());
224 current_block_->AddSuccessor(block);
225 }
226 graph_->AddBlock(block);
227 current_block_ = block;
228}
229
230void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, const uint16_t* code_end) {
231 // TODO: Support switch instructions.
232 branch_targets_.SetSize(code_end - code_ptr);
233
234 // Create the first block for the dex instructions, single successor of the entry block.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100235 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 branch_targets_.Put(0, block);
237 entry_block_->AddSuccessor(block);
238
239 // Iterate over all instructions and find branching instructions. Create blocks for
240 // the locations these instructions branch to.
241 size_t dex_offset = 0;
242 while (code_ptr < code_end) {
243 const Instruction& instruction = *Instruction::At(code_ptr);
244 if (instruction.IsBranch()) {
245 int32_t target = instruction.GetTargetOffset() + dex_offset;
246 // Create a block for the target instruction.
247 if (FindBlockStartingAt(target) == nullptr) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100248 block = new (arena_) HBasicBlock(graph_, target);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000249 branch_targets_.Put(target, block);
250 }
251 dex_offset += instruction.SizeInCodeUnits();
252 code_ptr += instruction.SizeInCodeUnits();
253 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_offset) == nullptr)) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100254 block = new (arena_) HBasicBlock(graph_, dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 branch_targets_.Put(dex_offset, block);
256 }
257 } else {
258 code_ptr += instruction.SizeInCodeUnits();
259 dex_offset += instruction.SizeInCodeUnits();
260 }
261 }
262}
263
264HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const {
265 DCHECK_GE(index, 0);
266 return branch_targets_.Get(index);
267}
268
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100269template<typename T>
Roland Levillain88cb1752014-10-20 16:36:47 +0100270void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) {
271 HInstruction* first = LoadLocal(instruction.VRegB(), type);
272 current_block_->AddInstruction(new (arena_) T(type, first));
273 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
274}
275
276template<typename T>
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100277void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100278 HInstruction* first = LoadLocal(instruction.VRegB(), type);
279 HInstruction* second = LoadLocal(instruction.VRegC(), type);
280 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100281 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
282}
283
284template<typename T>
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100285void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) {
286 HInstruction* first = LoadLocal(instruction.VRegA(), type);
287 HInstruction* second = LoadLocal(instruction.VRegB(), type);
288 current_block_->AddInstruction(new (arena_) T(type, first, second));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100289 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
290}
291
292template<typename T>
293void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100294 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
295 HInstruction* second = GetIntConstant(instruction.VRegC_22s());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100296 if (reverse) {
297 std::swap(first, second);
298 }
299 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
300 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
301}
302
303template<typename T>
304void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100305 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
306 HInstruction* second = GetIntConstant(instruction.VRegC_22b());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100307 if (reverse) {
308 std::swap(first, second);
309 }
310 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second));
311 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
312}
313
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100314void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) {
315 if (type == Primitive::kPrimVoid) {
316 current_block_->AddInstruction(new (arena_) HReturnVoid());
317 } else {
318 HInstruction* value = LoadLocal(instruction.VRegA(), type);
319 current_block_->AddInstruction(new (arena_) HReturn(value));
320 }
321 current_block_->AddSuccessor(exit_block_);
322 current_block_ = nullptr;
323}
324
325bool HGraphBuilder::BuildInvoke(const Instruction& instruction,
326 uint32_t dex_offset,
327 uint32_t method_idx,
328 uint32_t number_of_vreg_arguments,
329 bool is_range,
330 uint32_t* args,
331 uint32_t register_index) {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100332 Instruction::Code opcode = instruction.Opcode();
333 InvokeType invoke_type;
334 switch (opcode) {
335 case Instruction::INVOKE_STATIC:
336 case Instruction::INVOKE_STATIC_RANGE:
337 invoke_type = kStatic;
338 break;
339 case Instruction::INVOKE_DIRECT:
340 case Instruction::INVOKE_DIRECT_RANGE:
341 invoke_type = kDirect;
342 break;
343 case Instruction::INVOKE_VIRTUAL:
344 case Instruction::INVOKE_VIRTUAL_RANGE:
345 invoke_type = kVirtual;
346 break;
347 case Instruction::INVOKE_INTERFACE:
348 case Instruction::INVOKE_INTERFACE_RANGE:
349 invoke_type = kInterface;
350 break;
351 case Instruction::INVOKE_SUPER_RANGE:
352 case Instruction::INVOKE_SUPER:
353 invoke_type = kSuper;
354 break;
355 default:
356 LOG(FATAL) << "Unexpected invoke op: " << opcode;
357 return false;
358 }
359
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100360 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
361 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_);
362 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_);
363 Primitive::Type return_type = Primitive::GetType(descriptor[0]);
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100364 bool is_instance_call = invoke_type != kStatic;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100365 const size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1);
366
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100367 HInvoke* invoke = nullptr;
368 if (invoke_type == kVirtual) {
369 MethodReference target_method(dex_file_, method_idx);
370 uintptr_t direct_code;
371 uintptr_t direct_method;
372 int vtable_index;
373 // TODO: Add devirtualization support.
374 compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_offset, true, true,
375 &invoke_type, &target_method, &vtable_index,
376 &direct_code, &direct_method);
377 if (vtable_index == -1) {
378 return false;
379 }
380 invoke = new (arena_) HInvokeVirtual(
381 arena_, number_of_arguments, return_type, dex_offset, vtable_index);
382 } else {
383 // Treat invoke-direct like static calls for now.
384 invoke = new (arena_) HInvokeStatic(
385 arena_, number_of_arguments, return_type, dex_offset, method_idx);
386 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100387
388 size_t start_index = 0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100389 Temporaries temps(graph_, is_instance_call ? 1 : 0);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100390 if (is_instance_call) {
391 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +0100392 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_offset);
393 current_block_->AddInstruction(null_check);
394 temps.Add(null_check);
395 invoke->SetArgumentAt(0, null_check);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100396 start_index = 1;
397 }
398
399 uint32_t descriptor_index = 1;
400 uint32_t argument_index = start_index;
401 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) {
402 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100403 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
404 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100405 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol()
406 << " at " << dex_offset;
407 // We do not implement non sequential register pair.
408 return false;
409 }
410 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type);
411 invoke->SetArgumentAt(argument_index, arg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100412 if (is_wide) {
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100413 i++;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100414 }
415 }
416
417 DCHECK_EQ(argument_index, number_of_arguments);
418 current_block_->AddInstruction(invoke);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100419 latest_result_ = invoke;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100420 return true;
421}
422
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction,
424 uint32_t dex_offset,
425 bool is_put) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100426 uint32_t source_or_dest_reg = instruction.VRegA_22c();
427 uint32_t obj_reg = instruction.VRegB_22c();
428 uint16_t field_index = instruction.VRegC_22c();
429
430 ScopedObjectAccess soa(Thread::Current());
431 StackHandleScope<1> hs(soa.Self());
432 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
433 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
434
435 if (resolved_field.Get() == nullptr) {
436 return false;
437 }
438 if (resolved_field->IsVolatile()) {
439 return false;
440 }
441
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100442 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
443 if (!IsTypeSupported(field_type)) {
444 return false;
445 }
446
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100447 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
448 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
449 if (is_put) {
450 Temporaries temps(graph_, 1);
451 HInstruction* null_check = current_block_->GetLastInstruction();
452 // We need one temporary for the null check.
453 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100454 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
456 null_check,
457 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100458 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100459 resolved_field->GetOffset()));
460 } else {
461 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
462 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100463 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100464 resolved_field->GetOffset()));
465
466 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
467 }
468 return true;
469}
470
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100471
472
473bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction,
474 uint32_t dex_offset,
475 bool is_put) {
476 uint32_t source_or_dest_reg = instruction.VRegA_21c();
477 uint16_t field_index = instruction.VRegB_21c();
478
479 uint32_t storage_index;
480 bool is_referrers_class;
481 bool is_initialized;
482 bool is_volatile;
483 MemberOffset field_offset(0u);
484 Primitive::Type field_type;
485
486 bool fast_path = compiler_driver_->ComputeStaticFieldInfo(field_index,
487 dex_compilation_unit_,
488 is_put,
489 &field_offset,
490 &storage_index,
491 &is_referrers_class,
492 &is_volatile,
493 &is_initialized,
494 &field_type);
495 if (!fast_path) {
496 return false;
497 }
498
499 if (is_volatile) {
500 return false;
501 }
502
503 if (!IsTypeSupported(field_type)) {
504 return false;
505 }
506
507 HLoadClass* constant = new (arena_) HLoadClass(
508 storage_index, is_referrers_class, is_initialized, dex_offset);
509 current_block_->AddInstruction(constant);
510
511 HInstruction* cls = constant;
512 if (constant->NeedsInitialization()) {
513 cls = new (arena_) HClinitCheck(constant, dex_offset);
514 current_block_->AddInstruction(cls);
515 }
516
517 if (is_put) {
518 // We need to keep the class alive before loading the value.
519 Temporaries temps(graph_, 1);
520 temps.Add(cls);
521 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
522 DCHECK_EQ(value->GetType(), field_type);
523 current_block_->AddInstruction(
524 new (arena_) HStaticFieldSet(cls, value, field_type, field_offset));
525 } else {
526 current_block_->AddInstruction(new (arena_) HStaticFieldGet(cls, field_type, field_offset));
527 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
528 }
529 return true;
530}
531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100532void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
533 uint32_t dex_offset,
534 bool is_put,
535 Primitive::Type anticipated_type) {
536 uint8_t source_or_dest_reg = instruction.VRegA_23x();
537 uint8_t array_reg = instruction.VRegB_23x();
538 uint8_t index_reg = instruction.VRegC_23x();
539
540 DCHECK(IsTypeSupported(anticipated_type));
541
542 // We need one temporary for the null check, one for the index, and one for the length.
543 Temporaries temps(graph_, 3);
544
545 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
546 object = new (arena_) HNullCheck(object, dex_offset);
547 current_block_->AddInstruction(object);
548 temps.Add(object);
549
550 HInstruction* length = new (arena_) HArrayLength(object);
551 current_block_->AddInstruction(length);
552 temps.Add(length);
553 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
554 index = new (arena_) HBoundsCheck(index, length, dex_offset);
555 current_block_->AddInstruction(index);
556 temps.Add(index);
557 if (is_put) {
558 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
559 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100560 current_block_->AddInstruction(new (arena_) HArraySet(
561 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100562 } else {
563 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
564 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
565 }
566}
567
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100568void HGraphBuilder::BuildFilledNewArray(uint32_t dex_offset,
569 uint32_t type_index,
570 uint32_t number_of_vreg_arguments,
571 bool is_range,
572 uint32_t* args,
573 uint32_t register_index) {
574 HInstruction* length = GetIntConstant(number_of_vreg_arguments);
575 HInstruction* object = new (arena_) HNewArray(length, dex_offset, type_index);
576 current_block_->AddInstruction(object);
577
578 const char* descriptor = dex_file_->StringByTypeIdx(type_index);
579 DCHECK_EQ(descriptor[0], '[') << descriptor;
580 char primitive = descriptor[1];
581 DCHECK(primitive == 'I'
582 || primitive == 'L'
583 || primitive == '[') << descriptor;
584 bool is_reference_array = (primitive == 'L') || (primitive == '[');
585 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt;
586
587 Temporaries temps(graph_, 1);
588 temps.Add(object);
589 for (size_t i = 0; i < number_of_vreg_arguments; ++i) {
590 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type);
591 HInstruction* index = GetIntConstant(i);
592 current_block_->AddInstruction(
593 new (arena_) HArraySet(object, index, value, type, dex_offset));
594 }
595 latest_result_ = object;
596}
597
598template <typename T>
599void HGraphBuilder::BuildFillArrayData(HInstruction* object,
600 const T* data,
601 uint32_t element_count,
602 Primitive::Type anticipated_type,
603 uint32_t dex_offset) {
604 for (uint32_t i = 0; i < element_count; ++i) {
605 HInstruction* index = GetIntConstant(i);
606 HInstruction* value = GetIntConstant(data[i]);
607 current_block_->AddInstruction(new (arena_) HArraySet(
608 object, index, value, anticipated_type, dex_offset));
609 }
610}
611
612void HGraphBuilder::BuildFillWideArrayData(HInstruction* object,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +0100613 const int64_t* data,
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +0100614 uint32_t element_count,
615 uint32_t dex_offset) {
616 for (uint32_t i = 0; i < element_count; ++i) {
617 HInstruction* index = GetIntConstant(i);
618 HInstruction* value = GetLongConstant(data[i]);
619 current_block_->AddInstruction(new (arena_) HArraySet(
620 object, index, value, Primitive::kPrimLong, dex_offset));
621 }
622}
623
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000624void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
625 if (target_offset <= 0) {
626 // Unconditionnally add a suspend check to backward branches. We can remove
627 // them after we recognize loops in the graph.
628 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
629 }
630}
631
632bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000633 if (current_block_ == nullptr) {
634 return true; // Dead code
635 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000636
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000638 case Instruction::CONST_4: {
639 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100640 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000641 UpdateLocal(register_index, constant);
642 break;
643 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000644
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100645 case Instruction::CONST_16: {
646 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
648 UpdateLocal(register_index, constant);
649 break;
650 }
651
Dave Allison20dfc792014-06-16 20:44:29 -0700652 case Instruction::CONST: {
653 int32_t register_index = instruction.VRegA();
654 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
655 UpdateLocal(register_index, constant);
656 break;
657 }
658
659 case Instruction::CONST_HIGH16: {
660 int32_t register_index = instruction.VRegA();
661 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
662 UpdateLocal(register_index, constant);
663 break;
664 }
665
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 case Instruction::CONST_WIDE_16: {
667 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700668 // Get 16 bits of constant value, sign extended to 64 bits.
669 int64_t value = instruction.VRegB_21s();
670 value <<= 48;
671 value >>= 48;
672 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 UpdateLocal(register_index, constant);
674 break;
675 }
676
677 case Instruction::CONST_WIDE_32: {
678 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700679 // Get 32 bits of constant value, sign extended to 64 bits.
680 int64_t value = instruction.VRegB_31i();
681 value <<= 32;
682 value >>= 32;
683 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100684 UpdateLocal(register_index, constant);
685 break;
686 }
687
688 case Instruction::CONST_WIDE: {
689 int32_t register_index = instruction.VRegA();
690 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100691 UpdateLocal(register_index, constant);
692 break;
693 }
694
Dave Allison20dfc792014-06-16 20:44:29 -0700695 case Instruction::CONST_WIDE_HIGH16: {
696 int32_t register_index = instruction.VRegA();
697 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
698 HLongConstant* constant = GetLongConstant(value);
699 UpdateLocal(register_index, constant);
700 break;
701 }
702
703 // TODO: these instructions are also used to move floating point values, so what is
704 // the type (int or float)?
705 case Instruction::MOVE:
706 case Instruction::MOVE_FROM16:
707 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100708 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100709 UpdateLocal(instruction.VRegA(), value);
710 break;
711 }
712
Dave Allison20dfc792014-06-16 20:44:29 -0700713 // TODO: these instructions are also used to move floating point values, so what is
714 // the type (long or double)?
715 case Instruction::MOVE_WIDE:
716 case Instruction::MOVE_WIDE_FROM16:
717 case Instruction::MOVE_WIDE_16: {
718 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
719 UpdateLocal(instruction.VRegA(), value);
720 break;
721 }
722
723 case Instruction::MOVE_OBJECT:
724 case Instruction::MOVE_OBJECT_16:
725 case Instruction::MOVE_OBJECT_FROM16: {
726 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
727 UpdateLocal(instruction.VRegA(), value);
728 break;
729 }
730
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000731 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100732 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000734 }
735
Dave Allison20dfc792014-06-16 20:44:29 -0700736#define IF_XX(comparison, cond) \
737 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
738 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100739
Dave Allison20dfc792014-06-16 20:44:29 -0700740 IF_XX(HEqual, EQ);
741 IF_XX(HNotEqual, NE);
742 IF_XX(HLessThan, LT);
743 IF_XX(HLessThanOrEqual, LE);
744 IF_XX(HGreaterThan, GT);
745 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747 case Instruction::GOTO:
748 case Instruction::GOTO_16:
749 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000750 int32_t offset = instruction.GetTargetOffset();
751 PotentiallyAddSuspendCheck(offset, dex_offset);
752 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000753 DCHECK(target != nullptr);
754 current_block_->AddInstruction(new (arena_) HGoto());
755 current_block_->AddSuccessor(target);
756 current_block_ = nullptr;
757 break;
758 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000759
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100760 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100761 DCHECK_NE(return_type_, Primitive::kPrimNot);
762 DCHECK_NE(return_type_, Primitive::kPrimLong);
763 DCHECK_NE(return_type_, Primitive::kPrimDouble);
764 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100765 break;
766 }
767
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100768 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100769 DCHECK(return_type_ == Primitive::kPrimNot);
770 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 break;
772 }
773
774 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100775 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
776 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777 break;
778 }
779
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100780 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100781 case Instruction::INVOKE_DIRECT:
782 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000783 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100785 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700786 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100787 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
788 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100789 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100790 break;
791 }
792
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100793 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100794 case Instruction::INVOKE_DIRECT_RANGE:
795 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100796 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100797 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
798 uint32_t register_index = instruction.VRegC();
799 if (!BuildInvoke(instruction, dex_offset, method_idx,
800 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100801 return false;
802 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000803 break;
804 }
805
Roland Levillain88cb1752014-10-20 16:36:47 +0100806 case Instruction::NEG_INT: {
807 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
808 break;
809 }
810
Roland Levillain2e07b4f2014-10-23 18:12:09 +0100811 case Instruction::NEG_LONG: {
812 Unop_12x<HNeg>(instruction, Primitive::kPrimLong);
813 break;
814 }
815
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100816 case Instruction::NOT_INT: {
817 Unop_12x<HNot>(instruction, Primitive::kPrimInt);
818 break;
819 }
820
Roland Levillain70566432014-10-24 16:20:17 +0100821 case Instruction::NOT_LONG: {
822 Unop_12x<HNot>(instruction, Primitive::kPrimLong);
823 break;
824 }
825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000826 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100827 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100828 break;
829 }
830
831 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100832 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100833 break;
834 }
835
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100836 case Instruction::ADD_DOUBLE: {
837 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
838 break;
839 }
840
841 case Instruction::ADD_FLOAT: {
842 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
843 break;
844 }
845
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100846 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100847 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 break;
849 }
850
851 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100852 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000853 break;
854 }
855
Calin Juravle096cc022014-10-23 17:01:13 +0100856 case Instruction::SUB_FLOAT: {
857 Binop_23x<HSub>(instruction, Primitive::kPrimFloat);
858 break;
859 }
860
861 case Instruction::SUB_DOUBLE: {
862 Binop_23x<HSub>(instruction, Primitive::kPrimDouble);
863 break;
864 }
865
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000866 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100867 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
868 break;
869 }
870
Calin Juravle34bacdf2014-10-07 20:23:36 +0100871 case Instruction::MUL_INT: {
872 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
873 break;
874 }
875
876 case Instruction::MUL_LONG: {
877 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
878 break;
879 }
880
Calin Juravleb5bfa962014-10-21 18:02:24 +0100881 case Instruction::MUL_FLOAT: {
882 Binop_23x<HMul>(instruction, Primitive::kPrimFloat);
883 break;
884 }
885
886 case Instruction::MUL_DOUBLE: {
887 Binop_23x<HMul>(instruction, Primitive::kPrimDouble);
888 break;
889 }
890
Calin Juravle7c4954d2014-10-28 16:57:40 +0000891 case Instruction::DIV_FLOAT: {
892 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat);
893 break;
894 }
895
896 case Instruction::DIV_DOUBLE: {
897 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble);
898 break;
899 }
900
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100901 case Instruction::ADD_LONG_2ADDR: {
902 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100903 break;
904 }
905
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Instruction::ADD_DOUBLE_2ADDR: {
907 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
908 break;
909 }
910
911 case Instruction::ADD_FLOAT_2ADDR: {
912 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
913 break;
914 }
915
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100916 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100917 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
918 break;
919 }
920
921 case Instruction::SUB_LONG_2ADDR: {
922 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000923 break;
924 }
925
Calin Juravle096cc022014-10-23 17:01:13 +0100926 case Instruction::SUB_FLOAT_2ADDR: {
927 Binop_12x<HSub>(instruction, Primitive::kPrimFloat);
928 break;
929 }
930
931 case Instruction::SUB_DOUBLE_2ADDR: {
932 Binop_12x<HSub>(instruction, Primitive::kPrimDouble);
933 break;
934 }
935
Calin Juravle34bacdf2014-10-07 20:23:36 +0100936 case Instruction::MUL_INT_2ADDR: {
937 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
938 break;
939 }
940
941 case Instruction::MUL_LONG_2ADDR: {
942 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
943 break;
944 }
945
Calin Juravleb5bfa962014-10-21 18:02:24 +0100946 case Instruction::MUL_FLOAT_2ADDR: {
947 Binop_12x<HMul>(instruction, Primitive::kPrimFloat);
948 break;
949 }
950
951 case Instruction::MUL_DOUBLE_2ADDR: {
952 Binop_12x<HMul>(instruction, Primitive::kPrimDouble);
953 break;
954 }
955
Calin Juravle7c4954d2014-10-28 16:57:40 +0000956 case Instruction::DIV_FLOAT_2ADDR: {
957 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat);
958 break;
959 }
960
961 case Instruction::DIV_DOUBLE_2ADDR: {
962 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble);
963 break;
964 }
965
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000966 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100967 Binop_22s<HAdd>(instruction, false);
968 break;
969 }
970
971 case Instruction::RSUB_INT: {
972 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000973 break;
974 }
975
Calin Juravle34bacdf2014-10-07 20:23:36 +0100976 case Instruction::MUL_INT_LIT16: {
977 Binop_22s<HMul>(instruction, false);
978 break;
979 }
980
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000981 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100982 Binop_22b<HAdd>(instruction, false);
983 break;
984 }
985
986 case Instruction::RSUB_INT_LIT8: {
987 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000988 break;
989 }
990
Calin Juravle34bacdf2014-10-07 20:23:36 +0100991 case Instruction::MUL_INT_LIT8: {
992 Binop_22b<HMul>(instruction, false);
993 break;
994 }
995
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100996 case Instruction::NEW_INSTANCE: {
997 current_block_->AddInstruction(
998 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
999 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
1000 break;
1001 }
1002
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001003 case Instruction::NEW_ARRAY: {
1004 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt);
1005 current_block_->AddInstruction(
1006 new (arena_) HNewArray(length, dex_offset, instruction.VRegC_22c()));
1007 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction());
1008 break;
1009 }
1010
1011 case Instruction::FILLED_NEW_ARRAY: {
1012 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
1013 uint32_t type_index = instruction.VRegB_35c();
1014 uint32_t args[5];
1015 instruction.GetVarArgs(args);
1016 BuildFilledNewArray(dex_offset, type_index, number_of_vreg_arguments, false, args, 0);
1017 break;
1018 }
1019
1020 case Instruction::FILLED_NEW_ARRAY_RANGE: {
1021 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
1022 uint32_t type_index = instruction.VRegB_3rc();
1023 uint32_t register_index = instruction.VRegC_3rc();
1024 BuildFilledNewArray(
1025 dex_offset, type_index, number_of_vreg_arguments, true, nullptr, register_index);
1026 break;
1027 }
1028
1029 case Instruction::FILL_ARRAY_DATA: {
1030 Temporaries temps(graph_, 1);
1031 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot);
1032 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_offset);
1033 current_block_->AddInstruction(null_check);
1034 temps.Add(null_check);
1035
1036 HInstruction* length = new (arena_) HArrayLength(null_check);
1037 current_block_->AddInstruction(length);
1038
1039 int32_t payload_offset = instruction.VRegB_31t() + dex_offset;
1040 const Instruction::ArrayDataPayload* payload =
1041 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset);
1042 const uint8_t* data = payload->data;
1043 uint32_t element_count = payload->element_count;
1044
1045 // Implementation of this DEX instruction seems to be that the bounds check is
1046 // done before doing any stores.
1047 HInstruction* last_index = GetIntConstant(payload->element_count - 1);
1048 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_offset));
1049
1050 switch (payload->element_width) {
1051 case 1:
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001052 BuildFillArrayData(null_check,
1053 reinterpret_cast<const int8_t*>(data),
1054 element_count,
1055 Primitive::kPrimByte,
1056 dex_offset);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001057 break;
1058 case 2:
1059 BuildFillArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001060 reinterpret_cast<const int16_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001061 element_count,
1062 Primitive::kPrimShort,
1063 dex_offset);
1064 break;
1065 case 4:
1066 BuildFillArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001067 reinterpret_cast<const int32_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001068 element_count,
1069 Primitive::kPrimInt,
1070 dex_offset);
1071 break;
1072 case 8:
1073 BuildFillWideArrayData(null_check,
Nicolas Geoffray8d6ae522014-10-23 18:32:13 +01001074 reinterpret_cast<const int64_t*>(data),
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001075 element_count,
1076 dex_offset);
1077 break;
1078 default:
1079 LOG(FATAL) << "Unknown element width for " << payload->element_width;
1080 }
1081 break;
1082 }
1083
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001084 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -07001085 case Instruction::MOVE_RESULT_WIDE:
1086 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001087 UpdateLocal(instruction.VRegA(), latest_result_);
1088 latest_result_ = nullptr;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001089 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001091 case Instruction::CMP_LONG: {
1092 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
1093 break;
1094 }
1095
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001096 case Instruction::NOP:
1097 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001098
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001099 case Instruction::IGET:
1100 case Instruction::IGET_WIDE:
1101 case Instruction::IGET_OBJECT:
1102 case Instruction::IGET_BOOLEAN:
1103 case Instruction::IGET_BYTE:
1104 case Instruction::IGET_CHAR:
1105 case Instruction::IGET_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001106 if (!BuildInstanceFieldAccess(instruction, dex_offset, false)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001107 return false;
1108 }
1109 break;
1110 }
1111
1112 case Instruction::IPUT:
1113 case Instruction::IPUT_WIDE:
1114 case Instruction::IPUT_OBJECT:
1115 case Instruction::IPUT_BOOLEAN:
1116 case Instruction::IPUT_BYTE:
1117 case Instruction::IPUT_CHAR:
1118 case Instruction::IPUT_SHORT: {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001119 if (!BuildInstanceFieldAccess(instruction, dex_offset, true)) {
1120 return false;
1121 }
1122 break;
1123 }
1124
1125 case Instruction::SGET:
1126 case Instruction::SGET_WIDE:
1127 case Instruction::SGET_OBJECT:
1128 case Instruction::SGET_BOOLEAN:
1129 case Instruction::SGET_BYTE:
1130 case Instruction::SGET_CHAR:
1131 case Instruction::SGET_SHORT: {
1132 if (!BuildStaticFieldAccess(instruction, dex_offset, false)) {
1133 return false;
1134 }
1135 break;
1136 }
1137
1138 case Instruction::SPUT:
1139 case Instruction::SPUT_WIDE:
1140 case Instruction::SPUT_OBJECT:
1141 case Instruction::SPUT_BOOLEAN:
1142 case Instruction::SPUT_BYTE:
1143 case Instruction::SPUT_CHAR:
1144 case Instruction::SPUT_SHORT: {
1145 if (!BuildStaticFieldAccess(instruction, dex_offset, true)) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001146 return false;
1147 }
1148 break;
1149 }
1150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001151#define ARRAY_XX(kind, anticipated_type) \
1152 case Instruction::AGET##kind: { \
1153 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
1154 break; \
1155 } \
1156 case Instruction::APUT##kind: { \
1157 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
1158 break; \
1159 }
1160
1161 ARRAY_XX(, Primitive::kPrimInt);
1162 ARRAY_XX(_WIDE, Primitive::kPrimLong);
1163 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
1164 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
1165 ARRAY_XX(_BYTE, Primitive::kPrimByte);
1166 ARRAY_XX(_CHAR, Primitive::kPrimChar);
1167 ARRAY_XX(_SHORT, Primitive::kPrimShort);
1168
Nicolas Geoffray39468442014-09-02 15:17:15 +01001169 case Instruction::ARRAY_LENGTH: {
1170 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
1171 current_block_->AddInstruction(new (arena_) HArrayLength(object));
1172 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
1173 break;
1174 }
1175
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001176 case Instruction::CONST_STRING: {
1177 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_offset));
1178 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction());
1179 break;
1180 }
1181
1182 case Instruction::CONST_STRING_JUMBO: {
1183 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_offset));
1184 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction());
1185 break;
1186 }
1187
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001188 default:
1189 return false;
1190 }
1191 return true;
1192}
1193
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001194HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001195 if (constant0_ != nullptr) {
1196 return constant0_;
1197 }
1198 constant0_ = new(arena_) HIntConstant(0);
1199 entry_block_->AddInstruction(constant0_);
1200 return constant0_;
1201}
1202
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001203HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001204 if (constant1_ != nullptr) {
1205 return constant1_;
1206 }
1207 constant1_ = new(arena_) HIntConstant(1);
1208 entry_block_->AddInstruction(constant1_);
1209 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001210}
1211
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001213 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 case 0: return GetIntConstant0();
1215 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001216 default: {
1217 HIntConstant* instruction = new (arena_) HIntConstant(constant);
1218 entry_block_->AddInstruction(instruction);
1219 return instruction;
1220 }
1221 }
1222}
1223
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
1225 HLongConstant* instruction = new (arena_) HLongConstant(constant);
1226 entry_block_->AddInstruction(instruction);
1227 return instruction;
1228}
1229
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
1231 return locals_.Get(register_index);
1232}
1233
1234void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
1235 HLocal* local = GetLocalAt(register_index);
1236 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
1237}
1238
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001240 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001242 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001243}
1244
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001245} // namespace art