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