blob: 3122712505f79ed91e6258b14851ae9cc07a3de8 [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
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 Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
Logan Chienc670a8d2011-12-20 21:25:56 +080038#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080040#include <llvm/GlobalVariable.h>
41#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043namespace art {
44namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080045
Logan Chien42e0e152012-01-13 15:42:36 +080046using namespace runtime_support;
47
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048
Logan Chien8b977d32012-02-21 19:14:55 +080049MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080050 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080051 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080052 : cunit_(cunit), compiler_(compiler),
53 class_linker_(oat_compilation_unit->class_linker_),
54 class_loader_(oat_compilation_unit->class_loader_),
55 dex_file_(oat_compilation_unit->dex_file_),
56 dex_cache_(oat_compilation_unit->dex_cache_),
57 code_item_(oat_compilation_unit->code_item_),
58 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080059 method_idx_(oat_compilation_unit->method_idx_),
60 access_flags_(oat_compilation_unit->access_flags_),
61 module_(cunit->GetModule()),
62 context_(cunit->GetLLVMContext()),
TDYa1271d7e5102012-05-13 09:27:05 -070063 irb_(*cunit->GetIRBuilder()),
64 func_(NULL),
65 regs_(code_item_->registers_size_),
66 reg_to_shadow_frame_index_(code_item_->registers_size_, -1),
67 retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070068 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080069 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080070 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080071 basic_blocks_(code_item_->insns_size_in_code_units_),
72 basic_block_landing_pads_(code_item_->tries_size_, NULL),
73 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080074 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075}
76
77
78MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080079 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080080}
81
82
Logan Chien0b827102011-12-20 19:46:14 +080083void MethodCompiler::CreateFunction() {
84 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080085 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Get function type
88 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080089 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080090
91 // Create function
92 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
93 func_name, module_);
94
TDYa12767ae8ff2012-05-02 19:08:02 -070095#if !defined(NDEBUG)
Logan Chien0b827102011-12-20 19:46:14 +080096 // Set argument name
97 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
98 llvm::Function::arg_iterator arg_end(func_->arg_end());
99
100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("method");
102 ++arg_iter;
103
Logan Chiendd361c92012-04-10 23:40:37 +0800104 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800105 DCHECK_NE(arg_iter, arg_end);
106 arg_iter->setName("this");
107 ++arg_iter;
108 }
109
110 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
111 arg_iter->setName(StringPrintf("a%u", i));
112 }
TDYa12767ae8ff2012-05-02 19:08:02 -0700113#endif
Logan Chien0b827102011-12-20 19:46:14 +0800114}
115
116
117llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
118 bool is_static) {
119 // Get method signature
120 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
121
Logan Chien8faf8022012-02-24 12:25:29 +0800122 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800123 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800124 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800125
126 // Get return type
127 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
128
129 // Get argument type
130 std::vector<llvm::Type*> args_type;
131
132 args_type.push_back(irb_.getJObjectTy()); // method object pointer
133
134 if (!is_static) {
135 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
136 }
137
Logan Chien8faf8022012-02-24 12:25:29 +0800138 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800139 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
140 }
141
142 return llvm::FunctionType::get(ret_type, args_type, false);
143}
144
145
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800146void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800147 // Create basic blocks for prologue
TDYa12799489132012-04-29 01:27:58 -0700148#if !defined(NDEBUG)
149 // Add a BasicBlock named as PrettyMethod for debugging.
150 llvm::BasicBlock* entry =
151 llvm::BasicBlock::Create(*context_, PrettyMethod(method_idx_, *dex_file_), func_);
152#endif
Logan Chienc670a8d2011-12-20 21:25:56 +0800153 basic_block_reg_alloca_ =
154 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
155
TDYa12797339c42012-04-29 01:26:35 -0700156 basic_block_stack_overflow_ =
157 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
158
Logan Chien8dfcbea2012-02-17 18:50:32 +0800159 basic_block_shadow_frame_alloca_ =
160 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
161
Logan Chiend6ececa2011-12-27 16:20:15 +0800162 basic_block_reg_arg_init_ =
163 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
164
TDYa12799489132012-04-29 01:27:58 -0700165#if !defined(NDEBUG)
166 irb_.SetInsertPoint(entry);
167 irb_.CreateBr(basic_block_reg_alloca_);
168#endif
169
Logan Chienc670a8d2011-12-20 21:25:56 +0800170 // Create register array
171 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
TDYa1271d7e5102012-05-13 09:27:05 -0700172 regs_[r] = DalvikReg::CreateLocalVarReg(*this, r);
Logan Chienc670a8d2011-12-20 21:25:56 +0800173 }
174
175 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800176
Logan Chien8dfcbea2012-02-17 18:50:32 +0800177 // Create Shadow Frame
TDYa127cc1b4c32012-05-15 07:31:37 -0700178 if (method_info_.need_shadow_frame) {
179 EmitPrologueAllocShadowFrame();
180 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800181
Logan Chiend6ececa2011-12-27 16:20:15 +0800182 // Store argument to dalvik register
183 irb_.SetInsertPoint(basic_block_reg_arg_init_);
184 EmitPrologueAssignArgRegister();
185
186 // Branch to start address
187 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800188}
189
190
TDYa1274165a832012-04-03 17:47:16 -0700191void MethodCompiler::EmitStackOverflowCheck() {
192 irb_.SetInsertPoint(basic_block_stack_overflow_);
193
194 // Call llvm intrinsic function to get frame address.
195 llvm::Function* frameaddress =
196 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
197
198 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
199 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
200
201 // Cast i8* to int
202 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
203
204 // Get thread.stack_end_
205 llvm::Value* thread_object_addr =
206 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
207
TDYa127ee1f59b2012-04-25 00:56:40 -0700208 llvm::Value* stack_end =
209 irb_.LoadFromObjectOffset(thread_object_addr,
210 Thread::StackEndOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -0700211 irb_.getPtrEquivIntTy(),
212 kTBAARuntimeInfo);
TDYa1274165a832012-04-03 17:47:16 -0700213
214 // Check the frame address < thread.stack_end_ ?
215 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
216
217 llvm::BasicBlock* block_exception =
218 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
219
220 llvm::BasicBlock* block_continue =
221 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
222
TDYa127ac7b5bb2012-05-11 13:17:49 -0700223 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue, kUnlikely);
TDYa1274165a832012-04-03 17:47:16 -0700224
225 // If stack overflow, throw exception.
226 irb_.SetInsertPoint(block_exception);
227 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
228
229 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800230 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700231 if (ret_shorty == 'V') {
232 irb_.CreateRetVoid();
233 } else {
234 irb_.CreateRet(irb_.getJZero(ret_shorty));
235 }
236
237 basic_block_stack_overflow_ = block_continue;
238}
239
240
Logan Chienc670a8d2011-12-20 21:25:56 +0800241void MethodCompiler::EmitPrologueLastBranch() {
242 irb_.SetInsertPoint(basic_block_reg_alloca_);
TDYa12797339c42012-04-29 01:26:35 -0700243 irb_.CreateBr(basic_block_stack_overflow_);
244
TDYa127cc1b4c32012-05-15 07:31:37 -0700245 // If a method will not call to other method, and the method is small, we can avoid stack overflow
246 // check.
247 if (method_info_.has_invoke ||
248 code_item_->registers_size_ > 32) { // Small leaf function is OK given
249 // the 8KB reserved at Stack End
250 EmitStackOverflowCheck();
251 }
TDYa12797339c42012-04-29 01:26:35 -0700252
253 irb_.SetInsertPoint(basic_block_stack_overflow_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800254 irb_.CreateBr(basic_block_shadow_frame_alloca_);
255
256 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800257 irb_.CreateBr(basic_block_reg_arg_init_);
258}
259
260
Logan Chien8dfcbea2012-02-17 18:50:32 +0800261void MethodCompiler::EmitPrologueAllocShadowFrame() {
262 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
263
264 // Allocate the shadow frame now!
TDYa1271d7e5102012-05-13 09:27:05 -0700265 uint32_t sirt_size = 0;
TDYa127f1652862012-05-16 21:44:35 -0700266 if (method_info_.need_shadow_frame_entry) {
267 for (uint32_t i = 0, num_of_regs = code_item_->registers_size_; i < num_of_regs; ++i) {
268 if (IsRegCanBeObject(i)) {
269 reg_to_shadow_frame_index_[i] = sirt_size++;
270 }
TDYa1271d7e5102012-05-13 09:27:05 -0700271 }
272 }
Logan Chien8dfcbea2012-02-17 18:50:32 +0800273
274 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
275 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
276
277 // Zero-initialization of the shadow frame
278 llvm::ConstantAggregateZero* zero_initializer =
279 llvm::ConstantAggregateZero::get(shadow_frame_type);
280
TDYa127d955bec2012-05-11 10:54:02 -0700281 irb_.CreateStore(zero_initializer, shadow_frame_, kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800282
TDYa127ee1f59b2012-04-25 00:56:40 -0700283 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800284 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700285
286 // Store the method pointer
287 irb_.StoreToObjectOffset(shadow_frame_,
288 ShadowFrame::MethodOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700289 method_object_addr,
TDYa127d955bec2012-05-11 10:54:02 -0700290 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800291
292 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700293 irb_.StoreToObjectOffset(shadow_frame_,
294 ShadowFrame::NumberOfReferencesOffset(),
TDYa127aba61122012-05-04 18:28:36 -0700295 irb_.getJInt(sirt_size),
TDYa127d955bec2012-05-11 10:54:02 -0700296 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800297
298 // Push the shadow frame
299 llvm::Value* shadow_frame_upcast =
300 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
301
302 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
303}
304
305
Logan Chiend6ececa2011-12-27 16:20:15 +0800306void MethodCompiler::EmitPrologueAssignArgRegister() {
307 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
308
309 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
310 llvm::Function::arg_iterator arg_end(func_->arg_end());
311
Logan Chiendd361c92012-04-10 23:40:37 +0800312 uint32_t shorty_size = 0;
313 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
314 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800315
316 ++arg_iter; // skip method object
317
Logan Chiendd361c92012-04-10 23:40:37 +0800318 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800319 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
320 ++arg_iter;
321 ++arg_reg;
322 }
323
Logan Chiendd361c92012-04-10 23:40:37 +0800324 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800325 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
326
327 ++arg_reg;
328 if (shorty[i] == 'J' || shorty[i] == 'D') {
329 // Wide types, such as long and double, are using a pair of registers
330 // to store the value, so we have to increase arg_reg again.
331 ++arg_reg;
332 }
333 }
334
335 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800336}
337
338
Logan Chien83426162011-12-09 09:29:50 +0800339void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800340 uint32_t dex_pc = 0;
341 while (dex_pc < code_item_->insns_size_in_code_units_) {
342 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
343 EmitInstruction(dex_pc, insn);
344 dex_pc += insn->SizeInCodeUnits();
345 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800346}
347
348
Logan Chien83426162011-12-09 09:29:50 +0800349void MethodCompiler::EmitInstruction(uint32_t dex_pc,
350 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800351
352 // Set the IRBuilder insertion point
353 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
354
Logan Chien70f94b42011-12-27 17:49:11 +0800355#define ARGS dex_pc, insn
356
357 // Dispatch the instruction
358 switch (insn->Opcode()) {
359 case Instruction::NOP:
360 EmitInsn_Nop(ARGS);
361 break;
362
363 case Instruction::MOVE:
364 case Instruction::MOVE_FROM16:
365 case Instruction::MOVE_16:
366 EmitInsn_Move(ARGS, kInt);
367 break;
368
369 case Instruction::MOVE_WIDE:
370 case Instruction::MOVE_WIDE_FROM16:
371 case Instruction::MOVE_WIDE_16:
372 EmitInsn_Move(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_OBJECT:
376 case Instruction::MOVE_OBJECT_FROM16:
377 case Instruction::MOVE_OBJECT_16:
378 EmitInsn_Move(ARGS, kObject);
379 break;
380
381 case Instruction::MOVE_RESULT:
382 EmitInsn_MoveResult(ARGS, kInt);
383 break;
384
385 case Instruction::MOVE_RESULT_WIDE:
386 EmitInsn_MoveResult(ARGS, kLong);
387 break;
388
389 case Instruction::MOVE_RESULT_OBJECT:
390 EmitInsn_MoveResult(ARGS, kObject);
391 break;
392
393 case Instruction::MOVE_EXCEPTION:
394 EmitInsn_MoveException(ARGS);
395 break;
396
397 case Instruction::RETURN_VOID:
398 EmitInsn_ReturnVoid(ARGS);
399 break;
400
401 case Instruction::RETURN:
402 case Instruction::RETURN_WIDE:
403 case Instruction::RETURN_OBJECT:
404 EmitInsn_Return(ARGS);
405 break;
406
407 case Instruction::CONST_4:
408 case Instruction::CONST_16:
409 case Instruction::CONST:
410 case Instruction::CONST_HIGH16:
411 EmitInsn_LoadConstant(ARGS, kInt);
412 break;
413
414 case Instruction::CONST_WIDE_16:
415 case Instruction::CONST_WIDE_32:
416 case Instruction::CONST_WIDE:
417 case Instruction::CONST_WIDE_HIGH16:
418 EmitInsn_LoadConstant(ARGS, kLong);
419 break;
420
421 case Instruction::CONST_STRING:
422 case Instruction::CONST_STRING_JUMBO:
423 EmitInsn_LoadConstantString(ARGS);
424 break;
425
426 case Instruction::CONST_CLASS:
427 EmitInsn_LoadConstantClass(ARGS);
428 break;
429
430 case Instruction::MONITOR_ENTER:
431 EmitInsn_MonitorEnter(ARGS);
432 break;
433
434 case Instruction::MONITOR_EXIT:
435 EmitInsn_MonitorExit(ARGS);
436 break;
437
438 case Instruction::CHECK_CAST:
439 EmitInsn_CheckCast(ARGS);
440 break;
441
442 case Instruction::INSTANCE_OF:
443 EmitInsn_InstanceOf(ARGS);
444 break;
445
446 case Instruction::ARRAY_LENGTH:
447 EmitInsn_ArrayLength(ARGS);
448 break;
449
450 case Instruction::NEW_INSTANCE:
451 EmitInsn_NewInstance(ARGS);
452 break;
453
454 case Instruction::NEW_ARRAY:
455 EmitInsn_NewArray(ARGS);
456 break;
457
458 case Instruction::FILLED_NEW_ARRAY:
459 EmitInsn_FilledNewArray(ARGS, false);
460 break;
461
462 case Instruction::FILLED_NEW_ARRAY_RANGE:
463 EmitInsn_FilledNewArray(ARGS, true);
464 break;
465
466 case Instruction::FILL_ARRAY_DATA:
467 EmitInsn_FillArrayData(ARGS);
468 break;
469
470 case Instruction::THROW:
471 EmitInsn_ThrowException(ARGS);
472 break;
473
474 case Instruction::GOTO:
475 case Instruction::GOTO_16:
476 case Instruction::GOTO_32:
477 EmitInsn_UnconditionalBranch(ARGS);
478 break;
479
480 case Instruction::PACKED_SWITCH:
481 EmitInsn_PackedSwitch(ARGS);
482 break;
483
484 case Instruction::SPARSE_SWITCH:
485 EmitInsn_SparseSwitch(ARGS);
486 break;
487
488 case Instruction::CMPL_FLOAT:
489 EmitInsn_FPCompare(ARGS, kFloat, false);
490 break;
491
492 case Instruction::CMPG_FLOAT:
493 EmitInsn_FPCompare(ARGS, kFloat, true);
494 break;
495
496 case Instruction::CMPL_DOUBLE:
497 EmitInsn_FPCompare(ARGS, kDouble, false);
498 break;
499
500 case Instruction::CMPG_DOUBLE:
501 EmitInsn_FPCompare(ARGS, kDouble, true);
502 break;
503
504 case Instruction::CMP_LONG:
505 EmitInsn_LongCompare(ARGS);
506 break;
507
508 case Instruction::IF_EQ:
509 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
510 break;
511
512 case Instruction::IF_NE:
513 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
514 break;
515
516 case Instruction::IF_LT:
517 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
518 break;
519
520 case Instruction::IF_GE:
521 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
522 break;
523
524 case Instruction::IF_GT:
525 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
526 break;
527
528 case Instruction::IF_LE:
529 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
530 break;
531
532 case Instruction::IF_EQZ:
533 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
534 break;
535
536 case Instruction::IF_NEZ:
537 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
538 break;
539
540 case Instruction::IF_LTZ:
541 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
542 break;
543
544 case Instruction::IF_GEZ:
545 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
546 break;
547
548 case Instruction::IF_GTZ:
549 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
550 break;
551
552 case Instruction::IF_LEZ:
553 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
554 break;
555
556 case Instruction::AGET:
557 EmitInsn_AGet(ARGS, kInt);
558 break;
559
560 case Instruction::AGET_WIDE:
561 EmitInsn_AGet(ARGS, kLong);
562 break;
563
564 case Instruction::AGET_OBJECT:
565 EmitInsn_AGet(ARGS, kObject);
566 break;
567
568 case Instruction::AGET_BOOLEAN:
569 EmitInsn_AGet(ARGS, kBoolean);
570 break;
571
572 case Instruction::AGET_BYTE:
573 EmitInsn_AGet(ARGS, kByte);
574 break;
575
576 case Instruction::AGET_CHAR:
577 EmitInsn_AGet(ARGS, kChar);
578 break;
579
580 case Instruction::AGET_SHORT:
581 EmitInsn_AGet(ARGS, kShort);
582 break;
583
584 case Instruction::APUT:
585 EmitInsn_APut(ARGS, kInt);
586 break;
587
588 case Instruction::APUT_WIDE:
589 EmitInsn_APut(ARGS, kLong);
590 break;
591
592 case Instruction::APUT_OBJECT:
593 EmitInsn_APut(ARGS, kObject);
594 break;
595
596 case Instruction::APUT_BOOLEAN:
597 EmitInsn_APut(ARGS, kBoolean);
598 break;
599
600 case Instruction::APUT_BYTE:
601 EmitInsn_APut(ARGS, kByte);
602 break;
603
604 case Instruction::APUT_CHAR:
605 EmitInsn_APut(ARGS, kChar);
606 break;
607
608 case Instruction::APUT_SHORT:
609 EmitInsn_APut(ARGS, kShort);
610 break;
611
612 case Instruction::IGET:
613 EmitInsn_IGet(ARGS, kInt);
614 break;
615
616 case Instruction::IGET_WIDE:
617 EmitInsn_IGet(ARGS, kLong);
618 break;
619
620 case Instruction::IGET_OBJECT:
621 EmitInsn_IGet(ARGS, kObject);
622 break;
623
624 case Instruction::IGET_BOOLEAN:
625 EmitInsn_IGet(ARGS, kBoolean);
626 break;
627
628 case Instruction::IGET_BYTE:
629 EmitInsn_IGet(ARGS, kByte);
630 break;
631
632 case Instruction::IGET_CHAR:
633 EmitInsn_IGet(ARGS, kChar);
634 break;
635
636 case Instruction::IGET_SHORT:
637 EmitInsn_IGet(ARGS, kShort);
638 break;
639
640 case Instruction::IPUT:
641 EmitInsn_IPut(ARGS, kInt);
642 break;
643
644 case Instruction::IPUT_WIDE:
645 EmitInsn_IPut(ARGS, kLong);
646 break;
647
648 case Instruction::IPUT_OBJECT:
649 EmitInsn_IPut(ARGS, kObject);
650 break;
651
652 case Instruction::IPUT_BOOLEAN:
653 EmitInsn_IPut(ARGS, kBoolean);
654 break;
655
656 case Instruction::IPUT_BYTE:
657 EmitInsn_IPut(ARGS, kByte);
658 break;
659
660 case Instruction::IPUT_CHAR:
661 EmitInsn_IPut(ARGS, kChar);
662 break;
663
664 case Instruction::IPUT_SHORT:
665 EmitInsn_IPut(ARGS, kShort);
666 break;
667
668 case Instruction::SGET:
669 EmitInsn_SGet(ARGS, kInt);
670 break;
671
672 case Instruction::SGET_WIDE:
673 EmitInsn_SGet(ARGS, kLong);
674 break;
675
676 case Instruction::SGET_OBJECT:
677 EmitInsn_SGet(ARGS, kObject);
678 break;
679
680 case Instruction::SGET_BOOLEAN:
681 EmitInsn_SGet(ARGS, kBoolean);
682 break;
683
684 case Instruction::SGET_BYTE:
685 EmitInsn_SGet(ARGS, kByte);
686 break;
687
688 case Instruction::SGET_CHAR:
689 EmitInsn_SGet(ARGS, kChar);
690 break;
691
692 case Instruction::SGET_SHORT:
693 EmitInsn_SGet(ARGS, kShort);
694 break;
695
696 case Instruction::SPUT:
697 EmitInsn_SPut(ARGS, kInt);
698 break;
699
700 case Instruction::SPUT_WIDE:
701 EmitInsn_SPut(ARGS, kLong);
702 break;
703
704 case Instruction::SPUT_OBJECT:
705 EmitInsn_SPut(ARGS, kObject);
706 break;
707
708 case Instruction::SPUT_BOOLEAN:
709 EmitInsn_SPut(ARGS, kBoolean);
710 break;
711
712 case Instruction::SPUT_BYTE:
713 EmitInsn_SPut(ARGS, kByte);
714 break;
715
716 case Instruction::SPUT_CHAR:
717 EmitInsn_SPut(ARGS, kChar);
718 break;
719
720 case Instruction::SPUT_SHORT:
721 EmitInsn_SPut(ARGS, kShort);
722 break;
723
724
725 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800726 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800727 break;
728
729 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800730 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800731 break;
732
733 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800734 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800735 break;
736
737 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800738 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800739 break;
740
741 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800742 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800743 break;
744
745 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800746 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800747 break;
748
749 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800750 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800751 break;
752
753 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800754 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800755 break;
756
757 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800758 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800759 break;
760
761 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800762 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800763 break;
764
765 case Instruction::NEG_INT:
766 EmitInsn_Neg(ARGS, kInt);
767 break;
768
769 case Instruction::NOT_INT:
770 EmitInsn_Not(ARGS, kInt);
771 break;
772
773 case Instruction::NEG_LONG:
774 EmitInsn_Neg(ARGS, kLong);
775 break;
776
777 case Instruction::NOT_LONG:
778 EmitInsn_Not(ARGS, kLong);
779 break;
780
781 case Instruction::NEG_FLOAT:
782 EmitInsn_FNeg(ARGS, kFloat);
783 break;
784
785 case Instruction::NEG_DOUBLE:
786 EmitInsn_FNeg(ARGS, kDouble);
787 break;
788
789 case Instruction::INT_TO_LONG:
790 EmitInsn_SExt(ARGS);
791 break;
792
793 case Instruction::INT_TO_FLOAT:
794 EmitInsn_IntToFP(ARGS, kInt, kFloat);
795 break;
796
797 case Instruction::INT_TO_DOUBLE:
798 EmitInsn_IntToFP(ARGS, kInt, kDouble);
799 break;
800
801 case Instruction::LONG_TO_INT:
802 EmitInsn_Trunc(ARGS);
803 break;
804
805 case Instruction::LONG_TO_FLOAT:
806 EmitInsn_IntToFP(ARGS, kLong, kFloat);
807 break;
808
809 case Instruction::LONG_TO_DOUBLE:
810 EmitInsn_IntToFP(ARGS, kLong, kDouble);
811 break;
812
813 case Instruction::FLOAT_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700814 EmitInsn_FPToInt(ARGS, kFloat, kInt, art_f2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800815 break;
816
817 case Instruction::FLOAT_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700818 EmitInsn_FPToInt(ARGS, kFloat, kLong, art_f2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800819 break;
820
821 case Instruction::FLOAT_TO_DOUBLE:
822 EmitInsn_FExt(ARGS);
823 break;
824
825 case Instruction::DOUBLE_TO_INT:
jeffhao41005dd2012-05-09 17:58:52 -0700826 EmitInsn_FPToInt(ARGS, kDouble, kInt, art_d2i);
Logan Chien70f94b42011-12-27 17:49:11 +0800827 break;
828
829 case Instruction::DOUBLE_TO_LONG:
jeffhao41005dd2012-05-09 17:58:52 -0700830 EmitInsn_FPToInt(ARGS, kDouble, kLong, art_d2l);
Logan Chien70f94b42011-12-27 17:49:11 +0800831 break;
832
833 case Instruction::DOUBLE_TO_FLOAT:
834 EmitInsn_FTrunc(ARGS);
835 break;
836
837 case Instruction::INT_TO_BYTE:
838 EmitInsn_TruncAndSExt(ARGS, 8);
839 break;
840
841 case Instruction::INT_TO_CHAR:
842 EmitInsn_TruncAndZExt(ARGS, 16);
843 break;
844
845 case Instruction::INT_TO_SHORT:
846 EmitInsn_TruncAndSExt(ARGS, 16);
847 break;
848
849 case Instruction::ADD_INT:
850 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
851 break;
852
853 case Instruction::SUB_INT:
854 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
855 break;
856
857 case Instruction::MUL_INT:
858 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
859 break;
860
861 case Instruction::DIV_INT:
862 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
863 break;
864
865 case Instruction::REM_INT:
866 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
867 break;
868
869 case Instruction::AND_INT:
870 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
871 break;
872
873 case Instruction::OR_INT:
874 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
875 break;
876
877 case Instruction::XOR_INT:
878 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
879 break;
880
881 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800882 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800883 break;
884
885 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800886 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800887 break;
888
889 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800890 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800891 break;
892
893 case Instruction::ADD_LONG:
894 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
895 break;
896
897 case Instruction::SUB_LONG:
898 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
899 break;
900
901 case Instruction::MUL_LONG:
902 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
903 break;
904
905 case Instruction::DIV_LONG:
906 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
907 break;
908
909 case Instruction::REM_LONG:
910 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
911 break;
912
913 case Instruction::AND_LONG:
914 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
915 break;
916
917 case Instruction::OR_LONG:
918 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
919 break;
920
921 case Instruction::XOR_LONG:
922 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
923 break;
924
925 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800926 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800927 break;
928
929 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800930 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800931 break;
932
933 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800934 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800935 break;
936
937 case Instruction::ADD_FLOAT:
938 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
939 break;
940
941 case Instruction::SUB_FLOAT:
942 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
943 break;
944
945 case Instruction::MUL_FLOAT:
946 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
947 break;
948
949 case Instruction::DIV_FLOAT:
950 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
951 break;
952
953 case Instruction::REM_FLOAT:
954 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
955 break;
956
957 case Instruction::ADD_DOUBLE:
958 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
959 break;
960
961 case Instruction::SUB_DOUBLE:
962 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
963 break;
964
965 case Instruction::MUL_DOUBLE:
966 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
967 break;
968
969 case Instruction::DIV_DOUBLE:
970 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
971 break;
972
973 case Instruction::REM_DOUBLE:
974 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
975 break;
976
977 case Instruction::ADD_INT_2ADDR:
978 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
979 break;
980
981 case Instruction::SUB_INT_2ADDR:
982 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
983 break;
984
985 case Instruction::MUL_INT_2ADDR:
986 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
987 break;
988
989 case Instruction::DIV_INT_2ADDR:
990 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
991 break;
992
993 case Instruction::REM_INT_2ADDR:
994 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
995 break;
996
997 case Instruction::AND_INT_2ADDR:
998 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
999 break;
1000
1001 case Instruction::OR_INT_2ADDR:
1002 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
1003 break;
1004
1005 case Instruction::XOR_INT_2ADDR:
1006 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
1007 break;
1008
1009 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001010 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001011 break;
1012
1013 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001014 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001015 break;
1016
1017 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001018 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001019 break;
1020
1021 case Instruction::ADD_LONG_2ADDR:
1022 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1023 break;
1024
1025 case Instruction::SUB_LONG_2ADDR:
1026 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1027 break;
1028
1029 case Instruction::MUL_LONG_2ADDR:
1030 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1031 break;
1032
1033 case Instruction::DIV_LONG_2ADDR:
1034 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1035 break;
1036
1037 case Instruction::REM_LONG_2ADDR:
1038 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1039 break;
1040
1041 case Instruction::AND_LONG_2ADDR:
1042 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1043 break;
1044
1045 case Instruction::OR_LONG_2ADDR:
1046 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1047 break;
1048
1049 case Instruction::XOR_LONG_2ADDR:
1050 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1051 break;
1052
1053 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001054 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001055 break;
1056
1057 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001058 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001059 break;
1060
1061 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001062 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001063 break;
1064
1065 case Instruction::ADD_FLOAT_2ADDR:
1066 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1067 break;
1068
1069 case Instruction::SUB_FLOAT_2ADDR:
1070 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1071 break;
1072
1073 case Instruction::MUL_FLOAT_2ADDR:
1074 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1075 break;
1076
1077 case Instruction::DIV_FLOAT_2ADDR:
1078 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1079 break;
1080
1081 case Instruction::REM_FLOAT_2ADDR:
1082 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1083 break;
1084
1085 case Instruction::ADD_DOUBLE_2ADDR:
1086 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1087 break;
1088
1089 case Instruction::SUB_DOUBLE_2ADDR:
1090 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1091 break;
1092
1093 case Instruction::MUL_DOUBLE_2ADDR:
1094 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1095 break;
1096
1097 case Instruction::DIV_DOUBLE_2ADDR:
1098 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1099 break;
1100
1101 case Instruction::REM_DOUBLE_2ADDR:
1102 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1103 break;
1104
1105 case Instruction::ADD_INT_LIT16:
1106 case Instruction::ADD_INT_LIT8:
1107 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1108 break;
1109
1110 case Instruction::RSUB_INT:
1111 case Instruction::RSUB_INT_LIT8:
1112 EmitInsn_RSubImmediate(ARGS);
1113 break;
1114
1115 case Instruction::MUL_INT_LIT16:
1116 case Instruction::MUL_INT_LIT8:
1117 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1118 break;
1119
1120 case Instruction::DIV_INT_LIT16:
1121 case Instruction::DIV_INT_LIT8:
1122 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1123 break;
1124
1125 case Instruction::REM_INT_LIT16:
1126 case Instruction::REM_INT_LIT8:
1127 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1128 break;
1129
1130 case Instruction::AND_INT_LIT16:
1131 case Instruction::AND_INT_LIT8:
1132 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1133 break;
1134
1135 case Instruction::OR_INT_LIT16:
1136 case Instruction::OR_INT_LIT8:
1137 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1138 break;
1139
1140 case Instruction::XOR_INT_LIT16:
1141 case Instruction::XOR_INT_LIT8:
1142 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1143 break;
1144
1145 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001146 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001147 break;
1148
1149 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001150 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001151 break;
1152
1153 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001154 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001155 break;
1156
Logan Chien9e5f5c12012-04-10 13:51:45 +08001157 case Instruction::THROW_VERIFICATION_ERROR:
1158 EmitInsn_ThrowVerificationError(ARGS);
1159 break;
1160
Logan Chien70f94b42011-12-27 17:49:11 +08001161 case Instruction::UNUSED_3E:
1162 case Instruction::UNUSED_3F:
1163 case Instruction::UNUSED_40:
1164 case Instruction::UNUSED_41:
1165 case Instruction::UNUSED_42:
1166 case Instruction::UNUSED_43:
1167 case Instruction::UNUSED_73:
1168 case Instruction::UNUSED_79:
1169 case Instruction::UNUSED_7A:
1170 case Instruction::UNUSED_E3:
1171 case Instruction::UNUSED_E4:
1172 case Instruction::UNUSED_E5:
1173 case Instruction::UNUSED_E6:
1174 case Instruction::UNUSED_E7:
1175 case Instruction::UNUSED_E8:
1176 case Instruction::UNUSED_E9:
1177 case Instruction::UNUSED_EA:
1178 case Instruction::UNUSED_EB:
1179 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001180 case Instruction::UNUSED_EE:
1181 case Instruction::UNUSED_EF:
1182 case Instruction::UNUSED_F0:
1183 case Instruction::UNUSED_F1:
1184 case Instruction::UNUSED_F2:
1185 case Instruction::UNUSED_F3:
1186 case Instruction::UNUSED_F4:
1187 case Instruction::UNUSED_F5:
1188 case Instruction::UNUSED_F6:
1189 case Instruction::UNUSED_F7:
1190 case Instruction::UNUSED_F8:
1191 case Instruction::UNUSED_F9:
1192 case Instruction::UNUSED_FA:
1193 case Instruction::UNUSED_FB:
1194 case Instruction::UNUSED_FC:
1195 case Instruction::UNUSED_FD:
1196 case Instruction::UNUSED_FE:
1197 case Instruction::UNUSED_FF:
1198 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1199 break;
1200 }
1201
1202#undef ARGS
1203}
1204
1205
1206void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1207 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001208
1209 uint16_t insn_signature = code_item_->insns_[dex_pc];
1210
1211 if (insn_signature == Instruction::kPackedSwitchSignature ||
1212 insn_signature == Instruction::kSparseSwitchSignature ||
1213 insn_signature == Instruction::kArrayDataSignature) {
1214 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001215 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001218}
1219
1220
Logan Chien70f94b42011-12-27 17:49:11 +08001221void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1222 Instruction const* insn,
1223 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001224
Elliott Hughesadb8c672012-03-06 16:49:32 -08001225 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001226
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1228 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001229
Logan Chien70f94b42011-12-27 17:49:11 +08001230 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1231}
1232
1233
1234void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1235 Instruction const* insn,
1236 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001237
Elliott Hughesadb8c672012-03-06 16:49:32 -08001238 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001239
1240 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001241 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001242
Logan Chien70f94b42011-12-27 17:49:11 +08001243 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1244}
1245
1246
1247void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1248 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001249
Elliott Hughesadb8c672012-03-06 16:49:32 -08001250 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001251
TDYa127ee1f59b2012-04-25 00:56:40 -07001252 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001253 llvm::Value* thread_object_addr =
1254 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1255
TDYa127ee1f59b2012-04-25 00:56:40 -07001256 // Get thread-local exception field address
1257 llvm::Value* exception_object_addr =
1258 irb_.LoadFromObjectOffset(thread_object_addr,
1259 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001260 irb_.getJObjectTy(),
TDYa1278ca10052012-05-05 19:57:06 -07001261 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001262
1263 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001264 irb_.StoreToObjectOffset(thread_object_addr,
1265 Thread::ExceptionOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001266 irb_.getJNull(),
TDYa1278ca10052012-05-05 19:57:06 -07001267 kTBAAJRuntime);
Logan Chien3354cec2012-01-13 14:29:03 +08001268
1269 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001270 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001271
Logan Chien70f94b42011-12-27 17:49:11 +08001272 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1273}
1274
1275
1276void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1277 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001278
Elliott Hughesadb8c672012-03-06 16:49:32 -08001279 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001280
1281 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001282 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001283
TDYa127c8dc1012012-04-19 07:03:33 -07001284 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001285
Logan Chien6c6f12d2012-01-13 19:26:27 +08001286 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1287
1288 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001289}
1290
1291
Logan Chien9e5f5c12012-04-10 13:51:45 +08001292void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1293 Instruction const* insn) {
1294
1295 DecodedInstruction dec_insn(insn);
1296
TDYa127c8dc1012012-04-19 07:03:33 -07001297 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001298
1299 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1300 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1301 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1302
1303 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1304 method_object_addr, kind_value, ref_value);
1305
1306 EmitBranchExceptionLandingPad(dex_pc);
1307}
1308
1309
Logan Chien70f94b42011-12-27 17:49:11 +08001310void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1311 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001312 // Garbage collection safe-point
1313 EmitGuard_GarbageCollectionSuspend(dex_pc);
1314
Logan Chien8dfcbea2012-02-17 18:50:32 +08001315 // Pop the shadow frame
1316 EmitPopShadowFrame();
1317
Logan Chien8898a272011-12-27 17:51:56 +08001318 // Return!
1319 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001320}
1321
1322
1323void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1324 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001325
Elliott Hughesadb8c672012-03-06 16:49:32 -08001326 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001327
1328 // Garbage collection safe-point
1329 EmitGuard_GarbageCollectionSuspend(dex_pc);
1330
Logan Chien8dfcbea2012-02-17 18:50:32 +08001331 // Pop the shadow frame
1332 EmitPopShadowFrame();
1333 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1334 // the return value might be collected since the shadow stack is popped.
1335
Logan Chien8898a272011-12-27 17:51:56 +08001336 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001337 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001338 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001339
1340 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001341}
1342
1343
1344void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1345 Instruction const* insn,
1346 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001347
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001349
1350 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1351
1352 int64_t imm = 0;
1353
1354 switch (insn->Opcode()) {
1355 // 32-bit Immediate
1356 case Instruction::CONST_4:
1357 case Instruction::CONST_16:
1358 case Instruction::CONST:
1359 case Instruction::CONST_WIDE_16:
1360 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001361 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001362 break;
1363
1364 case Instruction::CONST_HIGH16:
1365 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001366 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001367 break;
1368
1369 // 64-bit Immediate
1370 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001372 break;
1373
1374 case Instruction::CONST_WIDE_HIGH16:
1375 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001377 break;
1378
1379 // Unknown opcode for load constant (unreachable)
1380 default:
1381 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1382 break;
1383 }
1384
1385 // Store the non-object register
1386 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1387 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001389
1390 // Store the object register if it is possible to be null.
1391 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001392 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001393 }
1394
Logan Chien70f94b42011-12-27 17:49:11 +08001395 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1396}
1397
1398
1399void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1400 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001401
Elliott Hughesadb8c672012-03-06 16:49:32 -08001402 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001403
Elliott Hughesadb8c672012-03-06 16:49:32 -08001404 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001405
1406 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1407
TDYa1278ca10052012-05-05 19:57:06 -07001408 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr, kTBAAJRuntime);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001409
1410 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1411 llvm::BasicBlock* block_str_exist =
1412 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1413
1414 llvm::BasicBlock* block_str_resolve =
1415 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1416
1417 // Test: Is the string resolved and in the dex cache?
1418 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1419
TDYa127ac7b5bb2012-05-11 13:17:49 -07001420 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist, kUnlikely);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001421
1422 // String is resolved, go to next basic block.
1423 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001425 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1426
1427 // String is not resolved yet, resolve it now.
1428 irb_.SetInsertPoint(block_str_resolve);
1429
1430 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1431
1432 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1433
1434 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1435
TDYa127c8dc1012012-04-19 07:03:33 -07001436 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001437
1438 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1439 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001440
1441 EmitGuard_ExceptionLandingPad(dex_pc);
1442 }
1443
1444 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001445 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001446
Logan Chien70f94b42011-12-27 17:49:11 +08001447 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1448}
1449
1450
Logan Chien27b30252012-01-14 03:43:35 +08001451llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1452 uint32_t type_idx) {
1453 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1454 *dex_file_, type_idx)) {
1455 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1456
1457 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1458
TDYa127706e9b62012-04-19 12:24:26 -07001459 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1460
Logan Chien27b30252012-01-14 03:43:35 +08001461 llvm::Function* runtime_func =
1462 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1463
TDYa127c8dc1012012-04-19 07:03:33 -07001464 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001465
Logan Chien27b30252012-01-14 03:43:35 +08001466 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001467 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001468
1469 EmitGuard_ExceptionLandingPad(dex_pc);
1470
1471 return type_object_addr;
1472
1473 } else {
1474 // Try to load the class (type) object from the test cache.
1475 llvm::Value* type_field_addr =
1476 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1477
TDYa1278ca10052012-05-05 19:57:06 -07001478 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr, kTBAAJRuntime);
Logan Chien27b30252012-01-14 03:43:35 +08001479
1480 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1481 return type_object_addr;
1482 }
1483
1484 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1485
1486 // Test whether class (type) object is in the dex cache or not
1487 llvm::Value* equal_null =
1488 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1489
1490 llvm::BasicBlock* block_cont =
1491 CreateBasicBlockWithDexPC(dex_pc, "cont");
1492
1493 llvm::BasicBlock* block_load_class =
1494 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1495
TDYa127ac7b5bb2012-05-11 13:17:49 -07001496 irb_.CreateCondBr(equal_null, block_load_class, block_cont, kUnlikely);
Logan Chien27b30252012-01-14 03:43:35 +08001497
1498 // Failback routine to load the class object
1499 irb_.SetInsertPoint(block_load_class);
1500
1501 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1502
1503 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1504
1505 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1506
TDYa127706e9b62012-04-19 12:24:26 -07001507 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1508
TDYa127c8dc1012012-04-19 07:03:33 -07001509 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001510
Logan Chien27b30252012-01-14 03:43:35 +08001511 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001512 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001513
1514 EmitGuard_ExceptionLandingPad(dex_pc);
1515
1516 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1517
1518 irb_.CreateBr(block_cont);
1519
1520 // Now the class object must be loaded
1521 irb_.SetInsertPoint(block_cont);
1522
1523 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1524
1525 phi->addIncoming(type_object_addr, block_original);
1526 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1527
1528 return phi;
1529 }
1530}
1531
1532
Logan Chien70f94b42011-12-27 17:49:11 +08001533void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1534 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001535
Elliott Hughesadb8c672012-03-06 16:49:32 -08001536 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001537
Elliott Hughesadb8c672012-03-06 16:49:32 -08001538 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1539 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001540
Logan Chien70f94b42011-12-27 17:49:11 +08001541 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1542}
1543
1544
1545void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1546 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
Elliott Hughesadb8c672012-03-06 16:49:32 -08001548 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
1550 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001551 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001552
TDYa127cc1b4c32012-05-15 07:31:37 -07001553 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1554 EmitGuard_NullPointerException(dex_pc, object_addr);
1555 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001556
TDYa127706e9b62012-04-19 12:24:26 -07001557 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1558
1559 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001560
Logan Chien70f94b42011-12-27 17:49:11 +08001561 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1562}
1563
1564
1565void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1566 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001567
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001569
1570 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001571 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001572
TDYa127cc1b4c32012-05-15 07:31:37 -07001573 if (!(method_info_.this_will_not_be_null && dec_insn.vA == method_info_.this_reg_idx)) {
1574 EmitGuard_NullPointerException(dex_pc, object_addr);
1575 }
Logan Chien9e0dbe42012-01-13 12:11:37 +08001576
TDYa127c8dc1012012-04-19 07:03:33 -07001577 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001578
TDYa127706e9b62012-04-19 12:24:26 -07001579 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1580
1581 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001582 EmitGuard_ExceptionLandingPad(dex_pc);
1583
Logan Chien70f94b42011-12-27 17:49:11 +08001584 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1585}
1586
1587
1588void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1589 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001590
Elliott Hughesadb8c672012-03-06 16:49:32 -08001591 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001592
1593 llvm::BasicBlock* block_test_class =
1594 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1595
1596 llvm::BasicBlock* block_test_sub_class =
1597 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1598
1599 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001600 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001601
1602 // Test: Is the reference equal to null? Act as no-op when it is null.
1603 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1604
1605 irb_.CreateCondBr(equal_null,
1606 GetNextBasicBlock(dex_pc),
1607 block_test_class);
1608
1609 // Test: Is the object instantiated from the given class?
1610 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001612 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1613
1614 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1615
1616 llvm::Value* object_type_field_addr =
1617 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1618
1619 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001620 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chienfc880952012-01-15 23:53:10 +08001621
1622 llvm::Value* equal_class =
1623 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1624
1625 irb_.CreateCondBr(equal_class,
1626 GetNextBasicBlock(dex_pc),
1627 block_test_sub_class);
1628
1629 // Test: Is the object instantiated from the subclass of the given class?
1630 irb_.SetInsertPoint(block_test_sub_class);
1631
TDYa127c8dc1012012-04-19 07:03:33 -07001632 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001633
Logan Chienfc880952012-01-15 23:53:10 +08001634 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1635 type_object_addr, object_type_object_addr);
1636
1637 EmitGuard_ExceptionLandingPad(dex_pc);
1638
Logan Chien70f94b42011-12-27 17:49:11 +08001639 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1640}
1641
1642
1643void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1644 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001645
Elliott Hughesadb8c672012-03-06 16:49:32 -08001646 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001647
1648 llvm::Constant* zero = irb_.getJInt(0);
1649 llvm::Constant* one = irb_.getJInt(1);
1650
1651 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1652
1653 llvm::BasicBlock* block_test_class =
1654 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1655
1656 llvm::BasicBlock* block_class_equals =
1657 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1658
1659 llvm::BasicBlock* block_test_sub_class =
1660 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1661
1662 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001663 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001664
1665 // Overview of the following code :
1666 // We check for null, if so, then false, otherwise check for class == . If so
1667 // then true, otherwise do callout slowpath.
1668 //
1669 // Test: Is the reference equal to null? Set 0 when it is null.
1670 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1671
1672 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1673
1674 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1677
1678 // Test: Is the object instantiated from the given class?
1679 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001680 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001681 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1682
1683 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1684
1685 llvm::Value* object_type_field_addr =
1686 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1687
1688 llvm::Value* object_type_object_addr =
TDYa127d3e24c22012-05-05 20:54:19 -07001689 irb_.CreateLoad(object_type_field_addr, kTBAAConstJObject);
Logan Chien68725e22012-01-15 22:25:34 +08001690
1691 llvm::Value* equal_class =
1692 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1693
1694 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1695
1696 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001697 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001698 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1699
1700 // Test: Is the object instantiated from the subclass of the given class?
1701 irb_.SetInsertPoint(block_test_sub_class);
1702
1703 llvm::Value* result =
1704 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1705 type_object_addr, object_type_object_addr);
1706
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001708
Logan Chien70f94b42011-12-27 17:49:11 +08001709 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1710}
1711
1712
Logan Chien61bb6142012-02-03 15:34:53 +08001713llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001714 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001715 return irb_.LoadFromObjectOffset(array,
1716 Array::LengthOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07001717 irb_.getJIntTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07001718 kTBAAConstJObject);
Logan Chien61bb6142012-02-03 15:34:53 +08001719}
1720
1721
Logan Chien70f94b42011-12-27 17:49:11 +08001722void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1723 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001724
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001726
1727 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001729 EmitGuard_NullPointerException(dex_pc, array_addr);
1730
1731 // Get the array length and store it to the register
1732 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001733 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001734
Logan Chien70f94b42011-12-27 17:49:11 +08001735 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1736}
1737
1738
1739void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1740 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001741
Elliott Hughesadb8c672012-03-06 16:49:32 -08001742 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001743
1744 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001745 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1746 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001747 runtime_func = irb_.GetRuntime(AllocObject);
1748 } else {
1749 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1750 }
1751
Elliott Hughesadb8c672012-03-06 16:49:32 -08001752 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001753
1754 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1755
TDYa127da83d972012-04-18 00:21:49 -07001756 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1757
TDYa127c8dc1012012-04-19 07:03:33 -07001758 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001759
Logan Chien032bdad2012-01-16 09:59:23 +08001760 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001761 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001762
1763 EmitGuard_ExceptionLandingPad(dex_pc);
1764
Elliott Hughesadb8c672012-03-06 16:49:32 -08001765 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001766
Logan Chien70f94b42011-12-27 17:49:11 +08001767 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1768}
1769
1770
Logan Chiena2cc6a32012-01-16 10:38:41 +08001771llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1772 int32_t length,
1773 uint32_t type_idx,
1774 bool is_filled_new_array) {
1775 llvm::Function* runtime_func;
1776
1777 bool skip_access_check =
1778 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1779 *dex_file_, type_idx);
1780
TDYa127a849cb62012-04-01 05:59:34 -07001781 llvm::Value* array_length_value;
1782
Logan Chiena2cc6a32012-01-16 10:38:41 +08001783 if (is_filled_new_array) {
1784 runtime_func = skip_access_check ?
1785 irb_.GetRuntime(CheckAndAllocArray) :
1786 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001787 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001788 } else {
1789 runtime_func = skip_access_check ?
1790 irb_.GetRuntime(AllocArray) :
1791 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001792 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001793 }
1794
1795 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1796
1797 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1798
TDYa127da83d972012-04-18 00:21:49 -07001799 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1800
TDYa127c8dc1012012-04-19 07:03:33 -07001801 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001802
Logan Chiena2cc6a32012-01-16 10:38:41 +08001803 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001804 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1805 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001806
1807 EmitGuard_ExceptionLandingPad(dex_pc);
1808
1809 return object_addr;
1810}
1811
1812
Logan Chien70f94b42011-12-27 17:49:11 +08001813void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1814 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001815
Elliott Hughesadb8c672012-03-06 16:49:32 -08001816 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001817
1818 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001820
Elliott Hughesadb8c672012-03-06 16:49:32 -08001821 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001822
Logan Chien70f94b42011-12-27 17:49:11 +08001823 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1824}
1825
1826
1827void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1828 Instruction const* insn,
1829 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830
Elliott Hughesadb8c672012-03-06 16:49:32 -08001831 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001832
1833 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001834 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001835
Elliott Hughesadb8c672012-03-06 16:49:32 -08001836 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001837 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001838 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001839 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1840 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001841 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842
Logan Chiendd361c92012-04-10 23:40:37 +08001843 uint32_t alignment;
1844 llvm::Constant* elem_size;
1845 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001846
Logan Chiendd361c92012-04-10 23:40:37 +08001847 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1848 // as the element, thus we are only checking 2 cases: primitive int and
1849 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001851 alignment = sizeof(int32_t);
1852 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001853 field_type = irb_.getJIntTy()->getPointerTo();
1854 } else {
1855 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001856 alignment = irb_.getSizeOfPtrEquivInt();
1857 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001858 field_type = irb_.getJObjectTy()->getPointerTo();
1859 }
1860
Logan Chiendd361c92012-04-10 23:40:37 +08001861 llvm::Value* data_field_offset =
1862 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1863
1864 llvm::Value* data_field_addr =
1865 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1866
Logan Chiena85fb2f2012-01-16 12:52:56 +08001867 // TODO: Tune this code. Currently we are generating one instruction for
1868 // one element which may be very space consuming. Maybe changing to use
1869 // memcpy may help; however, since we can't guarantee that the alloca of
1870 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001871 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001872 int reg_index;
1873 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001874 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001875 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001876 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001877 }
1878
1879 llvm::Value* reg_value;
1880 if (klass->IsPrimitiveInt()) {
1881 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1882 } else {
1883 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1884 }
1885
TDYa127706e7db2012-05-06 00:05:33 -07001886 irb_.CreateStore(reg_value, data_field_addr, kTBAAHeapArray);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001887
Logan Chiendd361c92012-04-10 23:40:37 +08001888 data_field_addr =
1889 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001890 }
1891 }
1892
1893 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1894
Logan Chien70f94b42011-12-27 17:49:11 +08001895 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1896}
1897
1898
1899void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1900 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001901
Elliott Hughesadb8c672012-03-06 16:49:32 -08001902 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001903
1904 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001905 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001906 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien19c350a2012-05-01 19:21:32 +08001908 const Instruction::ArrayDataPayload* payload =
1909 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1910 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001911
Logan Chien86f50672012-04-24 13:08:45 +08001912 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001913 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001914
Logan Chien86f50672012-04-24 13:08:45 +08001915 if (payload->element_count == 0) {
1916 // When the number of the elements in the payload is zero, we don't have
1917 // to copy any numbers. However, we should check whether the array object
1918 // address is equal to null or not.
1919 EmitGuard_NullPointerException(dex_pc, array_addr);
1920 } else {
1921 // To save the code size, we are going to call the runtime function to
1922 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001923
Logan Chien86f50672012-04-24 13:08:45 +08001924 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001925
Logan Chien86f50672012-04-24 13:08:45 +08001926 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001927
Logan Chien86f50672012-04-24 13:08:45 +08001928 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001929
Logan Chien86f50672012-04-24 13:08:45 +08001930 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001931
Logan Chien86f50672012-04-24 13:08:45 +08001932 irb_.CreateCall4(runtime_func,
1933 method_object_addr, irb_.getInt32(dex_pc),
1934 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001935
Logan Chien86f50672012-04-24 13:08:45 +08001936 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001937 }
1938
Logan Chien70f94b42011-12-27 17:49:11 +08001939 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1940}
1941
1942
1943void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1944 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001947
Elliott Hughesadb8c672012-03-06 16:49:32 -08001948 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001949
Logan Chiena466c162011-12-27 17:55:46 +08001950 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001951}
1952
1953
1954void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1955 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001956
Elliott Hughesadb8c672012-03-06 16:49:32 -08001957 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001958
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001960 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Logan Chien19c350a2012-05-01 19:21:32 +08001962 const Instruction::PackedSwitchPayload* payload =
1963 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1964 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001965
Elliott Hughesadb8c672012-03-06 16:49:32 -08001966 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001967
1968 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001969 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001970
Logan Chien19c350a2012-05-01 19:21:32 +08001971 for (uint16_t i = 0; i < payload->case_count; ++i) {
1972 sw->addCase(irb_.getInt32(payload->first_key + i),
1973 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001974 }
Logan Chien70f94b42011-12-27 17:49:11 +08001975}
1976
1977
1978void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1979 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
Elliott Hughesadb8c672012-03-06 16:49:32 -08001981 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001982
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001984 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001985
Logan Chien19c350a2012-05-01 19:21:32 +08001986 const Instruction::SparseSwitchPayload* payload =
1987 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1988 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001989
Logan Chien19c350a2012-05-01 19:21:32 +08001990 const int32_t* keys = payload->GetKeys();
1991 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001992
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001994
1995 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001996 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001997
Logan Chien19c350a2012-05-01 19:21:32 +08001998 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001999 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2000 }
Logan Chien70f94b42011-12-27 17:49:11 +08002001}
2002
2003
2004void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2005 Instruction const* insn,
2006 JType fp_jty,
2007 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002008
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002010
2011 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2012
Elliott Hughesadb8c672012-03-06 16:49:32 -08002013 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2014 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002015
2016 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2017 llvm::Value* cmp_lt;
2018
2019 if (gt_bias) {
2020 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2021 } else {
2022 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2023 }
2024
2025 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002027
Logan Chien70f94b42011-12-27 17:49:11 +08002028 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2029}
2030
2031
2032void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2033 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002034
Elliott Hughesadb8c672012-03-06 16:49:32 -08002035 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002036
Elliott Hughesadb8c672012-03-06 16:49:32 -08002037 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2038 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002039
2040 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2041 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2042
2043 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002044 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002045
Logan Chien70f94b42011-12-27 17:49:11 +08002046 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2047}
2048
2049
Logan Chien2c37e8e2011-12-27 17:58:46 +08002050llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2051 llvm::Value* cmp_lt) {
2052
2053 llvm::Constant* zero = irb_.getJInt(0);
2054 llvm::Constant* pos1 = irb_.getJInt(1);
2055 llvm::Constant* neg1 = irb_.getJInt(-1);
2056
2057 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2058 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2059
2060 return result_eq;
2061}
2062
2063
Logan Chien70f94b42011-12-27 17:49:11 +08002064void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2065 Instruction const* insn,
2066 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002067
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002069
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2071 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002072
2073 DCHECK_NE(kRegUnknown, src1_reg_cat);
2074 DCHECK_NE(kRegUnknown, src2_reg_cat);
2075 DCHECK_NE(kRegCat2, src1_reg_cat);
2076 DCHECK_NE(kRegCat2, src2_reg_cat);
2077
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002079
Logan Chiena78e3c82011-12-27 17:59:35 +08002080 llvm::Value* src1_value;
2081 llvm::Value* src2_value;
2082
TDYa1278e9b4492012-04-24 15:50:27 -07002083 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2084 src1_value = irb_.getInt32(0);
2085 src2_value = irb_.getInt32(0);
2086 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002087 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2088
2089 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002090 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2091 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002092 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002093 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2094 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002095 }
2096 } else {
2097 DCHECK(src1_reg_cat == kRegZero ||
2098 src2_reg_cat == kRegZero);
2099
2100 if (src1_reg_cat == kRegZero) {
2101 if (src2_reg_cat == kRegCat1nr) {
2102 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002103 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002104 } else {
2105 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002106 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002107 }
2108 } else { // src2_reg_cat == kRegZero
2109 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002110 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002111 src2_value = irb_.getJInt(0);
2112 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002113 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002114 src2_value = irb_.getJNull();
2115 }
2116 }
2117 }
2118
2119 llvm::Value* cond_value =
2120 EmitConditionResult(src1_value, src2_value, cond);
2121
2122 irb_.CreateCondBr(cond_value,
2123 GetBasicBlock(dex_pc + branch_offset),
2124 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002125}
2126
2127
2128void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2129 Instruction const* insn,
2130 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002131
Elliott Hughesadb8c672012-03-06 16:49:32 -08002132 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002133
Elliott Hughesadb8c672012-03-06 16:49:32 -08002134 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002135
2136 DCHECK_NE(kRegUnknown, src_reg_cat);
2137 DCHECK_NE(kRegCat2, src_reg_cat);
2138
Elliott Hughesadb8c672012-03-06 16:49:32 -08002139 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002140
Logan Chiena78e3c82011-12-27 17:59:35 +08002141 llvm::Value* src1_value;
2142 llvm::Value* src2_value;
2143
TDYa1278e9b4492012-04-24 15:50:27 -07002144 if (src_reg_cat == kRegZero) {
2145 src1_value = irb_.getInt32(0);
2146 src2_value = irb_.getInt32(0);
2147 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002148 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002149 src2_value = irb_.getInt32(0);
2150 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002151 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002152 src2_value = irb_.getJNull();
2153 }
2154
2155 llvm::Value* cond_value =
2156 EmitConditionResult(src1_value, src2_value, cond);
2157
2158 irb_.CreateCondBr(cond_value,
2159 GetBasicBlock(dex_pc + branch_offset),
2160 GetNextBasicBlock(dex_pc));
2161}
2162
TDYa1271d7e5102012-05-13 09:27:05 -07002163InferredRegCategoryMap const* MethodCompiler::GetInferredRegCategoryMap() {
Logan Chiendd361c92012-04-10 23:40:37 +08002164 Compiler::MethodReference mref(dex_file_, method_idx_);
2165
2166 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002167 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002168
Logan Chiena78e3c82011-12-27 17:59:35 +08002169 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2170
TDYa1271d7e5102012-05-13 09:27:05 -07002171 return map;
2172}
2173
2174RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2175 uint16_t reg_idx) {
2176 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2177
Logan Chiena78e3c82011-12-27 17:59:35 +08002178 return map->GetRegCategory(dex_pc, reg_idx);
2179}
2180
TDYa1271d7e5102012-05-13 09:27:05 -07002181bool MethodCompiler::IsRegCanBeObject(uint16_t reg_idx) {
2182 InferredRegCategoryMap const* map = GetInferredRegCategoryMap();
2183
2184 return map->IsRegCanBeObject(reg_idx);
2185}
2186
Logan Chiena78e3c82011-12-27 17:59:35 +08002187
2188llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2189 llvm::Value* rhs,
2190 CondBranchKind cond) {
2191 switch (cond) {
2192 case kCondBranch_EQ:
2193 return irb_.CreateICmpEQ(lhs, rhs);
2194
2195 case kCondBranch_NE:
2196 return irb_.CreateICmpNE(lhs, rhs);
2197
2198 case kCondBranch_LT:
2199 return irb_.CreateICmpSLT(lhs, rhs);
2200
2201 case kCondBranch_GE:
2202 return irb_.CreateICmpSGE(lhs, rhs);
2203
2204 case kCondBranch_GT:
2205 return irb_.CreateICmpSGT(lhs, rhs);
2206
2207 case kCondBranch_LE:
2208 return irb_.CreateICmpSLE(lhs, rhs);
2209
2210 default: // Unreachable
2211 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2212 return NULL;
2213 }
Logan Chien70f94b42011-12-27 17:49:11 +08002214}
2215
TDYa12783bb6622012-04-17 02:20:34 -07002216void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2217 // Using runtime support, let the target can override by InlineAssembly.
2218 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2219
2220 irb_.CreateCall2(runtime_func, value, target_addr);
2221}
Logan Chien70f94b42011-12-27 17:49:11 +08002222
Logan Chiene27fdbb2012-01-02 23:27:26 +08002223void
2224MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2225 llvm::Value* array,
2226 llvm::Value* index) {
2227 llvm::Value* array_len = EmitLoadArrayLength(array);
2228
2229 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2230
2231 llvm::BasicBlock* block_exception =
2232 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2233
2234 llvm::BasicBlock* block_continue =
2235 CreateBasicBlockWithDexPC(dex_pc, "cont");
2236
TDYa127ac7b5bb2012-05-11 13:17:49 -07002237 irb_.CreateCondBr(cmp, block_exception, block_continue, kUnlikely);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002238
2239 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002240
TDYa127c8dc1012012-04-19 07:03:33 -07002241 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002242 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2243 EmitBranchExceptionLandingPad(dex_pc);
2244
2245 irb_.SetInsertPoint(block_continue);
2246}
2247
2248
2249void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2250 llvm::Value* array,
2251 llvm::Value* index) {
2252 EmitGuard_NullPointerException(dex_pc, array);
2253 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2254}
2255
2256
2257// Emit Array GetElementPtr
2258llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2259 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002260 JType elem_jty) {
2261
2262 int data_offset;
2263 if (elem_jty == kLong || elem_jty == kDouble ||
2264 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2265 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2266 } else {
2267 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2268 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002269
2270 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002271 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002272
TDYa1278db6ea32012-05-17 04:48:42 -07002273 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2274
Logan Chiene27fdbb2012-01-02 23:27:26 +08002275 llvm::Value* array_data_addr =
2276 irb_.CreatePtrDisp(array_addr, data_offset_value,
2277 elem_type->getPointerTo());
2278
2279 return irb_.CreateGEP(array_data_addr, index_value);
2280}
2281
2282
Logan Chien70f94b42011-12-27 17:49:11 +08002283void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2284 Instruction const* insn,
2285 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002286
Elliott Hughesadb8c672012-03-06 16:49:32 -08002287 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002288
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2290 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002291
2292 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2293
TDYa1278db6ea32012-05-17 04:48:42 -07002294 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002295
TDYa127706e7db2012-05-06 00:05:33 -07002296 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002297
Elliott Hughesadb8c672012-03-06 16:49:32 -08002298 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002299
Logan Chien70f94b42011-12-27 17:49:11 +08002300 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2301}
2302
2303
2304void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2305 Instruction const* insn,
2306 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002307
Elliott Hughesadb8c672012-03-06 16:49:32 -08002308 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002309
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2311 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002312
2313 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2314
TDYa1278db6ea32012-05-17 04:48:42 -07002315 llvm::Value* array_elem_addr = EmitArrayGEP(array_addr, index_value, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002316
Elliott Hughesadb8c672012-03-06 16:49:32 -08002317 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002318
TDYa12783bb6622012-04-17 02:20:34 -07002319 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002320 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2321
2322 irb_.CreateCall2(runtime_func, new_value, array_addr);
2323
2324 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002325
2326 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002327 }
2328
TDYa127706e7db2012-05-06 00:05:33 -07002329 irb_.CreateStore(new_value, array_elem_addr, kTBAAHeapArray, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002330
Logan Chien70f94b42011-12-27 17:49:11 +08002331 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2332}
2333
2334
2335void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2336 Instruction const* insn,
2337 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002338
Elliott Hughesadb8c672012-03-06 16:49:32 -08002339 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002340
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 uint32_t reg_idx = dec_insn.vB;
2342 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002343
Logan Chien48f1d2a2012-01-02 22:49:53 +08002344 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2345
TDYa127cc1b4c32012-05-15 07:31:37 -07002346 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2347 EmitGuard_NullPointerException(dex_pc, object_addr);
2348 }
Logan Chien48f1d2a2012-01-02 22:49:53 +08002349
2350 llvm::Value* field_value;
2351
Logan Chien933abf82012-04-11 12:24:31 +08002352 int field_offset;
2353 bool is_volatile;
2354 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2355 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002356
Logan Chien933abf82012-04-11 12:24:31 +08002357 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002358 llvm::Function* runtime_func;
2359
2360 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002361 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002362 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002363 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002364 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002365 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002366 }
2367
2368 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2369
2370 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2371
TDYa127c8dc1012012-04-19 07:03:33 -07002372 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002373
Logan Chien3b2b2e72012-03-06 16:11:45 +08002374 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2375 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002376
2377 EmitGuard_ExceptionLandingPad(dex_pc);
2378
2379 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002380 DCHECK_GE(field_offset, 0);
2381
Logan Chien48f1d2a2012-01-02 22:49:53 +08002382 llvm::PointerType* field_type =
2383 irb_.getJType(field_jty, kField)->getPointerTo();
2384
Logan Chien933abf82012-04-11 12:24:31 +08002385 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002386
2387 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002388 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002389
Logan Chien933abf82012-04-11 12:24:31 +08002390 // TODO: Check is_volatile. We need to generate atomic load instruction
2391 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002392 field_value = irb_.CreateLoad(field_addr, kTBAAHeapInstance, field_jty);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002393 }
2394
Elliott Hughesadb8c672012-03-06 16:49:32 -08002395 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002396
Logan Chien70f94b42011-12-27 17:49:11 +08002397 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2398}
2399
2400
2401void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2402 Instruction const* insn,
2403 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002404
Elliott Hughesadb8c672012-03-06 16:49:32 -08002405 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002406
Elliott Hughesadb8c672012-03-06 16:49:32 -08002407 uint32_t reg_idx = dec_insn.vB;
2408 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002409
Logan Chiendd6aa872012-01-03 16:06:32 +08002410 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2411
TDYa127cc1b4c32012-05-15 07:31:37 -07002412 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2413 EmitGuard_NullPointerException(dex_pc, object_addr);
2414 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002415
Elliott Hughesadb8c672012-03-06 16:49:32 -08002416 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002417
Logan Chien933abf82012-04-11 12:24:31 +08002418 int field_offset;
2419 bool is_volatile;
2420 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2421 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002422
Logan Chien933abf82012-04-11 12:24:31 +08002423 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002424 llvm::Function* runtime_func;
2425
2426 if (field_jty == kObject) {
2427 runtime_func = irb_.GetRuntime(SetObjectInstance);
2428 } else if (field_jty == kLong || field_jty == kDouble) {
2429 runtime_func = irb_.GetRuntime(Set64Instance);
2430 } else {
2431 runtime_func = irb_.GetRuntime(Set32Instance);
2432 }
2433
2434 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2435
2436 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2437
TDYa127c8dc1012012-04-19 07:03:33 -07002438 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002439
Logan Chien3b2b2e72012-03-06 16:11:45 +08002440 irb_.CreateCall4(runtime_func, field_idx_value,
2441 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002442
2443 EmitGuard_ExceptionLandingPad(dex_pc);
2444
2445 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002446 DCHECK_GE(field_offset, 0);
2447
Logan Chiendd6aa872012-01-03 16:06:32 +08002448 llvm::PointerType* field_type =
2449 irb_.getJType(field_jty, kField)->getPointerTo();
2450
Logan Chien933abf82012-04-11 12:24:31 +08002451 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002452
2453 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002454 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002455
Logan Chien933abf82012-04-11 12:24:31 +08002456 // TODO: Check is_volatile. We need to generate atomic store instruction
2457 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002458 irb_.CreateStore(new_value, field_addr, kTBAAHeapInstance, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002459
2460 if (field_jty == kObject) { // If put an object, mark the GC card table.
2461 EmitMarkGCCard(new_value, object_addr);
2462 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002463 }
2464
Logan Chien70f94b42011-12-27 17:49:11 +08002465 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2466}
2467
2468
Logan Chien438c4b62012-01-17 16:06:00 +08002469llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2470 uint32_t type_idx) {
2471 llvm::BasicBlock* block_load_static =
2472 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2473
2474 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2475
2476 // Load static storage from dex cache
2477 llvm::Value* storage_field_addr =
2478 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2479
TDYa1278ca10052012-05-05 19:57:06 -07002480 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr, kTBAAJRuntime);
Logan Chien438c4b62012-01-17 16:06:00 +08002481
2482 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2483
2484 // Test: Is the static storage of this class initialized?
2485 llvm::Value* equal_null =
2486 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2487
TDYa127ac7b5bb2012-05-11 13:17:49 -07002488 irb_.CreateCondBr(equal_null, block_load_static, block_cont, kUnlikely);
Logan Chien438c4b62012-01-17 16:06:00 +08002489
2490 // Failback routine to load the class object
2491 irb_.SetInsertPoint(block_load_static);
2492
2493 llvm::Function* runtime_func =
2494 irb_.GetRuntime(InitializeStaticStorage);
2495
2496 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2497
2498 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2499
TDYa127706e9b62012-04-19 12:24:26 -07002500 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2501
TDYa127c8dc1012012-04-19 07:03:33 -07002502 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002503
Logan Chien438c4b62012-01-17 16:06:00 +08002504 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002505 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002506
2507 EmitGuard_ExceptionLandingPad(dex_pc);
2508
2509 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2510
2511 irb_.CreateBr(block_cont);
2512
2513 // Now the class object must be loaded
2514 irb_.SetInsertPoint(block_cont);
2515
2516 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2517
2518 phi->addIncoming(storage_object_addr, block_original);
2519 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2520
2521 return phi;
2522}
2523
2524
Logan Chien70f94b42011-12-27 17:49:11 +08002525void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2526 Instruction const* insn,
2527 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002528
Elliott Hughesadb8c672012-03-06 16:49:32 -08002529 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002530
Logan Chien933abf82012-04-11 12:24:31 +08002531 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002532
Logan Chien933abf82012-04-11 12:24:31 +08002533 int field_offset;
2534 int ssb_index;
2535 bool is_referrers_class;
2536 bool is_volatile;
2537
2538 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2539 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2540 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002541
2542 llvm::Value* static_field_value;
2543
Logan Chien933abf82012-04-11 12:24:31 +08002544 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002545 llvm::Function* runtime_func;
2546
2547 if (field_jty == kObject) {
2548 runtime_func = irb_.GetRuntime(GetObjectStatic);
2549 } else if (field_jty == kLong || field_jty == kDouble) {
2550 runtime_func = irb_.GetRuntime(Get64Static);
2551 } else {
2552 runtime_func = irb_.GetRuntime(Get32Static);
2553 }
2554
Elliott Hughesadb8c672012-03-06 16:49:32 -08002555 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002556
2557 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2558
TDYa127c8dc1012012-04-19 07:03:33 -07002559 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002560
Logan Chien438c4b62012-01-17 16:06:00 +08002561 static_field_value =
2562 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2563
2564 EmitGuard_ExceptionLandingPad(dex_pc);
2565
2566 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002567 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002568
Logan Chien933abf82012-04-11 12:24:31 +08002569 llvm::Value* static_storage_addr = NULL;
2570
2571 if (is_referrers_class) {
2572 // Fast path, static storage base is this method's class
2573 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2574
TDYa127ee1f59b2012-04-25 00:56:40 -07002575 static_storage_addr =
2576 irb_.LoadFromObjectOffset(method_object_addr,
2577 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002578 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002579 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002580 } else {
2581 // Medium path, static storage base in a different class which
2582 // requires checks that the other class is initialized
2583 DCHECK_GE(ssb_index, 0);
2584 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2585 }
2586
2587 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002588
2589 llvm::Value* static_field_addr =
2590 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2591 irb_.getJType(field_jty, kField)->getPointerTo());
2592
Logan Chien933abf82012-04-11 12:24:31 +08002593 // TODO: Check is_volatile. We need to generate atomic load instruction
2594 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002595 static_field_value = irb_.CreateLoad(static_field_addr, kTBAAHeapStatic, field_jty);
Logan Chien438c4b62012-01-17 16:06:00 +08002596 }
2597
Elliott Hughesadb8c672012-03-06 16:49:32 -08002598 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002599
Logan Chien70f94b42011-12-27 17:49:11 +08002600 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2601}
2602
2603
2604void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2605 Instruction const* insn,
2606 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002607
Elliott Hughesadb8c672012-03-06 16:49:32 -08002608 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002609
Logan Chien933abf82012-04-11 12:24:31 +08002610 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002611
Elliott Hughesadb8c672012-03-06 16:49:32 -08002612 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002613
Logan Chien933abf82012-04-11 12:24:31 +08002614 int field_offset;
2615 int ssb_index;
2616 bool is_referrers_class;
2617 bool is_volatile;
2618
2619 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2620 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2621 is_referrers_class, is_volatile, true);
2622
2623 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002624 llvm::Function* runtime_func;
2625
2626 if (field_jty == kObject) {
2627 runtime_func = irb_.GetRuntime(SetObjectStatic);
2628 } else if (field_jty == kLong || field_jty == kDouble) {
2629 runtime_func = irb_.GetRuntime(Set64Static);
2630 } else {
2631 runtime_func = irb_.GetRuntime(Set32Static);
2632 }
2633
Elliott Hughesadb8c672012-03-06 16:49:32 -08002634 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002635
2636 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2637
TDYa127c8dc1012012-04-19 07:03:33 -07002638 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002639
Logan Chien14179c82012-01-17 17:06:34 +08002640 irb_.CreateCall3(runtime_func, field_idx_value,
2641 method_object_addr, new_value);
2642
2643 EmitGuard_ExceptionLandingPad(dex_pc);
2644
2645 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002646 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002647
Logan Chien933abf82012-04-11 12:24:31 +08002648 llvm::Value* static_storage_addr = NULL;
2649
2650 if (is_referrers_class) {
2651 // Fast path, static storage base is this method's class
2652 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2653
TDYa127ee1f59b2012-04-25 00:56:40 -07002654 static_storage_addr =
2655 irb_.LoadFromObjectOffset(method_object_addr,
2656 Method::DeclaringClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002657 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002658 kTBAAConstJObject);
Logan Chien933abf82012-04-11 12:24:31 +08002659 } else {
2660 // Medium path, static storage base in a different class which
2661 // requires checks that the other class is initialized
2662 DCHECK_GE(ssb_index, 0);
2663 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2664 }
2665
2666 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002667
2668 llvm::Value* static_field_addr =
2669 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2670 irb_.getJType(field_jty, kField)->getPointerTo());
2671
Logan Chien933abf82012-04-11 12:24:31 +08002672 // TODO: Check is_volatile. We need to generate atomic store instruction
2673 // when is_volatile is true.
TDYa127706e7db2012-05-06 00:05:33 -07002674 irb_.CreateStore(new_value, static_field_addr, kTBAAHeapStatic, field_jty);
TDYa12783bb6622012-04-17 02:20:34 -07002675
2676 if (field_jty == kObject) { // If put an object, mark the GC card table.
2677 EmitMarkGCCard(new_value, static_storage_addr);
2678 }
Logan Chien14179c82012-01-17 17:06:34 +08002679 }
2680
Logan Chien70f94b42011-12-27 17:49:11 +08002681 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2682}
2683
2684
Logan Chien1a121b92012-02-15 22:23:42 +08002685void MethodCompiler::
2686EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2687 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002689 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002690 bool is_static) {
2691
2692 // Get method signature
2693 DexFile::MethodId const& method_id =
2694 dex_file_->GetMethodId(callee_method_idx);
2695
Logan Chien8faf8022012-02-24 12:25:29 +08002696 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002697 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002698 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002699
2700 // Load argument values according to the shorty (without "this")
2701 uint16_t reg_count = 0;
2702
2703 if (!is_static) {
2704 ++reg_count; // skip the "this" pointer
2705 }
2706
Logan Chien7e7fabc2012-04-10 18:59:11 +08002707 bool is_range = (arg_fmt == kArgRange);
2708
Logan Chien8faf8022012-02-24 12:25:29 +08002709 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002710 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2711 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002712
2713 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2714
2715 ++reg_count;
2716 if (shorty[i] == 'J' || shorty[i] == 'D') {
2717 // Wide types, such as long and double, are using a pair of registers
2718 // to store the value, so we have to increase arg_reg again.
2719 ++reg_count;
2720 }
2721 }
2722
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002724 << "Actual argument mismatch for callee: "
2725 << PrettyMethod(callee_method_idx, *dex_file_);
2726}
2727
TDYa1270b686e52012-04-09 22:43:35 -07002728llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2729 uint32_t method_idx,
2730 bool is_static) {
2731 // TODO: Remove this after we solve the link and trampoline related problems.
2732 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2733
2734 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2735
2736 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002737}
Logan Chien1a121b92012-02-15 22:23:42 +08002738
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002739
Logan Chien7e7fabc2012-04-10 18:59:11 +08002740void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2741 Instruction const* insn,
2742 InvokeType invoke_type,
2743 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002744 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002745
Logan Chien61c65dc2012-02-29 03:22:30 +08002746 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002747 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002748
Logan Chien7e7fabc2012-04-10 18:59:11 +08002749 // Compute invoke related information for compiler decision
2750 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002751 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002752 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002753 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002754 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002755 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002756
Logan Chien7e7fabc2012-04-10 18:59:11 +08002757 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002758 llvm::Value* this_addr = NULL;
2759
2760 if (!is_static) {
2761 // Test: Is *this* parameter equal to null?
TDYa127cc1b4c32012-05-15 07:31:37 -07002762 uint32_t reg_idx = (arg_fmt == kArgReg) ? dec_insn.arg[0] : (dec_insn.vC + 0);
2763 this_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002764
TDYa127cc1b4c32012-05-15 07:31:37 -07002765 if (!(method_info_.this_will_not_be_null && reg_idx == method_info_.this_reg_idx)) {
2766 EmitGuard_NullPointerException(dex_pc, this_addr);
2767 }
Logan Chien1a121b92012-02-15 22:23:42 +08002768 }
2769
Logan Chien7e7fabc2012-04-10 18:59:11 +08002770 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002771 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002772
2773 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002774 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002775 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2776 this_addr, dex_pc, is_fast_path);
2777 } else {
2778 switch (invoke_type) {
2779 case kStatic:
2780 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002781 if (direct_method != 0u &&
2782 direct_method != static_cast<uintptr_t>(-1)) {
2783 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002784 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2785 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002786 } else {
2787 callee_method_object_addr =
2788 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2789 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002790 break;
2791
2792 case kVirtual:
2793 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002794 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002795 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2796 break;
2797
2798 case kSuper:
2799 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2800 "the fast path.";
2801 break;
2802
2803 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002804 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002805 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2806 invoke_type, this_addr,
2807 dex_pc, is_fast_path);
2808 break;
2809 }
2810 }
2811
Logan Chien7e7fabc2012-04-10 18:59:11 +08002812 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002813 irb_.LoadFromObjectOffset(callee_method_object_addr,
2814 Method::GetCodeOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002815 GetFunctionType(callee_method_idx, is_static)->getPointerTo(),
TDYa1278ca10052012-05-05 19:57:06 -07002816 kTBAAJRuntime);
TDYa12785321912012-04-01 15:24:56 -07002817
Logan Chien1a121b92012-02-15 22:23:42 +08002818 // Load the actual parameter
2819 std::vector<llvm::Value*> args;
2820
2821 args.push_back(callee_method_object_addr); // method object for callee
2822
2823 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002824 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002825 args.push_back(this_addr); // "this" object for callee
2826 }
2827
2828 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002829 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002830
TDYa1275bb86012012-04-11 05:57:28 -07002831#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002832 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002833 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002834 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2835 EmitGuard_ExceptionLandingPad(dex_pc);
2836
Logan Chien7e7fabc2012-04-10 18:59:11 +08002837 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2838 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2839 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2840
2841 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002842 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002843 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2844 }
TDYa1275bb86012012-04-11 05:57:28 -07002845#else
2846 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2847 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2848 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2849
2850 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2851
2852
TDYa127c8dc1012012-04-19 07:03:33 -07002853 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002854
2855
2856 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002857 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002858 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2859
TDYa1275bb86012012-04-11 05:57:28 -07002860 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002861 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2862 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
TDYa127ac7b5bb2012-05-11 13:17:49 -07002863 irb_.CreateCondBr(is_stub, block_stub, block_normal, kUnlikely);
TDYa1275bb86012012-04-11 05:57:28 -07002864
2865
2866 irb_.SetInsertPoint(block_normal);
2867 {
2868 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002869 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002870 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002871 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002872 }
2873 }
2874 irb_.CreateBr(block_continue);
2875
2876
TDYa127ce154722012-04-21 16:43:29 -07002877 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002878 {
TDYa127ce154722012-04-21 16:43:29 -07002879 { // lazy link
2880 // TODO: Remove this after we solve the link problem.
2881 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2882 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2883
TDYa127ac7b5bb2012-05-11 13:17:49 -07002884 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub, kUnlikely);
TDYa127ce154722012-04-21 16:43:29 -07002885
2886
2887 irb_.SetInsertPoint(block_link);
2888 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2889
2890 EmitGuard_ExceptionLandingPad(dex_pc);
2891
2892 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2893 if (ret_shorty != 'V') {
2894 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2895 }
2896 irb_.CreateBr(block_continue);
2897
2898
2899 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002900 }
TDYa127ce154722012-04-21 16:43:29 -07002901 { // proxy stub
2902 llvm::Value* temp_space_addr;
2903 if (ret_shorty != 'V') {
2904 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2905 args.push_back(temp_space_addr);
2906 }
2907 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2908 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2909 if (ret_shorty != 'V') {
Shih-wei Liaoe6a7adc2012-05-09 02:50:08 -07002910 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
TDYa127ce154722012-04-21 16:43:29 -07002911 llvm::Value* result_addr =
2912 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
TDYa127aba61122012-05-04 18:28:36 -07002913 llvm::Value* retval = irb_.CreateLoad(result_addr, kTBAAStackTemp);
TDYa127ce154722012-04-21 16:43:29 -07002914 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2915 }
TDYa1275bb86012012-04-11 05:57:28 -07002916 }
2917 }
2918 irb_.CreateBr(block_continue);
2919
2920
2921 irb_.SetInsertPoint(block_continue);
2922
TDYa1275bb86012012-04-11 05:57:28 -07002923 EmitGuard_ExceptionLandingPad(dex_pc);
2924#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002925
Logan Chien70f94b42011-12-27 17:49:11 +08002926 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2927}
2928
2929
Logan Chien7e7fabc2012-04-10 18:59:11 +08002930llvm::Value* MethodCompiler::
2931EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2932 llvm::Value* callee_method_object_field_addr =
2933 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002934
TDYa1278ca10052012-05-05 19:57:06 -07002935 return irb_.CreateLoad(callee_method_object_field_addr, kTBAAJRuntime);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002936}
Logan Chien7caf37e2012-02-03 22:56:04 +08002937
Logan Chien7caf37e2012-02-03 22:56:04 +08002938
Logan Chien7e7fabc2012-04-10 18:59:11 +08002939llvm::Value* MethodCompiler::
2940EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2941 llvm::Value* this_addr) {
2942 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002943 llvm::Value* class_object_addr =
2944 irb_.LoadFromObjectOffset(this_addr,
2945 Object::ClassOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002946 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002947 kTBAAConstJObject);
Logan Chien7caf37e2012-02-03 22:56:04 +08002948
Logan Chien7e7fabc2012-04-10 18:59:11 +08002949 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002950 llvm::Value* vtable_addr =
2951 irb_.LoadFromObjectOffset(class_object_addr,
2952 Class::VTableOffset().Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07002953 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07002954 kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002955
2956 // Load callee method object
2957 llvm::Value* vtable_idx_value =
2958 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2959
2960 llvm::Value* method_field_addr =
TDYa1278db6ea32012-05-17 04:48:42 -07002961 EmitArrayGEP(vtable_addr, vtable_idx_value, kObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002962
TDYa127d3e24c22012-05-05 20:54:19 -07002963 return irb_.CreateLoad(method_field_addr, kTBAAConstJObject);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002964}
2965
2966
2967llvm::Value* MethodCompiler::
2968EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2969 InvokeType invoke_type,
2970 llvm::Value* this_addr,
2971 uint32_t dex_pc,
2972 bool is_fast_path) {
2973
2974 llvm::Function* runtime_func = NULL;
2975
2976 switch (invoke_type) {
2977 case kStatic:
2978 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2979 break;
2980
2981 case kDirect:
2982 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2983 break;
2984
2985 case kVirtual:
2986 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2987 break;
2988
2989 case kSuper:
2990 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2991 break;
2992
2993 case kInterface:
2994 if (is_fast_path) {
2995 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2996 } else {
2997 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
2998 }
2999 break;
3000 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003001
3002 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3003
Logan Chien7e7fabc2012-04-10 18:59:11 +08003004 if (this_addr == NULL) {
3005 DCHECK_EQ(invoke_type, kStatic);
3006 this_addr = irb_.getJNull();
3007 }
3008
3009 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3010
TDYa127da83d972012-04-18 00:21:49 -07003011 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3012
TDYa127c8dc1012012-04-19 07:03:33 -07003013 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003014
TDYa1270b686e52012-04-09 22:43:35 -07003015 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003016 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003017 callee_method_idx_value,
3018 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003019 caller_method_object_addr,
3020 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003021
3022 EmitGuard_ExceptionLandingPad(dex_pc);
3023
Logan Chien7e7fabc2012-04-10 18:59:11 +08003024 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003025}
3026
3027
3028void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3029 Instruction const* insn,
3030 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003031
Elliott Hughesadb8c672012-03-06 16:49:32 -08003032 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003033
3034 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3035
Elliott Hughesadb8c672012-03-06 16:49:32 -08003036 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003037 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003038 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003039
Logan Chien70f94b42011-12-27 17:49:11 +08003040 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3041}
3042
3043
3044void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3045 Instruction const* insn,
3046 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003047
Elliott Hughesadb8c672012-03-06 16:49:32 -08003048 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003049
3050 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3051
Elliott Hughesadb8c672012-03-06 16:49:32 -08003052 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003053 llvm::Value* result_value =
3054 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3055
Elliott Hughesadb8c672012-03-06 16:49:32 -08003056 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003057
Logan Chien70f94b42011-12-27 17:49:11 +08003058 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3059}
3060
3061
3062void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3063 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003064
Elliott Hughesadb8c672012-03-06 16:49:32 -08003065 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003066
Elliott Hughesadb8c672012-03-06 16:49:32 -08003067 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003068 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003069 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003070
Logan Chien70f94b42011-12-27 17:49:11 +08003071 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3072}
3073
3074
3075void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3076 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003077
Elliott Hughesadb8c672012-03-06 16:49:32 -08003078 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003079
Elliott Hughesadb8c672012-03-06 16:49:32 -08003080 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003081 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003082 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003083
Logan Chien70f94b42011-12-27 17:49:11 +08003084 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3085}
3086
3087
3088void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3089 Instruction const* insn,
3090 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003091
Elliott Hughesadb8c672012-03-06 16:49:32 -08003092 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003093
Elliott Hughesadb8c672012-03-06 16:49:32 -08003094 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003095
3096 llvm::Value* trunc_value =
3097 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3098
3099 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3100
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003102
Logan Chien70f94b42011-12-27 17:49:11 +08003103 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3104}
3105
3106
3107void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3108 Instruction const* insn,
3109 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003110
Elliott Hughesadb8c672012-03-06 16:49:32 -08003111 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003112
Elliott Hughesadb8c672012-03-06 16:49:32 -08003113 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003114
3115 llvm::Value* trunc_value =
3116 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3117
3118 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3119
Elliott Hughesadb8c672012-03-06 16:49:32 -08003120 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003121
Logan Chien70f94b42011-12-27 17:49:11 +08003122 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3123}
3124
3125
3126void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3127 Instruction const* insn,
3128 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003129
Elliott Hughesadb8c672012-03-06 16:49:32 -08003130 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003131
3132 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3133
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003135 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003136 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003137
Logan Chien70f94b42011-12-27 17:49:11 +08003138 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3139}
3140
3141
3142void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3143 Instruction const* insn,
3144 JType src_jty,
3145 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003146
Elliott Hughesadb8c672012-03-06 16:49:32 -08003147 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003148
3149 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3150 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3151
Elliott Hughesadb8c672012-03-06 16:49:32 -08003152 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003153 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3154 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003155 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003156
Logan Chien70f94b42011-12-27 17:49:11 +08003157 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3158}
3159
3160
3161void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3162 Instruction const* insn,
3163 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003164 JType dest_jty,
3165 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003166
Elliott Hughesadb8c672012-03-06 16:49:32 -08003167 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003168
3169 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3170 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3171
Elliott Hughesadb8c672012-03-06 16:49:32 -08003172 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003173 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003174 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003175
Logan Chien70f94b42011-12-27 17:49:11 +08003176 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3177}
3178
3179
3180void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3181 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003182
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003184
Elliott Hughesadb8c672012-03-06 16:49:32 -08003185 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003186 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003187 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003188
Logan Chien70f94b42011-12-27 17:49:11 +08003189 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3190}
3191
3192
3193void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3194 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003195
Elliott Hughesadb8c672012-03-06 16:49:32 -08003196 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003197
Elliott Hughesadb8c672012-03-06 16:49:32 -08003198 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003199 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003200 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003201
Logan Chien70f94b42011-12-27 17:49:11 +08003202 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3203}
3204
3205
3206void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3207 Instruction const* insn,
3208 IntArithmKind arithm,
3209 JType op_jty,
3210 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003211
Elliott Hughesadb8c672012-03-06 16:49:32 -08003212 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003213
3214 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3215
3216 llvm::Value* src1_value;
3217 llvm::Value* src2_value;
3218
3219 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003220 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3221 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003222 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003223 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3224 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003225 }
3226
3227 llvm::Value* result_value =
3228 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3229 arithm, op_jty);
3230
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003232
Logan Chien70f94b42011-12-27 17:49:11 +08003233 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3234}
3235
3236
3237void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3238 Instruction const* insn,
3239 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003240
Elliott Hughesadb8c672012-03-06 16:49:32 -08003241 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003242
Elliott Hughesadb8c672012-03-06 16:49:32 -08003243 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003244
Elliott Hughesadb8c672012-03-06 16:49:32 -08003245 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003246
3247 llvm::Value* result_value =
3248 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3249
Elliott Hughesadb8c672012-03-06 16:49:32 -08003250 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003251
Logan Chien70f94b42011-12-27 17:49:11 +08003252 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3253}
3254
3255
Logan Chienc3f7d962011-12-27 18:13:18 +08003256llvm::Value*
3257MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3258 llvm::Value* lhs,
3259 llvm::Value* rhs,
3260 IntArithmKind arithm,
3261 JType op_jty) {
3262 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3263
3264 switch (arithm) {
3265 case kIntArithm_Add:
3266 return irb_.CreateAdd(lhs, rhs);
3267
3268 case kIntArithm_Sub:
3269 return irb_.CreateSub(lhs, rhs);
3270
3271 case kIntArithm_Mul:
3272 return irb_.CreateMul(lhs, rhs);
3273
3274 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003275 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003276 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003277
3278 case kIntArithm_And:
3279 return irb_.CreateAnd(lhs, rhs);
3280
3281 case kIntArithm_Or:
3282 return irb_.CreateOr(lhs, rhs);
3283
3284 case kIntArithm_Xor:
3285 return irb_.CreateXor(lhs, rhs);
3286
Logan Chienc3f7d962011-12-27 18:13:18 +08003287 default:
3288 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3289 return NULL;
3290 }
3291}
3292
3293
TDYa127f8641ce2012-04-02 06:40:40 -07003294llvm::Value*
3295MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3296 llvm::Value* dividend,
3297 llvm::Value* divisor,
3298 IntArithmKind arithm,
3299 JType op_jty) {
3300 // Throw exception if the divisor is 0.
3301 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3302
3303 // Check the special case: MININT / -1 = MININT
3304 // That case will cause overflow, which is undefined behavior in llvm.
3305 // So we check the divisor is -1 or not, if the divisor is -1, we do
3306 // the special path to avoid undefined behavior.
3307 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3308 llvm::Value* zero = irb_.getJZero(op_jty);
3309 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3310 llvm::Value* result = irb_.CreateAlloca(op_type);
3311
3312 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3313 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3314 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3315
3316 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
TDYa127ac7b5bb2012-05-11 13:17:49 -07003317 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one, kUnlikely);
TDYa127f8641ce2012-04-02 06:40:40 -07003318
3319 // If divisor == -1
3320 irb_.SetInsertPoint(eq_neg_one);
3321 llvm::Value* eq_result;
3322 if (arithm == kIntArithm_Div) {
3323 // We can just change from "dividend div -1" to "neg dividend".
3324 // The sub don't care the sign/unsigned because of two's complement representation.
3325 // And the behavior is what we want:
3326 // -(2^n) (2^n)-1
3327 // MININT < k <= MAXINT -> mul k -1 = -k
3328 // MININT == k -> mul k -1 = k
3329 //
3330 // LLVM use sub to represent 'neg'
3331 eq_result = irb_.CreateSub(zero, dividend);
3332 } else {
3333 // Everything modulo -1 will be 0.
3334 eq_result = zero;
3335 }
TDYa127aba61122012-05-04 18:28:36 -07003336 irb_.CreateStore(eq_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003337 irb_.CreateBr(neg_one_cont);
3338
3339 // If divisor != -1, just do the division.
3340 irb_.SetInsertPoint(ne_neg_one);
3341 llvm::Value* ne_result;
3342 if (arithm == kIntArithm_Div) {
3343 ne_result = irb_.CreateSDiv(dividend, divisor);
3344 } else {
3345 ne_result = irb_.CreateSRem(dividend, divisor);
3346 }
TDYa127aba61122012-05-04 18:28:36 -07003347 irb_.CreateStore(ne_result, result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003348 irb_.CreateBr(neg_one_cont);
3349
3350 irb_.SetInsertPoint(neg_one_cont);
TDYa127aba61122012-05-04 18:28:36 -07003351 return irb_.CreateLoad(result, kTBAAStackTemp);
TDYa127f8641ce2012-04-02 06:40:40 -07003352}
3353
3354
Logan Chien5539ad02012-04-02 14:36:55 +08003355void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3356 Instruction const* insn,
3357 IntShiftArithmKind arithm,
3358 JType op_jty,
3359 bool is_2addr) {
3360
3361 DecodedInstruction dec_insn(insn);
3362
3363 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3364
3365 llvm::Value* src1_value;
3366 llvm::Value* src2_value;
3367
3368 // NOTE: The 2nd operand of the shift arithmetic instruction is
3369 // 32-bit integer regardless of the 1st operand.
3370 if (is_2addr) {
3371 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3372 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3373 } else {
3374 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3375 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3376 }
3377
3378 llvm::Value* result_value =
3379 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3380 arithm, op_jty);
3381
3382 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3383
3384 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3385}
3386
3387
3388void MethodCompiler::
3389EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3390 Instruction const* insn,
3391 IntShiftArithmKind arithm) {
3392
3393 DecodedInstruction dec_insn(insn);
3394
3395 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3396
3397 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3398
3399 llvm::Value* result_value =
3400 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3401 arithm, kInt);
3402
3403 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3404
3405 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3406}
3407
3408
3409llvm::Value*
3410MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3411 llvm::Value* lhs,
3412 llvm::Value* rhs,
3413 IntShiftArithmKind arithm,
3414 JType op_jty) {
3415 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3416
3417 if (op_jty == kInt) {
3418 rhs = irb_.CreateAnd(rhs, 0x1f);
3419 } else {
3420 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3421 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3422 }
3423
3424 switch (arithm) {
3425 case kIntArithm_Shl:
3426 return irb_.CreateShl(lhs, rhs);
3427
3428 case kIntArithm_Shr:
3429 return irb_.CreateAShr(lhs, rhs);
3430
3431 case kIntArithm_UShr:
3432 return irb_.CreateLShr(lhs, rhs);
3433
3434 default:
3435 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3436 return NULL;
3437 }
3438}
3439
3440
Logan Chien70f94b42011-12-27 17:49:11 +08003441void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3442 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003443
Elliott Hughesadb8c672012-03-06 16:49:32 -08003444 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003445
Elliott Hughesadb8c672012-03-06 16:49:32 -08003446 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3447 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003448 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003449 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003450
Logan Chien70f94b42011-12-27 17:49:11 +08003451 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3452}
3453
3454
3455void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3456 Instruction const* insn,
3457 FPArithmKind arithm,
3458 JType op_jty,
3459 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003460
Elliott Hughesadb8c672012-03-06 16:49:32 -08003461 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003462
3463 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3464
3465 llvm::Value* src1_value;
3466 llvm::Value* src2_value;
3467
3468 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003469 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3470 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003471 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003472 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3473 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003474 }
3475
3476 llvm::Value* result_value =
3477 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3478
Elliott Hughesadb8c672012-03-06 16:49:32 -08003479 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003480
Logan Chien70f94b42011-12-27 17:49:11 +08003481 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3482}
3483
3484
Logan Chien76e1c792011-12-27 18:15:01 +08003485llvm::Value*
3486MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3487 llvm::Value *lhs,
3488 llvm::Value *rhs,
3489 FPArithmKind arithm) {
3490 switch (arithm) {
3491 case kFPArithm_Add:
3492 return irb_.CreateFAdd(lhs, rhs);
3493
3494 case kFPArithm_Sub:
3495 return irb_.CreateFSub(lhs, rhs);
3496
3497 case kFPArithm_Mul:
3498 return irb_.CreateFMul(lhs, rhs);
3499
3500 case kFPArithm_Div:
3501 return irb_.CreateFDiv(lhs, rhs);
3502
3503 case kFPArithm_Rem:
3504 return irb_.CreateFRem(lhs, rhs);
3505
3506 default:
3507 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3508 return NULL;
3509 }
3510}
3511
3512
Logan Chienc3f7d962011-12-27 18:13:18 +08003513void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3514 llvm::Value* denominator,
3515 JType op_jty) {
3516 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3517
3518 llvm::Constant* zero = irb_.getJZero(op_jty);
3519
3520 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3521
3522 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3523
3524 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3525
TDYa127ac7b5bb2012-05-11 13:17:49 -07003526 irb_.CreateCondBr(equal_zero, block_exception, block_continue, kUnlikely);
Logan Chienc3f7d962011-12-27 18:13:18 +08003527
3528 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003529 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003530 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3531 EmitBranchExceptionLandingPad(dex_pc);
3532
3533 irb_.SetInsertPoint(block_continue);
3534}
3535
3536
Logan Chien61bb6142012-02-03 15:34:53 +08003537void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3538 llvm::Value* object) {
3539 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3540
3541 llvm::BasicBlock* block_exception =
3542 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3543
3544 llvm::BasicBlock* block_continue =
3545 CreateBasicBlockWithDexPC(dex_pc, "cont");
3546
TDYa127ac7b5bb2012-05-11 13:17:49 -07003547 irb_.CreateCondBr(equal_null, block_exception, block_continue, kUnlikely);
Logan Chien61bb6142012-02-03 15:34:53 +08003548
3549 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003550 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003551 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003552 EmitBranchExceptionLandingPad(dex_pc);
3553
3554 irb_.SetInsertPoint(block_continue);
3555}
3556
3557
Logan Chienbb4d12a2012-02-17 14:10:01 +08003558llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3559 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3560
TDYa127ee1f59b2012-04-25 00:56:40 -07003561 return irb_.LoadFromObjectOffset(method_object_addr,
3562 offset.Int32Value(),
TDYa127aba61122012-05-04 18:28:36 -07003563 irb_.getJObjectTy(),
TDYa127d3e24c22012-05-05 20:54:19 -07003564 kTBAAConstJObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003565}
3566
3567
Logan Chienbb4d12a2012-02-17 14:10:01 +08003568llvm::Value* MethodCompiler::
3569EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3570 llvm::Value* static_storage_dex_cache_addr =
3571 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3572
3573 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3574
TDYa1278db6ea32012-05-17 04:48:42 -07003575 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003576}
3577
3578
3579llvm::Value* MethodCompiler::
3580EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3581 llvm::Value* resolved_type_dex_cache_addr =
3582 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3583
3584 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3585
TDYa1278db6ea32012-05-17 04:48:42 -07003586 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003587}
3588
3589
3590llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003591EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3592 llvm::Value* resolved_method_dex_cache_addr =
3593 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3594
3595 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3596
TDYa1278db6ea32012-05-17 04:48:42 -07003597 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value, kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003598}
3599
3600
3601llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003602EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3603 llvm::Value* string_dex_cache_addr =
3604 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3605
3606 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3607
TDYa1278db6ea32012-05-17 04:48:42 -07003608 return EmitArrayGEP(string_dex_cache_addr, string_idx_value, kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003609}
3610
3611
Logan Chien83426162011-12-09 09:29:50 +08003612CompiledMethod *MethodCompiler::Compile() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003613 // TODO: Use high-level IR to do this
3614 // Compute method info
3615 ComputeMethodInfo();
3616
Logan Chien0b827102011-12-20 19:46:14 +08003617 // Code generation
3618 CreateFunction();
3619
3620 EmitPrologue();
3621 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003622 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003623
Logan Chiend6c239a2011-12-23 15:11:45 +08003624 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003625 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003626
Logan Chien8b977d32012-02-21 19:14:55 +08003627 // Add the memory usage approximation of the compilation unit
3628 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
TDYa127cc1b4c32012-05-15 07:31:37 -07003629 // NOTE: From statistics, the bitcode size is 4.5 times bigger than the
Logan Chien8b977d32012-02-21 19:14:55 +08003630 // Dex file. Besides, we have to convert the code unit into bytes.
3631 // Thus, we got our magic number 9.
3632
Logan Chien110bcba2012-04-16 19:11:28 +08003633 CompiledMethod* compiled_method =
3634 new CompiledMethod(cunit_->GetInstructionSet(),
3635 cunit_->GetElfIndex(),
3636 elf_func_idx_);
3637
3638 cunit_->RegisterCompiledMethod(func_, compiled_method);
3639
3640 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003641}
3642
3643
3644llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3645 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003646}
Logan Chien83426162011-12-09 09:29:50 +08003647
3648
Logan Chien5bcc04e2012-01-30 14:15:12 +08003649void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3650 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3651 irb_.CreateBr(lpad);
3652 } else {
3653 irb_.CreateBr(GetUnwindBasicBlock());
3654 }
3655}
3656
3657
3658void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3659 llvm::Value* exception_pending =
3660 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3661
3662 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3663
3664 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003665 irb_.CreateCondBr(exception_pending, lpad, block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003666 } else {
TDYa127ac7b5bb2012-05-11 13:17:49 -07003667 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont, kUnlikely);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003668 }
3669
3670 irb_.SetInsertPoint(block_cont);
3671}
3672
3673
Logan Chien924072f2012-01-30 15:07:24 +08003674void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003675 if (!method_info_.need_shadow_frame_entry) {
3676 return;
3677 }
3678
Logan Chien924072f2012-01-30 15:07:24 +08003679 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003680
3681 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3682
TDYa127853cd092012-04-21 22:15:31 -07003683 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003684}
3685
3686
Logan Chiend6c239a2011-12-23 15:11:45 +08003687llvm::BasicBlock* MethodCompiler::
3688CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3689 std::string name;
3690
TDYa12767ae8ff2012-05-02 19:08:02 -07003691#if !defined(NDEBUG)
Logan Chiend6c239a2011-12-23 15:11:45 +08003692 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003693 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003694 } else {
TDYa12799489132012-04-29 01:27:58 -07003695 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003696 }
TDYa12767ae8ff2012-05-02 19:08:02 -07003697#endif
Logan Chiend6c239a2011-12-23 15:11:45 +08003698
3699 return llvm::BasicBlock::Create(*context_, name, func_);
3700}
3701
3702
3703llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3704 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3705
3706 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3707
3708 if (!basic_block) {
3709 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3710 basic_blocks_[dex_pc] = basic_block;
3711 }
3712
3713 return basic_block;
3714}
3715
3716
3717llvm::BasicBlock*
3718MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3719 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3720 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3721}
3722
3723
Logan Chien5bcc04e2012-01-30 14:15:12 +08003724int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3725 // TODO: Since we are emitting the dex instructions in ascending order
3726 // w.r.t. address, we can cache the lastest try item offset so that we
3727 // don't have to do binary search for every query.
3728
3729 int32_t min = 0;
3730 int32_t max = code_item_->tries_size_ - 1;
3731
3732 while (min <= max) {
3733 int32_t mid = min + (max - min) / 2;
3734
3735 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3736 uint32_t start = ti->start_addr_;
3737 uint32_t end = start + ti->insn_count_;
3738
3739 if (dex_pc < start) {
3740 max = mid - 1;
3741 } else if (dex_pc >= end) {
3742 min = mid + 1;
3743 } else {
3744 return mid; // found
3745 }
3746 }
3747
3748 return -1; // not found
3749}
3750
3751
3752llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3753 // Find the try item for this address in this method
3754 int32_t ti_offset = GetTryItemOffset(dex_pc);
3755
3756 if (ti_offset == -1) {
3757 return NULL; // No landing pad is available for this address.
3758 }
3759
3760 // Check for the existing landing pad basic block
3761 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3762 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3763
3764 if (block_lpad) {
3765 // We have generated landing pad for this try item already. Return the
3766 // same basic block.
3767 return block_lpad;
3768 }
3769
3770 // Get try item from code item
3771 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3772
TDYa12767ae8ff2012-05-02 19:08:02 -07003773 std::string lpadname;
3774
3775#if !defined(NDEBUG)
3776 StringAppendF(&lpadname, "lpad%d_%04x_to_%04x", ti_offset, ti->start_addr_, ti->handler_off_);
3777#endif
3778
Logan Chien5bcc04e2012-01-30 14:15:12 +08003779 // Create landing pad basic block
TDYa12767ae8ff2012-05-02 19:08:02 -07003780 block_lpad = llvm::BasicBlock::Create(*context_, lpadname, func_);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003781
3782 // Change IRBuilder insert point
3783 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3784 irb_.SetInsertPoint(block_lpad);
3785
3786 // Find catch block with matching type
3787 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3788
Logan Chien736df022012-04-27 16:25:57 +08003789 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003790
3791 llvm::Value* catch_handler_index_value =
3792 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003793 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003794
3795 // Switch instruction (Go to unwind basic block by default)
3796 llvm::SwitchInst* sw =
3797 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3798
3799 // Cases with matched catch block
3800 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3801
3802 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3803 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3804 }
3805
3806 // Restore the orignal insert point for IRBuilder
3807 irb_.restoreIP(irb_ip_original);
3808
3809 // Cache this landing pad
3810 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3811 basic_block_landing_pads_[ti_offset] = block_lpad;
3812
3813 return block_lpad;
3814}
3815
3816
3817llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3818 // Check the existing unwinding baisc block block
3819 if (basic_block_unwind_ != NULL) {
3820 return basic_block_unwind_;
3821 }
3822
3823 // Create new basic block for unwinding
3824 basic_block_unwind_ =
3825 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3826
3827 // Change IRBuilder insert point
3828 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3829 irb_.SetInsertPoint(basic_block_unwind_);
3830
Logan Chien8dfcbea2012-02-17 18:50:32 +08003831 // Pop the shadow frame
3832 EmitPopShadowFrame();
3833
Logan Chien5bcc04e2012-01-30 14:15:12 +08003834 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003835 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003836 if (ret_shorty == 'V') {
3837 irb_.CreateRetVoid();
3838 } else {
3839 irb_.CreateRet(irb_.getJZero(ret_shorty));
3840 }
3841
3842 // Restore the orignal insert point for IRBuilder
3843 irb_.restoreIP(irb_ip_original);
3844
3845 return basic_block_unwind_;
3846}
3847
3848
Logan Chienc670a8d2011-12-20 21:25:56 +08003849llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3850 uint32_t reg_idx) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003851 // Get reg_type and reg_name from DalvikReg
3852 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3853 std::string reg_name;
3854
3855#if !defined(NDEBUG)
3856 StringAppendF(&reg_name, "%c%u", DalvikReg::GetRegCategoryNamePrefix(cat), reg_idx);
3857#endif
Logan Chienc670a8d2011-12-20 21:25:56 +08003858
3859 // Save current IR builder insert point
3860 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003861 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003862
3863 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003864 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003865
3866 // Restore IRBuilder insert point
3867 irb_.restoreIP(irb_ip_original);
3868
3869 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3870 return reg_addr;
3871}
3872
3873
TDYa1277f5b9be2012-04-29 01:31:49 -07003874llvm::Value* MethodCompiler::AllocShadowFrameEntry(uint32_t reg_idx) {
TDYa1271d7e5102012-05-13 09:27:05 -07003875 if (reg_to_shadow_frame_index_[reg_idx] == -1) {
3876 // This register dosen't need ShadowFrame entry
3877 return NULL;
3878 }
3879
TDYa127cc1b4c32012-05-15 07:31:37 -07003880 if (!method_info_.need_shadow_frame_entry) {
3881 return NULL;
3882 }
3883
TDYa12767ae8ff2012-05-02 19:08:02 -07003884 std::string reg_name;
3885
3886#if !defined(NDEBUG)
3887 StringAppendF(&reg_name, "o%u", reg_idx);
3888#endif
3889
TDYa1277f5b9be2012-04-29 01:31:49 -07003890 // Save current IR builder insert point
3891 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3892
3893 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
3894
3895 llvm::Value* gep_index[] = {
3896 irb_.getInt32(0), // No pointer displacement
3897 irb_.getInt32(1), // SIRT
TDYa1271d7e5102012-05-13 09:27:05 -07003898 irb_.getInt32(reg_to_shadow_frame_index_[reg_idx]) // Pointer field
TDYa1277f5b9be2012-04-29 01:31:49 -07003899 };
3900
TDYa12767ae8ff2012-05-02 19:08:02 -07003901 llvm::Value* reg_addr = irb_.CreateGEP(shadow_frame_, gep_index, reg_name);
TDYa1277f5b9be2012-04-29 01:31:49 -07003902
3903 // Restore IRBuilder insert point
3904 irb_.restoreIP(irb_ip_original);
3905
3906 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3907 return reg_addr;
3908}
3909
3910
Logan Chienc670a8d2011-12-20 21:25:56 +08003911llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
TDYa12767ae8ff2012-05-02 19:08:02 -07003912 // Get reg_type and reg_name from DalvikReg
3913 llvm::Type* reg_type = DalvikReg::GetRegCategoryEquivSizeTy(irb_, cat);
3914 std::string reg_name;
3915
3916#if !defined(NDEBUG)
3917 StringAppendF(&reg_name, "%c_res", DalvikReg::GetRegCategoryNamePrefix(cat));
3918#endif
3919
Logan Chienc670a8d2011-12-20 21:25:56 +08003920 // Save current IR builder insert point
3921 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
TDYa12767ae8ff2012-05-02 19:08:02 -07003922 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003923
3924 // Alloca
TDYa12767ae8ff2012-05-02 19:08:02 -07003925 llvm::Value* reg_addr = irb_.CreateAlloca(reg_type, 0, reg_name);
Logan Chienc670a8d2011-12-20 21:25:56 +08003926
3927 // Restore IRBuilder insert point
3928 irb_.restoreIP(irb_ip_original);
3929
3930 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3931 return reg_addr;
3932}
3933
3934
Logan Chien8dfcbea2012-02-17 18:50:32 +08003935void MethodCompiler::EmitPopShadowFrame() {
TDYa127cc1b4c32012-05-15 07:31:37 -07003936 if (!method_info_.need_shadow_frame) {
3937 return;
3938 }
Logan Chien8dfcbea2012-02-17 18:50:32 +08003939 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3940}
3941
3942
TDYa127c8dc1012012-04-19 07:03:33 -07003943void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
TDYa127cc1b4c32012-05-15 07:31:37 -07003944 if (!method_info_.need_shadow_frame) {
3945 return;
3946 }
TDYa127c8dc1012012-04-19 07:03:33 -07003947 irb_.StoreToObjectOffset(shadow_frame_,
3948 ShadowFrame::DexPCOffset(),
TDYa127aba61122012-05-04 18:28:36 -07003949 irb_.getInt32(dex_pc),
TDYa127d955bec2012-05-11 10:54:02 -07003950 kTBAAShadowFrame);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003951}
3952
3953
TDYa127cc1b4c32012-05-15 07:31:37 -07003954// TODO: Use high-level IR to do this
3955void MethodCompiler::ComputeMethodInfo() {
3956 // If this method is static, we set the "this" register index to -1. So we don't worry about this
3957 // method is static or not in the following comparison.
3958 int64_t this_reg_idx = (oat_compilation_unit_->IsStatic()) ?
3959 (-1) :
3960 (code_item_->registers_size_ - code_item_->ins_size_);
3961 bool has_invoke = false;
3962 bool may_have_loop = false;
3963 bool modify_this = false;
3964 bool may_throw_exception = false;
3965
3966 Instruction const* insn;
3967 for (uint32_t dex_pc = 0;
3968 dex_pc < code_item_->insns_size_in_code_units_;
3969 dex_pc += insn->SizeInCodeUnits()) {
3970 insn = Instruction::At(code_item_->insns_ + dex_pc);
3971 DecodedInstruction dec_insn(insn);
3972
3973 switch (insn->Opcode()) {
3974 case Instruction::NOP:
3975 break;
3976
3977 case Instruction::MOVE:
3978 case Instruction::MOVE_FROM16:
3979 case Instruction::MOVE_16:
3980 case Instruction::MOVE_WIDE:
3981 case Instruction::MOVE_WIDE_FROM16:
3982 case Instruction::MOVE_WIDE_16:
3983 case Instruction::MOVE_OBJECT:
3984 case Instruction::MOVE_OBJECT_FROM16:
3985 case Instruction::MOVE_OBJECT_16:
3986 case Instruction::MOVE_RESULT:
3987 case Instruction::MOVE_RESULT_WIDE:
3988 case Instruction::MOVE_RESULT_OBJECT:
3989 case Instruction::MOVE_EXCEPTION:
3990 if (dec_insn.vA == this_reg_idx) {
3991 modify_this = true;
3992 }
3993 break;
3994
3995 case Instruction::RETURN_VOID:
3996 case Instruction::RETURN:
3997 case Instruction::RETURN_WIDE:
3998 case Instruction::RETURN_OBJECT:
3999 break;
4000
4001 case Instruction::CONST_4:
4002 case Instruction::CONST_16:
4003 case Instruction::CONST:
4004 case Instruction::CONST_HIGH16:
4005 case Instruction::CONST_WIDE_16:
4006 case Instruction::CONST_WIDE_32:
4007 case Instruction::CONST_WIDE:
4008 case Instruction::CONST_WIDE_HIGH16:
4009 if (dec_insn.vA == this_reg_idx) {
4010 modify_this = true;
4011 }
4012 break;
4013
4014 case Instruction::CONST_STRING:
4015 case Instruction::CONST_STRING_JUMBO:
4016 // TODO: Will the ResolveString throw exception?
4017 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, dec_insn.vB)) {
4018 may_throw_exception = true;
4019 }
4020 if (dec_insn.vA == this_reg_idx) {
4021 modify_this = true;
4022 }
4023 break;
4024
4025 case Instruction::CONST_CLASS:
4026 may_throw_exception = true;
4027 if (dec_insn.vA == this_reg_idx) {
4028 modify_this = true;
4029 }
4030 break;
4031
4032 case Instruction::MONITOR_ENTER:
4033 case Instruction::MONITOR_EXIT:
4034 case Instruction::CHECK_CAST:
4035 may_throw_exception = true;
4036 break;
4037
4038 case Instruction::INSTANCE_OF:
4039 may_throw_exception = true;
4040 if (dec_insn.vA == this_reg_idx) {
4041 modify_this = true;
4042 }
4043 break;
4044
4045 case Instruction::ARRAY_LENGTH:
4046 if (dec_insn.vA == this_reg_idx) {
4047 modify_this = true;
4048 }
4049 break;
4050
4051 case Instruction::NEW_INSTANCE:
4052 case Instruction::NEW_ARRAY:
4053 may_throw_exception = true;
4054 if (dec_insn.vA == this_reg_idx) {
4055 modify_this = true;
4056 }
4057 break;
4058
4059 case Instruction::FILLED_NEW_ARRAY:
4060 case Instruction::FILLED_NEW_ARRAY_RANGE:
4061 case Instruction::FILL_ARRAY_DATA:
4062 case Instruction::THROW:
4063 may_throw_exception = true;
4064 break;
4065
4066 case Instruction::GOTO:
4067 case Instruction::GOTO_16:
4068 case Instruction::GOTO_32:
4069 {
4070 int32_t branch_offset = dec_insn.vA;
4071 if (branch_offset <= 0) {
4072 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4073 // According to the statistics, there are a few methods have "fake" back edge, which means
4074 // the backedge is not for a loop.
4075 // The most "fake" edges in the small methods are just branches to a return instruction. So
4076 // we can do an simple check to avoid false loop detection. After we have a high-level IR
4077 // before IRBuilder, we should remove this trick.
4078 switch (target->Opcode()) {
4079 default:
4080 may_have_loop = true;
4081 break;
4082 case Instruction::RETURN_VOID:
4083 case Instruction::RETURN:
4084 case Instruction::RETURN_WIDE:
4085 case Instruction::RETURN_OBJECT:
4086 break;
4087 }
4088 }
4089 }
4090 break;
4091
4092 case Instruction::PACKED_SWITCH:
4093 case Instruction::SPARSE_SWITCH:
4094 break;
4095
4096 case Instruction::CMPL_FLOAT:
4097 case Instruction::CMPG_FLOAT:
4098 case Instruction::CMPL_DOUBLE:
4099 case Instruction::CMPG_DOUBLE:
4100 case Instruction::CMP_LONG:
4101 if (dec_insn.vA == this_reg_idx) {
4102 modify_this = true;
4103 }
4104 break;
4105
4106 case Instruction::IF_EQ:
4107 case Instruction::IF_NE:
4108 case Instruction::IF_LT:
4109 case Instruction::IF_GE:
4110 case Instruction::IF_GT:
4111 case Instruction::IF_LE:
4112 {
4113 int32_t branch_offset = dec_insn.vC;
4114 if (branch_offset <= 0) {
4115 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4116 switch (target->Opcode()) {
4117 default:
4118 may_have_loop = true;
4119 break;
4120 case Instruction::RETURN_VOID:
4121 case Instruction::RETURN:
4122 case Instruction::RETURN_WIDE:
4123 case Instruction::RETURN_OBJECT:
4124 break;
4125 }
4126 }
4127 }
4128 break;
4129
4130 case Instruction::IF_EQZ:
4131 case Instruction::IF_NEZ:
4132 case Instruction::IF_LTZ:
4133 case Instruction::IF_GEZ:
4134 case Instruction::IF_GTZ:
4135 case Instruction::IF_LEZ:
4136 {
4137 int32_t branch_offset = dec_insn.vB;
4138 if (branch_offset <= 0) {
4139 Instruction const* target = Instruction::At(code_item_->insns_ + dex_pc + branch_offset);
4140 switch (target->Opcode()) {
4141 default:
4142 may_have_loop = true;
4143 break;
4144 case Instruction::RETURN_VOID:
4145 case Instruction::RETURN:
4146 case Instruction::RETURN_WIDE:
4147 case Instruction::RETURN_OBJECT:
4148 break;
4149 }
4150 }
4151 }
4152 break;
4153
4154 case Instruction::AGET:
4155 case Instruction::AGET_WIDE:
4156 case Instruction::AGET_OBJECT:
4157 case Instruction::AGET_BOOLEAN:
4158 case Instruction::AGET_BYTE:
4159 case Instruction::AGET_CHAR:
4160 case Instruction::AGET_SHORT:
4161 may_throw_exception = true;
4162 if (dec_insn.vA == this_reg_idx) {
4163 modify_this = true;
4164 }
4165 break;
4166
4167 case Instruction::APUT:
4168 case Instruction::APUT_WIDE:
4169 case Instruction::APUT_OBJECT:
4170 case Instruction::APUT_BOOLEAN:
4171 case Instruction::APUT_BYTE:
4172 case Instruction::APUT_CHAR:
4173 case Instruction::APUT_SHORT:
4174 may_throw_exception = true;
4175 break;
4176
4177 case Instruction::IGET:
4178 case Instruction::IGET_WIDE:
4179 case Instruction::IGET_OBJECT:
4180 case Instruction::IGET_BOOLEAN:
4181 case Instruction::IGET_BYTE:
4182 case Instruction::IGET_CHAR:
4183 case Instruction::IGET_SHORT:
4184 {
4185 if (dec_insn.vA == this_reg_idx) {
4186 modify_this = true;
4187 }
4188 uint32_t reg_idx = dec_insn.vB;
4189 uint32_t field_idx = dec_insn.vC;
4190 int field_offset;
4191 bool is_volatile;
4192 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4193 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
4194 if (!is_fast_path) {
4195 may_throw_exception = true;
4196 } else if (reg_idx != this_reg_idx) {
4197 // NullPointerException
4198 may_throw_exception = true;
4199 }
4200 }
4201 break;
4202
4203 case Instruction::IPUT:
4204 case Instruction::IPUT_WIDE:
4205 case Instruction::IPUT_OBJECT:
4206 case Instruction::IPUT_BOOLEAN:
4207 case Instruction::IPUT_BYTE:
4208 case Instruction::IPUT_CHAR:
4209 case Instruction::IPUT_SHORT:
4210 {
4211 uint32_t reg_idx = dec_insn.vB;
4212 uint32_t field_idx = dec_insn.vC;
4213 int field_offset;
4214 bool is_volatile;
4215 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
4216 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
4217 if (!is_fast_path) {
4218 may_throw_exception = true;
4219 } else if (reg_idx != this_reg_idx) {
4220 // NullPointerException
4221 may_throw_exception = true;
4222 }
4223 }
4224 break;
4225
4226 case Instruction::SGET:
4227 case Instruction::SGET_WIDE:
4228 case Instruction::SGET_OBJECT:
4229 case Instruction::SGET_BOOLEAN:
4230 case Instruction::SGET_BYTE:
4231 case Instruction::SGET_CHAR:
4232 case Instruction::SGET_SHORT:
4233 {
4234 if (dec_insn.vA == this_reg_idx) {
4235 modify_this = true;
4236 }
4237 uint32_t field_idx = dec_insn.vB;
4238
4239 int field_offset;
4240 int ssb_index;
4241 bool is_referrers_class;
4242 bool is_volatile;
4243
4244 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4245 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4246 is_referrers_class, is_volatile, false);
4247 if (!is_fast_path || !is_referrers_class) {
4248 may_throw_exception = true;
4249 }
4250 }
4251 break;
4252
4253 case Instruction::SPUT:
4254 case Instruction::SPUT_WIDE:
4255 case Instruction::SPUT_OBJECT:
4256 case Instruction::SPUT_BOOLEAN:
4257 case Instruction::SPUT_BYTE:
4258 case Instruction::SPUT_CHAR:
4259 case Instruction::SPUT_SHORT:
4260 {
4261 uint32_t field_idx = dec_insn.vB;
4262
4263 int field_offset;
4264 int ssb_index;
4265 bool is_referrers_class;
4266 bool is_volatile;
4267
4268 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
4269 field_idx, oat_compilation_unit_, field_offset, ssb_index,
4270 is_referrers_class, is_volatile, true);
4271 if (!is_fast_path || !is_referrers_class) {
4272 may_throw_exception = true;
4273 }
4274 }
4275 break;
4276
4277
4278 case Instruction::INVOKE_VIRTUAL:
4279 case Instruction::INVOKE_SUPER:
4280 case Instruction::INVOKE_DIRECT:
4281 case Instruction::INVOKE_STATIC:
4282 case Instruction::INVOKE_INTERFACE:
4283 case Instruction::INVOKE_VIRTUAL_RANGE:
4284 case Instruction::INVOKE_SUPER_RANGE:
4285 case Instruction::INVOKE_DIRECT_RANGE:
4286 case Instruction::INVOKE_STATIC_RANGE:
4287 case Instruction::INVOKE_INTERFACE_RANGE:
4288 has_invoke = true;
4289 may_throw_exception = true;
4290 break;
4291
4292 case Instruction::NEG_INT:
4293 case Instruction::NOT_INT:
4294 case Instruction::NEG_LONG:
4295 case Instruction::NOT_LONG:
4296 case Instruction::NEG_FLOAT:
4297 case Instruction::NEG_DOUBLE:
4298 case Instruction::INT_TO_LONG:
4299 case Instruction::INT_TO_FLOAT:
4300 case Instruction::INT_TO_DOUBLE:
4301 case Instruction::LONG_TO_INT:
4302 case Instruction::LONG_TO_FLOAT:
4303 case Instruction::LONG_TO_DOUBLE:
4304 case Instruction::FLOAT_TO_INT:
4305 case Instruction::FLOAT_TO_LONG:
4306 case Instruction::FLOAT_TO_DOUBLE:
4307 case Instruction::DOUBLE_TO_INT:
4308 case Instruction::DOUBLE_TO_LONG:
4309 case Instruction::DOUBLE_TO_FLOAT:
4310 case Instruction::INT_TO_BYTE:
4311 case Instruction::INT_TO_CHAR:
4312 case Instruction::INT_TO_SHORT:
4313 case Instruction::ADD_INT:
4314 case Instruction::SUB_INT:
4315 case Instruction::MUL_INT:
4316 case Instruction::AND_INT:
4317 case Instruction::OR_INT:
4318 case Instruction::XOR_INT:
4319 case Instruction::SHL_INT:
4320 case Instruction::SHR_INT:
4321 case Instruction::USHR_INT:
4322 case Instruction::ADD_LONG:
4323 case Instruction::SUB_LONG:
4324 case Instruction::MUL_LONG:
4325 case Instruction::AND_LONG:
4326 case Instruction::OR_LONG:
4327 case Instruction::XOR_LONG:
4328 case Instruction::SHL_LONG:
4329 case Instruction::SHR_LONG:
4330 case Instruction::USHR_LONG:
4331 case Instruction::ADD_INT_2ADDR:
4332 case Instruction::SUB_INT_2ADDR:
4333 case Instruction::MUL_INT_2ADDR:
4334 case Instruction::AND_INT_2ADDR:
4335 case Instruction::OR_INT_2ADDR:
4336 case Instruction::XOR_INT_2ADDR:
4337 case Instruction::SHL_INT_2ADDR:
4338 case Instruction::SHR_INT_2ADDR:
4339 case Instruction::USHR_INT_2ADDR:
4340 case Instruction::ADD_LONG_2ADDR:
4341 case Instruction::SUB_LONG_2ADDR:
4342 case Instruction::MUL_LONG_2ADDR:
4343 case Instruction::AND_LONG_2ADDR:
4344 case Instruction::OR_LONG_2ADDR:
4345 case Instruction::XOR_LONG_2ADDR:
4346 case Instruction::SHL_LONG_2ADDR:
4347 case Instruction::SHR_LONG_2ADDR:
4348 case Instruction::USHR_LONG_2ADDR:
4349 if (dec_insn.vA == this_reg_idx) {
4350 modify_this = true;
4351 }
4352 break;
4353
4354 case Instruction::DIV_INT:
4355 case Instruction::REM_INT:
4356 case Instruction::DIV_LONG:
4357 case Instruction::REM_LONG:
4358 case Instruction::DIV_INT_2ADDR:
4359 case Instruction::REM_INT_2ADDR:
4360 case Instruction::DIV_LONG_2ADDR:
4361 case Instruction::REM_LONG_2ADDR:
4362 may_throw_exception = true;
4363 if (dec_insn.vA == this_reg_idx) {
4364 modify_this = true;
4365 }
4366 break;
4367
4368 case Instruction::ADD_FLOAT:
4369 case Instruction::SUB_FLOAT:
4370 case Instruction::MUL_FLOAT:
4371 case Instruction::DIV_FLOAT:
4372 case Instruction::REM_FLOAT:
4373 case Instruction::ADD_DOUBLE:
4374 case Instruction::SUB_DOUBLE:
4375 case Instruction::MUL_DOUBLE:
4376 case Instruction::DIV_DOUBLE:
4377 case Instruction::REM_DOUBLE:
4378 case Instruction::ADD_FLOAT_2ADDR:
4379 case Instruction::SUB_FLOAT_2ADDR:
4380 case Instruction::MUL_FLOAT_2ADDR:
4381 case Instruction::DIV_FLOAT_2ADDR:
4382 case Instruction::REM_FLOAT_2ADDR:
4383 case Instruction::ADD_DOUBLE_2ADDR:
4384 case Instruction::SUB_DOUBLE_2ADDR:
4385 case Instruction::MUL_DOUBLE_2ADDR:
4386 case Instruction::DIV_DOUBLE_2ADDR:
4387 case Instruction::REM_DOUBLE_2ADDR:
4388 if (dec_insn.vA == this_reg_idx) {
4389 modify_this = true;
4390 }
4391 break;
4392
4393 case Instruction::ADD_INT_LIT16:
4394 case Instruction::ADD_INT_LIT8:
4395 case Instruction::RSUB_INT:
4396 case Instruction::RSUB_INT_LIT8:
4397 case Instruction::MUL_INT_LIT16:
4398 case Instruction::MUL_INT_LIT8:
4399 case Instruction::AND_INT_LIT16:
4400 case Instruction::AND_INT_LIT8:
4401 case Instruction::OR_INT_LIT16:
4402 case Instruction::OR_INT_LIT8:
4403 case Instruction::XOR_INT_LIT16:
4404 case Instruction::XOR_INT_LIT8:
4405 case Instruction::SHL_INT_LIT8:
4406 case Instruction::SHR_INT_LIT8:
4407 case Instruction::USHR_INT_LIT8:
4408 if (dec_insn.vA == this_reg_idx) {
4409 modify_this = true;
4410 }
4411 break;
4412 case Instruction::DIV_INT_LIT16:
4413 case Instruction::DIV_INT_LIT8:
4414 case Instruction::REM_INT_LIT16:
4415 case Instruction::REM_INT_LIT8:
4416 if (dec_insn.vC == 0) {
4417 may_throw_exception = true;
4418 }
4419 if (dec_insn.vA == this_reg_idx) {
4420 modify_this = true;
4421 }
4422 break;
4423
4424 case Instruction::THROW_VERIFICATION_ERROR:
4425 may_throw_exception = true;
4426 break;
4427
4428 case Instruction::UNUSED_3E:
4429 case Instruction::UNUSED_3F:
4430 case Instruction::UNUSED_40:
4431 case Instruction::UNUSED_41:
4432 case Instruction::UNUSED_42:
4433 case Instruction::UNUSED_43:
4434 case Instruction::UNUSED_73:
4435 case Instruction::UNUSED_79:
4436 case Instruction::UNUSED_7A:
4437 case Instruction::UNUSED_E3:
4438 case Instruction::UNUSED_E4:
4439 case Instruction::UNUSED_E5:
4440 case Instruction::UNUSED_E6:
4441 case Instruction::UNUSED_E7:
4442 case Instruction::UNUSED_E8:
4443 case Instruction::UNUSED_E9:
4444 case Instruction::UNUSED_EA:
4445 case Instruction::UNUSED_EB:
4446 case Instruction::UNUSED_EC:
4447 case Instruction::UNUSED_EE:
4448 case Instruction::UNUSED_EF:
4449 case Instruction::UNUSED_F0:
4450 case Instruction::UNUSED_F1:
4451 case Instruction::UNUSED_F2:
4452 case Instruction::UNUSED_F3:
4453 case Instruction::UNUSED_F4:
4454 case Instruction::UNUSED_F5:
4455 case Instruction::UNUSED_F6:
4456 case Instruction::UNUSED_F7:
4457 case Instruction::UNUSED_F8:
4458 case Instruction::UNUSED_F9:
4459 case Instruction::UNUSED_FA:
4460 case Instruction::UNUSED_FB:
4461 case Instruction::UNUSED_FC:
4462 case Instruction::UNUSED_FD:
4463 case Instruction::UNUSED_FE:
4464 case Instruction::UNUSED_FF:
4465 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
4466 break;
4467 }
4468 }
4469
4470 method_info_.this_reg_idx = this_reg_idx;
4471 // According to the statistics, there are few methods that modify the "this" pointer. So this is a
4472 // simple way to avoid data flow analysis. After we have a high-level IR before IRBuilder, we
4473 // should remove this trick.
4474 method_info_.this_will_not_be_null = !modify_this;
4475 method_info_.has_invoke = has_invoke;
4476 // If this method has loop or invoke instruction, it may suspend. Thus we need a shadow frame entry
4477 // for GC.
4478 method_info_.need_shadow_frame_entry = has_invoke || may_have_loop;
4479 // If this method may throw an exception, we need a shadow frame for stack trace (dexpc).
4480 method_info_.need_shadow_frame = method_info_.need_shadow_frame_entry || may_throw_exception;
4481}
4482
4483
4484
Logan Chien83426162011-12-09 09:29:50 +08004485} // namespace compiler_llvm
4486} // namespace art