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