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