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