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