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