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