blob: 4021f7bc8f27cc4f8463a37a036f032cd0080cf5 [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),
TDYa127eead4ac2012-06-03 07:15:25 -070075 shadow_frame_(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_);
TDYa127b9ff6b12012-05-17 11:14:29 -0700171
Logan Chien8dfcbea2012-02-17 18:50:32 +0800172 // Create Shadow Frame
TDYa127cc1b4c32012-05-15 07:31:37 -0700173 if (method_info_.need_shadow_frame) {
174 EmitPrologueAllocShadowFrame();
175 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800176
TDYa127e2102142012-05-26 10:27:38 -0700177 // Create register array
178 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
179 std::string name;
180#if !defined(NDEBUG)
181 name = StringPrintf("%u", r);
182#endif
183 regs_[r] = new DalvikReg(*this, name);
184
185 // Cache shadow frame entry address
186 shadow_frame_entries_[r] = GetShadowFrameEntry(r);
187 }
188
189 std::string name;
190#if !defined(NDEBUG)
191 name = "_res";
192#endif
193 retval_reg_.reset(new DalvikReg(*this, name));
194
Logan Chiend6ececa2011-12-27 16:20:15 +0800195 // Store argument to dalvik register
196 irb_.SetInsertPoint(basic_block_reg_arg_init_);
197 EmitPrologueAssignArgRegister();
198
199 // Branch to start address
200 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800201}
202
203
TDYa1274165a832012-04-03 17:47:16 -0700204void MethodCompiler::EmitStackOverflowCheck() {
TDYa1274165a832012-04-03 17:47:16 -0700205 // Call llvm intrinsic function to get frame address.
206 llvm::Function* frameaddress =
207 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
208
209 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
210 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
211
212 // Cast i8* to int
213 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
214
215 // Get thread.stack_end_
TDYa127ee1f59b2012-04-25 00:56:40 -0700216 llvm::Value* stack_end =
TDYa127de479be2012-05-31 08:03:26 -0700217 irb_.Runtime().EmitLoadFromThreadOffset(Thread::StackEndOffset().Int32Value(),
218 irb_.getPtrEquivIntTy(),
219 kTBAARuntimeInfo);
TDYa1274165a832012-04-03 17:47:16 -0700220
221 // Check the frame address < thread.stack_end_ ?
222 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
223
224 llvm::BasicBlock* block_exception =
225 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
226
227 llvm::BasicBlock* block_continue =
228 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
229
TDYa127ac7b5bb2012-05-11 13:17:49 -0700230 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
TDYa1274165a832012-04-03 17:47:16 -0700231
232 // If stack overflow, throw exception.
233 irb_.SetInsertPoint(block_exception);
234 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
235
236 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800237 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700238 if (ret_shorty == 'V') {
239 irb_.CreateRetVoid();
240 } else {
241 irb_.CreateRet(irb_.getJZero(ret_shorty));
242 }
243
TDYa127526643e2012-05-26 01:01:48 -0700244 irb_.SetInsertPoint(block_continue);
TDYa1274165a832012-04-03 17:47:16 -0700245}
246
247
Logan Chienc670a8d2011-12-20 21:25:56 +0800248void MethodCompiler::EmitPrologueLastBranch() {
TDYa127526643e2012-05-26 01:01:48 -0700249 llvm::BasicBlock* basic_block_stack_overflow =
250 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
TDYa12797339c42012-04-29 01:26:35 -0700251
TDYa127526643e2012-05-26 01:01:48 -0700252 irb_.SetInsertPoint(basic_block_alloca_);
253 irb_.CreateBr(basic_block_stack_overflow);
254
255 irb_.SetInsertPoint(basic_block_stack_overflow);
TDYa127cc1b4c32012-05-15 07:31:37 -0700256 // If a method will not call to other method, and the method is small, we can avoid stack overflow
257 // check.
258 if (method_info_.has_invoke ||
259 code_item_->registers_size_ > 32) { // Small leaf function is OK given
260 // the 8KB reserved at Stack End
261 EmitStackOverflowCheck();
262 }
TDYa127526643e2012-05-26 01:01:48 -0700263 // Garbage collection safe-point
264 EmitGuard_GarbageCollectionSuspend();
TDYa127b9ff6b12012-05-17 11:14:29 -0700265 irb_.CreateBr(basic_block_shadow_frame_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800266
TDYa127b9ff6b12012-05-17 11:14:29 -0700267 irb_.SetInsertPoint(basic_block_shadow_frame_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800268 irb_.CreateBr(basic_block_reg_arg_init_);
269}
270
271
Logan Chien8dfcbea2012-02-17 18:50:32 +0800272void MethodCompiler::EmitPrologueAllocShadowFrame() {
TDYa127b9ff6b12012-05-17 11:14:29 -0700273 irb_.SetInsertPoint(basic_block_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800274
275 // Allocate the shadow frame now!
TDYa127af543472012-05-28 21:49:23 -0700276 shadow_frame_size_ = 0;
TDYa127f1652862012-05-16 21:44:35 -0700277 if (method_info_.need_shadow_frame_entry) {
278 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
279 if (IsRegCanBeObject(i)) {
TDYa127af543472012-05-28 21:49:23 -0700280 reg_to_shadow_frame_index_[i] = shadow_frame_size_++;
TDYa127f1652862012-05-16 21:44:35 -0700281 }
TDYa1271d7e5102012-05-13 09:27:05 -0700282 }
283 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800284
TDYa127af543472012-05-28 21:49:23 -0700285 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(shadow_frame_size_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800286 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
287
TDYa127af543472012-05-28 21:49:23 -0700288 // Alloca a pointer to old shadow frame
289 old_shadow_frame_ = irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo());
290
TDYa127b9ff6b12012-05-17 11:14:29 -0700291 irb_.SetInsertPoint(basic_block_shadow_frame_);
292
TDYa1276e474f82012-05-17 21:23:57 -0700293 // Zero-initialization of the shadow frame table
294 llvm::Value* shadow_frame_table = irb_.CreateConstGEP2_32(shadow_frame_, 0, 1);
295 llvm::Type* table_type = shadow_frame_type->getElementType(1);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800296
TDYa1276e474f82012-05-17 21:23:57 -0700297 llvm::ConstantAggregateZero* zero_initializer =
298 llvm::ConstantAggregateZero::get(table_type);
299
300 irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800301
TDYa127af543472012-05-28 21:49:23 -0700302 // Lazy pushing shadow frame
303 if (method_info_.lazy_push_shadow_frame) {
304 irb_.SetInsertPoint(basic_block_alloca_);
305 already_pushed_shadow_frame_ = irb_.CreateAlloca(irb_.getInt1Ty());
306 irb_.SetInsertPoint(basic_block_shadow_frame_);
307 irb_.CreateStore(irb_.getFalse(), already_pushed_shadow_frame_, kTBAARegister);
308 return;
309 }
TDYa127ee1f59b2012-04-25 00:56:40 -0700310
TDYa127af543472012-05-28 21:49:23 -0700311 EmitPushShadowFrame(true);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800312}
313
314
Logan Chiend6ececa2011-12-27 16:20:15 +0800315void MethodCompiler::EmitPrologueAssignArgRegister() {
316 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
317
318 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
319 llvm::Function::arg_iterator arg_end(func_->arg_end());
320
Logan Chiendd361c92012-04-10 23:40:37 +0800321 uint32_t shorty_size = 0;
322 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
323 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800324
325 ++arg_iter; // skip method object
326
Logan Chiendd361c92012-04-10 23:40:37 +0800327 if (!oat_compilation_unit_->IsStatic()) {
TDYa127e2102142012-05-26 10:27:38 -0700328 regs_[arg_reg]->SetValue(kObject, kAccurate, arg_iter);
Logan Chiend6ececa2011-12-27 16:20:15 +0800329 ++arg_iter;
330 ++arg_reg;
331 }
332
Logan Chiendd361c92012-04-10 23:40:37 +0800333 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
TDYa127e2102142012-05-26 10:27:38 -0700334 regs_[arg_reg]->SetValue(shorty[i], kAccurate, arg_iter);
Logan Chiend6ececa2011-12-27 16:20:15 +0800335
336 ++arg_reg;
337 if (shorty[i] == 'J' || shorty[i] == 'D') {
338 // Wide types, such as long and double, are using a pair of registers
339 // to store the value, so we have to increase arg_reg again.
340 ++arg_reg;
341 }
342 }
343
344 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800345}
346
347
Logan Chien83426162011-12-09 09:29:50 +0800348void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800349 uint32_t dex_pc = 0;
350 while (dex_pc < code_item_->insns_size_in_code_units_) {
351 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
352 EmitInstruction(dex_pc, insn);
353 dex_pc += insn->SizeInCodeUnits();
354 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800355}
356
357
Logan Chien83426162011-12-09 09:29:50 +0800358void MethodCompiler::EmitInstruction(uint32_t dex_pc,
359 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800360
361 // Set the IRBuilder insertion point
362 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
363
Logan Chien70f94b42011-12-27 17:49:11 +0800364#define ARGS dex_pc, insn
365
366 // Dispatch the instruction
367 switch (insn->Opcode()) {
368 case Instruction::NOP:
369 EmitInsn_Nop(ARGS);
370 break;
371
372 case Instruction::MOVE:
373 case Instruction::MOVE_FROM16:
374 case Instruction::MOVE_16:
375 EmitInsn_Move(ARGS, kInt);
376 break;
377
378 case Instruction::MOVE_WIDE:
379 case Instruction::MOVE_WIDE_FROM16:
380 case Instruction::MOVE_WIDE_16:
381 EmitInsn_Move(ARGS, kLong);
382 break;
383
384 case Instruction::MOVE_OBJECT:
385 case Instruction::MOVE_OBJECT_FROM16:
386 case Instruction::MOVE_OBJECT_16:
387 EmitInsn_Move(ARGS, kObject);
388 break;
389
390 case Instruction::MOVE_RESULT:
391 EmitInsn_MoveResult(ARGS, kInt);
392 break;
393
394 case Instruction::MOVE_RESULT_WIDE:
395 EmitInsn_MoveResult(ARGS, kLong);
396 break;
397
398 case Instruction::MOVE_RESULT_OBJECT:
399 EmitInsn_MoveResult(ARGS, kObject);
400 break;
401
402 case Instruction::MOVE_EXCEPTION:
403 EmitInsn_MoveException(ARGS);
404 break;
405
406 case Instruction::RETURN_VOID:
407 EmitInsn_ReturnVoid(ARGS);
408 break;
409
410 case Instruction::RETURN:
411 case Instruction::RETURN_WIDE:
412 case Instruction::RETURN_OBJECT:
413 EmitInsn_Return(ARGS);
414 break;
415
416 case Instruction::CONST_4:
417 case Instruction::CONST_16:
418 case Instruction::CONST:
419 case Instruction::CONST_HIGH16:
420 EmitInsn_LoadConstant(ARGS, kInt);
421 break;
422
423 case Instruction::CONST_WIDE_16:
424 case Instruction::CONST_WIDE_32:
425 case Instruction::CONST_WIDE:
426 case Instruction::CONST_WIDE_HIGH16:
427 EmitInsn_LoadConstant(ARGS, kLong);
428 break;
429
430 case Instruction::CONST_STRING:
431 case Instruction::CONST_STRING_JUMBO:
432 EmitInsn_LoadConstantString(ARGS);
433 break;
434
435 case Instruction::CONST_CLASS:
436 EmitInsn_LoadConstantClass(ARGS);
437 break;
438
439 case Instruction::MONITOR_ENTER:
440 EmitInsn_MonitorEnter(ARGS);
441 break;
442
443 case Instruction::MONITOR_EXIT:
444 EmitInsn_MonitorExit(ARGS);
445 break;
446
447 case Instruction::CHECK_CAST:
448 EmitInsn_CheckCast(ARGS);
449 break;
450
451 case Instruction::INSTANCE_OF:
452 EmitInsn_InstanceOf(ARGS);
453 break;
454
455 case Instruction::ARRAY_LENGTH:
456 EmitInsn_ArrayLength(ARGS);
457 break;
458
459 case Instruction::NEW_INSTANCE:
460 EmitInsn_NewInstance(ARGS);
461 break;
462
463 case Instruction::NEW_ARRAY:
464 EmitInsn_NewArray(ARGS);
465 break;
466
467 case Instruction::FILLED_NEW_ARRAY:
468 EmitInsn_FilledNewArray(ARGS, false);
469 break;
470
471 case Instruction::FILLED_NEW_ARRAY_RANGE:
472 EmitInsn_FilledNewArray(ARGS, true);
473 break;
474
475 case Instruction::FILL_ARRAY_DATA:
476 EmitInsn_FillArrayData(ARGS);
477 break;
478
479 case Instruction::THROW:
480 EmitInsn_ThrowException(ARGS);
481 break;
482
483 case Instruction::GOTO:
484 case Instruction::GOTO_16:
485 case Instruction::GOTO_32:
486 EmitInsn_UnconditionalBranch(ARGS);
487 break;
488
489 case Instruction::PACKED_SWITCH:
490 EmitInsn_PackedSwitch(ARGS);
491 break;
492
493 case Instruction::SPARSE_SWITCH:
494 EmitInsn_SparseSwitch(ARGS);
495 break;
496
497 case Instruction::CMPL_FLOAT:
498 EmitInsn_FPCompare(ARGS, kFloat, false);
499 break;
500
501 case Instruction::CMPG_FLOAT:
502 EmitInsn_FPCompare(ARGS, kFloat, true);
503 break;
504
505 case Instruction::CMPL_DOUBLE:
506 EmitInsn_FPCompare(ARGS, kDouble, false);
507 break;
508
509 case Instruction::CMPG_DOUBLE:
510 EmitInsn_FPCompare(ARGS, kDouble, true);
511 break;
512
513 case Instruction::CMP_LONG:
514 EmitInsn_LongCompare(ARGS);
515 break;
516
517 case Instruction::IF_EQ:
518 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
519 break;
520
521 case Instruction::IF_NE:
522 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
523 break;
524
525 case Instruction::IF_LT:
526 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
527 break;
528
529 case Instruction::IF_GE:
530 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
531 break;
532
533 case Instruction::IF_GT:
534 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
535 break;
536
537 case Instruction::IF_LE:
538 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
539 break;
540
541 case Instruction::IF_EQZ:
542 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
543 break;
544
545 case Instruction::IF_NEZ:
546 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
547 break;
548
549 case Instruction::IF_LTZ:
550 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
551 break;
552
553 case Instruction::IF_GEZ:
554 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
555 break;
556
557 case Instruction::IF_GTZ:
558 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
559 break;
560
561 case Instruction::IF_LEZ:
562 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
563 break;
564
565 case Instruction::AGET:
566 EmitInsn_AGet(ARGS, kInt);
567 break;
568
569 case Instruction::AGET_WIDE:
570 EmitInsn_AGet(ARGS, kLong);
571 break;
572
573 case Instruction::AGET_OBJECT:
574 EmitInsn_AGet(ARGS, kObject);
575 break;
576
577 case Instruction::AGET_BOOLEAN:
578 EmitInsn_AGet(ARGS, kBoolean);
579 break;
580
581 case Instruction::AGET_BYTE:
582 EmitInsn_AGet(ARGS, kByte);
583 break;
584
585 case Instruction::AGET_CHAR:
586 EmitInsn_AGet(ARGS, kChar);
587 break;
588
589 case Instruction::AGET_SHORT:
590 EmitInsn_AGet(ARGS, kShort);
591 break;
592
593 case Instruction::APUT:
594 EmitInsn_APut(ARGS, kInt);
595 break;
596
597 case Instruction::APUT_WIDE:
598 EmitInsn_APut(ARGS, kLong);
599 break;
600
601 case Instruction::APUT_OBJECT:
602 EmitInsn_APut(ARGS, kObject);
603 break;
604
605 case Instruction::APUT_BOOLEAN:
606 EmitInsn_APut(ARGS, kBoolean);
607 break;
608
609 case Instruction::APUT_BYTE:
610 EmitInsn_APut(ARGS, kByte);
611 break;
612
613 case Instruction::APUT_CHAR:
614 EmitInsn_APut(ARGS, kChar);
615 break;
616
617 case Instruction::APUT_SHORT:
618 EmitInsn_APut(ARGS, kShort);
619 break;
620
621 case Instruction::IGET:
622 EmitInsn_IGet(ARGS, kInt);
623 break;
624
625 case Instruction::IGET_WIDE:
626 EmitInsn_IGet(ARGS, kLong);
627 break;
628
629 case Instruction::IGET_OBJECT:
630 EmitInsn_IGet(ARGS, kObject);
631 break;
632
633 case Instruction::IGET_BOOLEAN:
634 EmitInsn_IGet(ARGS, kBoolean);
635 break;
636
637 case Instruction::IGET_BYTE:
638 EmitInsn_IGet(ARGS, kByte);
639 break;
640
641 case Instruction::IGET_CHAR:
642 EmitInsn_IGet(ARGS, kChar);
643 break;
644
645 case Instruction::IGET_SHORT:
646 EmitInsn_IGet(ARGS, kShort);
647 break;
648
649 case Instruction::IPUT:
650 EmitInsn_IPut(ARGS, kInt);
651 break;
652
653 case Instruction::IPUT_WIDE:
654 EmitInsn_IPut(ARGS, kLong);
655 break;
656
657 case Instruction::IPUT_OBJECT:
658 EmitInsn_IPut(ARGS, kObject);
659 break;
660
661 case Instruction::IPUT_BOOLEAN:
662 EmitInsn_IPut(ARGS, kBoolean);
663 break;
664
665 case Instruction::IPUT_BYTE:
666 EmitInsn_IPut(ARGS, kByte);
667 break;
668
669 case Instruction::IPUT_CHAR:
670 EmitInsn_IPut(ARGS, kChar);
671 break;
672
673 case Instruction::IPUT_SHORT:
674 EmitInsn_IPut(ARGS, kShort);
675 break;
676
677 case Instruction::SGET:
678 EmitInsn_SGet(ARGS, kInt);
679 break;
680
681 case Instruction::SGET_WIDE:
682 EmitInsn_SGet(ARGS, kLong);
683 break;
684
685 case Instruction::SGET_OBJECT:
686 EmitInsn_SGet(ARGS, kObject);
687 break;
688
689 case Instruction::SGET_BOOLEAN:
690 EmitInsn_SGet(ARGS, kBoolean);
691 break;
692
693 case Instruction::SGET_BYTE:
694 EmitInsn_SGet(ARGS, kByte);
695 break;
696
697 case Instruction::SGET_CHAR:
698 EmitInsn_SGet(ARGS, kChar);
699 break;
700
701 case Instruction::SGET_SHORT:
702 EmitInsn_SGet(ARGS, kShort);
703 break;
704
705 case Instruction::SPUT:
706 EmitInsn_SPut(ARGS, kInt);
707 break;
708
709 case Instruction::SPUT_WIDE:
710 EmitInsn_SPut(ARGS, kLong);
711 break;
712
713 case Instruction::SPUT_OBJECT:
714 EmitInsn_SPut(ARGS, kObject);
715 break;
716
717 case Instruction::SPUT_BOOLEAN:
718 EmitInsn_SPut(ARGS, kBoolean);
719 break;
720
721 case Instruction::SPUT_BYTE:
722 EmitInsn_SPut(ARGS, kByte);
723 break;
724
725 case Instruction::SPUT_CHAR:
726 EmitInsn_SPut(ARGS, kChar);
727 break;
728
729 case Instruction::SPUT_SHORT:
730 EmitInsn_SPut(ARGS, kShort);
731 break;
732
733
734 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800735 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800736 break;
737
738 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800739 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800740 break;
741
742 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800743 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800744 break;
745
746 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800747 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800748 break;
749
750 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800751 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800752 break;
753
754 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800755 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800756 break;
757
758 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800759 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800760 break;
761
762 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800763 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800764 break;
765
766 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800767 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800768 break;
769
770 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800771 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800772 break;
773
774 case Instruction::NEG_INT:
775 EmitInsn_Neg(ARGS, kInt);
776 break;
777
778 case Instruction::NOT_INT:
779 EmitInsn_Not(ARGS, kInt);
780 break;
781
782 case Instruction::NEG_LONG:
783 EmitInsn_Neg(ARGS, kLong);
784 break;
785
786 case Instruction::NOT_LONG:
787 EmitInsn_Not(ARGS, kLong);
788 break;
789
790 case Instruction::NEG_FLOAT:
791 EmitInsn_FNeg(ARGS, kFloat);
792 break;
793
794 case Instruction::NEG_DOUBLE:
795 EmitInsn_FNeg(ARGS, kDouble);
796 break;
797
798 case Instruction::INT_TO_LONG:
799 EmitInsn_SExt(ARGS);
800 break;
801
802 case Instruction::INT_TO_FLOAT:
803 EmitInsn_IntToFP(ARGS, kInt, kFloat);
804 break;
805
806 case Instruction::INT_TO_DOUBLE:
807 EmitInsn_IntToFP(ARGS, kInt, kDouble);
808 break;
809
810 case Instruction::LONG_TO_INT:
811 EmitInsn_Trunc(ARGS);
812 break;
813
814 case Instruction::LONG_TO_FLOAT:
815 EmitInsn_IntToFP(ARGS, kLong, kFloat);
816 break;
817
818 case Instruction::LONG_TO_DOUBLE:
819 EmitInsn_IntToFP(ARGS, kLong, kDouble);
820 break;
821
822 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700823 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800824 break;
825
826 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700827 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800828 break;
829
830 case Instruction::FLOAT_TO_DOUBLE:
831 EmitInsn_FExt(ARGS);
832 break;
833
834 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700835 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800836 break;
837
838 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700839 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800840 break;
841
842 case Instruction::DOUBLE_TO_FLOAT:
843 EmitInsn_FTrunc(ARGS);
844 break;
845
846 case Instruction::INT_TO_BYTE:
847 EmitInsn_TruncAndSExt(ARGS, 8);
848 break;
849
850 case Instruction::INT_TO_CHAR:
851 EmitInsn_TruncAndZExt(ARGS, 16);
852 break;
853
854 case Instruction::INT_TO_SHORT:
855 EmitInsn_TruncAndSExt(ARGS, 16);
856 break;
857
858 case Instruction::ADD_INT:
859 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
860 break;
861
862 case Instruction::SUB_INT:
863 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
864 break;
865
866 case Instruction::MUL_INT:
867 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
868 break;
869
870 case Instruction::DIV_INT:
871 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
872 break;
873
874 case Instruction::REM_INT:
875 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
876 break;
877
878 case Instruction::AND_INT:
879 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
880 break;
881
882 case Instruction::OR_INT:
883 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
884 break;
885
886 case Instruction::XOR_INT:
887 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
888 break;
889
890 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800891 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800892 break;
893
894 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800895 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800896 break;
897
898 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800899 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800900 break;
901
902 case Instruction::ADD_LONG:
903 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
904 break;
905
906 case Instruction::SUB_LONG:
907 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
908 break;
909
910 case Instruction::MUL_LONG:
911 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
912 break;
913
914 case Instruction::DIV_LONG:
915 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
916 break;
917
918 case Instruction::REM_LONG:
919 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
920 break;
921
922 case Instruction::AND_LONG:
923 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
924 break;
925
926 case Instruction::OR_LONG:
927 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
928 break;
929
930 case Instruction::XOR_LONG:
931 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
932 break;
933
934 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800935 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800936 break;
937
938 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800939 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800940 break;
941
942 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800943 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800944 break;
945
946 case Instruction::ADD_FLOAT:
947 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
948 break;
949
950 case Instruction::SUB_FLOAT:
951 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
952 break;
953
954 case Instruction::MUL_FLOAT:
955 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
956 break;
957
958 case Instruction::DIV_FLOAT:
959 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
960 break;
961
962 case Instruction::REM_FLOAT:
963 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
964 break;
965
966 case Instruction::ADD_DOUBLE:
967 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
968 break;
969
970 case Instruction::SUB_DOUBLE:
971 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
972 break;
973
974 case Instruction::MUL_DOUBLE:
975 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
976 break;
977
978 case Instruction::DIV_DOUBLE:
979 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
980 break;
981
982 case Instruction::REM_DOUBLE:
983 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
984 break;
985
986 case Instruction::ADD_INT_2ADDR:
987 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
988 break;
989
990 case Instruction::SUB_INT_2ADDR:
991 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
992 break;
993
994 case Instruction::MUL_INT_2ADDR:
995 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
996 break;
997
998 case Instruction::DIV_INT_2ADDR:
999 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
1000 break;
1001
1002 case Instruction::REM_INT_2ADDR:
1003 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
1004 break;
1005
1006 case Instruction::AND_INT_2ADDR:
1007 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
1008 break;
1009
1010 case Instruction::OR_INT_2ADDR:
1011 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1012 break;
1013
1014 case Instruction::XOR_INT_2ADDR:
1015 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1016 break;
1017
1018 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001019 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001020 break;
1021
1022 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001023 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001024 break;
1025
1026 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001027 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001028 break;
1029
1030 case Instruction::ADD_LONG_2ADDR:
1031 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1032 break;
1033
1034 case Instruction::SUB_LONG_2ADDR:
1035 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1036 break;
1037
1038 case Instruction::MUL_LONG_2ADDR:
1039 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1040 break;
1041
1042 case Instruction::DIV_LONG_2ADDR:
1043 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1044 break;
1045
1046 case Instruction::REM_LONG_2ADDR:
1047 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1048 break;
1049
1050 case Instruction::AND_LONG_2ADDR:
1051 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1052 break;
1053
1054 case Instruction::OR_LONG_2ADDR:
1055 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1056 break;
1057
1058 case Instruction::XOR_LONG_2ADDR:
1059 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1060 break;
1061
1062 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001063 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001064 break;
1065
1066 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001067 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001068 break;
1069
1070 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001071 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001072 break;
1073
1074 case Instruction::ADD_FLOAT_2ADDR:
1075 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1076 break;
1077
1078 case Instruction::SUB_FLOAT_2ADDR:
1079 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1080 break;
1081
1082 case Instruction::MUL_FLOAT_2ADDR:
1083 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1084 break;
1085
1086 case Instruction::DIV_FLOAT_2ADDR:
1087 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1088 break;
1089
1090 case Instruction::REM_FLOAT_2ADDR:
1091 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1092 break;
1093
1094 case Instruction::ADD_DOUBLE_2ADDR:
1095 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1096 break;
1097
1098 case Instruction::SUB_DOUBLE_2ADDR:
1099 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1100 break;
1101
1102 case Instruction::MUL_DOUBLE_2ADDR:
1103 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1104 break;
1105
1106 case Instruction::DIV_DOUBLE_2ADDR:
1107 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1108 break;
1109
1110 case Instruction::REM_DOUBLE_2ADDR:
1111 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1112 break;
1113
1114 case Instruction::ADD_INT_LIT16:
1115 case Instruction::ADD_INT_LIT8:
1116 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1117 break;
1118
1119 case Instruction::RSUB_INT:
1120 case Instruction::RSUB_INT_LIT8:
1121 EmitInsn_RSubImmediate(ARGS);
1122 break;
1123
1124 case Instruction::MUL_INT_LIT16:
1125 case Instruction::MUL_INT_LIT8:
1126 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1127 break;
1128
1129 case Instruction::DIV_INT_LIT16:
1130 case Instruction::DIV_INT_LIT8:
1131 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1132 break;
1133
1134 case Instruction::REM_INT_LIT16:
1135 case Instruction::REM_INT_LIT8:
1136 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1137 break;
1138
1139 case Instruction::AND_INT_LIT16:
1140 case Instruction::AND_INT_LIT8:
1141 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1142 break;
1143
1144 case Instruction::OR_INT_LIT16:
1145 case Instruction::OR_INT_LIT8:
1146 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1147 break;
1148
1149 case Instruction::XOR_INT_LIT16:
1150 case Instruction::XOR_INT_LIT8:
1151 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1152 break;
1153
1154 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001155 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001156 break;
1157
1158 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001159 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001160 break;
1161
1162 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001163 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001164 break;
1165
Logan Chien9e5f5c12012-04-10 13:51:45 +08001166 case Instruction::THROW_VERIFICATION_ERROR:
1167 EmitInsn_ThrowVerificationError(ARGS);
1168 break;
1169
Logan Chien70f94b42011-12-27 17:49:11 +08001170 case Instruction::UNUSED_3E:
1171 case Instruction::UNUSED_3F:
1172 case Instruction::UNUSED_40:
1173 case Instruction::UNUSED_41:
1174 case Instruction::UNUSED_42:
1175 case Instruction::UNUSED_43:
1176 case Instruction::UNUSED_73:
1177 case Instruction::UNUSED_79:
1178 case Instruction::UNUSED_7A:
1179 case Instruction::UNUSED_E3:
1180 case Instruction::UNUSED_E4:
1181 case Instruction::UNUSED_E5:
1182 case Instruction::UNUSED_E6:
1183 case Instruction::UNUSED_E7:
1184 case Instruction::UNUSED_E8:
1185 case Instruction::UNUSED_E9:
1186 case Instruction::UNUSED_EA:
1187 case Instruction::UNUSED_EB:
1188 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001189 case Instruction::UNUSED_EE:
1190 case Instruction::UNUSED_EF:
1191 case Instruction::UNUSED_F0:
1192 case Instruction::UNUSED_F1:
1193 case Instruction::UNUSED_F2:
1194 case Instruction::UNUSED_F3:
1195 case Instruction::UNUSED_F4:
1196 case Instruction::UNUSED_F5:
1197 case Instruction::UNUSED_F6:
1198 case Instruction::UNUSED_F7:
1199 case Instruction::UNUSED_F8:
1200 case Instruction::UNUSED_F9:
1201 case Instruction::UNUSED_FA:
1202 case Instruction::UNUSED_FB:
1203 case Instruction::UNUSED_FC:
1204 case Instruction::UNUSED_FD:
1205 case Instruction::UNUSED_FE:
1206 case Instruction::UNUSED_FF:
1207 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1208 break;
1209 }
1210
1211#undef ARGS
1212}
1213
1214
1215void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1216 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001217
1218 uint16_t insn_signature = code_item_->insns_[dex_pc];
1219
1220 if (insn_signature == Instruction::kPackedSwitchSignature ||
1221 insn_signature == Instruction::kSparseSwitchSignature ||
1222 insn_signature == Instruction::kArrayDataSignature) {
1223 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001224 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001225 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1226 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001227}
1228
1229
Logan Chien70f94b42011-12-27 17:49:11 +08001230void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1231 Instruction const* insn,
1232 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001233
Elliott Hughesadb8c672012-03-06 16:49:32 -08001234 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001235
Elliott Hughesadb8c672012-03-06 16:49:32 -08001236 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1237 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001238
Logan Chien70f94b42011-12-27 17:49:11 +08001239 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1240}
1241
1242
1243void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1244 Instruction const* insn,
1245 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001246
Elliott Hughesadb8c672012-03-06 16:49:32 -08001247 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001248
1249 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001250 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001251
Logan Chien70f94b42011-12-27 17:49:11 +08001252 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1253}
1254
1255
1256void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1257 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001258
Elliott Hughesadb8c672012-03-06 16:49:32 -08001259 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001260
TDYa127ee1f59b2012-04-25 00:56:40 -07001261 // Get thread-local exception field address
1262 llvm::Value* exception_object_addr =
TDYa127de479be2012-05-31 08:03:26 -07001263 irb_.Runtime().EmitLoadFromThreadOffset(Thread::ExceptionOffset().Int32Value(),
1264 irb_.getJObjectTy(),
1265 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001266
1267 // Set thread-local exception field address to NULL
TDYa127de479be2012-05-31 08:03:26 -07001268 irb_.Runtime().EmitStoreToThreadOffset(Thread::ExceptionOffset().Int32Value(),
1269 irb_.getJNull(),
1270 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001271
1272 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001273 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001274
Logan Chien70f94b42011-12-27 17:49:11 +08001275 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1276}
1277
1278
1279void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1280 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001281
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001283
1284 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001285 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001286
TDYa127c8dc1012012-04-19 07:03:33 -07001287 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001288
Logan Chien6c6f12d2012-01-13 19:26:27 +08001289 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1290
1291 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001292}
1293
1294
Logan Chien9e5f5c12012-04-10 13:51:45 +08001295void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1296 Instruction const* insn) {
1297
1298 DecodedInstruction dec_insn(insn);
1299
TDYa127c8dc1012012-04-19 07:03:33 -07001300 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001301
1302 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1303 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1304 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1305
1306 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1307 method_object_addr, kind_value, ref_value);
1308
1309 EmitBranchExceptionLandingPad(dex_pc);
1310}
1311
1312
Logan Chien70f94b42011-12-27 17:49:11 +08001313void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1314 Instruction const* insn) {
Logan Chien8dfcbea2012-02-17 18:50:32 +08001315 // Pop the shadow frame
1316 EmitPopShadowFrame();
1317
Logan Chien8898a272011-12-27 17:51:56 +08001318 // Return!
1319 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001320}
1321
1322
1323void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1324 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001325
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001327
Logan Chien8dfcbea2012-02-17 18:50:32 +08001328 // Pop the shadow frame
1329 EmitPopShadowFrame();
1330 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1331 // the return value might be collected since the shadow stack is popped.
1332
Logan Chien8898a272011-12-27 17:51:56 +08001333 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001334 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001335 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001336
1337 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001338}
1339
1340
1341void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1342 Instruction const* insn,
1343 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001344
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001346
1347 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1348
1349 int64_t imm = 0;
1350
1351 switch (insn->Opcode()) {
1352 // 32-bit Immediate
1353 case Instruction::CONST_4:
1354 case Instruction::CONST_16:
1355 case Instruction::CONST:
1356 case Instruction::CONST_WIDE_16:
1357 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001358 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001359 break;
1360
1361 case Instruction::CONST_HIGH16:
1362 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001363 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001364 break;
1365
1366 // 64-bit Immediate
1367 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001368 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001369 break;
1370
1371 case Instruction::CONST_WIDE_HIGH16:
1372 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001373 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001374 break;
1375
1376 // Unknown opcode for load constant (unreachable)
1377 default:
1378 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1379 break;
1380 }
1381
1382 // Store the non-object register
1383 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1384 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001385 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001386
1387 // Store the object register if it is possible to be null.
1388 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001389 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001390 }
1391
Logan Chien70f94b42011-12-27 17:49:11 +08001392 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1393}
1394
1395
1396void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1397 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001398
Elliott Hughesadb8c672012-03-06 16:49:32 -08001399 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001400
Elliott Hughesadb8c672012-03-06 16:49:32 -08001401 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001402
1403 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1404
TDYa1278ca10052012-05-05 19:57:06 -07001405 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001406
1407 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1408 llvm::BasicBlock* block_str_exist =
1409 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1410
1411 llvm::BasicBlock* block_str_resolve =
1412 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1413
1414 // Test: Is the string resolved and in the dex cache?
1415 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1416
TDYa127ac7b5bb2012-05-11 13:17:49 -07001417 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001418
1419 // String is resolved, go to next basic block.
1420 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001421 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001422 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1423
1424 // String is not resolved yet, resolve it now.
1425 irb_.SetInsertPoint(block_str_resolve);
1426
1427 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1428
1429 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1430
1431 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1432
TDYa127c8dc1012012-04-19 07:03:33 -07001433 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001434
1435 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1436 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001437
TDYa127526643e2012-05-26 01:01:48 -07001438 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001439 }
1440
1441 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001442 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001443
Logan Chien70f94b42011-12-27 17:49:11 +08001444 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1445}
1446
1447
Logan Chien27b30252012-01-14 03:43:35 +08001448llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1449 uint32_t type_idx) {
1450 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1451 *dex_file_, type_idx)) {
1452 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1453
1454 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1455
TDYa127de479be2012-05-31 08:03:26 -07001456 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001457
Logan Chien27b30252012-01-14 03:43:35 +08001458 llvm::Function* runtime_func =
1459 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1460
TDYa127c8dc1012012-04-19 07:03:33 -07001461 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001462
Logan Chien27b30252012-01-14 03:43:35 +08001463 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001464 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001465
TDYa127526643e2012-05-26 01:01:48 -07001466 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien27b30252012-01-14 03:43:35 +08001467
1468 return type_object_addr;
1469
1470 } else {
1471 // Try to load the class (type) object from the test cache.
1472 llvm::Value* type_field_addr =
1473 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1474
TDYa1278ca10052012-05-05 19:57:06 -07001475 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001476
1477 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1478 return type_object_addr;
1479 }
1480
1481 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1482
1483 // Test whether class (type) object is in the dex cache or not
1484 llvm::Value* equal_null =
1485 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1486
1487 llvm::BasicBlock* block_cont =
1488 CreateBasicBlockWithDexPC(dex_pc, "cont");
1489
1490 llvm::BasicBlock* block_load_class =
1491 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1492
TDYa127ac7b5bb2012-05-11 13:17:49 -07001493 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001494
1495 // Failback routine to load the class object
1496 irb_.SetInsertPoint(block_load_class);
1497
1498 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1499
1500 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1501
1502 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1503
TDYa127de479be2012-05-31 08:03:26 -07001504 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07001505
TDYa127c8dc1012012-04-19 07:03:33 -07001506 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001507
Logan Chien27b30252012-01-14 03:43:35 +08001508 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001509 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001510
TDYa127526643e2012-05-26 01:01:48 -07001511 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien27b30252012-01-14 03:43:35 +08001512
1513 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1514
1515 irb_.CreateBr(block_cont);
1516
1517 // Now the class object must be loaded
1518 irb_.SetInsertPoint(block_cont);
1519
1520 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1521
1522 phi->addIncoming(type_object_addr, block_original);
1523 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1524
1525 return phi;
1526 }
1527}
1528
1529
Logan Chien70f94b42011-12-27 17:49:11 +08001530void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1531 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001532
Elliott Hughesadb8c672012-03-06 16:49:32 -08001533 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001534
Elliott Hughesadb8c672012-03-06 16:49:32 -08001535 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1536 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001537
Logan Chien70f94b42011-12-27 17:49:11 +08001538 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1539}
1540
1541
1542void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1543 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001544
Elliott Hughesadb8c672012-03-06 16:49:32 -08001545 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001546
1547 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001548 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
TDYa127cc1b4c32012-05-15 07:31:37 -07001550 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1551 EmitGuard_NullPointerException(dex_pc, object_addr);
1552 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001553
TDYa127b08ed122012-06-05 23:51:19 -07001554 irb_.Runtime().EmitLockObject(object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001555
Logan Chien70f94b42011-12-27 17:49:11 +08001556 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1557}
1558
1559
1560void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1561 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001562
Elliott Hughesadb8c672012-03-06 16:49:32 -08001563 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001564
1565 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001566 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
TDYa127cc1b4c32012-05-15 07:31:37 -07001568 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1569 EmitGuard_NullPointerException(dex_pc, object_addr);
1570 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001571
TDYa127c8dc1012012-04-19 07:03:33 -07001572 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001573
TDYa127b08ed122012-06-05 23:51:19 -07001574 irb_.Runtime().EmitUnlockObject(object_addr);
TDYa127526643e2012-05-26 01:01:48 -07001575
1576 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001577
Logan Chien70f94b42011-12-27 17:49:11 +08001578 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1579}
1580
1581
1582void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1583 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001584
Elliott Hughesadb8c672012-03-06 16:49:32 -08001585 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001586
1587 llvm::BasicBlock* block_test_class =
1588 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1589
1590 llvm::BasicBlock* block_test_sub_class =
1591 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1592
1593 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001594 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001595
1596 // Test: Is the reference equal to null? Act as no-op when it is null.
1597 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1598
1599 irb_.CreateCondBr(equal_null,
1600 GetNextBasicBlock(dex_pc),
1601 block_test_class);
1602
1603 // Test: Is the object instantiated from the given class?
1604 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001605 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001606 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1607
1608 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1609
1610 llvm::Value* object_type_field_addr =
1611 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1612
1613 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001614 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001615
1616 llvm::Value* equal_class =
1617 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1618
1619 irb_.CreateCondBr(equal_class,
1620 GetNextBasicBlock(dex_pc),
1621 block_test_sub_class);
1622
1623 // Test: Is the object instantiated from the subclass of the given class?
1624 irb_.SetInsertPoint(block_test_sub_class);
1625
TDYa127c8dc1012012-04-19 07:03:33 -07001626 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001627
Logan Chienfc880952012-01-15 23:53:10 +08001628 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1629 type_object_addr, object_type_object_addr);
1630
TDYa127526643e2012-05-26 01:01:48 -07001631 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chienfc880952012-01-15 23:53:10 +08001632
Logan Chien70f94b42011-12-27 17:49:11 +08001633 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1634}
1635
1636
1637void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1638 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001639
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001641
1642 llvm::Constant* zero = irb_.getJInt(0);
1643 llvm::Constant* one = irb_.getJInt(1);
1644
1645 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1646
1647 llvm::BasicBlock* block_test_class =
1648 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1649
1650 llvm::BasicBlock* block_class_equals =
1651 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1652
1653 llvm::BasicBlock* block_test_sub_class =
1654 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1655
1656 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001657 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001658
1659 // Overview of the following code :
1660 // We check for null, if so, then false, otherwise check for class == . If so
1661 // then true, otherwise do callout slowpath.
1662 //
1663 // Test: Is the reference equal to null? Set 0 when it is null.
1664 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1665
1666 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1667
1668 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001669 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001670 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1671
1672 // Test: Is the object instantiated from the given class?
1673 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001674 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001675 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1676
1677 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1678
1679 llvm::Value* object_type_field_addr =
1680 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1681
1682 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001683 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001684
1685 llvm::Value* equal_class =
1686 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1687
1688 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1689
1690 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001691 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001692 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1693
1694 // Test: Is the object instantiated from the subclass of the given class?
1695 irb_.SetInsertPoint(block_test_sub_class);
1696
1697 llvm::Value* result =
1698 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1699 type_object_addr, object_type_object_addr);
1700
Elliott Hughesadb8c672012-03-06 16:49:32 -08001701 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001702
Logan Chien70f94b42011-12-27 17:49:11 +08001703 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1704}
1705
1706
Logan Chien61bb6142012-02-03 15:34:53 +08001707llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001708 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001709 return irb_.LoadFromObjectOffset(array,
1710 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001711 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001712 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001713}
1714
1715
Logan Chien70f94b42011-12-27 17:49:11 +08001716void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1717 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001718
Elliott Hughesadb8c672012-03-06 16:49:32 -08001719 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001720
1721 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001722 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001723 EmitGuard_NullPointerException(dex_pc, array_addr);
1724
1725 // Get the array length and store it to the register
1726 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001728
Logan Chien70f94b42011-12-27 17:49:11 +08001729 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1730}
1731
1732
1733void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1734 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001735
Elliott Hughesadb8c672012-03-06 16:49:32 -08001736 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001737
1738 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001739 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1740 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001741 runtime_func = irb_.GetRuntime(AllocObject);
1742 } else {
1743 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1744 }
1745
Elliott Hughesadb8c672012-03-06 16:49:32 -08001746 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001747
1748 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1749
TDYa127de479be2012-05-31 08:03:26 -07001750 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07001751
TDYa127c8dc1012012-04-19 07:03:33 -07001752 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001753
Logan Chien032bdad2012-01-16 09:59:23 +08001754 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001755 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001756
TDYa127526643e2012-05-26 01:01:48 -07001757 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien032bdad2012-01-16 09:59:23 +08001758
Elliott Hughesadb8c672012-03-06 16:49:32 -08001759 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001760
Logan Chien70f94b42011-12-27 17:49:11 +08001761 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1762}
1763
1764
Logan Chiena2cc6a32012-01-16 10:38:41 +08001765llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1766 int32_t length,
1767 uint32_t type_idx,
1768 bool is_filled_new_array) {
1769 llvm::Function* runtime_func;
1770
1771 bool skip_access_check =
1772 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1773 *dex_file_, type_idx);
1774
TDYa127a849cb62012-04-01 05:59:34 -07001775 llvm::Value* array_length_value;
1776
Logan Chiena2cc6a32012-01-16 10:38:41 +08001777 if (is_filled_new_array) {
1778 runtime_func = skip_access_check ?
1779 irb_.GetRuntime(CheckAndAllocArray) :
1780 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001781 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001782 } else {
1783 runtime_func = skip_access_check ?
1784 irb_.GetRuntime(AllocArray) :
1785 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001786 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001787 }
1788
1789 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1790
1791 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1792
TDYa127de479be2012-05-31 08:03:26 -07001793 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07001794
TDYa127c8dc1012012-04-19 07:03:33 -07001795 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001796
Logan Chiena2cc6a32012-01-16 10:38:41 +08001797 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001798 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1799 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001800
TDYa127526643e2012-05-26 01:01:48 -07001801 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001802
1803 return object_addr;
1804}
1805
1806
Logan Chien70f94b42011-12-27 17:49:11 +08001807void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1808 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001809
Elliott Hughesadb8c672012-03-06 16:49:32 -08001810 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001811
1812 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001813 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001814
Elliott Hughesadb8c672012-03-06 16:49:32 -08001815 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001816
Logan Chien70f94b42011-12-27 17:49:11 +08001817 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1818}
1819
1820
1821void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1822 Instruction const* insn,
1823 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001824
Elliott Hughesadb8c672012-03-06 16:49:32 -08001825 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001826
1827 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001828 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001829
Elliott Hughesadb8c672012-03-06 16:49:32 -08001830 if (dec_insn.vA > 0) {
Logan Chien4b1baf12012-05-18 16:28:36 +08001831 // Check for the element type
1832 uint32_t type_desc_len = 0;
1833 const char* type_desc =
1834 dex_file_->StringByTypeIdx(dec_insn.vB, &type_desc_len);
1835
1836 DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier
1837 DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier
1838 bool is_elem_int_ty = (type_desc[1] == 'I');
Logan Chiena85fb2f2012-01-16 12:52:56 +08001839
Logan Chiendd361c92012-04-10 23:40:37 +08001840 uint32_t alignment;
1841 llvm::Constant* elem_size;
1842 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001843
Logan Chiendd361c92012-04-10 23:40:37 +08001844 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1845 // as the element, thus we are only checking 2 cases: primitive int and
1846 // non-primitive type.
Logan Chien4b1baf12012-05-18 16:28:36 +08001847 if (is_elem_int_ty) {
Logan Chiendd361c92012-04-10 23:40:37 +08001848 alignment = sizeof(int32_t);
1849 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 field_type = irb_.getJIntTy()->getPointerTo();
1851 } else {
Logan Chiendd361c92012-04-10 23:40:37 +08001852 alignment = irb_.getSizeOfPtrEquivInt();
1853 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001854 field_type = irb_.getJObjectTy()->getPointerTo();
1855 }
1856
Logan Chiendd361c92012-04-10 23:40:37 +08001857 llvm::Value* data_field_offset =
1858 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1859
1860 llvm::Value* data_field_addr =
1861 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1862
Logan Chiena85fb2f2012-01-16 12:52:56 +08001863 // TODO: Tune this code. Currently we are generating one instruction for
1864 // one element which may be very space consuming. Maybe changing to use
1865 // memcpy may help; however, since we can't guarantee that the alloca of
1866 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001867 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001868 int reg_index;
1869 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001870 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001871 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001872 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001873 }
1874
1875 llvm::Value* reg_value;
Logan Chien4b1baf12012-05-18 16:28:36 +08001876 if (is_elem_int_ty) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001877 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1878 } else {
1879 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1880 }
1881
TDYa127706e7db2012-05-06 00:05:33 -07001882 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001883
Logan Chiendd361c92012-04-10 23:40:37 +08001884 data_field_addr =
1885 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001886 }
1887 }
1888
1889 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1890
Logan Chien70f94b42011-12-27 17:49:11 +08001891 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1892}
1893
1894
1895void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1896 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001897
Elliott Hughesadb8c672012-03-06 16:49:32 -08001898 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001899
1900 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001901 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001902 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001903
Logan Chien19c350a2012-05-01 19:21:32 +08001904 const Instruction::ArrayDataPayload* payload =
1905 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1906 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien86f50672012-04-24 13:08:45 +08001908 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001909 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001910
Logan Chien86f50672012-04-24 13:08:45 +08001911 if (payload->element_count == 0) {
1912 // When the number of the elements in the payload is zero, we don't have
1913 // to copy any numbers. However, we should check whether the array object
1914 // address is equal to null or not.
1915 EmitGuard_NullPointerException(dex_pc, array_addr);
1916 } else {
1917 // To save the code size, we are going to call the runtime function to
1918 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001919
Logan Chien86f50672012-04-24 13:08:45 +08001920 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001921
Logan Chien86f50672012-04-24 13:08:45 +08001922 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001923
Logan Chien86f50672012-04-24 13:08:45 +08001924 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001925
Logan Chien86f50672012-04-24 13:08:45 +08001926 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001927
Logan Chien86f50672012-04-24 13:08:45 +08001928 irb_.CreateCall4(runtime_func,
1929 method_object_addr, irb_.getInt32(dex_pc),
1930 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001931
TDYa127526643e2012-05-26 01:01:48 -07001932 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chiene58b6582012-01-16 17:13:13 +08001933 }
1934
Logan Chien70f94b42011-12-27 17:49:11 +08001935 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1936}
1937
1938
1939void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1940 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001941
Elliott Hughesadb8c672012-03-06 16:49:32 -08001942 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001943
Elliott Hughesadb8c672012-03-06 16:49:32 -08001944 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001945
Logan Chiena466c162011-12-27 17:55:46 +08001946 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001947}
1948
1949
1950void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1951 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001952
Elliott Hughesadb8c672012-03-06 16:49:32 -08001953 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001954
Logan Chien7a89b6d2011-12-27 17:56:56 +08001955 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001956 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001957
Logan Chien19c350a2012-05-01 19:21:32 +08001958 const Instruction::PackedSwitchPayload* payload =
1959 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1960 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Elliott Hughesadb8c672012-03-06 16:49:32 -08001962 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001963
1964 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001965 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001966
Logan Chien19c350a2012-05-01 19:21:32 +08001967 for (uint16_t i = 0; i < payload->case_count; ++i) {
1968 sw->addCase(irb_.getInt32(payload->first_key + i),
1969 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001970 }
Logan Chien70f94b42011-12-27 17:49:11 +08001971}
1972
1973
1974void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1975 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001976
Elliott Hughesadb8c672012-03-06 16:49:32 -08001977 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001978
Logan Chien7a89b6d2011-12-27 17:56:56 +08001979 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001980 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001981
Logan Chien19c350a2012-05-01 19:21:32 +08001982 const Instruction::SparseSwitchPayload* payload =
1983 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1984 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001985
Logan Chien19c350a2012-05-01 19:21:32 +08001986 const int32_t* keys = payload->GetKeys();
1987 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001988
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001990
1991 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001992 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001993
Logan Chien19c350a2012-05-01 19:21:32 +08001994 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001995 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1996 }
Logan Chien70f94b42011-12-27 17:49:11 +08001997}
1998
1999
2000void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2001 Instruction const* insn,
2002 JType fp_jty,
2003 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002004
Elliott Hughesadb8c672012-03-06 16:49:32 -08002005 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002006
2007 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2008
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2010 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002011
2012 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2013 llvm::Value* cmp_lt;
2014
2015 if (gt_bias) {
2016 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2017 } else {
2018 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2019 }
2020
2021 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002022 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002023
Logan Chien70f94b42011-12-27 17:49:11 +08002024 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2025}
2026
2027
2028void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2029 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002030
Elliott Hughesadb8c672012-03-06 16:49:32 -08002031 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002032
Elliott Hughesadb8c672012-03-06 16:49:32 -08002033 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2034 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002035
2036 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2037 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2038
2039 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002040 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002041
Logan Chien70f94b42011-12-27 17:49:11 +08002042 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2043}
2044
2045
Logan Chien2c37e8e2011-12-27 17:58:46 +08002046llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2047 llvm::Value* cmp_lt) {
2048
2049 llvm::Constant* zero = irb_.getJInt(0);
2050 llvm::Constant* pos1 = irb_.getJInt(1);
2051 llvm::Constant* neg1 = irb_.getJInt(-1);
2052
2053 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2054 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2055
2056 return result_eq;
2057}
2058
2059
Logan Chien70f94b42011-12-27 17:49:11 +08002060void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2061 Instruction const* insn,
2062 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002063
Elliott Hughesadb8c672012-03-06 16:49:32 -08002064 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002065
Elliott Hughesadb8c672012-03-06 16:49:32 -08002066 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2067 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002068
2069 DCHECK_NE(kRegUnknown, src1_reg_cat);
2070 DCHECK_NE(kRegUnknown, src2_reg_cat);
2071 DCHECK_NE(kRegCat2, src1_reg_cat);
2072 DCHECK_NE(kRegCat2, src2_reg_cat);
2073
Elliott Hughesadb8c672012-03-06 16:49:32 -08002074 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002075
Logan Chiena78e3c82011-12-27 17:59:35 +08002076 llvm::Value* src1_value;
2077 llvm::Value* src2_value;
2078
TDYa1278e9b4492012-04-24 15:50:27 -07002079 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2080 src1_value = irb_.getInt32(0);
2081 src2_value = irb_.getInt32(0);
2082 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002083 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2084
2085 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002086 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2087 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002088 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002089 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2090 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002091 }
2092 } else {
2093 DCHECK(src1_reg_cat == kRegZero ||
2094 src2_reg_cat == kRegZero);
2095
2096 if (src1_reg_cat == kRegZero) {
2097 if (src2_reg_cat == kRegCat1nr) {
2098 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002099 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002100 } else {
2101 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002102 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002103 }
2104 } else { // src2_reg_cat == kRegZero
2105 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002106 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002107 src2_value = irb_.getJInt(0);
2108 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002109 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002110 src2_value = irb_.getJNull();
2111 }
2112 }
2113 }
2114
2115 llvm::Value* cond_value =
2116 EmitConditionResult(src1_value, src2_value, cond);
2117
2118 irb_.CreateCondBr(cond_value,
2119 GetBasicBlock(dex_pc + branch_offset),
2120 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002121}
2122
2123
2124void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2125 Instruction const* insn,
2126 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002127
Elliott Hughesadb8c672012-03-06 16:49:32 -08002128 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002129
Elliott Hughesadb8c672012-03-06 16:49:32 -08002130 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002131
2132 DCHECK_NE(kRegUnknown, src_reg_cat);
2133 DCHECK_NE(kRegCat2, src_reg_cat);
2134
Elliott Hughesadb8c672012-03-06 16:49:32 -08002135 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002136
Logan Chiena78e3c82011-12-27 17:59:35 +08002137 llvm::Value* src1_value;
2138 llvm::Value* src2_value;
2139
TDYa1278e9b4492012-04-24 15:50:27 -07002140 if (src_reg_cat == kRegZero) {
2141 src1_value = irb_.getInt32(0);
2142 src2_value = irb_.getInt32(0);
2143 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002144 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002145 src2_value = irb_.getInt32(0);
2146 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002148 src2_value = irb_.getJNull();
2149 }
2150
2151 llvm::Value* cond_value =
2152 EmitConditionResult(src1_value, src2_value, cond);
2153
2154 irb_.CreateCondBr(cond_value,
2155 GetBasicBlock(dex_pc + branch_offset),
2156 GetNextBasicBlock(dex_pc));
2157}
2158
TDYa1271d7e5102012-05-13 09:27:05 -07002159InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002160 Compiler::MethodReference mref(dex_file_, method_idx_);
2161
2162 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002163 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002164
Logan Chiena78e3c82011-12-27 17:59:35 +08002165 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2166
TDYa1271d7e5102012-05-13 09:27:05 -07002167 return map;
2168}
2169
2170RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2171 uint16_t reg_idx) {
2172 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2173
Logan Chiena78e3c82011-12-27 17:59:35 +08002174 return map->GetRegCategory(dex_pc, reg_idx);
2175}
2176
TDYa1271d7e5102012-05-13 09:27:05 -07002177bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2178 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2179
2180 return map->IsRegCanBeObject(reg_idx);
2181}
2182
Logan Chiena78e3c82011-12-27 17:59:35 +08002183
2184llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2185 llvm::Value* rhs,
2186 CondBranchKind cond) {
2187 switch (cond) {
2188 case kCondBranch_EQ:
2189 return irb_.CreateICmpEQ(lhs, rhs);
2190
2191 case kCondBranch_NE:
2192 return irb_.CreateICmpNE(lhs, rhs);
2193
2194 case kCondBranch_LT:
2195 return irb_.CreateICmpSLT(lhs, rhs);
2196
2197 case kCondBranch_GE:
2198 return irb_.CreateICmpSGE(lhs, rhs);
2199
2200 case kCondBranch_GT:
2201 return irb_.CreateICmpSGT(lhs, rhs);
2202
2203 case kCondBranch_LE:
2204 return irb_.CreateICmpSLE(lhs, rhs);
2205
2206 default: // Unreachable
2207 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2208 return NULL;
2209 }
Logan Chien70f94b42011-12-27 17:49:11 +08002210}
2211
TDYa12783bb6622012-04-17 02:20:34 -07002212void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2213 // Using runtime support, let the target can override by InlineAssembly.
2214 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2215
2216 irb_.CreateCall2(runtime_func, value, target_addr);
2217}
Logan Chien70f94b42011-12-27 17:49:11 +08002218
Logan Chiene27fdbb2012-01-02 23:27:26 +08002219void
2220MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2221 llvm::Value* array,
2222 llvm::Value* index) {
2223 llvm::Value* array_len = EmitLoadArrayLength(array);
2224
2225 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2226
2227 llvm::BasicBlock* block_exception =
2228 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2229
2230 llvm::BasicBlock* block_continue =
2231 CreateBasicBlockWithDexPC(dex_pc, "cont");
2232
TDYa127ac7b5bb2012-05-11 13:17:49 -07002233 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002234
2235 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002236
TDYa127c8dc1012012-04-19 07:03:33 -07002237 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002238 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2239 EmitBranchExceptionLandingPad(dex_pc);
2240
2241 irb_.SetInsertPoint(block_continue);
2242}
2243
2244
2245void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2246 llvm::Value* array,
2247 llvm::Value* index) {
2248 EmitGuard_NullPointerException(dex_pc, array);
2249 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2250}
2251
2252
2253// Emit Array GetElementPtr
2254llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2255 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002256 JType elem_jty) {
2257
2258 int data_offset;
2259 if (elem_jty == kLong || elem_jty == kDouble ||
2260 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2261 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2262 } else {
2263 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2264 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002265
2266 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002267 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002268
TDYa1278db6ea32012-05-17 04:48:42 -07002269 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2270
Logan Chiene27fdbb2012-01-02 23:27:26 +08002271 llvm::Value* array_data_addr =
2272 irb_.CreatePtrDisp(array_addr, data_offset_value,
2273 elem_type->getPointerTo());
2274
2275 return irb_.CreateGEP(array_data_addr, index_value);
2276}
2277
2278
Logan Chien70f94b42011-12-27 17:49:11 +08002279void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2280 Instruction const* insn,
2281 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002282
Elliott Hughesadb8c672012-03-06 16:49:32 -08002283 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002284
Elliott Hughesadb8c672012-03-06 16:49:32 -08002285 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2286 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002287
2288 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2289
TDYa1278db6ea32012-05-17 04:48:42 -07002290 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002291
TDYa127706e7db2012-05-06 00:05:33 -07002292 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002293
Elliott Hughesadb8c672012-03-06 16:49:32 -08002294 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002295
Logan Chien70f94b42011-12-27 17:49:11 +08002296 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2297}
2298
2299
2300void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2301 Instruction const* insn,
2302 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002303
Elliott Hughesadb8c672012-03-06 16:49:32 -08002304 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002305
Elliott Hughesadb8c672012-03-06 16:49:32 -08002306 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2307 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002308
2309 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2310
TDYa1278db6ea32012-05-17 04:48:42 -07002311 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002312
Elliott Hughesadb8c672012-03-06 16:49:32 -08002313 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002314
TDYa12783bb6622012-04-17 02:20:34 -07002315 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002316 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2317
2318 irb_.CreateCall2(runtime_func, new_value, array_addr);
2319
TDYa127526643e2012-05-26 01:01:48 -07002320 EmitGuard_ExceptionLandingPad(dex_pc, false);
TDYa12783bb6622012-04-17 02:20:34 -07002321
2322 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002323 }
2324
TDYa127706e7db2012-05-06 00:05:33 -07002325 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002326
Logan Chien70f94b42011-12-27 17:49:11 +08002327 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2328}
2329
2330
2331void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2332 Instruction const* insn,
2333 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002334
Elliott Hughesadb8c672012-03-06 16:49:32 -08002335 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002336
Elliott Hughesadb8c672012-03-06 16:49:32 -08002337 uint32_t reg_idx = dec_insn.vB;
2338 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002339
Logan Chien48f1d2a2012-01-02 22:49:53 +08002340 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2341
TDYa127cc1b4c32012-05-15 07:31:37 -07002342 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2343 EmitGuard_NullPointerException(dex_pc, object_addr);
2344 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002345
2346 llvm::Value* field_value;
2347
Logan Chien933abf82012-04-11 12:24:31 +08002348 int field_offset;
2349 bool is_volatile;
2350 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2351 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002352
Logan Chien933abf82012-04-11 12:24:31 +08002353 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002354 llvm::Function* runtime_func;
2355
2356 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002357 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002358 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002359 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002360 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002361 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002362 }
2363
2364 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2365
2366 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2367
TDYa127c8dc1012012-04-19 07:03:33 -07002368 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002369
Logan Chien3b2b2e72012-03-06 16:11:45 +08002370 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2371 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002372
TDYa127526643e2012-05-26 01:01:48 -07002373 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002374
2375 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002376 DCHECK_GE(field_offset, 0);
2377
Logan Chien48f1d2a2012-01-02 22:49:53 +08002378 llvm::PointerType* field_type =
2379 irb_.getJType(field_jty, kField)->getPointerTo();
2380
Logan Chien933abf82012-04-11 12:24:31 +08002381 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002382
2383 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002384 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002385
Logan Chien933abf82012-04-11 12:24:31 +08002386 // TODO: Check is_volatile. We need to generate atomic load instruction
2387 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002388 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002389 }
2390
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002392
Logan Chien70f94b42011-12-27 17:49:11 +08002393 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2394}
2395
2396
2397void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2398 Instruction const* insn,
2399 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002400
Elliott Hughesadb8c672012-03-06 16:49:32 -08002401 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002402
Elliott Hughesadb8c672012-03-06 16:49:32 -08002403 uint32_t reg_idx = dec_insn.vB;
2404 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002405
Logan Chiendd6aa872012-01-03 16:06:32 +08002406 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2407
TDYa127cc1b4c32012-05-15 07:31:37 -07002408 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2409 EmitGuard_NullPointerException(dex_pc, object_addr);
2410 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002411
Elliott Hughesadb8c672012-03-06 16:49:32 -08002412 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002413
Logan Chien933abf82012-04-11 12:24:31 +08002414 int field_offset;
2415 bool is_volatile;
2416 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2417 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002418
Logan Chien933abf82012-04-11 12:24:31 +08002419 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002420 llvm::Function* runtime_func;
2421
2422 if (field_jty == kObject) {
2423 runtime_func = irb_.GetRuntime(SetObjectInstance);
2424 } else if (field_jty == kLong || field_jty == kDouble) {
2425 runtime_func = irb_.GetRuntime(Set64Instance);
2426 } else {
2427 runtime_func = irb_.GetRuntime(Set32Instance);
2428 }
2429
2430 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2431
2432 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2433
TDYa127c8dc1012012-04-19 07:03:33 -07002434 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002435
Logan Chien3b2b2e72012-03-06 16:11:45 +08002436 irb_.CreateCall4(runtime_func, field_idx_value,
2437 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002438
TDYa127526643e2012-05-26 01:01:48 -07002439 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002440
2441 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002442 DCHECK_GE(field_offset, 0);
2443
Logan Chiendd6aa872012-01-03 16:06:32 +08002444 llvm::PointerType* field_type =
2445 irb_.getJType(field_jty, kField)->getPointerTo();
2446
Logan Chien933abf82012-04-11 12:24:31 +08002447 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002448
2449 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002450 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002451
Logan Chien933abf82012-04-11 12:24:31 +08002452 // TODO: Check is_volatile. We need to generate atomic store instruction
2453 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002454 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002455
2456 if (field_jty == kObject) { // If put an object, mark the GC card table.
2457 EmitMarkGCCard(new_value, object_addr);
2458 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002459 }
2460
Logan Chien70f94b42011-12-27 17:49:11 +08002461 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2462}
2463
2464
Logan Chien438c4b62012-01-17 16:06:00 +08002465llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2466 uint32_t type_idx) {
2467 llvm::BasicBlock* block_load_static =
2468 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2469
2470 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2471
2472 // Load static storage from dex cache
2473 llvm::Value* storage_field_addr =
2474 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2475
TDYa1278ca10052012-05-05 19:57:06 -07002476 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002477
2478 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2479
2480 // Test: Is the static storage of this class initialized?
2481 llvm::Value* equal_null =
2482 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2483
TDYa127ac7b5bb2012-05-11 13:17:49 -07002484 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002485
2486 // Failback routine to load the class object
2487 irb_.SetInsertPoint(block_load_static);
2488
2489 llvm::Function* runtime_func =
2490 irb_.GetRuntime(InitializeStaticStorage);
2491
2492 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2493
2494 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2495
TDYa127de479be2012-05-31 08:03:26 -07002496 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127706e9b62012-04-19 12:24:26 -07002497
TDYa127c8dc1012012-04-19 07:03:33 -07002498 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002499
Logan Chien438c4b62012-01-17 16:06:00 +08002500 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002501 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002502
TDYa127526643e2012-05-26 01:01:48 -07002503 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002504
2505 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2506
2507 irb_.CreateBr(block_cont);
2508
2509 // Now the class object must be loaded
2510 irb_.SetInsertPoint(block_cont);
2511
2512 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2513
2514 phi->addIncoming(storage_object_addr, block_original);
2515 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2516
2517 return phi;
2518}
2519
2520
Logan Chien70f94b42011-12-27 17:49:11 +08002521void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2522 Instruction const* insn,
2523 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002524
Elliott Hughesadb8c672012-03-06 16:49:32 -08002525 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002526
Logan Chien933abf82012-04-11 12:24:31 +08002527 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002528
Logan Chien933abf82012-04-11 12:24:31 +08002529 int field_offset;
2530 int ssb_index;
2531 bool is_referrers_class;
2532 bool is_volatile;
2533
2534 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2535 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2536 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002537
2538 llvm::Value* static_field_value;
2539
Logan Chien933abf82012-04-11 12:24:31 +08002540 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002541 llvm::Function* runtime_func;
2542
2543 if (field_jty == kObject) {
2544 runtime_func = irb_.GetRuntime(GetObjectStatic);
2545 } else if (field_jty == kLong || field_jty == kDouble) {
2546 runtime_func = irb_.GetRuntime(Get64Static);
2547 } else {
2548 runtime_func = irb_.GetRuntime(Get32Static);
2549 }
2550
Elliott Hughesadb8c672012-03-06 16:49:32 -08002551 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002552
2553 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2554
TDYa127c8dc1012012-04-19 07:03:33 -07002555 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002556
Logan Chien438c4b62012-01-17 16:06:00 +08002557 static_field_value =
2558 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2559
TDYa127526643e2012-05-26 01:01:48 -07002560 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien438c4b62012-01-17 16:06:00 +08002561
2562 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002563 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002564
Logan Chien933abf82012-04-11 12:24:31 +08002565 llvm::Value* static_storage_addr = NULL;
2566
2567 if (is_referrers_class) {
2568 // Fast path, static storage base is this method's class
2569 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2570
TDYa127ee1f59b2012-04-25 00:56:40 -07002571 static_storage_addr =
2572 irb_.LoadFromObjectOffset(method_object_addr,
2573 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002574 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002575 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002576 } else {
2577 // Medium path, static storage base in a different class which
2578 // requires checks that the other class is initialized
2579 DCHECK_GE(ssb_index, 0);
2580 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2581 }
2582
2583 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002584
2585 llvm::Value* static_field_addr =
2586 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2587 irb_.getJType(field_jty, kField)->getPointerTo());
2588
Logan Chien933abf82012-04-11 12:24:31 +08002589 // TODO: Check is_volatile. We need to generate atomic load instruction
2590 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002591 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002592 }
2593
Elliott Hughesadb8c672012-03-06 16:49:32 -08002594 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002595
Logan Chien70f94b42011-12-27 17:49:11 +08002596 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2597}
2598
2599
2600void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2601 Instruction const* insn,
2602 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002603
Elliott Hughesadb8c672012-03-06 16:49:32 -08002604 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002605
Logan Chien933abf82012-04-11 12:24:31 +08002606 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002607
Elliott Hughesadb8c672012-03-06 16:49:32 -08002608 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002609
Logan Chien933abf82012-04-11 12:24:31 +08002610 int field_offset;
2611 int ssb_index;
2612 bool is_referrers_class;
2613 bool is_volatile;
2614
2615 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2616 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2617 is_referrers_class, is_volatile, true);
2618
2619 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002620 llvm::Function* runtime_func;
2621
2622 if (field_jty == kObject) {
2623 runtime_func = irb_.GetRuntime(SetObjectStatic);
2624 } else if (field_jty == kLong || field_jty == kDouble) {
2625 runtime_func = irb_.GetRuntime(Set64Static);
2626 } else {
2627 runtime_func = irb_.GetRuntime(Set32Static);
2628 }
2629
Elliott Hughesadb8c672012-03-06 16:49:32 -08002630 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002631
2632 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2633
TDYa127c8dc1012012-04-19 07:03:33 -07002634 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002635
Logan Chien14179c82012-01-17 17:06:34 +08002636 irb_.CreateCall3(runtime_func, field_idx_value,
2637 method_object_addr, new_value);
2638
TDYa127526643e2012-05-26 01:01:48 -07002639 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien14179c82012-01-17 17:06:34 +08002640
2641 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002642 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002643
Logan Chien933abf82012-04-11 12:24:31 +08002644 llvm::Value* static_storage_addr = NULL;
2645
2646 if (is_referrers_class) {
2647 // Fast path, static storage base is this method's class
2648 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2649
TDYa127ee1f59b2012-04-25 00:56:40 -07002650 static_storage_addr =
2651 irb_.LoadFromObjectOffset(method_object_addr,
2652 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002653 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002654 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002655 } else {
2656 // Medium path, static storage base in a different class which
2657 // requires checks that the other class is initialized
2658 DCHECK_GE(ssb_index, 0);
2659 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2660 }
2661
2662 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002663
2664 llvm::Value* static_field_addr =
2665 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2666 irb_.getJType(field_jty, kField)->getPointerTo());
2667
Logan Chien933abf82012-04-11 12:24:31 +08002668 // TODO: Check is_volatile. We need to generate atomic store instruction
2669 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002670 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002671
2672 if (field_jty == kObject) { // If put an object, mark the GC card table.
2673 EmitMarkGCCard(new_value, static_storage_addr);
2674 }
Logan Chien14179c82012-01-17 17:06:34 +08002675 }
2676
Logan Chien70f94b42011-12-27 17:49:11 +08002677 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2678}
2679
2680
Logan Chien1a121b92012-02-15 22:23:42 +08002681void MethodCompiler::
2682EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2683 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002685 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002686 bool is_static) {
2687
2688 // Get method signature
2689 DexFile::MethodId const& method_id =
2690 dex_file_->GetMethodId(callee_method_idx);
2691
Logan Chien8faf8022012-02-24 12:25:29 +08002692 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002693 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002694 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002695
2696 // Load argument values according to the shorty (without "this")
2697 uint16_t reg_count = 0;
2698
2699 if (!is_static) {
2700 ++reg_count; // skip the "this" pointer
2701 }
2702
Logan Chien7e7fabc2012-04-10 18:59:11 +08002703 bool is_range = (arg_fmt == kArgRange);
2704
Logan Chien8faf8022012-02-24 12:25:29 +08002705 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002706 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2707 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002708
2709 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2710
2711 ++reg_count;
2712 if (shorty[i] == 'J' || shorty[i] == 'D') {
2713 // Wide types, such as long and double, are using a pair of registers
2714 // to store the value, so we have to increase arg_reg again.
2715 ++reg_count;
2716 }
2717 }
2718
Elliott Hughesadb8c672012-03-06 16:49:32 -08002719 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002720 << "Actual argument mismatch for callee: "
2721 << PrettyMethod(callee_method_idx, *dex_file_);
2722}
2723
TDYa1270b686e52012-04-09 22:43:35 -07002724llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2725 uint32_t method_idx,
2726 bool is_static) {
2727 // TODO: Remove this after we solve the link and trampoline related problems.
2728 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2729
2730 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2731
2732 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002733}
Logan Chien1a121b92012-02-15 22:23:42 +08002734
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002735
Logan Chien7e7fabc2012-04-10 18:59:11 +08002736void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2737 Instruction const* insn,
2738 InvokeType invoke_type,
2739 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002741
Logan Chien61c65dc2012-02-29 03:22:30 +08002742 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002743 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002744
Logan Chien7e7fabc2012-04-10 18:59:11 +08002745 // Compute invoke related information for compiler decision
2746 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002747 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002748 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002749 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002750 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002751 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002752
Logan Chien7e7fabc2012-04-10 18:59:11 +08002753 // Load *this* actual parameter
Logan Chien82d31cd2012-05-23 18:37:44 +08002754 uint32_t this_reg = -1u;
Logan Chien1a121b92012-02-15 22:23:42 +08002755 llvm::Value* this_addr = NULL;
2756
2757 if (!is_static) {
2758 // Test: Is *this* parameter equal to null?
Logan Chien82d31cd2012-05-23 18:37:44 +08002759 this_reg = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2760 this_addr = EmitLoadDalvikReg(this_reg, kObject, kAccurate);
Logan Chien1a121b92012-02-15 22:23:42 +08002761 }
2762
Logan Chien7e7fabc2012-04-10 18:59:11 +08002763 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002764 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002765
2766 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002767 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002768 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2769 this_addr, dex_pc, is_fast_path);
Logan Chien82d31cd2012-05-23 18:37:44 +08002770
2771 if (!is_static && (!method_info_.this_will_not_be_null ||
2772 this_reg != method_info_.this_reg_idx)) {
2773 // NOTE: The null pointer test should come after the method resolution.
2774 // So that the "NoSuchMethodError" can be thrown before the
2775 // "NullPointerException".
2776 EmitGuard_NullPointerException(dex_pc, this_addr);
2777 }
2778
Logan Chien7e7fabc2012-04-10 18:59:11 +08002779 } else {
Logan Chien82d31cd2012-05-23 18:37:44 +08002780 if (!is_static && (!method_info_.this_will_not_be_null ||
2781 this_reg != method_info_.this_reg_idx)) {
2782 // NOTE: In the fast path, we should do the null pointer check
2783 // before the access to the class object and/or direct invocation.
2784 EmitGuard_NullPointerException(dex_pc, this_addr);
2785 }
2786
Logan Chien7e7fabc2012-04-10 18:59:11 +08002787 switch (invoke_type) {
2788 case kStatic:
2789 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002790 if (direct_method != 0u &&
2791 direct_method != static_cast<uintptr_t>(-1)) {
2792 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002793 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2794 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002795 } else {
2796 callee_method_object_addr =
2797 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2798 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002799 break;
2800
2801 case kVirtual:
2802 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002803 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002804 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2805 break;
2806
2807 case kSuper:
2808 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2809 "the fast path.";
2810 break;
2811
2812 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002813 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002814 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2815 invoke_type, this_addr,
2816 dex_pc, is_fast_path);
2817 break;
2818 }
2819 }
2820
Logan Chien1a121b92012-02-15 22:23:42 +08002821 // Load the actual parameter
2822 std::vector<llvm::Value*> args;
2823
2824 args.push_back(callee_method_object_addr); // method object for callee
2825
2826 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002827 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002828 args.push_back(this_addr); // "this" object for callee
2829 }
2830
2831 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002832 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002833
TDYa12729c0cd12012-05-17 04:51:08 -07002834 if (is_fast_path && (invoke_type == kDirect || invoke_type == kStatic)) {
2835 bool need_retry = EmitInlineJavaIntrinsic(PrettyMethod(callee_method_idx, *dex_file_),
2836 args,
2837 GetNextBasicBlock(dex_pc));
2838 if (!need_retry) {
2839 return;
2840 }
2841 }
2842
2843 llvm::Value* code_addr =
2844 irb_.LoadFromObjectOffset(callee_method_object_addr,
2845 Method::GetCodeOffset().Int32Value(),
2846 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
2847 kTBAAJRuntime);
2848
TDYa1275bb86012012-04-11 05:57:28 -07002849#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002850 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002851 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002852 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa127526643e2012-05-26 01:01:48 -07002853 EmitGuard_ExceptionLandingPad(dex_pc, true);
Logan Chien1a121b92012-02-15 22:23:42 +08002854
Logan Chien7e7fabc2012-04-10 18:59:11 +08002855 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2856 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2857 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2858
2859 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002860 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002861 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2862 }
TDYa1275bb86012012-04-11 05:57:28 -07002863#else
2864 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2865 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2866 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2867
2868 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2869
2870
TDYa127c8dc1012012-04-19 07:03:33 -07002871 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002872
2873
2874 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002875 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002876 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2877
TDYa127eead4ac2012-06-03 07:15:25 -07002878 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002879
2880
2881 irb_.SetInsertPoint(block_normal);
2882 {
2883 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002884 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002885 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002886 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002887 }
2888 }
2889 irb_.CreateBr(block_continue);
2890
2891
TDYa127ce154722012-04-21 16:43:29 -07002892 irb_.SetInsertPoint(block_stub);
TDYa127eead4ac2012-06-03 07:15:25 -07002893 { // lazy link
2894 // TODO: Remove this after we solve the link problem.
2895 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
TDYa127ce154722012-04-21 16:43:29 -07002896
TDYa127eead4ac2012-06-03 07:15:25 -07002897 EmitGuard_ExceptionLandingPad(dex_pc, false);
TDYa127ce154722012-04-21 16:43:29 -07002898
TDYa127eead4ac2012-06-03 07:15:25 -07002899 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2900 if (ret_shorty != 'V') {
2901 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002902 }
2903 }
2904 irb_.CreateBr(block_continue);
2905
2906
2907 irb_.SetInsertPoint(block_continue);
2908
TDYa127526643e2012-05-26 01:01:48 -07002909 EmitGuard_ExceptionLandingPad(dex_pc, true);
TDYa1275bb86012012-04-11 05:57:28 -07002910#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002911
Logan Chien70f94b42011-12-27 17:49:11 +08002912 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2913}
2914
2915
Logan Chien7e7fabc2012-04-10 18:59:11 +08002916llvm::Value* MethodCompiler::
2917EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2918 llvm::Value* callee_method_object_field_addr =
2919 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002920
TDYa1278ca10052012-05-05 19:57:06 -07002921 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002922}
Logan Chien7caf37e2012-02-03 22:56:04 +08002923
Logan Chien7caf37e2012-02-03 22:56:04 +08002924
Logan Chien7e7fabc2012-04-10 18:59:11 +08002925llvm::Value* MethodCompiler::
2926EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2927 llvm::Value* this_addr) {
2928 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002929 llvm::Value* class_object_addr =
2930 irb_.LoadFromObjectOffset(this_addr,
2931 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002932 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002933 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002934
Logan Chien7e7fabc2012-04-10 18:59:11 +08002935 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002936 llvm::Value* vtable_addr =
2937 irb_.LoadFromObjectOffset(class_object_addr,
2938 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002939 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002940 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002941
2942 // Load callee method object
2943 llvm::Value* vtable_idx_value =
2944 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2945
2946 llvm::Value* method_field_addr =
TDYa1278db6ea32012-05-17 04:48:42 -07002947 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002948
TDYa127d3e24c22012-05-05 20:54:19 -07002949 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002950}
2951
2952
2953llvm::Value* MethodCompiler::
2954EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2955 InvokeType invoke_type,
2956 llvm::Value* this_addr,
2957 uint32_t dex_pc,
2958 bool is_fast_path) {
2959
2960 llvm::Function* runtime_func = NULL;
2961
2962 switch (invoke_type) {
2963 case kStatic:
2964 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2965 break;
2966
2967 case kDirect:
2968 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2969 break;
2970
2971 case kVirtual:
2972 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2973 break;
2974
2975 case kSuper:
2976 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2977 break;
2978
2979 case kInterface:
2980 if (is_fast_path) {
2981 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2982 } else {
2983 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
2984 }
2985 break;
2986 }
Logan Chien7caf37e2012-02-03 22:56:04 +08002987
2988 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2989
Logan Chien7e7fabc2012-04-10 18:59:11 +08002990 if (this_addr == NULL) {
2991 DCHECK_EQ(invoke_type, kStatic);
2992 this_addr = irb_.getJNull();
2993 }
2994
2995 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2996
TDYa127de479be2012-05-31 08:03:26 -07002997 llvm::Value* thread_object_addr = irb_.Runtime().EmitGetCurrentThread();
TDYa127da83d972012-04-18 00:21:49 -07002998
TDYa127c8dc1012012-04-19 07:03:33 -07002999 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003000
TDYa1270b686e52012-04-09 22:43:35 -07003001 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003002 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003003 callee_method_idx_value,
3004 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003005 caller_method_object_addr,
3006 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003007
TDYa127526643e2012-05-26 01:01:48 -07003008 EmitGuard_ExceptionLandingPad(dex_pc, false);
Logan Chien7caf37e2012-02-03 22:56:04 +08003009
Logan Chien7e7fabc2012-04-10 18:59:11 +08003010 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003011}
3012
3013
3014void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3015 Instruction const* insn,
3016 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003017
Elliott Hughesadb8c672012-03-06 16:49:32 -08003018 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003019
3020 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3021
Elliott Hughesadb8c672012-03-06 16:49:32 -08003022 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003023 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003024 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003025
Logan Chien70f94b42011-12-27 17:49:11 +08003026 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3027}
3028
3029
3030void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3031 Instruction const* insn,
3032 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003033
Elliott Hughesadb8c672012-03-06 16:49:32 -08003034 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003035
3036 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3037
Elliott Hughesadb8c672012-03-06 16:49:32 -08003038 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003039 llvm::Value* result_value =
3040 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3041
Elliott Hughesadb8c672012-03-06 16:49:32 -08003042 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003043
Logan Chien70f94b42011-12-27 17:49:11 +08003044 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3045}
3046
3047
3048void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3049 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003050
Elliott Hughesadb8c672012-03-06 16:49:32 -08003051 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003052
Elliott Hughesadb8c672012-03-06 16:49:32 -08003053 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003054 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003056
Logan Chien70f94b42011-12-27 17:49:11 +08003057 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3058}
3059
3060
3061void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3062 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003063
Elliott Hughesadb8c672012-03-06 16:49:32 -08003064 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003065
Elliott Hughesadb8c672012-03-06 16:49:32 -08003066 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003067 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003069
Logan Chien70f94b42011-12-27 17:49:11 +08003070 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3071}
3072
3073
3074void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3075 Instruction const* insn,
3076 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003077
Elliott Hughesadb8c672012-03-06 16:49:32 -08003078 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003079
Elliott Hughesadb8c672012-03-06 16:49:32 -08003080 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003081
3082 llvm::Value* trunc_value =
3083 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3084
3085 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3086
Elliott Hughesadb8c672012-03-06 16:49:32 -08003087 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003088
Logan Chien70f94b42011-12-27 17:49:11 +08003089 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3090}
3091
3092
3093void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3094 Instruction const* insn,
3095 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003096
Elliott Hughesadb8c672012-03-06 16:49:32 -08003097 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003098
Elliott Hughesadb8c672012-03-06 16:49:32 -08003099 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003100
3101 llvm::Value* trunc_value =
3102 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3103
3104 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3105
Elliott Hughesadb8c672012-03-06 16:49:32 -08003106 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003107
Logan Chien70f94b42011-12-27 17:49:11 +08003108 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3109}
3110
3111
3112void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3113 Instruction const* insn,
3114 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003115
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003117
3118 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3119
Elliott Hughesadb8c672012-03-06 16:49:32 -08003120 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003121 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003122 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003123
Logan Chien70f94b42011-12-27 17:49:11 +08003124 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3125}
3126
3127
3128void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3129 Instruction const* insn,
3130 JType src_jty,
3131 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003132
Elliott Hughesadb8c672012-03-06 16:49:32 -08003133 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003134
3135 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3136 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3137
Elliott Hughesadb8c672012-03-06 16:49:32 -08003138 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003139 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3140 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003141 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003142
Logan Chien70f94b42011-12-27 17:49:11 +08003143 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3144}
3145
3146
3147void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3148 Instruction const* insn,
3149 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003150 JType dest_jty,
3151 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003152
Elliott Hughesadb8c672012-03-06 16:49:32 -08003153 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003154
3155 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3156 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3157
Elliott Hughesadb8c672012-03-06 16:49:32 -08003158 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003159 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003160 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003161
Logan Chien70f94b42011-12-27 17:49:11 +08003162 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3163}
3164
3165
3166void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3167 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003168
Elliott Hughesadb8c672012-03-06 16:49:32 -08003169 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003170
Elliott Hughesadb8c672012-03-06 16:49:32 -08003171 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003172 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003174
Logan Chien70f94b42011-12-27 17:49:11 +08003175 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3176}
3177
3178
3179void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3180 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003181
Elliott Hughesadb8c672012-03-06 16:49:32 -08003182 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003183
Elliott Hughesadb8c672012-03-06 16:49:32 -08003184 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003185 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003186 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003187
Logan Chien70f94b42011-12-27 17:49:11 +08003188 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3189}
3190
3191
3192void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3193 Instruction const* insn,
3194 IntArithmKind arithm,
3195 JType op_jty,
3196 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003197
Elliott Hughesadb8c672012-03-06 16:49:32 -08003198 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003199
3200 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3201
3202 llvm::Value* src1_value;
3203 llvm::Value* src2_value;
3204
3205 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3207 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003208 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003209 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3210 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003211 }
3212
3213 llvm::Value* result_value =
3214 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3215 arithm, op_jty);
3216
Elliott Hughesadb8c672012-03-06 16:49:32 -08003217 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003218
Logan Chien70f94b42011-12-27 17:49:11 +08003219 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3220}
3221
3222
3223void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3224 Instruction const* insn,
3225 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003226
Elliott Hughesadb8c672012-03-06 16:49:32 -08003227 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003228
Elliott Hughesadb8c672012-03-06 16:49:32 -08003229 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003230
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003232
3233 llvm::Value* result_value =
3234 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3235
Elliott Hughesadb8c672012-03-06 16:49:32 -08003236 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003237
Logan Chien70f94b42011-12-27 17:49:11 +08003238 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3239}
3240
3241
Logan Chienc3f7d962011-12-27 18:13:18 +08003242llvm::Value*
3243MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3244 llvm::Value* lhs,
3245 llvm::Value* rhs,
3246 IntArithmKind arithm,
3247 JType op_jty) {
3248 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3249
3250 switch (arithm) {
3251 case kIntArithm_Add:
3252 return irb_.CreateAdd(lhs, rhs);
3253
3254 case kIntArithm_Sub:
3255 return irb_.CreateSub(lhs, rhs);
3256
3257 case kIntArithm_Mul:
3258 return irb_.CreateMul(lhs, rhs);
3259
3260 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003261 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003262 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003263
3264 case kIntArithm_And:
3265 return irb_.CreateAnd(lhs, rhs);
3266
3267 case kIntArithm_Or:
3268 return irb_.CreateOr(lhs, rhs);
3269
3270 case kIntArithm_Xor:
3271 return irb_.CreateXor(lhs, rhs);
3272
Logan Chienc3f7d962011-12-27 18:13:18 +08003273 default:
3274 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3275 return NULL;
3276 }
3277}
3278
3279
TDYa127f8641ce2012-04-02 06:40:40 -07003280llvm::Value*
3281MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3282 llvm::Value* dividend,
3283 llvm::Value* divisor,
3284 IntArithmKind arithm,
3285 JType op_jty) {
3286 // Throw exception if the divisor is 0.
3287 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3288
3289 // Check the special case: MININT / -1 = MININT
3290 // That case will cause overflow, which is undefined behavior in llvm.
3291 // So we check the divisor is -1 or not, if the divisor is -1, we do
3292 // the special path to avoid undefined behavior.
3293 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3294 llvm::Value* zero = irb_.getJZero(op_jty);
3295 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3296 llvm::Value* result = irb_.CreateAlloca(op_type);
3297
3298 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3299 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3300 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3301
3302 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003303 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003304
3305 // If divisor == -1
3306 irb_.SetInsertPoint(eq_neg_one);
3307 llvm::Value* eq_result;
3308 if (arithm == kIntArithm_Div) {
3309 // We can just change from "dividend div -1" to "neg dividend".
3310 // The sub don't care the sign/unsigned because of two's complement representation.
3311 // And the behavior is what we want:
3312 // -(2^n) (2^n)-1
3313 // MININT < k <= MAXINT -> mul k -1 = -k
3314 // MININT == k -> mul k -1 = k
3315 //
3316 // LLVM use sub to represent 'neg'
3317 eq_result = irb_.CreateSub(zero, dividend);
3318 } else {
3319 // Everything modulo -1 will be 0.
3320 eq_result = zero;
3321 }
TDYa127aba61122012-05-04 18:28:36 -07003322 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003323 irb_.CreateBr(neg_one_cont);
3324
3325 // If divisor != -1, just do the division.
3326 irb_.SetInsertPoint(ne_neg_one);
3327 llvm::Value* ne_result;
3328 if (arithm == kIntArithm_Div) {
3329 ne_result = irb_.CreateSDiv(dividend, divisor);
3330 } else {
3331 ne_result = irb_.CreateSRem(dividend, divisor);
3332 }
TDYa127aba61122012-05-04 18:28:36 -07003333 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003334 irb_.CreateBr(neg_one_cont);
3335
3336 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003337 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003338}
3339
3340
Logan Chien5539ad02012-04-02 14:36:55 +08003341void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3342 Instruction const* insn,
3343 IntShiftArithmKind arithm,
3344 JType op_jty,
3345 bool is_2addr) {
3346
3347 DecodedInstruction dec_insn(insn);
3348
3349 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3350
3351 llvm::Value* src1_value;
3352 llvm::Value* src2_value;
3353
3354 // NOTE: The 2nd operand of the shift arithmetic instruction is
3355 // 32-bit integer regardless of the 1st operand.
3356 if (is_2addr) {
3357 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3358 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3359 } else {
3360 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3361 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3362 }
3363
3364 llvm::Value* result_value =
3365 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3366 arithm, op_jty);
3367
3368 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3369
3370 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3371}
3372
3373
3374void MethodCompiler::
3375EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3376 Instruction const* insn,
3377 IntShiftArithmKind arithm) {
3378
3379 DecodedInstruction dec_insn(insn);
3380
3381 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3382
3383 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3384
3385 llvm::Value* result_value =
3386 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3387 arithm, kInt);
3388
3389 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3390
3391 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3392}
3393
3394
3395llvm::Value*
3396MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3397 llvm::Value* lhs,
3398 llvm::Value* rhs,
3399 IntShiftArithmKind arithm,
3400 JType op_jty) {
3401 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3402
3403 if (op_jty == kInt) {
3404 rhs = irb_.CreateAnd(rhs, 0x1f);
3405 } else {
3406 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3407 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3408 }
3409
3410 switch (arithm) {
3411 case kIntArithm_Shl:
3412 return irb_.CreateShl(lhs, rhs);
3413
3414 case kIntArithm_Shr:
3415 return irb_.CreateAShr(lhs, rhs);
3416
3417 case kIntArithm_UShr:
3418 return irb_.CreateLShr(lhs, rhs);
3419
3420 default:
3421 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3422 return NULL;
3423 }
3424}
3425
3426
Logan Chien70f94b42011-12-27 17:49:11 +08003427void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3428 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003429
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003431
Elliott Hughesadb8c672012-03-06 16:49:32 -08003432 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3433 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003434 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003435 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003436
Logan Chien70f94b42011-12-27 17:49:11 +08003437 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3438}
3439
3440
3441void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3442 Instruction const* insn,
3443 FPArithmKind arithm,
3444 JType op_jty,
3445 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003446
Elliott Hughesadb8c672012-03-06 16:49:32 -08003447 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003448
3449 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3450
3451 llvm::Value* src1_value;
3452 llvm::Value* src2_value;
3453
3454 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3456 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003457 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003458 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3459 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003460 }
3461
3462 llvm::Value* result_value =
3463 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3464
Elliott Hughesadb8c672012-03-06 16:49:32 -08003465 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003466
Logan Chien70f94b42011-12-27 17:49:11 +08003467 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3468}
3469
3470
Logan Chien76e1c792011-12-27 18:15:01 +08003471llvm::Value*
3472MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3473 llvm::Value *lhs,
3474 llvm::Value *rhs,
3475 FPArithmKind arithm) {
3476 switch (arithm) {
3477 case kFPArithm_Add:
3478 return irb_.CreateFAdd(lhs, rhs);
3479
3480 case kFPArithm_Sub:
3481 return irb_.CreateFSub(lhs, rhs);
3482
3483 case kFPArithm_Mul:
3484 return irb_.CreateFMul(lhs, rhs);
3485
3486 case kFPArithm_Div:
3487 return irb_.CreateFDiv(lhs, rhs);
3488
3489 case kFPArithm_Rem:
3490 return irb_.CreateFRem(lhs, rhs);
3491
3492 default:
3493 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3494 return NULL;
3495 }
3496}
3497
3498
Logan Chienc3f7d962011-12-27 18:13:18 +08003499void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3500 llvm::Value* denominator,
3501 JType op_jty) {
3502 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3503
3504 llvm::Constant* zero = irb_.getJZero(op_jty);
3505
3506 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3507
3508 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3509
3510 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3511
TDYa127ac7b5bb2012-05-11 13:17:49 -07003512 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003513
3514 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003515 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003516 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3517 EmitBranchExceptionLandingPad(dex_pc);
3518
3519 irb_.SetInsertPoint(block_continue);
3520}
3521
3522
Logan Chien61bb6142012-02-03 15:34:53 +08003523void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3524 llvm::Value* object) {
3525 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3526
3527 llvm::BasicBlock* block_exception =
3528 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3529
3530 llvm::BasicBlock* block_continue =
3531 CreateBasicBlockWithDexPC(dex_pc, "cont");
3532
TDYa127ac7b5bb2012-05-11 13:17:49 -07003533 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003534
3535 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003536 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003537 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003538 EmitBranchExceptionLandingPad(dex_pc);
3539
3540 irb_.SetInsertPoint(block_continue);
3541}
3542
3543
Logan Chienbb4d12a2012-02-17 14:10:01 +08003544llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3545 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3546
TDYa127ee1f59b2012-04-25 00:56:40 -07003547 return irb_.LoadFromObjectOffset(method_object_addr,
3548 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003549 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003550 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003551}
3552
3553
Logan Chienbb4d12a2012-02-17 14:10:01 +08003554llvm::Value* MethodCompiler::
3555EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3556 llvm::Value* static_storage_dex_cache_addr =
3557 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3558
3559 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3560
TDYa1278db6ea32012-05-17 04:48:42 -07003561 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003562}
3563
3564
3565llvm::Value* MethodCompiler::
3566EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3567 llvm::Value* resolved_type_dex_cache_addr =
3568 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3569
3570 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3571
TDYa1278db6ea32012-05-17 04:48:42 -07003572 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003573}
3574
3575
3576llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003577EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3578 llvm::Value* resolved_method_dex_cache_addr =
3579 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3580
3581 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3582
TDYa1278db6ea32012-05-17 04:48:42 -07003583 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003584}
3585
3586
3587llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003588EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3589 llvm::Value* string_dex_cache_addr =
3590 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3591
3592 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3593
TDYa1278db6ea32012-05-17 04:48:42 -07003594 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003595}
3596
3597
Logan Chien83426162011-12-09 09:29:50 +08003598CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003599 // TODO: Use high-level IR to do this
3600 // Compute method info
3601 ComputeMethodInfo();
3602
Logan Chien0b827102011-12-20 19:46:14 +08003603 // Code generation
3604 CreateFunction();
3605
3606 EmitPrologue();
3607 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003608 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003609
Logan Chiend6c239a2011-12-23 15:11:45 +08003610 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003611 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003612
Logan Chien8b977d32012-02-21 19:14:55 +08003613 // Add the memory usage approximation of the compilation unit
3614 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003615 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003616 // Dex file. Besides, we have to convert the code unit into bytes.
3617 // Thus, we got our magic number 9.
3618
Logan Chien110bcba2012-04-16 19:11:28 +08003619 CompiledMethod* compiled_method =
3620 new CompiledMethod(cunit_->GetInstructionSet(),
3621 cunit_->GetElfIndex(),
3622 elf_func_idx_);
3623
3624 cunit_->RegisterCompiledMethod(func_, compiled_method);
3625
3626 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003627}
3628
3629
3630llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3631 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003632}
Logan Chien83426162011-12-09 09:29:50 +08003633
3634
Logan Chien5bcc04e2012-01-30 14:15:12 +08003635void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3636 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3637 irb_.CreateBr(lpad);
3638 } else {
3639 irb_.CreateBr(GetUnwindBasicBlock());
3640 }
3641}
3642
3643
TDYa127526643e2012-05-26 01:01:48 -07003644void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc, bool can_skip_unwind) {
3645 llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc);
3646 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3647 if (lpad == NULL && can_skip_unwind &&
3648 IsInstructionDirectToReturn(dex_pc + insn->SizeInCodeUnits())) {
3649 return;
3650 }
3651
TDYa127de479be2012-05-31 08:03:26 -07003652 llvm::Value* exception_pending = irb_.Runtime().EmitIsExceptionPending();
Logan Chien5bcc04e2012-01-30 14:15:12 +08003653
3654 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3655
TDYa127526643e2012-05-26 01:01:48 -07003656 if (lpad) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003657 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003658 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003659 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003660 }
3661
3662 irb_.SetInsertPoint(block_cont);
3663}
3664
3665
TDYa127526643e2012-05-26 01:01:48 -07003666void MethodCompiler::EmitGuard_GarbageCollectionSuspend() {
3667 // Loop suspend will be added by our llvm pass.
3668 if (!method_info_.has_invoke) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003669 return;
3670 }
3671
TDYa127de479be2012-05-31 08:03:26 -07003672 irb_.Runtime().EmitTestSuspend();
Logan Chien924072f2012-01-30 15:07:24 +08003673}
3674
3675
Logan Chiend6c239a2011-12-23 15:11:45 +08003676llvm::BasicBlock* MethodCompiler::
3677CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3678 std::string name;
3679
TDYa12767ae8ff2012-05-02 19:08:02 -07003680#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003681 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003682 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003683 } else {
TDYa12799489132012-04-29 01:27:58 -07003684 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003685 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003686#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003687
3688 return llvm::BasicBlock::Create(*context_, name, func_);
3689}
3690
3691
3692llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3693 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3694
3695 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3696
3697 if (!basic_block) {
3698 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3699 basic_blocks_[dex_pc] = basic_block;
3700 }
3701
3702 return basic_block;
3703}
3704
3705
3706llvm::BasicBlock*
3707MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3708 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3709 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3710}
3711
3712
Logan Chien5bcc04e2012-01-30 14:15:12 +08003713int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3714 // TODO: Since we are emitting the dex instructions in ascending order
3715 // w.r.t. address, we can cache the lastest try item offset so that we
3716 // don't have to do binary search for every query.
3717
3718 int32_t min = 0;
3719 int32_t max = code_item_->tries_size_ - 1;
3720
3721 while (min <= max) {
3722 int32_t mid = min + (max - min) / 2;
3723
3724 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3725 uint32_t start = ti->start_addr_;
3726 uint32_t end = start + ti->insn_count_;
3727
3728 if (dex_pc < start) {
3729 max = mid - 1;
3730 } else if (dex_pc >= end) {
3731 min = mid + 1;
3732 } else {
3733 return mid; // found
3734 }
3735 }
3736
3737 return -1; // not found
3738}
3739
3740
3741llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3742 // Find the try item for this address in this method
3743 int32_t ti_offset = GetTryItemOffset(dex_pc);
3744
3745 if (ti_offset == -1) {
3746 return NULL; // No landing pad is available for this address.
3747 }
3748
3749 // Check for the existing landing pad basic block
3750 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3751 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3752
3753 if (block_lpad) {
3754 // We have generated landing pad for this try item already. Return the
3755 // same basic block.
3756 return block_lpad;
3757 }
3758
3759 // Get try item from code item
3760 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3761
TDYa12767ae8ff2012-05-02 19:08:02 -07003762 std::string lpadname;
3763
3764#if !defined(NDEBUG)
3765 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3766#endif
3767
Logan Chien5bcc04e2012-01-30 14:15:12 +08003768 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003769 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003770
3771 // Change IRBuilder insert point
3772 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3773 irb_.SetInsertPoint(block_lpad);
3774
3775 // Find catch block with matching type
3776 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3777
Logan Chien736df022012-04-27 16:25:57 +08003778 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003779
3780 llvm::Value* catch_handler_index_value =
3781 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003782 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003783
3784 // Switch instruction (Go to unwind basic block by default)
3785 llvm::SwitchInst* sw =
3786 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3787
3788 // Cases with matched catch block
3789 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3790
3791 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3792 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3793 }
3794
3795 // Restore the orignal insert point for IRBuilder
3796 irb_.restoreIP(irb_ip_original);
3797
3798 // Cache this landing pad
3799 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3800 basic_block_landing_pads_[ti_offset] = block_lpad;
3801
3802 return block_lpad;
3803}
3804
3805
3806llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3807 // Check the existing unwinding baisc block block
3808 if (basic_block_unwind_ != NULL) {
3809 return basic_block_unwind_;
3810 }
3811
3812 // Create new basic block for unwinding
3813 basic_block_unwind_ =
3814 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3815
3816 // Change IRBuilder insert point
3817 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3818 irb_.SetInsertPoint(basic_block_unwind_);
3819
Logan Chien8dfcbea2012-02-17 18:50:32 +08003820 // Pop the shadow frame
3821 EmitPopShadowFrame();
3822
Logan Chien5bcc04e2012-01-30 14:15:12 +08003823 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003824 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003825 if (ret_shorty == 'V') {
3826 irb_.CreateRetVoid();
3827 } else {
3828 irb_.CreateRet(irb_.getJZero(ret_shorty));
3829 }
3830
3831 // Restore the orignal insert point for IRBuilder
3832 irb_.restoreIP(irb_ip_original);
3833
3834 return basic_block_unwind_;
3835}
3836
3837
TDYa127e2102142012-05-26 10:27:38 -07003838llvm::Value* MethodCompiler::AllocDalvikReg(RegCategory cat, const std::string& name) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003839 // Get reg_type and reg_name from DalvikReg
3840 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3841 std::string reg_name;
3842
3843#if !defined(NDEBUG)
TDYa127e2102142012-05-26 10:27:38 -07003844 StringAppendF(&reg_name, "%c%s", DalvikReg::GetRegCategoryNamePrefix(cat), name.c_str());
TDYa12767ae8ff2012-05-02 19:08:02 -07003845#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003846
3847 // Save current IR builder insert point
3848 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa127b9ff6b12012-05-17 11:14:29 -07003849 irb_.SetInsertPoint(basic_block_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003850
3851 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003852 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003853
3854 // Restore IRBuilder insert point
3855 irb_.restoreIP(irb_ip_original);
3856
3857 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3858 return reg_addr;
3859}
3860
3861
TDYa127e2102142012-05-26 10:27:38 -07003862llvm::Value* MethodCompiler::GetShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003863 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3864 // This register dosen't need ShadowFrame entry
3865 return NULL;
3866 }
3867
TDYa127cc1b4c32012-05-15 07:31:37 -07003868 if (!method_info_.need_shadow_frame_entry) {
3869 return NULL;
3870 }
3871
TDYa12767ae8ff2012-05-02 19:08:02 -07003872 std::string reg_name;
3873
3874#if !defined(NDEBUG)
TDYa127e2102142012-05-26 10:27:38 -07003875 StringAppendF(&reg_name, "s%u", reg_idx);
TDYa12767ae8ff2012-05-02 19:08:02 -07003876#endif
3877
TDYa1277f5b9be2012-04-29 01:31:49 -07003878 // Save current IR builder insert point
3879 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3880
TDYa127b9ff6b12012-05-17 11:14:29 -07003881 irb_.SetInsertPoint(basic_block_shadow_frame_);
TDYa1277f5b9be2012-04-29 01:31:49 -07003882
3883 llvm::Value* gep_index[] = {
3884 irb_.getInt32(0), // No pointer displacement
3885 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003886 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003887 };
3888
TDYa12767ae8ff2012-05-02 19:08:02 -07003889 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003890
3891 // Restore IRBuilder insert point
3892 irb_.restoreIP(irb_ip_original);
3893
3894 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3895 return reg_addr;
3896}
3897
3898
TDYa127af543472012-05-28 21:49:23 -07003899void MethodCompiler::EmitPushShadowFrame(bool is_inline) {
3900 if (!method_info_.need_shadow_frame) {
3901 return;
3902 }
3903 DCHECK(shadow_frame_ != NULL);
3904 DCHECK(old_shadow_frame_ != NULL);
3905
3906 // Get method object
3907 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3908
3909 // Push the shadow frame
3910 llvm::Value* shadow_frame_upcast =
3911 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
3912
TDYa127de479be2012-05-31 08:03:26 -07003913 llvm::Value* result;
3914 if (is_inline) {
3915 result = irb_.Runtime().EmitPushShadowFrame(shadow_frame_upcast, method_object_addr,
3916 shadow_frame_size_);
3917 } else {
3918 DCHECK(shadow_frame_size_ == 0);
3919 result = irb_.Runtime().EmitPushShadowFrameNoInline(shadow_frame_upcast, method_object_addr,
3920 shadow_frame_size_);
3921 }
TDYa127af543472012-05-28 21:49:23 -07003922 irb_.CreateStore(result, old_shadow_frame_, kTBAARegister);
3923}
3924
3925
Logan Chien8dfcbea2012-02-17 18:50:32 +08003926void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003927 if (!method_info_.need_shadow_frame) {
3928 return;
3929 }
TDYa1270de52be2012-05-27 20:49:31 -07003930 DCHECK(old_shadow_frame_ != NULL);
TDYa127af543472012-05-28 21:49:23 -07003931
3932 if (method_info_.lazy_push_shadow_frame) {
3933 llvm::BasicBlock* bb_pop = llvm::BasicBlock::Create(*context_, "pop", func_);
3934 llvm::BasicBlock* bb_cont = llvm::BasicBlock::Create(*context_, "cont", func_);
3935
3936 llvm::Value* need_pop = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister);
3937 irb_.CreateCondBr(need_pop, bb_pop, bb_cont, kUnlikely);
3938
3939 irb_.SetInsertPoint(bb_pop);
TDYa127de479be2012-05-31 08:03:26 -07003940 irb_.Runtime().EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
TDYa127af543472012-05-28 21:49:23 -07003941 irb_.CreateBr(bb_cont);
3942
3943 irb_.SetInsertPoint(bb_cont);
3944 } else {
TDYa127de479be2012-05-31 08:03:26 -07003945 irb_.Runtime().EmitPopShadowFrame(irb_.CreateLoad(old_shadow_frame_, kTBAARegister));
TDYa127af543472012-05-28 21:49:23 -07003946 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003947}
3948
3949
TDYa127c8dc1012012-04-19 07:03:33 -07003950void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003951 if (!method_info_.need_shadow_frame) {
3952 return;
3953 }
TDYa127c8dc1012012-04-19 07:03:33 -07003954 irb_.StoreToObjectOffset(shadow_frame_,
3955 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003956 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003957 kTBAAShadowFrame);
TDYa127af543472012-05-28 21:49:23 -07003958 // Lazy pushing shadow frame
3959 if (method_info_.lazy_push_shadow_frame) {
3960 llvm::BasicBlock* bb_push = CreateBasicBlockWithDexPC(dex_pc, "push");
3961 llvm::BasicBlock* bb_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3962
3963 llvm::Value* no_need_push = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister);
3964 irb_.CreateCondBr(no_need_push, bb_cont, bb_push, kLikely);
3965
3966 irb_.SetInsertPoint(bb_push);
3967 EmitPushShadowFrame(false);
3968 irb_.CreateStore(irb_.getTrue(), already_pushed_shadow_frame_, kTBAARegister);
3969 irb_.CreateBr(bb_cont);
3970
3971 irb_.SetInsertPoint(bb_cont);
3972 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003973}
3974
3975
TDYa127e2102142012-05-26 10:27:38 -07003976llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
3977 JTypeSpace space) {
3978 return regs_[reg_idx]->GetValue(jty, space);
3979}
3980
3981llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
3982 JTypeSpace space) {
3983 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
3984}
3985
3986void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
3987 JTypeSpace space, llvm::Value* new_value) {
3988 regs_[reg_idx]->SetValue(jty, space, new_value);
3989 if (jty == kObject && shadow_frame_entries_[reg_idx] != NULL) {
3990 irb_.CreateStore(new_value, shadow_frame_entries_[reg_idx], kTBAAShadowFrame);
3991 }
3992}
3993
3994void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
3995 JTypeSpace space, llvm::Value* new_value) {
3996 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
3997}
3998
3999llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
4000 return retval_reg_->GetValue(jty, space);
4001}
4002
4003llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
4004 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
4005}
4006
4007void MethodCompiler::EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
4008 llvm::Value* new_value) {
4009 retval_reg_->SetValue(jty, space, new_value);
4010}
4011
4012void MethodCompiler::EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
4013 llvm::Value* new_value) {
4014 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
4015}
4016
4017
TDYa127cc1b4c32012-05-15 07:31:37 -07004018// TODO: Use high-level IR to do this
TDYa12729c0cd12012-05-17 04:51:08 -07004019bool MethodCompiler::EmitInlineJavaIntrinsic(const std::string& callee_method_name,
4020 const std::vector<llvm::Value*>& args,
4021 llvm::BasicBlock* after_invoke) {
4022 if (callee_method_name == "char java.lang.String.charAt(int)") {
4023 return EmitInlinedStringCharAt(args, after_invoke);
4024 }
4025 if (callee_method_name == "int java.lang.String.length()") {
4026 return EmitInlinedStringLength(args, after_invoke);
4027 }
TDYa127d4f82b62012-06-02 21:48:09 -07004028 if (callee_method_name == "int java.lang.String.indexOf(int, int)") {
4029 return EmitInlinedStringIndexOf(args, after_invoke, false /* base 0 */);
4030 }
4031 if (callee_method_name == "int java.lang.String.indexOf(int)") {
4032 return EmitInlinedStringIndexOf(args, after_invoke, true /* base 0 */);
4033 }
4034 if (callee_method_name == "int java.lang.String.compareTo(java.lang.String)") {
4035 return EmitInlinedStringCompareTo(args, after_invoke);
4036 }
TDYa12729c0cd12012-05-17 04:51:08 -07004037 return true;
4038}
4039
4040bool MethodCompiler::EmitInlinedStringCharAt(const std::vector<llvm::Value*>& args,
4041 llvm::BasicBlock* after_invoke) {
4042 DCHECK_EQ(args.size(), 3U) <<
4043 "char java.lang.String.charAt(int) has 3 args: method, this, char_index";
4044 llvm::Value* this_object = args[1];
4045 llvm::Value* char_index = args[2];
4046 llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "CharAtRetry", func_);
4047 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "CharAtCont", func_);
4048
TDYa12729c0cd12012-05-17 04:51:08 -07004049 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
4050 String::CountOffset().Int32Value(),
4051 irb_.getJIntTy(),
TDYa127d4f82b62012-06-02 21:48:09 -07004052 kTBAAConstJObject);
TDYa12729c0cd12012-05-17 04:51:08 -07004053 // Two's complement, so we can use only one "less than" to check "in bounds"
4054 llvm::Value* in_bounds = irb_.CreateICmpULT(char_index, string_count);
4055 irb_.CreateCondBr(in_bounds, block_cont, block_retry, kLikely);
4056
4057 irb_.SetInsertPoint(block_cont);
TDYa12729c0cd12012-05-17 04:51:08 -07004058 llvm::Value* string_offset = irb_.LoadFromObjectOffset(this_object,
4059 String::OffsetOffset().Int32Value(),
4060 irb_.getJIntTy(),
TDYa127d4f82b62012-06-02 21:48:09 -07004061 kTBAAConstJObject);
TDYa12729c0cd12012-05-17 04:51:08 -07004062 llvm::Value* string_value = irb_.LoadFromObjectOffset(this_object,
4063 String::ValueOffset().Int32Value(),
4064 irb_.getJObjectTy(),
TDYa127d4f82b62012-06-02 21:48:09 -07004065 kTBAAConstJObject);
TDYa12729c0cd12012-05-17 04:51:08 -07004066
4067 // index_value = string.offset + char_index
4068 llvm::Value* index_value = irb_.CreateAdd(string_offset, char_index);
4069
4070 // array_elem_value = string.value[index_value]
4071 llvm::Value* array_elem_addr = EmitArrayGEP(string_value, index_value, kChar);
4072 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, kChar);
4073
4074 EmitStoreDalvikRetValReg(kChar, kArray, array_elem_value);
4075 irb_.CreateBr(after_invoke);
4076
4077 irb_.SetInsertPoint(block_retry);
4078 return true;
4079}
4080
4081bool MethodCompiler::EmitInlinedStringLength(const std::vector<llvm::Value*>& args,
4082 llvm::BasicBlock* after_invoke) {
4083 DCHECK_EQ(args.size(), 2U) <<
4084 "int java.lang.String.length() has 2 args: method, this";
4085 llvm::Value* this_object = args[1];
TDYa12729c0cd12012-05-17 04:51:08 -07004086 llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object,
4087 String::CountOffset().Int32Value(),
4088 irb_.getJIntTy(),
TDYa127d4f82b62012-06-02 21:48:09 -07004089 kTBAAConstJObject);
TDYa12729c0cd12012-05-17 04:51:08 -07004090 EmitStoreDalvikRetValReg(kInt, kAccurate, string_count);
4091 irb_.CreateBr(after_invoke);
4092 return false;
4093}
4094
TDYa127d4f82b62012-06-02 21:48:09 -07004095bool MethodCompiler::EmitInlinedStringIndexOf(const std::vector<llvm::Value*>& args,
4096 llvm::BasicBlock* after_invoke,
4097 bool zero_based) {
4098 // TODO: Don't generate target specific bitcode, using intrinsic to delay to codegen.
4099 if (compiler_->GetInstructionSet() == kArm || compiler_->GetInstructionSet() == kThumb2) {
4100 DCHECK_EQ(args.size(), (zero_based ? 3U : 4U)) <<
4101 "int java.lang.String.indexOf(int, int = 0) has 3~4 args: method, this, char, start";
4102 llvm::Value* this_object = args[1];
4103 llvm::Value* char_target = args[2];
4104 llvm::Value* start_index = (zero_based ? irb_.getJInt(0) : args[3]);
4105 llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "IndexOfRetry", func_);
4106 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "IndexOfCont", func_);
4107
4108 llvm::Value* slowpath = irb_.CreateICmpSGT(char_target, irb_.getJInt(0xFFFF));
4109 irb_.CreateCondBr(slowpath, block_retry, block_cont, kUnlikely);
4110
4111 irb_.SetInsertPoint(block_cont);
4112
4113 llvm::Type* args_type[] = { irb_.getJObjectTy(), irb_.getJIntTy(), irb_.getJIntTy() };
4114 llvm::FunctionType* func_ty = llvm::FunctionType::get(irb_.getJIntTy(), args_type, false);
4115 llvm::Value* func =
4116 irb_.Runtime().EmitLoadFromThreadOffset(ENTRYPOINT_OFFSET(pIndexOf),
4117 func_ty->getPointerTo(),
4118 kTBAAConstJObject);
4119 llvm::Value* result = irb_.CreateCall3(func, this_object, char_target, start_index);
4120 EmitStoreDalvikRetValReg(kInt, kAccurate, result);
4121 irb_.CreateBr(after_invoke);
4122
4123 irb_.SetInsertPoint(block_retry);
4124 }
4125 return true;
4126}
4127
4128bool MethodCompiler::EmitInlinedStringCompareTo(const std::vector<llvm::Value*>& args,
4129 llvm::BasicBlock* after_invoke) {
4130 // TODO: Don't generate target specific bitcode, using intrinsic to delay to codegen.
4131 if (compiler_->GetInstructionSet() == kArm || compiler_->GetInstructionSet() == kThumb2) {
4132 DCHECK_EQ(args.size(), 3U) <<
4133 "int java.lang.String.compareTo(java.lang.String) has 3 args: method, this, cmpto";
4134 llvm::Value* this_object = args[1];
4135 llvm::Value* cmp_object = args[2];
4136 llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "CompareToRetry", func_);
4137 llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "CompareToCont", func_);
4138
4139 llvm::Value* is_null = irb_.CreateICmpEQ(cmp_object, irb_.getJNull());
4140 irb_.CreateCondBr(is_null, block_retry, block_cont, kUnlikely);
4141
4142 irb_.SetInsertPoint(block_cont);
4143
4144 llvm::Type* args_type[] = { irb_.getJObjectTy(), irb_.getJObjectTy() };
4145 llvm::FunctionType* func_ty = llvm::FunctionType::get(irb_.getJIntTy(), args_type, false);
4146 llvm::Value* func =
4147 irb_.Runtime().EmitLoadFromThreadOffset(ENTRYPOINT_OFFSET(pStringCompareTo),
4148 func_ty->getPointerTo(),
4149 kTBAAConstJObject);
4150 llvm::Value* result = irb_.CreateCall2(func, this_object, cmp_object);
4151 EmitStoreDalvikRetValReg(kInt, kAccurate, result);
4152 irb_.CreateBr(after_invoke);
4153
4154 irb_.SetInsertPoint(block_retry);
4155 }
4156 return true;
4157}
4158
TDYa12729c0cd12012-05-17 04:51:08 -07004159
TDYa127526643e2012-05-26 01:01:48 -07004160bool MethodCompiler::IsInstructionDirectToReturn(uint32_t dex_pc) {
4161 for (int i = 0; i < 8; ++i) { // Trace at most 8 instructions.
4162 if (dex_pc >= code_item_->insns_size_in_code_units_) {
4163 return false;
4164 }
4165
4166 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
4167
4168 if (insn->IsReturn()) {
4169 return true;
4170 }
4171
4172 // Is throw, switch, invoke or conditional branch.
4173 if (insn->IsThrow() || insn->IsSwitch() || insn->IsInvoke() ||
4174 (insn->IsBranch() && !insn->IsUnconditional())) {
4175 return false;
4176 }
4177
4178 switch (insn->Opcode()) {
4179 default:
4180 dex_pc += insn->SizeInCodeUnits();
4181 break;
4182
4183 // This instruction will remove the exception. Consider as a side effect.
4184 case Instruction::MOVE_EXCEPTION:
4185 return false;
4186 break;
4187
4188 case Instruction::GOTO:
4189 case Instruction::GOTO_16:
4190 case Instruction::GOTO_32:
4191 {
4192 DecodedInstruction dec_insn(insn);
4193 int32_t branch_offset = dec_insn.vA;
4194 dex_pc += branch_offset;
4195 }
4196 break;
4197 }
4198 }
4199 return false;
4200}
4201
4202
TDYa12729c0cd12012-05-17 04:51:08 -07004203// TODO: Use high-level IR to do this
TDYa127cc1b4c32012-05-15 07:31:37 -07004204void MethodCompiler::ComputeMethodInfo() {
4205 // If this method is static, we set the "this" register index to -1. So we don't worry about this
4206 // method is static or not in the following comparison.
4207 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
4208 (-1) :
4209 (code_item_->registers_size_ - code_item_->ins_size_);
4210 bool has_invoke = false;
4211 bool may_have_loop = false;
4212 bool modify_this = false;
4213 bool may_throw_exception = false;
4214
4215 Instruction const* insn;
4216 for (uint32_t dex_pc = 0;
4217 dex_pc < code_item_->insns_size_in_code_units_;
4218 dex_pc += insn->SizeInCodeUnits()) {
4219 insn = Instruction::At(code_item_->insns_ + dex_pc);
4220 DecodedInstruction dec_insn(insn);
4221
4222 switch (insn->Opcode()) {
4223 case Instruction::NOP:
4224 break;
4225
4226 case Instruction::MOVE:
4227 case Instruction::MOVE_FROM16:
4228 case Instruction::MOVE_16:
4229 case Instruction::MOVE_WIDE:
4230 case Instruction::MOVE_WIDE_FROM16:
4231 case Instruction::MOVE_WIDE_16:
4232 case Instruction::MOVE_OBJECT:
4233 case Instruction::MOVE_OBJECT_FROM16:
4234 case Instruction::MOVE_OBJECT_16:
4235 case Instruction::MOVE_RESULT:
4236 case Instruction::MOVE_RESULT_WIDE:
4237 case Instruction::MOVE_RESULT_OBJECT:
4238 case Instruction::MOVE_EXCEPTION:
4239 if (dec_insn.vA == this_reg_idx) {
4240 modify_this = true;
4241 }
4242 break;
4243
4244 case Instruction::RETURN_VOID:
4245 case Instruction::RETURN:
4246 case Instruction::RETURN_WIDE:
4247 case Instruction::RETURN_OBJECT:
4248 break;
4249
4250 case Instruction::CONST_4:
4251 case Instruction::CONST_16:
4252 case Instruction::CONST:
4253 case Instruction::CONST_HIGH16:
4254 case Instruction::CONST_WIDE_16:
4255 case Instruction::CONST_WIDE_32:
4256 case Instruction::CONST_WIDE:
4257 case Instruction::CONST_WIDE_HIGH16:
4258 if (dec_insn.vA == this_reg_idx) {
4259 modify_this = true;
4260 }
4261 break;
4262
4263 case Instruction::CONST_STRING:
4264 case Instruction::CONST_STRING_JUMBO:
4265 // TODO: Will the ResolveString throw exception?
4266 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4267 may_throw_exception = true;
4268 }
4269 if (dec_insn.vA == this_reg_idx) {
4270 modify_this = true;
4271 }
4272 break;
4273
4274 case Instruction::CONST_CLASS:
4275 may_throw_exception = true;
4276 if (dec_insn.vA == this_reg_idx) {
4277 modify_this = true;
4278 }
4279 break;
4280
4281 case Instruction::MONITOR_ENTER:
4282 case Instruction::MONITOR_EXIT:
4283 case Instruction::CHECK_CAST:
4284 may_throw_exception = true;
4285 break;
4286
4287 case Instruction::INSTANCE_OF:
TDYa127cc1b4c32012-05-15 07:31:37 -07004288 case Instruction::ARRAY_LENGTH:
TDYa127cc1b4c32012-05-15 07:31:37 -07004289 case Instruction::NEW_INSTANCE:
4290 case Instruction::NEW_ARRAY:
4291 may_throw_exception = true;
4292 if (dec_insn.vA == this_reg_idx) {
4293 modify_this = true;
4294 }
4295 break;
4296
4297 case Instruction::FILLED_NEW_ARRAY:
4298 case Instruction::FILLED_NEW_ARRAY_RANGE:
4299 case Instruction::FILL_ARRAY_DATA:
4300 case Instruction::THROW:
4301 may_throw_exception = true;
4302 break;
4303
4304 case Instruction::GOTO:
4305 case Instruction::GOTO_16:
4306 case Instruction::GOTO_32:
4307 {
4308 int32_t branch_offset = dec_insn.vA;
TDYa127526643e2012-05-26 01:01:48 -07004309 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4310 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004311 }
4312 }
4313 break;
4314
4315 case Instruction::PACKED_SWITCH:
4316 case Instruction::SPARSE_SWITCH:
4317 break;
4318
4319 case Instruction::CMPL_FLOAT:
4320 case Instruction::CMPG_FLOAT:
4321 case Instruction::CMPL_DOUBLE:
4322 case Instruction::CMPG_DOUBLE:
4323 case Instruction::CMP_LONG:
4324 if (dec_insn.vA == this_reg_idx) {
4325 modify_this = true;
4326 }
4327 break;
4328
4329 case Instruction::IF_EQ:
4330 case Instruction::IF_NE:
4331 case Instruction::IF_LT:
4332 case Instruction::IF_GE:
4333 case Instruction::IF_GT:
4334 case Instruction::IF_LE:
4335 {
4336 int32_t branch_offset = dec_insn.vC;
TDYa127526643e2012-05-26 01:01:48 -07004337 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4338 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004339 }
4340 }
4341 break;
4342
4343 case Instruction::IF_EQZ:
4344 case Instruction::IF_NEZ:
4345 case Instruction::IF_LTZ:
4346 case Instruction::IF_GEZ:
4347 case Instruction::IF_GTZ:
4348 case Instruction::IF_LEZ:
4349 {
4350 int32_t branch_offset = dec_insn.vB;
TDYa127526643e2012-05-26 01:01:48 -07004351 if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) {
4352 may_have_loop = true;
TDYa127cc1b4c32012-05-15 07:31:37 -07004353 }
4354 }
4355 break;
4356
4357 case Instruction::AGET:
4358 case Instruction::AGET_WIDE:
4359 case Instruction::AGET_OBJECT:
4360 case Instruction::AGET_BOOLEAN:
4361 case Instruction::AGET_BYTE:
4362 case Instruction::AGET_CHAR:
4363 case Instruction::AGET_SHORT:
4364 may_throw_exception = true;
4365 if (dec_insn.vA == this_reg_idx) {
4366 modify_this = true;
4367 }
4368 break;
4369
4370 case Instruction::APUT:
4371 case Instruction::APUT_WIDE:
4372 case Instruction::APUT_OBJECT:
4373 case Instruction::APUT_BOOLEAN:
4374 case Instruction::APUT_BYTE:
4375 case Instruction::APUT_CHAR:
4376 case Instruction::APUT_SHORT:
4377 may_throw_exception = true;
4378 break;
4379
4380 case Instruction::IGET:
4381 case Instruction::IGET_WIDE:
4382 case Instruction::IGET_OBJECT:
4383 case Instruction::IGET_BOOLEAN:
4384 case Instruction::IGET_BYTE:
4385 case Instruction::IGET_CHAR:
4386 case Instruction::IGET_SHORT:
4387 {
4388 if (dec_insn.vA == this_reg_idx) {
4389 modify_this = true;
4390 }
4391 uint32_t reg_idx = dec_insn.vB;
4392 uint32_t field_idx = dec_insn.vC;
4393 int field_offset;
4394 bool is_volatile;
4395 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4396 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4397 if (!is_fast_path) {
4398 may_throw_exception = true;
4399 } else if (reg_idx != this_reg_idx) {
4400 // NullPointerException
4401 may_throw_exception = true;
4402 }
4403 }
4404 break;
4405
4406 case Instruction::IPUT:
4407 case Instruction::IPUT_WIDE:
4408 case Instruction::IPUT_OBJECT:
4409 case Instruction::IPUT_BOOLEAN:
4410 case Instruction::IPUT_BYTE:
4411 case Instruction::IPUT_CHAR:
4412 case Instruction::IPUT_SHORT:
4413 {
4414 uint32_t reg_idx = dec_insn.vB;
4415 uint32_t field_idx = dec_insn.vC;
4416 int field_offset;
4417 bool is_volatile;
4418 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4419 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4420 if (!is_fast_path) {
4421 may_throw_exception = true;
4422 } else if (reg_idx != this_reg_idx) {
4423 // NullPointerException
4424 may_throw_exception = true;
4425 }
4426 }
4427 break;
4428
4429 case Instruction::SGET:
4430 case Instruction::SGET_WIDE:
4431 case Instruction::SGET_OBJECT:
4432 case Instruction::SGET_BOOLEAN:
4433 case Instruction::SGET_BYTE:
4434 case Instruction::SGET_CHAR:
4435 case Instruction::SGET_SHORT:
4436 {
4437 if (dec_insn.vA == this_reg_idx) {
4438 modify_this = true;
4439 }
4440 uint32_t field_idx = dec_insn.vB;
4441
4442 int field_offset;
4443 int ssb_index;
4444 bool is_referrers_class;
4445 bool is_volatile;
4446
4447 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4448 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4449 is_referrers_class, is_volatile, false);
4450 if (!is_fast_path || !is_referrers_class) {
4451 may_throw_exception = true;
4452 }
4453 }
4454 break;
4455
4456 case Instruction::SPUT:
4457 case Instruction::SPUT_WIDE:
4458 case Instruction::SPUT_OBJECT:
4459 case Instruction::SPUT_BOOLEAN:
4460 case Instruction::SPUT_BYTE:
4461 case Instruction::SPUT_CHAR:
4462 case Instruction::SPUT_SHORT:
4463 {
4464 uint32_t field_idx = dec_insn.vB;
4465
4466 int field_offset;
4467 int ssb_index;
4468 bool is_referrers_class;
4469 bool is_volatile;
4470
4471 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4472 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4473 is_referrers_class, is_volatile, true);
4474 if (!is_fast_path || !is_referrers_class) {
4475 may_throw_exception = true;
4476 }
4477 }
4478 break;
4479
4480
4481 case Instruction::INVOKE_VIRTUAL:
4482 case Instruction::INVOKE_SUPER:
4483 case Instruction::INVOKE_DIRECT:
4484 case Instruction::INVOKE_STATIC:
4485 case Instruction::INVOKE_INTERFACE:
4486 case Instruction::INVOKE_VIRTUAL_RANGE:
4487 case Instruction::INVOKE_SUPER_RANGE:
4488 case Instruction::INVOKE_DIRECT_RANGE:
4489 case Instruction::INVOKE_STATIC_RANGE:
4490 case Instruction::INVOKE_INTERFACE_RANGE:
4491 has_invoke = true;
4492 may_throw_exception = true;
4493 break;
4494
4495 case Instruction::NEG_INT:
4496 case Instruction::NOT_INT:
4497 case Instruction::NEG_LONG:
4498 case Instruction::NOT_LONG:
4499 case Instruction::NEG_FLOAT:
4500 case Instruction::NEG_DOUBLE:
4501 case Instruction::INT_TO_LONG:
4502 case Instruction::INT_TO_FLOAT:
4503 case Instruction::INT_TO_DOUBLE:
4504 case Instruction::LONG_TO_INT:
4505 case Instruction::LONG_TO_FLOAT:
4506 case Instruction::LONG_TO_DOUBLE:
4507 case Instruction::FLOAT_TO_INT:
4508 case Instruction::FLOAT_TO_LONG:
4509 case Instruction::FLOAT_TO_DOUBLE:
4510 case Instruction::DOUBLE_TO_INT:
4511 case Instruction::DOUBLE_TO_LONG:
4512 case Instruction::DOUBLE_TO_FLOAT:
4513 case Instruction::INT_TO_BYTE:
4514 case Instruction::INT_TO_CHAR:
4515 case Instruction::INT_TO_SHORT:
4516 case Instruction::ADD_INT:
4517 case Instruction::SUB_INT:
4518 case Instruction::MUL_INT:
4519 case Instruction::AND_INT:
4520 case Instruction::OR_INT:
4521 case Instruction::XOR_INT:
4522 case Instruction::SHL_INT:
4523 case Instruction::SHR_INT:
4524 case Instruction::USHR_INT:
4525 case Instruction::ADD_LONG:
4526 case Instruction::SUB_LONG:
4527 case Instruction::MUL_LONG:
4528 case Instruction::AND_LONG:
4529 case Instruction::OR_LONG:
4530 case Instruction::XOR_LONG:
4531 case Instruction::SHL_LONG:
4532 case Instruction::SHR_LONG:
4533 case Instruction::USHR_LONG:
4534 case Instruction::ADD_INT_2ADDR:
4535 case Instruction::SUB_INT_2ADDR:
4536 case Instruction::MUL_INT_2ADDR:
4537 case Instruction::AND_INT_2ADDR:
4538 case Instruction::OR_INT_2ADDR:
4539 case Instruction::XOR_INT_2ADDR:
4540 case Instruction::SHL_INT_2ADDR:
4541 case Instruction::SHR_INT_2ADDR:
4542 case Instruction::USHR_INT_2ADDR:
4543 case Instruction::ADD_LONG_2ADDR:
4544 case Instruction::SUB_LONG_2ADDR:
4545 case Instruction::MUL_LONG_2ADDR:
4546 case Instruction::AND_LONG_2ADDR:
4547 case Instruction::OR_LONG_2ADDR:
4548 case Instruction::XOR_LONG_2ADDR:
4549 case Instruction::SHL_LONG_2ADDR:
4550 case Instruction::SHR_LONG_2ADDR:
4551 case Instruction::USHR_LONG_2ADDR:
4552 if (dec_insn.vA == this_reg_idx) {
4553 modify_this = true;
4554 }
4555 break;
4556
4557 case Instruction::DIV_INT:
4558 case Instruction::REM_INT:
4559 case Instruction::DIV_LONG:
4560 case Instruction::REM_LONG:
4561 case Instruction::DIV_INT_2ADDR:
4562 case Instruction::REM_INT_2ADDR:
4563 case Instruction::DIV_LONG_2ADDR:
4564 case Instruction::REM_LONG_2ADDR:
4565 may_throw_exception = true;
4566 if (dec_insn.vA == this_reg_idx) {
4567 modify_this = true;
4568 }
4569 break;
4570
4571 case Instruction::ADD_FLOAT:
4572 case Instruction::SUB_FLOAT:
4573 case Instruction::MUL_FLOAT:
4574 case Instruction::DIV_FLOAT:
4575 case Instruction::REM_FLOAT:
4576 case Instruction::ADD_DOUBLE:
4577 case Instruction::SUB_DOUBLE:
4578 case Instruction::MUL_DOUBLE:
4579 case Instruction::DIV_DOUBLE:
4580 case Instruction::REM_DOUBLE:
4581 case Instruction::ADD_FLOAT_2ADDR:
4582 case Instruction::SUB_FLOAT_2ADDR:
4583 case Instruction::MUL_FLOAT_2ADDR:
4584 case Instruction::DIV_FLOAT_2ADDR:
4585 case Instruction::REM_FLOAT_2ADDR:
4586 case Instruction::ADD_DOUBLE_2ADDR:
4587 case Instruction::SUB_DOUBLE_2ADDR:
4588 case Instruction::MUL_DOUBLE_2ADDR:
4589 case Instruction::DIV_DOUBLE_2ADDR:
4590 case Instruction::REM_DOUBLE_2ADDR:
4591 if (dec_insn.vA == this_reg_idx) {
4592 modify_this = true;
4593 }
4594 break;
4595
4596 case Instruction::ADD_INT_LIT16:
4597 case Instruction::ADD_INT_LIT8:
4598 case Instruction::RSUB_INT:
4599 case Instruction::RSUB_INT_LIT8:
4600 case Instruction::MUL_INT_LIT16:
4601 case Instruction::MUL_INT_LIT8:
4602 case Instruction::AND_INT_LIT16:
4603 case Instruction::AND_INT_LIT8:
4604 case Instruction::OR_INT_LIT16:
4605 case Instruction::OR_INT_LIT8:
4606 case Instruction::XOR_INT_LIT16:
4607 case Instruction::XOR_INT_LIT8:
4608 case Instruction::SHL_INT_LIT8:
4609 case Instruction::SHR_INT_LIT8:
4610 case Instruction::USHR_INT_LIT8:
4611 if (dec_insn.vA == this_reg_idx) {
4612 modify_this = true;
4613 }
4614 break;
4615 case Instruction::DIV_INT_LIT16:
4616 case Instruction::DIV_INT_LIT8:
4617 case Instruction::REM_INT_LIT16:
4618 case Instruction::REM_INT_LIT8:
4619 if (dec_insn.vC == 0) {
4620 may_throw_exception = true;
4621 }
4622 if (dec_insn.vA == this_reg_idx) {
4623 modify_this = true;
4624 }
4625 break;
4626
4627 case Instruction::THROW_VERIFICATION_ERROR:
4628 may_throw_exception = true;
4629 break;
4630
4631 case Instruction::UNUSED_3E:
4632 case Instruction::UNUSED_3F:
4633 case Instruction::UNUSED_40:
4634 case Instruction::UNUSED_41:
4635 case Instruction::UNUSED_42:
4636 case Instruction::UNUSED_43:
4637 case Instruction::UNUSED_73:
4638 case Instruction::UNUSED_79:
4639 case Instruction::UNUSED_7A:
4640 case Instruction::UNUSED_E3:
4641 case Instruction::UNUSED_E4:
4642 case Instruction::UNUSED_E5:
4643 case Instruction::UNUSED_E6:
4644 case Instruction::UNUSED_E7:
4645 case Instruction::UNUSED_E8:
4646 case Instruction::UNUSED_E9:
4647 case Instruction::UNUSED_EA:
4648 case Instruction::UNUSED_EB:
4649 case Instruction::UNUSED_EC:
4650 case Instruction::UNUSED_EE:
4651 case Instruction::UNUSED_EF:
4652 case Instruction::UNUSED_F0:
4653 case Instruction::UNUSED_F1:
4654 case Instruction::UNUSED_F2:
4655 case Instruction::UNUSED_F3:
4656 case Instruction::UNUSED_F4:
4657 case Instruction::UNUSED_F5:
4658 case Instruction::UNUSED_F6:
4659 case Instruction::UNUSED_F7:
4660 case Instruction::UNUSED_F8:
4661 case Instruction::UNUSED_F9:
4662 case Instruction::UNUSED_FA:
4663 case Instruction::UNUSED_FB:
4664 case Instruction::UNUSED_FC:
4665 case Instruction::UNUSED_FD:
4666 case Instruction::UNUSED_FE:
4667 case Instruction::UNUSED_FF:
4668 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4669 break;
4670 }
4671 }
4672
4673 method_info_.this_reg_idx = this_reg_idx;
4674 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4675 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4676 // should remove this trick.
4677 method_info_.this_will_not_be_null = !modify_this;
4678 method_info_.has_invoke = has_invoke;
4679 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4680 // for GC.
4681 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4682 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4683 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
TDYa127af543472012-05-28 21:49:23 -07004684 // If can only throw exception, but can't suspend check (no loop, no invoke),
4685 // then there is no shadow frame entry. Only Shadow frame is needed.
4686 method_info_.lazy_push_shadow_frame =
4687 method_info_.need_shadow_frame && !method_info_.need_shadow_frame_entry;
TDYa127cc1b4c32012-05-15 07:31:37 -07004688}
4689
4690
4691
Logan Chien83426162011-12-09 09:29:50 +08004692} // namespace compiler_llvm
4693} // namespace art