blob: 474b89e8621b82705fcc60b9d72e7907a3d877a4 [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 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
17#include "method_compiler.h"
18
Logan Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
Logan Chienc670a8d2011-12-20 21:25:56 +080038#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080040#include <llvm/GlobalVariable.h>
41#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043namespace art {
44namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080045
Logan Chien42e0e152012-01-13 15:42:36 +080046using namespace runtime_support;
47
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048
Logan Chien8b977d32012-02-21 19:14:55 +080049MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080050 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080051 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080052 : cunit_(cunit), compiler_(compiler),
53 class_linker_(oat_compilation_unit->class_linker_),
54 class_loader_(oat_compilation_unit->class_loader_),
55 dex_file_(oat_compilation_unit->dex_file_),
56 dex_cache_(oat_compilation_unit->dex_cache_),
57 code_item_(oat_compilation_unit->code_item_),
58 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080059 method_idx_(oat_compilation_unit->method_idx_),
60 access_flags_(oat_compilation_unit->access_flags_),
61 module_(cunit->GetModule()),
62 context_(cunit->GetLLVMContext()),
TDYa1271d7e5102012-05-13 09:27:05 -070063 irb_(*cunit->GetIRBuilder()),
64 func_(NULL),
65 regs_(code_item_->registers_size_),
66 reg_to_shadow_frame_index_(code_item_->registers_size_, -1),
67 retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070068 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080069 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080070 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080071 basic_blocks_(code_item_->insns_size_in_code_units_),
72 basic_block_landing_pads_(code_item_->tries_size_, NULL),
73 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080074 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075}
76
77
78MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080079 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080080}
81
82
Logan Chien0b827102011-12-20 19:46:14 +080083void MethodCompiler::CreateFunction() {
84 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080085 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Get function type
88 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080089 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080090
91 // Create function
92 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
93 func_name, module_);
94
TDYa12767ae8ff2012-05-02 19:08:02 -070095#if !defined(NDEBUG)
Logan Chien0b827102011-12-20 19:46:14 +080096 // Set argument name
97 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
98 llvm::Function::arg_iterator arg_end(func_->arg_end());
99
100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("method");
102 ++arg_iter;
103
Logan Chiendd361c92012-04-10 23:40:37 +0800104 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800105 DCHECK_NE(arg_iter, arg_end);
106 arg_iter->setName("this");
107 ++arg_iter;
108 }
109
110 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
111 arg_iter->setName(StringPrintf("a%u", i));
112 }
TDYa12767ae8ff2012-05-02 19:08:02 -0700113#endif
Logan Chien0b827102011-12-20 19:46:14 +0800114}
115
116
117llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
118 bool is_static) {
119 // Get method signature
120 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
121
Logan Chien8faf8022012-02-24 12:25:29 +0800122 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800123 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800124 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800125
126 // Get return type
127 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
128
129 // Get argument type
130 std::vector<llvm::Type*> args_type;
131
132 args_type.push_back(irb_.getJObjectTy()); // method object pointer
133
134 if (!is_static) {
135 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
136 }
137
Logan Chien8faf8022012-02-24 12:25:29 +0800138 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800139 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
140 }
141
142 return llvm::FunctionType::get(ret_type, args_type, false);
143}
144
145
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800146void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800147 // Create basic blocks for prologue
TDYa12799489132012-04-29 01:27:58 -0700148#if !defined(NDEBUG)
149 // Add a BasicBlock named as PrettyMethod for debugging.
150 llvm::BasicBlock* entry =
151 llvm::BasicBlock::Create(*context_, PrettyMethod(method_idx_, *dex_file_), func_);
152#endif
Logan Chienc670a8d2011-12-20 21:25:56 +0800153 basic_block_reg_alloca_ =
154 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
155
TDYa12797339c42012-04-29 01:26:35 -0700156 basic_block_stack_overflow_ =
157 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
158
Logan Chien8dfcbea2012-02-17 18:50:32 +0800159 basic_block_shadow_frame_alloca_ =
160 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
161
Logan Chiend6ececa2011-12-27 16:20:15 +0800162 basic_block_reg_arg_init_ =
163 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
164
TDYa12799489132012-04-29 01:27:58 -0700165#if !defined(NDEBUG)
166 irb_.SetInsertPoint(entry);
167 irb_.CreateBr(basic_block_reg_alloca_);
168#endif
169
Logan Chienc670a8d2011-12-20 21:25:56 +0800170 // Create register array
171 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
TDYa1271d7e5102012-05-13 09:27:05 -0700172 regs_[r] = DalvikReg::CreateLocalVarReg(*this, r);
Logan Chienc670a8d2011-12-20 21:25:56 +0800173 }
174
175 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800176
Logan Chien8dfcbea2012-02-17 18:50:32 +0800177 // Create Shadow Frame
TDYa127cc1b4c32012-05-15 07:31:37 -0700178 if (method_info_.need_shadow_frame) {
179 EmitPrologueAllocShadowFrame();
180 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800181
Logan Chiend6ececa2011-12-27 16:20:15 +0800182 // Store argument to dalvik register
183 irb_.SetInsertPoint(basic_block_reg_arg_init_);
184 EmitPrologueAssignArgRegister();
185
186 // Branch to start address
187 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800188}
189
190
TDYa1274165a832012-04-03 17:47:16 -0700191void MethodCompiler::EmitStackOverflowCheck() {
192 irb_.SetInsertPoint(basic_block_stack_overflow_);
193
194 // Call llvm intrinsic function to get frame address.
195 llvm::Function* frameaddress =
196 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
197
198 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
199 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
200
201 // Cast i8* to int
202 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
203
204 // Get thread.stack_end_
205 llvm::Value* thread_object_addr =
206 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
207
TDYa127ee1f59b2012-04-25 00:56:40 -0700208 llvm::Value* stack_end =
209 irb_.LoadFromObjectOffset(thread_object_addr,
210 Thread::StackEndOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700211 irb_.getPtrEquivIntTy(),
212 kTBAARuntimeInfo);
TDYa1274165a832012-04-03 17:47:16 -0700213
214 // Check the frame address < thread.stack_end_ ?
215 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
216
217 llvm::BasicBlock* block_exception =
218 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
219
220 llvm::BasicBlock* block_continue =
221 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
222
TDYa127ac7b5bb2012-05-11 13:17:49 -0700223 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
TDYa1274165a832012-04-03 17:47:16 -0700224
225 // If stack overflow, throw exception.
226 irb_.SetInsertPoint(block_exception);
227 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
228
229 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800230 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700231 if (ret_shorty == 'V') {
232 irb_.CreateRetVoid();
233 } else {
234 irb_.CreateRet(irb_.getJZero(ret_shorty));
235 }
236
237 basic_block_stack_overflow_ = block_continue;
238}
239
240
Logan Chienc670a8d2011-12-20 21:25:56 +0800241void MethodCompiler::EmitPrologueLastBranch() {
242 irb_.SetInsertPoint(basic_block_reg_alloca_);
TDYa12797339c42012-04-29 01:26:35 -0700243 irb_.CreateBr(basic_block_stack_overflow_);
244
TDYa127cc1b4c32012-05-15 07:31:37 -0700245 // If a method will not call to other method, and the method is small, we can avoid stack overflow
246 // check.
247 if (method_info_.has_invoke ||
248 code_item_->registers_size_ > 32) { // Small leaf function is OK given
249 // the 8KB reserved at Stack End
250 EmitStackOverflowCheck();
251 }
TDYa12797339c42012-04-29 01:26:35 -0700252
253 irb_.SetInsertPoint(basic_block_stack_overflow_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800254 irb_.CreateBr(basic_block_shadow_frame_alloca_);
255
256 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800257 irb_.CreateBr(basic_block_reg_arg_init_);
258}
259
260
Logan Chien8dfcbea2012-02-17 18:50:32 +0800261void MethodCompiler::EmitPrologueAllocShadowFrame() {
262 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
263
264 // Allocate the shadow frame now!
TDYa1271d7e5102012-05-13 09:27:05 -0700265 uint32_t sirt_size = 0;
TDYa127f1652862012-05-16 21:44:35 -0700266 if (method_info_.need_shadow_frame_entry) {
267 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
268 if (IsRegCanBeObject(i)) {
269 reg_to_shadow_frame_index_[i] = sirt_size++;
270 }
TDYa1271d7e5102012-05-13 09:27:05 -0700271 }
272 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800273
274 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
275 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
276
277 // Zero-initialization of the shadow frame
278 llvm::ConstantAggregateZero* zero_initializer =
279 llvm::ConstantAggregateZero::get(shadow_frame_type);
280
TDYa127d955bec2012-05-11 10:54:02 -0700281 irb_.CreateStore(zero_initializer, shadow_frame_, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800282
TDYa127ee1f59b2012-04-25 00:56:40 -0700283 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800284 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700285
286 // Store the method pointer
287 irb_.StoreToObjectOffset(shadow_frame_,
288 ShadowFrame::MethodOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700289 method_object_addr,
TDYa127d955bec2012-05-11 10:54:02 -0700290 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800291
292 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700293 irb_.StoreToObjectOffset(shadow_frame_,
294 ShadowFrame::NumberOfReferencesOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700295 irb_.getJInt(sirt_size),
TDYa127d955bec2012-05-11 10:54:02 -0700296 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800297
298 // Push the shadow frame
299 llvm::Value* shadow_frame_upcast =
300 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
301
302 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
303}
304
305
Logan Chiend6ececa2011-12-27 16:20:15 +0800306void MethodCompiler::EmitPrologueAssignArgRegister() {
307 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
308
309 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
310 llvm::Function::arg_iterator arg_end(func_->arg_end());
311
Logan Chiendd361c92012-04-10 23:40:37 +0800312 uint32_t shorty_size = 0;
313 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
314 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800315
316 ++arg_iter; // skip method object
317
Logan Chiendd361c92012-04-10 23:40:37 +0800318 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800319 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
320 ++arg_iter;
321 ++arg_reg;
322 }
323
Logan Chiendd361c92012-04-10 23:40:37 +0800324 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800325 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
326
327 ++arg_reg;
328 if (shorty[i] == 'J' || shorty[i] == 'D') {
329 // Wide types, such as long and double, are using a pair of registers
330 // to store the value, so we have to increase arg_reg again.
331 ++arg_reg;
332 }
333 }
334
335 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800336}
337
338
Logan Chien83426162011-12-09 09:29:50 +0800339void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800340 uint32_t dex_pc = 0;
341 while (dex_pc < code_item_->insns_size_in_code_units_) {
342 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
343 EmitInstruction(dex_pc, insn);
344 dex_pc += insn->SizeInCodeUnits();
345 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800346}
347
348
Logan Chien83426162011-12-09 09:29:50 +0800349void MethodCompiler::EmitInstruction(uint32_t dex_pc,
350 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800351
352 // Set the IRBuilder insertion point
353 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
354
Logan Chien70f94b42011-12-27 17:49:11 +0800355#define ARGS dex_pc, insn
356
357 // Dispatch the instruction
358 switch (insn->Opcode()) {
359 case Instruction::NOP:
360 EmitInsn_Nop(ARGS);
361 break;
362
363 case Instruction::MOVE:
364 case Instruction::MOVE_FROM16:
365 case Instruction::MOVE_16:
366 EmitInsn_Move(ARGS, kInt);
367 break;
368
369 case Instruction::MOVE_WIDE:
370 case Instruction::MOVE_WIDE_FROM16:
371 case Instruction::MOVE_WIDE_16:
372 EmitInsn_Move(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_OBJECT:
376 case Instruction::MOVE_OBJECT_FROM16:
377 case Instruction::MOVE_OBJECT_16:
378 EmitInsn_Move(ARGS, kObject);
379 break;
380
381 case Instruction::MOVE_RESULT:
382 EmitInsn_MoveResult(ARGS, kInt);
383 break;
384
385 case Instruction::MOVE_RESULT_WIDE:
386 EmitInsn_MoveResult(ARGS, kLong);
387 break;
388
389 case Instruction::MOVE_RESULT_OBJECT:
390 EmitInsn_MoveResult(ARGS, kObject);
391 break;
392
393 case Instruction::MOVE_EXCEPTION:
394 EmitInsn_MoveException(ARGS);
395 break;
396
397 case Instruction::RETURN_VOID:
398 EmitInsn_ReturnVoid(ARGS);
399 break;
400
401 case Instruction::RETURN:
402 case Instruction::RETURN_WIDE:
403 case Instruction::RETURN_OBJECT:
404 EmitInsn_Return(ARGS);
405 break;
406
407 case Instruction::CONST_4:
408 case Instruction::CONST_16:
409 case Instruction::CONST:
410 case Instruction::CONST_HIGH16:
411 EmitInsn_LoadConstant(ARGS, kInt);
412 break;
413
414 case Instruction::CONST_WIDE_16:
415 case Instruction::CONST_WIDE_32:
416 case Instruction::CONST_WIDE:
417 case Instruction::CONST_WIDE_HIGH16:
418 EmitInsn_LoadConstant(ARGS, kLong);
419 break;
420
421 case Instruction::CONST_STRING:
422 case Instruction::CONST_STRING_JUMBO:
423 EmitInsn_LoadConstantString(ARGS);
424 break;
425
426 case Instruction::CONST_CLASS:
427 EmitInsn_LoadConstantClass(ARGS);
428 break;
429
430 case Instruction::MONITOR_ENTER:
431 EmitInsn_MonitorEnter(ARGS);
432 break;
433
434 case Instruction::MONITOR_EXIT:
435 EmitInsn_MonitorExit(ARGS);
436 break;
437
438 case Instruction::CHECK_CAST:
439 EmitInsn_CheckCast(ARGS);
440 break;
441
442 case Instruction::INSTANCE_OF:
443 EmitInsn_InstanceOf(ARGS);
444 break;
445
446 case Instruction::ARRAY_LENGTH:
447 EmitInsn_ArrayLength(ARGS);
448 break;
449
450 case Instruction::NEW_INSTANCE:
451 EmitInsn_NewInstance(ARGS);
452 break;
453
454 case Instruction::NEW_ARRAY:
455 EmitInsn_NewArray(ARGS);
456 break;
457
458 case Instruction::FILLED_NEW_ARRAY:
459 EmitInsn_FilledNewArray(ARGS, false);
460 break;
461
462 case Instruction::FILLED_NEW_ARRAY_RANGE:
463 EmitInsn_FilledNewArray(ARGS, true);
464 break;
465
466 case Instruction::FILL_ARRAY_DATA:
467 EmitInsn_FillArrayData(ARGS);
468 break;
469
470 case Instruction::THROW:
471 EmitInsn_ThrowException(ARGS);
472 break;
473
474 case Instruction::GOTO:
475 case Instruction::GOTO_16:
476 case Instruction::GOTO_32:
477 EmitInsn_UnconditionalBranch(ARGS);
478 break;
479
480 case Instruction::PACKED_SWITCH:
481 EmitInsn_PackedSwitch(ARGS);
482 break;
483
484 case Instruction::SPARSE_SWITCH:
485 EmitInsn_SparseSwitch(ARGS);
486 break;
487
488 case Instruction::CMPL_FLOAT:
489 EmitInsn_FPCompare(ARGS, kFloat, false);
490 break;
491
492 case Instruction::CMPG_FLOAT:
493 EmitInsn_FPCompare(ARGS, kFloat, true);
494 break;
495
496 case Instruction::CMPL_DOUBLE:
497 EmitInsn_FPCompare(ARGS, kDouble, false);
498 break;
499
500 case Instruction::CMPG_DOUBLE:
501 EmitInsn_FPCompare(ARGS, kDouble, true);
502 break;
503
504 case Instruction::CMP_LONG:
505 EmitInsn_LongCompare(ARGS);
506 break;
507
508 case Instruction::IF_EQ:
509 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
510 break;
511
512 case Instruction::IF_NE:
513 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
514 break;
515
516 case Instruction::IF_LT:
517 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
518 break;
519
520 case Instruction::IF_GE:
521 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
522 break;
523
524 case Instruction::IF_GT:
525 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
526 break;
527
528 case Instruction::IF_LE:
529 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
530 break;
531
532 case Instruction::IF_EQZ:
533 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
534 break;
535
536 case Instruction::IF_NEZ:
537 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
538 break;
539
540 case Instruction::IF_LTZ:
541 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
542 break;
543
544 case Instruction::IF_GEZ:
545 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
546 break;
547
548 case Instruction::IF_GTZ:
549 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
550 break;
551
552 case Instruction::IF_LEZ:
553 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
554 break;
555
556 case Instruction::AGET:
557 EmitInsn_AGet(ARGS, kInt);
558 break;
559
560 case Instruction::AGET_WIDE:
561 EmitInsn_AGet(ARGS, kLong);
562 break;
563
564 case Instruction::AGET_OBJECT:
565 EmitInsn_AGet(ARGS, kObject);
566 break;
567
568 case Instruction::AGET_BOOLEAN:
569 EmitInsn_AGet(ARGS, kBoolean);
570 break;
571
572 case Instruction::AGET_BYTE:
573 EmitInsn_AGet(ARGS, kByte);
574 break;
575
576 case Instruction::AGET_CHAR:
577 EmitInsn_AGet(ARGS, kChar);
578 break;
579
580 case Instruction::AGET_SHORT:
581 EmitInsn_AGet(ARGS, kShort);
582 break;
583
584 case Instruction::APUT:
585 EmitInsn_APut(ARGS, kInt);
586 break;
587
588 case Instruction::APUT_WIDE:
589 EmitInsn_APut(ARGS, kLong);
590 break;
591
592 case Instruction::APUT_OBJECT:
593 EmitInsn_APut(ARGS, kObject);
594 break;
595
596 case Instruction::APUT_BOOLEAN:
597 EmitInsn_APut(ARGS, kBoolean);
598 break;
599
600 case Instruction::APUT_BYTE:
601 EmitInsn_APut(ARGS, kByte);
602 break;
603
604 case Instruction::APUT_CHAR:
605 EmitInsn_APut(ARGS, kChar);
606 break;
607
608 case Instruction::APUT_SHORT:
609 EmitInsn_APut(ARGS, kShort);
610 break;
611
612 case Instruction::IGET:
613 EmitInsn_IGet(ARGS, kInt);
614 break;
615
616 case Instruction::IGET_WIDE:
617 EmitInsn_IGet(ARGS, kLong);
618 break;
619
620 case Instruction::IGET_OBJECT:
621 EmitInsn_IGet(ARGS, kObject);
622 break;
623
624 case Instruction::IGET_BOOLEAN:
625 EmitInsn_IGet(ARGS, kBoolean);
626 break;
627
628 case Instruction::IGET_BYTE:
629 EmitInsn_IGet(ARGS, kByte);
630 break;
631
632 case Instruction::IGET_CHAR:
633 EmitInsn_IGet(ARGS, kChar);
634 break;
635
636 case Instruction::IGET_SHORT:
637 EmitInsn_IGet(ARGS, kShort);
638 break;
639
640 case Instruction::IPUT:
641 EmitInsn_IPut(ARGS, kInt);
642 break;
643
644 case Instruction::IPUT_WIDE:
645 EmitInsn_IPut(ARGS, kLong);
646 break;
647
648 case Instruction::IPUT_OBJECT:
649 EmitInsn_IPut(ARGS, kObject);
650 break;
651
652 case Instruction::IPUT_BOOLEAN:
653 EmitInsn_IPut(ARGS, kBoolean);
654 break;
655
656 case Instruction::IPUT_BYTE:
657 EmitInsn_IPut(ARGS, kByte);
658 break;
659
660 case Instruction::IPUT_CHAR:
661 EmitInsn_IPut(ARGS, kChar);
662 break;
663
664 case Instruction::IPUT_SHORT:
665 EmitInsn_IPut(ARGS, kShort);
666 break;
667
668 case Instruction::SGET:
669 EmitInsn_SGet(ARGS, kInt);
670 break;
671
672 case Instruction::SGET_WIDE:
673 EmitInsn_SGet(ARGS, kLong);
674 break;
675
676 case Instruction::SGET_OBJECT:
677 EmitInsn_SGet(ARGS, kObject);
678 break;
679
680 case Instruction::SGET_BOOLEAN:
681 EmitInsn_SGet(ARGS, kBoolean);
682 break;
683
684 case Instruction::SGET_BYTE:
685 EmitInsn_SGet(ARGS, kByte);
686 break;
687
688 case Instruction::SGET_CHAR:
689 EmitInsn_SGet(ARGS, kChar);
690 break;
691
692 case Instruction::SGET_SHORT:
693 EmitInsn_SGet(ARGS, kShort);
694 break;
695
696 case Instruction::SPUT:
697 EmitInsn_SPut(ARGS, kInt);
698 break;
699
700 case Instruction::SPUT_WIDE:
701 EmitInsn_SPut(ARGS, kLong);
702 break;
703
704 case Instruction::SPUT_OBJECT:
705 EmitInsn_SPut(ARGS, kObject);
706 break;
707
708 case Instruction::SPUT_BOOLEAN:
709 EmitInsn_SPut(ARGS, kBoolean);
710 break;
711
712 case Instruction::SPUT_BYTE:
713 EmitInsn_SPut(ARGS, kByte);
714 break;
715
716 case Instruction::SPUT_CHAR:
717 EmitInsn_SPut(ARGS, kChar);
718 break;
719
720 case Instruction::SPUT_SHORT:
721 EmitInsn_SPut(ARGS, kShort);
722 break;
723
724
725 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800726 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800727 break;
728
729 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800730 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800731 break;
732
733 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800734 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800735 break;
736
737 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800738 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800739 break;
740
741 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800742 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800743 break;
744
745 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800746 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800747 break;
748
749 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800750 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800751 break;
752
753 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800754 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800755 break;
756
757 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800758 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800759 break;
760
761 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800762 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800763 break;
764
765 case Instruction::NEG_INT:
766 EmitInsn_Neg(ARGS, kInt);
767 break;
768
769 case Instruction::NOT_INT:
770 EmitInsn_Not(ARGS, kInt);
771 break;
772
773 case Instruction::NEG_LONG:
774 EmitInsn_Neg(ARGS, kLong);
775 break;
776
777 case Instruction::NOT_LONG:
778 EmitInsn_Not(ARGS, kLong);
779 break;
780
781 case Instruction::NEG_FLOAT:
782 EmitInsn_FNeg(ARGS, kFloat);
783 break;
784
785 case Instruction::NEG_DOUBLE:
786 EmitInsn_FNeg(ARGS, kDouble);
787 break;
788
789 case Instruction::INT_TO_LONG:
790 EmitInsn_SExt(ARGS);
791 break;
792
793 case Instruction::INT_TO_FLOAT:
794 EmitInsn_IntToFP(ARGS, kInt, kFloat);
795 break;
796
797 case Instruction::INT_TO_DOUBLE:
798 EmitInsn_IntToFP(ARGS, kInt, kDouble);
799 break;
800
801 case Instruction::LONG_TO_INT:
802 EmitInsn_Trunc(ARGS);
803 break;
804
805 case Instruction::LONG_TO_FLOAT:
806 EmitInsn_IntToFP(ARGS, kLong, kFloat);
807 break;
808
809 case Instruction::LONG_TO_DOUBLE:
810 EmitInsn_IntToFP(ARGS, kLong, kDouble);
811 break;
812
813 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700814 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800815 break;
816
817 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700818 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800819 break;
820
821 case Instruction::FLOAT_TO_DOUBLE:
822 EmitInsn_FExt(ARGS);
823 break;
824
825 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700826 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800827 break;
828
829 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700830 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800831 break;
832
833 case Instruction::DOUBLE_TO_FLOAT:
834 EmitInsn_FTrunc(ARGS);
835 break;
836
837 case Instruction::INT_TO_BYTE:
838 EmitInsn_TruncAndSExt(ARGS, 8);
839 break;
840
841 case Instruction::INT_TO_CHAR:
842 EmitInsn_TruncAndZExt(ARGS, 16);
843 break;
844
845 case Instruction::INT_TO_SHORT:
846 EmitInsn_TruncAndSExt(ARGS, 16);
847 break;
848
849 case Instruction::ADD_INT:
850 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
851 break;
852
853 case Instruction::SUB_INT:
854 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
855 break;
856
857 case Instruction::MUL_INT:
858 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
859 break;
860
861 case Instruction::DIV_INT:
862 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
863 break;
864
865 case Instruction::REM_INT:
866 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
867 break;
868
869 case Instruction::AND_INT:
870 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
871 break;
872
873 case Instruction::OR_INT:
874 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
875 break;
876
877 case Instruction::XOR_INT:
878 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
879 break;
880
881 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800882 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800883 break;
884
885 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800886 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800887 break;
888
889 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800890 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800891 break;
892
893 case Instruction::ADD_LONG:
894 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
895 break;
896
897 case Instruction::SUB_LONG:
898 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
899 break;
900
901 case Instruction::MUL_LONG:
902 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
903 break;
904
905 case Instruction::DIV_LONG:
906 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
907 break;
908
909 case Instruction::REM_LONG:
910 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
911 break;
912
913 case Instruction::AND_LONG:
914 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
915 break;
916
917 case Instruction::OR_LONG:
918 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
919 break;
920
921 case Instruction::XOR_LONG:
922 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
923 break;
924
925 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800926 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800927 break;
928
929 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800930 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800931 break;
932
933 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800934 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800935 break;
936
937 case Instruction::ADD_FLOAT:
938 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
939 break;
940
941 case Instruction::SUB_FLOAT:
942 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
943 break;
944
945 case Instruction::MUL_FLOAT:
946 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
947 break;
948
949 case Instruction::DIV_FLOAT:
950 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
951 break;
952
953 case Instruction::REM_FLOAT:
954 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
955 break;
956
957 case Instruction::ADD_DOUBLE:
958 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
959 break;
960
961 case Instruction::SUB_DOUBLE:
962 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
963 break;
964
965 case Instruction::MUL_DOUBLE:
966 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
967 break;
968
969 case Instruction::DIV_DOUBLE:
970 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
971 break;
972
973 case Instruction::REM_DOUBLE:
974 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
975 break;
976
977 case Instruction::ADD_INT_2ADDR:
978 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
979 break;
980
981 case Instruction::SUB_INT_2ADDR:
982 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
983 break;
984
985 case Instruction::MUL_INT_2ADDR:
986 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
987 break;
988
989 case Instruction::DIV_INT_2ADDR:
990 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
991 break;
992
993 case Instruction::REM_INT_2ADDR:
994 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
995 break;
996
997 case Instruction::AND_INT_2ADDR:
998 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
999 break;
1000
1001 case Instruction::OR_INT_2ADDR:
1002 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1003 break;
1004
1005 case Instruction::XOR_INT_2ADDR:
1006 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1007 break;
1008
1009 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001010 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001011 break;
1012
1013 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001014 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001015 break;
1016
1017 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001018 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001019 break;
1020
1021 case Instruction::ADD_LONG_2ADDR:
1022 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1023 break;
1024
1025 case Instruction::SUB_LONG_2ADDR:
1026 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1027 break;
1028
1029 case Instruction::MUL_LONG_2ADDR:
1030 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1031 break;
1032
1033 case Instruction::DIV_LONG_2ADDR:
1034 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1035 break;
1036
1037 case Instruction::REM_LONG_2ADDR:
1038 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1039 break;
1040
1041 case Instruction::AND_LONG_2ADDR:
1042 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1043 break;
1044
1045 case Instruction::OR_LONG_2ADDR:
1046 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1047 break;
1048
1049 case Instruction::XOR_LONG_2ADDR:
1050 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1051 break;
1052
1053 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001054 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001055 break;
1056
1057 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001058 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001059 break;
1060
1061 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001062 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001063 break;
1064
1065 case Instruction::ADD_FLOAT_2ADDR:
1066 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1067 break;
1068
1069 case Instruction::SUB_FLOAT_2ADDR:
1070 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1071 break;
1072
1073 case Instruction::MUL_FLOAT_2ADDR:
1074 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1075 break;
1076
1077 case Instruction::DIV_FLOAT_2ADDR:
1078 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1079 break;
1080
1081 case Instruction::REM_FLOAT_2ADDR:
1082 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1083 break;
1084
1085 case Instruction::ADD_DOUBLE_2ADDR:
1086 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1087 break;
1088
1089 case Instruction::SUB_DOUBLE_2ADDR:
1090 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1091 break;
1092
1093 case Instruction::MUL_DOUBLE_2ADDR:
1094 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1095 break;
1096
1097 case Instruction::DIV_DOUBLE_2ADDR:
1098 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1099 break;
1100
1101 case Instruction::REM_DOUBLE_2ADDR:
1102 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1103 break;
1104
1105 case Instruction::ADD_INT_LIT16:
1106 case Instruction::ADD_INT_LIT8:
1107 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1108 break;
1109
1110 case Instruction::RSUB_INT:
1111 case Instruction::RSUB_INT_LIT8:
1112 EmitInsn_RSubImmediate(ARGS);
1113 break;
1114
1115 case Instruction::MUL_INT_LIT16:
1116 case Instruction::MUL_INT_LIT8:
1117 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1118 break;
1119
1120 case Instruction::DIV_INT_LIT16:
1121 case Instruction::DIV_INT_LIT8:
1122 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1123 break;
1124
1125 case Instruction::REM_INT_LIT16:
1126 case Instruction::REM_INT_LIT8:
1127 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1128 break;
1129
1130 case Instruction::AND_INT_LIT16:
1131 case Instruction::AND_INT_LIT8:
1132 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1133 break;
1134
1135 case Instruction::OR_INT_LIT16:
1136 case Instruction::OR_INT_LIT8:
1137 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1138 break;
1139
1140 case Instruction::XOR_INT_LIT16:
1141 case Instruction::XOR_INT_LIT8:
1142 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1143 break;
1144
1145 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001146 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001147 break;
1148
1149 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001150 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001151 break;
1152
1153 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001154 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001155 break;
1156
Logan Chien9e5f5c12012-04-10 13:51:45 +08001157 case Instruction::THROW_VERIFICATION_ERROR:
1158 EmitInsn_ThrowVerificationError(ARGS);
1159 break;
1160
Logan Chien70f94b42011-12-27 17:49:11 +08001161 case Instruction::UNUSED_3E:
1162 case Instruction::UNUSED_3F:
1163 case Instruction::UNUSED_40:
1164 case Instruction::UNUSED_41:
1165 case Instruction::UNUSED_42:
1166 case Instruction::UNUSED_43:
1167 case Instruction::UNUSED_73:
1168 case Instruction::UNUSED_79:
1169 case Instruction::UNUSED_7A:
1170 case Instruction::UNUSED_E3:
1171 case Instruction::UNUSED_E4:
1172 case Instruction::UNUSED_E5:
1173 case Instruction::UNUSED_E6:
1174 case Instruction::UNUSED_E7:
1175 case Instruction::UNUSED_E8:
1176 case Instruction::UNUSED_E9:
1177 case Instruction::UNUSED_EA:
1178 case Instruction::UNUSED_EB:
1179 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001180 case Instruction::UNUSED_EE:
1181 case Instruction::UNUSED_EF:
1182 case Instruction::UNUSED_F0:
1183 case Instruction::UNUSED_F1:
1184 case Instruction::UNUSED_F2:
1185 case Instruction::UNUSED_F3:
1186 case Instruction::UNUSED_F4:
1187 case Instruction::UNUSED_F5:
1188 case Instruction::UNUSED_F6:
1189 case Instruction::UNUSED_F7:
1190 case Instruction::UNUSED_F8:
1191 case Instruction::UNUSED_F9:
1192 case Instruction::UNUSED_FA:
1193 case Instruction::UNUSED_FB:
1194 case Instruction::UNUSED_FC:
1195 case Instruction::UNUSED_FD:
1196 case Instruction::UNUSED_FE:
1197 case Instruction::UNUSED_FF:
1198 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1199 break;
1200 }
1201
1202#undef ARGS
1203}
1204
1205
1206void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1207 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001208
1209 uint16_t insn_signature = code_item_->insns_[dex_pc];
1210
1211 if (insn_signature == Instruction::kPackedSwitchSignature ||
1212 insn_signature == Instruction::kSparseSwitchSignature ||
1213 insn_signature == Instruction::kArrayDataSignature) {
1214 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001215 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001218}
1219
1220
Logan Chien70f94b42011-12-27 17:49:11 +08001221void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1222 Instruction const* insn,
1223 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001224
Elliott Hughesadb8c672012-03-06 16:49:32 -08001225 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001226
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1228 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001229
Logan Chien70f94b42011-12-27 17:49:11 +08001230 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1231}
1232
1233
1234void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1235 Instruction const* insn,
1236 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001237
Elliott Hughesadb8c672012-03-06 16:49:32 -08001238 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001239
1240 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001241 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001242
Logan Chien70f94b42011-12-27 17:49:11 +08001243 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1244}
1245
1246
1247void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1248 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001249
Elliott Hughesadb8c672012-03-06 16:49:32 -08001250 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001251
TDYa127ee1f59b2012-04-25 00:56:40 -07001252 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001253 llvm::Value* thread_object_addr =
1254 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1255
TDYa127ee1f59b2012-04-25 00:56:40 -07001256 // Get thread-local exception field address
1257 llvm::Value* exception_object_addr =
1258 irb_.LoadFromObjectOffset(thread_object_addr,
1259 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001260 irb_.getJObjectTy(),
TDYa1278ca10052012-05-05 19:57:06 -07001261 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001262
1263 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001264 irb_.StoreToObjectOffset(thread_object_addr,
1265 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001266 irb_.getJNull(),
TDYa1278ca10052012-05-05 19:57:06 -07001267 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001268
1269 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001270 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001271
Logan Chien70f94b42011-12-27 17:49:11 +08001272 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1273}
1274
1275
1276void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1277 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001278
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001280
1281 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001283
TDYa127c8dc1012012-04-19 07:03:33 -07001284 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001285
Logan Chien6c6f12d2012-01-13 19:26:27 +08001286 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1287
1288 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001289}
1290
1291
Logan Chien9e5f5c12012-04-10 13:51:45 +08001292void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1293 Instruction const* insn) {
1294
1295 DecodedInstruction dec_insn(insn);
1296
TDYa127c8dc1012012-04-19 07:03:33 -07001297 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001298
1299 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1300 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1301 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1302
1303 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1304 method_object_addr, kind_value, ref_value);
1305
1306 EmitBranchExceptionLandingPad(dex_pc);
1307}
1308
1309
Logan Chien70f94b42011-12-27 17:49:11 +08001310void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1311 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001312 // Garbage collection safe-point
1313 EmitGuard_GarbageCollectionSuspend(dex_pc);
1314
Logan Chien8dfcbea2012-02-17 18:50:32 +08001315 // Pop the shadow frame
1316 EmitPopShadowFrame();
1317
Logan Chien8898a272011-12-27 17:51:56 +08001318 // Return!
1319 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001320}
1321
1322
1323void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1324 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001325
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001327
1328 // Garbage collection safe-point
1329 EmitGuard_GarbageCollectionSuspend(dex_pc);
1330
Logan Chien8dfcbea2012-02-17 18:50:32 +08001331 // Pop the shadow frame
1332 EmitPopShadowFrame();
1333 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1334 // the return value might be collected since the shadow stack is popped.
1335
Logan Chien8898a272011-12-27 17:51:56 +08001336 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001337 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001338 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001339
1340 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001341}
1342
1343
1344void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1345 Instruction const* insn,
1346 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001349
1350 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1351
1352 int64_t imm = 0;
1353
1354 switch (insn->Opcode()) {
1355 // 32-bit Immediate
1356 case Instruction::CONST_4:
1357 case Instruction::CONST_16:
1358 case Instruction::CONST:
1359 case Instruction::CONST_WIDE_16:
1360 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001362 break;
1363
1364 case Instruction::CONST_HIGH16:
1365 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001366 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001367 break;
1368
1369 // 64-bit Immediate
1370 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001372 break;
1373
1374 case Instruction::CONST_WIDE_HIGH16:
1375 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001377 break;
1378
1379 // Unknown opcode for load constant (unreachable)
1380 default:
1381 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1382 break;
1383 }
1384
1385 // Store the non-object register
1386 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1387 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001389
1390 // Store the object register if it is possible to be null.
1391 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001393 }
1394
Logan Chien70f94b42011-12-27 17:49:11 +08001395 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1396}
1397
1398
1399void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1400 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001401
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001403
Elliott Hughesadb8c672012-03-06 16:49:32 -08001404 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001405
1406 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1407
TDYa1278ca10052012-05-05 19:57:06 -07001408 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001409
1410 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1411 llvm::BasicBlock* block_str_exist =
1412 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1413
1414 llvm::BasicBlock* block_str_resolve =
1415 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1416
1417 // Test: Is the string resolved and in the dex cache?
1418 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1419
TDYa127ac7b5bb2012-05-11 13:17:49 -07001420 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001421
1422 // String is resolved, go to next basic block.
1423 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001425 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1426
1427 // String is not resolved yet, resolve it now.
1428 irb_.SetInsertPoint(block_str_resolve);
1429
1430 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1431
1432 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1433
1434 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1435
TDYa127c8dc1012012-04-19 07:03:33 -07001436 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001437
1438 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1439 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001440
1441 EmitGuard_ExceptionLandingPad(dex_pc);
1442 }
1443
1444 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001446
Logan Chien70f94b42011-12-27 17:49:11 +08001447 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1448}
1449
1450
Logan Chien27b30252012-01-14 03:43:35 +08001451llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1452 uint32_t type_idx) {
1453 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1454 *dex_file_, type_idx)) {
1455 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1456
1457 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1458
TDYa127706e9b62012-04-19 12:24:26 -07001459 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1460
Logan Chien27b30252012-01-14 03:43:35 +08001461 llvm::Function* runtime_func =
1462 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1463
TDYa127c8dc1012012-04-19 07:03:33 -07001464 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001465
Logan Chien27b30252012-01-14 03:43:35 +08001466 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001467 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001468
1469 EmitGuard_ExceptionLandingPad(dex_pc);
1470
1471 return type_object_addr;
1472
1473 } else {
1474 // Try to load the class (type) object from the test cache.
1475 llvm::Value* type_field_addr =
1476 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1477
TDYa1278ca10052012-05-05 19:57:06 -07001478 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001479
1480 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1481 return type_object_addr;
1482 }
1483
1484 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1485
1486 // Test whether class (type) object is in the dex cache or not
1487 llvm::Value* equal_null =
1488 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1489
1490 llvm::BasicBlock* block_cont =
1491 CreateBasicBlockWithDexPC(dex_pc, "cont");
1492
1493 llvm::BasicBlock* block_load_class =
1494 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1495
TDYa127ac7b5bb2012-05-11 13:17:49 -07001496 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001497
1498 // Failback routine to load the class object
1499 irb_.SetInsertPoint(block_load_class);
1500
1501 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1502
1503 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1504
1505 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1506
TDYa127706e9b62012-04-19 12:24:26 -07001507 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1508
TDYa127c8dc1012012-04-19 07:03:33 -07001509 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001510
Logan Chien27b30252012-01-14 03:43:35 +08001511 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001512 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001513
1514 EmitGuard_ExceptionLandingPad(dex_pc);
1515
1516 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1517
1518 irb_.CreateBr(block_cont);
1519
1520 // Now the class object must be loaded
1521 irb_.SetInsertPoint(block_cont);
1522
1523 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1524
1525 phi->addIncoming(type_object_addr, block_original);
1526 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1527
1528 return phi;
1529 }
1530}
1531
1532
Logan Chien70f94b42011-12-27 17:49:11 +08001533void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1534 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001535
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001537
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1539 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001540
Logan Chien70f94b42011-12-27 17:49:11 +08001541 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1542}
1543
1544
1545void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1546 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
Elliott Hughesadb8c672012-03-06 16:49:32 -08001548 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
1550 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001551 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001552
TDYa127cc1b4c32012-05-15 07:31:37 -07001553 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1554 EmitGuard_NullPointerException(dex_pc, object_addr);
1555 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001556
TDYa127706e9b62012-04-19 12:24:26 -07001557 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1558
1559 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001560
Logan Chien70f94b42011-12-27 17:49:11 +08001561 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1562}
1563
1564
1565void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1566 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001569
1570 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001572
TDYa127cc1b4c32012-05-15 07:31:37 -07001573 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1574 EmitGuard_NullPointerException(dex_pc, object_addr);
1575 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001576
TDYa127c8dc1012012-04-19 07:03:33 -07001577 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001578
TDYa127706e9b62012-04-19 12:24:26 -07001579 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1580
1581 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001582 EmitGuard_ExceptionLandingPad(dex_pc);
1583
Logan Chien70f94b42011-12-27 17:49:11 +08001584 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1585}
1586
1587
1588void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1589 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001590
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001592
1593 llvm::BasicBlock* block_test_class =
1594 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1595
1596 llvm::BasicBlock* block_test_sub_class =
1597 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1598
1599 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001600 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001601
1602 // Test: Is the reference equal to null? Act as no-op when it is null.
1603 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1604
1605 irb_.CreateCondBr(equal_null,
1606 GetNextBasicBlock(dex_pc),
1607 block_test_class);
1608
1609 // Test: Is the object instantiated from the given class?
1610 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001612 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1613
1614 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1615
1616 llvm::Value* object_type_field_addr =
1617 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1618
1619 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001620 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001621
1622 llvm::Value* equal_class =
1623 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1624
1625 irb_.CreateCondBr(equal_class,
1626 GetNextBasicBlock(dex_pc),
1627 block_test_sub_class);
1628
1629 // Test: Is the object instantiated from the subclass of the given class?
1630 irb_.SetInsertPoint(block_test_sub_class);
1631
TDYa127c8dc1012012-04-19 07:03:33 -07001632 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001633
Logan Chienfc880952012-01-15 23:53:10 +08001634 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1635 type_object_addr, object_type_object_addr);
1636
1637 EmitGuard_ExceptionLandingPad(dex_pc);
1638
Logan Chien70f94b42011-12-27 17:49:11 +08001639 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1640}
1641
1642
1643void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1644 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001645
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001647
1648 llvm::Constant* zero = irb_.getJInt(0);
1649 llvm::Constant* one = irb_.getJInt(1);
1650
1651 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1652
1653 llvm::BasicBlock* block_test_class =
1654 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1655
1656 llvm::BasicBlock* block_class_equals =
1657 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1658
1659 llvm::BasicBlock* block_test_sub_class =
1660 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1661
1662 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001664
1665 // Overview of the following code :
1666 // We check for null, if so, then false, otherwise check for class == . If so
1667 // then true, otherwise do callout slowpath.
1668 //
1669 // Test: Is the reference equal to null? Set 0 when it is null.
1670 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1671
1672 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1673
1674 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1677
1678 // Test: Is the object instantiated from the given class?
1679 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001680 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001681 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1682
1683 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1684
1685 llvm::Value* object_type_field_addr =
1686 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1687
1688 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001689 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001690
1691 llvm::Value* equal_class =
1692 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1693
1694 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1695
1696 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001697 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001698 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1699
1700 // Test: Is the object instantiated from the subclass of the given class?
1701 irb_.SetInsertPoint(block_test_sub_class);
1702
1703 llvm::Value* result =
1704 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1705 type_object_addr, object_type_object_addr);
1706
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001708
Logan Chien70f94b42011-12-27 17:49:11 +08001709 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1710}
1711
1712
Logan Chien61bb6142012-02-03 15:34:53 +08001713llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001714 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001715 return irb_.LoadFromObjectOffset(array,
1716 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001717 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001718 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001719}
1720
1721
Logan Chien70f94b42011-12-27 17:49:11 +08001722void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1723 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001724
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001726
1727 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001729 EmitGuard_NullPointerException(dex_pc, array_addr);
1730
1731 // Get the array length and store it to the register
1732 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001733 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001734
Logan Chien70f94b42011-12-27 17:49:11 +08001735 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1736}
1737
1738
1739void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1740 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001741
Elliott Hughesadb8c672012-03-06 16:49:32 -08001742 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001743
1744 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001745 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1746 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001747 runtime_func = irb_.GetRuntime(AllocObject);
1748 } else {
1749 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1750 }
1751
Elliott Hughesadb8c672012-03-06 16:49:32 -08001752 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001753
1754 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1755
TDYa127da83d972012-04-18 00:21:49 -07001756 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1757
TDYa127c8dc1012012-04-19 07:03:33 -07001758 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001759
Logan Chien032bdad2012-01-16 09:59:23 +08001760 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001761 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001762
1763 EmitGuard_ExceptionLandingPad(dex_pc);
1764
Elliott Hughesadb8c672012-03-06 16:49:32 -08001765 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001766
Logan Chien70f94b42011-12-27 17:49:11 +08001767 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1768}
1769
1770
Logan Chiena2cc6a32012-01-16 10:38:41 +08001771llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1772 int32_t length,
1773 uint32_t type_idx,
1774 bool is_filled_new_array) {
1775 llvm::Function* runtime_func;
1776
1777 bool skip_access_check =
1778 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1779 *dex_file_, type_idx);
1780
TDYa127a849cb62012-04-01 05:59:34 -07001781 llvm::Value* array_length_value;
1782
Logan Chiena2cc6a32012-01-16 10:38:41 +08001783 if (is_filled_new_array) {
1784 runtime_func = skip_access_check ?
1785 irb_.GetRuntime(CheckAndAllocArray) :
1786 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001787 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001788 } else {
1789 runtime_func = skip_access_check ?
1790 irb_.GetRuntime(AllocArray) :
1791 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001792 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001793 }
1794
1795 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1796
1797 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1798
TDYa127da83d972012-04-18 00:21:49 -07001799 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1800
TDYa127c8dc1012012-04-19 07:03:33 -07001801 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001802
Logan Chiena2cc6a32012-01-16 10:38:41 +08001803 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001804 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1805 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001806
1807 EmitGuard_ExceptionLandingPad(dex_pc);
1808
1809 return object_addr;
1810}
1811
1812
Logan Chien70f94b42011-12-27 17:49:11 +08001813void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1814 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001815
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001817
1818 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001820
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001822
Logan Chien70f94b42011-12-27 17:49:11 +08001823 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1824}
1825
1826
1827void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1828 Instruction const* insn,
1829 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830
Elliott Hughesadb8c672012-03-06 16:49:32 -08001831 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001832
1833 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001834 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001835
Elliott Hughesadb8c672012-03-06 16:49:32 -08001836 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001837 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001838 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001839 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1840 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001841 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842
Logan Chiendd361c92012-04-10 23:40:37 +08001843 uint32_t alignment;
1844 llvm::Constant* elem_size;
1845 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001846
Logan Chiendd361c92012-04-10 23:40:37 +08001847 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1848 // as the element, thus we are only checking 2 cases: primitive int and
1849 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001851 alignment = sizeof(int32_t);
1852 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001853 field_type = irb_.getJIntTy()->getPointerTo();
1854 } else {
1855 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001856 alignment = irb_.getSizeOfPtrEquivInt();
1857 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001858 field_type = irb_.getJObjectTy()->getPointerTo();
1859 }
1860
Logan Chiendd361c92012-04-10 23:40:37 +08001861 llvm::Value* data_field_offset =
1862 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1863
1864 llvm::Value* data_field_addr =
1865 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1866
Logan Chiena85fb2f2012-01-16 12:52:56 +08001867 // TODO: Tune this code. Currently we are generating one instruction for
1868 // one element which may be very space consuming. Maybe changing to use
1869 // memcpy may help; however, since we can't guarantee that the alloca of
1870 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001871 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001872 int reg_index;
1873 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001875 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001876 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001877 }
1878
1879 llvm::Value* reg_value;
1880 if (klass->IsPrimitiveInt()) {
1881 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1882 } else {
1883 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1884 }
1885
TDYa127706e7db2012-05-06 00:05:33 -07001886 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001887
Logan Chiendd361c92012-04-10 23:40:37 +08001888 data_field_addr =
1889 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001890 }
1891 }
1892
1893 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1894
Logan Chien70f94b42011-12-27 17:49:11 +08001895 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1896}
1897
1898
1899void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1900 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001901
Elliott Hughesadb8c672012-03-06 16:49:32 -08001902 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001903
1904 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001905 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001906 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien19c350a2012-05-01 19:21:32 +08001908 const Instruction::ArrayDataPayload* payload =
1909 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1910 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001911
Logan Chien86f50672012-04-24 13:08:45 +08001912 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001913 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001914
Logan Chien86f50672012-04-24 13:08:45 +08001915 if (payload->element_count == 0) {
1916 // When the number of the elements in the payload is zero, we don't have
1917 // to copy any numbers. However, we should check whether the array object
1918 // address is equal to null or not.
1919 EmitGuard_NullPointerException(dex_pc, array_addr);
1920 } else {
1921 // To save the code size, we are going to call the runtime function to
1922 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001923
Logan Chien86f50672012-04-24 13:08:45 +08001924 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001925
Logan Chien86f50672012-04-24 13:08:45 +08001926 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001927
Logan Chien86f50672012-04-24 13:08:45 +08001928 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001929
Logan Chien86f50672012-04-24 13:08:45 +08001930 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001931
Logan Chien86f50672012-04-24 13:08:45 +08001932 irb_.CreateCall4(runtime_func,
1933 method_object_addr, irb_.getInt32(dex_pc),
1934 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001935
Logan Chien86f50672012-04-24 13:08:45 +08001936 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001937 }
1938
Logan Chien70f94b42011-12-27 17:49:11 +08001939 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1940}
1941
1942
1943void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1944 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001947
Elliott Hughesadb8c672012-03-06 16:49:32 -08001948 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001949
Logan Chiena466c162011-12-27 17:55:46 +08001950 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001951}
1952
1953
1954void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1955 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001956
Elliott Hughesadb8c672012-03-06 16:49:32 -08001957 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001958
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001960 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Logan Chien19c350a2012-05-01 19:21:32 +08001962 const Instruction::PackedSwitchPayload* payload =
1963 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1964 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001965
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001967
1968 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001969 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001970
Logan Chien19c350a2012-05-01 19:21:32 +08001971 for (uint16_t i = 0; i < payload->case_count; ++i) {
1972 sw->addCase(irb_.getInt32(payload->first_key + i),
1973 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001974 }
Logan Chien70f94b42011-12-27 17:49:11 +08001975}
1976
1977
1978void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1979 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001982
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001985
Logan Chien19c350a2012-05-01 19:21:32 +08001986 const Instruction::SparseSwitchPayload* payload =
1987 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1988 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001989
Logan Chien19c350a2012-05-01 19:21:32 +08001990 const int32_t* keys = payload->GetKeys();
1991 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001994
1995 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001996 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001997
Logan Chien19c350a2012-05-01 19:21:32 +08001998 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001999 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2000 }
Logan Chien70f94b42011-12-27 17:49:11 +08002001}
2002
2003
2004void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2005 Instruction const* insn,
2006 JType fp_jty,
2007 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002008
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002010
2011 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2012
Elliott Hughesadb8c672012-03-06 16:49:32 -08002013 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2014 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002015
2016 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2017 llvm::Value* cmp_lt;
2018
2019 if (gt_bias) {
2020 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2021 } else {
2022 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2023 }
2024
2025 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002027
Logan Chien70f94b42011-12-27 17:49:11 +08002028 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2029}
2030
2031
2032void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2033 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002034
Elliott Hughesadb8c672012-03-06 16:49:32 -08002035 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002036
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2038 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002039
2040 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2041 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2042
2043 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002045
Logan Chien70f94b42011-12-27 17:49:11 +08002046 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2047}
2048
2049
Logan Chien2c37e8e2011-12-27 17:58:46 +08002050llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2051 llvm::Value* cmp_lt) {
2052
2053 llvm::Constant* zero = irb_.getJInt(0);
2054 llvm::Constant* pos1 = irb_.getJInt(1);
2055 llvm::Constant* neg1 = irb_.getJInt(-1);
2056
2057 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2058 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2059
2060 return result_eq;
2061}
2062
2063
Logan Chien70f94b42011-12-27 17:49:11 +08002064void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2065 Instruction const* insn,
2066 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002067
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002069
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2071 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002072
2073 DCHECK_NE(kRegUnknown, src1_reg_cat);
2074 DCHECK_NE(kRegUnknown, src2_reg_cat);
2075 DCHECK_NE(kRegCat2, src1_reg_cat);
2076 DCHECK_NE(kRegCat2, src2_reg_cat);
2077
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002079
Logan Chiena78e3c82011-12-27 17:59:35 +08002080 llvm::Value* src1_value;
2081 llvm::Value* src2_value;
2082
TDYa1278e9b4492012-04-24 15:50:27 -07002083 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2084 src1_value = irb_.getInt32(0);
2085 src2_value = irb_.getInt32(0);
2086 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002087 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2088
2089 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002090 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2091 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002092 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002093 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2094 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002095 }
2096 } else {
2097 DCHECK(src1_reg_cat == kRegZero ||
2098 src2_reg_cat == kRegZero);
2099
2100 if (src1_reg_cat == kRegZero) {
2101 if (src2_reg_cat == kRegCat1nr) {
2102 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002103 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002104 } else {
2105 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002106 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002107 }
2108 } else { // src2_reg_cat == kRegZero
2109 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002110 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002111 src2_value = irb_.getJInt(0);
2112 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002114 src2_value = irb_.getJNull();
2115 }
2116 }
2117 }
2118
2119 llvm::Value* cond_value =
2120 EmitConditionResult(src1_value, src2_value, cond);
2121
2122 irb_.CreateCondBr(cond_value,
2123 GetBasicBlock(dex_pc + branch_offset),
2124 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002125}
2126
2127
2128void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2129 Instruction const* insn,
2130 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002131
Elliott Hughesadb8c672012-03-06 16:49:32 -08002132 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002133
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002135
2136 DCHECK_NE(kRegUnknown, src_reg_cat);
2137 DCHECK_NE(kRegCat2, src_reg_cat);
2138
Elliott Hughesadb8c672012-03-06 16:49:32 -08002139 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002140
Logan Chiena78e3c82011-12-27 17:59:35 +08002141 llvm::Value* src1_value;
2142 llvm::Value* src2_value;
2143
TDYa1278e9b4492012-04-24 15:50:27 -07002144 if (src_reg_cat == kRegZero) {
2145 src1_value = irb_.getInt32(0);
2146 src2_value = irb_.getInt32(0);
2147 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002148 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002149 src2_value = irb_.getInt32(0);
2150 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002152 src2_value = irb_.getJNull();
2153 }
2154
2155 llvm::Value* cond_value =
2156 EmitConditionResult(src1_value, src2_value, cond);
2157
2158 irb_.CreateCondBr(cond_value,
2159 GetBasicBlock(dex_pc + branch_offset),
2160 GetNextBasicBlock(dex_pc));
2161}
2162
TDYa1271d7e5102012-05-13 09:27:05 -07002163InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002164 Compiler::MethodReference mref(dex_file_, method_idx_);
2165
2166 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002167 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002168
Logan Chiena78e3c82011-12-27 17:59:35 +08002169 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2170
TDYa1271d7e5102012-05-13 09:27:05 -07002171 return map;
2172}
2173
2174RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2175 uint16_t reg_idx) {
2176 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2177
Logan Chiena78e3c82011-12-27 17:59:35 +08002178 return map->GetRegCategory(dex_pc, reg_idx);
2179}
2180
TDYa1271d7e5102012-05-13 09:27:05 -07002181bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2182 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2183
2184 return map->IsRegCanBeObject(reg_idx);
2185}
2186
Logan Chiena78e3c82011-12-27 17:59:35 +08002187
2188llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2189 llvm::Value* rhs,
2190 CondBranchKind cond) {
2191 switch (cond) {
2192 case kCondBranch_EQ:
2193 return irb_.CreateICmpEQ(lhs, rhs);
2194
2195 case kCondBranch_NE:
2196 return irb_.CreateICmpNE(lhs, rhs);
2197
2198 case kCondBranch_LT:
2199 return irb_.CreateICmpSLT(lhs, rhs);
2200
2201 case kCondBranch_GE:
2202 return irb_.CreateICmpSGE(lhs, rhs);
2203
2204 case kCondBranch_GT:
2205 return irb_.CreateICmpSGT(lhs, rhs);
2206
2207 case kCondBranch_LE:
2208 return irb_.CreateICmpSLE(lhs, rhs);
2209
2210 default: // Unreachable
2211 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2212 return NULL;
2213 }
Logan Chien70f94b42011-12-27 17:49:11 +08002214}
2215
TDYa12783bb6622012-04-17 02:20:34 -07002216void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2217 // Using runtime support, let the target can override by InlineAssembly.
2218 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2219
2220 irb_.CreateCall2(runtime_func, value, target_addr);
2221}
Logan Chien70f94b42011-12-27 17:49:11 +08002222
Logan Chiene27fdbb2012-01-02 23:27:26 +08002223void
2224MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2225 llvm::Value* array,
2226 llvm::Value* index) {
2227 llvm::Value* array_len = EmitLoadArrayLength(array);
2228
2229 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2230
2231 llvm::BasicBlock* block_exception =
2232 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2233
2234 llvm::BasicBlock* block_continue =
2235 CreateBasicBlockWithDexPC(dex_pc, "cont");
2236
TDYa127ac7b5bb2012-05-11 13:17:49 -07002237 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002238
2239 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002240
TDYa127c8dc1012012-04-19 07:03:33 -07002241 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002242 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2243 EmitBranchExceptionLandingPad(dex_pc);
2244
2245 irb_.SetInsertPoint(block_continue);
2246}
2247
2248
2249void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2250 llvm::Value* array,
2251 llvm::Value* index) {
2252 EmitGuard_NullPointerException(dex_pc, array);
2253 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2254}
2255
2256
2257// Emit Array GetElementPtr
2258llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2259 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002260 JType elem_jty) {
2261
2262 int data_offset;
2263 if (elem_jty == kLong || elem_jty == kDouble ||
2264 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2265 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2266 } else {
2267 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2268 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002269
2270 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002271 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002272
TDYa1278db6ea32012-05-17 04:48:42 -07002273 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2274
Logan Chiene27fdbb2012-01-02 23:27:26 +08002275 llvm::Value* array_data_addr =
2276 irb_.CreatePtrDisp(array_addr, data_offset_value,
2277 elem_type->getPointerTo());
2278
2279 return irb_.CreateGEP(array_data_addr, index_value);
2280}
2281
2282
Logan Chien70f94b42011-12-27 17:49:11 +08002283void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2284 Instruction const* insn,
2285 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002286
Elliott Hughesadb8c672012-03-06 16:49:32 -08002287 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002288
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2290 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002291
2292 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2293
TDYa1278db6ea32012-05-17 04:48:42 -07002294 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002295
TDYa127706e7db2012-05-06 00:05:33 -07002296 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002297
Elliott Hughesadb8c672012-03-06 16:49:32 -08002298 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002299
Logan Chien70f94b42011-12-27 17:49:11 +08002300 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2301}
2302
2303
2304void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2305 Instruction const* insn,
2306 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002307
Elliott Hughesadb8c672012-03-06 16:49:32 -08002308 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002309
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2311 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002312
2313 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2314
TDYa1278db6ea32012-05-17 04:48:42 -07002315 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002316
Elliott Hughesadb8c672012-03-06 16:49:32 -08002317 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002318
TDYa12783bb6622012-04-17 02:20:34 -07002319 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002320 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2321
2322 irb_.CreateCall2(runtime_func, new_value, array_addr);
2323
2324 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002325
2326 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002327 }
2328
TDYa127706e7db2012-05-06 00:05:33 -07002329 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002330
Logan Chien70f94b42011-12-27 17:49:11 +08002331 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2332}
2333
2334
2335void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2336 Instruction const* insn,
2337 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002338
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002340
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 uint32_t reg_idx = dec_insn.vB;
2342 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002343
Logan Chien48f1d2a2012-01-02 22:49:53 +08002344 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2345
TDYa127cc1b4c32012-05-15 07:31:37 -07002346 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2347 EmitGuard_NullPointerException(dex_pc, object_addr);
2348 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002349
2350 llvm::Value* field_value;
2351
Logan Chien933abf82012-04-11 12:24:31 +08002352 int field_offset;
2353 bool is_volatile;
2354 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2355 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002356
Logan Chien933abf82012-04-11 12:24:31 +08002357 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002358 llvm::Function* runtime_func;
2359
2360 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002361 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002362 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002363 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002364 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002365 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002366 }
2367
2368 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2369
2370 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2371
TDYa127c8dc1012012-04-19 07:03:33 -07002372 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002373
Logan Chien3b2b2e72012-03-06 16:11:45 +08002374 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2375 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002376
2377 EmitGuard_ExceptionLandingPad(dex_pc);
2378
2379 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002380 DCHECK_GE(field_offset, 0);
2381
Logan Chien48f1d2a2012-01-02 22:49:53 +08002382 llvm::PointerType* field_type =
2383 irb_.getJType(field_jty, kField)->getPointerTo();
2384
Logan Chien933abf82012-04-11 12:24:31 +08002385 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002386
2387 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002388 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002389
Logan Chien933abf82012-04-11 12:24:31 +08002390 // TODO: Check is_volatile. We need to generate atomic load instruction
2391 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002392 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002393 }
2394
Elliott Hughesadb8c672012-03-06 16:49:32 -08002395 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002396
Logan Chien70f94b42011-12-27 17:49:11 +08002397 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2398}
2399
2400
2401void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2402 Instruction const* insn,
2403 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002404
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002406
Elliott Hughesadb8c672012-03-06 16:49:32 -08002407 uint32_t reg_idx = dec_insn.vB;
2408 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002409
Logan Chiendd6aa872012-01-03 16:06:32 +08002410 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2411
TDYa127cc1b4c32012-05-15 07:31:37 -07002412 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2413 EmitGuard_NullPointerException(dex_pc, object_addr);
2414 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002415
Elliott Hughesadb8c672012-03-06 16:49:32 -08002416 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002417
Logan Chien933abf82012-04-11 12:24:31 +08002418 int field_offset;
2419 bool is_volatile;
2420 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2421 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002422
Logan Chien933abf82012-04-11 12:24:31 +08002423 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002424 llvm::Function* runtime_func;
2425
2426 if (field_jty == kObject) {
2427 runtime_func = irb_.GetRuntime(SetObjectInstance);
2428 } else if (field_jty == kLong || field_jty == kDouble) {
2429 runtime_func = irb_.GetRuntime(Set64Instance);
2430 } else {
2431 runtime_func = irb_.GetRuntime(Set32Instance);
2432 }
2433
2434 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2435
2436 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2437
TDYa127c8dc1012012-04-19 07:03:33 -07002438 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002439
Logan Chien3b2b2e72012-03-06 16:11:45 +08002440 irb_.CreateCall4(runtime_func, field_idx_value,
2441 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002442
2443 EmitGuard_ExceptionLandingPad(dex_pc);
2444
2445 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002446 DCHECK_GE(field_offset, 0);
2447
Logan Chiendd6aa872012-01-03 16:06:32 +08002448 llvm::PointerType* field_type =
2449 irb_.getJType(field_jty, kField)->getPointerTo();
2450
Logan Chien933abf82012-04-11 12:24:31 +08002451 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002452
2453 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002454 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002455
Logan Chien933abf82012-04-11 12:24:31 +08002456 // TODO: Check is_volatile. We need to generate atomic store instruction
2457 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002458 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002459
2460 if (field_jty == kObject) { // If put an object, mark the GC card table.
2461 EmitMarkGCCard(new_value, object_addr);
2462 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002463 }
2464
Logan Chien70f94b42011-12-27 17:49:11 +08002465 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2466}
2467
2468
Logan Chien438c4b62012-01-17 16:06:00 +08002469llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2470 uint32_t type_idx) {
2471 llvm::BasicBlock* block_load_static =
2472 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2473
2474 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2475
2476 // Load static storage from dex cache
2477 llvm::Value* storage_field_addr =
2478 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2479
TDYa1278ca10052012-05-05 19:57:06 -07002480 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002481
2482 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2483
2484 // Test: Is the static storage of this class initialized?
2485 llvm::Value* equal_null =
2486 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2487
TDYa127ac7b5bb2012-05-11 13:17:49 -07002488 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002489
2490 // Failback routine to load the class object
2491 irb_.SetInsertPoint(block_load_static);
2492
2493 llvm::Function* runtime_func =
2494 irb_.GetRuntime(InitializeStaticStorage);
2495
2496 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2497
2498 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2499
TDYa127706e9b62012-04-19 12:24:26 -07002500 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2501
TDYa127c8dc1012012-04-19 07:03:33 -07002502 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002503
Logan Chien438c4b62012-01-17 16:06:00 +08002504 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002505 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002506
2507 EmitGuard_ExceptionLandingPad(dex_pc);
2508
2509 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2510
2511 irb_.CreateBr(block_cont);
2512
2513 // Now the class object must be loaded
2514 irb_.SetInsertPoint(block_cont);
2515
2516 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2517
2518 phi->addIncoming(storage_object_addr, block_original);
2519 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2520
2521 return phi;
2522}
2523
2524
Logan Chien70f94b42011-12-27 17:49:11 +08002525void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2526 Instruction const* insn,
2527 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002528
Elliott Hughesadb8c672012-03-06 16:49:32 -08002529 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002530
Logan Chien933abf82012-04-11 12:24:31 +08002531 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002532
Logan Chien933abf82012-04-11 12:24:31 +08002533 int field_offset;
2534 int ssb_index;
2535 bool is_referrers_class;
2536 bool is_volatile;
2537
2538 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2539 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2540 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002541
2542 llvm::Value* static_field_value;
2543
Logan Chien933abf82012-04-11 12:24:31 +08002544 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002545 llvm::Function* runtime_func;
2546
2547 if (field_jty == kObject) {
2548 runtime_func = irb_.GetRuntime(GetObjectStatic);
2549 } else if (field_jty == kLong || field_jty == kDouble) {
2550 runtime_func = irb_.GetRuntime(Get64Static);
2551 } else {
2552 runtime_func = irb_.GetRuntime(Get32Static);
2553 }
2554
Elliott Hughesadb8c672012-03-06 16:49:32 -08002555 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002556
2557 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2558
TDYa127c8dc1012012-04-19 07:03:33 -07002559 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002560
Logan Chien438c4b62012-01-17 16:06:00 +08002561 static_field_value =
2562 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2563
2564 EmitGuard_ExceptionLandingPad(dex_pc);
2565
2566 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002567 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002568
Logan Chien933abf82012-04-11 12:24:31 +08002569 llvm::Value* static_storage_addr = NULL;
2570
2571 if (is_referrers_class) {
2572 // Fast path, static storage base is this method's class
2573 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2574
TDYa127ee1f59b2012-04-25 00:56:40 -07002575 static_storage_addr =
2576 irb_.LoadFromObjectOffset(method_object_addr,
2577 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002578 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002579 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002580 } else {
2581 // Medium path, static storage base in a different class which
2582 // requires checks that the other class is initialized
2583 DCHECK_GE(ssb_index, 0);
2584 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2585 }
2586
2587 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002588
2589 llvm::Value* static_field_addr =
2590 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2591 irb_.getJType(field_jty, kField)->getPointerTo());
2592
Logan Chien933abf82012-04-11 12:24:31 +08002593 // TODO: Check is_volatile. We need to generate atomic load instruction
2594 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002595 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002596 }
2597
Elliott Hughesadb8c672012-03-06 16:49:32 -08002598 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002599
Logan Chien70f94b42011-12-27 17:49:11 +08002600 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2601}
2602
2603
2604void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2605 Instruction const* insn,
2606 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002607
Elliott Hughesadb8c672012-03-06 16:49:32 -08002608 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002609
Logan Chien933abf82012-04-11 12:24:31 +08002610 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002611
Elliott Hughesadb8c672012-03-06 16:49:32 -08002612 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002613
Logan Chien933abf82012-04-11 12:24:31 +08002614 int field_offset;
2615 int ssb_index;
2616 bool is_referrers_class;
2617 bool is_volatile;
2618
2619 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2620 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2621 is_referrers_class, is_volatile, true);
2622
2623 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002624 llvm::Function* runtime_func;
2625
2626 if (field_jty == kObject) {
2627 runtime_func = irb_.GetRuntime(SetObjectStatic);
2628 } else if (field_jty == kLong || field_jty == kDouble) {
2629 runtime_func = irb_.GetRuntime(Set64Static);
2630 } else {
2631 runtime_func = irb_.GetRuntime(Set32Static);
2632 }
2633
Elliott Hughesadb8c672012-03-06 16:49:32 -08002634 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002635
2636 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2637
TDYa127c8dc1012012-04-19 07:03:33 -07002638 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002639
Logan Chien14179c82012-01-17 17:06:34 +08002640 irb_.CreateCall3(runtime_func, field_idx_value,
2641 method_object_addr, new_value);
2642
2643 EmitGuard_ExceptionLandingPad(dex_pc);
2644
2645 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002646 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002647
Logan Chien933abf82012-04-11 12:24:31 +08002648 llvm::Value* static_storage_addr = NULL;
2649
2650 if (is_referrers_class) {
2651 // Fast path, static storage base is this method's class
2652 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2653
TDYa127ee1f59b2012-04-25 00:56:40 -07002654 static_storage_addr =
2655 irb_.LoadFromObjectOffset(method_object_addr,
2656 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002657 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002658 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002659 } else {
2660 // Medium path, static storage base in a different class which
2661 // requires checks that the other class is initialized
2662 DCHECK_GE(ssb_index, 0);
2663 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2664 }
2665
2666 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002667
2668 llvm::Value* static_field_addr =
2669 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2670 irb_.getJType(field_jty, kField)->getPointerTo());
2671
Logan Chien933abf82012-04-11 12:24:31 +08002672 // TODO: Check is_volatile. We need to generate atomic store instruction
2673 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002674 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002675
2676 if (field_jty == kObject) { // If put an object, mark the GC card table.
2677 EmitMarkGCCard(new_value, static_storage_addr);
2678 }
Logan Chien14179c82012-01-17 17:06:34 +08002679 }
2680
Logan Chien70f94b42011-12-27 17:49:11 +08002681 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2682}
2683
2684
Logan Chien1a121b92012-02-15 22:23:42 +08002685void MethodCompiler::
2686EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2687 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002689 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002690 bool is_static) {
2691
2692 // Get method signature
2693 DexFile::MethodId const& method_id =
2694 dex_file_->GetMethodId(callee_method_idx);
2695
Logan Chien8faf8022012-02-24 12:25:29 +08002696 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002697 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002698 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002699
2700 // Load argument values according to the shorty (without "this")
2701 uint16_t reg_count = 0;
2702
2703 if (!is_static) {
2704 ++reg_count; // skip the "this" pointer
2705 }
2706
Logan Chien7e7fabc2012-04-10 18:59:11 +08002707 bool is_range = (arg_fmt == kArgRange);
2708
Logan Chien8faf8022012-02-24 12:25:29 +08002709 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002710 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2711 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002712
2713 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2714
2715 ++reg_count;
2716 if (shorty[i] == 'J' || shorty[i] == 'D') {
2717 // Wide types, such as long and double, are using a pair of registers
2718 // to store the value, so we have to increase arg_reg again.
2719 ++reg_count;
2720 }
2721 }
2722
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002724 << "Actual argument mismatch for callee: "
2725 << PrettyMethod(callee_method_idx, *dex_file_);
2726}
2727
TDYa1270b686e52012-04-09 22:43:35 -07002728llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2729 uint32_t method_idx,
2730 bool is_static) {
2731 // TODO: Remove this after we solve the link and trampoline related problems.
2732 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2733
2734 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2735
2736 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002737}
Logan Chien1a121b92012-02-15 22:23:42 +08002738
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002739
Logan Chien7e7fabc2012-04-10 18:59:11 +08002740void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2741 Instruction const* insn,
2742 InvokeType invoke_type,
2743 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002744 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002745
Logan Chien61c65dc2012-02-29 03:22:30 +08002746 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002747 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002748
Logan Chien7e7fabc2012-04-10 18:59:11 +08002749 // Compute invoke related information for compiler decision
2750 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002751 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002752 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002753 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002754 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002755 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002756
Logan Chien7e7fabc2012-04-10 18:59:11 +08002757 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002758 llvm::Value* this_addr = NULL;
2759
2760 if (!is_static) {
2761 // Test: Is *this* parameter equal to null?
TDYa127cc1b4c32012-05-15 07:31:37 -07002762 uint32_t reg_idx = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2763 this_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002764
TDYa127cc1b4c32012-05-15 07:31:37 -07002765 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2766 EmitGuard_NullPointerException(dex_pc, this_addr);
2767 }
Logan Chien1a121b92012-02-15 22:23:42 +08002768 }
2769
Logan Chien7e7fabc2012-04-10 18:59:11 +08002770 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002771 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002772
2773 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002774 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002775 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2776 this_addr, dex_pc, is_fast_path);
2777 } else {
2778 switch (invoke_type) {
2779 case kStatic:
2780 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002781 if (direct_method != 0u &&
2782 direct_method != static_cast<uintptr_t>(-1)) {
2783 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002784 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2785 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002786 } else {
2787 callee_method_object_addr =
2788 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2789 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002790 break;
2791
2792 case kVirtual:
2793 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002794 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002795 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2796 break;
2797
2798 case kSuper:
2799 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2800 "the fast path.";
2801 break;
2802
2803 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002804 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002805 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2806 invoke_type, this_addr,
2807 dex_pc, is_fast_path);
2808 break;
2809 }
2810 }
2811
Logan Chien1a121b92012-02-15 22:23:42 +08002812 // Load the actual parameter
2813 std::vector<llvm::Value*> args;
2814
2815 args.push_back(callee_method_object_addr); // method object for callee
2816
2817 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002818 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002819 args.push_back(this_addr); // "this" object for callee
2820 }
2821
2822 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002823 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002824
TDYa12729c0cd12012-05-17 04:51:08 -07002825 if (is_fast_path && (invoke_type == kDirect || invoke_type == kStatic)) {
2826 bool need_retry = EmitInlineJavaIntrinsic(PrettyMethod(callee_method_idx, *dex_file_),
2827 args,
2828 GetNextBasicBlock(dex_pc));
2829 if (!need_retry) {
2830 return;
2831 }
2832 }
2833
2834 llvm::Value* code_addr =
2835 irb_.LoadFromObjectOffset(callee_method_object_addr,
2836 Method::GetCodeOffset().Int32Value(),
2837 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2838 kTBAAJRuntime);
2839
TDYa1275bb86012012-04-11 05:57:28 -07002840#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002841 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002842 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002843 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2844 EmitGuard_ExceptionLandingPad(dex_pc);
2845
Logan Chien7e7fabc2012-04-10 18:59:11 +08002846 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2847 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2848 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2849
2850 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002851 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002852 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2853 }
TDYa1275bb86012012-04-11 05:57:28 -07002854#else
2855 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2856 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2857 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2858
2859 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2860
2861
TDYa127c8dc1012012-04-19 07:03:33 -07002862 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002863
2864
2865 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002866 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002867 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2868
TDYa1275bb86012012-04-11 05:57:28 -07002869 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002870 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2871 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
TDYa127ac7b5bb2012-05-11 13:17:49 -07002872 irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002873
2874
2875 irb_.SetInsertPoint(block_normal);
2876 {
2877 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002878 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002879 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002880 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002881 }
2882 }
2883 irb_.CreateBr(block_continue);
2884
2885
TDYa127ce154722012-04-21 16:43:29 -07002886 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002887 {
TDYa127ce154722012-04-21 16:43:29 -07002888 { // lazy link
2889 // TODO: Remove this after we solve the link problem.
2890 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2891 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2892
TDYa127ac7b5bb2012-05-11 13:17:49 -07002893 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely);
TDYa127ce154722012-04-21 16:43:29 -07002894
2895
2896 irb_.SetInsertPoint(block_link);
2897 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2898
2899 EmitGuard_ExceptionLandingPad(dex_pc);
2900
2901 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2902 if (ret_shorty != 'V') {
2903 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2904 }
2905 irb_.CreateBr(block_continue);
2906
2907
2908 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002909 }
TDYa127ce154722012-04-21 16:43:29 -07002910 { // proxy stub
2911 llvm::Value* temp_space_addr;
2912 if (ret_shorty != 'V') {
2913 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2914 args.push_back(temp_space_addr);
2915 }
2916 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2917 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2918 if (ret_shorty != 'V') {
Shih-wei Liaoe6a7adc2012-05-09 02:50:08 -07002919 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa127ce154722012-04-21 16:43:29 -07002920 llvm::Value* result_addr =
2921 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
TDYa127aba61122012-05-04 18:28:36 -07002922 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
TDYa127ce154722012-04-21 16:43:29 -07002923 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2924 }
TDYa1275bb86012012-04-11 05:57:28 -07002925 }
2926 }
2927 irb_.CreateBr(block_continue);
2928
2929
2930 irb_.SetInsertPoint(block_continue);
2931
TDYa1275bb86012012-04-11 05:57:28 -07002932 EmitGuard_ExceptionLandingPad(dex_pc);
2933#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002934
Logan Chien70f94b42011-12-27 17:49:11 +08002935 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2936}
2937
2938
Logan Chien7e7fabc2012-04-10 18:59:11 +08002939llvm::Value* MethodCompiler::
2940EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2941 llvm::Value* callee_method_object_field_addr =
2942 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002943
TDYa1278ca10052012-05-05 19:57:06 -07002944 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002945}
Logan Chien7caf37e2012-02-03 22:56:04 +08002946
Logan Chien7caf37e2012-02-03 22:56:04 +08002947
Logan Chien7e7fabc2012-04-10 18:59:11 +08002948llvm::Value* MethodCompiler::
2949EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2950 llvm::Value* this_addr) {
2951 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002952 llvm::Value* class_object_addr =
2953 irb_.LoadFromObjectOffset(this_addr,
2954 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002955 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002956 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002957
Logan Chien7e7fabc2012-04-10 18:59:11 +08002958 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002959 llvm::Value* vtable_addr =
2960 irb_.LoadFromObjectOffset(class_object_addr,
2961 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002962 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002963 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002964
2965 // Load callee method object
2966 llvm::Value* vtable_idx_value =
2967 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2968
2969 llvm::Value* method_field_addr =
TDYa1278db6ea32012-05-17 04:48:42 -07002970 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002971
TDYa127d3e24c22012-05-05 20:54:19 -07002972 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002973}
2974
2975
2976llvm::Value* MethodCompiler::
2977EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2978 InvokeType invoke_type,
2979 llvm::Value* this_addr,
2980 uint32_t dex_pc,
2981 bool is_fast_path) {
2982
2983 llvm::Function* runtime_func = NULL;
2984
2985 switch (invoke_type) {
2986 case kStatic:
2987 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2988 break;
2989
2990 case kDirect:
2991 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2992 break;
2993
2994 case kVirtual:
2995 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2996 break;
2997
2998 case kSuper:
2999 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3000 break;
3001
3002 case kInterface:
3003 if (is_fast_path) {
3004 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3005 } else {
3006 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3007 }
3008 break;
3009 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003010
3011 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3012
Logan Chien7e7fabc2012-04-10 18:59:11 +08003013 if (this_addr == NULL) {
3014 DCHECK_EQ(invoke_type, kStatic);
3015 this_addr = irb_.getJNull();
3016 }
3017
3018 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3019
TDYa127da83d972012-04-18 00:21:49 -07003020 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3021
TDYa127c8dc1012012-04-19 07:03:33 -07003022 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003023
TDYa1270b686e52012-04-09 22:43:35 -07003024 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003025 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003026 callee_method_idx_value,
3027 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003028 caller_method_object_addr,
3029 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003030
3031 EmitGuard_ExceptionLandingPad(dex_pc);
3032
Logan Chien7e7fabc2012-04-10 18:59:11 +08003033 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003034}
3035
3036
3037void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3038 Instruction const* insn,
3039 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003040
Elliott Hughesadb8c672012-03-06 16:49:32 -08003041 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003042
3043 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3044
Elliott Hughesadb8c672012-03-06 16:49:32 -08003045 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003046 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003047 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003048
Logan Chien70f94b42011-12-27 17:49:11 +08003049 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3050}
3051
3052
3053void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3054 Instruction const* insn,
3055 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003056
Elliott Hughesadb8c672012-03-06 16:49:32 -08003057 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003058
3059 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3060
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003062 llvm::Value* result_value =
3063 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3064
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003066
Logan Chien70f94b42011-12-27 17:49:11 +08003067 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3068}
3069
3070
3071void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3072 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003073
Elliott Hughesadb8c672012-03-06 16:49:32 -08003074 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003075
Elliott Hughesadb8c672012-03-06 16:49:32 -08003076 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003077 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003078 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003079
Logan Chien70f94b42011-12-27 17:49:11 +08003080 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3081}
3082
3083
3084void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3085 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003086
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003088
Elliott Hughesadb8c672012-03-06 16:49:32 -08003089 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003090 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003091 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003092
Logan Chien70f94b42011-12-27 17:49:11 +08003093 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3094}
3095
3096
3097void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3098 Instruction const* insn,
3099 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003100
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003102
Elliott Hughesadb8c672012-03-06 16:49:32 -08003103 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003104
3105 llvm::Value* trunc_value =
3106 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3107
3108 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3109
Elliott Hughesadb8c672012-03-06 16:49:32 -08003110 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003111
Logan Chien70f94b42011-12-27 17:49:11 +08003112 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3113}
3114
3115
3116void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3117 Instruction const* insn,
3118 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003119
Elliott Hughesadb8c672012-03-06 16:49:32 -08003120 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003121
Elliott Hughesadb8c672012-03-06 16:49:32 -08003122 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003123
3124 llvm::Value* trunc_value =
3125 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3126
3127 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3128
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003130
Logan Chien70f94b42011-12-27 17:49:11 +08003131 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3132}
3133
3134
3135void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3136 Instruction const* insn,
3137 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003138
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003140
3141 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3142
Elliott Hughesadb8c672012-03-06 16:49:32 -08003143 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003144 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003145 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003146
Logan Chien70f94b42011-12-27 17:49:11 +08003147 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3148}
3149
3150
3151void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3152 Instruction const* insn,
3153 JType src_jty,
3154 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003155
Elliott Hughesadb8c672012-03-06 16:49:32 -08003156 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003157
3158 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3159 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3160
Elliott Hughesadb8c672012-03-06 16:49:32 -08003161 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003162 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3163 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003164 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003165
Logan Chien70f94b42011-12-27 17:49:11 +08003166 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3167}
3168
3169
3170void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3171 Instruction const* insn,
3172 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003173 JType dest_jty,
3174 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003175
Elliott Hughesadb8c672012-03-06 16:49:32 -08003176 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003177
3178 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3179 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3180
Elliott Hughesadb8c672012-03-06 16:49:32 -08003181 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003182 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003184
Logan Chien70f94b42011-12-27 17:49:11 +08003185 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3186}
3187
3188
3189void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3190 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003191
Elliott Hughesadb8c672012-03-06 16:49:32 -08003192 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003193
Elliott Hughesadb8c672012-03-06 16:49:32 -08003194 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003195 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003196 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003197
Logan Chien70f94b42011-12-27 17:49:11 +08003198 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3199}
3200
3201
3202void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3203 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003204
Elliott Hughesadb8c672012-03-06 16:49:32 -08003205 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003206
Elliott Hughesadb8c672012-03-06 16:49:32 -08003207 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003208 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003209 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003210
Logan Chien70f94b42011-12-27 17:49:11 +08003211 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3212}
3213
3214
3215void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3216 Instruction const* insn,
3217 IntArithmKind arithm,
3218 JType op_jty,
3219 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003220
Elliott Hughesadb8c672012-03-06 16:49:32 -08003221 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003222
3223 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3224
3225 llvm::Value* src1_value;
3226 llvm::Value* src2_value;
3227
3228 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003229 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3230 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003231 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003232 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3233 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003234 }
3235
3236 llvm::Value* result_value =
3237 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3238 arithm, op_jty);
3239
Elliott Hughesadb8c672012-03-06 16:49:32 -08003240 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003241
Logan Chien70f94b42011-12-27 17:49:11 +08003242 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3243}
3244
3245
3246void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3247 Instruction const* insn,
3248 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003249
Elliott Hughesadb8c672012-03-06 16:49:32 -08003250 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003251
Elliott Hughesadb8c672012-03-06 16:49:32 -08003252 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003253
Elliott Hughesadb8c672012-03-06 16:49:32 -08003254 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003255
3256 llvm::Value* result_value =
3257 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3258
Elliott Hughesadb8c672012-03-06 16:49:32 -08003259 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003260
Logan Chien70f94b42011-12-27 17:49:11 +08003261 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3262}
3263
3264
Logan Chienc3f7d962011-12-27 18:13:18 +08003265llvm::Value*
3266MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3267 llvm::Value* lhs,
3268 llvm::Value* rhs,
3269 IntArithmKind arithm,
3270 JType op_jty) {
3271 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3272
3273 switch (arithm) {
3274 case kIntArithm_Add:
3275 return irb_.CreateAdd(lhs, rhs);
3276
3277 case kIntArithm_Sub:
3278 return irb_.CreateSub(lhs, rhs);
3279
3280 case kIntArithm_Mul:
3281 return irb_.CreateMul(lhs, rhs);
3282
3283 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003284 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003285 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003286
3287 case kIntArithm_And:
3288 return irb_.CreateAnd(lhs, rhs);
3289
3290 case kIntArithm_Or:
3291 return irb_.CreateOr(lhs, rhs);
3292
3293 case kIntArithm_Xor:
3294 return irb_.CreateXor(lhs, rhs);
3295
Logan Chienc3f7d962011-12-27 18:13:18 +08003296 default:
3297 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3298 return NULL;
3299 }
3300}
3301
3302
TDYa127f8641ce2012-04-02 06:40:40 -07003303llvm::Value*
3304MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3305 llvm::Value* dividend,
3306 llvm::Value* divisor,
3307 IntArithmKind arithm,
3308 JType op_jty) {
3309 // Throw exception if the divisor is 0.
3310 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3311
3312 // Check the special case: MININT / -1 = MININT
3313 // That case will cause overflow, which is undefined behavior in llvm.
3314 // So we check the divisor is -1 or not, if the divisor is -1, we do
3315 // the special path to avoid undefined behavior.
3316 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3317 llvm::Value* zero = irb_.getJZero(op_jty);
3318 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3319 llvm::Value* result = irb_.CreateAlloca(op_type);
3320
3321 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3322 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3323 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3324
3325 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003326 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003327
3328 // If divisor == -1
3329 irb_.SetInsertPoint(eq_neg_one);
3330 llvm::Value* eq_result;
3331 if (arithm == kIntArithm_Div) {
3332 // We can just change from "dividend div -1" to "neg dividend".
3333 // The sub don't care the sign/unsigned because of two's complement representation.
3334 // And the behavior is what we want:
3335 // -(2^n) (2^n)-1
3336 // MININT < k <= MAXINT -> mul k -1 = -k
3337 // MININT == k -> mul k -1 = k
3338 //
3339 // LLVM use sub to represent 'neg'
3340 eq_result = irb_.CreateSub(zero, dividend);
3341 } else {
3342 // Everything modulo -1 will be 0.
3343 eq_result = zero;
3344 }
TDYa127aba61122012-05-04 18:28:36 -07003345 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003346 irb_.CreateBr(neg_one_cont);
3347
3348 // If divisor != -1, just do the division.
3349 irb_.SetInsertPoint(ne_neg_one);
3350 llvm::Value* ne_result;
3351 if (arithm == kIntArithm_Div) {
3352 ne_result = irb_.CreateSDiv(dividend, divisor);
3353 } else {
3354 ne_result = irb_.CreateSRem(dividend, divisor);
3355 }
TDYa127aba61122012-05-04 18:28:36 -07003356 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003357 irb_.CreateBr(neg_one_cont);
3358
3359 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003360 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003361}
3362
3363
Logan Chien5539ad02012-04-02 14:36:55 +08003364void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3365 Instruction const* insn,
3366 IntShiftArithmKind arithm,
3367 JType op_jty,
3368 bool is_2addr) {
3369
3370 DecodedInstruction dec_insn(insn);
3371
3372 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3373
3374 llvm::Value* src1_value;
3375 llvm::Value* src2_value;
3376
3377 // NOTE: The 2nd operand of the shift arithmetic instruction is
3378 // 32-bit integer regardless of the 1st operand.
3379 if (is_2addr) {
3380 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3381 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3382 } else {
3383 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3384 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3385 }
3386
3387 llvm::Value* result_value =
3388 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3389 arithm, op_jty);
3390
3391 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3392
3393 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3394}
3395
3396
3397void MethodCompiler::
3398EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3399 Instruction const* insn,
3400 IntShiftArithmKind arithm) {
3401
3402 DecodedInstruction dec_insn(insn);
3403
3404 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3405
3406 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3407
3408 llvm::Value* result_value =
3409 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3410 arithm, kInt);
3411
3412 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3413
3414 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3415}
3416
3417
3418llvm::Value*
3419MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3420 llvm::Value* lhs,
3421 llvm::Value* rhs,
3422 IntShiftArithmKind arithm,
3423 JType op_jty) {
3424 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3425
3426 if (op_jty == kInt) {
3427 rhs = irb_.CreateAnd(rhs, 0x1f);
3428 } else {
3429 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3430 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3431 }
3432
3433 switch (arithm) {
3434 case kIntArithm_Shl:
3435 return irb_.CreateShl(lhs, rhs);
3436
3437 case kIntArithm_Shr:
3438 return irb_.CreateAShr(lhs, rhs);
3439
3440 case kIntArithm_UShr:
3441 return irb_.CreateLShr(lhs, rhs);
3442
3443 default:
3444 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3445 return NULL;
3446 }
3447}
3448
3449
Logan Chien70f94b42011-12-27 17:49:11 +08003450void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3451 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003452
Elliott Hughesadb8c672012-03-06 16:49:32 -08003453 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003454
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3456 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003457 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003458 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003459
Logan Chien70f94b42011-12-27 17:49:11 +08003460 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3461}
3462
3463
3464void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3465 Instruction const* insn,
3466 FPArithmKind arithm,
3467 JType op_jty,
3468 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003469
Elliott Hughesadb8c672012-03-06 16:49:32 -08003470 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003471
3472 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3473
3474 llvm::Value* src1_value;
3475 llvm::Value* src2_value;
3476
3477 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003478 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3479 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003480 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003481 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3482 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003483 }
3484
3485 llvm::Value* result_value =
3486 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3487
Elliott Hughesadb8c672012-03-06 16:49:32 -08003488 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003489
Logan Chien70f94b42011-12-27 17:49:11 +08003490 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3491}
3492
3493
Logan Chien76e1c792011-12-27 18:15:01 +08003494llvm::Value*
3495MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3496 llvm::Value *lhs,
3497 llvm::Value *rhs,
3498 FPArithmKind arithm) {
3499 switch (arithm) {
3500 case kFPArithm_Add:
3501 return irb_.CreateFAdd(lhs, rhs);
3502
3503 case kFPArithm_Sub:
3504 return irb_.CreateFSub(lhs, rhs);
3505
3506 case kFPArithm_Mul:
3507 return irb_.CreateFMul(lhs, rhs);
3508
3509 case kFPArithm_Div:
3510 return irb_.CreateFDiv(lhs, rhs);
3511
3512 case kFPArithm_Rem:
3513 return irb_.CreateFRem(lhs, rhs);
3514
3515 default:
3516 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3517 return NULL;
3518 }
3519}
3520
3521
Logan Chienc3f7d962011-12-27 18:13:18 +08003522void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3523 llvm::Value* denominator,
3524 JType op_jty) {
3525 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3526
3527 llvm::Constant* zero = irb_.getJZero(op_jty);
3528
3529 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3530
3531 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3532
3533 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3534
TDYa127ac7b5bb2012-05-11 13:17:49 -07003535 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003536
3537 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003538 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003539 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3540 EmitBranchExceptionLandingPad(dex_pc);
3541
3542 irb_.SetInsertPoint(block_continue);
3543}
3544
3545
Logan Chien61bb6142012-02-03 15:34:53 +08003546void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3547 llvm::Value* object) {
3548 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3549
3550 llvm::BasicBlock* block_exception =
3551 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3552
3553 llvm::BasicBlock* block_continue =
3554 CreateBasicBlockWithDexPC(dex_pc, "cont");
3555
TDYa127ac7b5bb2012-05-11 13:17:49 -07003556 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003557
3558 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003559 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003560 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003561 EmitBranchExceptionLandingPad(dex_pc);
3562
3563 irb_.SetInsertPoint(block_continue);
3564}
3565
3566
Logan Chienbb4d12a2012-02-17 14:10:01 +08003567llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3568 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3569
TDYa127ee1f59b2012-04-25 00:56:40 -07003570 return irb_.LoadFromObjectOffset(method_object_addr,
3571 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003572 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003573 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003574}
3575
3576
Logan Chienbb4d12a2012-02-17 14:10:01 +08003577llvm::Value* MethodCompiler::
3578EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3579 llvm::Value* static_storage_dex_cache_addr =
3580 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3581
3582 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3583
TDYa1278db6ea32012-05-17 04:48:42 -07003584 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003585}
3586
3587
3588llvm::Value* MethodCompiler::
3589EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3590 llvm::Value* resolved_type_dex_cache_addr =
3591 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3592
3593 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3594
TDYa1278db6ea32012-05-17 04:48:42 -07003595 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003596}
3597
3598
3599llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003600EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3601 llvm::Value* resolved_method_dex_cache_addr =
3602 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3603
3604 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3605
TDYa1278db6ea32012-05-17 04:48:42 -07003606 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003607}
3608
3609
3610llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003611EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3612 llvm::Value* string_dex_cache_addr =
3613 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3614
3615 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3616
TDYa1278db6ea32012-05-17 04:48:42 -07003617 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003618}
3619
3620
Logan Chien83426162011-12-09 09:29:50 +08003621CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003622 // TODO: Use high-level IR to do this
3623 // Compute method info
3624 ComputeMethodInfo();
3625
Logan Chien0b827102011-12-20 19:46:14 +08003626 // Code generation
3627 CreateFunction();
3628
3629 EmitPrologue();
3630 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003631 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003632
Logan Chiend6c239a2011-12-23 15:11:45 +08003633 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003634 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003635
Logan Chien8b977d32012-02-21 19:14:55 +08003636 // Add the memory usage approximation of the compilation unit
3637 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003638 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003639 // Dex file. Besides, we have to convert the code unit into bytes.
3640 // Thus, we got our magic number 9.
3641
Logan Chien110bcba2012-04-16 19:11:28 +08003642 CompiledMethod* compiled_method =
3643 new CompiledMethod(cunit_->GetInstructionSet(),
3644 cunit_->GetElfIndex(),
3645 elf_func_idx_);
3646
3647 cunit_->RegisterCompiledMethod(func_, compiled_method);
3648
3649 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003650}
3651
3652
3653llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3654 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003655}
Logan Chien83426162011-12-09 09:29:50 +08003656
3657
Logan Chien5bcc04e2012-01-30 14:15:12 +08003658void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3659 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3660 irb_.CreateBr(lpad);
3661 } else {
3662 irb_.CreateBr(GetUnwindBasicBlock());
3663 }
3664}
3665
3666
3667void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3668 llvm::Value* exception_pending =
3669 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3670
3671 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3672
3673 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003674 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003675 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003676 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003677 }
3678
3679 irb_.SetInsertPoint(block_cont);
3680}
3681
3682
Logan Chien924072f2012-01-30 15:07:24 +08003683void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003684 if (!method_info_.need_shadow_frame_entry) {
3685 return;
3686 }
3687
Logan Chien924072f2012-01-30 15:07:24 +08003688 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003689
3690 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3691
TDYa127853cd092012-04-21 22:15:31 -07003692 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003693}
3694
3695
Logan Chiend6c239a2011-12-23 15:11:45 +08003696llvm::BasicBlock* MethodCompiler::
3697CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3698 std::string name;
3699
TDYa12767ae8ff2012-05-02 19:08:02 -07003700#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003701 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003702 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003703 } else {
TDYa12799489132012-04-29 01:27:58 -07003704 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003705 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003706#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003707
3708 return llvm::BasicBlock::Create(*context_, name, func_);
3709}
3710
3711
3712llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3713 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3714
3715 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3716
3717 if (!basic_block) {
3718 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3719 basic_blocks_[dex_pc] = basic_block;
3720 }
3721
3722 return basic_block;
3723}
3724
3725
3726llvm::BasicBlock*
3727MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3728 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3729 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3730}
3731
3732
Logan Chien5bcc04e2012-01-30 14:15:12 +08003733int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3734 // TODO: Since we are emitting the dex instructions in ascending order
3735 // w.r.t. address, we can cache the lastest try item offset so that we
3736 // don't have to do binary search for every query.
3737
3738 int32_t min = 0;
3739 int32_t max = code_item_->tries_size_ - 1;
3740
3741 while (min <= max) {
3742 int32_t mid = min + (max - min) / 2;
3743
3744 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3745 uint32_t start = ti->start_addr_;
3746 uint32_t end = start + ti->insn_count_;
3747
3748 if (dex_pc < start) {
3749 max = mid - 1;
3750 } else if (dex_pc >= end) {
3751 min = mid + 1;
3752 } else {
3753 return mid; // found
3754 }
3755 }
3756
3757 return -1; // not found
3758}
3759
3760
3761llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3762 // Find the try item for this address in this method
3763 int32_t ti_offset = GetTryItemOffset(dex_pc);
3764
3765 if (ti_offset == -1) {
3766 return NULL; // No landing pad is available for this address.
3767 }
3768
3769 // Check for the existing landing pad basic block
3770 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3771 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3772
3773 if (block_lpad) {
3774 // We have generated landing pad for this try item already. Return the
3775 // same basic block.
3776 return block_lpad;
3777 }
3778
3779 // Get try item from code item
3780 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3781
TDYa12767ae8ff2012-05-02 19:08:02 -07003782 std::string lpadname;
3783
3784#if !defined(NDEBUG)
3785 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3786#endif
3787
Logan Chien5bcc04e2012-01-30 14:15:12 +08003788 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003789 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003790
3791 // Change IRBuilder insert point
3792 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3793 irb_.SetInsertPoint(block_lpad);
3794
3795 // Find catch block with matching type
3796 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3797
Logan Chien736df022012-04-27 16:25:57 +08003798 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003799
3800 llvm::Value* catch_handler_index_value =
3801 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003802 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003803
3804 // Switch instruction (Go to unwind basic block by default)
3805 llvm::SwitchInst* sw =
3806 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3807
3808 // Cases with matched catch block
3809 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3810
3811 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3812 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3813 }
3814
3815 // Restore the orignal insert point for IRBuilder
3816 irb_.restoreIP(irb_ip_original);
3817
3818 // Cache this landing pad
3819 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3820 basic_block_landing_pads_[ti_offset] = block_lpad;
3821
3822 return block_lpad;
3823}
3824
3825
3826llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3827 // Check the existing unwinding baisc block block
3828 if (basic_block_unwind_ != NULL) {
3829 return basic_block_unwind_;
3830 }
3831
3832 // Create new basic block for unwinding
3833 basic_block_unwind_ =
3834 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3835
3836 // Change IRBuilder insert point
3837 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3838 irb_.SetInsertPoint(basic_block_unwind_);
3839
Logan Chien8dfcbea2012-02-17 18:50:32 +08003840 // Pop the shadow frame
3841 EmitPopShadowFrame();
3842
Logan Chien5bcc04e2012-01-30 14:15:12 +08003843 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003844 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003845 if (ret_shorty == 'V') {
3846 irb_.CreateRetVoid();
3847 } else {
3848 irb_.CreateRet(irb_.getJZero(ret_shorty));
3849 }
3850
3851 // Restore the orignal insert point for IRBuilder
3852 irb_.restoreIP(irb_ip_original);
3853
3854 return basic_block_unwind_;
3855}
3856
3857
Logan Chienc670a8d2011-12-20 21:25:56 +08003858llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3859 uint32_t reg_idx) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003860 // Get reg_type and reg_name from DalvikReg
3861 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3862 std::string reg_name;
3863
3864#if !defined(NDEBUG)
3865 StringAppendF(&reg_name, "%c%u", DalvikReg::GetRegCategoryNamePrefix(cat), reg_idx);
3866#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003867
3868 // Save current IR builder insert point
3869 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003870 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003871
3872 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003873 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003874
3875 // Restore IRBuilder insert point
3876 irb_.restoreIP(irb_ip_original);
3877
3878 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3879 return reg_addr;
3880}
3881
3882
TDYa1277f5b9be2012-04-29 01:31:49 -07003883llvm::Value* MethodCompiler::AllocShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003884 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3885 // This register dosen't need ShadowFrame entry
3886 return NULL;
3887 }
3888
TDYa127cc1b4c32012-05-15 07:31:37 -07003889 if (!method_info_.need_shadow_frame_entry) {
3890 return NULL;
3891 }
3892
TDYa12767ae8ff2012-05-02 19:08:02 -07003893 std::string reg_name;
3894
3895#if !defined(NDEBUG)
3896 StringAppendF(&reg_name, "o%u", reg_idx);
3897#endif
3898
TDYa1277f5b9be2012-04-29 01:31:49 -07003899 // Save current IR builder insert point
3900 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3901
3902 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
3903
3904 llvm::Value* gep_index[] = {
3905 irb_.getInt32(0), // No pointer displacement
3906 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003907 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003908 };
3909
TDYa12767ae8ff2012-05-02 19:08:02 -07003910 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003911
3912 // Restore IRBuilder insert point
3913 irb_.restoreIP(irb_ip_original);
3914
3915 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3916 return reg_addr;
3917}
3918
3919
Logan Chienc670a8d2011-12-20 21:25:56 +08003920llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003921 // Get reg_type and reg_name from DalvikReg
3922 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3923 std::string reg_name;
3924
3925#if !defined(NDEBUG)
3926 StringAppendF(&reg_name, "%c_res", DalvikReg::GetRegCategoryNamePrefix(cat));
3927#endif
3928
Logan Chienc670a8d2011-12-20 21:25:56 +08003929 // Save current IR builder insert point
3930 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003931 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003932
3933 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003934 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003935
3936 // Restore IRBuilder insert point
3937 irb_.restoreIP(irb_ip_original);
3938
3939 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3940 return reg_addr;
3941}
3942
3943
Logan Chien8dfcbea2012-02-17 18:50:32 +08003944void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003945 if (!method_info_.need_shadow_frame) {
3946 return;
3947 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003948 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3949}
3950
3951
TDYa127c8dc1012012-04-19 07:03:33 -07003952void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003953 if (!method_info_.need_shadow_frame) {
3954 return;
3955 }
TDYa127c8dc1012012-04-19 07:03:33 -07003956 irb_.StoreToObjectOffset(shadow_frame_,
3957 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003958 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003959 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003960}
3961
3962
TDYa127cc1b4c32012-05-15 07:31:37 -07003963// TODO: Use high-level IR to do this
TDYa12729c0cd12012-05-17 04:51:08 -07003964bool MethodCompiler::EmitInlineJavaIntrinsic(const std::string& callee_method_name,
3965 const std::vector<llvm::Value*>& args,
3966 llvm::BasicBlock* after_invoke) {
3967 if (callee_method_name == "char java.lang.String.charAt(int)") {
3968 return EmitInlinedStringCharAt(args, after_invoke);
3969 }
3970 if (callee_method_name == "int java.lang.String.length()") {
3971 return EmitInlinedStringLength(args, after_invoke);
3972 }
3973 return true;
3974}
3975
3976bool MethodCompiler::EmitInlinedStringCharAt(const std::vector<llvm::Value*>& args,
3977 llvm::BasicBlock* after_invoke) {
3978 DCHECK_EQ(args.size(), 3U) <<
3979 "char java.lang.String.charAt(int) has 3 args: method, this, char_index";
3980 llvm::Value* this_object = args[1];
3981 llvm::Value* char_index = args[2];
3982 llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "CharAtRetry", func_);
3983 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "CharAtCont", func_);
3984
3985 // TODO: Can we safely say the String.count is ConstJObject(constant memory)? (there are so many
3986 // iput to String.count in the String.<init>(...))
3987 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
3988 String::CountOffset().Int32Value(),
3989 irb_.getJIntTy(),
3990 kTBAAHeapInstance, kInt);
3991 // Two's complement, so we can use only one "less than" to check "in bounds"
3992 llvm::Value* in_bounds = irb_.CreateICmpULT(char_index, string_count);
3993 irb_.CreateCondBr(in_bounds, block_cont, block_retry, kLikely);
3994
3995 irb_.SetInsertPoint(block_cont);
3996 // TODO: Can we safely say the String.offset is ConstJObject(constant memory)?
3997 llvm::Value* string_offset = irb_.LoadFromObjectOffset(this_object,
3998 String::OffsetOffset().Int32Value(),
3999 irb_.getJIntTy(),
4000 kTBAAHeapInstance, kInt);
4001 llvm::Value* string_value = irb_.LoadFromObjectOffset(this_object,
4002 String::ValueOffset().Int32Value(),
4003 irb_.getJObjectTy(),
4004 kTBAAHeapInstance, kObject);
4005
4006 // index_value = string.offset + char_index
4007 llvm::Value* index_value = irb_.CreateAdd(string_offset, char_index);
4008
4009 // array_elem_value = string.value[index_value]
4010 llvm::Value* array_elem_addr = EmitArrayGEP(string_value, index_value, kChar);
4011 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, kChar);
4012
4013 EmitStoreDalvikRetValReg(kChar, kArray, array_elem_value);
4014 irb_.CreateBr(after_invoke);
4015
4016 irb_.SetInsertPoint(block_retry);
4017 return true;
4018}
4019
4020bool MethodCompiler::EmitInlinedStringLength(const std::vector<llvm::Value*>& args,
4021 llvm::BasicBlock* after_invoke) {
4022 DCHECK_EQ(args.size(), 2U) <<
4023 "int java.lang.String.length() has 2 args: method, this";
4024 llvm::Value* this_object = args[1];
4025 // TODO: Can we safely say the String.count is ConstJObject(constant memory)?
4026 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
4027 String::CountOffset().Int32Value(),
4028 irb_.getJIntTy(),
4029 kTBAAHeapInstance, kInt);
4030 EmitStoreDalvikRetValReg(kInt, kAccurate, string_count);
4031 irb_.CreateBr(after_invoke);
4032 return false;
4033}
4034
4035
4036// TODO: Use high-level IR to do this
TDYa127cc1b4c32012-05-15 07:31:37 -07004037void MethodCompiler::ComputeMethodInfo() {
4038 // If this method is static, we set the "this" register index to -1. So we don't worry about this
4039 // method is static or not in the following comparison.
4040 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
4041 (-1) :
4042 (code_item_->registers_size_ - code_item_->ins_size_);
4043 bool has_invoke = false;
4044 bool may_have_loop = false;
4045 bool modify_this = false;
4046 bool may_throw_exception = false;
4047
4048 Instruction const* insn;
4049 for (uint32_t dex_pc = 0;
4050 dex_pc < code_item_->insns_size_in_code_units_;
4051 dex_pc += insn->SizeInCodeUnits()) {
4052 insn = Instruction::At(code_item_->insns_ + dex_pc);
4053 DecodedInstruction dec_insn(insn);
4054
4055 switch (insn->Opcode()) {
4056 case Instruction::NOP:
4057 break;
4058
4059 case Instruction::MOVE:
4060 case Instruction::MOVE_FROM16:
4061 case Instruction::MOVE_16:
4062 case Instruction::MOVE_WIDE:
4063 case Instruction::MOVE_WIDE_FROM16:
4064 case Instruction::MOVE_WIDE_16:
4065 case Instruction::MOVE_OBJECT:
4066 case Instruction::MOVE_OBJECT_FROM16:
4067 case Instruction::MOVE_OBJECT_16:
4068 case Instruction::MOVE_RESULT:
4069 case Instruction::MOVE_RESULT_WIDE:
4070 case Instruction::MOVE_RESULT_OBJECT:
4071 case Instruction::MOVE_EXCEPTION:
4072 if (dec_insn.vA == this_reg_idx) {
4073 modify_this = true;
4074 }
4075 break;
4076
4077 case Instruction::RETURN_VOID:
4078 case Instruction::RETURN:
4079 case Instruction::RETURN_WIDE:
4080 case Instruction::RETURN_OBJECT:
4081 break;
4082
4083 case Instruction::CONST_4:
4084 case Instruction::CONST_16:
4085 case Instruction::CONST:
4086 case Instruction::CONST_HIGH16:
4087 case Instruction::CONST_WIDE_16:
4088 case Instruction::CONST_WIDE_32:
4089 case Instruction::CONST_WIDE:
4090 case Instruction::CONST_WIDE_HIGH16:
4091 if (dec_insn.vA == this_reg_idx) {
4092 modify_this = true;
4093 }
4094 break;
4095
4096 case Instruction::CONST_STRING:
4097 case Instruction::CONST_STRING_JUMBO:
4098 // TODO: Will the ResolveString throw exception?
4099 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4100 may_throw_exception = true;
4101 }
4102 if (dec_insn.vA == this_reg_idx) {
4103 modify_this = true;
4104 }
4105 break;
4106
4107 case Instruction::CONST_CLASS:
4108 may_throw_exception = true;
4109 if (dec_insn.vA == this_reg_idx) {
4110 modify_this = true;
4111 }
4112 break;
4113
4114 case Instruction::MONITOR_ENTER:
4115 case Instruction::MONITOR_EXIT:
4116 case Instruction::CHECK_CAST:
4117 may_throw_exception = true;
4118 break;
4119
4120 case Instruction::INSTANCE_OF:
4121 may_throw_exception = true;
4122 if (dec_insn.vA == this_reg_idx) {
4123 modify_this = true;
4124 }
4125 break;
4126
4127 case Instruction::ARRAY_LENGTH:
4128 if (dec_insn.vA == this_reg_idx) {
4129 modify_this = true;
4130 }
4131 break;
4132
4133 case Instruction::NEW_INSTANCE:
4134 case Instruction::NEW_ARRAY:
4135 may_throw_exception = true;
4136 if (dec_insn.vA == this_reg_idx) {
4137 modify_this = true;
4138 }
4139 break;
4140
4141 case Instruction::FILLED_NEW_ARRAY:
4142 case Instruction::FILLED_NEW_ARRAY_RANGE:
4143 case Instruction::FILL_ARRAY_DATA:
4144 case Instruction::THROW:
4145 may_throw_exception = true;
4146 break;
4147
4148 case Instruction::GOTO:
4149 case Instruction::GOTO_16:
4150 case Instruction::GOTO_32:
4151 {
4152 int32_t branch_offset = dec_insn.vA;
4153 if (branch_offset <= 0) {
4154 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4155 // According to the statistics, there are a few methods have "fake" back edge, which means
4156 // the backedge is not for a loop.
4157 // The most "fake" edges in the small methods are just branches to a return instruction. So
4158 // we can do an simple check to avoid false loop detection. After we have a high-level IR
4159 // before IRBuilder, we should remove this trick.
4160 switch (target->Opcode()) {
4161 default:
4162 may_have_loop = true;
4163 break;
4164 case Instruction::RETURN_VOID:
4165 case Instruction::RETURN:
4166 case Instruction::RETURN_WIDE:
4167 case Instruction::RETURN_OBJECT:
4168 break;
4169 }
4170 }
4171 }
4172 break;
4173
4174 case Instruction::PACKED_SWITCH:
4175 case Instruction::SPARSE_SWITCH:
4176 break;
4177
4178 case Instruction::CMPL_FLOAT:
4179 case Instruction::CMPG_FLOAT:
4180 case Instruction::CMPL_DOUBLE:
4181 case Instruction::CMPG_DOUBLE:
4182 case Instruction::CMP_LONG:
4183 if (dec_insn.vA == this_reg_idx) {
4184 modify_this = true;
4185 }
4186 break;
4187
4188 case Instruction::IF_EQ:
4189 case Instruction::IF_NE:
4190 case Instruction::IF_LT:
4191 case Instruction::IF_GE:
4192 case Instruction::IF_GT:
4193 case Instruction::IF_LE:
4194 {
4195 int32_t branch_offset = dec_insn.vC;
4196 if (branch_offset <= 0) {
4197 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4198 switch (target->Opcode()) {
4199 default:
4200 may_have_loop = true;
4201 break;
4202 case Instruction::RETURN_VOID:
4203 case Instruction::RETURN:
4204 case Instruction::RETURN_WIDE:
4205 case Instruction::RETURN_OBJECT:
4206 break;
4207 }
4208 }
4209 }
4210 break;
4211
4212 case Instruction::IF_EQZ:
4213 case Instruction::IF_NEZ:
4214 case Instruction::IF_LTZ:
4215 case Instruction::IF_GEZ:
4216 case Instruction::IF_GTZ:
4217 case Instruction::IF_LEZ:
4218 {
4219 int32_t branch_offset = dec_insn.vB;
4220 if (branch_offset <= 0) {
4221 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4222 switch (target->Opcode()) {
4223 default:
4224 may_have_loop = true;
4225 break;
4226 case Instruction::RETURN_VOID:
4227 case Instruction::RETURN:
4228 case Instruction::RETURN_WIDE:
4229 case Instruction::RETURN_OBJECT:
4230 break;
4231 }
4232 }
4233 }
4234 break;
4235
4236 case Instruction::AGET:
4237 case Instruction::AGET_WIDE:
4238 case Instruction::AGET_OBJECT:
4239 case Instruction::AGET_BOOLEAN:
4240 case Instruction::AGET_BYTE:
4241 case Instruction::AGET_CHAR:
4242 case Instruction::AGET_SHORT:
4243 may_throw_exception = true;
4244 if (dec_insn.vA == this_reg_idx) {
4245 modify_this = true;
4246 }
4247 break;
4248
4249 case Instruction::APUT:
4250 case Instruction::APUT_WIDE:
4251 case Instruction::APUT_OBJECT:
4252 case Instruction::APUT_BOOLEAN:
4253 case Instruction::APUT_BYTE:
4254 case Instruction::APUT_CHAR:
4255 case Instruction::APUT_SHORT:
4256 may_throw_exception = true;
4257 break;
4258
4259 case Instruction::IGET:
4260 case Instruction::IGET_WIDE:
4261 case Instruction::IGET_OBJECT:
4262 case Instruction::IGET_BOOLEAN:
4263 case Instruction::IGET_BYTE:
4264 case Instruction::IGET_CHAR:
4265 case Instruction::IGET_SHORT:
4266 {
4267 if (dec_insn.vA == this_reg_idx) {
4268 modify_this = true;
4269 }
4270 uint32_t reg_idx = dec_insn.vB;
4271 uint32_t field_idx = dec_insn.vC;
4272 int field_offset;
4273 bool is_volatile;
4274 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4275 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4276 if (!is_fast_path) {
4277 may_throw_exception = true;
4278 } else if (reg_idx != this_reg_idx) {
4279 // NullPointerException
4280 may_throw_exception = true;
4281 }
4282 }
4283 break;
4284
4285 case Instruction::IPUT:
4286 case Instruction::IPUT_WIDE:
4287 case Instruction::IPUT_OBJECT:
4288 case Instruction::IPUT_BOOLEAN:
4289 case Instruction::IPUT_BYTE:
4290 case Instruction::IPUT_CHAR:
4291 case Instruction::IPUT_SHORT:
4292 {
4293 uint32_t reg_idx = dec_insn.vB;
4294 uint32_t field_idx = dec_insn.vC;
4295 int field_offset;
4296 bool is_volatile;
4297 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4298 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4299 if (!is_fast_path) {
4300 may_throw_exception = true;
4301 } else if (reg_idx != this_reg_idx) {
4302 // NullPointerException
4303 may_throw_exception = true;
4304 }
4305 }
4306 break;
4307
4308 case Instruction::SGET:
4309 case Instruction::SGET_WIDE:
4310 case Instruction::SGET_OBJECT:
4311 case Instruction::SGET_BOOLEAN:
4312 case Instruction::SGET_BYTE:
4313 case Instruction::SGET_CHAR:
4314 case Instruction::SGET_SHORT:
4315 {
4316 if (dec_insn.vA == this_reg_idx) {
4317 modify_this = true;
4318 }
4319 uint32_t field_idx = dec_insn.vB;
4320
4321 int field_offset;
4322 int ssb_index;
4323 bool is_referrers_class;
4324 bool is_volatile;
4325
4326 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4327 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4328 is_referrers_class, is_volatile, false);
4329 if (!is_fast_path || !is_referrers_class) {
4330 may_throw_exception = true;
4331 }
4332 }
4333 break;
4334
4335 case Instruction::SPUT:
4336 case Instruction::SPUT_WIDE:
4337 case Instruction::SPUT_OBJECT:
4338 case Instruction::SPUT_BOOLEAN:
4339 case Instruction::SPUT_BYTE:
4340 case Instruction::SPUT_CHAR:
4341 case Instruction::SPUT_SHORT:
4342 {
4343 uint32_t field_idx = dec_insn.vB;
4344
4345 int field_offset;
4346 int ssb_index;
4347 bool is_referrers_class;
4348 bool is_volatile;
4349
4350 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4351 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4352 is_referrers_class, is_volatile, true);
4353 if (!is_fast_path || !is_referrers_class) {
4354 may_throw_exception = true;
4355 }
4356 }
4357 break;
4358
4359
4360 case Instruction::INVOKE_VIRTUAL:
4361 case Instruction::INVOKE_SUPER:
4362 case Instruction::INVOKE_DIRECT:
4363 case Instruction::INVOKE_STATIC:
4364 case Instruction::INVOKE_INTERFACE:
4365 case Instruction::INVOKE_VIRTUAL_RANGE:
4366 case Instruction::INVOKE_SUPER_RANGE:
4367 case Instruction::INVOKE_DIRECT_RANGE:
4368 case Instruction::INVOKE_STATIC_RANGE:
4369 case Instruction::INVOKE_INTERFACE_RANGE:
4370 has_invoke = true;
4371 may_throw_exception = true;
4372 break;
4373
4374 case Instruction::NEG_INT:
4375 case Instruction::NOT_INT:
4376 case Instruction::NEG_LONG:
4377 case Instruction::NOT_LONG:
4378 case Instruction::NEG_FLOAT:
4379 case Instruction::NEG_DOUBLE:
4380 case Instruction::INT_TO_LONG:
4381 case Instruction::INT_TO_FLOAT:
4382 case Instruction::INT_TO_DOUBLE:
4383 case Instruction::LONG_TO_INT:
4384 case Instruction::LONG_TO_FLOAT:
4385 case Instruction::LONG_TO_DOUBLE:
4386 case Instruction::FLOAT_TO_INT:
4387 case Instruction::FLOAT_TO_LONG:
4388 case Instruction::FLOAT_TO_DOUBLE:
4389 case Instruction::DOUBLE_TO_INT:
4390 case Instruction::DOUBLE_TO_LONG:
4391 case Instruction::DOUBLE_TO_FLOAT:
4392 case Instruction::INT_TO_BYTE:
4393 case Instruction::INT_TO_CHAR:
4394 case Instruction::INT_TO_SHORT:
4395 case Instruction::ADD_INT:
4396 case Instruction::SUB_INT:
4397 case Instruction::MUL_INT:
4398 case Instruction::AND_INT:
4399 case Instruction::OR_INT:
4400 case Instruction::XOR_INT:
4401 case Instruction::SHL_INT:
4402 case Instruction::SHR_INT:
4403 case Instruction::USHR_INT:
4404 case Instruction::ADD_LONG:
4405 case Instruction::SUB_LONG:
4406 case Instruction::MUL_LONG:
4407 case Instruction::AND_LONG:
4408 case Instruction::OR_LONG:
4409 case Instruction::XOR_LONG:
4410 case Instruction::SHL_LONG:
4411 case Instruction::SHR_LONG:
4412 case Instruction::USHR_LONG:
4413 case Instruction::ADD_INT_2ADDR:
4414 case Instruction::SUB_INT_2ADDR:
4415 case Instruction::MUL_INT_2ADDR:
4416 case Instruction::AND_INT_2ADDR:
4417 case Instruction::OR_INT_2ADDR:
4418 case Instruction::XOR_INT_2ADDR:
4419 case Instruction::SHL_INT_2ADDR:
4420 case Instruction::SHR_INT_2ADDR:
4421 case Instruction::USHR_INT_2ADDR:
4422 case Instruction::ADD_LONG_2ADDR:
4423 case Instruction::SUB_LONG_2ADDR:
4424 case Instruction::MUL_LONG_2ADDR:
4425 case Instruction::AND_LONG_2ADDR:
4426 case Instruction::OR_LONG_2ADDR:
4427 case Instruction::XOR_LONG_2ADDR:
4428 case Instruction::SHL_LONG_2ADDR:
4429 case Instruction::SHR_LONG_2ADDR:
4430 case Instruction::USHR_LONG_2ADDR:
4431 if (dec_insn.vA == this_reg_idx) {
4432 modify_this = true;
4433 }
4434 break;
4435
4436 case Instruction::DIV_INT:
4437 case Instruction::REM_INT:
4438 case Instruction::DIV_LONG:
4439 case Instruction::REM_LONG:
4440 case Instruction::DIV_INT_2ADDR:
4441 case Instruction::REM_INT_2ADDR:
4442 case Instruction::DIV_LONG_2ADDR:
4443 case Instruction::REM_LONG_2ADDR:
4444 may_throw_exception = true;
4445 if (dec_insn.vA == this_reg_idx) {
4446 modify_this = true;
4447 }
4448 break;
4449
4450 case Instruction::ADD_FLOAT:
4451 case Instruction::SUB_FLOAT:
4452 case Instruction::MUL_FLOAT:
4453 case Instruction::DIV_FLOAT:
4454 case Instruction::REM_FLOAT:
4455 case Instruction::ADD_DOUBLE:
4456 case Instruction::SUB_DOUBLE:
4457 case Instruction::MUL_DOUBLE:
4458 case Instruction::DIV_DOUBLE:
4459 case Instruction::REM_DOUBLE:
4460 case Instruction::ADD_FLOAT_2ADDR:
4461 case Instruction::SUB_FLOAT_2ADDR:
4462 case Instruction::MUL_FLOAT_2ADDR:
4463 case Instruction::DIV_FLOAT_2ADDR:
4464 case Instruction::REM_FLOAT_2ADDR:
4465 case Instruction::ADD_DOUBLE_2ADDR:
4466 case Instruction::SUB_DOUBLE_2ADDR:
4467 case Instruction::MUL_DOUBLE_2ADDR:
4468 case Instruction::DIV_DOUBLE_2ADDR:
4469 case Instruction::REM_DOUBLE_2ADDR:
4470 if (dec_insn.vA == this_reg_idx) {
4471 modify_this = true;
4472 }
4473 break;
4474
4475 case Instruction::ADD_INT_LIT16:
4476 case Instruction::ADD_INT_LIT8:
4477 case Instruction::RSUB_INT:
4478 case Instruction::RSUB_INT_LIT8:
4479 case Instruction::MUL_INT_LIT16:
4480 case Instruction::MUL_INT_LIT8:
4481 case Instruction::AND_INT_LIT16:
4482 case Instruction::AND_INT_LIT8:
4483 case Instruction::OR_INT_LIT16:
4484 case Instruction::OR_INT_LIT8:
4485 case Instruction::XOR_INT_LIT16:
4486 case Instruction::XOR_INT_LIT8:
4487 case Instruction::SHL_INT_LIT8:
4488 case Instruction::SHR_INT_LIT8:
4489 case Instruction::USHR_INT_LIT8:
4490 if (dec_insn.vA == this_reg_idx) {
4491 modify_this = true;
4492 }
4493 break;
4494 case Instruction::DIV_INT_LIT16:
4495 case Instruction::DIV_INT_LIT8:
4496 case Instruction::REM_INT_LIT16:
4497 case Instruction::REM_INT_LIT8:
4498 if (dec_insn.vC == 0) {
4499 may_throw_exception = true;
4500 }
4501 if (dec_insn.vA == this_reg_idx) {
4502 modify_this = true;
4503 }
4504 break;
4505
4506 case Instruction::THROW_VERIFICATION_ERROR:
4507 may_throw_exception = true;
4508 break;
4509
4510 case Instruction::UNUSED_3E:
4511 case Instruction::UNUSED_3F:
4512 case Instruction::UNUSED_40:
4513 case Instruction::UNUSED_41:
4514 case Instruction::UNUSED_42:
4515 case Instruction::UNUSED_43:
4516 case Instruction::UNUSED_73:
4517 case Instruction::UNUSED_79:
4518 case Instruction::UNUSED_7A:
4519 case Instruction::UNUSED_E3:
4520 case Instruction::UNUSED_E4:
4521 case Instruction::UNUSED_E5:
4522 case Instruction::UNUSED_E6:
4523 case Instruction::UNUSED_E7:
4524 case Instruction::UNUSED_E8:
4525 case Instruction::UNUSED_E9:
4526 case Instruction::UNUSED_EA:
4527 case Instruction::UNUSED_EB:
4528 case Instruction::UNUSED_EC:
4529 case Instruction::UNUSED_EE:
4530 case Instruction::UNUSED_EF:
4531 case Instruction::UNUSED_F0:
4532 case Instruction::UNUSED_F1:
4533 case Instruction::UNUSED_F2:
4534 case Instruction::UNUSED_F3:
4535 case Instruction::UNUSED_F4:
4536 case Instruction::UNUSED_F5:
4537 case Instruction::UNUSED_F6:
4538 case Instruction::UNUSED_F7:
4539 case Instruction::UNUSED_F8:
4540 case Instruction::UNUSED_F9:
4541 case Instruction::UNUSED_FA:
4542 case Instruction::UNUSED_FB:
4543 case Instruction::UNUSED_FC:
4544 case Instruction::UNUSED_FD:
4545 case Instruction::UNUSED_FE:
4546 case Instruction::UNUSED_FF:
4547 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4548 break;
4549 }
4550 }
4551
4552 method_info_.this_reg_idx = this_reg_idx;
4553 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4554 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4555 // should remove this trick.
4556 method_info_.this_will_not_be_null = !modify_this;
4557 method_info_.has_invoke = has_invoke;
4558 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4559 // for GC.
4560 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4561 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4562 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
4563}
4564
4565
4566
Logan Chien83426162011-12-09 09:29:50 +08004567} // namespace compiler_llvm
4568} // namespace art