blob: 620ba3904c617eb0414a3fea2d0c0251168047a4 [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"
TDYa127e2102142012-05-26 10:27:38 -070022#include "dalvik_reg.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080023#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024#include "ir_builder.h"
25#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080026#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "object.h"
28#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080029#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070030#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080031#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080033#include "stringprintf.h"
34#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070035#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080036
37#include <iomanip>
38
Logan Chienc670a8d2011-12-20 21:25:56 +080039#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080041#include <llvm/GlobalVariable.h>
42#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043
Logan Chien83426162011-12-09 09:29:50 +080044namespace art {
45namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
Logan Chien42e0e152012-01-13 15:42:36 +080047using namespace runtime_support;
48
Shih-wei Liaod1fec812012-02-13 09:51:10 -080049
Logan Chien8b977d32012-02-21 19:14:55 +080050MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080051 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080052 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080053 : cunit_(cunit), compiler_(compiler),
54 class_linker_(oat_compilation_unit->class_linker_),
55 class_loader_(oat_compilation_unit->class_loader_),
56 dex_file_(oat_compilation_unit->dex_file_),
57 dex_cache_(oat_compilation_unit->dex_cache_),
58 code_item_(oat_compilation_unit->code_item_),
59 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080060 method_idx_(oat_compilation_unit->method_idx_),
61 access_flags_(oat_compilation_unit->access_flags_),
62 module_(cunit->GetModule()),
63 context_(cunit->GetLLVMContext()),
TDYa1271d7e5102012-05-13 09:27:05 -070064 irb_(*cunit->GetIRBuilder()),
65 func_(NULL),
66 regs_(code_item_->registers_size_),
TDYa127e2102142012-05-26 10:27:38 -070067 shadow_frame_entries_(code_item_->registers_size_),
TDYa1271d7e5102012-05-13 09:27:05 -070068 reg_to_shadow_frame_index_(code_item_->registers_size_, -1),
69 retval_reg_(NULL),
TDYa127b9ff6b12012-05-17 11:14:29 -070070 basic_block_alloca_(NULL), basic_block_shadow_frame_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080071 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080072 basic_blocks_(code_item_->insns_size_in_code_units_),
73 basic_block_landing_pads_(code_item_->tries_size_, NULL),
74 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
TDYa1270de52be2012-05-27 20:49:31 -070075 shadow_frame_(NULL), jvalue_temp_(NULL), old_shadow_frame_(NULL),
TDYa127af543472012-05-28 21:49:23 -070076 already_pushed_shadow_frame_(NULL), shadow_frame_size_(0),
TDYa1270de52be2012-05-27 20:49:31 -070077 elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080078}
79
80
81MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080082 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080083}
84
85
Logan Chien0b827102011-12-20 19:46:14 +080086void MethodCompiler::CreateFunction() {
87 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080088 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080089
90 // Get function type
91 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080092 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080093
94 // Create function
95 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
96 func_name, module_);
97
TDYa12767ae8ff2012-05-02 19:08:02 -070098#if !defined(NDEBUG)
Logan Chien0b827102011-12-20 19:46:14 +080099 // Set argument name
100 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
101 llvm::Function::arg_iterator arg_end(func_->arg_end());
102
103 DCHECK_NE(arg_iter, arg_end);
104 arg_iter->setName("method");
105 ++arg_iter;
106
Logan Chiendd361c92012-04-10 23:40:37 +0800107 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800108 DCHECK_NE(arg_iter, arg_end);
109 arg_iter->setName("this");
110 ++arg_iter;
111 }
112
113 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
114 arg_iter->setName(StringPrintf("a%u", i));
115 }
TDYa12767ae8ff2012-05-02 19:08:02 -0700116#endif
Logan Chien0b827102011-12-20 19:46:14 +0800117}
118
119
120llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
121 bool is_static) {
122 // Get method signature
123 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
124
Logan Chien8faf8022012-02-24 12:25:29 +0800125 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800126 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800127 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800128
129 // Get return type
130 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
131
132 // Get argument type
133 std::vector<llvm::Type*> args_type;
134
135 args_type.push_back(irb_.getJObjectTy()); // method object pointer
136
137 if (!is_static) {
138 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
139 }
140
Logan Chien8faf8022012-02-24 12:25:29 +0800141 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800142 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
143 }
144
145 return llvm::FunctionType::get(ret_type, args_type, false);
146}
147
148
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800149void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800150 // Create basic blocks for prologue
TDYa12799489132012-04-29 01:27:58 -0700151#if !defined(NDEBUG)
152 // Add a BasicBlock named as PrettyMethod for debugging.
153 llvm::BasicBlock* entry =
154 llvm::BasicBlock::Create(*context_, PrettyMethod(method_idx_, *dex_file_), func_);
155#endif
TDYa127b9ff6b12012-05-17 11:14:29 -0700156 basic_block_alloca_ =
Logan Chienc670a8d2011-12-20 21:25:56 +0800157 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
158
TDYa127b9ff6b12012-05-17 11:14:29 -0700159 basic_block_shadow_frame_ =
Logan Chien8dfcbea2012-02-17 18:50:32 +0800160 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);
TDYa127b9ff6b12012-05-17 11:14:29 -0700167 irb_.CreateBr(basic_block_alloca_);
TDYa12799489132012-04-29 01:27:58 -0700168#endif
169
TDYa127b9ff6b12012-05-17 11:14:29 -0700170 irb_.SetInsertPoint(basic_block_alloca_);
171 jvalue_temp_ = irb_.CreateAlloca(irb_.getJValueTy());
172
Logan Chien8dfcbea2012-02-17 18:50:32 +0800173 // Create Shadow Frame
TDYa127cc1b4c32012-05-15 07:31:37 -0700174 if (method_info_.need_shadow_frame) {
175 EmitPrologueAllocShadowFrame();
176 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800177
TDYa127e2102142012-05-26 10:27:38 -0700178 // Create register array
179 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
180 std::string name;
181#if !defined(NDEBUG)
182 name = StringPrintf("%u", r);
183#endif
184 regs_[r] = new DalvikReg(*this, name);
185
186 // Cache shadow frame entry address
187 shadow_frame_entries_[r] = GetShadowFrameEntry(r);
188 }
189
190 std::string name;
191#if !defined(NDEBUG)
192 name = "_res";
193#endif
194 retval_reg_.reset(new DalvikReg(*this, name));
195
Logan Chiend6ececa2011-12-27 16:20:15 +0800196 // Store argument to dalvik register
197 irb_.SetInsertPoint(basic_block_reg_arg_init_);
198 EmitPrologueAssignArgRegister();
199
200 // Branch to start address
201 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800202}
203
204
TDYa1274165a832012-04-03 17:47:16 -0700205void MethodCompiler::EmitStackOverflowCheck() {
TDYa1274165a832012-04-03 17:47:16 -0700206 // Call llvm intrinsic function to get frame address.
207 llvm::Function* frameaddress =
208 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
209
210 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
211 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
212
213 // Cast i8* to int
214 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
215
216 // Get thread.stack_end_
TDYa127ee1f59b2012-04-25 00:56:40 -0700217 llvm::Value* stack_end =
TDYa127de479be2012-05-31 08:03:26 -0700218 irb_.Runtime().EmitLoadFromThreadOffset(Thread::StackEndOffset().Int32Value(),
219 irb_.getPtrEquivIntTy(),
220 kTBAARuntimeInfo);
TDYa1274165a832012-04-03 17:47:16 -0700221
222 // Check the frame address < thread.stack_end_ ?
223 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
224
225 llvm::BasicBlock* block_exception =
226 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
227
228 llvm::BasicBlock* block_continue =
229 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
230
TDYa127ac7b5bb2012-05-11 13:17:49 -0700231 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
TDYa1274165a832012-04-03 17:47:16 -0700232
233 // If stack overflow, throw exception.
234 irb_.SetInsertPoint(block_exception);
235 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
236
237 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800238 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700239 if (ret_shorty == 'V') {
240 irb_.CreateRetVoid();
241 } else {
242 irb_.CreateRet(irb_.getJZero(ret_shorty));
243 }
244
TDYa127526643e2012-05-26 01:01:48 -0700245 irb_.SetInsertPoint(block_continue);
TDYa1274165a832012-04-03 17:47:16 -0700246}
247
248
Logan Chienc670a8d2011-12-20 21:25:56 +0800249void MethodCompiler::EmitPrologueLastBranch() {
TDYa127526643e2012-05-26 01:01:48 -0700250 llvm::BasicBlock* basic_block_stack_overflow =
251 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
TDYa12797339c42012-04-29 01:26:35 -0700252
TDYa127526643e2012-05-26 01:01:48 -0700253 irb_.SetInsertPoint(basic_block_alloca_);
254 irb_.CreateBr(basic_block_stack_overflow);
255
256 irb_.SetInsertPoint(basic_block_stack_overflow);
TDYa127cc1b4c32012-05-15 07:31:37 -0700257 // If a method will not call to other method, and the method is small, we can avoid stack overflow
258 // check.
259 if (method_info_.has_invoke ||
260 code_item_->registers_size_ > 32) { // Small leaf function is OK given
261 // the 8KB reserved at Stack End
262 EmitStackOverflowCheck();
263 }
TDYa127526643e2012-05-26 01:01:48 -0700264 // Garbage collection safe-point
265 EmitGuard_GarbageCollectionSuspend();
TDYa127b9ff6b12012-05-17 11:14:29 -0700266 irb_.CreateBr(basic_block_shadow_frame_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800267
TDYa127b9ff6b12012-05-17 11:14:29 -0700268 irb_.SetInsertPoint(basic_block_shadow_frame_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800269 irb_.CreateBr(basic_block_reg_arg_init_);
270}
271
272
Logan Chien8dfcbea2012-02-17 18:50:32 +0800273void MethodCompiler::EmitPrologueAllocShadowFrame() {
TDYa127b9ff6b12012-05-17 11:14:29 -0700274 irb_.SetInsertPoint(basic_block_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800275
276 // Allocate the shadow frame now!
TDYa127af543472012-05-28 21:49:23 -0700277 shadow_frame_size_ = 0;
TDYa127f1652862012-05-16 21:44:35 -0700278 if (method_info_.need_shadow_frame_entry) {
279 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
280 if (IsRegCanBeObject(i)) {
TDYa127af543472012-05-28 21:49:23 -0700281 reg_to_shadow_frame_index_[i] = shadow_frame_size_++;
TDYa127f1652862012-05-16 21:44:35 -0700282 }
TDYa1271d7e5102012-05-13 09:27:05 -0700283 }
284 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800285
TDYa127af543472012-05-28 21:49:23 -0700286 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(shadow_frame_size_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800287 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
288
TDYa127af543472012-05-28 21:49:23 -0700289 // Alloca a pointer to old shadow frame
290 old_shadow_frame_ = irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
291
TDYa127b9ff6b12012-05-17 11:14:29 -0700292 irb_.SetInsertPoint(basic_block_shadow_frame_);
293
TDYa1276e474f82012-05-17 21:23:57 -0700294 // Zero-initialization of the shadow frame table
295 llvm::Value* shadow_frame_table = irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
296 llvm::Type* table_type = shadow_frame_type->getElementType(1);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800297
TDYa1276e474f82012-05-17 21:23:57 -0700298 llvm::ConstantAggregateZero* zero_initializer =
299 llvm::ConstantAggregateZero::get(table_type);
300
301 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800302
TDYa127af543472012-05-28 21:49:23 -0700303 // Lazy pushing shadow frame
304 if (method_info_.lazy_push_shadow_frame) {
305 irb_.SetInsertPoint(basic_block_alloca_);
306 already_pushed_shadow_frame_ = irb_.CreateAlloca(irb_.getInt1Ty());
307 irb_.SetInsertPoint(basic_block_shadow_frame_);
308 irb_.CreateStore(irb_.getFalse(), already_pushed_shadow_frame_, kTBAARegister);
309 return;
310 }
TDYa127ee1f59b2012-04-25 00:56:40 -0700311
TDYa127af543472012-05-28 21:49:23 -0700312 EmitPushShadowFrame(true);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800313}
314
315
Logan Chiend6ececa2011-12-27 16:20:15 +0800316void MethodCompiler::EmitPrologueAssignArgRegister() {
317 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
318
319 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
320 llvm::Function::arg_iterator arg_end(func_->arg_end());
321
Logan Chiendd361c92012-04-10 23:40:37 +0800322 uint32_t shorty_size = 0;
323 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
324 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800325
326 ++arg_iter; // skip method object
327
Logan Chiendd361c92012-04-10 23:40:37 +0800328 if (!oat_compilation_unit_->IsStatic()) {
TDYa127e2102142012-05-26 10:27:38 -0700329 regs_[arg_reg]->SetValue(kObject, kAccurate, arg_iter);
Logan Chiend6ececa2011-12-27 16:20:15 +0800330 ++arg_iter;
331 ++arg_reg;
332 }
333
Logan Chiendd361c92012-04-10 23:40:37 +0800334 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
TDYa127e2102142012-05-26 10:27:38 -0700335 regs_[arg_reg]->SetValue(shorty[i], kAccurate, arg_iter);
Logan Chiend6ececa2011-12-27 16:20:15 +0800336
337 ++arg_reg;
338 if (shorty[i] == 'J' || shorty[i] == 'D') {
339 // Wide types, such as long and double, are using a pair of registers
340 // to store the value, so we have to increase arg_reg again.
341 ++arg_reg;
342 }
343 }
344
345 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800346}
347
348
Logan Chien83426162011-12-09 09:29:50 +0800349void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800350 uint32_t dex_pc = 0;
351 while (dex_pc < code_item_->insns_size_in_code_units_) {
352 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
353 EmitInstruction(dex_pc, insn);
354 dex_pc += insn->SizeInCodeUnits();
355 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800356}
357
358
Logan Chien83426162011-12-09 09:29:50 +0800359void MethodCompiler::EmitInstruction(uint32_t dex_pc,
360 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800361
362 // Set the IRBuilder insertion point
363 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
364
Logan Chien70f94b42011-12-27 17:49:11 +0800365#define ARGS dex_pc, insn
366
367 // Dispatch the instruction
368 switch (insn->Opcode()) {
369 case Instruction::NOP:
370 EmitInsn_Nop(ARGS);
371 break;
372
373 case Instruction::MOVE:
374 case Instruction::MOVE_FROM16:
375 case Instruction::MOVE_16:
376 EmitInsn_Move(ARGS, kInt);
377 break;
378
379 case Instruction::MOVE_WIDE:
380 case Instruction::MOVE_WIDE_FROM16:
381 case Instruction::MOVE_WIDE_16:
382 EmitInsn_Move(ARGS, kLong);
383 break;
384
385 case Instruction::MOVE_OBJECT:
386 case Instruction::MOVE_OBJECT_FROM16:
387 case Instruction::MOVE_OBJECT_16:
388 EmitInsn_Move(ARGS, kObject);
389 break;
390
391 case Instruction::MOVE_RESULT:
392 EmitInsn_MoveResult(ARGS, kInt);
393 break;
394
395 case Instruction::MOVE_RESULT_WIDE:
396 EmitInsn_MoveResult(ARGS, kLong);
397 break;
398
399 case Instruction::MOVE_RESULT_OBJECT:
400 EmitInsn_MoveResult(ARGS, kObject);
401 break;
402
403 case Instruction::MOVE_EXCEPTION:
404 EmitInsn_MoveException(ARGS);
405 break;
406
407 case Instruction::RETURN_VOID:
408 EmitInsn_ReturnVoid(ARGS);
409 break;
410
411 case Instruction::RETURN:
412 case Instruction::RETURN_WIDE:
413 case Instruction::RETURN_OBJECT:
414 EmitInsn_Return(ARGS);
415 break;
416
417 case Instruction::CONST_4:
418 case Instruction::CONST_16:
419 case Instruction::CONST:
420 case Instruction::CONST_HIGH16:
421 EmitInsn_LoadConstant(ARGS, kInt);
422 break;
423
424 case Instruction::CONST_WIDE_16:
425 case Instruction::CONST_WIDE_32:
426 case Instruction::CONST_WIDE:
427 case Instruction::CONST_WIDE_HIGH16:
428 EmitInsn_LoadConstant(ARGS, kLong);
429 break;
430
431 case Instruction::CONST_STRING:
432 case Instruction::CONST_STRING_JUMBO:
433 EmitInsn_LoadConstantString(ARGS);
434 break;
435
436 case Instruction::CONST_CLASS:
437 EmitInsn_LoadConstantClass(ARGS);
438 break;
439
440 case Instruction::MONITOR_ENTER:
441 EmitInsn_MonitorEnter(ARGS);
442 break;
443
444 case Instruction::MONITOR_EXIT:
445 EmitInsn_MonitorExit(ARGS);
446 break;
447
448 case Instruction::CHECK_CAST:
449 EmitInsn_CheckCast(ARGS);
450 break;
451
452 case Instruction::INSTANCE_OF:
453 EmitInsn_InstanceOf(ARGS);
454 break;
455
456 case Instruction::ARRAY_LENGTH:
457 EmitInsn_ArrayLength(ARGS);
458 break;
459
460 case Instruction::NEW_INSTANCE:
461 EmitInsn_NewInstance(ARGS);
462 break;
463
464 case Instruction::NEW_ARRAY:
465 EmitInsn_NewArray(ARGS);
466 break;
467
468 case Instruction::FILLED_NEW_ARRAY:
469 EmitInsn_FilledNewArray(ARGS, false);
470 break;
471
472 case Instruction::FILLED_NEW_ARRAY_RANGE:
473 EmitInsn_FilledNewArray(ARGS, true);
474 break;
475
476 case Instruction::FILL_ARRAY_DATA:
477 EmitInsn_FillArrayData(ARGS);
478 break;
479
480 case Instruction::THROW:
481 EmitInsn_ThrowException(ARGS);
482 break;
483
484 case Instruction::GOTO:
485 case Instruction::GOTO_16:
486 case Instruction::GOTO_32:
487 EmitInsn_UnconditionalBranch(ARGS);
488 break;
489
490 case Instruction::PACKED_SWITCH:
491 EmitInsn_PackedSwitch(ARGS);
492 break;
493
494 case Instruction::SPARSE_SWITCH:
495 EmitInsn_SparseSwitch(ARGS);
496 break;
497
498 case Instruction::CMPL_FLOAT:
499 EmitInsn_FPCompare(ARGS, kFloat, false);
500 break;
501
502 case Instruction::CMPG_FLOAT:
503 EmitInsn_FPCompare(ARGS, kFloat, true);
504 break;
505
506 case Instruction::CMPL_DOUBLE:
507 EmitInsn_FPCompare(ARGS, kDouble, false);
508 break;
509
510 case Instruction::CMPG_DOUBLE:
511 EmitInsn_FPCompare(ARGS, kDouble, true);
512 break;
513
514 case Instruction::CMP_LONG:
515 EmitInsn_LongCompare(ARGS);
516 break;
517
518 case Instruction::IF_EQ:
519 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
520 break;
521
522 case Instruction::IF_NE:
523 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
524 break;
525
526 case Instruction::IF_LT:
527 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
528 break;
529
530 case Instruction::IF_GE:
531 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
532 break;
533
534 case Instruction::IF_GT:
535 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
536 break;
537
538 case Instruction::IF_LE:
539 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
540 break;
541
542 case Instruction::IF_EQZ:
543 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
544 break;
545
546 case Instruction::IF_NEZ:
547 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
548 break;
549
550 case Instruction::IF_LTZ:
551 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
552 break;
553
554 case Instruction::IF_GEZ:
555 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
556 break;
557
558 case Instruction::IF_GTZ:
559 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
560 break;
561
562 case Instruction::IF_LEZ:
563 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
564 break;
565
566 case Instruction::AGET:
567 EmitInsn_AGet(ARGS, kInt);
568 break;
569
570 case Instruction::AGET_WIDE:
571 EmitInsn_AGet(ARGS, kLong);
572 break;
573
574 case Instruction::AGET_OBJECT:
575 EmitInsn_AGet(ARGS, kObject);
576 break;
577
578 case Instruction::AGET_BOOLEAN:
579 EmitInsn_AGet(ARGS, kBoolean);
580 break;
581
582 case Instruction::AGET_BYTE:
583 EmitInsn_AGet(ARGS, kByte);
584 break;
585
586 case Instruction::AGET_CHAR:
587 EmitInsn_AGet(ARGS, kChar);
588 break;
589
590 case Instruction::AGET_SHORT:
591 EmitInsn_AGet(ARGS, kShort);
592 break;
593
594 case Instruction::APUT:
595 EmitInsn_APut(ARGS, kInt);
596 break;
597
598 case Instruction::APUT_WIDE:
599 EmitInsn_APut(ARGS, kLong);
600 break;
601
602 case Instruction::APUT_OBJECT:
603 EmitInsn_APut(ARGS, kObject);
604 break;
605
606 case Instruction::APUT_BOOLEAN:
607 EmitInsn_APut(ARGS, kBoolean);
608 break;
609
610 case Instruction::APUT_BYTE:
611 EmitInsn_APut(ARGS, kByte);
612 break;
613
614 case Instruction::APUT_CHAR:
615 EmitInsn_APut(ARGS, kChar);
616 break;
617
618 case Instruction::APUT_SHORT:
619 EmitInsn_APut(ARGS, kShort);
620 break;
621
622 case Instruction::IGET:
623 EmitInsn_IGet(ARGS, kInt);
624 break;
625
626 case Instruction::IGET_WIDE:
627 EmitInsn_IGet(ARGS, kLong);
628 break;
629
630 case Instruction::IGET_OBJECT:
631 EmitInsn_IGet(ARGS, kObject);
632 break;
633
634 case Instruction::IGET_BOOLEAN:
635 EmitInsn_IGet(ARGS, kBoolean);
636 break;
637
638 case Instruction::IGET_BYTE:
639 EmitInsn_IGet(ARGS, kByte);
640 break;
641
642 case Instruction::IGET_CHAR:
643 EmitInsn_IGet(ARGS, kChar);
644 break;
645
646 case Instruction::IGET_SHORT:
647 EmitInsn_IGet(ARGS, kShort);
648 break;
649
650 case Instruction::IPUT:
651 EmitInsn_IPut(ARGS, kInt);
652 break;
653
654 case Instruction::IPUT_WIDE:
655 EmitInsn_IPut(ARGS, kLong);
656 break;
657
658 case Instruction::IPUT_OBJECT:
659 EmitInsn_IPut(ARGS, kObject);
660 break;
661
662 case Instruction::IPUT_BOOLEAN:
663 EmitInsn_IPut(ARGS, kBoolean);
664 break;
665
666 case Instruction::IPUT_BYTE:
667 EmitInsn_IPut(ARGS, kByte);
668 break;
669
670 case Instruction::IPUT_CHAR:
671 EmitInsn_IPut(ARGS, kChar);
672 break;
673
674 case Instruction::IPUT_SHORT:
675 EmitInsn_IPut(ARGS, kShort);
676 break;
677
678 case Instruction::SGET:
679 EmitInsn_SGet(ARGS, kInt);
680 break;
681
682 case Instruction::SGET_WIDE:
683 EmitInsn_SGet(ARGS, kLong);
684 break;
685
686 case Instruction::SGET_OBJECT:
687 EmitInsn_SGet(ARGS, kObject);
688 break;
689
690 case Instruction::SGET_BOOLEAN:
691 EmitInsn_SGet(ARGS, kBoolean);
692 break;
693
694 case Instruction::SGET_BYTE:
695 EmitInsn_SGet(ARGS, kByte);
696 break;
697
698 case Instruction::SGET_CHAR:
699 EmitInsn_SGet(ARGS, kChar);
700 break;
701
702 case Instruction::SGET_SHORT:
703 EmitInsn_SGet(ARGS, kShort);
704 break;
705
706 case Instruction::SPUT:
707 EmitInsn_SPut(ARGS, kInt);
708 break;
709
710 case Instruction::SPUT_WIDE:
711 EmitInsn_SPut(ARGS, kLong);
712 break;
713
714 case Instruction::SPUT_OBJECT:
715 EmitInsn_SPut(ARGS, kObject);
716 break;
717
718 case Instruction::SPUT_BOOLEAN:
719 EmitInsn_SPut(ARGS, kBoolean);
720 break;
721
722 case Instruction::SPUT_BYTE:
723 EmitInsn_SPut(ARGS, kByte);
724 break;
725
726 case Instruction::SPUT_CHAR:
727 EmitInsn_SPut(ARGS, kChar);
728 break;
729
730 case Instruction::SPUT_SHORT:
731 EmitInsn_SPut(ARGS, kShort);
732 break;
733
734
735 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800736 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800737 break;
738
739 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800740 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800741 break;
742
743 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800744 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800745 break;
746
747 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800748 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800749 break;
750
751 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800752 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800753 break;
754
755 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800756 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800757 break;
758
759 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800760 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800761 break;
762
763 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800764 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800765 break;
766
767 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800768 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800769 break;
770
771 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800772 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800773 break;
774
775 case Instruction::NEG_INT:
776 EmitInsn_Neg(ARGS, kInt);
777 break;
778
779 case Instruction::NOT_INT:
780 EmitInsn_Not(ARGS, kInt);
781 break;
782
783 case Instruction::NEG_LONG:
784 EmitInsn_Neg(ARGS, kLong);
785 break;
786
787 case Instruction::NOT_LONG:
788 EmitInsn_Not(ARGS, kLong);
789 break;
790
791 case Instruction::NEG_FLOAT:
792 EmitInsn_FNeg(ARGS, kFloat);
793 break;
794
795 case Instruction::NEG_DOUBLE:
796 EmitInsn_FNeg(ARGS, kDouble);
797 break;
798
799 case Instruction::INT_TO_LONG:
800 EmitInsn_SExt(ARGS);
801 break;
802
803 case Instruction::INT_TO_FLOAT:
804 EmitInsn_IntToFP(ARGS, kInt, kFloat);
805 break;
806
807 case Instruction::INT_TO_DOUBLE:
808 EmitInsn_IntToFP(ARGS, kInt, kDouble);
809 break;
810
811 case Instruction::LONG_TO_INT:
812 EmitInsn_Trunc(ARGS);
813 break;
814
815 case Instruction::LONG_TO_FLOAT:
816 EmitInsn_IntToFP(ARGS, kLong, kFloat);
817 break;
818
819 case Instruction::LONG_TO_DOUBLE:
820 EmitInsn_IntToFP(ARGS, kLong, kDouble);
821 break;
822
823 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700824 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800825 break;
826
827 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700828 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800829 break;
830
831 case Instruction::FLOAT_TO_DOUBLE:
832 EmitInsn_FExt(ARGS);
833 break;
834
835 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700836 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800837 break;
838
839 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700840 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800841 break;
842
843 case Instruction::DOUBLE_TO_FLOAT:
844 EmitInsn_FTrunc(ARGS);
845 break;
846
847 case Instruction::INT_TO_BYTE:
848 EmitInsn_TruncAndSExt(ARGS, 8);
849 break;
850
851 case Instruction::INT_TO_CHAR:
852 EmitInsn_TruncAndZExt(ARGS, 16);
853 break;
854
855 case Instruction::INT_TO_SHORT:
856 EmitInsn_TruncAndSExt(ARGS, 16);
857 break;
858
859 case Instruction::ADD_INT:
860 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
861 break;
862
863 case Instruction::SUB_INT:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
865 break;
866
867 case Instruction::MUL_INT:
868 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
869 break;
870
871 case Instruction::DIV_INT:
872 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
873 break;
874
875 case Instruction::REM_INT:
876 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
877 break;
878
879 case Instruction::AND_INT:
880 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
881 break;
882
883 case Instruction::OR_INT:
884 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
885 break;
886
887 case Instruction::XOR_INT:
888 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
889 break;
890
891 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800892 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800893 break;
894
895 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800896 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800897 break;
898
899 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800900 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800901 break;
902
903 case Instruction::ADD_LONG:
904 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
905 break;
906
907 case Instruction::SUB_LONG:
908 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
909 break;
910
911 case Instruction::MUL_LONG:
912 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
913 break;
914
915 case Instruction::DIV_LONG:
916 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
917 break;
918
919 case Instruction::REM_LONG:
920 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
921 break;
922
923 case Instruction::AND_LONG:
924 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
925 break;
926
927 case Instruction::OR_LONG:
928 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
929 break;
930
931 case Instruction::XOR_LONG:
932 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
933 break;
934
935 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800936 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800937 break;
938
939 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800940 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800941 break;
942
943 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800944 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800945 break;
946
947 case Instruction::ADD_FLOAT:
948 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
949 break;
950
951 case Instruction::SUB_FLOAT:
952 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
953 break;
954
955 case Instruction::MUL_FLOAT:
956 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
957 break;
958
959 case Instruction::DIV_FLOAT:
960 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
961 break;
962
963 case Instruction::REM_FLOAT:
964 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
965 break;
966
967 case Instruction::ADD_DOUBLE:
968 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
969 break;
970
971 case Instruction::SUB_DOUBLE:
972 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
973 break;
974
975 case Instruction::MUL_DOUBLE:
976 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
977 break;
978
979 case Instruction::DIV_DOUBLE:
980 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
981 break;
982
983 case Instruction::REM_DOUBLE:
984 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
985 break;
986
987 case Instruction::ADD_INT_2ADDR:
988 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
989 break;
990
991 case Instruction::SUB_INT_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
993 break;
994
995 case Instruction::MUL_INT_2ADDR:
996 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
997 break;
998
999 case Instruction::DIV_INT_2ADDR:
1000 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
1001 break;
1002
1003 case Instruction::REM_INT_2ADDR:
1004 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
1005 break;
1006
1007 case Instruction::AND_INT_2ADDR:
1008 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
1009 break;
1010
1011 case Instruction::OR_INT_2ADDR:
1012 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1013 break;
1014
1015 case Instruction::XOR_INT_2ADDR:
1016 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1017 break;
1018
1019 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001020 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001021 break;
1022
1023 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001024 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001025 break;
1026
1027 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001028 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001029 break;
1030
1031 case Instruction::ADD_LONG_2ADDR:
1032 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1033 break;
1034
1035 case Instruction::SUB_LONG_2ADDR:
1036 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1037 break;
1038
1039 case Instruction::MUL_LONG_2ADDR:
1040 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1041 break;
1042
1043 case Instruction::DIV_LONG_2ADDR:
1044 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1045 break;
1046
1047 case Instruction::REM_LONG_2ADDR:
1048 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1049 break;
1050
1051 case Instruction::AND_LONG_2ADDR:
1052 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1053 break;
1054
1055 case Instruction::OR_LONG_2ADDR:
1056 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1057 break;
1058
1059 case Instruction::XOR_LONG_2ADDR:
1060 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1061 break;
1062
1063 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001064 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001065 break;
1066
1067 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001068 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001069 break;
1070
1071 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001072 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001073 break;
1074
1075 case Instruction::ADD_FLOAT_2ADDR:
1076 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1077 break;
1078
1079 case Instruction::SUB_FLOAT_2ADDR:
1080 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1081 break;
1082
1083 case Instruction::MUL_FLOAT_2ADDR:
1084 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1085 break;
1086
1087 case Instruction::DIV_FLOAT_2ADDR:
1088 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1089 break;
1090
1091 case Instruction::REM_FLOAT_2ADDR:
1092 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1093 break;
1094
1095 case Instruction::ADD_DOUBLE_2ADDR:
1096 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1097 break;
1098
1099 case Instruction::SUB_DOUBLE_2ADDR:
1100 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1101 break;
1102
1103 case Instruction::MUL_DOUBLE_2ADDR:
1104 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1105 break;
1106
1107 case Instruction::DIV_DOUBLE_2ADDR:
1108 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1109 break;
1110
1111 case Instruction::REM_DOUBLE_2ADDR:
1112 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1113 break;
1114
1115 case Instruction::ADD_INT_LIT16:
1116 case Instruction::ADD_INT_LIT8:
1117 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1118 break;
1119
1120 case Instruction::RSUB_INT:
1121 case Instruction::RSUB_INT_LIT8:
1122 EmitInsn_RSubImmediate(ARGS);
1123 break;
1124
1125 case Instruction::MUL_INT_LIT16:
1126 case Instruction::MUL_INT_LIT8:
1127 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1128 break;
1129
1130 case Instruction::DIV_INT_LIT16:
1131 case Instruction::DIV_INT_LIT8:
1132 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1133 break;
1134
1135 case Instruction::REM_INT_LIT16:
1136 case Instruction::REM_INT_LIT8:
1137 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1138 break;
1139
1140 case Instruction::AND_INT_LIT16:
1141 case Instruction::AND_INT_LIT8:
1142 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1143 break;
1144
1145 case Instruction::OR_INT_LIT16:
1146 case Instruction::OR_INT_LIT8:
1147 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1148 break;
1149
1150 case Instruction::XOR_INT_LIT16:
1151 case Instruction::XOR_INT_LIT8:
1152 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1153 break;
1154
1155 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001156 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001157 break;
1158
1159 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001160 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001161 break;
1162
1163 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001164 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001165 break;
1166
Logan Chien9e5f5c12012-04-10 13:51:45 +08001167 case Instruction::THROW_VERIFICATION_ERROR:
1168 EmitInsn_ThrowVerificationError(ARGS);
1169 break;
1170
Logan Chien70f94b42011-12-27 17:49:11 +08001171 case Instruction::UNUSED_3E:
1172 case Instruction::UNUSED_3F:
1173 case Instruction::UNUSED_40:
1174 case Instruction::UNUSED_41:
1175 case Instruction::UNUSED_42:
1176 case Instruction::UNUSED_43:
1177 case Instruction::UNUSED_73:
1178 case Instruction::UNUSED_79:
1179 case Instruction::UNUSED_7A:
1180 case Instruction::UNUSED_E3:
1181 case Instruction::UNUSED_E4:
1182 case Instruction::UNUSED_E5:
1183 case Instruction::UNUSED_E6:
1184 case Instruction::UNUSED_E7:
1185 case Instruction::UNUSED_E8:
1186 case Instruction::UNUSED_E9:
1187 case Instruction::UNUSED_EA:
1188 case Instruction::UNUSED_EB:
1189 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001190 case Instruction::UNUSED_EE:
1191 case Instruction::UNUSED_EF:
1192 case Instruction::UNUSED_F0:
1193 case Instruction::UNUSED_F1:
1194 case Instruction::UNUSED_F2:
1195 case Instruction::UNUSED_F3:
1196 case Instruction::UNUSED_F4:
1197 case Instruction::UNUSED_F5:
1198 case Instruction::UNUSED_F6:
1199 case Instruction::UNUSED_F7:
1200 case Instruction::UNUSED_F8:
1201 case Instruction::UNUSED_F9:
1202 case Instruction::UNUSED_FA:
1203 case Instruction::UNUSED_FB:
1204 case Instruction::UNUSED_FC:
1205 case Instruction::UNUSED_FD:
1206 case Instruction::UNUSED_FE:
1207 case Instruction::UNUSED_FF:
1208 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1209 break;
1210 }
1211
1212#undef ARGS
1213}
1214
1215
1216void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1217 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001218
1219 uint16_t insn_signature = code_item_->insns_[dex_pc];
1220
1221 if (insn_signature == Instruction::kPackedSwitchSignature ||
1222 insn_signature == Instruction::kSparseSwitchSignature ||
1223 insn_signature == Instruction::kArrayDataSignature) {
1224 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001225 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001226 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1227 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001228}
1229
1230
Logan Chien70f94b42011-12-27 17:49:11 +08001231void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1232 Instruction const* insn,
1233 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001234
Elliott Hughesadb8c672012-03-06 16:49:32 -08001235 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001236
Elliott Hughesadb8c672012-03-06 16:49:32 -08001237 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1238 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001239
Logan Chien70f94b42011-12-27 17:49:11 +08001240 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1241}
1242
1243
1244void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1245 Instruction const* insn,
1246 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001247
Elliott Hughesadb8c672012-03-06 16:49:32 -08001248 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001249
1250 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001251 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001252
Logan Chien70f94b42011-12-27 17:49:11 +08001253 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1254}
1255
1256
1257void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1258 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001259
Elliott Hughesadb8c672012-03-06 16:49:32 -08001260 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001261
TDYa127ee1f59b2012-04-25 00:56:40 -07001262 // Get thread-local exception field address
1263 llvm::Value* exception_object_addr =
TDYa127de479be2012-05-31 08:03:26 -07001264 irb_.Runtime().EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
1265 irb_.getJObjectTy(),
1266 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001267
1268 // Set thread-local exception field address to NULL
TDYa127de479be2012-05-31 08:03:26 -07001269 irb_.Runtime().EmitStoreToThreadOffset(Thread::ExceptionOffset().Int32Value(),
1270 irb_.getJNull(),
1271 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001272
1273 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001274 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001275
Logan Chien70f94b42011-12-27 17:49:11 +08001276 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1277}
1278
1279
1280void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1281 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001282
Elliott Hughesadb8c672012-03-06 16:49:32 -08001283 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001284
1285 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001286 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001287
TDYa127c8dc1012012-04-19 07:03:33 -07001288 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001289
Logan Chien6c6f12d2012-01-13 19:26:27 +08001290 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1291
1292 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001293}
1294
1295
Logan Chien9e5f5c12012-04-10 13:51:45 +08001296void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1297 Instruction const* insn) {
1298
1299 DecodedInstruction dec_insn(insn);
1300
TDYa127c8dc1012012-04-19 07:03:33 -07001301 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001302
1303 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1304 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1305 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1306
1307 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1308 method_object_addr, kind_value, ref_value);
1309
1310 EmitBranchExceptionLandingPad(dex_pc);
1311}
1312
1313
Logan Chien70f94b42011-12-27 17:49:11 +08001314void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1315 Instruction const* insn) {
Logan Chien8dfcbea2012-02-17 18:50:32 +08001316 // Pop the shadow frame
1317 EmitPopShadowFrame();
1318
Logan Chien8898a272011-12-27 17:51:56 +08001319 // Return!
1320 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001321}
1322
1323
1324void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1325 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001326
Elliott Hughesadb8c672012-03-06 16:49:32 -08001327 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001328
Logan Chien8dfcbea2012-02-17 18:50:32 +08001329 // Pop the shadow frame
1330 EmitPopShadowFrame();
1331 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1332 // the return value might be collected since the shadow stack is popped.
1333
Logan Chien8898a272011-12-27 17:51:56 +08001334 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001335 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001336 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001337
1338 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001339}
1340
1341
1342void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1343 Instruction const* insn,
1344 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001345
Elliott Hughesadb8c672012-03-06 16:49:32 -08001346 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347
1348 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1349
1350 int64_t imm = 0;
1351
1352 switch (insn->Opcode()) {
1353 // 32-bit Immediate
1354 case Instruction::CONST_4:
1355 case Instruction::CONST_16:
1356 case Instruction::CONST:
1357 case Instruction::CONST_WIDE_16:
1358 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001359 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001360 break;
1361
1362 case Instruction::CONST_HIGH16:
1363 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001364 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001365 break;
1366
1367 // 64-bit Immediate
1368 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001370 break;
1371
1372 case Instruction::CONST_WIDE_HIGH16:
1373 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001375 break;
1376
1377 // Unknown opcode for load constant (unreachable)
1378 default:
1379 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1380 break;
1381 }
1382
1383 // Store the non-object register
1384 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1385 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001386 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001387
1388 // Store the object register if it is possible to be null.
1389 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001391 }
1392
Logan Chien70f94b42011-12-27 17:49:11 +08001393 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1394}
1395
1396
1397void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1398 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001399
Elliott Hughesadb8c672012-03-06 16:49:32 -08001400 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001401
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001403
1404 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1405
TDYa1278ca10052012-05-05 19:57:06 -07001406 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001407
1408 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1409 llvm::BasicBlock* block_str_exist =
1410 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1411
1412 llvm::BasicBlock* block_str_resolve =
1413 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1414
1415 // Test: Is the string resolved and in the dex cache?
1416 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1417
TDYa127ac7b5bb2012-05-11 13:17:49 -07001418 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001419
1420 // String is resolved, go to next basic block.
1421 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001423 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1424
1425 // String is not resolved yet, resolve it now.
1426 irb_.SetInsertPoint(block_str_resolve);
1427
1428 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1429
1430 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1431
1432 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1433
TDYa127c8dc1012012-04-19 07:03:33 -07001434 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001435
1436 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1437 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001438
TDYa127526643e2012-05-26 01:01:48 -07001439 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001440 }
1441
1442 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001443 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001444
Logan Chien70f94b42011-12-27 17:49:11 +08001445 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1446}
1447
1448
Logan Chien27b30252012-01-14 03:43:35 +08001449llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1450 uint32_t type_idx) {
1451 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1452 *dex_file_, type_idx)) {
1453 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1454
1455 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1456
TDYa127de479be2012-05-31 08:03:26 -07001457 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001458
Logan Chien27b30252012-01-14 03:43:35 +08001459 llvm::Function* runtime_func =
1460 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1461
TDYa127c8dc1012012-04-19 07:03:33 -07001462 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001463
Logan Chien27b30252012-01-14 03:43:35 +08001464 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001465 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001466
TDYa127526643e2012-05-26 01:01:48 -07001467 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien27b30252012-01-14 03:43:35 +08001468
1469 return type_object_addr;
1470
1471 } else {
1472 // Try to load the class (type) object from the test cache.
1473 llvm::Value* type_field_addr =
1474 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1475
TDYa1278ca10052012-05-05 19:57:06 -07001476 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001477
1478 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1479 return type_object_addr;
1480 }
1481
1482 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1483
1484 // Test whether class (type) object is in the dex cache or not
1485 llvm::Value* equal_null =
1486 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1487
1488 llvm::BasicBlock* block_cont =
1489 CreateBasicBlockWithDexPC(dex_pc, "cont");
1490
1491 llvm::BasicBlock* block_load_class =
1492 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1493
TDYa127ac7b5bb2012-05-11 13:17:49 -07001494 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001495
1496 // Failback routine to load the class object
1497 irb_.SetInsertPoint(block_load_class);
1498
1499 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1500
1501 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1502
1503 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1504
TDYa127de479be2012-05-31 08:03:26 -07001505 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001506
TDYa127c8dc1012012-04-19 07:03:33 -07001507 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001508
Logan Chien27b30252012-01-14 03:43:35 +08001509 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001510 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001511
TDYa127526643e2012-05-26 01:01:48 -07001512 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien27b30252012-01-14 03:43:35 +08001513
1514 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1515
1516 irb_.CreateBr(block_cont);
1517
1518 // Now the class object must be loaded
1519 irb_.SetInsertPoint(block_cont);
1520
1521 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1522
1523 phi->addIncoming(type_object_addr, block_original);
1524 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1525
1526 return phi;
1527 }
1528}
1529
1530
Logan Chien70f94b42011-12-27 17:49:11 +08001531void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1532 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001533
Elliott Hughesadb8c672012-03-06 16:49:32 -08001534 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001535
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1537 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001538
Logan Chien70f94b42011-12-27 17:49:11 +08001539 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1540}
1541
1542
1543void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1544 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001545
Elliott Hughesadb8c672012-03-06 16:49:32 -08001546 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
1548 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550
TDYa127cc1b4c32012-05-15 07:31:37 -07001551 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1552 EmitGuard_NullPointerException(dex_pc, object_addr);
1553 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001554
TDYa127de479be2012-05-31 08:03:26 -07001555 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001556
1557 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001558
Logan Chien70f94b42011-12-27 17:49:11 +08001559 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1560}
1561
1562
1563void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1564 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001565
Elliott Hughesadb8c672012-03-06 16:49:32 -08001566 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
1568 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001570
TDYa127cc1b4c32012-05-15 07:31:37 -07001571 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1572 EmitGuard_NullPointerException(dex_pc, object_addr);
1573 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001574
TDYa127c8dc1012012-04-19 07:03:33 -07001575 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001576
TDYa127de479be2012-05-31 08:03:26 -07001577 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001578
1579 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
TDYa127526643e2012-05-26 01:01:48 -07001580
1581 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001582
Logan Chien70f94b42011-12-27 17:49:11 +08001583 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1584}
1585
1586
1587void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1588 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001589
Elliott Hughesadb8c672012-03-06 16:49:32 -08001590 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001591
1592 llvm::BasicBlock* block_test_class =
1593 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1594
1595 llvm::BasicBlock* block_test_sub_class =
1596 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1597
1598 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001599 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001600
1601 // Test: Is the reference equal to null? Act as no-op when it is null.
1602 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1603
1604 irb_.CreateCondBr(equal_null,
1605 GetNextBasicBlock(dex_pc),
1606 block_test_class);
1607
1608 // Test: Is the object instantiated from the given class?
1609 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001610 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001611 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1612
1613 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1614
1615 llvm::Value* object_type_field_addr =
1616 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1617
1618 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001619 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001620
1621 llvm::Value* equal_class =
1622 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1623
1624 irb_.CreateCondBr(equal_class,
1625 GetNextBasicBlock(dex_pc),
1626 block_test_sub_class);
1627
1628 // Test: Is the object instantiated from the subclass of the given class?
1629 irb_.SetInsertPoint(block_test_sub_class);
1630
TDYa127c8dc1012012-04-19 07:03:33 -07001631 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001632
Logan Chienfc880952012-01-15 23:53:10 +08001633 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1634 type_object_addr, object_type_object_addr);
1635
TDYa127526643e2012-05-26 01:01:48 -07001636 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chienfc880952012-01-15 23:53:10 +08001637
Logan Chien70f94b42011-12-27 17:49:11 +08001638 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1639}
1640
1641
1642void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1643 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001644
Elliott Hughesadb8c672012-03-06 16:49:32 -08001645 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001646
1647 llvm::Constant* zero = irb_.getJInt(0);
1648 llvm::Constant* one = irb_.getJInt(1);
1649
1650 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1651
1652 llvm::BasicBlock* block_test_class =
1653 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1654
1655 llvm::BasicBlock* block_class_equals =
1656 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1657
1658 llvm::BasicBlock* block_test_sub_class =
1659 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1660
1661 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001662 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001663
1664 // Overview of the following code :
1665 // We check for null, if so, then false, otherwise check for class == . If so
1666 // then true, otherwise do callout slowpath.
1667 //
1668 // Test: Is the reference equal to null? Set 0 when it is null.
1669 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1670
1671 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1672
1673 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001674 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001675 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1676
1677 // Test: Is the object instantiated from the given class?
1678 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001679 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001680 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1681
1682 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1683
1684 llvm::Value* object_type_field_addr =
1685 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1686
1687 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001688 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001689
1690 llvm::Value* equal_class =
1691 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1692
1693 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1694
1695 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001697 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1698
1699 // Test: Is the object instantiated from the subclass of the given class?
1700 irb_.SetInsertPoint(block_test_sub_class);
1701
1702 llvm::Value* result =
1703 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1704 type_object_addr, object_type_object_addr);
1705
Elliott Hughesadb8c672012-03-06 16:49:32 -08001706 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001707
Logan Chien70f94b42011-12-27 17:49:11 +08001708 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1709}
1710
1711
Logan Chien61bb6142012-02-03 15:34:53 +08001712llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001713 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001714 return irb_.LoadFromObjectOffset(array,
1715 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001716 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001717 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001718}
1719
1720
Logan Chien70f94b42011-12-27 17:49:11 +08001721void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1722 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001723
Elliott Hughesadb8c672012-03-06 16:49:32 -08001724 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001725
1726 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001728 EmitGuard_NullPointerException(dex_pc, array_addr);
1729
1730 // Get the array length and store it to the register
1731 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001732 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001733
Logan Chien70f94b42011-12-27 17:49:11 +08001734 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1735}
1736
1737
1738void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1739 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001740
Elliott Hughesadb8c672012-03-06 16:49:32 -08001741 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001742
1743 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001744 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1745 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001746 runtime_func = irb_.GetRuntime(AllocObject);
1747 } else {
1748 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1749 }
1750
Elliott Hughesadb8c672012-03-06 16:49:32 -08001751 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001752
1753 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1754
TDYa127de479be2012-05-31 08:03:26 -07001755 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07001756
TDYa127c8dc1012012-04-19 07:03:33 -07001757 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001758
Logan Chien032bdad2012-01-16 09:59:23 +08001759 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001760 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001761
TDYa127526643e2012-05-26 01:01:48 -07001762 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien032bdad2012-01-16 09:59:23 +08001763
Elliott Hughesadb8c672012-03-06 16:49:32 -08001764 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001765
Logan Chien70f94b42011-12-27 17:49:11 +08001766 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1767}
1768
1769
Logan Chiena2cc6a32012-01-16 10:38:41 +08001770llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1771 int32_t length,
1772 uint32_t type_idx,
1773 bool is_filled_new_array) {
1774 llvm::Function* runtime_func;
1775
1776 bool skip_access_check =
1777 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1778 *dex_file_, type_idx);
1779
TDYa127a849cb62012-04-01 05:59:34 -07001780 llvm::Value* array_length_value;
1781
Logan Chiena2cc6a32012-01-16 10:38:41 +08001782 if (is_filled_new_array) {
1783 runtime_func = skip_access_check ?
1784 irb_.GetRuntime(CheckAndAllocArray) :
1785 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001786 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001787 } else {
1788 runtime_func = skip_access_check ?
1789 irb_.GetRuntime(AllocArray) :
1790 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001791 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001792 }
1793
1794 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1795
1796 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1797
TDYa127de479be2012-05-31 08:03:26 -07001798 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07001799
TDYa127c8dc1012012-04-19 07:03:33 -07001800 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001801
Logan Chiena2cc6a32012-01-16 10:38:41 +08001802 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001803 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1804 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001805
TDYa127526643e2012-05-26 01:01:48 -07001806 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001807
1808 return object_addr;
1809}
1810
1811
Logan Chien70f94b42011-12-27 17:49:11 +08001812void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1813 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001814
Elliott Hughesadb8c672012-03-06 16:49:32 -08001815 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001816
1817 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001818 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001819
Elliott Hughesadb8c672012-03-06 16:49:32 -08001820 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001821
Logan Chien70f94b42011-12-27 17:49:11 +08001822 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1823}
1824
1825
1826void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1827 Instruction const* insn,
1828 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001829
Elliott Hughesadb8c672012-03-06 16:49:32 -08001830 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001831
1832 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001833 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001834
Elliott Hughesadb8c672012-03-06 16:49:32 -08001835 if (dec_insn.vA > 0) {
Logan Chien4b1baf12012-05-18 16:28:36 +08001836 // Check for the element type
1837 uint32_t type_desc_len = 0;
1838 const char* type_desc =
1839 dex_file_->StringByTypeIdx(dec_insn.vB, &type_desc_len);
1840
1841 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
1842 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
1843 bool is_elem_int_ty = (type_desc[1] == 'I');
Logan Chiena85fb2f2012-01-16 12:52:56 +08001844
Logan Chiendd361c92012-04-10 23:40:37 +08001845 uint32_t alignment;
1846 llvm::Constant* elem_size;
1847 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001848
Logan Chiendd361c92012-04-10 23:40:37 +08001849 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1850 // as the element, thus we are only checking 2 cases: primitive int and
1851 // non-primitive type.
Logan Chien4b1baf12012-05-18 16:28:36 +08001852 if (is_elem_int_ty) {
Logan Chiendd361c92012-04-10 23:40:37 +08001853 alignment = sizeof(int32_t);
1854 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001855 field_type = irb_.getJIntTy()->getPointerTo();
1856 } else {
Logan Chiendd361c92012-04-10 23:40:37 +08001857 alignment = irb_.getSizeOfPtrEquivInt();
1858 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001859 field_type = irb_.getJObjectTy()->getPointerTo();
1860 }
1861
Logan Chiendd361c92012-04-10 23:40:37 +08001862 llvm::Value* data_field_offset =
1863 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1864
1865 llvm::Value* data_field_addr =
1866 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1867
Logan Chiena85fb2f2012-01-16 12:52:56 +08001868 // TODO: Tune this code. Currently we are generating one instruction for
1869 // one element which may be very space consuming. Maybe changing to use
1870 // memcpy may help; however, since we can't guarantee that the alloca of
1871 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001872 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001873 int reg_index;
1874 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001875 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001876 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001877 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001878 }
1879
1880 llvm::Value* reg_value;
Logan Chien4b1baf12012-05-18 16:28:36 +08001881 if (is_elem_int_ty) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001882 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1883 } else {
1884 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1885 }
1886
TDYa127706e7db2012-05-06 00:05:33 -07001887 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001888
Logan Chiendd361c92012-04-10 23:40:37 +08001889 data_field_addr =
1890 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001891 }
1892 }
1893
1894 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1895
Logan Chien70f94b42011-12-27 17:49:11 +08001896 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1897}
1898
1899
1900void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1901 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001902
Elliott Hughesadb8c672012-03-06 16:49:32 -08001903 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001904
1905 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001906 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001907 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001908
Logan Chien19c350a2012-05-01 19:21:32 +08001909 const Instruction::ArrayDataPayload* payload =
1910 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1911 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001912
Logan Chien86f50672012-04-24 13:08:45 +08001913 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001914 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001915
Logan Chien86f50672012-04-24 13:08:45 +08001916 if (payload->element_count == 0) {
1917 // When the number of the elements in the payload is zero, we don't have
1918 // to copy any numbers. However, we should check whether the array object
1919 // address is equal to null or not.
1920 EmitGuard_NullPointerException(dex_pc, array_addr);
1921 } else {
1922 // To save the code size, we are going to call the runtime function to
1923 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001924
Logan Chien86f50672012-04-24 13:08:45 +08001925 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001926
Logan Chien86f50672012-04-24 13:08:45 +08001927 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001928
Logan Chien86f50672012-04-24 13:08:45 +08001929 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001930
Logan Chien86f50672012-04-24 13:08:45 +08001931 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001932
Logan Chien86f50672012-04-24 13:08:45 +08001933 irb_.CreateCall4(runtime_func,
1934 method_object_addr, irb_.getInt32(dex_pc),
1935 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001936
TDYa127526643e2012-05-26 01:01:48 -07001937 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chiene58b6582012-01-16 17:13:13 +08001938 }
1939
Logan Chien70f94b42011-12-27 17:49:11 +08001940 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1941}
1942
1943
1944void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1945 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001946
Elliott Hughesadb8c672012-03-06 16:49:32 -08001947 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001948
Elliott Hughesadb8c672012-03-06 16:49:32 -08001949 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001950
Logan Chiena466c162011-12-27 17:55:46 +08001951 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001952}
1953
1954
1955void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1956 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001957
Elliott Hughesadb8c672012-03-06 16:49:32 -08001958 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959
Logan Chien7a89b6d2011-12-27 17:56:56 +08001960 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001961 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001962
Logan Chien19c350a2012-05-01 19:21:32 +08001963 const Instruction::PackedSwitchPayload* payload =
1964 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1965 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001966
Elliott Hughesadb8c672012-03-06 16:49:32 -08001967 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001968
1969 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001970 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001971
Logan Chien19c350a2012-05-01 19:21:32 +08001972 for (uint16_t i = 0; i < payload->case_count; ++i) {
1973 sw->addCase(irb_.getInt32(payload->first_key + i),
1974 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001975 }
Logan Chien70f94b42011-12-27 17:49:11 +08001976}
1977
1978
1979void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1980 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001981
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983
Logan Chien7a89b6d2011-12-27 17:56:56 +08001984 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001985 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001986
Logan Chien19c350a2012-05-01 19:21:32 +08001987 const Instruction::SparseSwitchPayload* payload =
1988 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1989 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001990
Logan Chien19c350a2012-05-01 19:21:32 +08001991 const int32_t* keys = payload->GetKeys();
1992 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001993
Elliott Hughesadb8c672012-03-06 16:49:32 -08001994 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001995
1996 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001997 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001998
Logan Chien19c350a2012-05-01 19:21:32 +08001999 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002000 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2001 }
Logan Chien70f94b42011-12-27 17:49:11 +08002002}
2003
2004
2005void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2006 Instruction const* insn,
2007 JType fp_jty,
2008 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002009
Elliott Hughesadb8c672012-03-06 16:49:32 -08002010 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002011
2012 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2013
Elliott Hughesadb8c672012-03-06 16:49:32 -08002014 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2015 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002016
2017 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2018 llvm::Value* cmp_lt;
2019
2020 if (gt_bias) {
2021 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2022 } else {
2023 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2024 }
2025
2026 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002027 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002028
Logan Chien70f94b42011-12-27 17:49:11 +08002029 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2030}
2031
2032
2033void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2034 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002035
Elliott Hughesadb8c672012-03-06 16:49:32 -08002036 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002037
Elliott Hughesadb8c672012-03-06 16:49:32 -08002038 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2039 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002040
2041 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2042 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2043
2044 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002045 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002046
Logan Chien70f94b42011-12-27 17:49:11 +08002047 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2048}
2049
2050
Logan Chien2c37e8e2011-12-27 17:58:46 +08002051llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2052 llvm::Value* cmp_lt) {
2053
2054 llvm::Constant* zero = irb_.getJInt(0);
2055 llvm::Constant* pos1 = irb_.getJInt(1);
2056 llvm::Constant* neg1 = irb_.getJInt(-1);
2057
2058 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2059 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2060
2061 return result_eq;
2062}
2063
2064
Logan Chien70f94b42011-12-27 17:49:11 +08002065void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2066 Instruction const* insn,
2067 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002068
Elliott Hughesadb8c672012-03-06 16:49:32 -08002069 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002070
Elliott Hughesadb8c672012-03-06 16:49:32 -08002071 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2072 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002073
2074 DCHECK_NE(kRegUnknown, src1_reg_cat);
2075 DCHECK_NE(kRegUnknown, src2_reg_cat);
2076 DCHECK_NE(kRegCat2, src1_reg_cat);
2077 DCHECK_NE(kRegCat2, src2_reg_cat);
2078
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002080
Logan Chiena78e3c82011-12-27 17:59:35 +08002081 llvm::Value* src1_value;
2082 llvm::Value* src2_value;
2083
TDYa1278e9b4492012-04-24 15:50:27 -07002084 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2085 src1_value = irb_.getInt32(0);
2086 src2_value = irb_.getInt32(0);
2087 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002088 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2089
2090 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2092 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002093 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002094 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2095 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002096 }
2097 } else {
2098 DCHECK(src1_reg_cat == kRegZero ||
2099 src2_reg_cat == kRegZero);
2100
2101 if (src1_reg_cat == kRegZero) {
2102 if (src2_reg_cat == kRegCat1nr) {
2103 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002104 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002105 } else {
2106 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002107 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002108 }
2109 } else { // src2_reg_cat == kRegZero
2110 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002111 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002112 src2_value = irb_.getJInt(0);
2113 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002114 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002115 src2_value = irb_.getJNull();
2116 }
2117 }
2118 }
2119
2120 llvm::Value* cond_value =
2121 EmitConditionResult(src1_value, src2_value, cond);
2122
2123 irb_.CreateCondBr(cond_value,
2124 GetBasicBlock(dex_pc + branch_offset),
2125 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002126}
2127
2128
2129void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2130 Instruction const* insn,
2131 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002132
Elliott Hughesadb8c672012-03-06 16:49:32 -08002133 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002134
Elliott Hughesadb8c672012-03-06 16:49:32 -08002135 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002136
2137 DCHECK_NE(kRegUnknown, src_reg_cat);
2138 DCHECK_NE(kRegCat2, src_reg_cat);
2139
Elliott Hughesadb8c672012-03-06 16:49:32 -08002140 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002141
Logan Chiena78e3c82011-12-27 17:59:35 +08002142 llvm::Value* src1_value;
2143 llvm::Value* src2_value;
2144
TDYa1278e9b4492012-04-24 15:50:27 -07002145 if (src_reg_cat == kRegZero) {
2146 src1_value = irb_.getInt32(0);
2147 src2_value = irb_.getInt32(0);
2148 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002149 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002150 src2_value = irb_.getInt32(0);
2151 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002152 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002153 src2_value = irb_.getJNull();
2154 }
2155
2156 llvm::Value* cond_value =
2157 EmitConditionResult(src1_value, src2_value, cond);
2158
2159 irb_.CreateCondBr(cond_value,
2160 GetBasicBlock(dex_pc + branch_offset),
2161 GetNextBasicBlock(dex_pc));
2162}
2163
TDYa1271d7e5102012-05-13 09:27:05 -07002164InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002165 Compiler::MethodReference mref(dex_file_, method_idx_);
2166
2167 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002168 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002169
Logan Chiena78e3c82011-12-27 17:59:35 +08002170 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2171
TDYa1271d7e5102012-05-13 09:27:05 -07002172 return map;
2173}
2174
2175RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2176 uint16_t reg_idx) {
2177 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2178
Logan Chiena78e3c82011-12-27 17:59:35 +08002179 return map->GetRegCategory(dex_pc, reg_idx);
2180}
2181
TDYa1271d7e5102012-05-13 09:27:05 -07002182bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2183 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2184
2185 return map->IsRegCanBeObject(reg_idx);
2186}
2187
Logan Chiena78e3c82011-12-27 17:59:35 +08002188
2189llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2190 llvm::Value* rhs,
2191 CondBranchKind cond) {
2192 switch (cond) {
2193 case kCondBranch_EQ:
2194 return irb_.CreateICmpEQ(lhs, rhs);
2195
2196 case kCondBranch_NE:
2197 return irb_.CreateICmpNE(lhs, rhs);
2198
2199 case kCondBranch_LT:
2200 return irb_.CreateICmpSLT(lhs, rhs);
2201
2202 case kCondBranch_GE:
2203 return irb_.CreateICmpSGE(lhs, rhs);
2204
2205 case kCondBranch_GT:
2206 return irb_.CreateICmpSGT(lhs, rhs);
2207
2208 case kCondBranch_LE:
2209 return irb_.CreateICmpSLE(lhs, rhs);
2210
2211 default: // Unreachable
2212 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2213 return NULL;
2214 }
Logan Chien70f94b42011-12-27 17:49:11 +08002215}
2216
TDYa12783bb6622012-04-17 02:20:34 -07002217void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2218 // Using runtime support, let the target can override by InlineAssembly.
2219 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2220
2221 irb_.CreateCall2(runtime_func, value, target_addr);
2222}
Logan Chien70f94b42011-12-27 17:49:11 +08002223
Logan Chiene27fdbb2012-01-02 23:27:26 +08002224void
2225MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2226 llvm::Value* array,
2227 llvm::Value* index) {
2228 llvm::Value* array_len = EmitLoadArrayLength(array);
2229
2230 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2231
2232 llvm::BasicBlock* block_exception =
2233 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2234
2235 llvm::BasicBlock* block_continue =
2236 CreateBasicBlockWithDexPC(dex_pc, "cont");
2237
TDYa127ac7b5bb2012-05-11 13:17:49 -07002238 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002239
2240 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002241
TDYa127c8dc1012012-04-19 07:03:33 -07002242 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002243 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2244 EmitBranchExceptionLandingPad(dex_pc);
2245
2246 irb_.SetInsertPoint(block_continue);
2247}
2248
2249
2250void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2251 llvm::Value* array,
2252 llvm::Value* index) {
2253 EmitGuard_NullPointerException(dex_pc, array);
2254 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2255}
2256
2257
2258// Emit Array GetElementPtr
2259llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2260 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002261 JType elem_jty) {
2262
2263 int data_offset;
2264 if (elem_jty == kLong || elem_jty == kDouble ||
2265 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2266 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2267 } else {
2268 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2269 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002270
2271 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002272 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002273
TDYa1278db6ea32012-05-17 04:48:42 -07002274 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2275
Logan Chiene27fdbb2012-01-02 23:27:26 +08002276 llvm::Value* array_data_addr =
2277 irb_.CreatePtrDisp(array_addr, data_offset_value,
2278 elem_type->getPointerTo());
2279
2280 return irb_.CreateGEP(array_data_addr, index_value);
2281}
2282
2283
Logan Chien70f94b42011-12-27 17:49:11 +08002284void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2285 Instruction const* insn,
2286 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002287
Elliott Hughesadb8c672012-03-06 16:49:32 -08002288 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002289
Elliott Hughesadb8c672012-03-06 16:49:32 -08002290 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2291 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002292
2293 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2294
TDYa1278db6ea32012-05-17 04:48:42 -07002295 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002296
TDYa127706e7db2012-05-06 00:05:33 -07002297 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002298
Elliott Hughesadb8c672012-03-06 16:49:32 -08002299 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002300
Logan Chien70f94b42011-12-27 17:49:11 +08002301 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2302}
2303
2304
2305void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2306 Instruction const* insn,
2307 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002308
Elliott Hughesadb8c672012-03-06 16:49:32 -08002309 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002310
Elliott Hughesadb8c672012-03-06 16:49:32 -08002311 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2312 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002313
2314 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2315
TDYa1278db6ea32012-05-17 04:48:42 -07002316 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002317
Elliott Hughesadb8c672012-03-06 16:49:32 -08002318 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002319
TDYa12783bb6622012-04-17 02:20:34 -07002320 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002321 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2322
2323 irb_.CreateCall2(runtime_func, new_value, array_addr);
2324
TDYa127526643e2012-05-26 01:01:48 -07002325 EmitGuard_ExceptionLandingPad(dex_pc, false);
TDYa12783bb6622012-04-17 02:20:34 -07002326
2327 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002328 }
2329
TDYa127706e7db2012-05-06 00:05:33 -07002330 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002331
Logan Chien70f94b42011-12-27 17:49:11 +08002332 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2333}
2334
2335
2336void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2337 Instruction const* insn,
2338 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002339
Elliott Hughesadb8c672012-03-06 16:49:32 -08002340 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002341
Elliott Hughesadb8c672012-03-06 16:49:32 -08002342 uint32_t reg_idx = dec_insn.vB;
2343 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002344
Logan Chien48f1d2a2012-01-02 22:49:53 +08002345 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2346
TDYa127cc1b4c32012-05-15 07:31:37 -07002347 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2348 EmitGuard_NullPointerException(dex_pc, object_addr);
2349 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002350
2351 llvm::Value* field_value;
2352
Logan Chien933abf82012-04-11 12:24:31 +08002353 int field_offset;
2354 bool is_volatile;
2355 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2356 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002357
Logan Chien933abf82012-04-11 12:24:31 +08002358 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002359 llvm::Function* runtime_func;
2360
2361 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002362 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002363 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002364 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002365 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002366 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002367 }
2368
2369 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2370
2371 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2372
TDYa127c8dc1012012-04-19 07:03:33 -07002373 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002374
Logan Chien3b2b2e72012-03-06 16:11:45 +08002375 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2376 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002377
TDYa127526643e2012-05-26 01:01:48 -07002378 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002379
2380 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002381 DCHECK_GE(field_offset, 0);
2382
Logan Chien48f1d2a2012-01-02 22:49:53 +08002383 llvm::PointerType* field_type =
2384 irb_.getJType(field_jty, kField)->getPointerTo();
2385
Logan Chien933abf82012-04-11 12:24:31 +08002386 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002387
2388 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002389 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002390
Logan Chien933abf82012-04-11 12:24:31 +08002391 // TODO: Check is_volatile. We need to generate atomic load instruction
2392 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002393 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002394 }
2395
Elliott Hughesadb8c672012-03-06 16:49:32 -08002396 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002397
Logan Chien70f94b42011-12-27 17:49:11 +08002398 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2399}
2400
2401
2402void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2403 Instruction const* insn,
2404 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002405
Elliott Hughesadb8c672012-03-06 16:49:32 -08002406 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002407
Elliott Hughesadb8c672012-03-06 16:49:32 -08002408 uint32_t reg_idx = dec_insn.vB;
2409 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002410
Logan Chiendd6aa872012-01-03 16:06:32 +08002411 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2412
TDYa127cc1b4c32012-05-15 07:31:37 -07002413 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2414 EmitGuard_NullPointerException(dex_pc, object_addr);
2415 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002416
Elliott Hughesadb8c672012-03-06 16:49:32 -08002417 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002418
Logan Chien933abf82012-04-11 12:24:31 +08002419 int field_offset;
2420 bool is_volatile;
2421 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2422 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002423
Logan Chien933abf82012-04-11 12:24:31 +08002424 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002425 llvm::Function* runtime_func;
2426
2427 if (field_jty == kObject) {
2428 runtime_func = irb_.GetRuntime(SetObjectInstance);
2429 } else if (field_jty == kLong || field_jty == kDouble) {
2430 runtime_func = irb_.GetRuntime(Set64Instance);
2431 } else {
2432 runtime_func = irb_.GetRuntime(Set32Instance);
2433 }
2434
2435 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2436
2437 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2438
TDYa127c8dc1012012-04-19 07:03:33 -07002439 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002440
Logan Chien3b2b2e72012-03-06 16:11:45 +08002441 irb_.CreateCall4(runtime_func, field_idx_value,
2442 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002443
TDYa127526643e2012-05-26 01:01:48 -07002444 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002445
2446 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002447 DCHECK_GE(field_offset, 0);
2448
Logan Chiendd6aa872012-01-03 16:06:32 +08002449 llvm::PointerType* field_type =
2450 irb_.getJType(field_jty, kField)->getPointerTo();
2451
Logan Chien933abf82012-04-11 12:24:31 +08002452 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002453
2454 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002455 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002456
Logan Chien933abf82012-04-11 12:24:31 +08002457 // TODO: Check is_volatile. We need to generate atomic store instruction
2458 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002459 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002460
2461 if (field_jty == kObject) { // If put an object, mark the GC card table.
2462 EmitMarkGCCard(new_value, object_addr);
2463 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002464 }
2465
Logan Chien70f94b42011-12-27 17:49:11 +08002466 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2467}
2468
2469
Logan Chien438c4b62012-01-17 16:06:00 +08002470llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2471 uint32_t type_idx) {
2472 llvm::BasicBlock* block_load_static =
2473 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2474
2475 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2476
2477 // Load static storage from dex cache
2478 llvm::Value* storage_field_addr =
2479 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2480
TDYa1278ca10052012-05-05 19:57:06 -07002481 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002482
2483 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2484
2485 // Test: Is the static storage of this class initialized?
2486 llvm::Value* equal_null =
2487 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2488
TDYa127ac7b5bb2012-05-11 13:17:49 -07002489 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002490
2491 // Failback routine to load the class object
2492 irb_.SetInsertPoint(block_load_static);
2493
2494 llvm::Function* runtime_func =
2495 irb_.GetRuntime(InitializeStaticStorage);
2496
2497 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2498
2499 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2500
TDYa127de479be2012-05-31 08:03:26 -07002501 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07002502
TDYa127c8dc1012012-04-19 07:03:33 -07002503 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002504
Logan Chien438c4b62012-01-17 16:06:00 +08002505 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002506 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002507
TDYa127526643e2012-05-26 01:01:48 -07002508 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002509
2510 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2511
2512 irb_.CreateBr(block_cont);
2513
2514 // Now the class object must be loaded
2515 irb_.SetInsertPoint(block_cont);
2516
2517 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2518
2519 phi->addIncoming(storage_object_addr, block_original);
2520 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2521
2522 return phi;
2523}
2524
2525
Logan Chien70f94b42011-12-27 17:49:11 +08002526void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2527 Instruction const* insn,
2528 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002529
Elliott Hughesadb8c672012-03-06 16:49:32 -08002530 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002531
Logan Chien933abf82012-04-11 12:24:31 +08002532 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002533
Logan Chien933abf82012-04-11 12:24:31 +08002534 int field_offset;
2535 int ssb_index;
2536 bool is_referrers_class;
2537 bool is_volatile;
2538
2539 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2540 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2541 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002542
2543 llvm::Value* static_field_value;
2544
Logan Chien933abf82012-04-11 12:24:31 +08002545 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002546 llvm::Function* runtime_func;
2547
2548 if (field_jty == kObject) {
2549 runtime_func = irb_.GetRuntime(GetObjectStatic);
2550 } else if (field_jty == kLong || field_jty == kDouble) {
2551 runtime_func = irb_.GetRuntime(Get64Static);
2552 } else {
2553 runtime_func = irb_.GetRuntime(Get32Static);
2554 }
2555
Elliott Hughesadb8c672012-03-06 16:49:32 -08002556 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002557
2558 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2559
TDYa127c8dc1012012-04-19 07:03:33 -07002560 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002561
Logan Chien438c4b62012-01-17 16:06:00 +08002562 static_field_value =
2563 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2564
TDYa127526643e2012-05-26 01:01:48 -07002565 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien438c4b62012-01-17 16:06:00 +08002566
2567 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002568 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002569
Logan Chien933abf82012-04-11 12:24:31 +08002570 llvm::Value* static_storage_addr = NULL;
2571
2572 if (is_referrers_class) {
2573 // Fast path, static storage base is this method's class
2574 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2575
TDYa127ee1f59b2012-04-25 00:56:40 -07002576 static_storage_addr =
2577 irb_.LoadFromObjectOffset(method_object_addr,
2578 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002579 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002580 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002581 } else {
2582 // Medium path, static storage base in a different class which
2583 // requires checks that the other class is initialized
2584 DCHECK_GE(ssb_index, 0);
2585 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2586 }
2587
2588 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002589
2590 llvm::Value* static_field_addr =
2591 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2592 irb_.getJType(field_jty, kField)->getPointerTo());
2593
Logan Chien933abf82012-04-11 12:24:31 +08002594 // TODO: Check is_volatile. We need to generate atomic load instruction
2595 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002596 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002597 }
2598
Elliott Hughesadb8c672012-03-06 16:49:32 -08002599 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002600
Logan Chien70f94b42011-12-27 17:49:11 +08002601 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2602}
2603
2604
2605void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2606 Instruction const* insn,
2607 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002608
Elliott Hughesadb8c672012-03-06 16:49:32 -08002609 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002610
Logan Chien933abf82012-04-11 12:24:31 +08002611 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002612
Elliott Hughesadb8c672012-03-06 16:49:32 -08002613 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002614
Logan Chien933abf82012-04-11 12:24:31 +08002615 int field_offset;
2616 int ssb_index;
2617 bool is_referrers_class;
2618 bool is_volatile;
2619
2620 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2621 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2622 is_referrers_class, is_volatile, true);
2623
2624 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002625 llvm::Function* runtime_func;
2626
2627 if (field_jty == kObject) {
2628 runtime_func = irb_.GetRuntime(SetObjectStatic);
2629 } else if (field_jty == kLong || field_jty == kDouble) {
2630 runtime_func = irb_.GetRuntime(Set64Static);
2631 } else {
2632 runtime_func = irb_.GetRuntime(Set32Static);
2633 }
2634
Elliott Hughesadb8c672012-03-06 16:49:32 -08002635 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002636
2637 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2638
TDYa127c8dc1012012-04-19 07:03:33 -07002639 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002640
Logan Chien14179c82012-01-17 17:06:34 +08002641 irb_.CreateCall3(runtime_func, field_idx_value,
2642 method_object_addr, new_value);
2643
TDYa127526643e2012-05-26 01:01:48 -07002644 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien14179c82012-01-17 17:06:34 +08002645
2646 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002647 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002648
Logan Chien933abf82012-04-11 12:24:31 +08002649 llvm::Value* static_storage_addr = NULL;
2650
2651 if (is_referrers_class) {
2652 // Fast path, static storage base is this method's class
2653 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2654
TDYa127ee1f59b2012-04-25 00:56:40 -07002655 static_storage_addr =
2656 irb_.LoadFromObjectOffset(method_object_addr,
2657 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002658 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002659 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002660 } else {
2661 // Medium path, static storage base in a different class which
2662 // requires checks that the other class is initialized
2663 DCHECK_GE(ssb_index, 0);
2664 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2665 }
2666
2667 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002668
2669 llvm::Value* static_field_addr =
2670 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2671 irb_.getJType(field_jty, kField)->getPointerTo());
2672
Logan Chien933abf82012-04-11 12:24:31 +08002673 // TODO: Check is_volatile. We need to generate atomic store instruction
2674 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002675 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002676
2677 if (field_jty == kObject) { // If put an object, mark the GC card table.
2678 EmitMarkGCCard(new_value, static_storage_addr);
2679 }
Logan Chien14179c82012-01-17 17:06:34 +08002680 }
2681
Logan Chien70f94b42011-12-27 17:49:11 +08002682 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2683}
2684
2685
Logan Chien1a121b92012-02-15 22:23:42 +08002686void MethodCompiler::
2687EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2688 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002690 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002691 bool is_static) {
2692
2693 // Get method signature
2694 DexFile::MethodId const& method_id =
2695 dex_file_->GetMethodId(callee_method_idx);
2696
Logan Chien8faf8022012-02-24 12:25:29 +08002697 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002698 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002699 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002700
2701 // Load argument values according to the shorty (without "this")
2702 uint16_t reg_count = 0;
2703
2704 if (!is_static) {
2705 ++reg_count; // skip the "this" pointer
2706 }
2707
Logan Chien7e7fabc2012-04-10 18:59:11 +08002708 bool is_range = (arg_fmt == kArgRange);
2709
Logan Chien8faf8022012-02-24 12:25:29 +08002710 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002711 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2712 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002713
2714 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2715
2716 ++reg_count;
2717 if (shorty[i] == 'J' || shorty[i] == 'D') {
2718 // Wide types, such as long and double, are using a pair of registers
2719 // to store the value, so we have to increase arg_reg again.
2720 ++reg_count;
2721 }
2722 }
2723
Elliott Hughesadb8c672012-03-06 16:49:32 -08002724 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002725 << "Actual argument mismatch for callee: "
2726 << PrettyMethod(callee_method_idx, *dex_file_);
2727}
2728
TDYa1270b686e52012-04-09 22:43:35 -07002729llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2730 uint32_t method_idx,
2731 bool is_static) {
2732 // TODO: Remove this after we solve the link and trampoline related problems.
2733 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2734
2735 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2736
2737 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002738}
Logan Chien1a121b92012-02-15 22:23:42 +08002739
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002740
Logan Chien7e7fabc2012-04-10 18:59:11 +08002741void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2742 Instruction const* insn,
2743 InvokeType invoke_type,
2744 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002745 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002746
Logan Chien61c65dc2012-02-29 03:22:30 +08002747 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002748 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002749
Logan Chien7e7fabc2012-04-10 18:59:11 +08002750 // Compute invoke related information for compiler decision
2751 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002752 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002753 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002754 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002755 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002756 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002757
Logan Chien7e7fabc2012-04-10 18:59:11 +08002758 // Load *this* actual parameter
Logan Chien82d31cd2012-05-23 18:37:44 +08002759 uint32_t this_reg = -1u;
Logan Chien1a121b92012-02-15 22:23:42 +08002760 llvm::Value* this_addr = NULL;
2761
2762 if (!is_static) {
2763 // Test: Is *this* parameter equal to null?
Logan Chien82d31cd2012-05-23 18:37:44 +08002764 this_reg = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2765 this_addr = EmitLoadDalvikReg(this_reg, kObject, kAccurate);
Logan Chien1a121b92012-02-15 22:23:42 +08002766 }
2767
Logan Chien7e7fabc2012-04-10 18:59:11 +08002768 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002769 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002770
2771 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002772 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002773 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2774 this_addr, dex_pc, is_fast_path);
Logan Chien82d31cd2012-05-23 18:37:44 +08002775
2776 if (!is_static && (!method_info_.this_will_not_be_null ||
2777 this_reg != method_info_.this_reg_idx)) {
2778 // NOTE: The null pointer test should come after the method resolution.
2779 // So that the "NoSuchMethodError" can be thrown before the
2780 // "NullPointerException".
2781 EmitGuard_NullPointerException(dex_pc, this_addr);
2782 }
2783
Logan Chien7e7fabc2012-04-10 18:59:11 +08002784 } else {
Logan Chien82d31cd2012-05-23 18:37:44 +08002785 if (!is_static && (!method_info_.this_will_not_be_null ||
2786 this_reg != method_info_.this_reg_idx)) {
2787 // NOTE: In the fast path, we should do the null pointer check
2788 // before the access to the class object and/or direct invocation.
2789 EmitGuard_NullPointerException(dex_pc, this_addr);
2790 }
2791
Logan Chien7e7fabc2012-04-10 18:59:11 +08002792 switch (invoke_type) {
2793 case kStatic:
2794 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002795 if (direct_method != 0u &&
2796 direct_method != static_cast<uintptr_t>(-1)) {
2797 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002798 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2799 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002800 } else {
2801 callee_method_object_addr =
2802 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2803 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002804 break;
2805
2806 case kVirtual:
2807 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002808 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002809 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2810 break;
2811
2812 case kSuper:
2813 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2814 "the fast path.";
2815 break;
2816
2817 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002818 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002819 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2820 invoke_type, this_addr,
2821 dex_pc, is_fast_path);
2822 break;
2823 }
2824 }
2825
Logan Chien1a121b92012-02-15 22:23:42 +08002826 // Load the actual parameter
2827 std::vector<llvm::Value*> args;
2828
2829 args.push_back(callee_method_object_addr); // method object for callee
2830
2831 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002832 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002833 args.push_back(this_addr); // "this" object for callee
2834 }
2835
2836 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002837 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002838
TDYa12729c0cd12012-05-17 04:51:08 -07002839 if (is_fast_path && (invoke_type == kDirect || invoke_type == kStatic)) {
2840 bool need_retry = EmitInlineJavaIntrinsic(PrettyMethod(callee_method_idx, *dex_file_),
2841 args,
2842 GetNextBasicBlock(dex_pc));
2843 if (!need_retry) {
2844 return;
2845 }
2846 }
2847
2848 llvm::Value* code_addr =
2849 irb_.LoadFromObjectOffset(callee_method_object_addr,
2850 Method::GetCodeOffset().Int32Value(),
2851 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2852 kTBAAJRuntime);
2853
TDYa1275bb86012012-04-11 05:57:28 -07002854#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002855 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002856 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002857 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa127526643e2012-05-26 01:01:48 -07002858 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien1a121b92012-02-15 22:23:42 +08002859
Logan Chien7e7fabc2012-04-10 18:59:11 +08002860 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2861 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2862 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2863
2864 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002865 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002866 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2867 }
TDYa1275bb86012012-04-11 05:57:28 -07002868#else
2869 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2870 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2871 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2872
2873 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2874
2875
TDYa127c8dc1012012-04-19 07:03:33 -07002876 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002877
2878
2879 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002880 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002881 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2882
TDYa1275bb86012012-04-11 05:57:28 -07002883 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002884 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2885 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
TDYa127ac7b5bb2012-05-11 13:17:49 -07002886 irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002887
2888
2889 irb_.SetInsertPoint(block_normal);
2890 {
2891 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002892 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002893 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002894 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002895 }
2896 }
2897 irb_.CreateBr(block_continue);
2898
2899
TDYa127ce154722012-04-21 16:43:29 -07002900 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002901 {
TDYa127ce154722012-04-21 16:43:29 -07002902 { // lazy link
2903 // TODO: Remove this after we solve the link problem.
2904 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2905 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2906
TDYa127ac7b5bb2012-05-11 13:17:49 -07002907 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely);
TDYa127ce154722012-04-21 16:43:29 -07002908
2909
2910 irb_.SetInsertPoint(block_link);
2911 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2912
TDYa127526643e2012-05-26 01:01:48 -07002913 EmitGuard_ExceptionLandingPad(dex_pc, false);
TDYa127ce154722012-04-21 16:43:29 -07002914
2915 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2916 if (ret_shorty != 'V') {
2917 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2918 }
2919 irb_.CreateBr(block_continue);
2920
2921
2922 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002923 }
TDYa127ce154722012-04-21 16:43:29 -07002924 { // proxy stub
TDYa127ce154722012-04-21 16:43:29 -07002925 if (ret_shorty != 'V') {
TDYa127b9ff6b12012-05-17 11:14:29 -07002926 args.push_back(jvalue_temp_);
TDYa127ce154722012-04-21 16:43:29 -07002927 }
2928 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2929 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2930 if (ret_shorty != 'V') {
Shih-wei Liaoe6a7adc2012-05-09 02:50:08 -07002931 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa127ce154722012-04-21 16:43:29 -07002932 llvm::Value* result_addr =
TDYa127b9ff6b12012-05-17 11:14:29 -07002933 irb_.CreateBitCast(jvalue_temp_, accurate_ret_type->getPointerTo());
TDYa127aba61122012-05-04 18:28:36 -07002934 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
TDYa127ce154722012-04-21 16:43:29 -07002935 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2936 }
TDYa1275bb86012012-04-11 05:57:28 -07002937 }
2938 }
2939 irb_.CreateBr(block_continue);
2940
2941
2942 irb_.SetInsertPoint(block_continue);
2943
TDYa127526643e2012-05-26 01:01:48 -07002944 EmitGuard_ExceptionLandingPad(dex_pc, true);
TDYa1275bb86012012-04-11 05:57:28 -07002945#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002946
Logan Chien70f94b42011-12-27 17:49:11 +08002947 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2948}
2949
2950
Logan Chien7e7fabc2012-04-10 18:59:11 +08002951llvm::Value* MethodCompiler::
2952EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2953 llvm::Value* callee_method_object_field_addr =
2954 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002955
TDYa1278ca10052012-05-05 19:57:06 -07002956 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002957}
Logan Chien7caf37e2012-02-03 22:56:04 +08002958
Logan Chien7caf37e2012-02-03 22:56:04 +08002959
Logan Chien7e7fabc2012-04-10 18:59:11 +08002960llvm::Value* MethodCompiler::
2961EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2962 llvm::Value* this_addr) {
2963 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002964 llvm::Value* class_object_addr =
2965 irb_.LoadFromObjectOffset(this_addr,
2966 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002967 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002968 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002969
Logan Chien7e7fabc2012-04-10 18:59:11 +08002970 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002971 llvm::Value* vtable_addr =
2972 irb_.LoadFromObjectOffset(class_object_addr,
2973 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002974 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002975 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002976
2977 // Load callee method object
2978 llvm::Value* vtable_idx_value =
2979 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2980
2981 llvm::Value* method_field_addr =
TDYa1278db6ea32012-05-17 04:48:42 -07002982 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002983
TDYa127d3e24c22012-05-05 20:54:19 -07002984 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002985}
2986
2987
2988llvm::Value* MethodCompiler::
2989EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2990 InvokeType invoke_type,
2991 llvm::Value* this_addr,
2992 uint32_t dex_pc,
2993 bool is_fast_path) {
2994
2995 llvm::Function* runtime_func = NULL;
2996
2997 switch (invoke_type) {
2998 case kStatic:
2999 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
3000 break;
3001
3002 case kDirect:
3003 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
3004 break;
3005
3006 case kVirtual:
3007 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
3008 break;
3009
3010 case kSuper:
3011 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3012 break;
3013
3014 case kInterface:
3015 if (is_fast_path) {
3016 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3017 } else {
3018 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3019 }
3020 break;
3021 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003022
3023 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3024
Logan Chien7e7fabc2012-04-10 18:59:11 +08003025 if (this_addr == NULL) {
3026 DCHECK_EQ(invoke_type, kStatic);
3027 this_addr = irb_.getJNull();
3028 }
3029
3030 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3031
TDYa127de479be2012-05-31 08:03:26 -07003032 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07003033
TDYa127c8dc1012012-04-19 07:03:33 -07003034 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003035
TDYa1270b686e52012-04-09 22:43:35 -07003036 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003037 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003038 callee_method_idx_value,
3039 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003040 caller_method_object_addr,
3041 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003042
TDYa127526643e2012-05-26 01:01:48 -07003043 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien7caf37e2012-02-03 22:56:04 +08003044
Logan Chien7e7fabc2012-04-10 18:59:11 +08003045 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003046}
3047
3048
3049void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3050 Instruction const* insn,
3051 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003052
Elliott Hughesadb8c672012-03-06 16:49:32 -08003053 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003054
3055 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3056
Elliott Hughesadb8c672012-03-06 16:49:32 -08003057 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003058 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003059 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003060
Logan Chien70f94b42011-12-27 17:49:11 +08003061 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3062}
3063
3064
3065void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3066 Instruction const* insn,
3067 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003068
Elliott Hughesadb8c672012-03-06 16:49:32 -08003069 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003070
3071 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3072
Elliott Hughesadb8c672012-03-06 16:49:32 -08003073 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003074 llvm::Value* result_value =
3075 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3076
Elliott Hughesadb8c672012-03-06 16:49:32 -08003077 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003078
Logan Chien70f94b42011-12-27 17:49:11 +08003079 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3080}
3081
3082
3083void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3084 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003085
Elliott Hughesadb8c672012-03-06 16:49:32 -08003086 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003087
Elliott Hughesadb8c672012-03-06 16:49:32 -08003088 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003089 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003090 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003091
Logan Chien70f94b42011-12-27 17:49:11 +08003092 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3093}
3094
3095
3096void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3097 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003098
Elliott Hughesadb8c672012-03-06 16:49:32 -08003099 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003100
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003102 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003103 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003104
Logan Chien70f94b42011-12-27 17:49:11 +08003105 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3106}
3107
3108
3109void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3110 Instruction const* insn,
3111 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003112
Elliott Hughesadb8c672012-03-06 16:49:32 -08003113 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003114
Elliott Hughesadb8c672012-03-06 16:49:32 -08003115 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003116
3117 llvm::Value* trunc_value =
3118 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3119
3120 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3121
Elliott Hughesadb8c672012-03-06 16:49:32 -08003122 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003123
Logan Chien70f94b42011-12-27 17:49:11 +08003124 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3125}
3126
3127
3128void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3129 Instruction const* insn,
3130 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003131
Elliott Hughesadb8c672012-03-06 16:49:32 -08003132 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003133
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003135
3136 llvm::Value* trunc_value =
3137 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3138
3139 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3140
Elliott Hughesadb8c672012-03-06 16:49:32 -08003141 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003142
Logan Chien70f94b42011-12-27 17:49:11 +08003143 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3144}
3145
3146
3147void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3148 Instruction const* insn,
3149 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003150
Elliott Hughesadb8c672012-03-06 16:49:32 -08003151 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003152
3153 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3154
Elliott Hughesadb8c672012-03-06 16:49:32 -08003155 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003156 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003157 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003158
Logan Chien70f94b42011-12-27 17:49:11 +08003159 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3160}
3161
3162
3163void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3164 Instruction const* insn,
3165 JType src_jty,
3166 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003167
Elliott Hughesadb8c672012-03-06 16:49:32 -08003168 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003169
3170 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3171 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3172
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003174 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3175 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003176 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003177
Logan Chien70f94b42011-12-27 17:49:11 +08003178 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3179}
3180
3181
3182void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3183 Instruction const* insn,
3184 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003185 JType dest_jty,
3186 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003187
Elliott Hughesadb8c672012-03-06 16:49:32 -08003188 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003189
3190 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3191 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3192
Elliott Hughesadb8c672012-03-06 16:49:32 -08003193 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003194 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003195 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003196
Logan Chien70f94b42011-12-27 17:49:11 +08003197 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3198}
3199
3200
3201void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3202 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003203
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003205
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003207 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003208 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003209
Logan Chien70f94b42011-12-27 17:49:11 +08003210 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3211}
3212
3213
3214void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3215 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003216
Elliott Hughesadb8c672012-03-06 16:49:32 -08003217 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003218
Elliott Hughesadb8c672012-03-06 16:49:32 -08003219 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003220 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003221 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003222
Logan Chien70f94b42011-12-27 17:49:11 +08003223 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3224}
3225
3226
3227void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3228 Instruction const* insn,
3229 IntArithmKind arithm,
3230 JType op_jty,
3231 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003232
Elliott Hughesadb8c672012-03-06 16:49:32 -08003233 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003234
3235 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3236
3237 llvm::Value* src1_value;
3238 llvm::Value* src2_value;
3239
3240 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003241 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3242 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003243 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003244 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3245 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003246 }
3247
3248 llvm::Value* result_value =
3249 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3250 arithm, op_jty);
3251
Elliott Hughesadb8c672012-03-06 16:49:32 -08003252 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003253
Logan Chien70f94b42011-12-27 17:49:11 +08003254 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3255}
3256
3257
3258void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3259 Instruction const* insn,
3260 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003261
Elliott Hughesadb8c672012-03-06 16:49:32 -08003262 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003263
Elliott Hughesadb8c672012-03-06 16:49:32 -08003264 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003265
Elliott Hughesadb8c672012-03-06 16:49:32 -08003266 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003267
3268 llvm::Value* result_value =
3269 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3270
Elliott Hughesadb8c672012-03-06 16:49:32 -08003271 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003272
Logan Chien70f94b42011-12-27 17:49:11 +08003273 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3274}
3275
3276
Logan Chienc3f7d962011-12-27 18:13:18 +08003277llvm::Value*
3278MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3279 llvm::Value* lhs,
3280 llvm::Value* rhs,
3281 IntArithmKind arithm,
3282 JType op_jty) {
3283 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3284
3285 switch (arithm) {
3286 case kIntArithm_Add:
3287 return irb_.CreateAdd(lhs, rhs);
3288
3289 case kIntArithm_Sub:
3290 return irb_.CreateSub(lhs, rhs);
3291
3292 case kIntArithm_Mul:
3293 return irb_.CreateMul(lhs, rhs);
3294
3295 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003296 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003297 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003298
3299 case kIntArithm_And:
3300 return irb_.CreateAnd(lhs, rhs);
3301
3302 case kIntArithm_Or:
3303 return irb_.CreateOr(lhs, rhs);
3304
3305 case kIntArithm_Xor:
3306 return irb_.CreateXor(lhs, rhs);
3307
Logan Chienc3f7d962011-12-27 18:13:18 +08003308 default:
3309 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3310 return NULL;
3311 }
3312}
3313
3314
TDYa127f8641ce2012-04-02 06:40:40 -07003315llvm::Value*
3316MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3317 llvm::Value* dividend,
3318 llvm::Value* divisor,
3319 IntArithmKind arithm,
3320 JType op_jty) {
3321 // Throw exception if the divisor is 0.
3322 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3323
3324 // Check the special case: MININT / -1 = MININT
3325 // That case will cause overflow, which is undefined behavior in llvm.
3326 // So we check the divisor is -1 or not, if the divisor is -1, we do
3327 // the special path to avoid undefined behavior.
3328 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3329 llvm::Value* zero = irb_.getJZero(op_jty);
3330 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3331 llvm::Value* result = irb_.CreateAlloca(op_type);
3332
3333 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3334 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3335 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3336
3337 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003338 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003339
3340 // If divisor == -1
3341 irb_.SetInsertPoint(eq_neg_one);
3342 llvm::Value* eq_result;
3343 if (arithm == kIntArithm_Div) {
3344 // We can just change from "dividend div -1" to "neg dividend".
3345 // The sub don't care the sign/unsigned because of two's complement representation.
3346 // And the behavior is what we want:
3347 // -(2^n) (2^n)-1
3348 // MININT < k <= MAXINT -> mul k -1 = -k
3349 // MININT == k -> mul k -1 = k
3350 //
3351 // LLVM use sub to represent 'neg'
3352 eq_result = irb_.CreateSub(zero, dividend);
3353 } else {
3354 // Everything modulo -1 will be 0.
3355 eq_result = zero;
3356 }
TDYa127aba61122012-05-04 18:28:36 -07003357 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003358 irb_.CreateBr(neg_one_cont);
3359
3360 // If divisor != -1, just do the division.
3361 irb_.SetInsertPoint(ne_neg_one);
3362 llvm::Value* ne_result;
3363 if (arithm == kIntArithm_Div) {
3364 ne_result = irb_.CreateSDiv(dividend, divisor);
3365 } else {
3366 ne_result = irb_.CreateSRem(dividend, divisor);
3367 }
TDYa127aba61122012-05-04 18:28:36 -07003368 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003369 irb_.CreateBr(neg_one_cont);
3370
3371 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003372 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003373}
3374
3375
Logan Chien5539ad02012-04-02 14:36:55 +08003376void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3377 Instruction const* insn,
3378 IntShiftArithmKind arithm,
3379 JType op_jty,
3380 bool is_2addr) {
3381
3382 DecodedInstruction dec_insn(insn);
3383
3384 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3385
3386 llvm::Value* src1_value;
3387 llvm::Value* src2_value;
3388
3389 // NOTE: The 2nd operand of the shift arithmetic instruction is
3390 // 32-bit integer regardless of the 1st operand.
3391 if (is_2addr) {
3392 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3393 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3394 } else {
3395 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3396 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3397 }
3398
3399 llvm::Value* result_value =
3400 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3401 arithm, op_jty);
3402
3403 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3404
3405 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3406}
3407
3408
3409void MethodCompiler::
3410EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3411 Instruction const* insn,
3412 IntShiftArithmKind arithm) {
3413
3414 DecodedInstruction dec_insn(insn);
3415
3416 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3417
3418 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3419
3420 llvm::Value* result_value =
3421 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3422 arithm, kInt);
3423
3424 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3425
3426 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3427}
3428
3429
3430llvm::Value*
3431MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3432 llvm::Value* lhs,
3433 llvm::Value* rhs,
3434 IntShiftArithmKind arithm,
3435 JType op_jty) {
3436 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3437
3438 if (op_jty == kInt) {
3439 rhs = irb_.CreateAnd(rhs, 0x1f);
3440 } else {
3441 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3442 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3443 }
3444
3445 switch (arithm) {
3446 case kIntArithm_Shl:
3447 return irb_.CreateShl(lhs, rhs);
3448
3449 case kIntArithm_Shr:
3450 return irb_.CreateAShr(lhs, rhs);
3451
3452 case kIntArithm_UShr:
3453 return irb_.CreateLShr(lhs, rhs);
3454
3455 default:
3456 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3457 return NULL;
3458 }
3459}
3460
3461
Logan Chien70f94b42011-12-27 17:49:11 +08003462void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3463 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003464
Elliott Hughesadb8c672012-03-06 16:49:32 -08003465 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003466
Elliott Hughesadb8c672012-03-06 16:49:32 -08003467 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3468 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003469 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003470 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003471
Logan Chien70f94b42011-12-27 17:49:11 +08003472 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3473}
3474
3475
3476void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3477 Instruction const* insn,
3478 FPArithmKind arithm,
3479 JType op_jty,
3480 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003481
Elliott Hughesadb8c672012-03-06 16:49:32 -08003482 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003483
3484 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3485
3486 llvm::Value* src1_value;
3487 llvm::Value* src2_value;
3488
3489 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003490 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3491 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003492 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003493 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3494 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003495 }
3496
3497 llvm::Value* result_value =
3498 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3499
Elliott Hughesadb8c672012-03-06 16:49:32 -08003500 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003501
Logan Chien70f94b42011-12-27 17:49:11 +08003502 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3503}
3504
3505
Logan Chien76e1c792011-12-27 18:15:01 +08003506llvm::Value*
3507MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3508 llvm::Value *lhs,
3509 llvm::Value *rhs,
3510 FPArithmKind arithm) {
3511 switch (arithm) {
3512 case kFPArithm_Add:
3513 return irb_.CreateFAdd(lhs, rhs);
3514
3515 case kFPArithm_Sub:
3516 return irb_.CreateFSub(lhs, rhs);
3517
3518 case kFPArithm_Mul:
3519 return irb_.CreateFMul(lhs, rhs);
3520
3521 case kFPArithm_Div:
3522 return irb_.CreateFDiv(lhs, rhs);
3523
3524 case kFPArithm_Rem:
3525 return irb_.CreateFRem(lhs, rhs);
3526
3527 default:
3528 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3529 return NULL;
3530 }
3531}
3532
3533
Logan Chienc3f7d962011-12-27 18:13:18 +08003534void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3535 llvm::Value* denominator,
3536 JType op_jty) {
3537 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3538
3539 llvm::Constant* zero = irb_.getJZero(op_jty);
3540
3541 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3542
3543 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3544
3545 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3546
TDYa127ac7b5bb2012-05-11 13:17:49 -07003547 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003548
3549 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003550 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003551 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3552 EmitBranchExceptionLandingPad(dex_pc);
3553
3554 irb_.SetInsertPoint(block_continue);
3555}
3556
3557
Logan Chien61bb6142012-02-03 15:34:53 +08003558void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3559 llvm::Value* object) {
3560 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3561
3562 llvm::BasicBlock* block_exception =
3563 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3564
3565 llvm::BasicBlock* block_continue =
3566 CreateBasicBlockWithDexPC(dex_pc, "cont");
3567
TDYa127ac7b5bb2012-05-11 13:17:49 -07003568 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003569
3570 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003571 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003572 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003573 EmitBranchExceptionLandingPad(dex_pc);
3574
3575 irb_.SetInsertPoint(block_continue);
3576}
3577
3578
Logan Chienbb4d12a2012-02-17 14:10:01 +08003579llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3580 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3581
TDYa127ee1f59b2012-04-25 00:56:40 -07003582 return irb_.LoadFromObjectOffset(method_object_addr,
3583 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003584 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003585 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003586}
3587
3588
Logan Chienbb4d12a2012-02-17 14:10:01 +08003589llvm::Value* MethodCompiler::
3590EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3591 llvm::Value* static_storage_dex_cache_addr =
3592 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3593
3594 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3595
TDYa1278db6ea32012-05-17 04:48:42 -07003596 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003597}
3598
3599
3600llvm::Value* MethodCompiler::
3601EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3602 llvm::Value* resolved_type_dex_cache_addr =
3603 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3604
3605 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3606
TDYa1278db6ea32012-05-17 04:48:42 -07003607 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003608}
3609
3610
3611llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003612EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3613 llvm::Value* resolved_method_dex_cache_addr =
3614 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3615
3616 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3617
TDYa1278db6ea32012-05-17 04:48:42 -07003618 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003619}
3620
3621
3622llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003623EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3624 llvm::Value* string_dex_cache_addr =
3625 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3626
3627 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3628
TDYa1278db6ea32012-05-17 04:48:42 -07003629 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003630}
3631
3632
Logan Chien83426162011-12-09 09:29:50 +08003633CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003634 // TODO: Use high-level IR to do this
3635 // Compute method info
3636 ComputeMethodInfo();
3637
Logan Chien0b827102011-12-20 19:46:14 +08003638 // Code generation
3639 CreateFunction();
3640
3641 EmitPrologue();
3642 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003643 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003644
Logan Chiend6c239a2011-12-23 15:11:45 +08003645 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003646 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003647
Logan Chien8b977d32012-02-21 19:14:55 +08003648 // Add the memory usage approximation of the compilation unit
3649 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003650 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003651 // Dex file. Besides, we have to convert the code unit into bytes.
3652 // Thus, we got our magic number 9.
3653
Logan Chien110bcba2012-04-16 19:11:28 +08003654 CompiledMethod* compiled_method =
3655 new CompiledMethod(cunit_->GetInstructionSet(),
3656 cunit_->GetElfIndex(),
3657 elf_func_idx_);
3658
3659 cunit_->RegisterCompiledMethod(func_, compiled_method);
3660
3661 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003662}
3663
3664
3665llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3666 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003667}
Logan Chien83426162011-12-09 09:29:50 +08003668
3669
Logan Chien5bcc04e2012-01-30 14:15:12 +08003670void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3671 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3672 irb_.CreateBr(lpad);
3673 } else {
3674 irb_.CreateBr(GetUnwindBasicBlock());
3675 }
3676}
3677
3678
TDYa127526643e2012-05-26 01:01:48 -07003679void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc, bool can_skip_unwind) {
3680 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
3681 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3682 if (lpad == NULL && can_skip_unwind &&
3683 IsInstructionDirectToReturn(dex_pc + insn->SizeInCodeUnits())) {
3684 return;
3685 }
3686
TDYa127de479be2012-05-31 08:03:26 -07003687 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
Logan Chien5bcc04e2012-01-30 14:15:12 +08003688
3689 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3690
TDYa127526643e2012-05-26 01:01:48 -07003691 if (lpad) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003692 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003693 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003694 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003695 }
3696
3697 irb_.SetInsertPoint(block_cont);
3698}
3699
3700
TDYa127526643e2012-05-26 01:01:48 -07003701void MethodCompiler::EmitGuard_GarbageCollectionSuspend() {
3702 // Loop suspend will be added by our llvm pass.
3703 if (!method_info_.has_invoke) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003704 return;
3705 }
3706
TDYa127de479be2012-05-31 08:03:26 -07003707 irb_.Runtime().EmitTestSuspend();
Logan Chien924072f2012-01-30 15:07:24 +08003708}
3709
3710
Logan Chiend6c239a2011-12-23 15:11:45 +08003711llvm::BasicBlock* MethodCompiler::
3712CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3713 std::string name;
3714
TDYa12767ae8ff2012-05-02 19:08:02 -07003715#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003716 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003717 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003718 } else {
TDYa12799489132012-04-29 01:27:58 -07003719 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003720 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003721#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003722
3723 return llvm::BasicBlock::Create(*context_, name, func_);
3724}
3725
3726
3727llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3728 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3729
3730 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3731
3732 if (!basic_block) {
3733 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3734 basic_blocks_[dex_pc] = basic_block;
3735 }
3736
3737 return basic_block;
3738}
3739
3740
3741llvm::BasicBlock*
3742MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3743 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3744 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3745}
3746
3747
Logan Chien5bcc04e2012-01-30 14:15:12 +08003748int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3749 // TODO: Since we are emitting the dex instructions in ascending order
3750 // w.r.t. address, we can cache the lastest try item offset so that we
3751 // don't have to do binary search for every query.
3752
3753 int32_t min = 0;
3754 int32_t max = code_item_->tries_size_ - 1;
3755
3756 while (min <= max) {
3757 int32_t mid = min + (max - min) / 2;
3758
3759 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3760 uint32_t start = ti->start_addr_;
3761 uint32_t end = start + ti->insn_count_;
3762
3763 if (dex_pc < start) {
3764 max = mid - 1;
3765 } else if (dex_pc >= end) {
3766 min = mid + 1;
3767 } else {
3768 return mid; // found
3769 }
3770 }
3771
3772 return -1; // not found
3773}
3774
3775
3776llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3777 // Find the try item for this address in this method
3778 int32_t ti_offset = GetTryItemOffset(dex_pc);
3779
3780 if (ti_offset == -1) {
3781 return NULL; // No landing pad is available for this address.
3782 }
3783
3784 // Check for the existing landing pad basic block
3785 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3786 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3787
3788 if (block_lpad) {
3789 // We have generated landing pad for this try item already. Return the
3790 // same basic block.
3791 return block_lpad;
3792 }
3793
3794 // Get try item from code item
3795 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3796
TDYa12767ae8ff2012-05-02 19:08:02 -07003797 std::string lpadname;
3798
3799#if !defined(NDEBUG)
3800 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3801#endif
3802
Logan Chien5bcc04e2012-01-30 14:15:12 +08003803 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003804 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003805
3806 // Change IRBuilder insert point
3807 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3808 irb_.SetInsertPoint(block_lpad);
3809
3810 // Find catch block with matching type
3811 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3812
Logan Chien736df022012-04-27 16:25:57 +08003813 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003814
3815 llvm::Value* catch_handler_index_value =
3816 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003817 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003818
3819 // Switch instruction (Go to unwind basic block by default)
3820 llvm::SwitchInst* sw =
3821 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3822
3823 // Cases with matched catch block
3824 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3825
3826 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3827 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3828 }
3829
3830 // Restore the orignal insert point for IRBuilder
3831 irb_.restoreIP(irb_ip_original);
3832
3833 // Cache this landing pad
3834 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3835 basic_block_landing_pads_[ti_offset] = block_lpad;
3836
3837 return block_lpad;
3838}
3839
3840
3841llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3842 // Check the existing unwinding baisc block block
3843 if (basic_block_unwind_ != NULL) {
3844 return basic_block_unwind_;
3845 }
3846
3847 // Create new basic block for unwinding
3848 basic_block_unwind_ =
3849 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3850
3851 // Change IRBuilder insert point
3852 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3853 irb_.SetInsertPoint(basic_block_unwind_);
3854
Logan Chien8dfcbea2012-02-17 18:50:32 +08003855 // Pop the shadow frame
3856 EmitPopShadowFrame();
3857
Logan Chien5bcc04e2012-01-30 14:15:12 +08003858 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003859 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003860 if (ret_shorty == 'V') {
3861 irb_.CreateRetVoid();
3862 } else {
3863 irb_.CreateRet(irb_.getJZero(ret_shorty));
3864 }
3865
3866 // Restore the orignal insert point for IRBuilder
3867 irb_.restoreIP(irb_ip_original);
3868
3869 return basic_block_unwind_;
3870}
3871
3872
TDYa127e2102142012-05-26 10:27:38 -07003873llvm::Value* MethodCompiler::AllocDalvikReg(RegCategory cat, const std::string& name) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003874 // Get reg_type and reg_name from DalvikReg
3875 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3876 std::string reg_name;
3877
3878#if !defined(NDEBUG)
TDYa127e2102142012-05-26 10:27:38 -07003879 StringAppendF(&reg_name, "%c%s", DalvikReg::GetRegCategoryNamePrefix(cat), name.c_str());
TDYa12767ae8ff2012-05-02 19:08:02 -07003880#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003881
3882 // Save current IR builder insert point
3883 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa127b9ff6b12012-05-17 11:14:29 -07003884 irb_.SetInsertPoint(basic_block_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003885
3886 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003887 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003888
3889 // Restore IRBuilder insert point
3890 irb_.restoreIP(irb_ip_original);
3891
3892 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3893 return reg_addr;
3894}
3895
3896
TDYa127e2102142012-05-26 10:27:38 -07003897llvm::Value* MethodCompiler::GetShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003898 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3899 // This register dosen't need ShadowFrame entry
3900 return NULL;
3901 }
3902
TDYa127cc1b4c32012-05-15 07:31:37 -07003903 if (!method_info_.need_shadow_frame_entry) {
3904 return NULL;
3905 }
3906
TDYa12767ae8ff2012-05-02 19:08:02 -07003907 std::string reg_name;
3908
3909#if !defined(NDEBUG)
TDYa127e2102142012-05-26 10:27:38 -07003910 StringAppendF(&reg_name, "s%u", reg_idx);
TDYa12767ae8ff2012-05-02 19:08:02 -07003911#endif
3912
TDYa1277f5b9be2012-04-29 01:31:49 -07003913 // Save current IR builder insert point
3914 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3915
TDYa127b9ff6b12012-05-17 11:14:29 -07003916 irb_.SetInsertPoint(basic_block_shadow_frame_);
TDYa1277f5b9be2012-04-29 01:31:49 -07003917
3918 llvm::Value* gep_index[] = {
3919 irb_.getInt32(0), // No pointer displacement
3920 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003921 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003922 };
3923
TDYa12767ae8ff2012-05-02 19:08:02 -07003924 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003925
3926 // Restore IRBuilder insert point
3927 irb_.restoreIP(irb_ip_original);
3928
3929 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3930 return reg_addr;
3931}
3932
3933
TDYa127af543472012-05-28 21:49:23 -07003934void MethodCompiler::EmitPushShadowFrame(bool is_inline) {
3935 if (!method_info_.need_shadow_frame) {
3936 return;
3937 }
3938 DCHECK(shadow_frame_ != NULL);
3939 DCHECK(old_shadow_frame_ != NULL);
3940
3941 // Get method object
3942 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3943
3944 // Push the shadow frame
3945 llvm::Value* shadow_frame_upcast =
3946 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
3947
TDYa127de479be2012-05-31 08:03:26 -07003948 llvm::Value* result;
3949 if (is_inline) {
3950 result = irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr,
3951 shadow_frame_size_);
3952 } else {
3953 DCHECK(shadow_frame_size_ == 0);
3954 result = irb_.Runtime().EmitPushShadowFrameNoInline(shadow_frame_upcast, method_object_addr,
3955 shadow_frame_size_);
3956 }
TDYa127af543472012-05-28 21:49:23 -07003957 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
3958}
3959
3960
Logan Chien8dfcbea2012-02-17 18:50:32 +08003961void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003962 if (!method_info_.need_shadow_frame) {
3963 return;
3964 }
TDYa1270de52be2012-05-27 20:49:31 -07003965 DCHECK(old_shadow_frame_ != NULL);
TDYa127af543472012-05-28 21:49:23 -07003966
3967 if (method_info_.lazy_push_shadow_frame) {
3968 llvm::BasicBlock* bb_pop = llvm::BasicBlock::Create(*context_, "pop", func_);
3969 llvm::BasicBlock* bb_cont = llvm::BasicBlock::Create(*context_, "cont", func_);
3970
3971 llvm::Value* need_pop = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister);
3972 irb_.CreateCondBr(need_pop, bb_pop, bb_cont, kUnlikely);
3973
3974 irb_.SetInsertPoint(bb_pop);
TDYa127de479be2012-05-31 08:03:26 -07003975 irb_.Runtime().EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
TDYa127af543472012-05-28 21:49:23 -07003976 irb_.CreateBr(bb_cont);
3977
3978 irb_.SetInsertPoint(bb_cont);
3979 } else {
TDYa127de479be2012-05-31 08:03:26 -07003980 irb_.Runtime().EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
TDYa127af543472012-05-28 21:49:23 -07003981 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003982}
3983
3984
TDYa127c8dc1012012-04-19 07:03:33 -07003985void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003986 if (!method_info_.need_shadow_frame) {
3987 return;
3988 }
TDYa127c8dc1012012-04-19 07:03:33 -07003989 irb_.StoreToObjectOffset(shadow_frame_,
3990 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003991 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003992 kTBAAShadowFrame);
TDYa127af543472012-05-28 21:49:23 -07003993 // Lazy pushing shadow frame
3994 if (method_info_.lazy_push_shadow_frame) {
3995 llvm::BasicBlock* bb_push = CreateBasicBlockWithDexPC(dex_pc, "push");
3996 llvm::BasicBlock* bb_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3997
3998 llvm::Value* no_need_push = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister);
3999 irb_.CreateCondBr(no_need_push, bb_cont, bb_push, kLikely);
4000
4001 irb_.SetInsertPoint(bb_push);
4002 EmitPushShadowFrame(false);
4003 irb_.CreateStore(irb_.getTrue(), already_pushed_shadow_frame_, kTBAARegister);
4004 irb_.CreateBr(bb_cont);
4005
4006 irb_.SetInsertPoint(bb_cont);
4007 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08004008}
4009
4010
TDYa127e2102142012-05-26 10:27:38 -07004011llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
4012 JTypeSpace space) {
4013 return regs_[reg_idx]->GetValue(jty, space);
4014}
4015
4016llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
4017 JTypeSpace space) {
4018 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
4019}
4020
4021void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
4022 JTypeSpace space, llvm::Value* new_value) {
4023 regs_[reg_idx]->SetValue(jty, space, new_value);
4024 if (jty == kObject && shadow_frame_entries_[reg_idx] != NULL) {
4025 irb_.CreateStore(new_value, shadow_frame_entries_[reg_idx], kTBAAShadowFrame);
4026 }
4027}
4028
4029void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
4030 JTypeSpace space, llvm::Value* new_value) {
4031 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
4032}
4033
4034llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
4035 return retval_reg_->GetValue(jty, space);
4036}
4037
4038llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
4039 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
4040}
4041
4042void MethodCompiler::EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
4043 llvm::Value* new_value) {
4044 retval_reg_->SetValue(jty, space, new_value);
4045}
4046
4047void MethodCompiler::EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
4048 llvm::Value* new_value) {
4049 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
4050}
4051
4052
TDYa127cc1b4c32012-05-15 07:31:37 -07004053// TODO: Use high-level IR to do this
TDYa12729c0cd12012-05-17 04:51:08 -07004054bool MethodCompiler::EmitInlineJavaIntrinsic(const std::string& callee_method_name,
4055 const std::vector<llvm::Value*>& args,
4056 llvm::BasicBlock* after_invoke) {
4057 if (callee_method_name == "char java.lang.String.charAt(int)") {
4058 return EmitInlinedStringCharAt(args, after_invoke);
4059 }
4060 if (callee_method_name == "int java.lang.String.length()") {
4061 return EmitInlinedStringLength(args, after_invoke);
4062 }
4063 return true;
4064}
4065
4066bool MethodCompiler::EmitInlinedStringCharAt(const std::vector<llvm::Value*>& args,
4067 llvm::BasicBlock* after_invoke) {
4068 DCHECK_EQ(args.size(), 3U) <<
4069 "char java.lang.String.charAt(int) has 3 args: method, this, char_index";
4070 llvm::Value* this_object = args[1];
4071 llvm::Value* char_index = args[2];
4072 llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "CharAtRetry", func_);
4073 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "CharAtCont", func_);
4074
4075 // TODO: Can we safely say the String.count is ConstJObject(constant memory)? (there are so many
4076 // iput to String.count in the String.<init>(...))
4077 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
4078 String::CountOffset().Int32Value(),
4079 irb_.getJIntTy(),
4080 kTBAAHeapInstance, kInt);
4081 // Two's complement, so we can use only one "less than" to check "in bounds"
4082 llvm::Value* in_bounds = irb_.CreateICmpULT(char_index, string_count);
4083 irb_.CreateCondBr(in_bounds, block_cont, block_retry, kLikely);
4084
4085 irb_.SetInsertPoint(block_cont);
4086 // TODO: Can we safely say the String.offset is ConstJObject(constant memory)?
4087 llvm::Value* string_offset = irb_.LoadFromObjectOffset(this_object,
4088 String::OffsetOffset().Int32Value(),
4089 irb_.getJIntTy(),
4090 kTBAAHeapInstance, kInt);
4091 llvm::Value* string_value = irb_.LoadFromObjectOffset(this_object,
4092 String::ValueOffset().Int32Value(),
4093 irb_.getJObjectTy(),
4094 kTBAAHeapInstance, kObject);
4095
4096 // index_value = string.offset + char_index
4097 llvm::Value* index_value = irb_.CreateAdd(string_offset, char_index);
4098
4099 // array_elem_value = string.value[index_value]
4100 llvm::Value* array_elem_addr = EmitArrayGEP(string_value, index_value, kChar);
4101 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, kChar);
4102
4103 EmitStoreDalvikRetValReg(kChar, kArray, array_elem_value);
4104 irb_.CreateBr(after_invoke);
4105
4106 irb_.SetInsertPoint(block_retry);
4107 return true;
4108}
4109
4110bool MethodCompiler::EmitInlinedStringLength(const std::vector<llvm::Value*>& args,
4111 llvm::BasicBlock* after_invoke) {
4112 DCHECK_EQ(args.size(), 2U) <<
4113 "int java.lang.String.length() has 2 args: method, this";
4114 llvm::Value* this_object = args[1];
4115 // TODO: Can we safely say the String.count is ConstJObject(constant memory)?
4116 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
4117 String::CountOffset().Int32Value(),
4118 irb_.getJIntTy(),
4119 kTBAAHeapInstance, kInt);
4120 EmitStoreDalvikRetValReg(kInt, kAccurate, string_count);
4121 irb_.CreateBr(after_invoke);
4122 return false;
4123}
4124
4125
TDYa127526643e2012-05-26 01:01:48 -07004126bool MethodCompiler::IsInstructionDirectToReturn(uint32_t dex_pc) {
4127 for (int i = 0; i < 8; ++i) { // Trace at most 8 instructions.
4128 if (dex_pc >= code_item_->insns_size_in_code_units_) {
4129 return false;
4130 }
4131
4132 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
4133
4134 if (insn->IsReturn()) {
4135 return true;
4136 }
4137
4138 // Is throw, switch, invoke or conditional branch.
4139 if (insn->IsThrow() || insn->IsSwitch() || insn->IsInvoke() ||
4140 (insn->IsBranch() && !insn->IsUnconditional())) {
4141 return false;
4142 }
4143
4144 switch (insn->Opcode()) {
4145 default:
4146 dex_pc += insn->SizeInCodeUnits();
4147 break;
4148
4149 // This instruction will remove the exception. Consider as a side effect.
4150 case Instruction::MOVE_EXCEPTION:
4151 return false;
4152 break;
4153
4154 case Instruction::GOTO:
4155 case Instruction::GOTO_16:
4156 case Instruction::GOTO_32:
4157 {
4158 DecodedInstruction dec_insn(insn);
4159 int32_t branch_offset = dec_insn.vA;
4160 dex_pc += branch_offset;
4161 }
4162 break;
4163 }
4164 }
4165 return false;
4166}
4167
4168
TDYa12729c0cd12012-05-17 04:51:08 -07004169// TODO: Use high-level IR to do this
TDYa127cc1b4c32012-05-15 07:31:37 -07004170void MethodCompiler::ComputeMethodInfo() {
4171 // If this method is static, we set the "this" register index to -1. So we don't worry about this
4172 // method is static or not in the following comparison.
4173 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
4174 (-1) :
4175 (code_item_->registers_size_ - code_item_->ins_size_);
4176 bool has_invoke = false;
4177 bool may_have_loop = false;
4178 bool modify_this = false;
4179 bool may_throw_exception = false;
4180
4181 Instruction const* insn;
4182 for (uint32_t dex_pc = 0;
4183 dex_pc < code_item_->insns_size_in_code_units_;
4184 dex_pc += insn->SizeInCodeUnits()) {
4185 insn = Instruction::At(code_item_->insns_ + dex_pc);
4186 DecodedInstruction dec_insn(insn);
4187
4188 switch (insn->Opcode()) {
4189 case Instruction::NOP:
4190 break;
4191
4192 case Instruction::MOVE:
4193 case Instruction::MOVE_FROM16:
4194 case Instruction::MOVE_16:
4195 case Instruction::MOVE_WIDE:
4196 case Instruction::MOVE_WIDE_FROM16:
4197 case Instruction::MOVE_WIDE_16:
4198 case Instruction::MOVE_OBJECT:
4199 case Instruction::MOVE_OBJECT_FROM16:
4200 case Instruction::MOVE_OBJECT_16:
4201 case Instruction::MOVE_RESULT:
4202 case Instruction::MOVE_RESULT_WIDE:
4203 case Instruction::MOVE_RESULT_OBJECT:
4204 case Instruction::MOVE_EXCEPTION:
4205 if (dec_insn.vA == this_reg_idx) {
4206 modify_this = true;
4207 }
4208 break;
4209
4210 case Instruction::RETURN_VOID:
4211 case Instruction::RETURN:
4212 case Instruction::RETURN_WIDE:
4213 case Instruction::RETURN_OBJECT:
4214 break;
4215
4216 case Instruction::CONST_4:
4217 case Instruction::CONST_16:
4218 case Instruction::CONST:
4219 case Instruction::CONST_HIGH16:
4220 case Instruction::CONST_WIDE_16:
4221 case Instruction::CONST_WIDE_32:
4222 case Instruction::CONST_WIDE:
4223 case Instruction::CONST_WIDE_HIGH16:
4224 if (dec_insn.vA == this_reg_idx) {
4225 modify_this = true;
4226 }
4227 break;
4228
4229 case Instruction::CONST_STRING:
4230 case Instruction::CONST_STRING_JUMBO:
4231 // TODO: Will the ResolveString throw exception?
4232 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4233 may_throw_exception = true;
4234 }
4235 if (dec_insn.vA == this_reg_idx) {
4236 modify_this = true;
4237 }
4238 break;
4239
4240 case Instruction::CONST_CLASS:
4241 may_throw_exception = true;
4242 if (dec_insn.vA == this_reg_idx) {
4243 modify_this = true;
4244 }
4245 break;
4246
4247 case Instruction::MONITOR_ENTER:
4248 case Instruction::MONITOR_EXIT:
4249 case Instruction::CHECK_CAST:
4250 may_throw_exception = true;
4251 break;
4252
4253 case Instruction::INSTANCE_OF:
TDYa127cc1b4c32012-05-15 07:31:37 -07004254 case Instruction::ARRAY_LENGTH:
TDYa127cc1b4c32012-05-15 07:31:37 -07004255 case Instruction::NEW_INSTANCE:
4256 case Instruction::NEW_ARRAY:
4257 may_throw_exception = true;
4258 if (dec_insn.vA == this_reg_idx) {
4259 modify_this = true;
4260 }
4261 break;
4262
4263 case Instruction::FILLED_NEW_ARRAY:
4264 case Instruction::FILLED_NEW_ARRAY_RANGE:
4265 case Instruction::FILL_ARRAY_DATA:
4266 case Instruction::THROW:
4267 may_throw_exception = true;
4268 break;
4269
4270 case Instruction::GOTO:
4271 case Instruction::GOTO_16:
4272 case Instruction::GOTO_32:
4273 {
4274 int32_t branch_offset = dec_insn.vA;
TDYa127526643e2012-05-26 01:01:48 -07004275 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4276 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004277 }
4278 }
4279 break;
4280
4281 case Instruction::PACKED_SWITCH:
4282 case Instruction::SPARSE_SWITCH:
4283 break;
4284
4285 case Instruction::CMPL_FLOAT:
4286 case Instruction::CMPG_FLOAT:
4287 case Instruction::CMPL_DOUBLE:
4288 case Instruction::CMPG_DOUBLE:
4289 case Instruction::CMP_LONG:
4290 if (dec_insn.vA == this_reg_idx) {
4291 modify_this = true;
4292 }
4293 break;
4294
4295 case Instruction::IF_EQ:
4296 case Instruction::IF_NE:
4297 case Instruction::IF_LT:
4298 case Instruction::IF_GE:
4299 case Instruction::IF_GT:
4300 case Instruction::IF_LE:
4301 {
4302 int32_t branch_offset = dec_insn.vC;
TDYa127526643e2012-05-26 01:01:48 -07004303 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4304 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004305 }
4306 }
4307 break;
4308
4309 case Instruction::IF_EQZ:
4310 case Instruction::IF_NEZ:
4311 case Instruction::IF_LTZ:
4312 case Instruction::IF_GEZ:
4313 case Instruction::IF_GTZ:
4314 case Instruction::IF_LEZ:
4315 {
4316 int32_t branch_offset = dec_insn.vB;
TDYa127526643e2012-05-26 01:01:48 -07004317 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4318 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004319 }
4320 }
4321 break;
4322
4323 case Instruction::AGET:
4324 case Instruction::AGET_WIDE:
4325 case Instruction::AGET_OBJECT:
4326 case Instruction::AGET_BOOLEAN:
4327 case Instruction::AGET_BYTE:
4328 case Instruction::AGET_CHAR:
4329 case Instruction::AGET_SHORT:
4330 may_throw_exception = true;
4331 if (dec_insn.vA == this_reg_idx) {
4332 modify_this = true;
4333 }
4334 break;
4335
4336 case Instruction::APUT:
4337 case Instruction::APUT_WIDE:
4338 case Instruction::APUT_OBJECT:
4339 case Instruction::APUT_BOOLEAN:
4340 case Instruction::APUT_BYTE:
4341 case Instruction::APUT_CHAR:
4342 case Instruction::APUT_SHORT:
4343 may_throw_exception = true;
4344 break;
4345
4346 case Instruction::IGET:
4347 case Instruction::IGET_WIDE:
4348 case Instruction::IGET_OBJECT:
4349 case Instruction::IGET_BOOLEAN:
4350 case Instruction::IGET_BYTE:
4351 case Instruction::IGET_CHAR:
4352 case Instruction::IGET_SHORT:
4353 {
4354 if (dec_insn.vA == this_reg_idx) {
4355 modify_this = true;
4356 }
4357 uint32_t reg_idx = dec_insn.vB;
4358 uint32_t field_idx = dec_insn.vC;
4359 int field_offset;
4360 bool is_volatile;
4361 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4362 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4363 if (!is_fast_path) {
4364 may_throw_exception = true;
4365 } else if (reg_idx != this_reg_idx) {
4366 // NullPointerException
4367 may_throw_exception = true;
4368 }
4369 }
4370 break;
4371
4372 case Instruction::IPUT:
4373 case Instruction::IPUT_WIDE:
4374 case Instruction::IPUT_OBJECT:
4375 case Instruction::IPUT_BOOLEAN:
4376 case Instruction::IPUT_BYTE:
4377 case Instruction::IPUT_CHAR:
4378 case Instruction::IPUT_SHORT:
4379 {
4380 uint32_t reg_idx = dec_insn.vB;
4381 uint32_t field_idx = dec_insn.vC;
4382 int field_offset;
4383 bool is_volatile;
4384 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4385 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4386 if (!is_fast_path) {
4387 may_throw_exception = true;
4388 } else if (reg_idx != this_reg_idx) {
4389 // NullPointerException
4390 may_throw_exception = true;
4391 }
4392 }
4393 break;
4394
4395 case Instruction::SGET:
4396 case Instruction::SGET_WIDE:
4397 case Instruction::SGET_OBJECT:
4398 case Instruction::SGET_BOOLEAN:
4399 case Instruction::SGET_BYTE:
4400 case Instruction::SGET_CHAR:
4401 case Instruction::SGET_SHORT:
4402 {
4403 if (dec_insn.vA == this_reg_idx) {
4404 modify_this = true;
4405 }
4406 uint32_t field_idx = dec_insn.vB;
4407
4408 int field_offset;
4409 int ssb_index;
4410 bool is_referrers_class;
4411 bool is_volatile;
4412
4413 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4414 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4415 is_referrers_class, is_volatile, false);
4416 if (!is_fast_path || !is_referrers_class) {
4417 may_throw_exception = true;
4418 }
4419 }
4420 break;
4421
4422 case Instruction::SPUT:
4423 case Instruction::SPUT_WIDE:
4424 case Instruction::SPUT_OBJECT:
4425 case Instruction::SPUT_BOOLEAN:
4426 case Instruction::SPUT_BYTE:
4427 case Instruction::SPUT_CHAR:
4428 case Instruction::SPUT_SHORT:
4429 {
4430 uint32_t field_idx = dec_insn.vB;
4431
4432 int field_offset;
4433 int ssb_index;
4434 bool is_referrers_class;
4435 bool is_volatile;
4436
4437 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4438 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4439 is_referrers_class, is_volatile, true);
4440 if (!is_fast_path || !is_referrers_class) {
4441 may_throw_exception = true;
4442 }
4443 }
4444 break;
4445
4446
4447 case Instruction::INVOKE_VIRTUAL:
4448 case Instruction::INVOKE_SUPER:
4449 case Instruction::INVOKE_DIRECT:
4450 case Instruction::INVOKE_STATIC:
4451 case Instruction::INVOKE_INTERFACE:
4452 case Instruction::INVOKE_VIRTUAL_RANGE:
4453 case Instruction::INVOKE_SUPER_RANGE:
4454 case Instruction::INVOKE_DIRECT_RANGE:
4455 case Instruction::INVOKE_STATIC_RANGE:
4456 case Instruction::INVOKE_INTERFACE_RANGE:
4457 has_invoke = true;
4458 may_throw_exception = true;
4459 break;
4460
4461 case Instruction::NEG_INT:
4462 case Instruction::NOT_INT:
4463 case Instruction::NEG_LONG:
4464 case Instruction::NOT_LONG:
4465 case Instruction::NEG_FLOAT:
4466 case Instruction::NEG_DOUBLE:
4467 case Instruction::INT_TO_LONG:
4468 case Instruction::INT_TO_FLOAT:
4469 case Instruction::INT_TO_DOUBLE:
4470 case Instruction::LONG_TO_INT:
4471 case Instruction::LONG_TO_FLOAT:
4472 case Instruction::LONG_TO_DOUBLE:
4473 case Instruction::FLOAT_TO_INT:
4474 case Instruction::FLOAT_TO_LONG:
4475 case Instruction::FLOAT_TO_DOUBLE:
4476 case Instruction::DOUBLE_TO_INT:
4477 case Instruction::DOUBLE_TO_LONG:
4478 case Instruction::DOUBLE_TO_FLOAT:
4479 case Instruction::INT_TO_BYTE:
4480 case Instruction::INT_TO_CHAR:
4481 case Instruction::INT_TO_SHORT:
4482 case Instruction::ADD_INT:
4483 case Instruction::SUB_INT:
4484 case Instruction::MUL_INT:
4485 case Instruction::AND_INT:
4486 case Instruction::OR_INT:
4487 case Instruction::XOR_INT:
4488 case Instruction::SHL_INT:
4489 case Instruction::SHR_INT:
4490 case Instruction::USHR_INT:
4491 case Instruction::ADD_LONG:
4492 case Instruction::SUB_LONG:
4493 case Instruction::MUL_LONG:
4494 case Instruction::AND_LONG:
4495 case Instruction::OR_LONG:
4496 case Instruction::XOR_LONG:
4497 case Instruction::SHL_LONG:
4498 case Instruction::SHR_LONG:
4499 case Instruction::USHR_LONG:
4500 case Instruction::ADD_INT_2ADDR:
4501 case Instruction::SUB_INT_2ADDR:
4502 case Instruction::MUL_INT_2ADDR:
4503 case Instruction::AND_INT_2ADDR:
4504 case Instruction::OR_INT_2ADDR:
4505 case Instruction::XOR_INT_2ADDR:
4506 case Instruction::SHL_INT_2ADDR:
4507 case Instruction::SHR_INT_2ADDR:
4508 case Instruction::USHR_INT_2ADDR:
4509 case Instruction::ADD_LONG_2ADDR:
4510 case Instruction::SUB_LONG_2ADDR:
4511 case Instruction::MUL_LONG_2ADDR:
4512 case Instruction::AND_LONG_2ADDR:
4513 case Instruction::OR_LONG_2ADDR:
4514 case Instruction::XOR_LONG_2ADDR:
4515 case Instruction::SHL_LONG_2ADDR:
4516 case Instruction::SHR_LONG_2ADDR:
4517 case Instruction::USHR_LONG_2ADDR:
4518 if (dec_insn.vA == this_reg_idx) {
4519 modify_this = true;
4520 }
4521 break;
4522
4523 case Instruction::DIV_INT:
4524 case Instruction::REM_INT:
4525 case Instruction::DIV_LONG:
4526 case Instruction::REM_LONG:
4527 case Instruction::DIV_INT_2ADDR:
4528 case Instruction::REM_INT_2ADDR:
4529 case Instruction::DIV_LONG_2ADDR:
4530 case Instruction::REM_LONG_2ADDR:
4531 may_throw_exception = true;
4532 if (dec_insn.vA == this_reg_idx) {
4533 modify_this = true;
4534 }
4535 break;
4536
4537 case Instruction::ADD_FLOAT:
4538 case Instruction::SUB_FLOAT:
4539 case Instruction::MUL_FLOAT:
4540 case Instruction::DIV_FLOAT:
4541 case Instruction::REM_FLOAT:
4542 case Instruction::ADD_DOUBLE:
4543 case Instruction::SUB_DOUBLE:
4544 case Instruction::MUL_DOUBLE:
4545 case Instruction::DIV_DOUBLE:
4546 case Instruction::REM_DOUBLE:
4547 case Instruction::ADD_FLOAT_2ADDR:
4548 case Instruction::SUB_FLOAT_2ADDR:
4549 case Instruction::MUL_FLOAT_2ADDR:
4550 case Instruction::DIV_FLOAT_2ADDR:
4551 case Instruction::REM_FLOAT_2ADDR:
4552 case Instruction::ADD_DOUBLE_2ADDR:
4553 case Instruction::SUB_DOUBLE_2ADDR:
4554 case Instruction::MUL_DOUBLE_2ADDR:
4555 case Instruction::DIV_DOUBLE_2ADDR:
4556 case Instruction::REM_DOUBLE_2ADDR:
4557 if (dec_insn.vA == this_reg_idx) {
4558 modify_this = true;
4559 }
4560 break;
4561
4562 case Instruction::ADD_INT_LIT16:
4563 case Instruction::ADD_INT_LIT8:
4564 case Instruction::RSUB_INT:
4565 case Instruction::RSUB_INT_LIT8:
4566 case Instruction::MUL_INT_LIT16:
4567 case Instruction::MUL_INT_LIT8:
4568 case Instruction::AND_INT_LIT16:
4569 case Instruction::AND_INT_LIT8:
4570 case Instruction::OR_INT_LIT16:
4571 case Instruction::OR_INT_LIT8:
4572 case Instruction::XOR_INT_LIT16:
4573 case Instruction::XOR_INT_LIT8:
4574 case Instruction::SHL_INT_LIT8:
4575 case Instruction::SHR_INT_LIT8:
4576 case Instruction::USHR_INT_LIT8:
4577 if (dec_insn.vA == this_reg_idx) {
4578 modify_this = true;
4579 }
4580 break;
4581 case Instruction::DIV_INT_LIT16:
4582 case Instruction::DIV_INT_LIT8:
4583 case Instruction::REM_INT_LIT16:
4584 case Instruction::REM_INT_LIT8:
4585 if (dec_insn.vC == 0) {
4586 may_throw_exception = true;
4587 }
4588 if (dec_insn.vA == this_reg_idx) {
4589 modify_this = true;
4590 }
4591 break;
4592
4593 case Instruction::THROW_VERIFICATION_ERROR:
4594 may_throw_exception = true;
4595 break;
4596
4597 case Instruction::UNUSED_3E:
4598 case Instruction::UNUSED_3F:
4599 case Instruction::UNUSED_40:
4600 case Instruction::UNUSED_41:
4601 case Instruction::UNUSED_42:
4602 case Instruction::UNUSED_43:
4603 case Instruction::UNUSED_73:
4604 case Instruction::UNUSED_79:
4605 case Instruction::UNUSED_7A:
4606 case Instruction::UNUSED_E3:
4607 case Instruction::UNUSED_E4:
4608 case Instruction::UNUSED_E5:
4609 case Instruction::UNUSED_E6:
4610 case Instruction::UNUSED_E7:
4611 case Instruction::UNUSED_E8:
4612 case Instruction::UNUSED_E9:
4613 case Instruction::UNUSED_EA:
4614 case Instruction::UNUSED_EB:
4615 case Instruction::UNUSED_EC:
4616 case Instruction::UNUSED_EE:
4617 case Instruction::UNUSED_EF:
4618 case Instruction::UNUSED_F0:
4619 case Instruction::UNUSED_F1:
4620 case Instruction::UNUSED_F2:
4621 case Instruction::UNUSED_F3:
4622 case Instruction::UNUSED_F4:
4623 case Instruction::UNUSED_F5:
4624 case Instruction::UNUSED_F6:
4625 case Instruction::UNUSED_F7:
4626 case Instruction::UNUSED_F8:
4627 case Instruction::UNUSED_F9:
4628 case Instruction::UNUSED_FA:
4629 case Instruction::UNUSED_FB:
4630 case Instruction::UNUSED_FC:
4631 case Instruction::UNUSED_FD:
4632 case Instruction::UNUSED_FE:
4633 case Instruction::UNUSED_FF:
4634 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4635 break;
4636 }
4637 }
4638
4639 method_info_.this_reg_idx = this_reg_idx;
4640 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4641 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4642 // should remove this trick.
4643 method_info_.this_will_not_be_null = !modify_this;
4644 method_info_.has_invoke = has_invoke;
4645 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4646 // for GC.
4647 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4648 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4649 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
TDYa127af543472012-05-28 21:49:23 -07004650 // If can only throw exception, but can't suspend check (no loop, no invoke),
4651 // then there is no shadow frame entry. Only Shadow frame is needed.
4652 method_info_.lazy_push_shadow_frame =
4653 method_info_.need_shadow_frame && !method_info_.need_shadow_frame_entry;
TDYa127cc1b4c32012-05-15 07:31:37 -07004654}
4655
4656
4657
Logan Chien83426162011-12-09 09:29:50 +08004658} // namespace compiler_llvm
4659} // namespace art