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