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