Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1 | /* |
| 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 Chien | fca7e87 | 2011-12-20 20:08:22 +0800 | [diff] [blame] | 19 | #include "backend_types.h" |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 20 | #include "compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 21 | #include "compiler.h" |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 22 | #include "dalvik_reg.h" |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 23 | #include "inferred_reg_category_map.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 24 | #include "ir_builder.h" |
| 25 | #include "logging.h" |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 26 | #include "oat_compilation_unit.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 27 | #include "object.h" |
| 28 | #include "object_utils.h" |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 29 | #include "runtime_support_func.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 30 | #include "runtime_support_llvm.h" |
Logan Chien | 1b0a1b7 | 2012-03-15 06:20:17 +0800 | [diff] [blame] | 31 | #include "shadow_frame.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 32 | #include "stl_util.h" |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 33 | #include "stringprintf.h" |
| 34 | #include "utils_llvm.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 35 | #include "verifier/method_verifier.h" |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 36 | |
| 37 | #include <iomanip> |
| 38 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 39 | #include <llvm/BasicBlock.h> |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 40 | #include <llvm/Function.h> |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 41 | #include <llvm/GlobalVariable.h> |
| 42 | #include <llvm/Intrinsics.h> |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 43 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 44 | namespace art { |
| 45 | namespace compiler_llvm { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 46 | |
Logan Chien | 42e0e15 | 2012-01-13 15:42:36 +0800 | [diff] [blame] | 47 | using namespace runtime_support; |
| 48 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 49 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 50 | MethodCompiler::MethodCompiler(CompilationUnit* cunit, |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 51 | Compiler* compiler, |
Logan Chien | 4dd96f5 | 2012-02-29 01:26:58 +0800 | [diff] [blame] | 52 | OatCompilationUnit* oat_compilation_unit) |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 53 | : 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 Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 60 | method_idx_(oat_compilation_unit->method_idx_), |
| 61 | access_flags_(oat_compilation_unit->access_flags_), |
| 62 | module_(cunit->GetModule()), |
| 63 | context_(cunit->GetLLVMContext()), |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 64 | irb_(*cunit->GetIRBuilder()), |
| 65 | func_(NULL), |
| 66 | regs_(code_item_->registers_size_), |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 67 | shadow_frame_entries_(code_item_->registers_size_), |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 68 | reg_to_shadow_frame_index_(code_item_->registers_size_, -1), |
| 69 | retval_reg_(NULL), |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 70 | basic_block_alloca_(NULL), basic_block_shadow_frame_(NULL), |
Logan Chien | ef4a656 | 2012-04-24 18:02:24 +0800 | [diff] [blame] | 71 | basic_block_reg_arg_init_(NULL), |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 72 | 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), |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 75 | shadow_frame_(NULL), jvalue_temp_(NULL), old_shadow_frame_(NULL), |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 76 | already_pushed_shadow_frame_(NULL), shadow_frame_size_(0), |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 77 | elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) { |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | MethodCompiler::~MethodCompiler() { |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 82 | STLDeleteElements(®s_); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 86 | void MethodCompiler::CreateFunction() { |
| 87 | // LLVM function name |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 88 | std::string func_name(ElfFuncName(elf_func_idx_)); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 89 | |
| 90 | // Get function type |
| 91 | llvm::FunctionType* func_type = |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 92 | GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic()); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 93 | |
| 94 | // Create function |
| 95 | func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, |
| 96 | func_name, module_); |
| 97 | |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 98 | #if !defined(NDEBUG) |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 99 | // 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 Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 107 | if (!oat_compilation_unit_->IsStatic()) { |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 108 | 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 | } |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 116 | #endif |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | llvm::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 Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 125 | uint32_t shorty_size; |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 126 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 127 | CHECK_GE(shorty_size, 1u); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 128 | |
| 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 Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 141 | for (uint32_t i = 1; i < shorty_size; ++i) { |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 142 | 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 Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 149 | void MethodCompiler::EmitPrologue() { |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 150 | // Create basic blocks for prologue |
TDYa127 | 9948913 | 2012-04-29 01:27:58 -0700 | [diff] [blame] | 151 | #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 |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 156 | basic_block_alloca_ = |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 157 | llvm::BasicBlock::Create(*context_, "prologue.alloca", func_); |
| 158 | |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 159 | basic_block_shadow_frame_ = |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 160 | llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_); |
| 161 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 162 | basic_block_reg_arg_init_ = |
| 163 | llvm::BasicBlock::Create(*context_, "prologue.arginit", func_); |
| 164 | |
TDYa127 | 9948913 | 2012-04-29 01:27:58 -0700 | [diff] [blame] | 165 | #if !defined(NDEBUG) |
| 166 | irb_.SetInsertPoint(entry); |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 167 | irb_.CreateBr(basic_block_alloca_); |
TDYa127 | 9948913 | 2012-04-29 01:27:58 -0700 | [diff] [blame] | 168 | #endif |
| 169 | |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 170 | irb_.SetInsertPoint(basic_block_alloca_); |
| 171 | jvalue_temp_ = irb_.CreateAlloca(irb_.getJValueTy()); |
| 172 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 173 | // Create Shadow Frame |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 174 | if (method_info_.need_shadow_frame) { |
| 175 | EmitPrologueAllocShadowFrame(); |
| 176 | } |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 177 | |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 178 | // Create register array |
| 179 | for (uint16_t r = 0; r < code_item_->registers_size_; ++r) { |
| 180 | std::string name; |
| 181 | #if !defined(NDEBUG) |
| 182 | name = StringPrintf("%u", r); |
| 183 | #endif |
| 184 | regs_[r] = new DalvikReg(*this, name); |
| 185 | |
| 186 | // Cache shadow frame entry address |
| 187 | shadow_frame_entries_[r] = GetShadowFrameEntry(r); |
| 188 | } |
| 189 | |
| 190 | std::string name; |
| 191 | #if !defined(NDEBUG) |
| 192 | name = "_res"; |
| 193 | #endif |
| 194 | retval_reg_.reset(new DalvikReg(*this, name)); |
| 195 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 196 | // Store argument to dalvik register |
| 197 | irb_.SetInsertPoint(basic_block_reg_arg_init_); |
| 198 | EmitPrologueAssignArgRegister(); |
| 199 | |
| 200 | // Branch to start address |
| 201 | irb_.CreateBr(GetBasicBlock(0)); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 205 | void MethodCompiler::EmitStackOverflowCheck() { |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 206 | // Call llvm intrinsic function to get frame address. |
| 207 | llvm::Function* frameaddress = |
| 208 | llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress); |
| 209 | |
| 210 | // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32) |
| 211 | llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0)); |
| 212 | |
| 213 | // Cast i8* to int |
| 214 | frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy()); |
| 215 | |
| 216 | // Get thread.stack_end_ |
| 217 | llvm::Value* thread_object_addr = |
| 218 | irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 219 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 220 | llvm::Value* stack_end = |
| 221 | irb_.LoadFromObjectOffset(thread_object_addr, |
| 222 | Thread::StackEndOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 223 | irb_.getPtrEquivIntTy(), |
| 224 | kTBAARuntimeInfo); |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 225 | |
| 226 | // Check the frame address < thread.stack_end_ ? |
| 227 | llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end); |
| 228 | |
| 229 | llvm::BasicBlock* block_exception = |
| 230 | llvm::BasicBlock::Create(*context_, "stack_overflow", func_); |
| 231 | |
| 232 | llvm::BasicBlock* block_continue = |
| 233 | llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_); |
| 234 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 235 | irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely); |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 236 | |
| 237 | // If stack overflow, throw exception. |
| 238 | irb_.SetInsertPoint(block_exception); |
| 239 | irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException)); |
| 240 | |
| 241 | // Unwind. |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 242 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 243 | if (ret_shorty == 'V') { |
| 244 | irb_.CreateRetVoid(); |
| 245 | } else { |
| 246 | irb_.CreateRet(irb_.getJZero(ret_shorty)); |
| 247 | } |
| 248 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 249 | irb_.SetInsertPoint(block_continue); |
TDYa127 | 4165a83 | 2012-04-03 17:47:16 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 253 | void MethodCompiler::EmitPrologueLastBranch() { |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 254 | llvm::BasicBlock* basic_block_stack_overflow = |
| 255 | llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_); |
TDYa127 | 97339c4 | 2012-04-29 01:26:35 -0700 | [diff] [blame] | 256 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 257 | irb_.SetInsertPoint(basic_block_alloca_); |
| 258 | irb_.CreateBr(basic_block_stack_overflow); |
| 259 | |
| 260 | irb_.SetInsertPoint(basic_block_stack_overflow); |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 261 | // If a method will not call to other method, and the method is small, we can avoid stack overflow |
| 262 | // check. |
| 263 | if (method_info_.has_invoke || |
| 264 | code_item_->registers_size_ > 32) { // Small leaf function is OK given |
| 265 | // the 8KB reserved at Stack End |
| 266 | EmitStackOverflowCheck(); |
| 267 | } |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 268 | // Garbage collection safe-point |
| 269 | EmitGuard_GarbageCollectionSuspend(); |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 270 | irb_.CreateBr(basic_block_shadow_frame_); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 271 | |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 272 | irb_.SetInsertPoint(basic_block_shadow_frame_); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 273 | irb_.CreateBr(basic_block_reg_arg_init_); |
| 274 | } |
| 275 | |
| 276 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 277 | void MethodCompiler::EmitPrologueAllocShadowFrame() { |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 278 | irb_.SetInsertPoint(basic_block_alloca_); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 279 | |
| 280 | // Allocate the shadow frame now! |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 281 | shadow_frame_size_ = 0; |
TDYa127 | f165286 | 2012-05-16 21:44:35 -0700 | [diff] [blame] | 282 | if (method_info_.need_shadow_frame_entry) { |
| 283 | for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) { |
| 284 | if (IsRegCanBeObject(i)) { |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 285 | reg_to_shadow_frame_index_[i] = shadow_frame_size_++; |
TDYa127 | f165286 | 2012-05-16 21:44:35 -0700 | [diff] [blame] | 286 | } |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 289 | |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 290 | llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(shadow_frame_size_); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 291 | shadow_frame_ = irb_.CreateAlloca(shadow_frame_type); |
| 292 | |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 293 | // Alloca a pointer to old shadow frame |
| 294 | old_shadow_frame_ = irb_.CreateAlloca(shadow_frame_type->getElementType(0)->getPointerTo()); |
| 295 | |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 296 | irb_.SetInsertPoint(basic_block_shadow_frame_); |
| 297 | |
TDYa127 | 6e474f8 | 2012-05-17 21:23:57 -0700 | [diff] [blame] | 298 | // Zero-initialization of the shadow frame table |
| 299 | llvm::Value* shadow_frame_table = irb_.CreateConstGEP2_32(shadow_frame_, 0, 1); |
| 300 | llvm::Type* table_type = shadow_frame_type->getElementType(1); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 301 | |
TDYa127 | 6e474f8 | 2012-05-17 21:23:57 -0700 | [diff] [blame] | 302 | llvm::ConstantAggregateZero* zero_initializer = |
| 303 | llvm::ConstantAggregateZero::get(table_type); |
| 304 | |
| 305 | irb_.CreateStore(zero_initializer, shadow_frame_table, kTBAAShadowFrame); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 306 | |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 307 | // Lazy pushing shadow frame |
| 308 | if (method_info_.lazy_push_shadow_frame) { |
| 309 | irb_.SetInsertPoint(basic_block_alloca_); |
| 310 | already_pushed_shadow_frame_ = irb_.CreateAlloca(irb_.getInt1Ty()); |
| 311 | irb_.SetInsertPoint(basic_block_shadow_frame_); |
| 312 | irb_.CreateStore(irb_.getFalse(), already_pushed_shadow_frame_, kTBAARegister); |
| 313 | return; |
| 314 | } |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 315 | |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 316 | EmitPushShadowFrame(true); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 320 | void MethodCompiler::EmitPrologueAssignArgRegister() { |
| 321 | uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_; |
| 322 | |
| 323 | llvm::Function::arg_iterator arg_iter(func_->arg_begin()); |
| 324 | llvm::Function::arg_iterator arg_end(func_->arg_end()); |
| 325 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 326 | uint32_t shorty_size = 0; |
| 327 | char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size); |
| 328 | CHECK_GE(shorty_size, 1u); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 329 | |
| 330 | ++arg_iter; // skip method object |
| 331 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 332 | if (!oat_compilation_unit_->IsStatic()) { |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 333 | regs_[arg_reg]->SetValue(kObject, kAccurate, arg_iter); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 334 | ++arg_iter; |
| 335 | ++arg_reg; |
| 336 | } |
| 337 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 338 | for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) { |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 339 | regs_[arg_reg]->SetValue(shorty[i], kAccurate, arg_iter); |
Logan Chien | d6ececa | 2011-12-27 16:20:15 +0800 | [diff] [blame] | 340 | |
| 341 | ++arg_reg; |
| 342 | if (shorty[i] == 'J' || shorty[i] == 'D') { |
| 343 | // Wide types, such as long and double, are using a pair of registers |
| 344 | // to store the value, so we have to increase arg_reg again. |
| 345 | ++arg_reg; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | DCHECK_EQ(arg_end, arg_iter); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 353 | void MethodCompiler::EmitInstructions() { |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 354 | uint32_t dex_pc = 0; |
| 355 | while (dex_pc < code_item_->insns_size_in_code_units_) { |
| 356 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 357 | EmitInstruction(dex_pc, insn); |
| 358 | dex_pc += insn->SizeInCodeUnits(); |
| 359 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 363 | void MethodCompiler::EmitInstruction(uint32_t dex_pc, |
| 364 | Instruction const* insn) { |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 365 | |
| 366 | // Set the IRBuilder insertion point |
| 367 | irb_.SetInsertPoint(GetBasicBlock(dex_pc)); |
| 368 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 369 | #define ARGS dex_pc, insn |
| 370 | |
| 371 | // Dispatch the instruction |
| 372 | switch (insn->Opcode()) { |
| 373 | case Instruction::NOP: |
| 374 | EmitInsn_Nop(ARGS); |
| 375 | break; |
| 376 | |
| 377 | case Instruction::MOVE: |
| 378 | case Instruction::MOVE_FROM16: |
| 379 | case Instruction::MOVE_16: |
| 380 | EmitInsn_Move(ARGS, kInt); |
| 381 | break; |
| 382 | |
| 383 | case Instruction::MOVE_WIDE: |
| 384 | case Instruction::MOVE_WIDE_FROM16: |
| 385 | case Instruction::MOVE_WIDE_16: |
| 386 | EmitInsn_Move(ARGS, kLong); |
| 387 | break; |
| 388 | |
| 389 | case Instruction::MOVE_OBJECT: |
| 390 | case Instruction::MOVE_OBJECT_FROM16: |
| 391 | case Instruction::MOVE_OBJECT_16: |
| 392 | EmitInsn_Move(ARGS, kObject); |
| 393 | break; |
| 394 | |
| 395 | case Instruction::MOVE_RESULT: |
| 396 | EmitInsn_MoveResult(ARGS, kInt); |
| 397 | break; |
| 398 | |
| 399 | case Instruction::MOVE_RESULT_WIDE: |
| 400 | EmitInsn_MoveResult(ARGS, kLong); |
| 401 | break; |
| 402 | |
| 403 | case Instruction::MOVE_RESULT_OBJECT: |
| 404 | EmitInsn_MoveResult(ARGS, kObject); |
| 405 | break; |
| 406 | |
| 407 | case Instruction::MOVE_EXCEPTION: |
| 408 | EmitInsn_MoveException(ARGS); |
| 409 | break; |
| 410 | |
| 411 | case Instruction::RETURN_VOID: |
| 412 | EmitInsn_ReturnVoid(ARGS); |
| 413 | break; |
| 414 | |
| 415 | case Instruction::RETURN: |
| 416 | case Instruction::RETURN_WIDE: |
| 417 | case Instruction::RETURN_OBJECT: |
| 418 | EmitInsn_Return(ARGS); |
| 419 | break; |
| 420 | |
| 421 | case Instruction::CONST_4: |
| 422 | case Instruction::CONST_16: |
| 423 | case Instruction::CONST: |
| 424 | case Instruction::CONST_HIGH16: |
| 425 | EmitInsn_LoadConstant(ARGS, kInt); |
| 426 | break; |
| 427 | |
| 428 | case Instruction::CONST_WIDE_16: |
| 429 | case Instruction::CONST_WIDE_32: |
| 430 | case Instruction::CONST_WIDE: |
| 431 | case Instruction::CONST_WIDE_HIGH16: |
| 432 | EmitInsn_LoadConstant(ARGS, kLong); |
| 433 | break; |
| 434 | |
| 435 | case Instruction::CONST_STRING: |
| 436 | case Instruction::CONST_STRING_JUMBO: |
| 437 | EmitInsn_LoadConstantString(ARGS); |
| 438 | break; |
| 439 | |
| 440 | case Instruction::CONST_CLASS: |
| 441 | EmitInsn_LoadConstantClass(ARGS); |
| 442 | break; |
| 443 | |
| 444 | case Instruction::MONITOR_ENTER: |
| 445 | EmitInsn_MonitorEnter(ARGS); |
| 446 | break; |
| 447 | |
| 448 | case Instruction::MONITOR_EXIT: |
| 449 | EmitInsn_MonitorExit(ARGS); |
| 450 | break; |
| 451 | |
| 452 | case Instruction::CHECK_CAST: |
| 453 | EmitInsn_CheckCast(ARGS); |
| 454 | break; |
| 455 | |
| 456 | case Instruction::INSTANCE_OF: |
| 457 | EmitInsn_InstanceOf(ARGS); |
| 458 | break; |
| 459 | |
| 460 | case Instruction::ARRAY_LENGTH: |
| 461 | EmitInsn_ArrayLength(ARGS); |
| 462 | break; |
| 463 | |
| 464 | case Instruction::NEW_INSTANCE: |
| 465 | EmitInsn_NewInstance(ARGS); |
| 466 | break; |
| 467 | |
| 468 | case Instruction::NEW_ARRAY: |
| 469 | EmitInsn_NewArray(ARGS); |
| 470 | break; |
| 471 | |
| 472 | case Instruction::FILLED_NEW_ARRAY: |
| 473 | EmitInsn_FilledNewArray(ARGS, false); |
| 474 | break; |
| 475 | |
| 476 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 477 | EmitInsn_FilledNewArray(ARGS, true); |
| 478 | break; |
| 479 | |
| 480 | case Instruction::FILL_ARRAY_DATA: |
| 481 | EmitInsn_FillArrayData(ARGS); |
| 482 | break; |
| 483 | |
| 484 | case Instruction::THROW: |
| 485 | EmitInsn_ThrowException(ARGS); |
| 486 | break; |
| 487 | |
| 488 | case Instruction::GOTO: |
| 489 | case Instruction::GOTO_16: |
| 490 | case Instruction::GOTO_32: |
| 491 | EmitInsn_UnconditionalBranch(ARGS); |
| 492 | break; |
| 493 | |
| 494 | case Instruction::PACKED_SWITCH: |
| 495 | EmitInsn_PackedSwitch(ARGS); |
| 496 | break; |
| 497 | |
| 498 | case Instruction::SPARSE_SWITCH: |
| 499 | EmitInsn_SparseSwitch(ARGS); |
| 500 | break; |
| 501 | |
| 502 | case Instruction::CMPL_FLOAT: |
| 503 | EmitInsn_FPCompare(ARGS, kFloat, false); |
| 504 | break; |
| 505 | |
| 506 | case Instruction::CMPG_FLOAT: |
| 507 | EmitInsn_FPCompare(ARGS, kFloat, true); |
| 508 | break; |
| 509 | |
| 510 | case Instruction::CMPL_DOUBLE: |
| 511 | EmitInsn_FPCompare(ARGS, kDouble, false); |
| 512 | break; |
| 513 | |
| 514 | case Instruction::CMPG_DOUBLE: |
| 515 | EmitInsn_FPCompare(ARGS, kDouble, true); |
| 516 | break; |
| 517 | |
| 518 | case Instruction::CMP_LONG: |
| 519 | EmitInsn_LongCompare(ARGS); |
| 520 | break; |
| 521 | |
| 522 | case Instruction::IF_EQ: |
| 523 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ); |
| 524 | break; |
| 525 | |
| 526 | case Instruction::IF_NE: |
| 527 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE); |
| 528 | break; |
| 529 | |
| 530 | case Instruction::IF_LT: |
| 531 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT); |
| 532 | break; |
| 533 | |
| 534 | case Instruction::IF_GE: |
| 535 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE); |
| 536 | break; |
| 537 | |
| 538 | case Instruction::IF_GT: |
| 539 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT); |
| 540 | break; |
| 541 | |
| 542 | case Instruction::IF_LE: |
| 543 | EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE); |
| 544 | break; |
| 545 | |
| 546 | case Instruction::IF_EQZ: |
| 547 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ); |
| 548 | break; |
| 549 | |
| 550 | case Instruction::IF_NEZ: |
| 551 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE); |
| 552 | break; |
| 553 | |
| 554 | case Instruction::IF_LTZ: |
| 555 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT); |
| 556 | break; |
| 557 | |
| 558 | case Instruction::IF_GEZ: |
| 559 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE); |
| 560 | break; |
| 561 | |
| 562 | case Instruction::IF_GTZ: |
| 563 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT); |
| 564 | break; |
| 565 | |
| 566 | case Instruction::IF_LEZ: |
| 567 | EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE); |
| 568 | break; |
| 569 | |
| 570 | case Instruction::AGET: |
| 571 | EmitInsn_AGet(ARGS, kInt); |
| 572 | break; |
| 573 | |
| 574 | case Instruction::AGET_WIDE: |
| 575 | EmitInsn_AGet(ARGS, kLong); |
| 576 | break; |
| 577 | |
| 578 | case Instruction::AGET_OBJECT: |
| 579 | EmitInsn_AGet(ARGS, kObject); |
| 580 | break; |
| 581 | |
| 582 | case Instruction::AGET_BOOLEAN: |
| 583 | EmitInsn_AGet(ARGS, kBoolean); |
| 584 | break; |
| 585 | |
| 586 | case Instruction::AGET_BYTE: |
| 587 | EmitInsn_AGet(ARGS, kByte); |
| 588 | break; |
| 589 | |
| 590 | case Instruction::AGET_CHAR: |
| 591 | EmitInsn_AGet(ARGS, kChar); |
| 592 | break; |
| 593 | |
| 594 | case Instruction::AGET_SHORT: |
| 595 | EmitInsn_AGet(ARGS, kShort); |
| 596 | break; |
| 597 | |
| 598 | case Instruction::APUT: |
| 599 | EmitInsn_APut(ARGS, kInt); |
| 600 | break; |
| 601 | |
| 602 | case Instruction::APUT_WIDE: |
| 603 | EmitInsn_APut(ARGS, kLong); |
| 604 | break; |
| 605 | |
| 606 | case Instruction::APUT_OBJECT: |
| 607 | EmitInsn_APut(ARGS, kObject); |
| 608 | break; |
| 609 | |
| 610 | case Instruction::APUT_BOOLEAN: |
| 611 | EmitInsn_APut(ARGS, kBoolean); |
| 612 | break; |
| 613 | |
| 614 | case Instruction::APUT_BYTE: |
| 615 | EmitInsn_APut(ARGS, kByte); |
| 616 | break; |
| 617 | |
| 618 | case Instruction::APUT_CHAR: |
| 619 | EmitInsn_APut(ARGS, kChar); |
| 620 | break; |
| 621 | |
| 622 | case Instruction::APUT_SHORT: |
| 623 | EmitInsn_APut(ARGS, kShort); |
| 624 | break; |
| 625 | |
| 626 | case Instruction::IGET: |
| 627 | EmitInsn_IGet(ARGS, kInt); |
| 628 | break; |
| 629 | |
| 630 | case Instruction::IGET_WIDE: |
| 631 | EmitInsn_IGet(ARGS, kLong); |
| 632 | break; |
| 633 | |
| 634 | case Instruction::IGET_OBJECT: |
| 635 | EmitInsn_IGet(ARGS, kObject); |
| 636 | break; |
| 637 | |
| 638 | case Instruction::IGET_BOOLEAN: |
| 639 | EmitInsn_IGet(ARGS, kBoolean); |
| 640 | break; |
| 641 | |
| 642 | case Instruction::IGET_BYTE: |
| 643 | EmitInsn_IGet(ARGS, kByte); |
| 644 | break; |
| 645 | |
| 646 | case Instruction::IGET_CHAR: |
| 647 | EmitInsn_IGet(ARGS, kChar); |
| 648 | break; |
| 649 | |
| 650 | case Instruction::IGET_SHORT: |
| 651 | EmitInsn_IGet(ARGS, kShort); |
| 652 | break; |
| 653 | |
| 654 | case Instruction::IPUT: |
| 655 | EmitInsn_IPut(ARGS, kInt); |
| 656 | break; |
| 657 | |
| 658 | case Instruction::IPUT_WIDE: |
| 659 | EmitInsn_IPut(ARGS, kLong); |
| 660 | break; |
| 661 | |
| 662 | case Instruction::IPUT_OBJECT: |
| 663 | EmitInsn_IPut(ARGS, kObject); |
| 664 | break; |
| 665 | |
| 666 | case Instruction::IPUT_BOOLEAN: |
| 667 | EmitInsn_IPut(ARGS, kBoolean); |
| 668 | break; |
| 669 | |
| 670 | case Instruction::IPUT_BYTE: |
| 671 | EmitInsn_IPut(ARGS, kByte); |
| 672 | break; |
| 673 | |
| 674 | case Instruction::IPUT_CHAR: |
| 675 | EmitInsn_IPut(ARGS, kChar); |
| 676 | break; |
| 677 | |
| 678 | case Instruction::IPUT_SHORT: |
| 679 | EmitInsn_IPut(ARGS, kShort); |
| 680 | break; |
| 681 | |
| 682 | case Instruction::SGET: |
| 683 | EmitInsn_SGet(ARGS, kInt); |
| 684 | break; |
| 685 | |
| 686 | case Instruction::SGET_WIDE: |
| 687 | EmitInsn_SGet(ARGS, kLong); |
| 688 | break; |
| 689 | |
| 690 | case Instruction::SGET_OBJECT: |
| 691 | EmitInsn_SGet(ARGS, kObject); |
| 692 | break; |
| 693 | |
| 694 | case Instruction::SGET_BOOLEAN: |
| 695 | EmitInsn_SGet(ARGS, kBoolean); |
| 696 | break; |
| 697 | |
| 698 | case Instruction::SGET_BYTE: |
| 699 | EmitInsn_SGet(ARGS, kByte); |
| 700 | break; |
| 701 | |
| 702 | case Instruction::SGET_CHAR: |
| 703 | EmitInsn_SGet(ARGS, kChar); |
| 704 | break; |
| 705 | |
| 706 | case Instruction::SGET_SHORT: |
| 707 | EmitInsn_SGet(ARGS, kShort); |
| 708 | break; |
| 709 | |
| 710 | case Instruction::SPUT: |
| 711 | EmitInsn_SPut(ARGS, kInt); |
| 712 | break; |
| 713 | |
| 714 | case Instruction::SPUT_WIDE: |
| 715 | EmitInsn_SPut(ARGS, kLong); |
| 716 | break; |
| 717 | |
| 718 | case Instruction::SPUT_OBJECT: |
| 719 | EmitInsn_SPut(ARGS, kObject); |
| 720 | break; |
| 721 | |
| 722 | case Instruction::SPUT_BOOLEAN: |
| 723 | EmitInsn_SPut(ARGS, kBoolean); |
| 724 | break; |
| 725 | |
| 726 | case Instruction::SPUT_BYTE: |
| 727 | EmitInsn_SPut(ARGS, kByte); |
| 728 | break; |
| 729 | |
| 730 | case Instruction::SPUT_CHAR: |
| 731 | EmitInsn_SPut(ARGS, kChar); |
| 732 | break; |
| 733 | |
| 734 | case Instruction::SPUT_SHORT: |
| 735 | EmitInsn_SPut(ARGS, kShort); |
| 736 | break; |
| 737 | |
| 738 | |
| 739 | case Instruction::INVOKE_VIRTUAL: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 740 | EmitInsn_Invoke(ARGS, kVirtual, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 741 | break; |
| 742 | |
| 743 | case Instruction::INVOKE_SUPER: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 744 | EmitInsn_Invoke(ARGS, kSuper, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 745 | break; |
| 746 | |
| 747 | case Instruction::INVOKE_DIRECT: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 748 | EmitInsn_Invoke(ARGS, kDirect, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 749 | break; |
| 750 | |
| 751 | case Instruction::INVOKE_STATIC: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 752 | EmitInsn_Invoke(ARGS, kStatic, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 753 | break; |
| 754 | |
| 755 | case Instruction::INVOKE_INTERFACE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 756 | EmitInsn_Invoke(ARGS, kInterface, kArgReg); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 757 | break; |
| 758 | |
| 759 | case Instruction::INVOKE_VIRTUAL_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 760 | EmitInsn_Invoke(ARGS, kVirtual, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 761 | break; |
| 762 | |
| 763 | case Instruction::INVOKE_SUPER_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 764 | EmitInsn_Invoke(ARGS, kSuper, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 765 | break; |
| 766 | |
| 767 | case Instruction::INVOKE_DIRECT_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 768 | EmitInsn_Invoke(ARGS, kDirect, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 769 | break; |
| 770 | |
| 771 | case Instruction::INVOKE_STATIC_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 772 | EmitInsn_Invoke(ARGS, kStatic, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 773 | break; |
| 774 | |
| 775 | case Instruction::INVOKE_INTERFACE_RANGE: |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 776 | EmitInsn_Invoke(ARGS, kInterface, kArgRange); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 777 | break; |
| 778 | |
| 779 | case Instruction::NEG_INT: |
| 780 | EmitInsn_Neg(ARGS, kInt); |
| 781 | break; |
| 782 | |
| 783 | case Instruction::NOT_INT: |
| 784 | EmitInsn_Not(ARGS, kInt); |
| 785 | break; |
| 786 | |
| 787 | case Instruction::NEG_LONG: |
| 788 | EmitInsn_Neg(ARGS, kLong); |
| 789 | break; |
| 790 | |
| 791 | case Instruction::NOT_LONG: |
| 792 | EmitInsn_Not(ARGS, kLong); |
| 793 | break; |
| 794 | |
| 795 | case Instruction::NEG_FLOAT: |
| 796 | EmitInsn_FNeg(ARGS, kFloat); |
| 797 | break; |
| 798 | |
| 799 | case Instruction::NEG_DOUBLE: |
| 800 | EmitInsn_FNeg(ARGS, kDouble); |
| 801 | break; |
| 802 | |
| 803 | case Instruction::INT_TO_LONG: |
| 804 | EmitInsn_SExt(ARGS); |
| 805 | break; |
| 806 | |
| 807 | case Instruction::INT_TO_FLOAT: |
| 808 | EmitInsn_IntToFP(ARGS, kInt, kFloat); |
| 809 | break; |
| 810 | |
| 811 | case Instruction::INT_TO_DOUBLE: |
| 812 | EmitInsn_IntToFP(ARGS, kInt, kDouble); |
| 813 | break; |
| 814 | |
| 815 | case Instruction::LONG_TO_INT: |
| 816 | EmitInsn_Trunc(ARGS); |
| 817 | break; |
| 818 | |
| 819 | case Instruction::LONG_TO_FLOAT: |
| 820 | EmitInsn_IntToFP(ARGS, kLong, kFloat); |
| 821 | break; |
| 822 | |
| 823 | case Instruction::LONG_TO_DOUBLE: |
| 824 | EmitInsn_IntToFP(ARGS, kLong, kDouble); |
| 825 | break; |
| 826 | |
| 827 | case Instruction::FLOAT_TO_INT: |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 828 | EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 829 | break; |
| 830 | |
| 831 | case Instruction::FLOAT_TO_LONG: |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 832 | EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 833 | break; |
| 834 | |
| 835 | case Instruction::FLOAT_TO_DOUBLE: |
| 836 | EmitInsn_FExt(ARGS); |
| 837 | break; |
| 838 | |
| 839 | case Instruction::DOUBLE_TO_INT: |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 840 | EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 841 | break; |
| 842 | |
| 843 | case Instruction::DOUBLE_TO_LONG: |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 844 | EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | case Instruction::DOUBLE_TO_FLOAT: |
| 848 | EmitInsn_FTrunc(ARGS); |
| 849 | break; |
| 850 | |
| 851 | case Instruction::INT_TO_BYTE: |
| 852 | EmitInsn_TruncAndSExt(ARGS, 8); |
| 853 | break; |
| 854 | |
| 855 | case Instruction::INT_TO_CHAR: |
| 856 | EmitInsn_TruncAndZExt(ARGS, 16); |
| 857 | break; |
| 858 | |
| 859 | case Instruction::INT_TO_SHORT: |
| 860 | EmitInsn_TruncAndSExt(ARGS, 16); |
| 861 | break; |
| 862 | |
| 863 | case Instruction::ADD_INT: |
| 864 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false); |
| 865 | break; |
| 866 | |
| 867 | case Instruction::SUB_INT: |
| 868 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false); |
| 869 | break; |
| 870 | |
| 871 | case Instruction::MUL_INT: |
| 872 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false); |
| 873 | break; |
| 874 | |
| 875 | case Instruction::DIV_INT: |
| 876 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false); |
| 877 | break; |
| 878 | |
| 879 | case Instruction::REM_INT: |
| 880 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false); |
| 881 | break; |
| 882 | |
| 883 | case Instruction::AND_INT: |
| 884 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false); |
| 885 | break; |
| 886 | |
| 887 | case Instruction::OR_INT: |
| 888 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false); |
| 889 | break; |
| 890 | |
| 891 | case Instruction::XOR_INT: |
| 892 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false); |
| 893 | break; |
| 894 | |
| 895 | case Instruction::SHL_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 896 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 897 | break; |
| 898 | |
| 899 | case Instruction::SHR_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 900 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 901 | break; |
| 902 | |
| 903 | case Instruction::USHR_INT: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 904 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 905 | break; |
| 906 | |
| 907 | case Instruction::ADD_LONG: |
| 908 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false); |
| 909 | break; |
| 910 | |
| 911 | case Instruction::SUB_LONG: |
| 912 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false); |
| 913 | break; |
| 914 | |
| 915 | case Instruction::MUL_LONG: |
| 916 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false); |
| 917 | break; |
| 918 | |
| 919 | case Instruction::DIV_LONG: |
| 920 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false); |
| 921 | break; |
| 922 | |
| 923 | case Instruction::REM_LONG: |
| 924 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false); |
| 925 | break; |
| 926 | |
| 927 | case Instruction::AND_LONG: |
| 928 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false); |
| 929 | break; |
| 930 | |
| 931 | case Instruction::OR_LONG: |
| 932 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false); |
| 933 | break; |
| 934 | |
| 935 | case Instruction::XOR_LONG: |
| 936 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false); |
| 937 | break; |
| 938 | |
| 939 | case Instruction::SHL_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 940 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 941 | break; |
| 942 | |
| 943 | case Instruction::SHR_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 944 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 945 | break; |
| 946 | |
| 947 | case Instruction::USHR_LONG: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 948 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 949 | break; |
| 950 | |
| 951 | case Instruction::ADD_FLOAT: |
| 952 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false); |
| 953 | break; |
| 954 | |
| 955 | case Instruction::SUB_FLOAT: |
| 956 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false); |
| 957 | break; |
| 958 | |
| 959 | case Instruction::MUL_FLOAT: |
| 960 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false); |
| 961 | break; |
| 962 | |
| 963 | case Instruction::DIV_FLOAT: |
| 964 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false); |
| 965 | break; |
| 966 | |
| 967 | case Instruction::REM_FLOAT: |
| 968 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false); |
| 969 | break; |
| 970 | |
| 971 | case Instruction::ADD_DOUBLE: |
| 972 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false); |
| 973 | break; |
| 974 | |
| 975 | case Instruction::SUB_DOUBLE: |
| 976 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false); |
| 977 | break; |
| 978 | |
| 979 | case Instruction::MUL_DOUBLE: |
| 980 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false); |
| 981 | break; |
| 982 | |
| 983 | case Instruction::DIV_DOUBLE: |
| 984 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false); |
| 985 | break; |
| 986 | |
| 987 | case Instruction::REM_DOUBLE: |
| 988 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false); |
| 989 | break; |
| 990 | |
| 991 | case Instruction::ADD_INT_2ADDR: |
| 992 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true); |
| 993 | break; |
| 994 | |
| 995 | case Instruction::SUB_INT_2ADDR: |
| 996 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true); |
| 997 | break; |
| 998 | |
| 999 | case Instruction::MUL_INT_2ADDR: |
| 1000 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true); |
| 1001 | break; |
| 1002 | |
| 1003 | case Instruction::DIV_INT_2ADDR: |
| 1004 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true); |
| 1005 | break; |
| 1006 | |
| 1007 | case Instruction::REM_INT_2ADDR: |
| 1008 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true); |
| 1009 | break; |
| 1010 | |
| 1011 | case Instruction::AND_INT_2ADDR: |
| 1012 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true); |
| 1013 | break; |
| 1014 | |
| 1015 | case Instruction::OR_INT_2ADDR: |
| 1016 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true); |
| 1017 | break; |
| 1018 | |
| 1019 | case Instruction::XOR_INT_2ADDR: |
| 1020 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true); |
| 1021 | break; |
| 1022 | |
| 1023 | case Instruction::SHL_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1024 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1025 | break; |
| 1026 | |
| 1027 | case Instruction::SHR_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1028 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1029 | break; |
| 1030 | |
| 1031 | case Instruction::USHR_INT_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1032 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1033 | break; |
| 1034 | |
| 1035 | case Instruction::ADD_LONG_2ADDR: |
| 1036 | EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true); |
| 1037 | break; |
| 1038 | |
| 1039 | case Instruction::SUB_LONG_2ADDR: |
| 1040 | EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true); |
| 1041 | break; |
| 1042 | |
| 1043 | case Instruction::MUL_LONG_2ADDR: |
| 1044 | EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true); |
| 1045 | break; |
| 1046 | |
| 1047 | case Instruction::DIV_LONG_2ADDR: |
| 1048 | EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true); |
| 1049 | break; |
| 1050 | |
| 1051 | case Instruction::REM_LONG_2ADDR: |
| 1052 | EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true); |
| 1053 | break; |
| 1054 | |
| 1055 | case Instruction::AND_LONG_2ADDR: |
| 1056 | EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true); |
| 1057 | break; |
| 1058 | |
| 1059 | case Instruction::OR_LONG_2ADDR: |
| 1060 | EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true); |
| 1061 | break; |
| 1062 | |
| 1063 | case Instruction::XOR_LONG_2ADDR: |
| 1064 | EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true); |
| 1065 | break; |
| 1066 | |
| 1067 | case Instruction::SHL_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1068 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1069 | break; |
| 1070 | |
| 1071 | case Instruction::SHR_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1072 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1073 | break; |
| 1074 | |
| 1075 | case Instruction::USHR_LONG_2ADDR: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1076 | EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case Instruction::ADD_FLOAT_2ADDR: |
| 1080 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true); |
| 1081 | break; |
| 1082 | |
| 1083 | case Instruction::SUB_FLOAT_2ADDR: |
| 1084 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true); |
| 1085 | break; |
| 1086 | |
| 1087 | case Instruction::MUL_FLOAT_2ADDR: |
| 1088 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true); |
| 1089 | break; |
| 1090 | |
| 1091 | case Instruction::DIV_FLOAT_2ADDR: |
| 1092 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true); |
| 1093 | break; |
| 1094 | |
| 1095 | case Instruction::REM_FLOAT_2ADDR: |
| 1096 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true); |
| 1097 | break; |
| 1098 | |
| 1099 | case Instruction::ADD_DOUBLE_2ADDR: |
| 1100 | EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true); |
| 1101 | break; |
| 1102 | |
| 1103 | case Instruction::SUB_DOUBLE_2ADDR: |
| 1104 | EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true); |
| 1105 | break; |
| 1106 | |
| 1107 | case Instruction::MUL_DOUBLE_2ADDR: |
| 1108 | EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true); |
| 1109 | break; |
| 1110 | |
| 1111 | case Instruction::DIV_DOUBLE_2ADDR: |
| 1112 | EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true); |
| 1113 | break; |
| 1114 | |
| 1115 | case Instruction::REM_DOUBLE_2ADDR: |
| 1116 | EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true); |
| 1117 | break; |
| 1118 | |
| 1119 | case Instruction::ADD_INT_LIT16: |
| 1120 | case Instruction::ADD_INT_LIT8: |
| 1121 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add); |
| 1122 | break; |
| 1123 | |
| 1124 | case Instruction::RSUB_INT: |
| 1125 | case Instruction::RSUB_INT_LIT8: |
| 1126 | EmitInsn_RSubImmediate(ARGS); |
| 1127 | break; |
| 1128 | |
| 1129 | case Instruction::MUL_INT_LIT16: |
| 1130 | case Instruction::MUL_INT_LIT8: |
| 1131 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul); |
| 1132 | break; |
| 1133 | |
| 1134 | case Instruction::DIV_INT_LIT16: |
| 1135 | case Instruction::DIV_INT_LIT8: |
| 1136 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div); |
| 1137 | break; |
| 1138 | |
| 1139 | case Instruction::REM_INT_LIT16: |
| 1140 | case Instruction::REM_INT_LIT8: |
| 1141 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem); |
| 1142 | break; |
| 1143 | |
| 1144 | case Instruction::AND_INT_LIT16: |
| 1145 | case Instruction::AND_INT_LIT8: |
| 1146 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And); |
| 1147 | break; |
| 1148 | |
| 1149 | case Instruction::OR_INT_LIT16: |
| 1150 | case Instruction::OR_INT_LIT8: |
| 1151 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or); |
| 1152 | break; |
| 1153 | |
| 1154 | case Instruction::XOR_INT_LIT16: |
| 1155 | case Instruction::XOR_INT_LIT8: |
| 1156 | EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor); |
| 1157 | break; |
| 1158 | |
| 1159 | case Instruction::SHL_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1160 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1161 | break; |
| 1162 | |
| 1163 | case Instruction::SHR_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1164 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1165 | break; |
| 1166 | |
| 1167 | case Instruction::USHR_INT_LIT8: |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 1168 | EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1169 | break; |
| 1170 | |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1171 | case Instruction::THROW_VERIFICATION_ERROR: |
| 1172 | EmitInsn_ThrowVerificationError(ARGS); |
| 1173 | break; |
| 1174 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1175 | case Instruction::UNUSED_3E: |
| 1176 | case Instruction::UNUSED_3F: |
| 1177 | case Instruction::UNUSED_40: |
| 1178 | case Instruction::UNUSED_41: |
| 1179 | case Instruction::UNUSED_42: |
| 1180 | case Instruction::UNUSED_43: |
| 1181 | case Instruction::UNUSED_73: |
| 1182 | case Instruction::UNUSED_79: |
| 1183 | case Instruction::UNUSED_7A: |
| 1184 | case Instruction::UNUSED_E3: |
| 1185 | case Instruction::UNUSED_E4: |
| 1186 | case Instruction::UNUSED_E5: |
| 1187 | case Instruction::UNUSED_E6: |
| 1188 | case Instruction::UNUSED_E7: |
| 1189 | case Instruction::UNUSED_E8: |
| 1190 | case Instruction::UNUSED_E9: |
| 1191 | case Instruction::UNUSED_EA: |
| 1192 | case Instruction::UNUSED_EB: |
| 1193 | case Instruction::UNUSED_EC: |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1194 | case Instruction::UNUSED_EE: |
| 1195 | case Instruction::UNUSED_EF: |
| 1196 | case Instruction::UNUSED_F0: |
| 1197 | case Instruction::UNUSED_F1: |
| 1198 | case Instruction::UNUSED_F2: |
| 1199 | case Instruction::UNUSED_F3: |
| 1200 | case Instruction::UNUSED_F4: |
| 1201 | case Instruction::UNUSED_F5: |
| 1202 | case Instruction::UNUSED_F6: |
| 1203 | case Instruction::UNUSED_F7: |
| 1204 | case Instruction::UNUSED_F8: |
| 1205 | case Instruction::UNUSED_F9: |
| 1206 | case Instruction::UNUSED_FA: |
| 1207 | case Instruction::UNUSED_FB: |
| 1208 | case Instruction::UNUSED_FC: |
| 1209 | case Instruction::UNUSED_FD: |
| 1210 | case Instruction::UNUSED_FE: |
| 1211 | case Instruction::UNUSED_FF: |
| 1212 | LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode(); |
| 1213 | break; |
| 1214 | } |
| 1215 | |
| 1216 | #undef ARGS |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc, |
| 1221 | Instruction const* insn) { |
Logan Chien | e09a6b7 | 2011-12-27 17:50:21 +0800 | [diff] [blame] | 1222 | |
| 1223 | uint16_t insn_signature = code_item_->insns_[dex_pc]; |
| 1224 | |
| 1225 | if (insn_signature == Instruction::kPackedSwitchSignature || |
| 1226 | insn_signature == Instruction::kSparseSwitchSignature || |
| 1227 | insn_signature == Instruction::kArrayDataSignature) { |
| 1228 | irb_.CreateUnreachable(); |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 1229 | } else { |
Logan Chien | e09a6b7 | 2011-12-27 17:50:21 +0800 | [diff] [blame] | 1230 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1231 | } |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1235 | void MethodCompiler::EmitInsn_Move(uint32_t dex_pc, |
| 1236 | Instruction const* insn, |
| 1237 | JType jty) { |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1238 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1239 | DecodedInstruction dec_insn(insn); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1240 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1241 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg); |
| 1242 | EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1243 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1244 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc, |
| 1249 | Instruction const* insn, |
| 1250 | JType jty) { |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1251 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1252 | DecodedInstruction dec_insn(insn); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1253 | |
| 1254 | llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1255 | EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value); |
Logan Chien | 4817313 | 2011-12-27 17:51:13 +0800 | [diff] [blame] | 1256 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1257 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc, |
| 1262 | Instruction const* insn) { |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1263 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1264 | DecodedInstruction dec_insn(insn); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1265 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 1266 | // Get thread |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1267 | llvm::Value* thread_object_addr = |
| 1268 | irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1269 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 1270 | // Get thread-local exception field address |
| 1271 | llvm::Value* exception_object_addr = |
| 1272 | irb_.LoadFromObjectOffset(thread_object_addr, |
| 1273 | Thread::ExceptionOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 1274 | irb_.getJObjectTy(), |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 1275 | kTBAAJRuntime); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1276 | |
| 1277 | // Set thread-local exception field address to NULL |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 1278 | irb_.StoreToObjectOffset(thread_object_addr, |
| 1279 | Thread::ExceptionOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 1280 | irb_.getJNull(), |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 1281 | kTBAAJRuntime); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1282 | |
| 1283 | // Keep the exception object in the Dalvik register |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1284 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr); |
Logan Chien | 3354cec | 2012-01-13 14:29:03 +0800 | [diff] [blame] | 1285 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1286 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc, |
| 1291 | Instruction const* insn) { |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1292 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1293 | DecodedInstruction dec_insn(insn); |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1294 | |
| 1295 | llvm::Value* exception_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1296 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1297 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1298 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1299 | |
Logan Chien | 6c6f12d | 2012-01-13 19:26:27 +0800 | [diff] [blame] | 1300 | irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr); |
| 1301 | |
| 1302 | EmitBranchExceptionLandingPad(dex_pc); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1306 | void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc, |
| 1307 | Instruction const* insn) { |
| 1308 | |
| 1309 | DecodedInstruction dec_insn(insn); |
| 1310 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1311 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 1312 | |
| 1313 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1314 | llvm::Value* kind_value = irb_.getInt32(dec_insn.vA); |
| 1315 | llvm::Value* ref_value = irb_.getInt32(dec_insn.vB); |
| 1316 | |
| 1317 | irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError), |
| 1318 | method_object_addr, kind_value, ref_value); |
| 1319 | |
| 1320 | EmitBranchExceptionLandingPad(dex_pc); |
| 1321 | } |
| 1322 | |
| 1323 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1324 | void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc, |
| 1325 | Instruction const* insn) { |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1326 | // Pop the shadow frame |
| 1327 | EmitPopShadowFrame(); |
| 1328 | |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1329 | // Return! |
| 1330 | irb_.CreateRetVoid(); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | |
| 1334 | void MethodCompiler::EmitInsn_Return(uint32_t dex_pc, |
| 1335 | Instruction const* insn) { |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1336 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1337 | DecodedInstruction dec_insn(insn); |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1338 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1339 | // Pop the shadow frame |
| 1340 | EmitPopShadowFrame(); |
| 1341 | // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise, |
| 1342 | // the return value might be collected since the shadow stack is popped. |
| 1343 | |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1344 | // Return! |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1345 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1346 | llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate); |
Logan Chien | 8898a27 | 2011-12-27 17:51:56 +0800 | [diff] [blame] | 1347 | |
| 1348 | irb_.CreateRet(retval); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc, |
| 1353 | Instruction const* insn, |
| 1354 | JType imm_jty) { |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1355 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1356 | DecodedInstruction dec_insn(insn); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1357 | |
| 1358 | DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty; |
| 1359 | |
| 1360 | int64_t imm = 0; |
| 1361 | |
| 1362 | switch (insn->Opcode()) { |
| 1363 | // 32-bit Immediate |
| 1364 | case Instruction::CONST_4: |
| 1365 | case Instruction::CONST_16: |
| 1366 | case Instruction::CONST: |
| 1367 | case Instruction::CONST_WIDE_16: |
| 1368 | case Instruction::CONST_WIDE_32: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1369 | imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB)); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case Instruction::CONST_HIGH16: |
| 1373 | imm = static_cast<int64_t>(static_cast<int32_t>( |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1374 | static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16)); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1375 | break; |
| 1376 | |
| 1377 | // 64-bit Immediate |
| 1378 | case Instruction::CONST_WIDE: |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1379 | imm = static_cast<int64_t>(dec_insn.vB_wide); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1380 | break; |
| 1381 | |
| 1382 | case Instruction::CONST_WIDE_HIGH16: |
| 1383 | imm = static_cast<int64_t>( |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1384 | static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1385 | break; |
| 1386 | |
| 1387 | // Unknown opcode for load constant (unreachable) |
| 1388 | default: |
| 1389 | LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode(); |
| 1390 | break; |
| 1391 | } |
| 1392 | |
| 1393 | // Store the non-object register |
| 1394 | llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate); |
| 1395 | llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1396 | EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1397 | |
| 1398 | // Store the object register if it is possible to be null. |
| 1399 | if (imm_jty == kInt && imm == 0) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1400 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull()); |
Shih-wei Liao | 798366e | 2012-02-16 09:25:33 -0800 | [diff] [blame] | 1401 | } |
| 1402 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1403 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc, |
| 1408 | Instruction const* insn) { |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1409 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1410 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1411 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1412 | uint32_t string_idx = dec_insn.vB; |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1413 | |
| 1414 | llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx); |
| 1415 | |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 1416 | llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1417 | |
| 1418 | if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) { |
| 1419 | llvm::BasicBlock* block_str_exist = |
| 1420 | CreateBasicBlockWithDexPC(dex_pc, "str_exist"); |
| 1421 | |
| 1422 | llvm::BasicBlock* block_str_resolve = |
| 1423 | CreateBasicBlockWithDexPC(dex_pc, "str_resolve"); |
| 1424 | |
| 1425 | // Test: Is the string resolved and in the dex cache? |
| 1426 | llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull()); |
| 1427 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 1428 | irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1429 | |
| 1430 | // String is resolved, go to next basic block. |
| 1431 | irb_.SetInsertPoint(block_str_exist); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1432 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1433 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1434 | |
| 1435 | // String is not resolved yet, resolve it now. |
| 1436 | irb_.SetInsertPoint(block_str_resolve); |
| 1437 | |
| 1438 | llvm::Function* runtime_func = irb_.GetRuntime(ResolveString); |
| 1439 | |
| 1440 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1441 | |
| 1442 | llvm::Value* string_idx_value = irb_.getInt32(string_idx); |
| 1443 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1444 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1445 | |
| 1446 | string_addr = irb_.CreateCall2(runtime_func, method_object_addr, |
| 1447 | string_idx_value); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1448 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1449 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | // Store the string object to the Dalvik register |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1453 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr); |
Logan Chien | c3b4ba1 | 2012-01-16 19:52:53 +0800 | [diff] [blame] | 1454 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1455 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1456 | } |
| 1457 | |
| 1458 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1459 | llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc, |
| 1460 | uint32_t type_idx) { |
| 1461 | if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_, |
| 1462 | *dex_file_, type_idx)) { |
| 1463 | llvm::Value* type_idx_value = irb_.getInt32(type_idx); |
| 1464 | |
| 1465 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1466 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1467 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1468 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1469 | llvm::Function* runtime_func = |
| 1470 | irb_.GetRuntime(InitializeTypeAndVerifyAccess); |
| 1471 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1472 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1473 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1474 | llvm::Value* type_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1475 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1476 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1477 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1478 | |
| 1479 | return type_object_addr; |
| 1480 | |
| 1481 | } else { |
| 1482 | // Try to load the class (type) object from the test cache. |
| 1483 | llvm::Value* type_field_addr = |
| 1484 | EmitLoadDexCacheResolvedTypeFieldAddr(type_idx); |
| 1485 | |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 1486 | llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1487 | |
| 1488 | if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) { |
| 1489 | return type_object_addr; |
| 1490 | } |
| 1491 | |
| 1492 | llvm::BasicBlock* block_original = irb_.GetInsertBlock(); |
| 1493 | |
| 1494 | // Test whether class (type) object is in the dex cache or not |
| 1495 | llvm::Value* equal_null = |
| 1496 | irb_.CreateICmpEQ(type_object_addr, irb_.getJNull()); |
| 1497 | |
| 1498 | llvm::BasicBlock* block_cont = |
| 1499 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 1500 | |
| 1501 | llvm::BasicBlock* block_load_class = |
| 1502 | CreateBasicBlockWithDexPC(dex_pc, "load_class"); |
| 1503 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 1504 | irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1505 | |
| 1506 | // Failback routine to load the class object |
| 1507 | irb_.SetInsertPoint(block_load_class); |
| 1508 | |
| 1509 | llvm::Function* runtime_func = irb_.GetRuntime(InitializeType); |
| 1510 | |
| 1511 | llvm::Constant* type_idx_value = irb_.getInt32(type_idx); |
| 1512 | |
| 1513 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1514 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1515 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1516 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1517 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1518 | |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1519 | llvm::Value* loaded_type_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1520 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1521 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1522 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1523 | |
| 1524 | llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock(); |
| 1525 | |
| 1526 | irb_.CreateBr(block_cont); |
| 1527 | |
| 1528 | // Now the class object must be loaded |
| 1529 | irb_.SetInsertPoint(block_cont); |
| 1530 | |
| 1531 | llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2); |
| 1532 | |
| 1533 | phi->addIncoming(type_object_addr, block_original); |
| 1534 | phi->addIncoming(loaded_type_object_addr, block_after_load_class); |
| 1535 | |
| 1536 | return phi; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1541 | void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc, |
| 1542 | Instruction const* insn) { |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1543 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1544 | DecodedInstruction dec_insn(insn); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1545 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1546 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB); |
| 1547 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr); |
Logan Chien | 27b3025 | 2012-01-14 03:43:35 +0800 | [diff] [blame] | 1548 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1549 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc, |
| 1554 | Instruction const* insn) { |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1555 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1556 | DecodedInstruction dec_insn(insn); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1557 | |
| 1558 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1559 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1560 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 1561 | if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) { |
| 1562 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 1563 | } |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1564 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1565 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1566 | |
| 1567 | irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1568 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1569 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc, |
| 1574 | Instruction const* insn) { |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1575 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1576 | DecodedInstruction dec_insn(insn); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1577 | |
| 1578 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1579 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1580 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 1581 | if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) { |
| 1582 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 1583 | } |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1584 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1585 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1586 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 1587 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1588 | |
| 1589 | irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr); |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1590 | |
| 1591 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 9e0dbe4 | 2012-01-13 12:11:37 +0800 | [diff] [blame] | 1592 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1593 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc, |
| 1598 | Instruction const* insn) { |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1599 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1600 | DecodedInstruction dec_insn(insn); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1601 | |
| 1602 | llvm::BasicBlock* block_test_class = |
| 1603 | CreateBasicBlockWithDexPC(dex_pc, "test_class"); |
| 1604 | |
| 1605 | llvm::BasicBlock* block_test_sub_class = |
| 1606 | CreateBasicBlockWithDexPC(dex_pc, "test_sub_class"); |
| 1607 | |
| 1608 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1609 | EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1610 | |
| 1611 | // Test: Is the reference equal to null? Act as no-op when it is null. |
| 1612 | llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull()); |
| 1613 | |
| 1614 | irb_.CreateCondBr(equal_null, |
| 1615 | GetNextBasicBlock(dex_pc), |
| 1616 | block_test_class); |
| 1617 | |
| 1618 | // Test: Is the object instantiated from the given class? |
| 1619 | irb_.SetInsertPoint(block_test_class); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1620 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1621 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1622 | |
| 1623 | llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy(); |
| 1624 | |
| 1625 | llvm::Value* object_type_field_addr = |
| 1626 | irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo()); |
| 1627 | |
| 1628 | llvm::Value* object_type_object_addr = |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 1629 | irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1630 | |
| 1631 | llvm::Value* equal_class = |
| 1632 | irb_.CreateICmpEQ(type_object_addr, object_type_object_addr); |
| 1633 | |
| 1634 | irb_.CreateCondBr(equal_class, |
| 1635 | GetNextBasicBlock(dex_pc), |
| 1636 | block_test_sub_class); |
| 1637 | |
| 1638 | // Test: Is the object instantiated from the subclass of the given class? |
| 1639 | irb_.SetInsertPoint(block_test_sub_class); |
| 1640 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1641 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1642 | |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1643 | irb_.CreateCall2(irb_.GetRuntime(CheckCast), |
| 1644 | type_object_addr, object_type_object_addr); |
| 1645 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1646 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | fc88095 | 2012-01-15 23:53:10 +0800 | [diff] [blame] | 1647 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1648 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc, |
| 1653 | Instruction const* insn) { |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1654 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1655 | DecodedInstruction dec_insn(insn); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1656 | |
| 1657 | llvm::Constant* zero = irb_.getJInt(0); |
| 1658 | llvm::Constant* one = irb_.getJInt(1); |
| 1659 | |
| 1660 | llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp"); |
| 1661 | |
| 1662 | llvm::BasicBlock* block_test_class = |
| 1663 | CreateBasicBlockWithDexPC(dex_pc, "test_class"); |
| 1664 | |
| 1665 | llvm::BasicBlock* block_class_equals = |
| 1666 | CreateBasicBlockWithDexPC(dex_pc, "class_eq"); |
| 1667 | |
| 1668 | llvm::BasicBlock* block_test_sub_class = |
| 1669 | CreateBasicBlockWithDexPC(dex_pc, "test_sub_class"); |
| 1670 | |
| 1671 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1672 | EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1673 | |
| 1674 | // Overview of the following code : |
| 1675 | // We check for null, if so, then false, otherwise check for class == . If so |
| 1676 | // then true, otherwise do callout slowpath. |
| 1677 | // |
| 1678 | // Test: Is the reference equal to null? Set 0 when it is null. |
| 1679 | llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull()); |
| 1680 | |
| 1681 | irb_.CreateCondBr(equal_null, block_nullp, block_test_class); |
| 1682 | |
| 1683 | irb_.SetInsertPoint(block_nullp); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1684 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1685 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1686 | |
| 1687 | // Test: Is the object instantiated from the given class? |
| 1688 | irb_.SetInsertPoint(block_test_class); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1689 | llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1690 | DCHECK_EQ(Object::ClassOffset().Int32Value(), 0); |
| 1691 | |
| 1692 | llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy(); |
| 1693 | |
| 1694 | llvm::Value* object_type_field_addr = |
| 1695 | irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo()); |
| 1696 | |
| 1697 | llvm::Value* object_type_object_addr = |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 1698 | irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1699 | |
| 1700 | llvm::Value* equal_class = |
| 1701 | irb_.CreateICmpEQ(type_object_addr, object_type_object_addr); |
| 1702 | |
| 1703 | irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class); |
| 1704 | |
| 1705 | irb_.SetInsertPoint(block_class_equals); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1706 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1707 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1708 | |
| 1709 | // Test: Is the object instantiated from the subclass of the given class? |
| 1710 | irb_.SetInsertPoint(block_test_sub_class); |
| 1711 | |
| 1712 | llvm::Value* result = |
| 1713 | irb_.CreateCall2(irb_.GetRuntime(IsAssignable), |
| 1714 | type_object_addr, object_type_object_addr); |
| 1715 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1716 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 68725e2 | 2012-01-15 22:25:34 +0800 | [diff] [blame] | 1717 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1718 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1719 | } |
| 1720 | |
| 1721 | |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1722 | llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) { |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1723 | // Load array length |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 1724 | return irb_.LoadFromObjectOffset(array, |
| 1725 | Array::LengthOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 1726 | irb_.getJIntTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 1727 | kTBAAConstJObject); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1731 | void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc, |
| 1732 | Instruction const* insn) { |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1733 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1734 | DecodedInstruction dec_insn(insn); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1735 | |
| 1736 | // Get the array object address |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1737 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1738 | EmitGuard_NullPointerException(dex_pc, array_addr); |
| 1739 | |
| 1740 | // Get the array length and store it to the register |
| 1741 | llvm::Value* array_len = EmitLoadArrayLength(array_addr); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1742 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 1743 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1744 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc, |
| 1749 | Instruction const* insn) { |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1750 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1751 | DecodedInstruction dec_insn(insn); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1752 | |
| 1753 | llvm::Function* runtime_func; |
Logan Chien | 1a032b1 | 2012-04-11 11:43:04 +0800 | [diff] [blame] | 1754 | if (compiler_->CanAccessInstantiableTypeWithoutChecks( |
| 1755 | method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) { |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1756 | runtime_func = irb_.GetRuntime(AllocObject); |
| 1757 | } else { |
| 1758 | runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck); |
| 1759 | } |
| 1760 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1761 | llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1762 | |
| 1763 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1764 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1765 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1766 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1767 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1768 | |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1769 | llvm::Value* object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1770 | irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1771 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1772 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1773 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1774 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr); |
Logan Chien | 032bdad | 2012-01-16 09:59:23 +0800 | [diff] [blame] | 1775 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1776 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1777 | } |
| 1778 | |
| 1779 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1780 | llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc, |
| 1781 | int32_t length, |
| 1782 | uint32_t type_idx, |
| 1783 | bool is_filled_new_array) { |
| 1784 | llvm::Function* runtime_func; |
| 1785 | |
| 1786 | bool skip_access_check = |
| 1787 | compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_, |
| 1788 | *dex_file_, type_idx); |
| 1789 | |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1790 | llvm::Value* array_length_value; |
| 1791 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1792 | if (is_filled_new_array) { |
| 1793 | runtime_func = skip_access_check ? |
| 1794 | irb_.GetRuntime(CheckAndAllocArray) : |
| 1795 | irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck); |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1796 | array_length_value = irb_.getInt32(length); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1797 | } else { |
| 1798 | runtime_func = skip_access_check ? |
| 1799 | irb_.GetRuntime(AllocArray) : |
| 1800 | irb_.GetRuntime(AllocArrayWithAccessCheck); |
TDYa127 | a849cb6 | 2012-04-01 05:59:34 -0700 | [diff] [blame] | 1801 | array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | llvm::Constant* type_index_value = irb_.getInt32(type_idx); |
| 1805 | |
| 1806 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 1807 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1808 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 1809 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 1810 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 1811 | |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1812 | llvm::Value* object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 1813 | irb_.CreateCall4(runtime_func, type_index_value, method_object_addr, |
| 1814 | array_length_value, thread_object_addr); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1815 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1816 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1817 | |
| 1818 | return object_addr; |
| 1819 | } |
| 1820 | |
| 1821 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1822 | void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc, |
| 1823 | Instruction const* insn) { |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1824 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1825 | DecodedInstruction dec_insn(insn); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1826 | |
| 1827 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1828 | EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1829 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1830 | EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr); |
Logan Chien | a2cc6a3 | 2012-01-16 10:38:41 +0800 | [diff] [blame] | 1831 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1832 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1833 | } |
| 1834 | |
| 1835 | |
| 1836 | void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc, |
| 1837 | Instruction const* insn, |
| 1838 | bool is_range) { |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1839 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1840 | DecodedInstruction dec_insn(insn); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1841 | |
| 1842 | llvm::Value* object_addr = |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1843 | EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1844 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1845 | if (dec_insn.vA > 0) { |
Logan Chien | 4b1baf1 | 2012-05-18 16:28:36 +0800 | [diff] [blame] | 1846 | // Check for the element type |
| 1847 | uint32_t type_desc_len = 0; |
| 1848 | const char* type_desc = |
| 1849 | dex_file_->StringByTypeIdx(dec_insn.vB, &type_desc_len); |
| 1850 | |
| 1851 | DCHECK_GE(type_desc_len, 2u); // should be guaranteed by verifier |
| 1852 | DCHECK_EQ(type_desc[0], '['); // should be guaranteed by verifier |
| 1853 | bool is_elem_int_ty = (type_desc[1] == 'I'); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1854 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1855 | uint32_t alignment; |
| 1856 | llvm::Constant* elem_size; |
| 1857 | llvm::PointerType* field_type; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1858 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1859 | // NOTE: Currently filled-new-array only supports 'L', '[', and 'I' |
| 1860 | // as the element, thus we are only checking 2 cases: primitive int and |
| 1861 | // non-primitive type. |
Logan Chien | 4b1baf1 | 2012-05-18 16:28:36 +0800 | [diff] [blame] | 1862 | if (is_elem_int_ty) { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1863 | alignment = sizeof(int32_t); |
| 1864 | elem_size = irb_.getPtrEquivInt(sizeof(int32_t)); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1865 | field_type = irb_.getJIntTy()->getPointerTo(); |
| 1866 | } else { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1867 | alignment = irb_.getSizeOfPtrEquivInt(); |
| 1868 | elem_size = irb_.getSizeOfPtrEquivIntValue(); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1869 | field_type = irb_.getJObjectTy()->getPointerTo(); |
| 1870 | } |
| 1871 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1872 | llvm::Value* data_field_offset = |
| 1873 | irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value()); |
| 1874 | |
| 1875 | llvm::Value* data_field_addr = |
| 1876 | irb_.CreatePtrDisp(object_addr, data_field_offset, field_type); |
| 1877 | |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1878 | // TODO: Tune this code. Currently we are generating one instruction for |
| 1879 | // one element which may be very space consuming. Maybe changing to use |
| 1880 | // memcpy may help; however, since we can't guarantee that the alloca of |
| 1881 | // dalvik register are continuous, we can't perform such optimization yet. |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1882 | for (uint32_t i = 0; i < dec_insn.vA; ++i) { |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1883 | int reg_index; |
| 1884 | if (is_range) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1885 | reg_index = dec_insn.vC + i; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1886 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1887 | reg_index = dec_insn.arg[i]; |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | llvm::Value* reg_value; |
Logan Chien | 4b1baf1 | 2012-05-18 16:28:36 +0800 | [diff] [blame] | 1891 | if (is_elem_int_ty) { |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1892 | reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate); |
| 1893 | } else { |
| 1894 | reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate); |
| 1895 | } |
| 1896 | |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 1897 | irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1898 | |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 1899 | data_field_addr = |
| 1900 | irb_.CreatePtrDisp(data_field_addr, elem_size, field_type); |
Logan Chien | a85fb2f | 2012-01-16 12:52:56 +0800 | [diff] [blame] | 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr); |
| 1905 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1906 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1907 | } |
| 1908 | |
| 1909 | |
| 1910 | void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc, |
| 1911 | Instruction const* insn) { |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1912 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1913 | DecodedInstruction dec_insn(insn); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1914 | |
| 1915 | // Read the payload |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1916 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1917 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1918 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1919 | const Instruction::ArrayDataPayload* payload = |
| 1920 | reinterpret_cast<const Instruction::ArrayDataPayload*>( |
| 1921 | code_item_->insns_ + payload_offset); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1922 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1923 | // Load array object |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1924 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1925 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1926 | if (payload->element_count == 0) { |
| 1927 | // When the number of the elements in the payload is zero, we don't have |
| 1928 | // to copy any numbers. However, we should check whether the array object |
| 1929 | // address is equal to null or not. |
| 1930 | EmitGuard_NullPointerException(dex_pc, array_addr); |
| 1931 | } else { |
| 1932 | // To save the code size, we are going to call the runtime function to |
| 1933 | // copy the content from DexFile. |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1934 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1935 | // NOTE: We will check for the NullPointerException in the runtime. |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1936 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1937 | llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1938 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1939 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1940 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1941 | EmitUpdateDexPC(dex_pc); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1942 | |
Logan Chien | 86f5067 | 2012-04-24 13:08:45 +0800 | [diff] [blame] | 1943 | irb_.CreateCall4(runtime_func, |
| 1944 | method_object_addr, irb_.getInt32(dex_pc), |
| 1945 | array_addr, irb_.getInt32(payload_offset)); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1946 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 1947 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | e58b658 | 2012-01-16 17:13:13 +0800 | [diff] [blame] | 1948 | } |
| 1949 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1950 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 1951 | } |
| 1952 | |
| 1953 | |
| 1954 | void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc, |
| 1955 | Instruction const* insn) { |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1956 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1957 | DecodedInstruction dec_insn(insn); |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1958 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1959 | int32_t branch_offset = dec_insn.vA; |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1960 | |
Logan Chien | a466c16 | 2011-12-27 17:55:46 +0800 | [diff] [blame] | 1961 | irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset)); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | |
| 1965 | void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc, |
| 1966 | Instruction const* insn) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1967 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1968 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1969 | |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1970 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1971 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1972 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1973 | const Instruction::PackedSwitchPayload* payload = |
| 1974 | reinterpret_cast<const Instruction::PackedSwitchPayload*>( |
| 1975 | code_item_->insns_ + payload_offset); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1976 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1977 | llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1978 | |
| 1979 | llvm::SwitchInst* sw = |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1980 | irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1981 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1982 | for (uint16_t i = 0; i < payload->case_count; ++i) { |
| 1983 | sw->addCase(irb_.getInt32(payload->first_key + i), |
| 1984 | GetBasicBlock(dex_pc + payload->targets[i])); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1985 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc, |
| 1990 | Instruction const* insn) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1991 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1992 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1993 | |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1994 | int32_t payload_offset = static_cast<int32_t>(dex_pc) + |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 1995 | static_cast<int32_t>(dec_insn.vB); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 1996 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 1997 | const Instruction::SparseSwitchPayload* payload = |
| 1998 | reinterpret_cast<const Instruction::SparseSwitchPayload*>( |
| 1999 | code_item_->insns_ + payload_offset); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 2000 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 2001 | const int32_t* keys = payload->GetKeys(); |
| 2002 | const int32_t* targets = payload->GetTargets(); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 2003 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2004 | llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 2005 | |
| 2006 | llvm::SwitchInst* sw = |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 2007 | irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count); |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 2008 | |
Logan Chien | 19c350a | 2012-05-01 19:21:32 +0800 | [diff] [blame] | 2009 | for (size_t i = 0; i < payload->case_count; ++i) { |
Logan Chien | 7a89b6d | 2011-12-27 17:56:56 +0800 | [diff] [blame] | 2010 | sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i])); |
| 2011 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc, |
| 2016 | Instruction const* insn, |
| 2017 | JType fp_jty, |
| 2018 | bool gt_bias) { |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2019 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2020 | DecodedInstruction dec_insn(insn); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2021 | |
| 2022 | DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty; |
| 2023 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2024 | llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate); |
| 2025 | llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2026 | |
| 2027 | llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value); |
| 2028 | llvm::Value* cmp_lt; |
| 2029 | |
| 2030 | if (gt_bias) { |
| 2031 | cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value); |
| 2032 | } else { |
| 2033 | cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value); |
| 2034 | } |
| 2035 | |
| 2036 | llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2037 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2038 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2039 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2040 | } |
| 2041 | |
| 2042 | |
| 2043 | void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc, |
| 2044 | Instruction const* insn) { |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2045 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2046 | DecodedInstruction dec_insn(insn); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2047 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2048 | llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate); |
| 2049 | llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2050 | |
| 2051 | llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value); |
| 2052 | llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value); |
| 2053 | |
| 2054 | llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2055 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result); |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2056 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2057 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2058 | } |
| 2059 | |
| 2060 | |
Logan Chien | 2c37e8e | 2011-12-27 17:58:46 +0800 | [diff] [blame] | 2061 | llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq, |
| 2062 | llvm::Value* cmp_lt) { |
| 2063 | |
| 2064 | llvm::Constant* zero = irb_.getJInt(0); |
| 2065 | llvm::Constant* pos1 = irb_.getJInt(1); |
| 2066 | llvm::Constant* neg1 = irb_.getJInt(-1); |
| 2067 | |
| 2068 | llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1); |
| 2069 | llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt); |
| 2070 | |
| 2071 | return result_eq; |
| 2072 | } |
| 2073 | |
| 2074 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2075 | void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc, |
| 2076 | Instruction const* insn, |
| 2077 | CondBranchKind cond) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2078 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2079 | DecodedInstruction dec_insn(insn); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2080 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2081 | int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA); |
| 2082 | int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2083 | |
| 2084 | DCHECK_NE(kRegUnknown, src1_reg_cat); |
| 2085 | DCHECK_NE(kRegUnknown, src2_reg_cat); |
| 2086 | DCHECK_NE(kRegCat2, src1_reg_cat); |
| 2087 | DCHECK_NE(kRegCat2, src2_reg_cat); |
| 2088 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2089 | int32_t branch_offset = dec_insn.vC; |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2090 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2091 | llvm::Value* src1_value; |
| 2092 | llvm::Value* src2_value; |
| 2093 | |
TDYa127 | 8e9b449 | 2012-04-24 15:50:27 -0700 | [diff] [blame] | 2094 | if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) { |
| 2095 | src1_value = irb_.getInt32(0); |
| 2096 | src2_value = irb_.getInt32(0); |
| 2097 | } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2098 | CHECK_EQ(src1_reg_cat, src2_reg_cat); |
| 2099 | |
| 2100 | if (src1_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2101 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
| 2102 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2103 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2104 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
| 2105 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2106 | } |
| 2107 | } else { |
| 2108 | DCHECK(src1_reg_cat == kRegZero || |
| 2109 | src2_reg_cat == kRegZero); |
| 2110 | |
| 2111 | if (src1_reg_cat == kRegZero) { |
| 2112 | if (src2_reg_cat == kRegCat1nr) { |
| 2113 | src1_value = irb_.getJInt(0); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2114 | src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2115 | } else { |
| 2116 | src1_value = irb_.getJNull(); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2117 | src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2118 | } |
| 2119 | } else { // src2_reg_cat == kRegZero |
| 2120 | if (src2_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2121 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2122 | src2_value = irb_.getJInt(0); |
| 2123 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2124 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2125 | src2_value = irb_.getJNull(); |
| 2126 | } |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | llvm::Value* cond_value = |
| 2131 | EmitConditionResult(src1_value, src2_value, cond); |
| 2132 | |
| 2133 | irb_.CreateCondBr(cond_value, |
| 2134 | GetBasicBlock(dex_pc + branch_offset), |
| 2135 | GetNextBasicBlock(dex_pc)); |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | |
| 2139 | void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc, |
| 2140 | Instruction const* insn, |
| 2141 | CondBranchKind cond) { |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2142 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2143 | DecodedInstruction dec_insn(insn); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2144 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2145 | int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2146 | |
| 2147 | DCHECK_NE(kRegUnknown, src_reg_cat); |
| 2148 | DCHECK_NE(kRegCat2, src_reg_cat); |
| 2149 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2150 | int32_t branch_offset = dec_insn.vB; |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2151 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2152 | llvm::Value* src1_value; |
| 2153 | llvm::Value* src2_value; |
| 2154 | |
TDYa127 | 8e9b449 | 2012-04-24 15:50:27 -0700 | [diff] [blame] | 2155 | if (src_reg_cat == kRegZero) { |
| 2156 | src1_value = irb_.getInt32(0); |
| 2157 | src2_value = irb_.getInt32(0); |
| 2158 | } else if (src_reg_cat == kRegCat1nr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2159 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2160 | src2_value = irb_.getInt32(0); |
| 2161 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2162 | src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate); |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2163 | src2_value = irb_.getJNull(); |
| 2164 | } |
| 2165 | |
| 2166 | llvm::Value* cond_value = |
| 2167 | EmitConditionResult(src1_value, src2_value, cond); |
| 2168 | |
| 2169 | irb_.CreateCondBr(cond_value, |
| 2170 | GetBasicBlock(dex_pc + branch_offset), |
| 2171 | GetNextBasicBlock(dex_pc)); |
| 2172 | } |
| 2173 | |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 2174 | InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() { |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 2175 | Compiler::MethodReference mref(dex_file_, method_idx_); |
| 2176 | |
| 2177 | InferredRegCategoryMap const* map = |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 2178 | verifier::MethodVerifier::GetInferredRegCategoryMap(mref); |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 2179 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2180 | CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL)); |
| 2181 | |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 2182 | return map; |
| 2183 | } |
| 2184 | |
| 2185 | RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc, |
| 2186 | uint16_t reg_idx) { |
| 2187 | InferredRegCategoryMap const* map = GetInferredRegCategoryMap(); |
| 2188 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2189 | return map->GetRegCategory(dex_pc, reg_idx); |
| 2190 | } |
| 2191 | |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 2192 | bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) { |
| 2193 | InferredRegCategoryMap const* map = GetInferredRegCategoryMap(); |
| 2194 | |
| 2195 | return map->IsRegCanBeObject(reg_idx); |
| 2196 | } |
| 2197 | |
Logan Chien | a78e3c8 | 2011-12-27 17:59:35 +0800 | [diff] [blame] | 2198 | |
| 2199 | llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs, |
| 2200 | llvm::Value* rhs, |
| 2201 | CondBranchKind cond) { |
| 2202 | switch (cond) { |
| 2203 | case kCondBranch_EQ: |
| 2204 | return irb_.CreateICmpEQ(lhs, rhs); |
| 2205 | |
| 2206 | case kCondBranch_NE: |
| 2207 | return irb_.CreateICmpNE(lhs, rhs); |
| 2208 | |
| 2209 | case kCondBranch_LT: |
| 2210 | return irb_.CreateICmpSLT(lhs, rhs); |
| 2211 | |
| 2212 | case kCondBranch_GE: |
| 2213 | return irb_.CreateICmpSGE(lhs, rhs); |
| 2214 | |
| 2215 | case kCondBranch_GT: |
| 2216 | return irb_.CreateICmpSGT(lhs, rhs); |
| 2217 | |
| 2218 | case kCondBranch_LE: |
| 2219 | return irb_.CreateICmpSLE(lhs, rhs); |
| 2220 | |
| 2221 | default: // Unreachable |
| 2222 | LOG(FATAL) << "Unknown conditional branch kind: " << cond; |
| 2223 | return NULL; |
| 2224 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2225 | } |
| 2226 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2227 | void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) { |
| 2228 | // Using runtime support, let the target can override by InlineAssembly. |
| 2229 | llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard); |
| 2230 | |
| 2231 | irb_.CreateCall2(runtime_func, value, target_addr); |
| 2232 | } |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2233 | |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2234 | void |
| 2235 | MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc, |
| 2236 | llvm::Value* array, |
| 2237 | llvm::Value* index) { |
| 2238 | llvm::Value* array_len = EmitLoadArrayLength(array); |
| 2239 | |
| 2240 | llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len); |
| 2241 | |
| 2242 | llvm::BasicBlock* block_exception = |
| 2243 | CreateBasicBlockWithDexPC(dex_pc, "overflow"); |
| 2244 | |
| 2245 | llvm::BasicBlock* block_continue = |
| 2246 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2247 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 2248 | irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2249 | |
| 2250 | irb_.SetInsertPoint(block_exception); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2251 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2252 | EmitUpdateDexPC(dex_pc); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2253 | irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len); |
| 2254 | EmitBranchExceptionLandingPad(dex_pc); |
| 2255 | |
| 2256 | irb_.SetInsertPoint(block_continue); |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc, |
| 2261 | llvm::Value* array, |
| 2262 | llvm::Value* index) { |
| 2263 | EmitGuard_NullPointerException(dex_pc, array); |
| 2264 | EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index); |
| 2265 | } |
| 2266 | |
| 2267 | |
| 2268 | // Emit Array GetElementPtr |
| 2269 | llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr, |
| 2270 | llvm::Value* index_value, |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2271 | JType elem_jty) { |
| 2272 | |
| 2273 | int data_offset; |
| 2274 | if (elem_jty == kLong || elem_jty == kDouble || |
| 2275 | (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) { |
| 2276 | data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value(); |
| 2277 | } else { |
| 2278 | data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value(); |
| 2279 | } |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2280 | |
| 2281 | llvm::Constant* data_offset_value = |
Ian Rogers | 04ec04e | 2012-02-28 16:15:33 -0800 | [diff] [blame] | 2282 | irb_.getPtrEquivInt(data_offset); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2283 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 2284 | llvm::Type* elem_type = irb_.getJType(elem_jty, kArray); |
| 2285 | |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2286 | llvm::Value* array_data_addr = |
| 2287 | irb_.CreatePtrDisp(array_addr, data_offset_value, |
| 2288 | elem_type->getPointerTo()); |
| 2289 | |
| 2290 | return irb_.CreateGEP(array_data_addr, index_value); |
| 2291 | } |
| 2292 | |
| 2293 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2294 | void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc, |
| 2295 | Instruction const* insn, |
| 2296 | JType elem_jty) { |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2297 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2298 | DecodedInstruction dec_insn(insn); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2299 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2300 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
| 2301 | llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2302 | |
| 2303 | EmitGuard_ArrayException(dex_pc, array_addr, index_value); |
| 2304 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 2305 | llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2306 | |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2307 | llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2308 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2309 | EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value); |
Logan Chien | e27fdbb | 2012-01-02 23:27:26 +0800 | [diff] [blame] | 2310 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2311 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | void MethodCompiler::EmitInsn_APut(uint32_t dex_pc, |
| 2316 | Instruction const* insn, |
| 2317 | JType elem_jty) { |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2318 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2319 | DecodedInstruction dec_insn(insn); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2320 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2321 | llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate); |
| 2322 | llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2323 | |
| 2324 | EmitGuard_ArrayException(dex_pc, array_addr, index_value); |
| 2325 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 2326 | llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2327 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2328 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2329 | |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2330 | if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table. |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 2331 | llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement); |
| 2332 | |
| 2333 | irb_.CreateCall2(runtime_func, new_value, array_addr); |
| 2334 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2335 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2336 | |
| 2337 | EmitMarkGCCard(new_value, array_addr); |
TDYa127 | 1b86d07 | 2012-04-05 17:38:56 -0700 | [diff] [blame] | 2338 | } |
| 2339 | |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2340 | irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty); |
Logan Chien | 8dabb43 | 2012-01-02 23:29:32 +0800 | [diff] [blame] | 2341 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2342 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2343 | } |
| 2344 | |
| 2345 | |
| 2346 | void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc, |
| 2347 | Instruction const* insn, |
| 2348 | JType field_jty) { |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2349 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2350 | DecodedInstruction dec_insn(insn); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2351 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2352 | uint32_t reg_idx = dec_insn.vB; |
| 2353 | uint32_t field_idx = dec_insn.vC; |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2354 | |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2355 | llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate); |
| 2356 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 2357 | if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) { |
| 2358 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 2359 | } |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2360 | |
| 2361 | llvm::Value* field_value; |
| 2362 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2363 | int field_offset; |
| 2364 | bool is_volatile; |
| 2365 | bool is_fast_path = compiler_->ComputeInstanceFieldInfo( |
| 2366 | field_idx, oat_compilation_unit_, field_offset, is_volatile, false); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2367 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2368 | if (!is_fast_path) { |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2369 | llvm::Function* runtime_func; |
| 2370 | |
| 2371 | if (field_jty == kObject) { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2372 | runtime_func = irb_.GetRuntime(GetObjectInstance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2373 | } else if (field_jty == kLong || field_jty == kDouble) { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2374 | runtime_func = irb_.GetRuntime(Get64Instance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2375 | } else { |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2376 | runtime_func = irb_.GetRuntime(Get32Instance); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx); |
| 2380 | |
| 2381 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2382 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2383 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2384 | |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2385 | field_value = irb_.CreateCall3(runtime_func, field_idx_value, |
| 2386 | method_object_addr, object_addr); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2387 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2388 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2389 | |
| 2390 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2391 | DCHECK_GE(field_offset, 0); |
| 2392 | |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2393 | llvm::PointerType* field_type = |
| 2394 | irb_.getJType(field_jty, kField)->getPointerTo(); |
| 2395 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2396 | llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2397 | |
| 2398 | llvm::Value* field_addr = |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2399 | irb_.CreatePtrDisp(object_addr, field_offset_value, field_type); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2400 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2401 | // TODO: Check is_volatile. We need to generate atomic load instruction |
| 2402 | // when is_volatile is true. |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2403 | field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2404 | } |
| 2405 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2406 | EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value); |
Logan Chien | 48f1d2a | 2012-01-02 22:49:53 +0800 | [diff] [blame] | 2407 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2408 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc, |
| 2413 | Instruction const* insn, |
| 2414 | JType field_jty) { |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2415 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2416 | DecodedInstruction dec_insn(insn); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2417 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2418 | uint32_t reg_idx = dec_insn.vB; |
| 2419 | uint32_t field_idx = dec_insn.vC; |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2420 | |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2421 | llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate); |
| 2422 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 2423 | if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) { |
| 2424 | EmitGuard_NullPointerException(dex_pc, object_addr); |
| 2425 | } |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2426 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2427 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2428 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2429 | int field_offset; |
| 2430 | bool is_volatile; |
| 2431 | bool is_fast_path = compiler_->ComputeInstanceFieldInfo( |
| 2432 | field_idx, oat_compilation_unit_, field_offset, is_volatile, true); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2433 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2434 | if (!is_fast_path) { |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2435 | llvm::Function* runtime_func; |
| 2436 | |
| 2437 | if (field_jty == kObject) { |
| 2438 | runtime_func = irb_.GetRuntime(SetObjectInstance); |
| 2439 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2440 | runtime_func = irb_.GetRuntime(Set64Instance); |
| 2441 | } else { |
| 2442 | runtime_func = irb_.GetRuntime(Set32Instance); |
| 2443 | } |
| 2444 | |
| 2445 | llvm::Value* field_idx_value = irb_.getInt32(field_idx); |
| 2446 | |
| 2447 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2448 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2449 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2450 | |
Logan Chien | 3b2b2e7 | 2012-03-06 16:11:45 +0800 | [diff] [blame] | 2451 | irb_.CreateCall4(runtime_func, field_idx_value, |
| 2452 | method_object_addr, object_addr, new_value); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2453 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2454 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2455 | |
| 2456 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2457 | DCHECK_GE(field_offset, 0); |
| 2458 | |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2459 | llvm::PointerType* field_type = |
| 2460 | irb_.getJType(field_jty, kField)->getPointerTo(); |
| 2461 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2462 | llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2463 | |
| 2464 | llvm::Value* field_addr = |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2465 | irb_.CreatePtrDisp(object_addr, field_offset_value, field_type); |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2466 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2467 | // TODO: Check is_volatile. We need to generate atomic store instruction |
| 2468 | // when is_volatile is true. |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2469 | irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2470 | |
| 2471 | if (field_jty == kObject) { // If put an object, mark the GC card table. |
| 2472 | EmitMarkGCCard(new_value, object_addr); |
| 2473 | } |
Logan Chien | dd6aa87 | 2012-01-03 16:06:32 +0800 | [diff] [blame] | 2474 | } |
| 2475 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2476 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2477 | } |
| 2478 | |
| 2479 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2480 | llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc, |
| 2481 | uint32_t type_idx) { |
| 2482 | llvm::BasicBlock* block_load_static = |
| 2483 | CreateBasicBlockWithDexPC(dex_pc, "load_static"); |
| 2484 | |
| 2485 | llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2486 | |
| 2487 | // Load static storage from dex cache |
| 2488 | llvm::Value* storage_field_addr = |
| 2489 | EmitLoadDexCacheStaticStorageFieldAddr(type_idx); |
| 2490 | |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 2491 | llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2492 | |
| 2493 | llvm::BasicBlock* block_original = irb_.GetInsertBlock(); |
| 2494 | |
| 2495 | // Test: Is the static storage of this class initialized? |
| 2496 | llvm::Value* equal_null = |
| 2497 | irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull()); |
| 2498 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 2499 | irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2500 | |
| 2501 | // Failback routine to load the class object |
| 2502 | irb_.SetInsertPoint(block_load_static); |
| 2503 | |
| 2504 | llvm::Function* runtime_func = |
| 2505 | irb_.GetRuntime(InitializeStaticStorage); |
| 2506 | |
| 2507 | llvm::Constant* type_idx_value = irb_.getInt32(type_idx); |
| 2508 | |
| 2509 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2510 | |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 2511 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 2512 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2513 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2514 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2515 | llvm::Value* loaded_storage_object_addr = |
TDYa127 | 706e9b6 | 2012-04-19 12:24:26 -0700 | [diff] [blame] | 2516 | irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2517 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2518 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2519 | |
| 2520 | llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock(); |
| 2521 | |
| 2522 | irb_.CreateBr(block_cont); |
| 2523 | |
| 2524 | // Now the class object must be loaded |
| 2525 | irb_.SetInsertPoint(block_cont); |
| 2526 | |
| 2527 | llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2); |
| 2528 | |
| 2529 | phi->addIncoming(storage_object_addr, block_original); |
| 2530 | phi->addIncoming(loaded_storage_object_addr, block_after_load_static); |
| 2531 | |
| 2532 | return phi; |
| 2533 | } |
| 2534 | |
| 2535 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2536 | void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc, |
| 2537 | Instruction const* insn, |
| 2538 | JType field_jty) { |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2539 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2540 | DecodedInstruction dec_insn(insn); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2541 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2542 | uint32_t field_idx = dec_insn.vB; |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2543 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2544 | int field_offset; |
| 2545 | int ssb_index; |
| 2546 | bool is_referrers_class; |
| 2547 | bool is_volatile; |
| 2548 | |
| 2549 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 2550 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 2551 | is_referrers_class, is_volatile, false); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2552 | |
| 2553 | llvm::Value* static_field_value; |
| 2554 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2555 | if (!is_fast_path) { |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2556 | llvm::Function* runtime_func; |
| 2557 | |
| 2558 | if (field_jty == kObject) { |
| 2559 | runtime_func = irb_.GetRuntime(GetObjectStatic); |
| 2560 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2561 | runtime_func = irb_.GetRuntime(Get64Static); |
| 2562 | } else { |
| 2563 | runtime_func = irb_.GetRuntime(Get32Static); |
| 2564 | } |
| 2565 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2566 | llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2567 | |
| 2568 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2569 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2570 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2571 | |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2572 | static_field_value = |
| 2573 | irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr); |
| 2574 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2575 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2576 | |
| 2577 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2578 | DCHECK_GE(field_offset, 0); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2579 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2580 | llvm::Value* static_storage_addr = NULL; |
| 2581 | |
| 2582 | if (is_referrers_class) { |
| 2583 | // Fast path, static storage base is this method's class |
| 2584 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2585 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 2586 | static_storage_addr = |
| 2587 | irb_.LoadFromObjectOffset(method_object_addr, |
| 2588 | Method::DeclaringClassOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 2589 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 2590 | kTBAAConstJObject); |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2591 | } else { |
| 2592 | // Medium path, static storage base in a different class which |
| 2593 | // requires checks that the other class is initialized |
| 2594 | DCHECK_GE(ssb_index, 0); |
| 2595 | static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index); |
| 2596 | } |
| 2597 | |
| 2598 | llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2599 | |
| 2600 | llvm::Value* static_field_addr = |
| 2601 | irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value, |
| 2602 | irb_.getJType(field_jty, kField)->getPointerTo()); |
| 2603 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2604 | // TODO: Check is_volatile. We need to generate atomic load instruction |
| 2605 | // when is_volatile is true. |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2606 | static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2607 | } |
| 2608 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2609 | EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value); |
Logan Chien | 438c4b6 | 2012-01-17 16:06:00 +0800 | [diff] [blame] | 2610 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2611 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2612 | } |
| 2613 | |
| 2614 | |
| 2615 | void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc, |
| 2616 | Instruction const* insn, |
| 2617 | JType field_jty) { |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2618 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2619 | DecodedInstruction dec_insn(insn); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2620 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2621 | uint32_t field_idx = dec_insn.vB; |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2622 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2623 | llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2624 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2625 | int field_offset; |
| 2626 | int ssb_index; |
| 2627 | bool is_referrers_class; |
| 2628 | bool is_volatile; |
| 2629 | |
| 2630 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 2631 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 2632 | is_referrers_class, is_volatile, true); |
| 2633 | |
| 2634 | if (!is_fast_path) { |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2635 | llvm::Function* runtime_func; |
| 2636 | |
| 2637 | if (field_jty == kObject) { |
| 2638 | runtime_func = irb_.GetRuntime(SetObjectStatic); |
| 2639 | } else if (field_jty == kLong || field_jty == kDouble) { |
| 2640 | runtime_func = irb_.GetRuntime(Set64Static); |
| 2641 | } else { |
| 2642 | runtime_func = irb_.GetRuntime(Set32Static); |
| 2643 | } |
| 2644 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2645 | llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2646 | |
| 2647 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2648 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2649 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2650 | |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2651 | irb_.CreateCall3(runtime_func, field_idx_value, |
| 2652 | method_object_addr, new_value); |
| 2653 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2654 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2655 | |
| 2656 | } else { |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2657 | DCHECK_GE(field_offset, 0); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2658 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2659 | llvm::Value* static_storage_addr = NULL; |
| 2660 | |
| 2661 | if (is_referrers_class) { |
| 2662 | // Fast path, static storage base is this method's class |
| 2663 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 2664 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 2665 | static_storage_addr = |
| 2666 | irb_.LoadFromObjectOffset(method_object_addr, |
| 2667 | Method::DeclaringClassOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 2668 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 2669 | kTBAAConstJObject); |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2670 | } else { |
| 2671 | // Medium path, static storage base in a different class which |
| 2672 | // requires checks that the other class is initialized |
| 2673 | DCHECK_GE(ssb_index, 0); |
| 2674 | static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index); |
| 2675 | } |
| 2676 | |
| 2677 | llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset); |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2678 | |
| 2679 | llvm::Value* static_field_addr = |
| 2680 | irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value, |
| 2681 | irb_.getJType(field_jty, kField)->getPointerTo()); |
| 2682 | |
Logan Chien | 933abf8 | 2012-04-11 12:24:31 +0800 | [diff] [blame] | 2683 | // TODO: Check is_volatile. We need to generate atomic store instruction |
| 2684 | // when is_volatile is true. |
TDYa127 | 706e7db | 2012-05-06 00:05:33 -0700 | [diff] [blame] | 2685 | irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty); |
TDYa127 | 83bb662 | 2012-04-17 02:20:34 -0700 | [diff] [blame] | 2686 | |
| 2687 | if (field_jty == kObject) { // If put an object, mark the GC card table. |
| 2688 | EmitMarkGCCard(new_value, static_storage_addr); |
| 2689 | } |
Logan Chien | 14179c8 | 2012-01-17 17:06:34 +0800 | [diff] [blame] | 2690 | } |
| 2691 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2692 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2693 | } |
| 2694 | |
| 2695 | |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2696 | void MethodCompiler:: |
| 2697 | EmitLoadActualParameters(std::vector<llvm::Value*>& args, |
| 2698 | uint32_t callee_method_idx, |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2699 | DecodedInstruction const& dec_insn, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2700 | InvokeArgFmt arg_fmt, |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2701 | bool is_static) { |
| 2702 | |
| 2703 | // Get method signature |
| 2704 | DexFile::MethodId const& method_id = |
| 2705 | dex_file_->GetMethodId(callee_method_idx); |
| 2706 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2707 | uint32_t shorty_size; |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2708 | char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size); |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2709 | CHECK_GE(shorty_size, 1u); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2710 | |
| 2711 | // Load argument values according to the shorty (without "this") |
| 2712 | uint16_t reg_count = 0; |
| 2713 | |
| 2714 | if (!is_static) { |
| 2715 | ++reg_count; // skip the "this" pointer |
| 2716 | } |
| 2717 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2718 | bool is_range = (arg_fmt == kArgRange); |
| 2719 | |
Logan Chien | 8faf802 | 2012-02-24 12:25:29 +0800 | [diff] [blame] | 2720 | for (uint32_t i = 1; i < shorty_size; ++i) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2721 | uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count) |
| 2722 | : (dec_insn.arg[reg_count]); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2723 | |
| 2724 | args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate)); |
| 2725 | |
| 2726 | ++reg_count; |
| 2727 | if (shorty[i] == 'J' || shorty[i] == 'D') { |
| 2728 | // Wide types, such as long and double, are using a pair of registers |
| 2729 | // to store the value, so we have to increase arg_reg again. |
| 2730 | ++reg_count; |
| 2731 | } |
| 2732 | } |
| 2733 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2734 | DCHECK_EQ(reg_count, dec_insn.vA) |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2735 | << "Actual argument mismatch for callee: " |
| 2736 | << PrettyMethod(callee_method_idx, *dex_file_); |
| 2737 | } |
| 2738 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 2739 | llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr, |
| 2740 | uint32_t method_idx, |
| 2741 | bool is_static) { |
| 2742 | // TODO: Remove this after we solve the link and trampoline related problems. |
| 2743 | llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr); |
| 2744 | |
| 2745 | llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static); |
| 2746 | |
| 2747 | return irb_.CreatePointerCast(code_addr, method_type->getPointerTo()); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2748 | } |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2749 | |
Shih-wei Liao | 399ed3f | 2012-03-08 01:27:04 -0800 | [diff] [blame] | 2750 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2751 | void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc, |
| 2752 | Instruction const* insn, |
| 2753 | InvokeType invoke_type, |
| 2754 | InvokeArgFmt arg_fmt) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 2755 | DecodedInstruction dec_insn(insn); |
Logan Chien | 46fbb41 | 2012-02-15 22:29:08 +0800 | [diff] [blame] | 2756 | |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2757 | bool is_static = (invoke_type == kStatic); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2758 | uint32_t callee_method_idx = dec_insn.vB; |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2759 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2760 | // Compute invoke related information for compiler decision |
| 2761 | int vtable_idx = -1; |
Logan Chien | 92ad16d | 2012-03-18 05:48:55 +0800 | [diff] [blame] | 2762 | uintptr_t direct_code = 0; // Currently unused |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2763 | uintptr_t direct_method = 0; |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2764 | bool is_fast_path = compiler_-> |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2765 | ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_, |
Logan Chien | 92ad16d | 2012-03-18 05:48:55 +0800 | [diff] [blame] | 2766 | invoke_type, vtable_idx, direct_code, direct_method); |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 2767 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2768 | // Load *this* actual parameter |
Logan Chien | 82d31cd | 2012-05-23 18:37:44 +0800 | [diff] [blame] | 2769 | uint32_t this_reg = -1u; |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2770 | llvm::Value* this_addr = NULL; |
| 2771 | |
| 2772 | if (!is_static) { |
| 2773 | // Test: Is *this* parameter equal to null? |
Logan Chien | 82d31cd | 2012-05-23 18:37:44 +0800 | [diff] [blame] | 2774 | this_reg = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0); |
| 2775 | this_addr = EmitLoadDalvikReg(this_reg, kObject, kAccurate); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2776 | } |
| 2777 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2778 | // Load the method object |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2779 | llvm::Value* callee_method_object_addr = NULL; |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2780 | |
| 2781 | if (!is_fast_path) { |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2782 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2783 | EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type, |
| 2784 | this_addr, dex_pc, is_fast_path); |
Logan Chien | 82d31cd | 2012-05-23 18:37:44 +0800 | [diff] [blame] | 2785 | |
| 2786 | if (!is_static && (!method_info_.this_will_not_be_null || |
| 2787 | this_reg != method_info_.this_reg_idx)) { |
| 2788 | // NOTE: The null pointer test should come after the method resolution. |
| 2789 | // So that the "NoSuchMethodError" can be thrown before the |
| 2790 | // "NullPointerException". |
| 2791 | EmitGuard_NullPointerException(dex_pc, this_addr); |
| 2792 | } |
| 2793 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2794 | } else { |
Logan Chien | 82d31cd | 2012-05-23 18:37:44 +0800 | [diff] [blame] | 2795 | if (!is_static && (!method_info_.this_will_not_be_null || |
| 2796 | this_reg != method_info_.this_reg_idx)) { |
| 2797 | // NOTE: In the fast path, we should do the null pointer check |
| 2798 | // before the access to the class object and/or direct invocation. |
| 2799 | EmitGuard_NullPointerException(dex_pc, this_addr); |
| 2800 | } |
| 2801 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2802 | switch (invoke_type) { |
| 2803 | case kStatic: |
| 2804 | case kDirect: |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2805 | if (direct_method != 0u && |
| 2806 | direct_method != static_cast<uintptr_t>(-1)) { |
| 2807 | callee_method_object_addr = |
TDYa127 | 17826bf | 2012-04-24 01:15:10 -0700 | [diff] [blame] | 2808 | irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method), |
| 2809 | irb_.getJObjectTy()); |
Logan Chien | fca6437 | 2012-04-23 14:57:01 +0800 | [diff] [blame] | 2810 | } else { |
| 2811 | callee_method_object_addr = |
| 2812 | EmitLoadSDCalleeMethodObjectAddr(callee_method_idx); |
| 2813 | } |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2814 | break; |
| 2815 | |
| 2816 | case kVirtual: |
| 2817 | DCHECK(vtable_idx != -1); |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2818 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2819 | EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr); |
| 2820 | break; |
| 2821 | |
| 2822 | case kSuper: |
| 2823 | LOG(FATAL) << "invoke-super should be promoted to invoke-direct in " |
| 2824 | "the fast path."; |
| 2825 | break; |
| 2826 | |
| 2827 | case kInterface: |
TDYa127 | 4e42a59 | 2012-04-10 20:13:54 -0700 | [diff] [blame] | 2828 | callee_method_object_addr = |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2829 | EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, |
| 2830 | invoke_type, this_addr, |
| 2831 | dex_pc, is_fast_path); |
| 2832 | break; |
| 2833 | } |
| 2834 | } |
| 2835 | |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2836 | // Load the actual parameter |
| 2837 | std::vector<llvm::Value*> args; |
| 2838 | |
| 2839 | args.push_back(callee_method_object_addr); // method object for callee |
| 2840 | |
| 2841 | if (!is_static) { |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2842 | DCHECK(this_addr != NULL); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2843 | args.push_back(this_addr); // "this" object for callee |
| 2844 | } |
| 2845 | |
| 2846 | EmitLoadActualParameters(args, callee_method_idx, dec_insn, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2847 | arg_fmt, is_static); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2848 | |
TDYa127 | 29c0cd1 | 2012-05-17 04:51:08 -0700 | [diff] [blame] | 2849 | if (is_fast_path && (invoke_type == kDirect || invoke_type == kStatic)) { |
| 2850 | bool need_retry = EmitInlineJavaIntrinsic(PrettyMethod(callee_method_idx, *dex_file_), |
| 2851 | args, |
| 2852 | GetNextBasicBlock(dex_pc)); |
| 2853 | if (!need_retry) { |
| 2854 | return; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | llvm::Value* code_addr = |
| 2859 | irb_.LoadFromObjectOffset(callee_method_object_addr, |
| 2860 | Method::GetCodeOffset().Int32Value(), |
| 2861 | GetFunctionType(callee_method_idx, is_static)->getPointerTo(), |
| 2862 | kTBAAJRuntime); |
| 2863 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2864 | #if 0 |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 2865 | // Invoke callee |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2866 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2867 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2868 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2869 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2870 | uint32_t callee_access_flags = is_static ? kAccStatic : 0; |
| 2871 | UniquePtr<OatCompilationUnit> callee_oat_compilation_unit( |
| 2872 | oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags)); |
| 2873 | |
| 2874 | char ret_shorty = callee_oat_compilation_unit->GetShorty()[0]; |
Shih-wei Liao | 90d5099 | 2012-02-19 03:32:05 -0800 | [diff] [blame] | 2875 | if (ret_shorty != 'V') { |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2876 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2877 | } |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2878 | #else |
| 2879 | uint32_t callee_access_flags = is_static ? kAccStatic : 0; |
| 2880 | UniquePtr<OatCompilationUnit> callee_oat_compilation_unit( |
| 2881 | oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags)); |
| 2882 | |
| 2883 | char ret_shorty = callee_oat_compilation_unit->GetShorty()[0]; |
| 2884 | |
| 2885 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 2886 | EmitUpdateDexPC(dex_pc); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2887 | |
| 2888 | |
| 2889 | llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal"); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2890 | llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub"); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2891 | llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 2892 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2893 | llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy()); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2894 | llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub); |
| 2895 | llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int); |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 2896 | irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2897 | |
| 2898 | |
| 2899 | irb_.SetInsertPoint(block_normal); |
| 2900 | { |
| 2901 | // Invoke callee |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2902 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2903 | if (ret_shorty != 'V') { |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2904 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2905 | } |
| 2906 | } |
| 2907 | irb_.CreateBr(block_continue); |
| 2908 | |
| 2909 | |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2910 | irb_.SetInsertPoint(block_stub); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2911 | { |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2912 | { // lazy link |
| 2913 | // TODO: Remove this after we solve the link problem. |
| 2914 | llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy"); |
| 2915 | llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link"); |
| 2916 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 2917 | irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2918 | |
| 2919 | |
| 2920 | irb_.SetInsertPoint(block_link); |
| 2921 | code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static); |
| 2922 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2923 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2924 | |
| 2925 | llvm::Value* retval = irb_.CreateCall(code_addr, args); |
| 2926 | if (ret_shorty != 'V') { |
| 2927 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2928 | } |
| 2929 | irb_.CreateBr(block_continue); |
| 2930 | |
| 2931 | |
| 2932 | irb_.SetInsertPoint(block_proxy_stub); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2933 | } |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2934 | { // proxy stub |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2935 | if (ret_shorty != 'V') { |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 2936 | args.push_back(jvalue_temp_); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2937 | } |
| 2938 | // TODO: Remove this after we solve the proxy trampoline calling convention problem. |
| 2939 | irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args); |
| 2940 | if (ret_shorty != 'V') { |
Shih-wei Liao | e6a7adc | 2012-05-09 02:50:08 -0700 | [diff] [blame] | 2941 | llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2942 | llvm::Value* result_addr = |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 2943 | irb_.CreateBitCast(jvalue_temp_, accurate_ret_type->getPointerTo()); |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 2944 | llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp); |
TDYa127 | ce15472 | 2012-04-21 16:43:29 -0700 | [diff] [blame] | 2945 | EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval); |
| 2946 | } |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | irb_.CreateBr(block_continue); |
| 2950 | |
| 2951 | |
| 2952 | irb_.SetInsertPoint(block_continue); |
| 2953 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 2954 | EmitGuard_ExceptionLandingPad(dex_pc, true); |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 2955 | #endif |
Logan Chien | 1a121b9 | 2012-02-15 22:23:42 +0800 | [diff] [blame] | 2956 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 2957 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 2958 | } |
| 2959 | |
| 2960 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2961 | llvm::Value* MethodCompiler:: |
| 2962 | EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) { |
| 2963 | llvm::Value* callee_method_object_field_addr = |
| 2964 | EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2965 | |
TDYa127 | 8ca1005 | 2012-05-05 19:57:06 -0700 | [diff] [blame] | 2966 | return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2967 | } |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2968 | |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2969 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2970 | llvm::Value* MethodCompiler:: |
| 2971 | EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx, |
| 2972 | llvm::Value* this_addr) { |
| 2973 | // Load class object of *this* pointer |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 2974 | llvm::Value* class_object_addr = |
| 2975 | irb_.LoadFromObjectOffset(this_addr, |
| 2976 | Object::ClassOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 2977 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 2978 | kTBAAConstJObject); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 2979 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2980 | // Load vtable address |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 2981 | llvm::Value* vtable_addr = |
| 2982 | irb_.LoadFromObjectOffset(class_object_addr, |
| 2983 | Class::VTableOffset().Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 2984 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 2985 | kTBAAConstJObject); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2986 | |
| 2987 | // Load callee method object |
| 2988 | llvm::Value* vtable_idx_value = |
| 2989 | irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx)); |
| 2990 | |
| 2991 | llvm::Value* method_field_addr = |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 2992 | EmitArrayGEP(vtable_addr, vtable_idx_value, kObject); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2993 | |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 2994 | return irb_.CreateLoad(method_field_addr, kTBAAConstJObject); |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | |
| 2998 | llvm::Value* MethodCompiler:: |
| 2999 | EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx, |
| 3000 | InvokeType invoke_type, |
| 3001 | llvm::Value* this_addr, |
| 3002 | uint32_t dex_pc, |
| 3003 | bool is_fast_path) { |
| 3004 | |
| 3005 | llvm::Function* runtime_func = NULL; |
| 3006 | |
| 3007 | switch (invoke_type) { |
| 3008 | case kStatic: |
| 3009 | runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck); |
| 3010 | break; |
| 3011 | |
| 3012 | case kDirect: |
| 3013 | runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck); |
| 3014 | break; |
| 3015 | |
| 3016 | case kVirtual: |
| 3017 | runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck); |
| 3018 | break; |
| 3019 | |
| 3020 | case kSuper: |
| 3021 | runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck); |
| 3022 | break; |
| 3023 | |
| 3024 | case kInterface: |
| 3025 | if (is_fast_path) { |
| 3026 | runtime_func = irb_.GetRuntime(FindInterfaceMethod); |
| 3027 | } else { |
| 3028 | runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck); |
| 3029 | } |
| 3030 | break; |
| 3031 | } |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 3032 | |
| 3033 | llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx); |
| 3034 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3035 | if (this_addr == NULL) { |
| 3036 | DCHECK_EQ(invoke_type, kStatic); |
| 3037 | this_addr = irb_.getJNull(); |
| 3038 | } |
| 3039 | |
| 3040 | llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr(); |
| 3041 | |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3042 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 3043 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3044 | EmitUpdateDexPC(dex_pc); |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3045 | |
TDYa127 | 0b686e5 | 2012-04-09 22:43:35 -0700 | [diff] [blame] | 3046 | llvm::Value* callee_method_object_addr = |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3047 | irb_.CreateCall4(runtime_func, |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3048 | callee_method_idx_value, |
| 3049 | this_addr, |
TDYa127 | da83d97 | 2012-04-18 00:21:49 -0700 | [diff] [blame] | 3050 | caller_method_object_addr, |
| 3051 | thread_object_addr); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 3052 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 3053 | EmitGuard_ExceptionLandingPad(dex_pc, false); |
Logan Chien | 7caf37e | 2012-02-03 22:56:04 +0800 | [diff] [blame] | 3054 | |
Logan Chien | 7e7fabc | 2012-04-10 18:59:11 +0800 | [diff] [blame] | 3055 | return callee_method_object_addr; |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc, |
| 3060 | Instruction const* insn, |
| 3061 | JType op_jty) { |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3062 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3063 | DecodedInstruction dec_insn(insn); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3064 | |
| 3065 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3066 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3067 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3068 | llvm::Value* result_value = irb_.CreateNeg(src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3069 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 1b5685f | 2011-12-27 18:01:14 +0800 | [diff] [blame] | 3070 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3071 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | void MethodCompiler::EmitInsn_Not(uint32_t dex_pc, |
| 3076 | Instruction const* insn, |
| 3077 | JType op_jty) { |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3078 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3079 | DecodedInstruction dec_insn(insn); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3080 | |
| 3081 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3082 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3083 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3084 | llvm::Value* result_value = |
| 3085 | irb_.CreateXor(src_value, static_cast<uint64_t>(-1)); |
| 3086 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3087 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | e53750d | 2011-12-27 18:02:27 +0800 | [diff] [blame] | 3088 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3089 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc, |
| 3094 | Instruction const* insn) { |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3095 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3096 | DecodedInstruction dec_insn(insn); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3097 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3098 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3099 | llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3100 | EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value); |
Logan Chien | 61752ad | 2011-12-27 18:03:51 +0800 | [diff] [blame] | 3101 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3102 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3103 | } |
| 3104 | |
| 3105 | |
| 3106 | void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc, |
| 3107 | Instruction const* insn) { |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3108 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3109 | DecodedInstruction dec_insn(insn); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3110 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3111 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3112 | llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3113 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | 17a5766 | 2011-12-27 18:05:14 +0800 | [diff] [blame] | 3114 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3115 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3116 | } |
| 3117 | |
| 3118 | |
| 3119 | void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc, |
| 3120 | Instruction const* insn, |
| 3121 | unsigned N) { |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3122 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3123 | DecodedInstruction dec_insn(insn); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3124 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3125 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3126 | |
| 3127 | llvm::Value* trunc_value = |
| 3128 | irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N)); |
| 3129 | |
| 3130 | llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy()); |
| 3131 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3132 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3133 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3134 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3135 | } |
| 3136 | |
| 3137 | |
| 3138 | void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc, |
| 3139 | Instruction const* insn, |
| 3140 | unsigned N) { |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3141 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3142 | DecodedInstruction dec_insn(insn); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3143 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3144 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3145 | |
| 3146 | llvm::Value* trunc_value = |
| 3147 | irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N)); |
| 3148 | |
| 3149 | llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy()); |
| 3150 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3151 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | b6744c5 | 2011-12-27 18:06:26 +0800 | [diff] [blame] | 3152 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3153 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc, |
| 3158 | Instruction const* insn, |
| 3159 | JType op_jty) { |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3160 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3161 | DecodedInstruction dec_insn(insn); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3162 | |
| 3163 | DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty; |
| 3164 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3165 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3166 | llvm::Value* result_value = irb_.CreateFNeg(src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3167 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 7a48b09 | 2011-12-27 18:07:45 +0800 | [diff] [blame] | 3168 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3169 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3170 | } |
| 3171 | |
| 3172 | |
| 3173 | void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc, |
| 3174 | Instruction const* insn, |
| 3175 | JType src_jty, |
| 3176 | JType dest_jty) { |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3177 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3178 | DecodedInstruction dec_insn(insn); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3179 | |
| 3180 | DCHECK(src_jty == kInt || src_jty == kLong) << src_jty; |
| 3181 | DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty; |
| 3182 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3183 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3184 | llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate); |
| 3185 | llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3186 | EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value); |
Logan Chien | 62dd453 | 2011-12-27 18:09:00 +0800 | [diff] [blame] | 3187 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3188 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3189 | } |
| 3190 | |
| 3191 | |
| 3192 | void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc, |
| 3193 | Instruction const* insn, |
| 3194 | JType src_jty, |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 3195 | JType dest_jty, |
| 3196 | runtime_support::RuntimeId runtime_func_id) { |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3197 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3198 | DecodedInstruction dec_insn(insn); |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3199 | |
| 3200 | DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty; |
| 3201 | DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty; |
| 3202 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3203 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate); |
TDYa127 | a474687 | 2012-04-11 23:48:55 -0700 | [diff] [blame] | 3204 | llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3205 | EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value); |
Logan Chien | 12dc175 | 2011-12-27 18:10:15 +0800 | [diff] [blame] | 3206 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3207 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3208 | } |
| 3209 | |
| 3210 | |
| 3211 | void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc, |
| 3212 | Instruction const* insn) { |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3213 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3214 | DecodedInstruction dec_insn(insn); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3215 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3216 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3217 | llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3218 | EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value); |
Logan Chien | c56ded9 | 2011-12-27 18:10:57 +0800 | [diff] [blame] | 3219 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3220 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3221 | } |
| 3222 | |
| 3223 | |
| 3224 | void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc, |
| 3225 | Instruction const* insn) { |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3226 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3227 | DecodedInstruction dec_insn(insn); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3228 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3229 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3230 | llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy()); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3231 | EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value); |
Logan Chien | 927744f | 2011-12-27 18:11:52 +0800 | [diff] [blame] | 3232 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3233 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3234 | } |
| 3235 | |
| 3236 | |
| 3237 | void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc, |
| 3238 | Instruction const* insn, |
| 3239 | IntArithmKind arithm, |
| 3240 | JType op_jty, |
| 3241 | bool is_2addr) { |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3242 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3243 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3244 | |
| 3245 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3246 | |
| 3247 | llvm::Value* src1_value; |
| 3248 | llvm::Value* src2_value; |
| 3249 | |
| 3250 | if (is_2addr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3251 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3252 | src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3253 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3254 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3255 | src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | llvm::Value* result_value = |
| 3259 | EmitIntArithmResultComputation(dex_pc, src1_value, src2_value, |
| 3260 | arithm, op_jty); |
| 3261 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3262 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3263 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3264 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3265 | } |
| 3266 | |
| 3267 | |
| 3268 | void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc, |
| 3269 | Instruction const* insn, |
| 3270 | IntArithmKind arithm) { |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3271 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3272 | DecodedInstruction dec_insn(insn); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3273 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3274 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3275 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3276 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3277 | |
| 3278 | llvm::Value* result_value = |
| 3279 | EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt); |
| 3280 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3281 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3282 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3283 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3284 | } |
| 3285 | |
| 3286 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3287 | llvm::Value* |
| 3288 | MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc, |
| 3289 | llvm::Value* lhs, |
| 3290 | llvm::Value* rhs, |
| 3291 | IntArithmKind arithm, |
| 3292 | JType op_jty) { |
| 3293 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3294 | |
| 3295 | switch (arithm) { |
| 3296 | case kIntArithm_Add: |
| 3297 | return irb_.CreateAdd(lhs, rhs); |
| 3298 | |
| 3299 | case kIntArithm_Sub: |
| 3300 | return irb_.CreateSub(lhs, rhs); |
| 3301 | |
| 3302 | case kIntArithm_Mul: |
| 3303 | return irb_.CreateMul(lhs, rhs); |
| 3304 | |
| 3305 | case kIntArithm_Div: |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3306 | case kIntArithm_Rem: |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3307 | return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3308 | |
| 3309 | case kIntArithm_And: |
| 3310 | return irb_.CreateAnd(lhs, rhs); |
| 3311 | |
| 3312 | case kIntArithm_Or: |
| 3313 | return irb_.CreateOr(lhs, rhs); |
| 3314 | |
| 3315 | case kIntArithm_Xor: |
| 3316 | return irb_.CreateXor(lhs, rhs); |
| 3317 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3318 | default: |
| 3319 | LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm; |
| 3320 | return NULL; |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3325 | llvm::Value* |
| 3326 | MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc, |
| 3327 | llvm::Value* dividend, |
| 3328 | llvm::Value* divisor, |
| 3329 | IntArithmKind arithm, |
| 3330 | JType op_jty) { |
| 3331 | // Throw exception if the divisor is 0. |
| 3332 | EmitGuard_DivZeroException(dex_pc, divisor, op_jty); |
| 3333 | |
| 3334 | // Check the special case: MININT / -1 = MININT |
| 3335 | // That case will cause overflow, which is undefined behavior in llvm. |
| 3336 | // So we check the divisor is -1 or not, if the divisor is -1, we do |
| 3337 | // the special path to avoid undefined behavior. |
| 3338 | llvm::Type* op_type = irb_.getJType(op_jty, kAccurate); |
| 3339 | llvm::Value* zero = irb_.getJZero(op_jty); |
| 3340 | llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1); |
| 3341 | llvm::Value* result = irb_.CreateAlloca(op_type); |
| 3342 | |
| 3343 | llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one"); |
| 3344 | llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one"); |
| 3345 | llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont"); |
| 3346 | |
| 3347 | llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ); |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 3348 | irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely); |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3349 | |
| 3350 | // If divisor == -1 |
| 3351 | irb_.SetInsertPoint(eq_neg_one); |
| 3352 | llvm::Value* eq_result; |
| 3353 | if (arithm == kIntArithm_Div) { |
| 3354 | // We can just change from "dividend div -1" to "neg dividend". |
| 3355 | // The sub don't care the sign/unsigned because of two's complement representation. |
| 3356 | // And the behavior is what we want: |
| 3357 | // -(2^n) (2^n)-1 |
| 3358 | // MININT < k <= MAXINT -> mul k -1 = -k |
| 3359 | // MININT == k -> mul k -1 = k |
| 3360 | // |
| 3361 | // LLVM use sub to represent 'neg' |
| 3362 | eq_result = irb_.CreateSub(zero, dividend); |
| 3363 | } else { |
| 3364 | // Everything modulo -1 will be 0. |
| 3365 | eq_result = zero; |
| 3366 | } |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 3367 | irb_.CreateStore(eq_result, result, kTBAAStackTemp); |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3368 | irb_.CreateBr(neg_one_cont); |
| 3369 | |
| 3370 | // If divisor != -1, just do the division. |
| 3371 | irb_.SetInsertPoint(ne_neg_one); |
| 3372 | llvm::Value* ne_result; |
| 3373 | if (arithm == kIntArithm_Div) { |
| 3374 | ne_result = irb_.CreateSDiv(dividend, divisor); |
| 3375 | } else { |
| 3376 | ne_result = irb_.CreateSRem(dividend, divisor); |
| 3377 | } |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 3378 | irb_.CreateStore(ne_result, result, kTBAAStackTemp); |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3379 | irb_.CreateBr(neg_one_cont); |
| 3380 | |
| 3381 | irb_.SetInsertPoint(neg_one_cont); |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 3382 | return irb_.CreateLoad(result, kTBAAStackTemp); |
TDYa127 | f8641ce | 2012-04-02 06:40:40 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | |
Logan Chien | 5539ad0 | 2012-04-02 14:36:55 +0800 | [diff] [blame] | 3386 | void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc, |
| 3387 | Instruction const* insn, |
| 3388 | IntShiftArithmKind arithm, |
| 3389 | JType op_jty, |
| 3390 | bool is_2addr) { |
| 3391 | |
| 3392 | DecodedInstruction dec_insn(insn); |
| 3393 | |
| 3394 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3395 | |
| 3396 | llvm::Value* src1_value; |
| 3397 | llvm::Value* src2_value; |
| 3398 | |
| 3399 | // NOTE: The 2nd operand of the shift arithmetic instruction is |
| 3400 | // 32-bit integer regardless of the 1st operand. |
| 3401 | if (is_2addr) { |
| 3402 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3403 | src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3404 | } else { |
| 3405 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3406 | src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate); |
| 3407 | } |
| 3408 | |
| 3409 | llvm::Value* result_value = |
| 3410 | EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value, |
| 3411 | arithm, op_jty); |
| 3412 | |
| 3413 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
| 3414 | |
| 3415 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3416 | } |
| 3417 | |
| 3418 | |
| 3419 | void MethodCompiler:: |
| 3420 | EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc, |
| 3421 | Instruction const* insn, |
| 3422 | IntShiftArithmKind arithm) { |
| 3423 | |
| 3424 | DecodedInstruction dec_insn(insn); |
| 3425 | |
| 3426 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3427 | |
| 3428 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
| 3429 | |
| 3430 | llvm::Value* result_value = |
| 3431 | EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value, |
| 3432 | arithm, kInt); |
| 3433 | |
| 3434 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
| 3435 | |
| 3436 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | llvm::Value* |
| 3441 | MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc, |
| 3442 | llvm::Value* lhs, |
| 3443 | llvm::Value* rhs, |
| 3444 | IntShiftArithmKind arithm, |
| 3445 | JType op_jty) { |
| 3446 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3447 | |
| 3448 | if (op_jty == kInt) { |
| 3449 | rhs = irb_.CreateAnd(rhs, 0x1f); |
| 3450 | } else { |
| 3451 | llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f); |
| 3452 | rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy()); |
| 3453 | } |
| 3454 | |
| 3455 | switch (arithm) { |
| 3456 | case kIntArithm_Shl: |
| 3457 | return irb_.CreateShl(lhs, rhs); |
| 3458 | |
| 3459 | case kIntArithm_Shr: |
| 3460 | return irb_.CreateAShr(lhs, rhs); |
| 3461 | |
| 3462 | case kIntArithm_UShr: |
| 3463 | return irb_.CreateLShr(lhs, rhs); |
| 3464 | |
| 3465 | default: |
| 3466 | LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm; |
| 3467 | return NULL; |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3472 | void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc, |
| 3473 | Instruction const* insn) { |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3474 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3475 | DecodedInstruction dec_insn(insn); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3476 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3477 | llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate); |
| 3478 | llvm::Value* imm_value = irb_.getInt32(dec_insn.vC); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3479 | llvm::Value* result_value = irb_.CreateSub(imm_value, src_value); |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3480 | EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value); |
Logan Chien | 65c62d4 | 2011-12-27 18:14:18 +0800 | [diff] [blame] | 3481 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3482 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3483 | } |
| 3484 | |
| 3485 | |
| 3486 | void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc, |
| 3487 | Instruction const* insn, |
| 3488 | FPArithmKind arithm, |
| 3489 | JType op_jty, |
| 3490 | bool is_2addr) { |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3491 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3492 | DecodedInstruction dec_insn(insn); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3493 | |
| 3494 | DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty; |
| 3495 | |
| 3496 | llvm::Value* src1_value; |
| 3497 | llvm::Value* src2_value; |
| 3498 | |
| 3499 | if (is_2addr) { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3500 | src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate); |
| 3501 | src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3502 | } else { |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3503 | src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate); |
| 3504 | src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | llvm::Value* result_value = |
| 3508 | EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm); |
| 3509 | |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 3510 | EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value); |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3511 | |
Logan Chien | 70f94b4 | 2011-12-27 17:49:11 +0800 | [diff] [blame] | 3512 | irb_.CreateBr(GetNextBasicBlock(dex_pc)); |
| 3513 | } |
| 3514 | |
| 3515 | |
Logan Chien | 76e1c79 | 2011-12-27 18:15:01 +0800 | [diff] [blame] | 3516 | llvm::Value* |
| 3517 | MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc, |
| 3518 | llvm::Value *lhs, |
| 3519 | llvm::Value *rhs, |
| 3520 | FPArithmKind arithm) { |
| 3521 | switch (arithm) { |
| 3522 | case kFPArithm_Add: |
| 3523 | return irb_.CreateFAdd(lhs, rhs); |
| 3524 | |
| 3525 | case kFPArithm_Sub: |
| 3526 | return irb_.CreateFSub(lhs, rhs); |
| 3527 | |
| 3528 | case kFPArithm_Mul: |
| 3529 | return irb_.CreateFMul(lhs, rhs); |
| 3530 | |
| 3531 | case kFPArithm_Div: |
| 3532 | return irb_.CreateFDiv(lhs, rhs); |
| 3533 | |
| 3534 | case kFPArithm_Rem: |
| 3535 | return irb_.CreateFRem(lhs, rhs); |
| 3536 | |
| 3537 | default: |
| 3538 | LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm; |
| 3539 | return NULL; |
| 3540 | } |
| 3541 | } |
| 3542 | |
| 3543 | |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3544 | void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc, |
| 3545 | llvm::Value* denominator, |
| 3546 | JType op_jty) { |
| 3547 | DCHECK(op_jty == kInt || op_jty == kLong) << op_jty; |
| 3548 | |
| 3549 | llvm::Constant* zero = irb_.getJZero(op_jty); |
| 3550 | |
| 3551 | llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero); |
| 3552 | |
| 3553 | llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0"); |
| 3554 | |
| 3555 | llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3556 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 3557 | irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3558 | |
| 3559 | irb_.SetInsertPoint(block_exception); |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3560 | EmitUpdateDexPC(dex_pc); |
Logan Chien | c3f7d96 | 2011-12-27 18:13:18 +0800 | [diff] [blame] | 3561 | irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException)); |
| 3562 | EmitBranchExceptionLandingPad(dex_pc); |
| 3563 | |
| 3564 | irb_.SetInsertPoint(block_continue); |
| 3565 | } |
| 3566 | |
| 3567 | |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 3568 | void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc, |
| 3569 | llvm::Value* object) { |
| 3570 | llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull()); |
| 3571 | |
| 3572 | llvm::BasicBlock* block_exception = |
| 3573 | CreateBasicBlockWithDexPC(dex_pc, "nullp"); |
| 3574 | |
| 3575 | llvm::BasicBlock* block_continue = |
| 3576 | CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3577 | |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 3578 | irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 3579 | |
| 3580 | irb_.SetInsertPoint(block_exception); |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3581 | EmitUpdateDexPC(dex_pc); |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 3582 | irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc)); |
Logan Chien | 61bb614 | 2012-02-03 15:34:53 +0800 | [diff] [blame] | 3583 | EmitBranchExceptionLandingPad(dex_pc); |
| 3584 | |
| 3585 | irb_.SetInsertPoint(block_continue); |
| 3586 | } |
| 3587 | |
| 3588 | |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3589 | llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) { |
| 3590 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 3591 | |
TDYa127 | ee1f59b | 2012-04-25 00:56:40 -0700 | [diff] [blame] | 3592 | return irb_.LoadFromObjectOffset(method_object_addr, |
| 3593 | offset.Int32Value(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 3594 | irb_.getJObjectTy(), |
TDYa127 | d3e24c2 | 2012-05-05 20:54:19 -0700 | [diff] [blame] | 3595 | kTBAAConstJObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3599 | llvm::Value* MethodCompiler:: |
| 3600 | EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) { |
| 3601 | llvm::Value* static_storage_dex_cache_addr = |
| 3602 | EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset()); |
| 3603 | |
| 3604 | llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx); |
| 3605 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 3606 | return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | |
| 3610 | llvm::Value* MethodCompiler:: |
| 3611 | EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) { |
| 3612 | llvm::Value* resolved_type_dex_cache_addr = |
| 3613 | EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset()); |
| 3614 | |
| 3615 | llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx); |
| 3616 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 3617 | return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | |
| 3621 | llvm::Value* MethodCompiler:: |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 3622 | EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) { |
| 3623 | llvm::Value* resolved_method_dex_cache_addr = |
| 3624 | EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset()); |
| 3625 | |
| 3626 | llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx); |
| 3627 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 3628 | return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject); |
Logan Chien | 61c65dc | 2012-02-29 03:22:30 +0800 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | llvm::Value* MethodCompiler:: |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3633 | EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) { |
| 3634 | llvm::Value* string_dex_cache_addr = |
| 3635 | EmitLoadDexCacheAddr(Method::DexCacheStringsOffset()); |
| 3636 | |
| 3637 | llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx); |
| 3638 | |
TDYa127 | 8db6ea3 | 2012-05-17 04:48:42 -0700 | [diff] [blame] | 3639 | return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject); |
Logan Chien | bb4d12a | 2012-02-17 14:10:01 +0800 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 3643 | CompiledMethod *MethodCompiler::Compile() { |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3644 | // TODO: Use high-level IR to do this |
| 3645 | // Compute method info |
| 3646 | ComputeMethodInfo(); |
| 3647 | |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3648 | // Code generation |
| 3649 | CreateFunction(); |
| 3650 | |
| 3651 | EmitPrologue(); |
| 3652 | EmitInstructions(); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3653 | EmitPrologueLastBranch(); |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3654 | |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3655 | // Verify the generated bitcode |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3656 | VERIFY_LLVM_FUNCTION(*func_); |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3657 | |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 3658 | // Add the memory usage approximation of the compilation unit |
| 3659 | cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900); |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3660 | // NOTE: From statistics, the bitcode size is 4.5 times bigger than the |
Logan Chien | 8b977d3 | 2012-02-21 19:14:55 +0800 | [diff] [blame] | 3661 | // Dex file. Besides, we have to convert the code unit into bytes. |
| 3662 | // Thus, we got our magic number 9. |
| 3663 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 3664 | CompiledMethod* compiled_method = |
| 3665 | new CompiledMethod(cunit_->GetInstructionSet(), |
| 3666 | cunit_->GetElfIndex(), |
| 3667 | elf_func_idx_); |
| 3668 | |
| 3669 | cunit_->RegisterCompiledMethod(func_, compiled_method); |
| 3670 | |
| 3671 | return compiled_method; |
Logan Chien | 0b82710 | 2011-12-20 19:46:14 +0800 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | |
| 3675 | llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() { |
| 3676 | return func_->arg_begin(); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 3677 | } |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 3678 | |
| 3679 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3680 | void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) { |
| 3681 | if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) { |
| 3682 | irb_.CreateBr(lpad); |
| 3683 | } else { |
| 3684 | irb_.CreateBr(GetUnwindBasicBlock()); |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 3689 | void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc, bool can_skip_unwind) { |
| 3690 | llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc); |
| 3691 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 3692 | if (lpad == NULL && can_skip_unwind && |
| 3693 | IsInstructionDirectToReturn(dex_pc + insn->SizeInCodeUnits())) { |
| 3694 | return; |
| 3695 | } |
| 3696 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3697 | llvm::Value* exception_pending = |
| 3698 | irb_.CreateCall(irb_.GetRuntime(IsExceptionPending)); |
| 3699 | |
| 3700 | llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 3701 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 3702 | if (lpad) { |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 3703 | irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3704 | } else { |
TDYa127 | ac7b5bb | 2012-05-11 13:17:49 -0700 | [diff] [blame] | 3705 | irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | irb_.SetInsertPoint(block_cont); |
| 3709 | } |
| 3710 | |
| 3711 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 3712 | void MethodCompiler::EmitGuard_GarbageCollectionSuspend() { |
| 3713 | // Loop suspend will be added by our llvm pass. |
| 3714 | if (!method_info_.has_invoke) { |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3715 | return; |
| 3716 | } |
| 3717 | |
Logan Chien | 924072f | 2012-01-30 15:07:24 +0800 | [diff] [blame] | 3718 | llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend); |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3719 | |
| 3720 | llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread)); |
| 3721 | |
TDYa127 | 853cd09 | 2012-04-21 22:15:31 -0700 | [diff] [blame] | 3722 | irb_.CreateCall(runtime_func, thread_object_addr); |
Logan Chien | 924072f | 2012-01-30 15:07:24 +0800 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3726 | llvm::BasicBlock* MethodCompiler:: |
| 3727 | CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) { |
| 3728 | std::string name; |
| 3729 | |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3730 | #if !defined(NDEBUG) |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3731 | if (postfix) { |
TDYa127 | 9948913 | 2012-04-29 01:27:58 -0700 | [diff] [blame] | 3732 | StringAppendF(&name, "B%04x.%s", dex_pc, postfix); |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3733 | } else { |
TDYa127 | 9948913 | 2012-04-29 01:27:58 -0700 | [diff] [blame] | 3734 | StringAppendF(&name, "B%04x", dex_pc); |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3735 | } |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3736 | #endif |
Logan Chien | d6c239a | 2011-12-23 15:11:45 +0800 | [diff] [blame] | 3737 | |
| 3738 | return llvm::BasicBlock::Create(*context_, name, func_); |
| 3739 | } |
| 3740 | |
| 3741 | |
| 3742 | llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) { |
| 3743 | DCHECK(dex_pc < code_item_->insns_size_in_code_units_); |
| 3744 | |
| 3745 | llvm::BasicBlock* basic_block = basic_blocks_[dex_pc]; |
| 3746 | |
| 3747 | if (!basic_block) { |
| 3748 | basic_block = CreateBasicBlockWithDexPC(dex_pc); |
| 3749 | basic_blocks_[dex_pc] = basic_block; |
| 3750 | } |
| 3751 | |
| 3752 | return basic_block; |
| 3753 | } |
| 3754 | |
| 3755 | |
| 3756 | llvm::BasicBlock* |
| 3757 | MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) { |
| 3758 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 3759 | return GetBasicBlock(dex_pc + insn->SizeInCodeUnits()); |
| 3760 | } |
| 3761 | |
| 3762 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3763 | int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) { |
| 3764 | // TODO: Since we are emitting the dex instructions in ascending order |
| 3765 | // w.r.t. address, we can cache the lastest try item offset so that we |
| 3766 | // don't have to do binary search for every query. |
| 3767 | |
| 3768 | int32_t min = 0; |
| 3769 | int32_t max = code_item_->tries_size_ - 1; |
| 3770 | |
| 3771 | while (min <= max) { |
| 3772 | int32_t mid = min + (max - min) / 2; |
| 3773 | |
| 3774 | DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid); |
| 3775 | uint32_t start = ti->start_addr_; |
| 3776 | uint32_t end = start + ti->insn_count_; |
| 3777 | |
| 3778 | if (dex_pc < start) { |
| 3779 | max = mid - 1; |
| 3780 | } else if (dex_pc >= end) { |
| 3781 | min = mid + 1; |
| 3782 | } else { |
| 3783 | return mid; // found |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | return -1; // not found |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) { |
| 3792 | // Find the try item for this address in this method |
| 3793 | int32_t ti_offset = GetTryItemOffset(dex_pc); |
| 3794 | |
| 3795 | if (ti_offset == -1) { |
| 3796 | return NULL; // No landing pad is available for this address. |
| 3797 | } |
| 3798 | |
| 3799 | // Check for the existing landing pad basic block |
| 3800 | DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset)); |
| 3801 | llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset]; |
| 3802 | |
| 3803 | if (block_lpad) { |
| 3804 | // We have generated landing pad for this try item already. Return the |
| 3805 | // same basic block. |
| 3806 | return block_lpad; |
| 3807 | } |
| 3808 | |
| 3809 | // Get try item from code item |
| 3810 | DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset); |
| 3811 | |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3812 | std::string lpadname; |
| 3813 | |
| 3814 | #if !defined(NDEBUG) |
| 3815 | StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_); |
| 3816 | #endif |
| 3817 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3818 | // Create landing pad basic block |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3819 | block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3820 | |
| 3821 | // Change IRBuilder insert point |
| 3822 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3823 | irb_.SetInsertPoint(block_lpad); |
| 3824 | |
| 3825 | // Find catch block with matching type |
| 3826 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 3827 | |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 3828 | llvm::Value* ti_offset_value = irb_.getInt32(ti_offset); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3829 | |
| 3830 | llvm::Value* catch_handler_index_value = |
| 3831 | irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock), |
Logan Chien | 736df02 | 2012-04-27 16:25:57 +0800 | [diff] [blame] | 3832 | method_object_addr, ti_offset_value); |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3833 | |
| 3834 | // Switch instruction (Go to unwind basic block by default) |
| 3835 | llvm::SwitchInst* sw = |
| 3836 | irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock()); |
| 3837 | |
| 3838 | // Cases with matched catch block |
| 3839 | CatchHandlerIterator iter(*code_item_, ti->start_addr_); |
| 3840 | |
| 3841 | for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) { |
| 3842 | sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress())); |
| 3843 | } |
| 3844 | |
| 3845 | // Restore the orignal insert point for IRBuilder |
| 3846 | irb_.restoreIP(irb_ip_original); |
| 3847 | |
| 3848 | // Cache this landing pad |
| 3849 | DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset)); |
| 3850 | basic_block_landing_pads_[ti_offset] = block_lpad; |
| 3851 | |
| 3852 | return block_lpad; |
| 3853 | } |
| 3854 | |
| 3855 | |
| 3856 | llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() { |
| 3857 | // Check the existing unwinding baisc block block |
| 3858 | if (basic_block_unwind_ != NULL) { |
| 3859 | return basic_block_unwind_; |
| 3860 | } |
| 3861 | |
| 3862 | // Create new basic block for unwinding |
| 3863 | basic_block_unwind_ = |
| 3864 | llvm::BasicBlock::Create(*context_, "exception_unwind", func_); |
| 3865 | |
| 3866 | // Change IRBuilder insert point |
| 3867 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3868 | irb_.SetInsertPoint(basic_block_unwind_); |
| 3869 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3870 | // Pop the shadow frame |
| 3871 | EmitPopShadowFrame(); |
| 3872 | |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3873 | // Emit the code to return default value (zero) for the given return type. |
Logan Chien | dd361c9 | 2012-04-10 23:40:37 +0800 | [diff] [blame] | 3874 | char ret_shorty = oat_compilation_unit_->GetShorty()[0]; |
Logan Chien | 5bcc04e | 2012-01-30 14:15:12 +0800 | [diff] [blame] | 3875 | if (ret_shorty == 'V') { |
| 3876 | irb_.CreateRetVoid(); |
| 3877 | } else { |
| 3878 | irb_.CreateRet(irb_.getJZero(ret_shorty)); |
| 3879 | } |
| 3880 | |
| 3881 | // Restore the orignal insert point for IRBuilder |
| 3882 | irb_.restoreIP(irb_ip_original); |
| 3883 | |
| 3884 | return basic_block_unwind_; |
| 3885 | } |
| 3886 | |
| 3887 | |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 3888 | llvm::Value* MethodCompiler::AllocDalvikReg(RegCategory cat, const std::string& name) { |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3889 | // Get reg_type and reg_name from DalvikReg |
| 3890 | llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat); |
| 3891 | std::string reg_name; |
| 3892 | |
| 3893 | #if !defined(NDEBUG) |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 3894 | StringAppendF(®_name, "%c%s", DalvikReg::GetRegCategoryNamePrefix(cat), name.c_str()); |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3895 | #endif |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3896 | |
| 3897 | // Save current IR builder insert point |
| 3898 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 3899 | irb_.SetInsertPoint(basic_block_alloca_); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3900 | |
| 3901 | // Alloca |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3902 | llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name); |
Logan Chien | c670a8d | 2011-12-20 21:25:56 +0800 | [diff] [blame] | 3903 | |
| 3904 | // Restore IRBuilder insert point |
| 3905 | irb_.restoreIP(irb_ip_original); |
| 3906 | |
| 3907 | DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL)); |
| 3908 | return reg_addr; |
| 3909 | } |
| 3910 | |
| 3911 | |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 3912 | llvm::Value* MethodCompiler::GetShadowFrameEntry(uint32_t reg_idx) { |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 3913 | if (reg_to_shadow_frame_index_[reg_idx] == -1) { |
| 3914 | // This register dosen't need ShadowFrame entry |
| 3915 | return NULL; |
| 3916 | } |
| 3917 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3918 | if (!method_info_.need_shadow_frame_entry) { |
| 3919 | return NULL; |
| 3920 | } |
| 3921 | |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3922 | std::string reg_name; |
| 3923 | |
| 3924 | #if !defined(NDEBUG) |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 3925 | StringAppendF(®_name, "s%u", reg_idx); |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3926 | #endif |
| 3927 | |
TDYa127 | 7f5b9be | 2012-04-29 01:31:49 -0700 | [diff] [blame] | 3928 | // Save current IR builder insert point |
| 3929 | llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP(); |
| 3930 | |
TDYa127 | b9ff6b1 | 2012-05-17 11:14:29 -0700 | [diff] [blame] | 3931 | irb_.SetInsertPoint(basic_block_shadow_frame_); |
TDYa127 | 7f5b9be | 2012-04-29 01:31:49 -0700 | [diff] [blame] | 3932 | |
| 3933 | llvm::Value* gep_index[] = { |
| 3934 | irb_.getInt32(0), // No pointer displacement |
| 3935 | irb_.getInt32(1), // SIRT |
TDYa127 | 1d7e510 | 2012-05-13 09:27:05 -0700 | [diff] [blame] | 3936 | irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field |
TDYa127 | 7f5b9be | 2012-04-29 01:31:49 -0700 | [diff] [blame] | 3937 | }; |
| 3938 | |
TDYa127 | 67ae8ff | 2012-05-02 19:08:02 -0700 | [diff] [blame] | 3939 | llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name); |
TDYa127 | 7f5b9be | 2012-04-29 01:31:49 -0700 | [diff] [blame] | 3940 | |
| 3941 | // Restore IRBuilder insert point |
| 3942 | irb_.restoreIP(irb_ip_original); |
| 3943 | |
| 3944 | DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL)); |
| 3945 | return reg_addr; |
| 3946 | } |
| 3947 | |
| 3948 | |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 3949 | void MethodCompiler::EmitPushShadowFrame(bool is_inline) { |
| 3950 | if (!method_info_.need_shadow_frame) { |
| 3951 | return; |
| 3952 | } |
| 3953 | DCHECK(shadow_frame_ != NULL); |
| 3954 | DCHECK(old_shadow_frame_ != NULL); |
| 3955 | |
| 3956 | // Get method object |
| 3957 | llvm::Value* method_object_addr = EmitLoadMethodObjectAddr(); |
| 3958 | |
| 3959 | // Push the shadow frame |
| 3960 | llvm::Value* shadow_frame_upcast = |
| 3961 | irb_.CreateConstGEP2_32(shadow_frame_, 0, 0); |
| 3962 | |
| 3963 | llvm::Value* result = |
| 3964 | irb_.CreateCall3(irb_.GetRuntime(is_inline ? PushShadowFrame : PushShadowFrameNoInline), |
| 3965 | shadow_frame_upcast, method_object_addr, irb_.getJInt(shadow_frame_size_)); |
| 3966 | irb_.CreateStore(result, old_shadow_frame_, kTBAARegister); |
| 3967 | } |
| 3968 | |
| 3969 | |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3970 | void MethodCompiler::EmitPopShadowFrame() { |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3971 | if (!method_info_.need_shadow_frame) { |
| 3972 | return; |
| 3973 | } |
TDYa127 | 0de52be | 2012-05-27 20:49:31 -0700 | [diff] [blame] | 3974 | DCHECK(old_shadow_frame_ != NULL); |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 3975 | |
| 3976 | if (method_info_.lazy_push_shadow_frame) { |
| 3977 | llvm::BasicBlock* bb_pop = llvm::BasicBlock::Create(*context_, "pop", func_); |
| 3978 | llvm::BasicBlock* bb_cont = llvm::BasicBlock::Create(*context_, "cont", func_); |
| 3979 | |
| 3980 | llvm::Value* need_pop = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister); |
| 3981 | irb_.CreateCondBr(need_pop, bb_pop, bb_cont, kUnlikely); |
| 3982 | |
| 3983 | irb_.SetInsertPoint(bb_pop); |
| 3984 | irb_.CreateCall(irb_.GetRuntime(PopShadowFrame), |
| 3985 | irb_.CreateLoad(old_shadow_frame_, kTBAARegister)); |
| 3986 | irb_.CreateBr(bb_cont); |
| 3987 | |
| 3988 | irb_.SetInsertPoint(bb_cont); |
| 3989 | } else { |
| 3990 | irb_.CreateCall(irb_.GetRuntime(PopShadowFrame), |
| 3991 | irb_.CreateLoad(old_shadow_frame_, kTBAARegister)); |
| 3992 | } |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 3996 | void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) { |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 3997 | if (!method_info_.need_shadow_frame) { |
| 3998 | return; |
| 3999 | } |
TDYa127 | c8dc101 | 2012-04-19 07:03:33 -0700 | [diff] [blame] | 4000 | irb_.StoreToObjectOffset(shadow_frame_, |
| 4001 | ShadowFrame::DexPCOffset(), |
TDYa127 | aba6112 | 2012-05-04 18:28:36 -0700 | [diff] [blame] | 4002 | irb_.getInt32(dex_pc), |
TDYa127 | d955bec | 2012-05-11 10:54:02 -0700 | [diff] [blame] | 4003 | kTBAAShadowFrame); |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 4004 | // Lazy pushing shadow frame |
| 4005 | if (method_info_.lazy_push_shadow_frame) { |
| 4006 | llvm::BasicBlock* bb_push = CreateBasicBlockWithDexPC(dex_pc, "push"); |
| 4007 | llvm::BasicBlock* bb_cont = CreateBasicBlockWithDexPC(dex_pc, "cont"); |
| 4008 | |
| 4009 | llvm::Value* no_need_push = irb_.CreateLoad(already_pushed_shadow_frame_, kTBAARegister); |
| 4010 | irb_.CreateCondBr(no_need_push, bb_cont, bb_push, kLikely); |
| 4011 | |
| 4012 | irb_.SetInsertPoint(bb_push); |
| 4013 | EmitPushShadowFrame(false); |
| 4014 | irb_.CreateStore(irb_.getTrue(), already_pushed_shadow_frame_, kTBAARegister); |
| 4015 | irb_.CreateBr(bb_cont); |
| 4016 | |
| 4017 | irb_.SetInsertPoint(bb_cont); |
| 4018 | } |
Logan Chien | 8dfcbea | 2012-02-17 18:50:32 +0800 | [diff] [blame] | 4019 | } |
| 4020 | |
| 4021 | |
TDYa127 | e210214 | 2012-05-26 10:27:38 -0700 | [diff] [blame] | 4022 | llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, JType jty, |
| 4023 | JTypeSpace space) { |
| 4024 | return regs_[reg_idx]->GetValue(jty, space); |
| 4025 | } |
| 4026 | |
| 4027 | llvm::Value* MethodCompiler::EmitLoadDalvikReg(uint32_t reg_idx, char shorty, |
| 4028 | JTypeSpace space) { |
| 4029 | return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space); |
| 4030 | } |
| 4031 | |
| 4032 | void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, JType jty, |
| 4033 | JTypeSpace space, llvm::Value* new_value) { |
| 4034 | regs_[reg_idx]->SetValue(jty, space, new_value); |
| 4035 | if (jty == kObject && shadow_frame_entries_[reg_idx] != NULL) { |
| 4036 | irb_.CreateStore(new_value, shadow_frame_entries_[reg_idx], kTBAAShadowFrame); |
| 4037 | } |
| 4038 | } |
| 4039 | |
| 4040 | void MethodCompiler::EmitStoreDalvikReg(uint32_t reg_idx, char shorty, |
| 4041 | JTypeSpace space, llvm::Value* new_value) { |
| 4042 | EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value); |
| 4043 | } |
| 4044 | |
| 4045 | llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) { |
| 4046 | return retval_reg_->GetValue(jty, space); |
| 4047 | } |
| 4048 | |
| 4049 | llvm::Value* MethodCompiler::EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) { |
| 4050 | return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space); |
| 4051 | } |
| 4052 | |
| 4053 | void MethodCompiler::EmitStoreDalvikRetValReg(JType jty, JTypeSpace space, |
| 4054 | llvm::Value* new_value) { |
| 4055 | retval_reg_->SetValue(jty, space, new_value); |
| 4056 | } |
| 4057 | |
| 4058 | void MethodCompiler::EmitStoreDalvikRetValReg(char shorty, JTypeSpace space, |
| 4059 | llvm::Value* new_value) { |
| 4060 | EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value); |
| 4061 | } |
| 4062 | |
| 4063 | |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4064 | // TODO: Use high-level IR to do this |
TDYa127 | 29c0cd1 | 2012-05-17 04:51:08 -0700 | [diff] [blame] | 4065 | bool MethodCompiler::EmitInlineJavaIntrinsic(const std::string& callee_method_name, |
| 4066 | const std::vector<llvm::Value*>& args, |
| 4067 | llvm::BasicBlock* after_invoke) { |
| 4068 | if (callee_method_name == "char java.lang.String.charAt(int)") { |
| 4069 | return EmitInlinedStringCharAt(args, after_invoke); |
| 4070 | } |
| 4071 | if (callee_method_name == "int java.lang.String.length()") { |
| 4072 | return EmitInlinedStringLength(args, after_invoke); |
| 4073 | } |
| 4074 | return true; |
| 4075 | } |
| 4076 | |
| 4077 | bool MethodCompiler::EmitInlinedStringCharAt(const std::vector<llvm::Value*>& args, |
| 4078 | llvm::BasicBlock* after_invoke) { |
| 4079 | DCHECK_EQ(args.size(), 3U) << |
| 4080 | "char java.lang.String.charAt(int) has 3 args: method, this, char_index"; |
| 4081 | llvm::Value* this_object = args[1]; |
| 4082 | llvm::Value* char_index = args[2]; |
| 4083 | llvm::BasicBlock* block_retry = llvm::BasicBlock::Create(*context_, "CharAtRetry", func_); |
| 4084 | llvm::BasicBlock* block_cont = llvm::BasicBlock::Create(*context_, "CharAtCont", func_); |
| 4085 | |
| 4086 | // TODO: Can we safely say the String.count is ConstJObject(constant memory)? (there are so many |
| 4087 | // iput to String.count in the String.<init>(...)) |
| 4088 | llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object, |
| 4089 | String::CountOffset().Int32Value(), |
| 4090 | irb_.getJIntTy(), |
| 4091 | kTBAAHeapInstance, kInt); |
| 4092 | // Two's complement, so we can use only one "less than" to check "in bounds" |
| 4093 | llvm::Value* in_bounds = irb_.CreateICmpULT(char_index, string_count); |
| 4094 | irb_.CreateCondBr(in_bounds, block_cont, block_retry, kLikely); |
| 4095 | |
| 4096 | irb_.SetInsertPoint(block_cont); |
| 4097 | // TODO: Can we safely say the String.offset is ConstJObject(constant memory)? |
| 4098 | llvm::Value* string_offset = irb_.LoadFromObjectOffset(this_object, |
| 4099 | String::OffsetOffset().Int32Value(), |
| 4100 | irb_.getJIntTy(), |
| 4101 | kTBAAHeapInstance, kInt); |
| 4102 | llvm::Value* string_value = irb_.LoadFromObjectOffset(this_object, |
| 4103 | String::ValueOffset().Int32Value(), |
| 4104 | irb_.getJObjectTy(), |
| 4105 | kTBAAHeapInstance, kObject); |
| 4106 | |
| 4107 | // index_value = string.offset + char_index |
| 4108 | llvm::Value* index_value = irb_.CreateAdd(string_offset, char_index); |
| 4109 | |
| 4110 | // array_elem_value = string.value[index_value] |
| 4111 | llvm::Value* array_elem_addr = EmitArrayGEP(string_value, index_value, kChar); |
| 4112 | llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, kChar); |
| 4113 | |
| 4114 | EmitStoreDalvikRetValReg(kChar, kArray, array_elem_value); |
| 4115 | irb_.CreateBr(after_invoke); |
| 4116 | |
| 4117 | irb_.SetInsertPoint(block_retry); |
| 4118 | return true; |
| 4119 | } |
| 4120 | |
| 4121 | bool MethodCompiler::EmitInlinedStringLength(const std::vector<llvm::Value*>& args, |
| 4122 | llvm::BasicBlock* after_invoke) { |
| 4123 | DCHECK_EQ(args.size(), 2U) << |
| 4124 | "int java.lang.String.length() has 2 args: method, this"; |
| 4125 | llvm::Value* this_object = args[1]; |
| 4126 | // TODO: Can we safely say the String.count is ConstJObject(constant memory)? |
| 4127 | llvm::Value* string_count = irb_.LoadFromObjectOffset(this_object, |
| 4128 | String::CountOffset().Int32Value(), |
| 4129 | irb_.getJIntTy(), |
| 4130 | kTBAAHeapInstance, kInt); |
| 4131 | EmitStoreDalvikRetValReg(kInt, kAccurate, string_count); |
| 4132 | irb_.CreateBr(after_invoke); |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
| 4136 | |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 4137 | bool MethodCompiler::IsInstructionDirectToReturn(uint32_t dex_pc) { |
| 4138 | for (int i = 0; i < 8; ++i) { // Trace at most 8 instructions. |
| 4139 | if (dex_pc >= code_item_->insns_size_in_code_units_) { |
| 4140 | return false; |
| 4141 | } |
| 4142 | |
| 4143 | Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 4144 | |
| 4145 | if (insn->IsReturn()) { |
| 4146 | return true; |
| 4147 | } |
| 4148 | |
| 4149 | // Is throw, switch, invoke or conditional branch. |
| 4150 | if (insn->IsThrow() || insn->IsSwitch() || insn->IsInvoke() || |
| 4151 | (insn->IsBranch() && !insn->IsUnconditional())) { |
| 4152 | return false; |
| 4153 | } |
| 4154 | |
| 4155 | switch (insn->Opcode()) { |
| 4156 | default: |
| 4157 | dex_pc += insn->SizeInCodeUnits(); |
| 4158 | break; |
| 4159 | |
| 4160 | // This instruction will remove the exception. Consider as a side effect. |
| 4161 | case Instruction::MOVE_EXCEPTION: |
| 4162 | return false; |
| 4163 | break; |
| 4164 | |
| 4165 | case Instruction::GOTO: |
| 4166 | case Instruction::GOTO_16: |
| 4167 | case Instruction::GOTO_32: |
| 4168 | { |
| 4169 | DecodedInstruction dec_insn(insn); |
| 4170 | int32_t branch_offset = dec_insn.vA; |
| 4171 | dex_pc += branch_offset; |
| 4172 | } |
| 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | return false; |
| 4177 | } |
| 4178 | |
| 4179 | |
TDYa127 | 29c0cd1 | 2012-05-17 04:51:08 -0700 | [diff] [blame] | 4180 | // TODO: Use high-level IR to do this |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4181 | void MethodCompiler::ComputeMethodInfo() { |
| 4182 | // If this method is static, we set the "this" register index to -1. So we don't worry about this |
| 4183 | // method is static or not in the following comparison. |
| 4184 | int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ? |
| 4185 | (-1) : |
| 4186 | (code_item_->registers_size_ - code_item_->ins_size_); |
| 4187 | bool has_invoke = false; |
| 4188 | bool may_have_loop = false; |
| 4189 | bool modify_this = false; |
| 4190 | bool may_throw_exception = false; |
| 4191 | |
| 4192 | Instruction const* insn; |
| 4193 | for (uint32_t dex_pc = 0; |
| 4194 | dex_pc < code_item_->insns_size_in_code_units_; |
| 4195 | dex_pc += insn->SizeInCodeUnits()) { |
| 4196 | insn = Instruction::At(code_item_->insns_ + dex_pc); |
| 4197 | DecodedInstruction dec_insn(insn); |
| 4198 | |
| 4199 | switch (insn->Opcode()) { |
| 4200 | case Instruction::NOP: |
| 4201 | break; |
| 4202 | |
| 4203 | case Instruction::MOVE: |
| 4204 | case Instruction::MOVE_FROM16: |
| 4205 | case Instruction::MOVE_16: |
| 4206 | case Instruction::MOVE_WIDE: |
| 4207 | case Instruction::MOVE_WIDE_FROM16: |
| 4208 | case Instruction::MOVE_WIDE_16: |
| 4209 | case Instruction::MOVE_OBJECT: |
| 4210 | case Instruction::MOVE_OBJECT_FROM16: |
| 4211 | case Instruction::MOVE_OBJECT_16: |
| 4212 | case Instruction::MOVE_RESULT: |
| 4213 | case Instruction::MOVE_RESULT_WIDE: |
| 4214 | case Instruction::MOVE_RESULT_OBJECT: |
| 4215 | case Instruction::MOVE_EXCEPTION: |
| 4216 | if (dec_insn.vA == this_reg_idx) { |
| 4217 | modify_this = true; |
| 4218 | } |
| 4219 | break; |
| 4220 | |
| 4221 | case Instruction::RETURN_VOID: |
| 4222 | case Instruction::RETURN: |
| 4223 | case Instruction::RETURN_WIDE: |
| 4224 | case Instruction::RETURN_OBJECT: |
| 4225 | break; |
| 4226 | |
| 4227 | case Instruction::CONST_4: |
| 4228 | case Instruction::CONST_16: |
| 4229 | case Instruction::CONST: |
| 4230 | case Instruction::CONST_HIGH16: |
| 4231 | case Instruction::CONST_WIDE_16: |
| 4232 | case Instruction::CONST_WIDE_32: |
| 4233 | case Instruction::CONST_WIDE: |
| 4234 | case Instruction::CONST_WIDE_HIGH16: |
| 4235 | if (dec_insn.vA == this_reg_idx) { |
| 4236 | modify_this = true; |
| 4237 | } |
| 4238 | break; |
| 4239 | |
| 4240 | case Instruction::CONST_STRING: |
| 4241 | case Instruction::CONST_STRING_JUMBO: |
| 4242 | // TODO: Will the ResolveString throw exception? |
| 4243 | if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) { |
| 4244 | may_throw_exception = true; |
| 4245 | } |
| 4246 | if (dec_insn.vA == this_reg_idx) { |
| 4247 | modify_this = true; |
| 4248 | } |
| 4249 | break; |
| 4250 | |
| 4251 | case Instruction::CONST_CLASS: |
| 4252 | may_throw_exception = true; |
| 4253 | if (dec_insn.vA == this_reg_idx) { |
| 4254 | modify_this = true; |
| 4255 | } |
| 4256 | break; |
| 4257 | |
| 4258 | case Instruction::MONITOR_ENTER: |
| 4259 | case Instruction::MONITOR_EXIT: |
| 4260 | case Instruction::CHECK_CAST: |
| 4261 | may_throw_exception = true; |
| 4262 | break; |
| 4263 | |
| 4264 | case Instruction::INSTANCE_OF: |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4265 | case Instruction::ARRAY_LENGTH: |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4266 | case Instruction::NEW_INSTANCE: |
| 4267 | case Instruction::NEW_ARRAY: |
| 4268 | may_throw_exception = true; |
| 4269 | if (dec_insn.vA == this_reg_idx) { |
| 4270 | modify_this = true; |
| 4271 | } |
| 4272 | break; |
| 4273 | |
| 4274 | case Instruction::FILLED_NEW_ARRAY: |
| 4275 | case Instruction::FILLED_NEW_ARRAY_RANGE: |
| 4276 | case Instruction::FILL_ARRAY_DATA: |
| 4277 | case Instruction::THROW: |
| 4278 | may_throw_exception = true; |
| 4279 | break; |
| 4280 | |
| 4281 | case Instruction::GOTO: |
| 4282 | case Instruction::GOTO_16: |
| 4283 | case Instruction::GOTO_32: |
| 4284 | { |
| 4285 | int32_t branch_offset = dec_insn.vA; |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 4286 | if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) { |
| 4287 | may_have_loop = true; |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | break; |
| 4291 | |
| 4292 | case Instruction::PACKED_SWITCH: |
| 4293 | case Instruction::SPARSE_SWITCH: |
| 4294 | break; |
| 4295 | |
| 4296 | case Instruction::CMPL_FLOAT: |
| 4297 | case Instruction::CMPG_FLOAT: |
| 4298 | case Instruction::CMPL_DOUBLE: |
| 4299 | case Instruction::CMPG_DOUBLE: |
| 4300 | case Instruction::CMP_LONG: |
| 4301 | if (dec_insn.vA == this_reg_idx) { |
| 4302 | modify_this = true; |
| 4303 | } |
| 4304 | break; |
| 4305 | |
| 4306 | case Instruction::IF_EQ: |
| 4307 | case Instruction::IF_NE: |
| 4308 | case Instruction::IF_LT: |
| 4309 | case Instruction::IF_GE: |
| 4310 | case Instruction::IF_GT: |
| 4311 | case Instruction::IF_LE: |
| 4312 | { |
| 4313 | int32_t branch_offset = dec_insn.vC; |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 4314 | if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) { |
| 4315 | may_have_loop = true; |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | break; |
| 4319 | |
| 4320 | case Instruction::IF_EQZ: |
| 4321 | case Instruction::IF_NEZ: |
| 4322 | case Instruction::IF_LTZ: |
| 4323 | case Instruction::IF_GEZ: |
| 4324 | case Instruction::IF_GTZ: |
| 4325 | case Instruction::IF_LEZ: |
| 4326 | { |
| 4327 | int32_t branch_offset = dec_insn.vB; |
TDYa127 | 526643e | 2012-05-26 01:01:48 -0700 | [diff] [blame] | 4328 | if (branch_offset <= 0 && !IsInstructionDirectToReturn(dex_pc + branch_offset)) { |
| 4329 | may_have_loop = true; |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4330 | } |
| 4331 | } |
| 4332 | break; |
| 4333 | |
| 4334 | case Instruction::AGET: |
| 4335 | case Instruction::AGET_WIDE: |
| 4336 | case Instruction::AGET_OBJECT: |
| 4337 | case Instruction::AGET_BOOLEAN: |
| 4338 | case Instruction::AGET_BYTE: |
| 4339 | case Instruction::AGET_CHAR: |
| 4340 | case Instruction::AGET_SHORT: |
| 4341 | may_throw_exception = true; |
| 4342 | if (dec_insn.vA == this_reg_idx) { |
| 4343 | modify_this = true; |
| 4344 | } |
| 4345 | break; |
| 4346 | |
| 4347 | case Instruction::APUT: |
| 4348 | case Instruction::APUT_WIDE: |
| 4349 | case Instruction::APUT_OBJECT: |
| 4350 | case Instruction::APUT_BOOLEAN: |
| 4351 | case Instruction::APUT_BYTE: |
| 4352 | case Instruction::APUT_CHAR: |
| 4353 | case Instruction::APUT_SHORT: |
| 4354 | may_throw_exception = true; |
| 4355 | break; |
| 4356 | |
| 4357 | case Instruction::IGET: |
| 4358 | case Instruction::IGET_WIDE: |
| 4359 | case Instruction::IGET_OBJECT: |
| 4360 | case Instruction::IGET_BOOLEAN: |
| 4361 | case Instruction::IGET_BYTE: |
| 4362 | case Instruction::IGET_CHAR: |
| 4363 | case Instruction::IGET_SHORT: |
| 4364 | { |
| 4365 | if (dec_insn.vA == this_reg_idx) { |
| 4366 | modify_this = true; |
| 4367 | } |
| 4368 | uint32_t reg_idx = dec_insn.vB; |
| 4369 | uint32_t field_idx = dec_insn.vC; |
| 4370 | int field_offset; |
| 4371 | bool is_volatile; |
| 4372 | bool is_fast_path = compiler_->ComputeInstanceFieldInfo( |
| 4373 | field_idx, oat_compilation_unit_, field_offset, is_volatile, false); |
| 4374 | if (!is_fast_path) { |
| 4375 | may_throw_exception = true; |
| 4376 | } else if (reg_idx != this_reg_idx) { |
| 4377 | // NullPointerException |
| 4378 | may_throw_exception = true; |
| 4379 | } |
| 4380 | } |
| 4381 | break; |
| 4382 | |
| 4383 | case Instruction::IPUT: |
| 4384 | case Instruction::IPUT_WIDE: |
| 4385 | case Instruction::IPUT_OBJECT: |
| 4386 | case Instruction::IPUT_BOOLEAN: |
| 4387 | case Instruction::IPUT_BYTE: |
| 4388 | case Instruction::IPUT_CHAR: |
| 4389 | case Instruction::IPUT_SHORT: |
| 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, true); |
| 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::SGET: |
| 4407 | case Instruction::SGET_WIDE: |
| 4408 | case Instruction::SGET_OBJECT: |
| 4409 | case Instruction::SGET_BOOLEAN: |
| 4410 | case Instruction::SGET_BYTE: |
| 4411 | case Instruction::SGET_CHAR: |
| 4412 | case Instruction::SGET_SHORT: |
| 4413 | { |
| 4414 | if (dec_insn.vA == this_reg_idx) { |
| 4415 | modify_this = true; |
| 4416 | } |
| 4417 | uint32_t field_idx = dec_insn.vB; |
| 4418 | |
| 4419 | int field_offset; |
| 4420 | int ssb_index; |
| 4421 | bool is_referrers_class; |
| 4422 | bool is_volatile; |
| 4423 | |
| 4424 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 4425 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 4426 | is_referrers_class, is_volatile, false); |
| 4427 | if (!is_fast_path || !is_referrers_class) { |
| 4428 | may_throw_exception = true; |
| 4429 | } |
| 4430 | } |
| 4431 | break; |
| 4432 | |
| 4433 | case Instruction::SPUT: |
| 4434 | case Instruction::SPUT_WIDE: |
| 4435 | case Instruction::SPUT_OBJECT: |
| 4436 | case Instruction::SPUT_BOOLEAN: |
| 4437 | case Instruction::SPUT_BYTE: |
| 4438 | case Instruction::SPUT_CHAR: |
| 4439 | case Instruction::SPUT_SHORT: |
| 4440 | { |
| 4441 | uint32_t field_idx = dec_insn.vB; |
| 4442 | |
| 4443 | int field_offset; |
| 4444 | int ssb_index; |
| 4445 | bool is_referrers_class; |
| 4446 | bool is_volatile; |
| 4447 | |
| 4448 | bool is_fast_path = compiler_->ComputeStaticFieldInfo( |
| 4449 | field_idx, oat_compilation_unit_, field_offset, ssb_index, |
| 4450 | is_referrers_class, is_volatile, true); |
| 4451 | if (!is_fast_path || !is_referrers_class) { |
| 4452 | may_throw_exception = true; |
| 4453 | } |
| 4454 | } |
| 4455 | break; |
| 4456 | |
| 4457 | |
| 4458 | case Instruction::INVOKE_VIRTUAL: |
| 4459 | case Instruction::INVOKE_SUPER: |
| 4460 | case Instruction::INVOKE_DIRECT: |
| 4461 | case Instruction::INVOKE_STATIC: |
| 4462 | case Instruction::INVOKE_INTERFACE: |
| 4463 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 4464 | case Instruction::INVOKE_SUPER_RANGE: |
| 4465 | case Instruction::INVOKE_DIRECT_RANGE: |
| 4466 | case Instruction::INVOKE_STATIC_RANGE: |
| 4467 | case Instruction::INVOKE_INTERFACE_RANGE: |
| 4468 | has_invoke = true; |
| 4469 | may_throw_exception = true; |
| 4470 | break; |
| 4471 | |
| 4472 | case Instruction::NEG_INT: |
| 4473 | case Instruction::NOT_INT: |
| 4474 | case Instruction::NEG_LONG: |
| 4475 | case Instruction::NOT_LONG: |
| 4476 | case Instruction::NEG_FLOAT: |
| 4477 | case Instruction::NEG_DOUBLE: |
| 4478 | case Instruction::INT_TO_LONG: |
| 4479 | case Instruction::INT_TO_FLOAT: |
| 4480 | case Instruction::INT_TO_DOUBLE: |
| 4481 | case Instruction::LONG_TO_INT: |
| 4482 | case Instruction::LONG_TO_FLOAT: |
| 4483 | case Instruction::LONG_TO_DOUBLE: |
| 4484 | case Instruction::FLOAT_TO_INT: |
| 4485 | case Instruction::FLOAT_TO_LONG: |
| 4486 | case Instruction::FLOAT_TO_DOUBLE: |
| 4487 | case Instruction::DOUBLE_TO_INT: |
| 4488 | case Instruction::DOUBLE_TO_LONG: |
| 4489 | case Instruction::DOUBLE_TO_FLOAT: |
| 4490 | case Instruction::INT_TO_BYTE: |
| 4491 | case Instruction::INT_TO_CHAR: |
| 4492 | case Instruction::INT_TO_SHORT: |
| 4493 | case Instruction::ADD_INT: |
| 4494 | case Instruction::SUB_INT: |
| 4495 | case Instruction::MUL_INT: |
| 4496 | case Instruction::AND_INT: |
| 4497 | case Instruction::OR_INT: |
| 4498 | case Instruction::XOR_INT: |
| 4499 | case Instruction::SHL_INT: |
| 4500 | case Instruction::SHR_INT: |
| 4501 | case Instruction::USHR_INT: |
| 4502 | case Instruction::ADD_LONG: |
| 4503 | case Instruction::SUB_LONG: |
| 4504 | case Instruction::MUL_LONG: |
| 4505 | case Instruction::AND_LONG: |
| 4506 | case Instruction::OR_LONG: |
| 4507 | case Instruction::XOR_LONG: |
| 4508 | case Instruction::SHL_LONG: |
| 4509 | case Instruction::SHR_LONG: |
| 4510 | case Instruction::USHR_LONG: |
| 4511 | case Instruction::ADD_INT_2ADDR: |
| 4512 | case Instruction::SUB_INT_2ADDR: |
| 4513 | case Instruction::MUL_INT_2ADDR: |
| 4514 | case Instruction::AND_INT_2ADDR: |
| 4515 | case Instruction::OR_INT_2ADDR: |
| 4516 | case Instruction::XOR_INT_2ADDR: |
| 4517 | case Instruction::SHL_INT_2ADDR: |
| 4518 | case Instruction::SHR_INT_2ADDR: |
| 4519 | case Instruction::USHR_INT_2ADDR: |
| 4520 | case Instruction::ADD_LONG_2ADDR: |
| 4521 | case Instruction::SUB_LONG_2ADDR: |
| 4522 | case Instruction::MUL_LONG_2ADDR: |
| 4523 | case Instruction::AND_LONG_2ADDR: |
| 4524 | case Instruction::OR_LONG_2ADDR: |
| 4525 | case Instruction::XOR_LONG_2ADDR: |
| 4526 | case Instruction::SHL_LONG_2ADDR: |
| 4527 | case Instruction::SHR_LONG_2ADDR: |
| 4528 | case Instruction::USHR_LONG_2ADDR: |
| 4529 | if (dec_insn.vA == this_reg_idx) { |
| 4530 | modify_this = true; |
| 4531 | } |
| 4532 | break; |
| 4533 | |
| 4534 | case Instruction::DIV_INT: |
| 4535 | case Instruction::REM_INT: |
| 4536 | case Instruction::DIV_LONG: |
| 4537 | case Instruction::REM_LONG: |
| 4538 | case Instruction::DIV_INT_2ADDR: |
| 4539 | case Instruction::REM_INT_2ADDR: |
| 4540 | case Instruction::DIV_LONG_2ADDR: |
| 4541 | case Instruction::REM_LONG_2ADDR: |
| 4542 | may_throw_exception = true; |
| 4543 | if (dec_insn.vA == this_reg_idx) { |
| 4544 | modify_this = true; |
| 4545 | } |
| 4546 | break; |
| 4547 | |
| 4548 | case Instruction::ADD_FLOAT: |
| 4549 | case Instruction::SUB_FLOAT: |
| 4550 | case Instruction::MUL_FLOAT: |
| 4551 | case Instruction::DIV_FLOAT: |
| 4552 | case Instruction::REM_FLOAT: |
| 4553 | case Instruction::ADD_DOUBLE: |
| 4554 | case Instruction::SUB_DOUBLE: |
| 4555 | case Instruction::MUL_DOUBLE: |
| 4556 | case Instruction::DIV_DOUBLE: |
| 4557 | case Instruction::REM_DOUBLE: |
| 4558 | case Instruction::ADD_FLOAT_2ADDR: |
| 4559 | case Instruction::SUB_FLOAT_2ADDR: |
| 4560 | case Instruction::MUL_FLOAT_2ADDR: |
| 4561 | case Instruction::DIV_FLOAT_2ADDR: |
| 4562 | case Instruction::REM_FLOAT_2ADDR: |
| 4563 | case Instruction::ADD_DOUBLE_2ADDR: |
| 4564 | case Instruction::SUB_DOUBLE_2ADDR: |
| 4565 | case Instruction::MUL_DOUBLE_2ADDR: |
| 4566 | case Instruction::DIV_DOUBLE_2ADDR: |
| 4567 | case Instruction::REM_DOUBLE_2ADDR: |
| 4568 | if (dec_insn.vA == this_reg_idx) { |
| 4569 | modify_this = true; |
| 4570 | } |
| 4571 | break; |
| 4572 | |
| 4573 | case Instruction::ADD_INT_LIT16: |
| 4574 | case Instruction::ADD_INT_LIT8: |
| 4575 | case Instruction::RSUB_INT: |
| 4576 | case Instruction::RSUB_INT_LIT8: |
| 4577 | case Instruction::MUL_INT_LIT16: |
| 4578 | case Instruction::MUL_INT_LIT8: |
| 4579 | case Instruction::AND_INT_LIT16: |
| 4580 | case Instruction::AND_INT_LIT8: |
| 4581 | case Instruction::OR_INT_LIT16: |
| 4582 | case Instruction::OR_INT_LIT8: |
| 4583 | case Instruction::XOR_INT_LIT16: |
| 4584 | case Instruction::XOR_INT_LIT8: |
| 4585 | case Instruction::SHL_INT_LIT8: |
| 4586 | case Instruction::SHR_INT_LIT8: |
| 4587 | case Instruction::USHR_INT_LIT8: |
| 4588 | if (dec_insn.vA == this_reg_idx) { |
| 4589 | modify_this = true; |
| 4590 | } |
| 4591 | break; |
| 4592 | case Instruction::DIV_INT_LIT16: |
| 4593 | case Instruction::DIV_INT_LIT8: |
| 4594 | case Instruction::REM_INT_LIT16: |
| 4595 | case Instruction::REM_INT_LIT8: |
| 4596 | if (dec_insn.vC == 0) { |
| 4597 | may_throw_exception = true; |
| 4598 | } |
| 4599 | if (dec_insn.vA == this_reg_idx) { |
| 4600 | modify_this = true; |
| 4601 | } |
| 4602 | break; |
| 4603 | |
| 4604 | case Instruction::THROW_VERIFICATION_ERROR: |
| 4605 | may_throw_exception = true; |
| 4606 | break; |
| 4607 | |
| 4608 | case Instruction::UNUSED_3E: |
| 4609 | case Instruction::UNUSED_3F: |
| 4610 | case Instruction::UNUSED_40: |
| 4611 | case Instruction::UNUSED_41: |
| 4612 | case Instruction::UNUSED_42: |
| 4613 | case Instruction::UNUSED_43: |
| 4614 | case Instruction::UNUSED_73: |
| 4615 | case Instruction::UNUSED_79: |
| 4616 | case Instruction::UNUSED_7A: |
| 4617 | case Instruction::UNUSED_E3: |
| 4618 | case Instruction::UNUSED_E4: |
| 4619 | case Instruction::UNUSED_E5: |
| 4620 | case Instruction::UNUSED_E6: |
| 4621 | case Instruction::UNUSED_E7: |
| 4622 | case Instruction::UNUSED_E8: |
| 4623 | case Instruction::UNUSED_E9: |
| 4624 | case Instruction::UNUSED_EA: |
| 4625 | case Instruction::UNUSED_EB: |
| 4626 | case Instruction::UNUSED_EC: |
| 4627 | case Instruction::UNUSED_EE: |
| 4628 | case Instruction::UNUSED_EF: |
| 4629 | case Instruction::UNUSED_F0: |
| 4630 | case Instruction::UNUSED_F1: |
| 4631 | case Instruction::UNUSED_F2: |
| 4632 | case Instruction::UNUSED_F3: |
| 4633 | case Instruction::UNUSED_F4: |
| 4634 | case Instruction::UNUSED_F5: |
| 4635 | case Instruction::UNUSED_F6: |
| 4636 | case Instruction::UNUSED_F7: |
| 4637 | case Instruction::UNUSED_F8: |
| 4638 | case Instruction::UNUSED_F9: |
| 4639 | case Instruction::UNUSED_FA: |
| 4640 | case Instruction::UNUSED_FB: |
| 4641 | case Instruction::UNUSED_FC: |
| 4642 | case Instruction::UNUSED_FD: |
| 4643 | case Instruction::UNUSED_FE: |
| 4644 | case Instruction::UNUSED_FF: |
| 4645 | LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode(); |
| 4646 | break; |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | method_info_.this_reg_idx = this_reg_idx; |
| 4651 | // According to the statistics, there are few methods that modify the "this" pointer. So this is a |
| 4652 | // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we |
| 4653 | // should remove this trick. |
| 4654 | method_info_.this_will_not_be_null = !modify_this; |
| 4655 | method_info_.has_invoke = has_invoke; |
| 4656 | // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry |
| 4657 | // for GC. |
| 4658 | method_info_.need_shadow_frame_entry = has_invoke || may_have_loop; |
| 4659 | // If this method may throw an exception, we need a shadow frame for stack trace (dexpc). |
| 4660 | method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception; |
TDYa127 | af54347 | 2012-05-28 21:49:23 -0700 | [diff] [blame^] | 4661 | // If can only throw exception, but can't suspend check (no loop, no invoke), |
| 4662 | // then there is no shadow frame entry. Only Shadow frame is needed. |
| 4663 | method_info_.lazy_push_shadow_frame = |
| 4664 | method_info_.need_shadow_frame && !method_info_.need_shadow_frame_entry; |
TDYa127 | cc1b4c3 | 2012-05-15 07:31:37 -0700 | [diff] [blame] | 4665 | } |
| 4666 | |
| 4667 | |
| 4668 | |
Logan Chien | 8342616 | 2011-12-09 09:29:50 +0800 | [diff] [blame] | 4669 | } // namespace compiler_llvm |
| 4670 | } // namespace art |