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