blob: 2f1a092ea83b69c9e583b71aad5ffd128118ade4 [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_;
174
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);
419 return true;
420}
421
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100422bool HGraphBuilder::BuildFieldAccess(const Instruction& instruction,
423 uint32_t dex_offset,
424 bool is_put) {
425 uint32_t source_or_dest_reg = instruction.VRegA_22c();
426 uint32_t obj_reg = instruction.VRegB_22c();
427 uint16_t field_index = instruction.VRegC_22c();
428
429 ScopedObjectAccess soa(Thread::Current());
430 StackHandleScope<1> hs(soa.Self());
431 Handle<mirror::ArtField> resolved_field(hs.NewHandle(
432 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa)));
433
434 if (resolved_field.Get() == nullptr) {
435 return false;
436 }
437 if (resolved_field->IsVolatile()) {
438 return false;
439 }
440
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100441 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType();
442 if (!IsTypeSupported(field_type)) {
443 return false;
444 }
445
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot);
447 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_offset));
448 if (is_put) {
449 Temporaries temps(graph_, 1);
450 HInstruction* null_check = current_block_->GetLastInstruction();
451 // We need one temporary for the null check.
452 temps.Add(null_check);
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100453 HInstruction* value = LoadLocal(source_or_dest_reg, field_type);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100454 current_block_->AddInstruction(new (arena_) HInstanceFieldSet(
455 null_check,
456 value,
Nicolas Geoffray39468442014-09-02 15:17:15 +0100457 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100458 resolved_field->GetOffset()));
459 } else {
460 current_block_->AddInstruction(new (arena_) HInstanceFieldGet(
461 current_block_->GetLastInstruction(),
Nicolas Geoffrayabed4d02014-07-14 15:24:11 +0100462 field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100463 resolved_field->GetOffset()));
464
465 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
466 }
467 return true;
468}
469
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100470void HGraphBuilder::BuildArrayAccess(const Instruction& instruction,
471 uint32_t dex_offset,
472 bool is_put,
473 Primitive::Type anticipated_type) {
474 uint8_t source_or_dest_reg = instruction.VRegA_23x();
475 uint8_t array_reg = instruction.VRegB_23x();
476 uint8_t index_reg = instruction.VRegC_23x();
477
478 DCHECK(IsTypeSupported(anticipated_type));
479
480 // We need one temporary for the null check, one for the index, and one for the length.
481 Temporaries temps(graph_, 3);
482
483 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot);
484 object = new (arena_) HNullCheck(object, dex_offset);
485 current_block_->AddInstruction(object);
486 temps.Add(object);
487
488 HInstruction* length = new (arena_) HArrayLength(object);
489 current_block_->AddInstruction(length);
490 temps.Add(length);
491 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt);
492 index = new (arena_) HBoundsCheck(index, length, dex_offset);
493 current_block_->AddInstruction(index);
494 temps.Add(index);
495 if (is_put) {
496 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type);
497 // TODO: Insert a type check node if the type is Object.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100498 current_block_->AddInstruction(new (arena_) HArraySet(
499 object, index, value, anticipated_type, dex_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100500 } else {
501 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type));
502 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction());
503 }
504}
505
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000506void HGraphBuilder::PotentiallyAddSuspendCheck(int32_t target_offset, uint32_t dex_offset) {
507 if (target_offset <= 0) {
508 // Unconditionnally add a suspend check to backward branches. We can remove
509 // them after we recognize loops in the graph.
510 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_offset));
511 }
512}
513
514bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_offset) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000515 if (current_block_ == nullptr) {
516 return true; // Dead code
517 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000518
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000519 switch (instruction.Opcode()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000520 case Instruction::CONST_4: {
521 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100522 HIntConstant* constant = GetIntConstant(instruction.VRegB_11n());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000523 UpdateLocal(register_index, constant);
524 break;
525 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000526
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100527 case Instruction::CONST_16: {
528 int32_t register_index = instruction.VRegA();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100529 HIntConstant* constant = GetIntConstant(instruction.VRegB_21s());
530 UpdateLocal(register_index, constant);
531 break;
532 }
533
Dave Allison20dfc792014-06-16 20:44:29 -0700534 case Instruction::CONST: {
535 int32_t register_index = instruction.VRegA();
536 HIntConstant* constant = GetIntConstant(instruction.VRegB_31i());
537 UpdateLocal(register_index, constant);
538 break;
539 }
540
541 case Instruction::CONST_HIGH16: {
542 int32_t register_index = instruction.VRegA();
543 HIntConstant* constant = GetIntConstant(instruction.VRegB_21h() << 16);
544 UpdateLocal(register_index, constant);
545 break;
546 }
547
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100548 case Instruction::CONST_WIDE_16: {
549 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700550 // Get 16 bits of constant value, sign extended to 64 bits.
551 int64_t value = instruction.VRegB_21s();
552 value <<= 48;
553 value >>= 48;
554 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100555 UpdateLocal(register_index, constant);
556 break;
557 }
558
559 case Instruction::CONST_WIDE_32: {
560 int32_t register_index = instruction.VRegA();
Dave Allison20dfc792014-06-16 20:44:29 -0700561 // Get 32 bits of constant value, sign extended to 64 bits.
562 int64_t value = instruction.VRegB_31i();
563 value <<= 32;
564 value >>= 32;
565 HLongConstant* constant = GetLongConstant(value);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100566 UpdateLocal(register_index, constant);
567 break;
568 }
569
570 case Instruction::CONST_WIDE: {
571 int32_t register_index = instruction.VRegA();
572 HLongConstant* constant = GetLongConstant(instruction.VRegB_51l());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100573 UpdateLocal(register_index, constant);
574 break;
575 }
576
Dave Allison20dfc792014-06-16 20:44:29 -0700577 case Instruction::CONST_WIDE_HIGH16: {
578 int32_t register_index = instruction.VRegA();
579 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48;
580 HLongConstant* constant = GetLongConstant(value);
581 UpdateLocal(register_index, constant);
582 break;
583 }
584
585 // TODO: these instructions are also used to move floating point values, so what is
586 // the type (int or float)?
587 case Instruction::MOVE:
588 case Instruction::MOVE_FROM16:
589 case Instruction::MOVE_16: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100590 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100591 UpdateLocal(instruction.VRegA(), value);
592 break;
593 }
594
Dave Allison20dfc792014-06-16 20:44:29 -0700595 // TODO: these instructions are also used to move floating point values, so what is
596 // the type (long or double)?
597 case Instruction::MOVE_WIDE:
598 case Instruction::MOVE_WIDE_FROM16:
599 case Instruction::MOVE_WIDE_16: {
600 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong);
601 UpdateLocal(instruction.VRegA(), value);
602 break;
603 }
604
605 case Instruction::MOVE_OBJECT:
606 case Instruction::MOVE_OBJECT_16:
607 case Instruction::MOVE_OBJECT_FROM16: {
608 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot);
609 UpdateLocal(instruction.VRegA(), value);
610 break;
611 }
612
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000613 case Instruction::RETURN_VOID: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100614 BuildReturn(instruction, Primitive::kPrimVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000615 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000616 }
617
Dave Allison20dfc792014-06-16 20:44:29 -0700618#define IF_XX(comparison, cond) \
619 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_offset); break; \
620 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_offset); break
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100621
Dave Allison20dfc792014-06-16 20:44:29 -0700622 IF_XX(HEqual, EQ);
623 IF_XX(HNotEqual, NE);
624 IF_XX(HLessThan, LT);
625 IF_XX(HLessThanOrEqual, LE);
626 IF_XX(HGreaterThan, GT);
627 IF_XX(HGreaterThanOrEqual, GE);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000628
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629 case Instruction::GOTO:
630 case Instruction::GOTO_16:
631 case Instruction::GOTO_32: {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000632 int32_t offset = instruction.GetTargetOffset();
633 PotentiallyAddSuspendCheck(offset, dex_offset);
634 HBasicBlock* target = FindBlockStartingAt(offset + dex_offset);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635 DCHECK(target != nullptr);
636 current_block_->AddInstruction(new (arena_) HGoto());
637 current_block_->AddSuccessor(target);
638 current_block_ = nullptr;
639 break;
640 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000641
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100642 case Instruction::RETURN: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100643 DCHECK_NE(return_type_, Primitive::kPrimNot);
644 DCHECK_NE(return_type_, Primitive::kPrimLong);
645 DCHECK_NE(return_type_, Primitive::kPrimDouble);
646 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100647 break;
648 }
649
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100650 case Instruction::RETURN_OBJECT: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100651 DCHECK(return_type_ == Primitive::kPrimNot);
652 BuildReturn(instruction, return_type_);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653 break;
654 }
655
656 case Instruction::RETURN_WIDE: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100657 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong);
658 BuildReturn(instruction, return_type_);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000659 break;
660 }
661
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100662 case Instruction::INVOKE_STATIC:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100663 case Instruction::INVOKE_DIRECT:
664 case Instruction::INVOKE_VIRTUAL: {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000665 uint32_t method_idx = instruction.VRegB_35c();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666 uint32_t number_of_vreg_arguments = instruction.VRegA_35c();
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100667 uint32_t args[5];
Ian Rogers29a26482014-05-02 15:27:29 -0700668 instruction.GetVarArgs(args);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100669 if (!BuildInvoke(instruction, dex_offset, method_idx, number_of_vreg_arguments, false, args, -1)) {
670 return false;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100671 }
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100672 break;
673 }
674
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100675 case Instruction::INVOKE_STATIC_RANGE:
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100676 case Instruction::INVOKE_DIRECT_RANGE:
677 case Instruction::INVOKE_VIRTUAL_RANGE: {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100678 uint32_t method_idx = instruction.VRegB_3rc();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc();
680 uint32_t register_index = instruction.VRegC();
681 if (!BuildInvoke(instruction, dex_offset, method_idx,
682 number_of_vreg_arguments, true, nullptr, register_index)) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100683 return false;
684 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000685 break;
686 }
687
Roland Levillain88cb1752014-10-20 16:36:47 +0100688 case Instruction::NEG_INT: {
689 Unop_12x<HNeg>(instruction, Primitive::kPrimInt);
690 break;
691 }
692
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000693 case Instruction::ADD_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100694 Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100695 break;
696 }
697
698 case Instruction::ADD_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100699 Binop_23x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100700 break;
701 }
702
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100703 case Instruction::ADD_DOUBLE: {
704 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble);
705 break;
706 }
707
708 case Instruction::ADD_FLOAT: {
709 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat);
710 break;
711 }
712
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100713 case Instruction::SUB_INT: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100714 Binop_23x<HSub>(instruction, Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 break;
716 }
717
718 case Instruction::SUB_LONG: {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100719 Binop_23x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000720 break;
721 }
722
723 case Instruction::ADD_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100724 Binop_12x<HAdd>(instruction, Primitive::kPrimInt);
725 break;
726 }
727
Calin Juravle34bacdf2014-10-07 20:23:36 +0100728 case Instruction::MUL_INT: {
729 Binop_23x<HMul>(instruction, Primitive::kPrimInt);
730 break;
731 }
732
733 case Instruction::MUL_LONG: {
734 Binop_23x<HMul>(instruction, Primitive::kPrimLong);
735 break;
736 }
737
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 case Instruction::ADD_LONG_2ADDR: {
739 Binop_12x<HAdd>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100740 break;
741 }
742
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100743 case Instruction::ADD_DOUBLE_2ADDR: {
744 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble);
745 break;
746 }
747
748 case Instruction::ADD_FLOAT_2ADDR: {
749 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat);
750 break;
751 }
752
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100753 case Instruction::SUB_INT_2ADDR: {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100754 Binop_12x<HSub>(instruction, Primitive::kPrimInt);
755 break;
756 }
757
758 case Instruction::SUB_LONG_2ADDR: {
759 Binop_12x<HSub>(instruction, Primitive::kPrimLong);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000760 break;
761 }
762
Calin Juravle34bacdf2014-10-07 20:23:36 +0100763 case Instruction::MUL_INT_2ADDR: {
764 Binop_12x<HMul>(instruction, Primitive::kPrimInt);
765 break;
766 }
767
768 case Instruction::MUL_LONG_2ADDR: {
769 Binop_12x<HMul>(instruction, Primitive::kPrimLong);
770 break;
771 }
772
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000773 case Instruction::ADD_INT_LIT16: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100774 Binop_22s<HAdd>(instruction, false);
775 break;
776 }
777
778 case Instruction::RSUB_INT: {
779 Binop_22s<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000780 break;
781 }
782
Calin Juravle34bacdf2014-10-07 20:23:36 +0100783 case Instruction::MUL_INT_LIT16: {
784 Binop_22s<HMul>(instruction, false);
785 break;
786 }
787
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000788 case Instruction::ADD_INT_LIT8: {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100789 Binop_22b<HAdd>(instruction, false);
790 break;
791 }
792
793 case Instruction::RSUB_INT_LIT8: {
794 Binop_22b<HSub>(instruction, true);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000795 break;
796 }
797
Calin Juravle34bacdf2014-10-07 20:23:36 +0100798 case Instruction::MUL_INT_LIT8: {
799 Binop_22b<HMul>(instruction, false);
800 break;
801 }
802
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100803 case Instruction::NEW_INSTANCE: {
804 current_block_->AddInstruction(
805 new (arena_) HNewInstance(dex_offset, instruction.VRegB_21c()));
806 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
807 break;
808 }
809
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100810 case Instruction::MOVE_RESULT:
Dave Allison20dfc792014-06-16 20:44:29 -0700811 case Instruction::MOVE_RESULT_WIDE:
812 case Instruction::MOVE_RESULT_OBJECT:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction());
814 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100815
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100816 case Instruction::CMP_LONG: {
817 Binop_23x<HCompare>(instruction, Primitive::kPrimLong);
818 break;
819 }
820
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000821 case Instruction::NOP:
822 break;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000823
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100824 case Instruction::IGET:
825 case Instruction::IGET_WIDE:
826 case Instruction::IGET_OBJECT:
827 case Instruction::IGET_BOOLEAN:
828 case Instruction::IGET_BYTE:
829 case Instruction::IGET_CHAR:
830 case Instruction::IGET_SHORT: {
831 if (!BuildFieldAccess(instruction, dex_offset, false)) {
832 return false;
833 }
834 break;
835 }
836
837 case Instruction::IPUT:
838 case Instruction::IPUT_WIDE:
839 case Instruction::IPUT_OBJECT:
840 case Instruction::IPUT_BOOLEAN:
841 case Instruction::IPUT_BYTE:
842 case Instruction::IPUT_CHAR:
843 case Instruction::IPUT_SHORT: {
844 if (!BuildFieldAccess(instruction, dex_offset, true)) {
845 return false;
846 }
847 break;
848 }
849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100850#define ARRAY_XX(kind, anticipated_type) \
851 case Instruction::AGET##kind: { \
852 BuildArrayAccess(instruction, dex_offset, false, anticipated_type); \
853 break; \
854 } \
855 case Instruction::APUT##kind: { \
856 BuildArrayAccess(instruction, dex_offset, true, anticipated_type); \
857 break; \
858 }
859
860 ARRAY_XX(, Primitive::kPrimInt);
861 ARRAY_XX(_WIDE, Primitive::kPrimLong);
862 ARRAY_XX(_OBJECT, Primitive::kPrimNot);
863 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean);
864 ARRAY_XX(_BYTE, Primitive::kPrimByte);
865 ARRAY_XX(_CHAR, Primitive::kPrimChar);
866 ARRAY_XX(_SHORT, Primitive::kPrimShort);
867
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 case Instruction::ARRAY_LENGTH: {
869 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot);
870 current_block_->AddInstruction(new (arena_) HArrayLength(object));
871 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction());
872 break;
873 }
874
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875 default:
876 return false;
877 }
878 return true;
879}
880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100881HIntConstant* HGraphBuilder::GetIntConstant0() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000882 if (constant0_ != nullptr) {
883 return constant0_;
884 }
885 constant0_ = new(arena_) HIntConstant(0);
886 entry_block_->AddInstruction(constant0_);
887 return constant0_;
888}
889
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890HIntConstant* HGraphBuilder::GetIntConstant1() {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891 if (constant1_ != nullptr) {
892 return constant1_;
893 }
894 constant1_ = new(arena_) HIntConstant(1);
895 entry_block_->AddInstruction(constant1_);
896 return constant1_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897}
898
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100899HIntConstant* HGraphBuilder::GetIntConstant(int32_t constant) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900 switch (constant) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100901 case 0: return GetIntConstant0();
902 case 1: return GetIntConstant1();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000903 default: {
904 HIntConstant* instruction = new (arena_) HIntConstant(constant);
905 entry_block_->AddInstruction(instruction);
906 return instruction;
907 }
908 }
909}
910
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911HLongConstant* HGraphBuilder::GetLongConstant(int64_t constant) {
912 HLongConstant* instruction = new (arena_) HLongConstant(constant);
913 entry_block_->AddInstruction(instruction);
914 return instruction;
915}
916
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000917HLocal* HGraphBuilder::GetLocalAt(int register_index) const {
918 return locals_.Get(register_index);
919}
920
921void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const {
922 HLocal* local = GetLocalAt(register_index);
923 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction));
924}
925
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100926HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000927 HLocal* local = GetLocalAt(register_index);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100928 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type));
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000929 return current_block_->GetLastInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000930}
931
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932} // namespace art