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